forked from segfault87/stitchy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.cpp
645 lines (520 loc) · 14.8 KB
/
canvas.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
#include <QApplication>
#include <QClipboard>
#include <QMouseEvent>
#include <QWheelEvent>
#include "cell.h"
#include "document.h"
#include "editor.h"
#include "editoractions.h"
#include "globalstate.h"
#include "selection.h"
#include "selectiongroup.h"
#include "sparsemap.h"
#include "canvas.h"
#define MAGNIFICATION_RATE 0.2
Canvas::Canvas(QWidget *parent)
: QGraphicsView(parent)
{
floatingSelection_ = NULL;
drawmap_ = NULL;
selecting_ = false;
moving_ = false;
dragging_ = false;
drawing_ = false;
erasing_ = false;
rectangle_ = false;
cursor_ = QPoint(-1, -1);
subcursor_ = Subarea_TopLeft;
}
Canvas::~Canvas()
{
if (floatingSelection_)
delete floatingSelection_;
if (drawmap_)
delete drawmap_;
}
bool Canvas::mapToGrid(const QPoint &pos, QPoint &out)
{
Document *d = GlobalState::self()->activeDocument();
if (!d)
return false;
const QSize dim = d->size();
QPointF coord = mapToScene(pos);
QPoint cint(coord.x() / 10, coord.y() / 10);
if (cint.x() < 0 || cint.x() >= dim.width() ||
cint.y() < 0 || cint.y() >= dim.height())
return false;
out = cint;
coord.setX(coord.x() - (cint.x() * 10));
coord.setY(coord.y() - (cint.y() * 10));
return true;
}
bool Canvas::mapToGrid(const QPoint &pos, QPoint &out, Subarea &subareaOut)
{
Document *d = GlobalState::self()->activeDocument();
if (!d)
return false;
const QSize dim = d->size();
QPointF coord = mapToScene(pos);
QPoint cint(coord.x() / 10, coord.y() / 10);
if (cint.x() < 0 || cint.x() >= dim.width() ||
cint.y() < 0 || cint.y() >= dim.height())
return false;
out = cint;
coord.setX(coord.x() - (cint.x() * 10));
coord.setY(coord.y() - (cint.y() * 10));
if (coord.x() > 5.0f) {
if (coord.y() > 5.0f)
subareaOut = Subarea_BottomRight;
else
subareaOut = Subarea_TopRight;
} else {
if (coord.y() > 5.0f)
subareaOut = Subarea_BottomLeft;
else
subareaOut = Subarea_TopLeft;
}
return true;
}
void Canvas::setDocument(Document *doc)
{
if (doc)
setScene(doc);
else
setScene(NULL);
}
void Canvas::zoomIn()
{
scale(1.0 + MAGNIFICATION_RATE, 1.0 + MAGNIFICATION_RATE);
}
void Canvas::zoomOut()
{
scale(1.0 - MAGNIFICATION_RATE, 1.0 - MAGNIFICATION_RATE);
}
void Canvas::zoomReset()
{
resetMatrix();
}
void Canvas::toggleGrid(bool enabled)
{
}
void Canvas::cut()
{
Document *d = GlobalState::self()->activeDocument();
if (!d)
return;
if (!d->selection())
return;
copy();
deleteSelected();
clearSelection();
}
void Canvas::copy()
{
Document *d = GlobalState::self()->activeDocument();
if (!d)
return;
if (!d->selection())
return;
SelectionGroup *grp = new SelectionGroup(d, d->selection()->rect(), false);
QClipboard *clipboard = QApplication::clipboard();
QMimeData *mimeData = new QMimeData();
mimeData->setData(SelectionGroup::mimeType(), grp->serialize());
clipboard->setMimeData(mimeData);
delete grp;
}
void Canvas::paste()
{
Document *d = GlobalState::self()->activeDocument();
if (!d)
return;
QClipboard *clipboard = QApplication::clipboard();
const QMimeData *data = clipboard->mimeData();
if (!data)
return;
if (!data->hasFormat(SelectionGroup::mimeType()))
return;
paste(data->data(SelectionGroup::mimeType()), true);
}
void Canvas::paste(const QByteArray &data, bool action)
{
Document *d = GlobalState::self()->activeDocument();
if (!d)
return;
if (action) {
d->editor()->edit(new ActionPaste(d, this, data));
} else {
commitPaste();
d->createFloatingSelection(data);
d->createSelection(d->floatingSelection()->region());
}
}
void Canvas::deleteSelected()
{
Document *d = GlobalState::self()->activeDocument();
if (!d)
return;
if (!d->selection())
return;
Selection *selection = d->selection();
const QRect &rect = selection->rect();
SparseMap *orig = d->map();
SparseMap *map = new SparseMap(d);
for (int y = rect.y(); y < rect.y() + rect.height(); ++y) {
for (int x = rect.x(); x < rect.x() + rect.width(); ++x) {
QPoint p(x, y);
if (orig->contains(p))
map->cellAt(QPoint(x, y));
}
}
d->editor()->edit(new ActionErase(d, map));
delete map;
}
void Canvas::clearSelection()
{
Document *doc = GlobalState::self()->activeDocument();
if (!doc)
return;
if (doc->selection()) {
doc->clearSelection();
emit clearedSelection();
}
}
void Canvas::clearFloatingSelection()
{
Document *doc = GlobalState::self()->activeDocument();
if (!doc)
return;
if (floatingSelection_) {
delete floatingSelection_;
floatingSelection_ = NULL;
}
doc->clearFloatingSelection();
clearSelection();
}
void Canvas::moveFloatingSelection(const QPoint &pos)
{
Document *doc = GlobalState::self()->activeDocument();
if (!doc)
return;
if (!doc->floatingSelection())
return;
doc->floatingSelection()->moveTo(pos);
if (doc->selection())
doc->selection()->move(pos);
}
void Canvas::commitPaste()
{
Document *doc = GlobalState::self()->activeDocument();
if (!doc || !doc->floatingSelection())
return;
doc->editor()->edit(new ActionFloatCommit(doc, this, doc->floatingSelection()));
}
void Canvas::setCenter(const QPointF& centerPoint)
{
// Get the rectangle of the visible area in scene coords
QRectF visibleArea = mapToScene(rect()).boundingRect();
// Get the scene area
QRectF sceneBounds = sceneRect();
double boundX = sceneBounds.x() + visibleArea.width() / 2.0;
double boundY = sceneBounds.y() + visibleArea.height() / 2.0;
double boundWidth = sceneBounds.width() - 2.0 * boundX;
double boundHeight = sceneBounds.height() - 2.0 * boundY;
// The max boundary that the centerPoint can be to
QRectF bounds(boundX, boundY, boundWidth, boundHeight);
if (bounds.contains(centerPoint)) {
// We are within the bounds
center_ = centerPoint;
} else {
// We need to clamp or use the center of the screen
if (visibleArea.contains(sceneBounds)) {
// Use the center of scene ie. we can see the whole scene
center_ = sceneBounds.center();
} else {
center_ = centerPoint;
// We need to clamp the center. The centerPoint is too large
if (centerPoint.x() > bounds.x() + bounds.width()) {
center_.setX(bounds.x() + bounds.width());
} else if (centerPoint.x() < bounds.x()) {
center_.setX(bounds.x());
}
if (centerPoint.y() > bounds.y() + bounds.height()) {
center_.setY(bounds.y() + bounds.height());
} else if (centerPoint.y() < bounds.y()) {
center_.setY(bounds.y());
}
}
}
// Update the scrollbars
centerOn(center_);
}
void Canvas::mousePressEvent(QMouseEvent *event)
{
Document *doc = GlobalState::self()->activeDocument();
if (!doc)
return;
if (event->button() & Qt::LeftButton) {
ToolMode mode = GlobalState::self()->toolMode();
if (mode == ToolMode_Select) {
QPoint cursor;
if (!mapToGrid(event->pos(), cursor))
return;
selecting_ = true;
Selection *sel;
if (!doc->selection())
sel = doc->createSelection();
else
sel = doc->selection();
startPos_ = cursor;
sel->set(QRect(cursor, QSize(0, 0)));
return;
} else if (mode == ToolMode_Move) {
if (!doc->selection())
return;
Selection *sel = doc->selection();
QPoint cursor;
if (!mapToGrid(event->pos(), cursor)) {
return;
} else if (sel && !sel->within(cursor)) {
commitPaste();
clearSelection();
return;
}
moving_ = true;
if (!doc->floatingSelection() && !floatingSelection_)
floatingSelection_ = new SelectionGroup(doc, sel->rect(), true);
startPos_ = sel->rect().topLeft();
lastPos_ = cursor;
return;
}
if (!GlobalState::self()->color() || !doc)
return;
drawmap_ = new SparseMap(doc);
Selection *sel = doc->selection();
if (mode == ToolMode_Full || mode == ToolMode_Half ||
mode == ToolMode_Petite || mode == ToolMode_Quarter) {
QPoint cursor;
if (!mapToGrid(event->pos(), cursor))
return;
if (sel && !sel->within(cursor))
return;
drawing_ = true;
} else if (mode == ToolMode_Erase) {
erasing_ = true;
} else if (mode == ToolMode_Rectangle) {
QPoint cursor;
if (!mapToGrid(event->pos(), cursor))
return;
if (sel && !sel->within(cursor))
return;
rectangle_ = true;
startPos_ = cursor;
lastRect_ = QRect(cursor, cursor);
}
mouseMoveEvent(event);
} else if (event->button() & Qt::RightButton) {
dragging_ = true;
lastPos_ = event->pos();
event->accept();
return;
}
event->ignore();
}
void Canvas::mouseMoveEvent(QMouseEvent *event)
{
const Color *c = GlobalState::self()->color();
Document *doc = GlobalState::self()->activeDocument();
if (!doc)
return;
if (selecting_) {
/* select */
QPoint cursor;
if (!mapToGrid(event->pos(), cursor))
return;
doc->selection()->set(QRect(startPos_, cursor));
} else if (dragging_) {
/* pan */
QPointF delta = mapToScene(lastPos_) - mapToScene(event->pos());
lastPos_ = event->pos();
setCenter(center_ + delta);
} else if (drawing_) {
Subarea subcursor;
QPoint cursor;
if (!mapToGrid(event->pos(), cursor, subcursor))
return;
Selection *sel = doc->selection();
if (sel && !sel->within(cursor))
return;
ToolMode mode = GlobalState::self()->toolMode();
if (cursor != cursor_) {
/* draw */
Cell *cell = drawmap_->cellAt(cursor);
if (!cell)
return;
cursor_ = cursor;
if (mode == ToolMode_Full) {
cell->addFullStitch(c);
} else {
Orientation o;
if (subcursor == Subarea_TopLeft ||
subcursor == Subarea_BottomRight)
o = Orientation_Backslash;
else
o = Orientation_Slash;
subcursor_ = subcursor;
if (mode == ToolMode_Half)
cell->addHalfStitch(o, c);
else if (mode == ToolMode_Petite)
cell->addPetiteStitch(subcursor, c);
else if (mode == ToolMode_Quarter)
cell->addQuarterStitch(o, subcursor, c);
}
cell->createGraphicsItems();
}
} else if (erasing_) {
/* erase */
QPoint cursor;
if (!mapToGrid(event->pos(), cursor))
return;
Selection *sel = doc->selection();
if (sel && !sel->within(cursor))
return;
if (cursor != cursor_) {
cursor_ = cursor;
SparseMap *map = doc->map();
if (map->contains(cursor)) {
Cell *c = map->cellAt(cursor);
c->clearGraphicsItems();
drawmap_->cellAt(cursor);
}
}
} else if (rectangle_) {
/* rect */
QPoint cursor;
if (!mapToGrid(event->pos(), cursor))
return;
Selection *sel = doc->selection();
if (sel && !sel->within(cursor))
return;
QRect rect(startPos_, cursor);
if (!rect.isValid())
rect = rect.normalized();
for (int y = rect.y(); y < rect.y() + rect.height(); ++y) {
for (int x = rect.x(); x < rect.x() + rect.width(); ++x) {
if (!drawmap_->contains(QPoint(x, y))) {
Cell *cell = drawmap_->cellAt(QPoint(x, y));
cell->addFullStitch(c);
cell->createGraphicsItems();
}
}
}
if (rect.x() > lastRect_.x()) {
for (int y = lastRect_.y(); y < lastRect_.y() + lastRect_.height(); ++y) {
for (int x = lastRect_.x(); x < rect.x(); ++x) {
drawmap_->remove(QPoint(x, y));
}
}
}
if (rect.y() > lastRect_.y()) {
for (int y = lastRect_.y(); y < rect.y(); ++y) {
for (int x = lastRect_.x(); x < lastRect_.x() + lastRect_.width(); ++x) {
drawmap_->remove(QPoint(x, y));
}
}
}
if (rect.x() + rect.width() < lastRect_.x() + lastRect_.width()) {
for (int y = lastRect_.y(); y < lastRect_.y() + lastRect_.height(); ++y) {
for (int x = rect.x() + rect.width(); x < lastRect_.x() + lastRect_.width(); ++x) {
drawmap_->remove(QPoint(x, y));
}
}
}
if (rect.y() + rect.height() < lastRect_.y() + lastRect_.height()) {
for (int y = rect.y() + rect.height(); y < lastRect_.y() + lastRect_.height(); ++y) {
for (int x = lastRect_.x(); x < lastRect_.x() + lastRect_.width(); ++x) {
drawmap_->remove(QPoint(x, y));
}
}
}
lastRect_ = rect;
} else if (moving_) {
if (!floatingSelection_ && !doc->floatingSelection())
return;
Selection *sel = doc->selection();
if (!sel)
return;
QPoint cursor;
if (!mapToGrid(event->pos(), cursor))
return;
SelectionGroup *selgroup;
if (floatingSelection_)
selgroup = floatingSelection_;
else if (doc->floatingSelection())
selgroup = doc->floatingSelection();
QRect tpos = selgroup->region();
tpos.moveTopLeft(tpos.topLeft() + (cursor - lastPos_));
if (!QRect(QPoint(0, 0), doc->size()).contains(tpos))
return;
selgroup->moveRel(cursor - lastPos_);
sel->move(selgroup->position());
lastPos_ = cursor;
}
}
void Canvas::mouseReleaseEvent(QMouseEvent *event)
{
Document *doc = GlobalState::self()->activeDocument();
if (!doc)
return;
if (dragging_ && event->button() & Qt::RightButton) {
dragging_ = false;
} else if (event->button() & Qt::LeftButton) {
if (drawing_ || rectangle_) {
drawing_ = false;
rectangle_ = false;
cursor_ = QPoint(-1, -1);
subcursor_ = Subarea_TopLeft;
doc->editor()->edit(new ActionDraw(doc, drawmap_));
delete drawmap_;
drawmap_ = NULL;
} else if (erasing_) {
erasing_ = false;
cursor_ = QPoint(-1, -1);
doc->editor()->edit(new ActionErase(doc, drawmap_));
delete drawmap_;
drawmap_ = NULL;
} else if (selecting_) {
Selection *sel = doc->selection();
const QRect &r = sel->rect();
if (r.width() == 0 && r.height() == 0) {
doc->clearSelection();
emit clearedSelection();
} else {
emit madeSelection(r);
}
selecting_ = false;
} else if (moving_) {
Selection *sel = doc->selection();
moving_ = false;
if (floatingSelection_) {
doc->editor()->edit(new ActionMove(doc, startPos_, sel->rect().size(),
floatingSelection_));
delete floatingSelection_;
floatingSelection_ = NULL;
} else if (doc->floatingSelection()) {
doc->editor()->edit(new ActionFloatMove(doc, this, startPos_,
sel->rect().topLeft()));
}
startPos_ = QPoint();
}
}
}
void Canvas::wheelEvent(QWheelEvent *event)
{
double mag;
if (event->delta() > 0) {
mag = 1.0 + MAGNIFICATION_RATE;
} else {
mag = 1.0 - MAGNIFICATION_RATE;
}
scale(mag, mag);
}