-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.cpp
More file actions
1527 lines (1277 loc) · 44.8 KB
/
node.cpp
File metadata and controls
1527 lines (1277 loc) · 44.8 KB
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "canvas.h"
#include "node.h"
#include "colorpalette.h"
#include "constants.h"
#include <QPainter>
#include <QGraphicsDropShadowEffect>
#include <QGraphicsSceneHoverEvent>
#include <QGraphicsSceneMouseEvent>
#include <QtCore/QtMath>
#include <QDebug>
#include <QQueue>
// Forward declarations for helper functions (implementation located at end)
QPointF snapPoint(const QPointF &pt);
qreal dist(const QPointF &a, const QPointF &b);
bool rectsCollide(const QRectF &a, const QRectF &b);
void printPt(const QString &s, const QPointF &pt);
void printRect(const QString &s, const QRectF &r);
void printMinMax(qreal minX, qreal minY, qreal maxX, qreal maxY);
QList<QPointF> constructBloom(QPointF scenePos, QPointF sceneTarget);
bool pointInRect(const QPointF &pt, const QRectF &rect);
QList<QPointF> constructAddBloom(const QPointF &scenePos);
bool rectSurroundedBy(QRectF inside, QRectF outside);
// Static var intitial declaration
int Node::globalID = 0;
////////////////////
/// Construction ///
////////////////////
/*
* (Static) Returns a new root node
*/
Node* Node::makeRoot(Canvas* can)
{
return new Node(can, nullptr, Root, QPointF(0,0));
}
/*
* Private constructor for all types of nodes
*/
Node::Node(Canvas* can, Node* par, NodeType t, QPointF pt) :
myID(globalID++),
canvas(can),
parent(par),
type(t),
highlighted(false),
mouseDown(false),
locked(false),
copying(false),
mouseOffset(0, 0),
selected(false),
parentSelected(false),
ghost(false),
newParent(nullptr),
newCopy(nullptr),
target(false)
{
// Drop shadow on click and drag
shadow = new QGraphicsDropShadowEffect(this);
shadow->setEnabled(false);
shadow->setBlurRadius(2);
shadow->setOffset(2);
this->setGraphicsEffect(shadow);
if ( isRoot() )
{
}
else if ( isCut() )
{
setFlag(ItemSendsGeometryChanges);
setCacheMode(DeviceCoordinateCache);
setAcceptHoverEvents(true);
QPointF br(pt.x() + qreal(EMPTY_CUT_SIZE),
pt.y() + qreal(EMPTY_CUT_SIZE));
drawBox = QRectF(pt, br);
}
// Colors
//gradDefault = QRadialGradient( drawBox.x() + 3,
//drawBox.y() + 3,
//(dist(drawBox.topLeft(), drawBox.bottomRight()) * 2 ));
//gradDefault.setColorAt(0, QColor(249, 249, 249));
//gradDefault.setColorAt(1, QColor(249, 249, 249));
//gradHighlighted = QRadialGradient( drawBox.x() + 3,
//drawBox.y() + 3,
//(dist(drawBox.topLeft(), drawBox.bottomRight()) * 2 ));
//gradHighlighted.setColorAt(0, QColor(240, 240, 240));
//gradHighlighted.setColorAt(1, QColor(210, 210, 210));
//gradClicked = QRadialGradient( drawBox.x() + 3,
//drawBox.y() + 3,
//(dist(drawBox.topLeft(), drawBox.bottomRight()) * 2 ));
//gradClicked.setColorAt(0, QColor(210, 210, 210));
//gradClicked.setColorAt(1, QColor(240, 240, 240));
//gradSelected = QRadialGradient( drawBox.x() + 3,
//drawBox.y() + 3,
//(dist(drawBox.topLeft(), drawBox.bottomRight()) * 2 ));
//gradSelected.setColorAt(0, QColor(110, 226, 218));
//gradSelected.setColorAt(1, QColor(0, 209, 140));
}
/* Statement constructor */
Node::Node(Canvas* can, Node* par, QString s, QPointF pt) :
myID(globalID++),
canvas(can),
parent(par),
type(Statement),
highlighted(false),
mouseDown(false),
locked(false),
copying(false),
mouseOffset(0, 0),
letter(s),
selected(false),
parentSelected(false),
ghost(false),
newParent(nullptr),
newCopy(nullptr),
target(false)
{
// Qt flags
setFlag(ItemSendsGeometryChanges);
setCacheMode(DeviceCoordinateCache);
setAcceptHoverEvents(true);
// Shadow
shadow = new QGraphicsDropShadowEffect(this);
shadow->setEnabled(false);
shadow->setBlurRadius(2);
shadow->setOffset(2);
this->setGraphicsEffect(shadow);
// Draw box
QPointF br(pt.x() + qreal(STATEMENT_SIZE),
pt.y() + qreal(STATEMENT_SIZE));
drawBox = QRectF(pt, br);
// Color palette
//gradDefault = QRadialGradient( drawBox.x() + 3,
//drawBox.y() + 3,
//(dist(drawBox.topLeft(), drawBox.bottomRight()) * 2 ));
//gradDefault.setColorAt(0, QColor(0, 0, 0, 0));
//gradDefault.setColorAt(1, QColor(0, 0, 0, 0));
//gradHighlighted = QRadialGradient( drawBox.x() + 3,
//drawBox.y() + 3,
//(dist(drawBox.topLeft(), drawBox.bottomRight()) * 2 ));
//gradHighlighted.setColorAt(0, QColor(240, 240, 240));
//gradHighlighted.setColorAt(1, QColor(210, 210, 210));
//gradClicked = QRadialGradient( drawBox.x() + 3,
//drawBox.y() + 3,
//(dist(drawBox.topLeft(), drawBox.bottomRight()) * 2 ));
//gradClicked.setColorAt(0, QColor(210, 210, 210));
//gradClicked.setColorAt(1, QColor(240, 240, 240));
//gradSelected = QRadialGradient( drawBox.x() + 3,
//drawBox.y() + 3,
//(dist(drawBox.topLeft(), drawBox.bottomRight()) * 2 ));
//gradSelected.setColorAt(0, QColor(110, 226, 218));
//gradSelected.setColorAt(1, QColor(0, 209, 140));
// Statement font
font = QFont();
font.setPixelSize(GRID_SPACING * 2 - 6);
}
// Destructor
Node::~Node()
{
qDebug() << "Calling destructor";
if (parent != nullptr)
{
parent->children.removeOne(this);
parent->updateAncestors();
}
for (Node* child : children)
delete child;
}
Node* Node::addChildCut(QPointF pt, bool usePrediction)
{
if (isStatement())
return nullptr;
QPointF finalPoint;
if (usePrediction) {
QList<QPointF> bloom = constructAddBloom(pt);
finalPoint = findPoint(bloom,
qreal(EMPTY_CUT_SIZE),
qreal(EMPTY_CUT_SIZE));
}
else
finalPoint = snapPoint(pt);
//Node* newChild = new Node(canvas, this, Cut, finalPoint);
Node* newChild = new Node(canvas, this, Cut, mapFromScene(finalPoint));
children.append(newChild);
newChild->setParentItem(this);
updateAncestors();
return newChild;
}
Node* Node::addChildStatement(QPointF pt, QString t, bool usePrediction)
{
if (isStatement())
return nullptr;
QPointF finalPoint;
if (usePrediction) {
QList<QPointF> bloom = constructAddBloom(pt);
finalPoint = findPoint(bloom,
qreal(STATEMENT_SIZE),
qreal(STATEMENT_SIZE));
}
else
finalPoint = snapPoint(pt);
Node* newChild = new Node(canvas, this, t, mapFromScene(finalPoint));
children.append(newChild);
newChild->setParentItem(this);
//newChild->setPos(mapFromScene(finalPoint));
//newChild->setPos(mapFromScene(QPointF(finalPoint.x() - qreal(STATEMENT_SIZE / 2),
//finalPoint.y() - qreal(STATEMENT_SIZE / 2))));
updateAncestors();
return newChild;
}
/////////////////
/// Highlight ///
/////////////////
/*
* Highlight this node
*/
void Node::setAsHighlight()
{
highlighted = true;
update();
}
/*
* Clear highlighting from this node
*/
void Node::removeHighlight()
{
highlighted = false;
update();
}
/////////////////
/// Selection ///
/////////////////
/*
* Select this node
*/
void Node::selectThis()
{
selected = true;
for (Node* n : children)
n->colorDueToSelectedParent();
update();
}
/*
* Deselect this node
*/
void Node::deselectThis()
{
selected = false;
for (Node* n : children)
n->removeColorDueToUnselectedParent();
update();
}
void Node::selectAllKids()
{
for (Node* child : children)
canvas->selectNode(child);
}
/*
* Inverts whether this is selected or not
*/
void Node::toggleSelection()
{
if (selected)
canvas->deselectNode(this);
else
canvas->selectNode(this);
}
void Node::colorDueToSelectedParent() {
parentSelected = true;
for (Node* n : children)
n->colorDueToSelectedParent();
update();
}
void Node::removeColorDueToUnselectedParent() {
parentSelected = false;
for (Node* n : children)
n->removeColorDueToUnselectedParent();
update();
}
/*
* (Static)
* Working off a root node, (though technically can be any node), try and find
* all decendents that are completely surrounded by the given selection box. All
* these nodes then are selected automatically.
*
* If a node's drawBox is not completely surrounded, its children may have a
* chance to be surrounded if it at least collides with the selBox. However, a
* restriction is in place such that nodes closer to root have a higher priority
* for selection than those deeper down. This will ensure that older nodes that
* are closer to root aren't deselected in favor of decendents.
*
* There are some cases where it's still seemingly unintuitive, where selection
* comes down to children list order, but these are unavoidable due to the
* requirement that all selected nodes must share the same parent.
*/
void Node::setSelectionFromBox(Node* root, QRectF selBox)
{
QList<Node*> queue;
for (Node* c : root->children)
queue.append(c);
// For each "level" (i.e. nodes at the same depth that share a parent)
while (true)
{
bool selOnThisLevel = false;
QList<Node*> next;
while (!queue.empty())
{
Node* curr = queue.first();
queue.pop_front();
// Try and find a node that is surrounded
if (rectSurroundedBy(curr->getSceneDraw(), selBox))
{
selOnThisLevel = true;
curr->canvas->selectNode(curr);
}
// If its not completely surrounded, but is colliding at least, then
// its children need to be checked at the next level
else if (rectsCollide(curr->getSceneDraw(), selBox))
for (Node* c : curr->children)
next.append(c);
}
// Finished this level: if we haven't found any surrounded nodes but
// found potential kids, select them for the next check
if (!selOnThisLevel && !next.empty())
{
queue = next;
next.clear();
}
else // Otherwise, we found a surrounded node or no kids, so quit
return;
}
}
////////////////
/// Graphics ///
////////////////
QRectF Node::boundingRect() const
{
return drawBox;
}
QPainterPath Node::shape() const
{
QPainterPath path;
path.addRect(toCollision(drawBox));
return path;
}
void Node::paint(QPainter* painter,
const QStyleOptionGraphicsItem* option,
QWidget* widget)
{
Q_UNUSED(option)
Q_UNUSED(widget)
if (isRoot())
return;
if (isStatement())
painter->setPen(QPen(QColor(0,0,0,0)));
else
painter->setPen(QPen(ColorPalette::strokeColor()));
if (selected || parentSelected)
painter->setBrush(QBrush(ColorPalette::selectColor()));
else if (target)
painter->setBrush(QBrush(ColorPalette::targetColor()));
else if (mouseDown)
painter->setBrush(QBrush(ColorPalette::mouseDownColor()));
else if (highlighted)
painter->setBrush(QBrush(ColorPalette::highlightColor()));
else
painter->setBrush(QBrush(ColorPalette::defaultColor()));
if (ghost || copying)
setOpacity(0.5);
else
setOpacity(1.0);
painter->drawRoundedRect(drawBox, qreal(BORDER_RADIUS), qreal(BORDER_RADIUS));
if ( isStatement() )
{
painter->setPen(QPen(ColorPalette::fontColor()));
painter->setFont(font);
painter->drawText( drawBox, Qt::AlignCenter, letter );
}
}
//////////////
/// Sizing ///
//////////////
/*
* Converts the passed in QRectF (which should be a drawBox) to a collision box
*/
QRectF Node::toCollision(QRectF draw) const
{
return QRectF( QPointF(draw.topLeft().x() - qreal(COLLISION_OFFSET),
draw.topLeft().y() - qreal(COLLISION_OFFSET)),
QPointF(draw.bottomRight().x() + qreal(COLLISION_OFFSET),
draw.bottomRight().y() + qreal(COLLISION_OFFSET)));
}
/*
* Converts the passed in QRectF (which should be a collision box) to a drawBox
*/
QRectF Node::toDraw(QRectF coll) const
{
return QRectF( QPointF(coll.topLeft().x() + qreal(COLLISION_OFFSET),
coll.topLeft().y() + qreal(COLLISION_OFFSET)),
QPointF(coll.bottomRight().x() - qreal(COLLISION_OFFSET),
coll.bottomRight().y() - qreal(COLLISION_OFFSET)));
}
/*
* Returns a scene mapped collision box, offset by deltaX and deltaY if desired.
*
* deltaX and deltaY are given the default value of 0, so the params can be
* ignored if only interested in the actual collision box and not a prediction
*/
QRectF Node::getSceneCollisionBox(qreal deltaX, qreal deltaY) const
{
QRectF c = toCollision(drawBox);
return QRectF(mapToScene( QPointF(c.topLeft().x() + deltaX,
c.topLeft().y() + deltaY)),
mapToScene( QPointF(c.bottomRight().x() + deltaX,
c.bottomRight().y() + deltaY )));
}
/*
* Similar to getSceneCollisionBox, except for a drawBox instead. This isn't
* super efficient implemented this way, but it's not a big deal and I'm lazy
*/
QRectF Node::getSceneDraw(qreal deltaX, qreal deltaY) const
{
return toDraw(getSceneCollisionBox(deltaX, deltaY));
}
/*
* Creates a copy of this node in place where the parent is. The copy is given
* the flag "locked" which means that it cannot be the target of the resulting
* copy destination.
*
* Returns the newly created node (child of the original parent / sibling of
* the copy target)
*/
Node* Node::copyMeToParent() {
if (isRoot())
return nullptr;
Node* copy;
if (isStatement()) {
copy = parent->addChildStatement(getSceneDraw().topLeft(), letter, false);
}
else if (isCut()) {
copy = parent->addChildCut(getSceneDraw().topLeft());
// Copy all children
QQueue<Node*> queue;
QQueue<Node*> parents;
for (Node* n : children) {
queue.enqueue(n);
parents.enqueue(copy);
}
while (!queue.empty()) {
Node* originalCurr = queue.dequeue();
Node* newParent = parents.dequeue();
Node* child;
if (originalCurr->isCut())
child = newParent->addChildCut(originalCurr->getSceneDraw().topLeft(), false);
else if (originalCurr->isStatement())
child = newParent->addChildStatement(originalCurr->getSceneDraw().topLeft(), originalCurr->letter, false);
// Recurse down
for (Node* originalChild : originalCurr->children) {
queue.enqueue(originalChild);
parents.enqueue(child);
}
}
}
copy->locked = true;
if (parent->isRoot())
canvas->addNodeToScene(copy);
return copy;
}
/*
* Recursive function to update all draw boxes from child boxes.
*
* Call this on the parent of an added or deleted node, and it'll make sure to
* update all the way up the tree
*/
void Node::updateAncestors()
{
qDebug() << "node " << myID << "updating ancestors";
if (isRoot())
return;
QRectF myNewDrawBox = predictMySceneDraw(QList<Node*>(), QList<QRectF>());
QRectF sceneDraw = getSceneDraw();
//canvas->addRedBound(myNewDrawBox);
//canvas->addBlueBound(sceneDraw);
// Check if any changes occur
if ( myNewDrawBox.left() != sceneDraw.left() ||
myNewDrawBox.top() != sceneDraw.top() ||
myNewDrawBox.right() != sceneDraw.right() ||
myNewDrawBox.bottom() != sceneDraw.bottom() )
{
qDebug() << "drawbox did change";
// New draw box, so we have to update it
QPointF tl = mapFromScene(myNewDrawBox.topLeft());
QPointF br = mapFromScene(myNewDrawBox.bottomRight());
setDrawBoxFromPotential(QRectF(tl, br));
parent->updateAncestors();
}
else {
qDebug() << "drawbox stayed the same";
}
}
//////////////////////////
/// Collision Checking ///
//////////////////////////
/*
* Checks if the selection list sel can be moved by the relative delta given by
* pt. If no collisions occur (including those potentially created if the parent
* grows), then this function will update all the draw boxes of the parent,
* grandparent, etc.
*
* Important note: this function will NOT alter the draw box of the selection
* list. As these nodes are siblings and will not change in size, I believe it's
* more efficient to simply move with the provided moveBy Qt function, rather
* than needing to recalculate the whole draw box as is required for ancestor
* nodes. This move should happen elsewhere, iff checkPotential returns true.
*
* Params:
* sel - the selection list (assumed to share a parent)
* pt - a QPointF storing a deltaX and deltaY value (in GRID_SPACING units)
*
* Returns:
* true - if no collisions occur; drawboxes are automatically updated, but the
* selection list is not moved
* false - if any collision occurs at any point traveling up the tree; in this
* case, no changes are made to any nodes
*/
bool Node::checkPotential(QList<Node*> changedNodes, QPointF pt)
{
QList<QRectF> drawBoxes; // scene mapped
for (Node* n : changedNodes)
drawBoxes.append(n->getSceneDraw(pt.x(), pt.y()));
QList<Node*> updateNodes;
QList<QRectF> updateBoxes;
if (changedNodes.empty() || changedNodes.first()->isRoot())
return false;
Node* parent = changedNodes.first()->parent;
bool first = true;
do
{
QList<QRectF> alteredDraws;
// This is kind of confusing: basically, at each stage, check each of
// the items in the changedNodes list against all of its siblings that
// aren't also changed... i.e. check the moved nodes against the nodes
// that don't move.
QList<Node*>::iterator itn = changedNodes.begin();
QList<QRectF>::iterator itr = drawBoxes.begin();
for ( ; itn != changedNodes.end(); ++itn, ++itr)
{
Node* changed = (*itn);
QRectF changedRect = (*itr);
// Compare a changed node against each non changed node
for (Node* n : parent->children)
{
if (!changedNodes.contains(n) )
{
// Both statements, so only compare drawboxes (allows
// statements to be closer together visually)
if (changed->isStatement() && n->isStatement())
{
if (rectsCollide(n->getSceneDraw(),
changedRect))
return false;
}
else
{
// Compare the collision boxes, since at least one of
// the nodes to compare is a cut
if (rectsCollide(n->getSceneCollisionBox(),
changed->toCollision(changedRect)))
return false;
}
}
}
// Store
if (!first) // don't want to update the original nodes' drawBoxes
{
updateNodes.append(changed);
updateBoxes.append(changedRect);
}
alteredDraws.append(changedRect);
}
// Quit early
if (parent->isRoot())
break;
// Percolate up
QRectF next = parent->predictMySceneDraw(changedNodes, alteredDraws);
//parent->canvas->addRedBound(next);
changedNodes.clear();
changedNodes.append(parent);
drawBoxes.clear();
drawBoxes.append(next);
parent = parent->parent;
first = false;
// TODO: check if next rect is the same as its drawBox to save work
// (honestly, it's probably not worth the effort to optimize here)
}
while (parent != nullptr);
// Update all the nodes
QList<Node*>::iterator itn = updateNodes.begin();
QList<QRectF>::iterator itr = updateBoxes.begin();
for (; itn != updateNodes.end(); ++itn, ++itr)
{
Node* n = (*itn);
QRectF r = (*itr);
n->setDrawBoxFromPotential(QRectF(n->mapFromScene(r.topLeft()),
n->mapFromScene(r.bottomRight())));
}
return true;
}
/*
* Takes in a set of nodes and their adjusted drawBoxes, and constructs my
* resulting drawBox mapped to scene coords.
*
* Params:
* altNodes - nodes that have a different potential drawBox
* altDraws - parallel to altNodes, these are the scene-mapped potentials
*
* Returns:
* a QRectF of my drawBox, should those altNodes be moved to their altDraw
* locations
*
* NOTE/BUG:
* an empty children list is an unhandled bug -- no way currently to predict
* an empty cut if this is used for updating after a deletion of a node
* -- update: the edge case check should work to solve this now
*/
QRectF Node::predictMySceneDraw(QList<Node*> altNodes, QList<QRectF> altDraws)
{
// Edge case
if (children.empty() && isCut())
{
if (isCut())
{
// Return an empty cut
QRectF sceneDraw = getSceneDraw();
qreal cx = (sceneDraw.left() + sceneDraw.right()) / 2;
qreal cy = (sceneDraw.top() + sceneDraw.bottom()) / 2;
QPointF tl(cx - qreal(EMPTY_CUT_SIZE / 2),
cy - qreal(EMPTY_CUT_SIZE / 2));
QPointF br(cx + qreal(EMPTY_CUT_SIZE / 2),
cy + qreal(EMPTY_CUT_SIZE / 2));
return QRectF(tl, br);
}
else if (isStatement())
{
QRectF sceneDraw = getSceneDraw();
qreal cx = (sceneDraw.left() + sceneDraw.right()) / 2;
qreal cy = (sceneDraw.top() + sceneDraw.bottom()) / 2;
QPointF tl(cx - qreal(STATEMENT_SIZE / 2),
cy - qreal(STATEMENT_SIZE / 2));
QPointF br(cx + qreal(STATEMENT_SIZE / 2),
cy + qreal(STATEMENT_SIZE / 2));
return QRectF(tl, br);
}
}
qreal minX, minY, maxX, maxY;
minX = minY = BIG_NUMBER;
maxX = maxY = -BIG_NUMBER;
for (Node* child : children)
{
QPointF tl, br;
bool usedAlt = false;
// Check if this child is in altNodes
// NOTE: this is a bit convoluted because I used parallel lists. I
// should have used a smarter data structure, or even a list of pairs
// but oh well.
QList<Node*>::iterator itn = altNodes.begin();
QList<QRectF>::iterator itr = altDraws.begin();
for (; itn != altNodes.end(); ++itn, ++itr)
{
Node* n = (*itn);
QRectF r = (*itr);
if (n == child)
{
usedAlt = true;
tl = mapFromScene(r.topLeft());
br = mapFromScene(r.bottomRight());
break;
}
}
// We didn't set tl & br yet, since this node wasn't an altNode,
// therefore use the existing sceneDraw box for the coords instead
if (!usedAlt)
{
tl = mapFromScene(child->getSceneDraw().topLeft());
br = mapFromScene(child->getSceneDraw().bottomRight());
}
// Update the min and max values with these new points
if (tl.x() < minX)
minX = tl.x();
if (tl.y() < minY)
minY = tl.y();
if (br.x() > maxX)
maxX = br.x();
if (br.y() > maxY)
maxY = br.y();
}
// Calculated points are a draw box in parent coords
QPointF tlp = QPointF(minX - qreal(GRID_SPACING),
minY - qreal(GRID_SPACING));
QPointF brp = QPointF(maxX + qreal(GRID_SPACING),
maxY + qreal(GRID_SPACING));
// Convert back to scene
QPointF tls = mapToScene(tlp);
QPointF brs = mapToScene(brp);
return QRectF(tls, brs);
}
/*
* If we determine a new drawBox is okay, call this function to set it for real.
* This makes sure to update the bookkeeping required by Qt and schedule a
* repaint.
*/
void Node::setDrawBoxFromPotential(QRectF potDraw)
{
prepareGeometryChange();
drawBox = potDraw;
}
/////////////
/// Mouse ///
/////////////
void Node::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
qDebug() << "clicked on node " << myID << "sel is " << selected;
if (event->buttons() & Qt::LeftButton)
{
if (event->modifiers() & Qt::AltModifier)
{
ghost = true;
raiseAllAncestors();
}
else if (event->modifiers() & Qt::ControlModifier) {
qDebug() << "copying";
canvas->clearSelection(); // TODO: make copying copy a selection
copying = true;
locked = true;
newCopy = copyMeToParent();
raiseAllAncestors();
}
mouseOffset = event->pos();
shadow->setEnabled(true);
mouseDown = true;
update();
}
else if (event->buttons() & Qt::RightButton)
{
//canvas->removeFromScene(this);
canvas->selectNode(this);
canvas->deleteSelection();
}
}
void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
mouseDown = false;
shadow->setEnabled(false);
if (ghost || copying) {
copying = ghost = locked = false;
lowerAllAncestors();
if (newParent != nullptr && newParent->target) {
newParent->target = false;
newParent->update();
}
if (newParent != parent) {
QRectF oldSceneDraw = getSceneDraw();
// Change parents
newParent->adoptChild(this);
if (newParent->isRoot())
canvas->addNodeToScene(this);
// Update to the new coordinate system
QRectF newSceneDraw = getSceneDraw();
qreal dx = oldSceneDraw.left() - newSceneDraw.left();
qreal dy = oldSceneDraw.top() - newSceneDraw.top();
moveBy(dx, dy);
newParent->updateAncestors();
}
else {
qDebug() << "Moved around in same parent";
parent->updateAncestors();
}
// Unlock the copy
if (newCopy != nullptr)
newCopy->locked = false;
newCopy = nullptr;
}
update();
QGraphicsObject::mouseReleaseEvent(event);
}
void Node::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
{
canvas->setHighlight(this);
QGraphicsObject::hoverEnterEvent(event);
}
void Node::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
{
canvas->setHighlight(parent);
QGraphicsObject::hoverLeaveEvent(event);
}
/*
* Dragging the mouse around will initiate the collision testing. If it finds a
* valid target, it will visually move the node there.
*/
void Node::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
if (mouseDown)
{
// Adjusted with the mouseOffset, so that we are standardizing
// calculations against the upper left corner
qreal dx = mouseOffset.x();
qreal dy = mouseOffset.y();
// Work on this as a selected item
QList<Node*> sel = canvas->selectionIncluding(this);
QList<QPointF> scenePts;
QPointF adj = QPointF(event->pos().x() - dx, event->pos().y() - dy);
scenePts.append(mapToScene(adj));
for (Node* n : sel)
{
if (n == this) // (covered above)
continue;
scenePts.append(n->getSceneDraw().topLeft());
}
// Construct potential points to check collision against
QList<QPointF> bloom = constructBloom(scenePos(), mapToScene(adj));
// No movement
if (bloom.empty())
{
if (sel.size() == 1) canvas->clearSelection();
return;
}
canvas->clearBounds();
// Moved a lil bit at least, so lets check it
for (QPointF pt : bloom)
{
// Check
if (ghost || copying)
{
if (newParent != nullptr && newParent->target) {
newParent->target = false;
newParent->update();
}
Node* collider = determineNewParent(event->scenePos());
newParent = collider;
if (newParent != nullptr && !newParent->target && !newParent->isRoot()) {
newParent->target = true;
newParent->update();
}
//canvas->addBlueBound(collider->getSceneDraw());
for (Node* n : sel)
n->moveBy(pt.x(), pt.y());
if (sel.size() == 1)
canvas->clearSelection();
return;
}
if (checkPotential(sel, pt))
{
for (Node* n : sel)
n->moveBy(pt.x(), pt.y());
if (sel.size() == 1)
canvas->clearSelection();
return;
}
}
// not sure if i like this, but it's needed for now (lazy coding made
// selections basically mandatory; flickers if constantly selecting/