-
Notifications
You must be signed in to change notification settings - Fork 1
/
scene.cpp
374 lines (341 loc) · 11.6 KB
/
scene.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "scene.h"
#include "item.h"
#include <QDebug>
#include <QPoint>
#include "window.h"
Scene::Scene(QWidget *parent) : QGraphicsScene (parent)
{
const int WIDTH = 1200;
const int HEIGH = 600;
this->setSceneRect(0, 0, WIDTH, HEIGH);
offx = static_cast<int>(width()) / thickness / 2;
offy = static_cast<int>(height()) / thickness / 2;
}
void Scene::setWindow(Window *value){
this->window = value;
}
void Scene::translateItem(Item *item, int dx, int dy)
{
if (!item) return;
switch (item->getType()) {
case Item::Type::LINE:
{
Line *line = static_cast<Line*>(item);
line->setPoint1(affine.translate(line->getPoint1(), dx, dy));
line->setPoint2(affine.translate(line->getPoint2(), dx, dy));
line->reDraw();
break;
}
case Item::Type::RECT:
{
Rectangle *rect = static_cast<Rectangle*>(item);
rect->setTopLeft(affine.translate(rect->getTopLeft(), dx, dy));
rect->setTopRight(affine.translate(rect->getTopRight(), dx, dy));
rect->setBottomLeft(affine.translate(rect->getBottomLeft(), dx, dy));
rect->setBottomRight(affine.translate(rect->getBottomRight(), dx, dy));
rect->reDraw();
break;
}
case Item::Type::CIRCLE:
{
Circle *circle = static_cast<Circle*>(item);
circle->setPoint(affine.translate(circle->getPoint(), dx, dy));
circle->reDraw();
break;
}
case Item::Type::ELLIPSE:
{
Ellipse *ellipse = static_cast<Ellipse*>(item);
ellipse->setPoint(affine.translate(ellipse->getPoint(), dx, dy));
ellipse->reDraw();
break;
}
default:
break;
}
}
void Scene::rotateItem(Item *item, int x, int y, int angle)
{
if (!item) return;
switch (item->getType()) {
case Item::Type::LINE:
{
Line *line = static_cast<Line*>(item);
line->setPoint1(affine.rotate(line->getPoint1(), x, y, angle));
line->setPoint2(affine.rotate(line->getPoint2(), x, y, angle));
line->reDraw();
break;
}
case Item::Type::RECT:
{
Rectangle *rect = static_cast<Rectangle*>(item);
rect->setTopLeft(affine.rotate(rect->getTopLeft(), x, y, angle));
rect->setTopRight(affine.rotate(rect->getTopRight(), x, y, angle));
rect->setBottomLeft(affine.rotate(rect->getBottomLeft(), x, y, angle));
rect->setBottomRight(affine.rotate(rect->getBottomRight(), x, y, angle));
rect->reDraw();
break;
}
case Item::Type::CIRCLE:
{
Circle *circle = static_cast<Circle*>(item);
circle->setPoint(affine.rotate(circle->getPoint(), x, y, angle));
circle->reDraw();
break;
}
case Item::Type::ELLIPSE:
{
Ellipse *ellipse = static_cast<Ellipse*>(item);
// QPoint ellipseCenter = ellipse->getPoint();
// QPoint leftXRadius(ellipseCenter.x() - ellipse->getXRadius(), ellipseCenter.y());
// QPoint rightXRadius(ellipseCenter.x() + ellipse->getXRadius(), ellipseCenter.y());
// QPoint topYRadius(ellipseCenter.x(), ellipseCenter.y() + ellipse->getYRadius());
// QPoint bottomYRadius(ellipseCenter.x(), ellipseCenter.y() + ellipse->getYRadius());
// leftXRadius = affine.rotate(leftXRadius, x, y, angle);
// rightXRadius = affine.rotate(rightXRadius, x, y, angle);
// topYRadius = affine.rotate(topYRadius, x, y, angle);
// bottomYRadius = affine.rotate(bottomYRadius, x, y, angle);
ellipse->setPoint(affine.rotate(ellipse->getPoint(), x, y, angle));
ellipse->reDraw();
break;
}
default:
break;
}
}
void Scene::scaleItem(Item *item, float sx, float sy)
{
if (!item) return;
switch (item->getType()) {
case Item::Type::LINE:
{
Line *line = static_cast<Line*>(item);
line->setPoint1(affine.scale(line->getPoint1(), sx, sy));
line->setPoint2(affine.scale(line->getPoint2(), sx, sy));
line->reDraw();
break;
}
case Item::Type::RECT:
{
Rectangle *rect = static_cast<Rectangle*>(item);
rect->setTopLeft(affine.scale(rect->getTopLeft(), sx, sy));
rect->setTopRight(affine.scale(rect->getTopRight(), sx, sy));
rect->setBottomLeft(affine.scale(rect->getBottomLeft(), sx, sy));
rect->setBottomRight(affine.scale(rect->getBottomRight(), sx, sy));
rect->reDraw();
break;
}
case Item::Type::CIRCLE:
{
Circle *circle = static_cast<Circle*>(item);
// circle->setR(static_cast<int>(sx * sy * circle->getR()));
circle->setPoint(affine.scale(circle->getPoint(), sx, sy));
circle->reDraw();
break;
}
case Item::Type::ELLIPSE:
{
Ellipse *ellipse = static_cast<Ellipse*>(item);
ellipse->setPoint(affine.scale(ellipse->getPoint(), sx, sy));
ellipse->reDraw();
break;
}
default:
break;
}
}
void Scene::scaleItem(Item *item, float sx, float sy, int x, int y)
{
if (!item) return;
switch (item->getType()) {
case Item::Type::LINE:
{
Line *line = static_cast<Line*>(item);
line->setPoint1(affine.scale(line->getPoint1(), sx, sy, x, y));
line->setPoint2(affine.scale(line->getPoint2(), sx, sy, x, y));
line->reDraw();
break;
}
case Item::Type::RECT:
{
Rectangle *rect = static_cast<Rectangle*>(item);
rect->setTopLeft(affine.scale(rect->getTopLeft(), sx, sy, x, y));
rect->setTopRight(affine.scale(rect->getTopRight(), sx, sy, x, y));
rect->setBottomLeft(affine.scale(rect->getBottomLeft(), sx, sy, x, y));
rect->setBottomRight(affine.scale(rect->getBottomRight(), sx, sy, x, y));
rect->reDraw();
break;
}
case Item::Type::CIRCLE:
{
Circle *circle = static_cast<Circle*>(item);
circle->setPoint(affine.scale(circle->getPoint(), sx, sy, x, y));
circle->reDraw();
break;
}
case Item::Type::ELLIPSE:
{
Ellipse *ellipse = static_cast<Ellipse*>(item);
ellipse->setPoint(affine.scale(ellipse->getPoint(), sx, sy, x, y));
ellipse->reDraw();
break;
}
default:
break;
}
}
void Scene::reflectItem(Item *item, int x, int y)
{
if (!item) return;
switch (item->getType()) {
case Item::Type::LINE:
{
Line *line = static_cast<Line*>(item);
line->setPoint1(affine.reflect(line->getPoint1(), x, y));
line->setPoint2(affine.reflect(line->getPoint2(), x, y));
line->reDraw();
break;
}
case Item::Type::RECT:
{
Rectangle *rect = static_cast<Rectangle*>(item);
rect->setTopLeft(affine.reflect(rect->getTopLeft(), x, y));
rect->setTopRight(affine.reflect(rect->getTopRight(), x, y));
rect->setBottomLeft(affine.reflect(rect->getBottomLeft(), x, y));
rect->setBottomRight(affine.reflect(rect->getBottomRight(), x, y));
rect->reDraw();
break;
}
case Item::Type::CIRCLE:
{
Circle *circle = static_cast<Circle*>(item);
circle->setPoint(affine.reflect(circle->getPoint(), x, y));
circle->reDraw();
break;
}
case Item::Type::ELLIPSE:
{
Ellipse *ellipse = static_cast<Ellipse*>(item);
ellipse->setPoint(affine.reflect(ellipse->getPoint(), x, y));
ellipse->reDraw();
break;
}
default:
break;
}
}
void Scene::changeColor(Item *item, const QColor &color)
{
if (!item) return;
item->setBrush(QBrush(color));
}
void Scene::changeFillColor(Item *item, const QColor &color)
{
if (!item) return;
switch (item->getType()) {
case Item::Type::RECT:
static_cast<Rectangle*>(item)->setFillColor(color);
break;
case Item::Type::CIRCLE:
static_cast<Circle*>(item)->setFillColor(color);
break;
case Item::Type::ELLIPSE:
static_cast<Ellipse*>(item)->setFillColor(color);
break;
default:
break;
}
}
void Scene::drawBackground(QPainter *painter, const QRectF &rect)
{
Q_UNUSED(rect);
int halfHeight = static_cast<int>(this->height()) / 2;
int halfWidth = static_cast<int>(this->width()) / 2;
const int halfThick = thickness / 2; //make it center
// Draw ox and oy
painter->setPen(QPen(QBrush(Qt::black), 1));
painter->setOpacity(0.6);
painter->drawLine(0, halfHeight + halfThick, static_cast<int>(this->width()), halfHeight + halfThick);
painter->drawLine(halfWidth + halfThick, 0, halfWidth + halfThick, static_cast<int>(this->height()));
const int space = thickness * 5; //line spacing
painter->setOpacity(0.25);
// Draw vertical line
for(int xJump = halfThick; xJump < this->width(); xJump += space){
painter->drawLine(xJump, 0, xJump, static_cast<int>(this->height()));
}
// Draw horizontal line
for(int yJump = halfThick; yJump < this->height(); yJump += space){
painter->drawLine(0, yJump, static_cast<int>(this->width()), yJump);
}
// Draw grid
painter->setOpacity(0.08);
for(int xJump = halfThick; xJump < this->width(); xJump += thickness){
painter->drawLine(xJump, 0, xJump, static_cast<int>(this->height()));
}
for(int yJump = halfThick; yJump < this->height(); yJump += thickness){
painter->drawLine(0, yJump, static_cast<int>(this->width()), yJump);
}
//Draw ruler
painter->setOpacity(0.8);
painter->setFont(QFont("Segoe UI", 8));
const int jump = 10 * thickness;
for(int xJump = halfThick + jump, x = -offx + 10; xJump < this->width(); xJump += jump, x += 10){
painter->drawText(xJump - 10, halfHeight + halfThick + 10, QString::number(x));
}
for(int yJump = halfThick + jump, y = offy - 10; yJump < this->height(); yJump += jump, y -= 10){
if (y == 0) continue;
painter->drawText(halfWidth + halfThick - 15, yJump + 5, QString::number(y));
}
painter->setOpacity(1.0);
// Draw x, y
painter->setFont(QFont("Segoe UI", 10));
painter->drawText(halfWidth - 10, 10, QString("y"));
painter->drawText(static_cast<int>(this->width()) - 10, halfHeight + 15, QString("x"));
// Draw arrow
halfWidth += halfThick;
halfHeight += halfThick;
painter->drawLine(static_cast<int>(this->width()) - 5, halfHeight - 5, static_cast<int>(this->width()), halfHeight);
painter->drawLine(static_cast<int>(this->width()) - 5, halfHeight + 5, static_cast<int>(this->width()), halfHeight);
painter->drawLine(halfWidth - 5, 5, halfWidth, 0);
painter->drawLine(halfWidth + 5, 5, halfWidth, 0);
}
void Scene::wheelEvent(QGraphicsSceneWheelEvent *wheelEvent)
{
QGraphicsScene::wheelEvent(wheelEvent);
if (wheelEvent->modifiers() & Qt::ControlModifier){
if (wheelEvent->delta() > 0){
setThickness(std::min(thickness + 5, 20));
} else {
setThickness(std::max(thickness - 5, 5));
}
window->setThickness(thickness);
update();
}
}
int Scene::getOffx() const
{
return offx;
}
int Scene::getOffy() const
{
return offy;
}
int Scene::getThickness() const
{
return thickness;
}
QPoint Scene::toUserCoordinate(const QPointF &scenePos) const
{
return QPoint(static_cast<int>(scenePos.x()) / thickness - offx,
offy - static_cast<int>(scenePos.y()) / thickness);
}
void Scene::setThickness(int value)
{
thickness = value;
offx = static_cast<int>(std::round(width() / thickness / 2));
offy = static_cast<int>(std::round(height() / thickness / 2));
}
QPoint Scene::toScenePos(const QPoint &userPos) const
{
return QPoint((userPos.x() + offx) * thickness, (offy - userPos.y()) * thickness);
}