Swiftray  1.0
transform.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <QHoverEvent>
4 #include <QMouseEvent>
6 #include <document.h>
7 #include <cmath>
8 #include <limits>
9 #include <shape/shape.h>
10 
11 namespace Controls {
12 
13  class Transform : public CanvasControl {
14  Q_OBJECT
15  public:
16  enum class Control {
17  NONE = -1,
18  NW = 0,
19  N = 1,
20  NE = 2,
21  E = 3,
22  SE = 4,
23  S = 5,
24  SW = 6,
25  W = 7,
26  ROTATION = 8
27  };
28 
29  Transform(Canvas *canvas) noexcept;
30 
31  bool keyPressEvent(QKeyEvent *e) override;
32 
33  bool hoverEvent(QHoverEvent *e, Qt::CursorShape *cursor) override;
34 
35  bool mousePressEvent(QMouseEvent *e) override;
36 
37  bool mouseReleaseEvent(QMouseEvent *e) override;
38 
39  bool mouseMoveEvent(QMouseEvent *e) override;
40 
41  void paint(QPainter *painter) override;
42 
43  bool isActive() override;
44 
45  void reset();
46 
47  const QPointF *controlPoints();
48 
49  QRectF boundingRect();
50 
51  QList<ShapePtr> &selections();
52 
53  void calcScale(QPointF canvas_coord);
54 
55  void applyMove(bool temporarily = false);
56 
57  double rotation() {
58  return bbox_angle_;
59  }
60 
61  void updateTransform(double new_x, double new_y, double new_r, double new_w, double new_h) {
62  if (abs(new_x - x()) > 0.01 || abs(new_y - y()) > 0.01) {
63  translate_to_apply_ = QPointF(new_x - x(), new_y - y());
64  applyMove();
65  }
66  if (abs(new_r - rotation()) > 0.01) {
67  rotation_to_apply_ = new_r - rotation();
68  applyRotate(boundingRect().center(), rotation_to_apply_);
69  }
70  if (abs(new_w - width()) > 0.01 || abs(new_h - height()) > 0.01) {
71  scale_x_to_apply_ = new_w / width();
72  scale_y_to_apply_ = new_h / height();
73  applyScale(boundingRect().center(), scale_x_to_apply_, scale_y_to_apply_);
74  }
75  }
76 
77  double x() {
78  return boundingRect().center().x();
79  }
80 
81  double y() {
82  return boundingRect().center().y();
83  }
84 
85  double width() {
86  return boundingRect().width();
87  }
88 
89  double height() {
90  return boundingRect().height();
91  }
92 
93  bool isScaleLock() const;
94 
95  void setScaleLock(bool scale_lock);
96 
97  void applyScale(QPointF center, double scale_x, double scale_y, bool temporarily = false);
98 
99  private:
100  void applyRotate(QPointF center, double rotation, bool temporarily = false);
101 
102  Control hitTest(QPointF clickPoint, float tolerance);
103 
104  QPointF controls_[8];
105  Control active_control_;
106  QPointF action_center_;
107  QRectF bounding_rect_;
108 
109  QList<ShapePtr> selections_;
110 
111  double scale_x_to_apply_;
112  double scale_y_to_apply_;
113  double rotation_to_apply_;
114  QPointF translate_to_apply_;
115 
116  QPointF cursor_;
117 
118  double rotated_from_;
119  QSizeF transformed_from_;
120 
121  qreal bbox_angle_;
122  bool bbox_need_recalc_;
123 
124  bool scale_locked_;
125  public slots:
126 
127  void updateSelections();
128 
129  void updateBoundingRect();
130  };
131 
132 }
Definition: canvas.h:27
Definition: canvas-control.h:16
Canvas & canvas()
Definition: canvas-control.cpp:34
Definition: transform.h:13
bool keyPressEvent(QKeyEvent *e) override
Definition: transform.cpp:400
void updateSelections()
Definition: transform.cpp:32
QRectF boundingRect()
Definition: transform.cpp:92
void applyScale(QPointF center, double scale_x, double scale_y, bool temporarily=false)
Definition: transform.cpp:128
bool isActive() override
Definition: transform.cpp:22
bool mouseReleaseEvent(QMouseEvent *e) override
Definition: transform.cpp:247
const QPointF * controlPoints()
Definition: transform.cpp:176
Transform(Canvas *canvas) noexcept
Definition: transform.cpp:10
void updateTransform(double new_x, double new_y, double new_r, double new_w, double new_h)
Definition: transform.h:61
double x()
Definition: transform.h:77
void paint(QPainter *painter) override
Definition: transform.cpp:375
double y()
Definition: transform.h:81
double rotation()
Definition: transform.h:57
Control
Definition: transform.h:16
double width()
Definition: transform.h:85
bool mousePressEvent(QMouseEvent *e) override
Definition: transform.cpp:226
QList< ShapePtr > & selections()
Definition: transform.cpp:30
void reset()
Definition: transform.cpp:393
bool hoverEvent(QHoverEvent *e, Qt::CursorShape *cursor) override
Definition: transform.cpp:339
void setScaleLock(bool scale_lock)
Definition: transform.cpp:426
void applyMove(bool temporarily=false)
Definition: transform.cpp:155
double height()
Definition: transform.h:89
bool mouseMoveEvent(QMouseEvent *e) override
Definition: transform.cpp:304
void calcScale(QPointF canvas_coord)
Definition: transform.cpp:269
bool isScaleLock() const
Definition: transform.cpp:422
void updateBoundingRect()
Definition: transform.cpp:39
In-canvas controls with its own painting and event handling functions.
Definition: canvas-control.h:8