-
Notifications
You must be signed in to change notification settings - Fork 0
/
veshaperect.cpp
218 lines (178 loc) · 7.2 KB
/
veshaperect.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "veshaperect.h"
VeShapeRect::VeShapeRect(QObject *parent)
: VeShapeItem(parent)
{
initializeGrabbers();
}
int VeShapeRect::type() const
{
return Type;
}
QBrush VeShapeRect::brush() const
{
return QGraphicsRectItem::brush();
}
QPen VeShapeRect::pen() const
{
return QGraphicsRectItem::pen();
}
void VeShapeRect::setBrush(const QBrush &p_brush)
{
QGraphicsRectItem::setBrush(p_brush);
}
void VeShapeRect::setPen(const QPen &p_pen)
{
QGraphicsRectItem::setPen(p_pen);
}
void VeShapeRect::moveBy(const QPointF &p_pos)
{
QPointF local_delta_pos = mapFromScene(p_pos) - mapFromScene(QPointF(0, 0));
setRect(rect().x() + local_delta_pos.x(), rect().y() + local_delta_pos.y(), rect().width(), rect().height());
}
QPainterPath VeShapeRect::shape() const
{
return QGraphicsRectItem::shape();
}
void VeShapeRect::initizlizeShape(const QPointF &p_start, const QPointF &p_end)
{
resetTransform();
QPointF local_start = mapFromScene(p_start);
QPointF local_end = mapFromScene(p_end);
qreal new_x = qMin(local_start.x(), local_end.x());
qreal new_y = qMin(local_start.y(), local_end.y());
qreal new_w = (local_start.x() == local_end.x())? 1 : qAbs(local_start.x() - local_end.x());
qreal new_h = (local_start.y() == local_end.y())? 1 : qAbs(local_start.y() - local_end.y());
setRect(new_x, new_y, new_w, new_h);
}
int VeShapeRect::fromSvgElement(const QDomElement &p_element, const QTransform &p_transform)
{
if (p_element.hasAttribute("x") && p_element.hasAttribute("y") &&
p_element.hasAttribute("width") && p_element.hasAttribute("height")) {
resetTransform();
setRect(p_element.attribute("x", "0").toFloat(), p_element.attribute("y", "0").toFloat(),
p_element.attribute("width", "0").toFloat(), p_element.attribute("height", "0").toFloat());
setTransform(p_transform);
initializeGrabbers();
return 0;
} else {
return -1;
}
}
QRectF VeShapeRect::boundingRect() const
{
return QGraphicsRectItem::boundingRect();
}
bool VeShapeRect::contains(const QPointF &p_point) const
{
if (brush().color().alpha() > 0) {
return QGraphicsRectItem::contains(p_point);
} else {
qreal offset_k = pen().width();
QRectF item_bounding_rect = boundingRect();
if (item_bounding_rect.x() + offset_k < item_bounding_rect.right() &&
item_bounding_rect.y() + offset_k < item_bounding_rect.bottom() &&
item_bounding_rect.width() - 2 * offset_k > 0 &&
item_bounding_rect.height() - 2 * offset_k > 0) {
QRectF item_inside_rect(item_bounding_rect.x() + offset_k, item_bounding_rect.y() + offset_k,
item_bounding_rect.width() - 2 * offset_k, item_bounding_rect.height() - 2 * offset_k);
return QGraphicsRectItem::contains(p_point) && !item_inside_rect.contains(p_point);
} else {
return QGraphicsRectItem::contains(p_point);
}
}
}
void VeShapeRect::paint(QPainter *p_painter, const QStyleOptionGraphicsItem *p_option, QWidget *p_widget)
{
QGraphicsRectItem::paint(p_painter, p_option, p_widget);
drawPattern(p_painter);
}
void VeShapeRect::setRect(const QRectF &p_rectangle)
{
setRect(p_rectangle.x(), p_rectangle.y(), p_rectangle.width(), p_rectangle.height());
}
void VeShapeRect::setRect(qreal p_x, qreal p_y, qreal p_width, qreal p_height)
{
QGraphicsRectItem::setRect(p_x, p_y, p_width, p_height);
setGrabbersPositions();
}
void VeShapeRect::rotate(qreal angle)
{
QRectF item_rect = rect();
QTransform transform_item = transform();
transform_item.translate(item_rect.center().x(), item_rect.center().y());
transform_item.rotate(angle);
transform_item.translate(- item_rect.center().x(), - item_rect.center().y());
setTransform(transform_item);
}
void VeShapeRect::setGrabbersPositions()
{
QRectF item_rect = rect();
grabbers_[GRABBER_TOP_LEFT]->setPos(item_rect.topLeft());
grabbers_[GRABBER_TOP_RIGHT]->setPos(item_rect.topRight());
grabbers_[GRABBER_BOTTOM_LEFT]->setPos(item_rect.bottomLeft());
grabbers_[GRABBER_BOTTOM_RIGHT]->setPos(item_rect.bottomRight());
grabbers_[GRABBER_TOP]->setPos(item_rect.left() + item_rect.width()/2, item_rect.top());
grabbers_[GRABBER_BOTTOM]->setPos(item_rect.left() + item_rect.width()/2, item_rect.bottom());
grabbers_[GRABBER_LEFT]->setPos(item_rect.left(), item_rect.top() + item_rect.height()/2);
grabbers_[GRABBER_RIGHT]->setPos(item_rect.right(), item_rect.top() + item_rect.height()/2);
}
void VeShapeRect::doOnGrabberMoved(VeGrabberDot *grabber, const QPointF &p_scene_pos, Qt::MouseButtons p_action)
{
QRectF item_rect = rect();
QPointF local_pos = mapFromScene(p_scene_pos);
int grabber_type = grabbers_.indexOf(grabber);
if (p_action & Qt::LeftButton) {
switch(grabber_type) {
case GRABBER_TOP_LEFT: {
item_rect.setLeft((local_pos.x() < item_rect.right())? local_pos.x() : item_rect.right() - 1);
item_rect.setTop((local_pos.y() < item_rect.bottom())? local_pos.y() : item_rect.bottom() - 1);
break;
}
case GRABBER_TOP_RIGHT: {
item_rect.setRight((local_pos.x() > item_rect.left())? local_pos.x() : item_rect.left() + 1);
item_rect.setTop((local_pos.y() < item_rect.bottom())? local_pos.y() : item_rect.bottom() - 1);
break;
}
case GRABBER_BOTTOM_LEFT: {
item_rect.setLeft((local_pos.x() < item_rect.right())? local_pos.x() : item_rect.right() - 1);
item_rect.setBottom((local_pos.y() > item_rect.top())? local_pos.y() : item_rect.top() + 1);
break;
}
case GRABBER_BOTTOM_RIGHT: {
item_rect.setRight((local_pos.x() > item_rect.left())? local_pos.x() : item_rect.left() + 1);
item_rect.setBottom((local_pos.y() > item_rect.top())? local_pos.y() : item_rect.top() + 1);
break;
}
case GRABBER_TOP: {
item_rect.setTop((local_pos.y() < item_rect.bottom())? local_pos.y() : item_rect.bottom() - 1);
break;
}
case GRABBER_RIGHT: {
item_rect.setRight((local_pos.x() > item_rect.left())? local_pos.x() : item_rect.left() + 1);
break;
}
case GRABBER_BOTTOM: {
item_rect.setBottom((local_pos.y() > item_rect.top())? local_pos.y() : item_rect.top() + 1);
break;
}
case GRABBER_LEFT: {
item_rect.setLeft((local_pos.x() < item_rect.right())? local_pos.x() : item_rect.right() - 1);
break;
}
default: {
break;
}
}
setRect(item_rect);
setGrabbersPositions();
} else if (p_action & Qt::RightButton) {
QLineF line_center_to_cursor(item_rect.center(), local_pos);
QLineF line_center_to_corner(item_rect.center(), grabber->pos());
qreal rotation_angle = line_center_to_cursor.angleTo(line_center_to_corner);
rotate(rotation_angle);
}
}
int VeShapeRect::grabbersCount()
{
return 8;
}