redesign layout dimension change window

This commit is contained in:
garak
2024-11-12 01:09:43 -05:00
committed by t
parent 6dcbe45051
commit 54e41b0c20
13 changed files with 854 additions and 107 deletions

View File

@@ -5,12 +5,13 @@
#include <QPainter>
#include <QRgb>
class MovableRect : public QGraphicsItem
class MovableRect : public QGraphicsRectItem
{
public:
MovableRect(bool *enabled, int width, int height, QRgb color);
QRectF boundingRect() const override
{
QRectF boundingRect() const override {
qreal penWidth = 4;
return QRectF(-penWidth,
-penWidth,
@@ -18,21 +19,62 @@ public:
20 * 8 + penWidth * 2);
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override
{
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override {
if (!(*enabled)) return;
painter->setPen(this->color);
painter->drawRect(x() - 2, y() - 2, this->width + 3, this->height + 3);
painter->drawRect(this->rect().x() - 2, this->rect().y() - 2, this->rect().width() + 3, this->rect().height() + 3);
painter->setPen(QColor(0, 0, 0));
painter->drawRect(x() - 3, y() - 3, this->width + 5, this->height + 5);
painter->drawRect(x() - 1, y() - 1, this->width + 1, this->height + 1);
painter->drawRect(this->rect().x() - 3, this->rect().y() - 3, this->rect().width() + 5, this->rect().height() + 5);
painter->drawRect(this->rect().x() - 1, this->rect().y() - 1, this->rect().width() + 1, this->rect().height() + 1);
}
void updateLocation(int x, int y);
bool *enabled;
private:
int width;
int height;
protected:
QRgb color;
};
/// A MovableRect with the addition of being resizable.
class ResizableRect : public QObject, public MovableRect
{
Q_OBJECT
public:
ResizableRect(QObject *parent, bool *enabled, int width, int height, QRgb color);
QRectF boundingRect() const override {
return QRectF(this->rect() + QMargins(lineWidth, lineWidth, lineWidth, lineWidth));
}
QPainterPath shape() const override {
QPainterPath path;
path.addRect(this->rect() + QMargins(lineWidth, lineWidth, lineWidth, lineWidth));
path.addRect(this->rect() - QMargins(lineWidth, lineWidth, lineWidth, lineWidth));
return path;
}
void updatePosFromRect(QRect newPos);
protected:
void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override;
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
private:
enum class Edge { None, Left, Right, Top, Bottom, TopLeft, BottomLeft, TopRight, BottomRight };
ResizableRect::Edge detectEdge(int x, int y);
// Variables for keeping state of original rect while resizing
ResizableRect::Edge clickedEdge = ResizableRect::Edge::None;
QPointF clickedPos = QPointF();
QRect clickedRect;
int lineWidth = 8;
signals:
void rectUpdated(QRect rect);
};
#endif // MOVABLERECT_H

View File

@@ -12,6 +12,8 @@ public:
void wheelEvent(QWheelEvent *event) override;
void focusOutEvent(QFocusEvent *event) override;
void setLineEditEnabled(bool enabled);
unsigned getActionId();
private:

View File

@@ -0,0 +1,106 @@
#ifndef RESIZELAYOUTPOPUP_H
#define RESIZELAYOUTPOPUP_H
#include <QDialog>
#include <QPointer>
#include <QGraphicsScene>
#include <QGraphicsLineItem>
#include <QGraphicsRectItem>
class ResizableRect;
class Editor;
namespace Ui {
class ResizeLayoutPopup;
}
/// Custom scene that paints its background a gray checkered pattern.
/// Additionally there is a definable "valid" area which will paint the checkerboard green inside.
class CheckeredBgScene : public QGraphicsScene {
Q_OBJECT
public:
CheckeredBgScene(QObject *parent = nullptr);
void setValidRect(int x, int y, int width, int height) {
this->validRect = QRect(x * this->gridSize, y * this->gridSize, width * this->gridSize, height * this->gridSize);
}
void setValidRect(QRect rect) {
this->validRect = rect;
}
protected:
void drawBackground(QPainter *painter, const QRectF &rect) override;
private:
int gridSize = 16; // virtual pixels
QRect validRect = QRect();
};
/// PixmapItem subclass which allows for creating a boundary which determine whether
/// the pixmap paints normally or with a black tint.
/// This item is movable and snaps on a 16x16 grid.
class BoundedPixmapItem : public QGraphicsPixmapItem {
public:
BoundedPixmapItem(const QPixmap &pixmap, QGraphicsItem *parent = nullptr);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override;
void setBoundary(ResizableRect *rect) { this->boundary = rect; }
protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
private:
ResizableRect *boundary = nullptr;
QPointF clickedPos = QPointF();
};
/// The main (modal) dialog window for resizing layout and border dimensions.
/// The dialog itself is minimal, and is connected to the parent widget's geometry.
class ResizeLayoutPopup : public QDialog
{
Q_OBJECT
public:
ResizeLayoutPopup(QWidget *parent, Editor *editor);
~ResizeLayoutPopup();
void setupLayoutView();
void resetPosition();
QMargins getResult();
QSize getBorderResult();
protected:
void moveEvent(QMoveEvent *) override {
// Prevent the dialog from being moved
this->resetPosition();
}
void resizeEvent(QResizeEvent *) override {
// Prevent the dialog from being resized
this->resetPosition();
}
private slots:
void on_spinBox_width_valueChanged(int value);
void on_spinBox_height_valueChanged(int value);
private:
QWidget *parent = nullptr;
Editor *editor = nullptr;
Ui::ResizeLayoutPopup *ui;
ResizableRect *outline = nullptr;
BoundedPixmapItem *layoutPixmap = nullptr;
QPointer<CheckeredBgScene> scene = nullptr;
};
#endif // RESIZELAYOUTPOPUP_H