summaryrefslogtreecommitdiff
path: root/utils/themeeditor/graphics/rbmovable.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'utils/themeeditor/graphics/rbmovable.cpp')
-rw-r--r--utils/themeeditor/graphics/rbmovable.cpp223
1 files changed, 219 insertions, 4 deletions
diff --git a/utils/themeeditor/graphics/rbmovable.cpp b/utils/themeeditor/graphics/rbmovable.cpp
index 3eca8bd18c..5d493c61e7 100644
--- a/utils/themeeditor/graphics/rbmovable.cpp
+++ b/utils/themeeditor/graphics/rbmovable.cpp
@@ -19,13 +19,16 @@
19 * 19 *
20 ****************************************************************************/ 20 ****************************************************************************/
21 21
22#include <QGraphicsSceneMouseEvent>
22#include <QPainter> 23#include <QPainter>
23#include <QDebug> 24#include <QDebug>
24 25
25#include "rbmovable.h" 26#include "rbmovable.h"
26 27
28const double RBMovable::handleSize = 7;
29
27RBMovable::RBMovable(QGraphicsItem* parent) 30RBMovable::RBMovable(QGraphicsItem* parent)
28 : QGraphicsItem(parent), geomChanged(false) 31 : QGraphicsItem(parent), dragMode(None)
29{ 32{
30 setFlags(ItemIsMovable | ItemIsSelectable | ItemSendsGeometryChanges); 33 setFlags(ItemIsMovable | ItemIsSelectable | ItemSendsGeometryChanges);
31} 34}
@@ -45,6 +48,11 @@ void RBMovable::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
45 pen.setColor(Qt::green); 48 pen.setColor(Qt::green);
46 painter->setPen(pen); 49 painter->setPen(pen);
47 painter->drawRect(boundingRect()); 50 painter->drawRect(boundingRect());
51
52 painter->fillRect(topLeftHandle(), Qt::green);
53 painter->fillRect(topRightHandle(), Qt::green);
54 painter->fillRect(bottomLeftHandle(), Qt::green);
55 painter->fillRect(bottomRightHandle(), Qt::green);
48 } 56 }
49} 57}
50 58
@@ -61,7 +69,6 @@ QVariant RBMovable::itemChange(GraphicsItemChange change, const QVariant &value)
61 pos.setY(qMax(0., pos.y())); 69 pos.setY(qMax(0., pos.y()));
62 pos.setY(qMin(pos.y(), bound.height() - boundingRect().height())); 70 pos.setY(qMin(pos.y(), bound.height() - boundingRect().height()));
63 71
64 geomChanged = true;
65 72
66 return pos; 73 return pos;
67 } 74 }
@@ -71,15 +78,223 @@ QVariant RBMovable::itemChange(GraphicsItemChange change, const QVariant &value)
71 78
72void RBMovable::mousePressEvent(QGraphicsSceneMouseEvent *event) 79void RBMovable::mousePressEvent(QGraphicsSceneMouseEvent *event)
73{ 80{
74 QGraphicsItem::mousePressEvent(event); 81 if(!isSelected())
82 {
83 QGraphicsItem::mousePressEvent(event);
84 return;
85 }
86
87 if(topLeftHandle().contains(event->pos()))
88 {
89 dragStartClick = event->pos() + pos();
90 dragStartPos = pos();
91 dragStartSize = boundingRect();
92
93 dWidthMin = -1. * pos().x();
94 dWidthMax = boundingRect().width() - childrenBoundingRect().right();
95
96 dHeightMin = -1. * pos().y();
97 dHeightMax = boundingRect().height() - childrenBoundingRect().bottom();
98
99 dragMode = TopLeft;
100 }
101 else if(topRightHandle().contains(event->pos()))
102 {
103 dragStartClick = event->pos() + pos();
104 dragStartPos = pos();
105 dragStartSize = boundingRect();
106
107 dWidthMin = childrenBoundingRect().width() - boundingRect().width();
108 dWidthMin = qMax(dWidthMin, -1. * size.width());
109 dWidthMax = parentItem()->boundingRect().width()
110 - boundingRect().width() - pos().x();
111
112 dHeightMin = -1. * pos().y();
113 dHeightMax = boundingRect().height() - childrenBoundingRect().bottom();
114 dHeightMax = qMin(dHeightMax, boundingRect().height());
115
116 dragMode = TopRight;
117 }
118 else if(bottomLeftHandle().contains(event->pos()))
119 {
120 dragStartClick = event->pos() + pos();
121 dragStartPos = pos();
122 dragStartSize = boundingRect();
123
124 dWidthMin = -1. * pos().x();
125 dWidthMax = boundingRect().width() - childrenBoundingRect().right();
126 dWidthMax = qMin(dWidthMax, size.width());
127
128 dHeightMin = -1. * (boundingRect().height()
129 - childrenBoundingRect().bottom());
130 dHeightMin = qMax(dHeightMin, -1. * boundingRect().height());
131
132 dragMode = BottomLeft;
133 }
134 else if(bottomRightHandle().contains(event->pos()))
135 {
136 dragStartClick = event->pos() + pos();
137 dragStartPos = pos();
138 dragStartSize = boundingRect();
139
140 dWidthMin = -1. * (boundingRect().width()
141 - childrenBoundingRect().right());
142 dWidthMin = qMax(dWidthMin, -1. * boundingRect().width());
143 dWidthMax = parentItem()->boundingRect().width() -
144 boundingRect().width() - pos().x();
145
146 dHeightMin = -1. * (boundingRect().height()
147 - childrenBoundingRect().bottom());
148 dHeightMin = qMax(dHeightMin, -1. * boundingRect().height());
149 dHeightMax = parentItem()->boundingRect().height() -
150 boundingRect().height() - pos().y();
151
152 dragMode = BottomRight;
153 }
154 else
155 {
156 QGraphicsItem::mousePressEvent(event);
157 }
158}
159
160void RBMovable::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
161{
162
163 QPointF absPos;
164 QPointF dPos;
165 QPointF dMouse;
166 switch(dragMode)
167 {
168 case None:
169 QGraphicsItem::mouseMoveEvent(event);
170 break;
171
172 case TopLeft:
173 /* Dragging from the top left corner */
174 absPos = event->pos() + pos();
175 dMouse = absPos - dragStartClick;
176
177 dPos.setX(qMin(dMouse.x(), dWidthMax));
178 dPos.setX(qMax(dPos.x(), dWidthMin));
179
180 dPos.setY(qMin(dMouse.y(), dHeightMax));
181 dPos.setY(qMax(dPos.y(), dHeightMin));
182
183 prepareGeometryChange();
184
185 setPos(dragStartPos + dPos);
186
187 size.setWidth(dragStartSize.width() - dPos.x());
188 size.setHeight(dragStartSize.height() - dPos.y());
189
190 break;
191
192 case TopRight:
193 /* Dragging from the top right corner */
194 absPos = event->pos() + pos();
195 dMouse = absPos - dragStartClick;
196
197 dPos.setX(qMin(dMouse.x(), dWidthMax));
198 dPos.setX(qMax(dPos.x(), dWidthMin));
199
200 dPos.setY(qMin(dMouse.y(), dHeightMax));
201 dPos.setY(qMax(dPos.y(), dHeightMin));
202
203 prepareGeometryChange();
204
205 setPos(dragStartPos.x(), dragStartPos.y() + dPos.y());
206
207 size.setWidth(dragStartSize.width() + dPos.x());
208 size.setHeight(dragStartSize.height() - dPos.y());
209
210 break;
211
212 case BottomLeft:
213 /* Dragging from the bottom left corner */
214 absPos = event->pos() + pos();
215 dMouse = absPos - dragStartClick;
216
217 dPos.setX(qMin(dMouse.x(), dWidthMax));
218 dPos.setX(qMax(dPos.x(), dWidthMin));
219
220 dPos.setY(qMin(dMouse.y(), dHeightMax));
221 dPos.setY(qMax(dPos.y(), dHeightMin));
222
223 prepareGeometryChange();
224 setPos(dragStartPos.x() + dPos.x(), dragStartPos.y());
225 size.setHeight(dragStartSize.height() + dPos.y());
226 size.setWidth(dragStartSize.width() - dPos.x());
227
228 break;
229
230 case BottomRight:
231 /* Dragging from the bottom right corner */
232 absPos = event->pos() + pos();
233 dMouse = absPos - dragStartClick;
234
235 dPos.setX(qMin(dMouse.x(), dWidthMax));
236 dPos.setX(qMax(dPos.x(), dWidthMin));
237
238 dPos.setY(qMin(dMouse.y(), dHeightMax));
239 dPos.setY(qMax(dPos.y(), dHeightMin));
240
241 prepareGeometryChange();
242
243 size.setWidth(dragStartSize.width() + dPos.x());
244 size.setHeight(dragStartSize.height() + dPos.y());
245
246 break;
247 }
75} 248}
76 249
77void RBMovable::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) 250void RBMovable::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
78{ 251{
79 QGraphicsItem::mouseReleaseEvent(event); 252 QGraphicsItem::mouseReleaseEvent(event);
253
254 dragMode = None;
255
80 if(isSelected()) 256 if(isSelected())
81 { 257 {
82 saveGeometry(); 258 saveGeometry();
83 geomChanged = false;
84 } 259 }
85} 260}
261
262QRectF RBMovable::topLeftHandle()
263{
264 QRectF bounds = boundingRect();
265 double width = qMin(bounds.width() / 2. - .5, handleSize);
266 double height = qMin(bounds.height() / 2. - .5, handleSize);
267 double size = qMin(width, height);
268
269 return QRectF(0, 0, size, size);
270}
271
272QRectF RBMovable::topRightHandle()
273{
274 QRectF bounds = boundingRect();
275 double width = qMin(bounds.width() / 2. - .5, handleSize);
276 double height = qMin(bounds.height() / 2. - .5, handleSize);
277 double size = qMin(width, height);
278
279 return QRectF(bounds.width() - size, 0, size, size);
280}
281
282QRectF RBMovable::bottomLeftHandle()
283{
284 QRectF bounds = boundingRect();
285 double width = qMin(bounds.width() / 2. - .5, handleSize);
286 double height = qMin(bounds.height() / 2. - .5, handleSize);
287 double size = qMin(width, height);
288
289 return QRectF(0, bounds.height() - size, size, size);
290}
291
292QRectF RBMovable::bottomRightHandle()
293{
294 QRectF bounds = boundingRect();
295 double width = qMin(bounds.width() / 2. - .5, handleSize);
296 double height = qMin(bounds.height() / 2. - .5, handleSize);
297 double size = qMin(width, height);
298
299 return QRectF(bounds.width() - size, bounds.height() - size, size, size);
300}