-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvecanvas.cpp
330 lines (291 loc) · 10.7 KB
/
vecanvas.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include "vecanvas.h"
VeCanvas::VeCanvas(QObject *parent)
: QGraphicsScene(parent)
, current_tool_(TOOL_SEL_MOVE_TRANS)
, current_action_(ACT_SELECTION)
, selected_item_(nullptr)
, is_action_in_progress_(false)
, current_brush_(Qt::transparent)
, current_pen_(Qt::black, 1)
, top_item_under_cursor_(nullptr)
{
}
void VeCanvas::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() & Qt::LeftButton) {
switch (current_action_) {
case ACT_DRAW_ITEM: {
switch (current_tool_) {
case TOOL_DRAW_RECTANGLE: {
selected_item_ = new VeShapeRect(this);
break;
}
case TOOL_DRAW_POLY_LINE: {
selected_item_ = new VeShapePolyline(this);
break;
}
default: {
selected_item_ = nullptr;
break;
}
}
if (selected_item_) {
addItem(dynamic_cast<QGraphicsItem *>(selected_item_));
start_point_ = event->scenePos();
selected_item_->setBrush(getCurrentBrush());
selected_item_->setPen(getCurrentPen());
selected_item_->initizlizeShape(start_point_, event->scenePos());
is_action_in_progress_ = true;
}
break;
}
case ACT_SELECTION: {
QGraphicsItem *top_item_under_cursor = this->itemAt(event->scenePos(), QTransform());
setItemSelected(top_item_under_cursor);
if (dynamic_cast<VeGrabberDot *>(top_item_under_cursor)) {
current_action_ = ACT_TRANSFORM;
QGraphicsScene::mousePressEvent(event);
} else if (selected_item_) {
current_action_ = ACT_MOVE_ITEM;
emit itemUnderCursorChanged(nullptr);
update();
} else {
current_action_ = ACT_MOVE_VIEW;
start_point_ = event->screenPos();
}
is_action_in_progress_ = true;
break;
}
default: {
QGraphicsScene::mousePressEvent(event);
break;
}
}
} else {
QGraphicsScene::mousePressEvent(event);
}
}
void VeCanvas::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if (is_action_in_progress_) {
switch (current_action_) {
case ACT_DRAW_ITEM: {
selected_item_->initizlizeShape(start_point_, event->scenePos());
break;
}
case ACT_MOVE_ITEM: {
selected_item_->moveBy(event->scenePos() - event->lastScenePos());
break;
}
case ACT_MOVE_VIEW: {
QRectF rect = sceneRect();
setSceneRect(rect.x() - event->screenPos().x() + event->lastScreenPos().x(),
rect.y() - event->screenPos().y() + event->lastScreenPos().y(),
rect.width(),
rect.height());
break;
}
default: {
QGraphicsScene::mouseMoveEvent(event);
break;
}
}
} else {
QGraphicsItem *top_item = itemAt(event->scenePos(), QTransform());
if (top_item_under_cursor_ != top_item && !(dynamic_cast<VeGrabberDot *>(top_item_under_cursor_) != nullptr &&
event->buttons() & Qt::RightButton)) {
top_item_under_cursor_ = (top_item == dynamic_cast<QGraphicsItem *>(selected_item_))? nullptr : top_item;
emit itemUnderCursorChanged(top_item_under_cursor_);
update();
}
QGraphicsScene::mouseMoveEvent(event);
}
}
void VeCanvas::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() & Qt::LeftButton && is_action_in_progress_) {
is_action_in_progress_ = false;
switch (current_action_) {
case ACT_DRAW_ITEM: {
selected_item_ = nullptr;
break;
}
case ACT_MOVE_ITEM: {
current_action_ = ACT_SELECTION;
break;
}
case ACT_MOVE_VIEW: {
current_action_ = ACT_SELECTION;
break;
}
case ACT_TRANSFORM: {
current_action_ = ACT_SELECTION;
break;
}
default: {
selected_item_ = nullptr;
QGraphicsScene::mouseReleaseEvent(event);
break;
}
}
}
QGraphicsScene::mouseReleaseEvent(event);
}
void VeCanvas::keyPressEvent(QKeyEvent *event)
{
if (!is_action_in_progress_ && event->key() == Qt::Key_Delete && selected_item_) {
removeItem(dynamic_cast<QGraphicsItem *>(selected_item_));
delete selected_item_;
selected_item_ = nullptr;
top_item_under_cursor_ = nullptr;
setItemSelected(nullptr);
}
}
// true - если произошла смена выбора объекта
// false - если выбор остался прежний
bool VeCanvas::setItemSelected(QGraphicsItem *p_item)
{
if (dynamic_cast<VeShapeItem *>(p_item) != selected_item_ && qgraphicsitem_cast<VeGrabberDot *>(p_item) == nullptr) {
selected_item_ = dynamic_cast<VeShapeItem *>(p_item);
if (selected_item_) {
current_brush_ = selected_item_->brush();
current_pen_ = selected_item_->pen();
}
emit itemSelected(selected_item_);
return true;
} else {
return false;
}
}
int VeCanvas::svgItemNameToShapeType(const QString &p_name)
{
if (p_name == "rect") return VeShapeItem::RECTANGLE;
if (p_name == "path") return VeShapeItem::POLYLINE;
return 0;
}
void VeCanvas::setCurrentBrush(const QBrush &p_brush)
{
current_brush_ = p_brush;
if (selected_item_) {
selected_item_->setBrush(current_brush_);
}
}
QBrush VeCanvas::getCurrentBrush() const
{
return current_brush_;
}
void VeCanvas::setCurrentPen(const QPen &p_pen)
{
current_pen_ = p_pen;
if (selected_item_) {
selected_item_->setPen(current_pen_);
}
}
QPen VeCanvas::getCurrentPen() const
{
return current_pen_;
}
void VeCanvas::saveToSVG(const QString &p_file)
{
QSvgGenerator generator;
QRectF saving_rect = itemsBoundingRect();
generator.setFileName(p_file);
generator.setSize(QSize(saving_rect.width(), saving_rect.height()));
generator.setViewBox(saving_rect);
setItemSelected(nullptr);
QPainter painter;
painter.begin(&generator);
render(&painter, saving_rect, saving_rect);
painter.end();
}
void VeCanvas::loadFromSVG(const QString &p_file)
{
foreach (QGraphicsItem *item, items()) {
selected_item_ = nullptr;
top_item_under_cursor_ = nullptr;
removeItem(dynamic_cast<QGraphicsItem *>(item));
delete item;
}
setSceneRect(0, 0, width(), height());
QDomDocument svg_doc;
QFile file(p_file);
if (file.open(QIODevice::ReadOnly) && svg_doc.setContent(&file)) {
QDomNodeList gList = svg_doc.elementsByTagName("g");
for (int i = 0; i < gList.size(); i++) {
QDomNode gNode = gList.item(i);
QDomElement element = gNode.firstChildElement();
QDomElement element_propertys = gNode.toElement();
VeShapeItem *new_item = nullptr;
switch (svgItemNameToShapeType(element.tagName())) {
case VeShapeItem::RECTANGLE: {
new_item = new VeShapeRect(this);
break;
}
case VeShapeItem::POLYLINE: {
new_item = new VeShapePolyline(this);
break;
}
default: {
new_item = nullptr;
break;
}
}
if (new_item) {
QColor item_brush_color(element_propertys.attribute("fill", "#ffffff"));
item_brush_color.setAlphaF(element_propertys.attribute("fill-opacity", "0").toFloat());
new_item->setBrush(QBrush(item_brush_color));
QColor item_pen_color(element_propertys.attribute("stroke", "#000000"));
item_pen_color.setAlphaF(element_propertys.attribute("stroke-opacity", "0").toFloat());
new_item->setPen(QPen(item_pen_color, element_propertys.attribute("stroke-width", "0").toInt()));
QTransform item_transform;
QString transform_string = element_propertys.attribute("transform");
if (transform_string.startsWith("matrix")) {
transform_string.replace("matrix(", "");
transform_string.replace(")", "");
QStringList transform_list = transform_string.split(",");
item_transform.setMatrix(transform_list[0].toFloat(),
transform_list[1].toFloat(),
item_transform.m13(),
transform_list[2].toFloat(),
transform_list[3].toFloat(),
item_transform.m23(),
transform_list[4].toFloat(),
transform_list[5].toFloat(),
item_transform.m33());
}
if (new_item->fromSvgElement(element, item_transform) == 0) {
addItem(dynamic_cast<QGraphicsItem *>(new_item));
} else {
delete new_item;
}
}
}
}
file.close();
}
void VeCanvas::setCurrentTool(int p_tool)
{
current_tool_ = p_tool;
if (current_tool_ != TOOL_SEL_MOVE_TRANS) {
setItemSelected(nullptr);
}
switch (current_tool_) {
case TOOL_SEL_MOVE_TRANS: {
current_action_ = ACT_SELECTION;
break;
}
case TOOL_DRAW_RECTANGLE:
case TOOL_DRAW_POLY_LINE: {
current_action_ = ACT_DRAW_ITEM;
break;
}
default: {
current_action_ = TOOL_SEL_MOVE_TRANS;
break;
}
}
}
int VeCanvas::getCurrentTool() const
{
return current_action_;
}