forked from scribusproject/scribus
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcanvasmode_nodeedit.cpp
More file actions
1473 lines (1329 loc) · 47 KB
/
canvasmode_nodeedit.cpp
File metadata and controls
1473 lines (1329 loc) · 47 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
/*
For general Scribus (>=1.3.2) copyright and licensing information please refer
to the COPYING file provided with the program. Following this notice may exist
a copyright and/or license notice that predates the release of Scribus 1.3.2
for which a new license (GPL+exception) is in place.
*/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <QMenu>
#include <QMouseEvent>
#include <QPainter>
#include "canvas.h"
#include "canvasgesture_pan.h"
#include "canvasgesture_rectselect.h"
#include "canvasmode_nodeedit.h"
#include "fpoint.h"
#include "iconmanager.h"
#include "nodeeditcontext.h"
#include "pageitem.h"
#include "scribus.h"
#include "scribusdoc.h"
#include "scribusview.h"
#include "selection.h"
#include "ui/nodeeditpalette.h"
#include "ui/pageselector.h"
#include "ui/scrspinbox.h"
CanvasMode_NodeEdit::CanvasMode_NodeEdit(ScribusView* view) : CanvasMode(view)
{
m_ScMW = m_view->m_ScMW;
}
void CanvasMode_NodeEdit::drawControls(QPainter* p)
{
if (m_doc->m_Selection->isEmpty() || m_doc->nodeEdit.previewMode())
return;
PageItem* currItem = m_doc->m_Selection->itemAt(0);
FPointArray cli;
p->save();
p->scale(m_canvas->scale(), m_canvas->scale());
p->translate(-m_doc->minCanvasCoordinate.x(), -m_doc->minCanvasCoordinate.y());
p->setTransform(currItem->getTransform(), true);
p->setBrush(Qt::NoBrush);
p->setRenderHint(QPainter::Antialiasing);
if ((m_doc->nodeEdit.isContourLine()) && (!currItem->ContourLine.empty()))
cli = currItem->ContourLine;
else
{
cli = currItem->PoLine;
if (currItem->isSymbol() || currItem->isGroup())
{
if (currItem->imageFlippedH())
{
p->translate(currItem->width(), 0);
p->scale(-1, 1);
}
if (currItem->imageFlippedV())
{
p->translate(0, currItem->height());
p->scale(1, -1);
}
}
}
const double scale = m_canvas->m_viewMode.scale;
double nx = 0.0;
double ny = 0.0;
double cx = 0.0;
double cy = 0.0;
QPen penControl = pens().value("node");
bool isActiveNode = false;
bool isActiveControl = false;
bool highlight = false;
bool mergedNodes = true;
int nr, nl, inr;
// FPoint pn, pc;
// Draw Vector Path
if (cli.size() > 3)
{
for (int i = 0; i < cli.size() - 3; i += 4)
{
if (cli.isMarker(i))
continue;
const FPoint& a1 = cli.point(i);
const FPoint& a2 = cli.point(i + 1);
const FPoint& a3 = cli.point(i + 3);
const FPoint& a4 = cli.point(i + 2);
QPainterPath Bez;
Bez.moveTo(a1.x(), a1.y());
Bez.cubicTo(a2.x(), a2.y(), a3.x(), a3.y(), a4.x(), a4.y());
// Vector Path
p->setPen(pens().value("vector"));
p->drawPath(Bez);
}
}
// Draw Nodes
for (int i = 0; i < cli.size() - 1; i += 2)
{
if (cli.isMarker(i))
continue;
nr = NodeEditContext::indexOfNode(m_doc->nodeEdit.clre(), NodeEditContext::NodeType::NodeRight, cli.size());
nl = NodeEditContext::indexOfNode(m_doc->nodeEdit.clre(), NodeEditContext::NodeType::NodeLeft, cli.size());
inr = NodeEditContext::indexOfNode(i, NodeEditContext::NodeType::NodeRight, cli.size());
mergedNodes = NodeEditContext::hasTwoNodes(i, cli);
highlight = (i == nr || i == nl) && NodeEditContext::hasTwoNodes(m_doc->nodeEdit.clre(), cli);
isActiveControl = m_doc->nodeEdit.clre() == i + 1 ? true : false;
isActiveNode = m_doc->nodeEdit.clre() == i ||
m_doc->nodeEdit.selNode().contains(i) ||
m_doc->nodeEdit.selNode().contains(i - 2) ||
(m_doc->nodeEdit.selNode().contains(inr) && mergedNodes) ? true : false;
cli.point(i, &nx, &ny);
cli.point(i + 1, &cx, &cy);
// Draw Control Handle Lines
if (FPoint(cx, cy) != FPoint(nx, ny))
{
// changes color to highlight if angle is same as opponent handle
if (highlight && state.isSameAngle && state.clickedOn)
p->setPen(pens().value("highlight"));
else
p->setPen(pens().value("node-handle"));
p->setRenderHint(QPainter::Antialiasing, true);
p->drawLine(QPointF(nx, ny), QPointF(cx, cy));
}
p->setRenderHint(QPainter::Antialiasing, false);
// Draw Node Handle
drawNodeHandle(p, QPointF(nx, ny), pens().value("node"), scale, isActiveNode);
// Draw Control Handle
if (FPoint(cx, cy) != FPoint(nx, ny))
{
// changes color to highlight if length is same as opponent handle
if (highlight && state.isSameLength && state.isSameAngle && state.clickedOn)
penControl = pens().value("highlight");
else
penControl = pens().value("node");
drawNodeControl(p, QPointF(cx, cy), penControl, scale, isActiveControl);
}
}
emit m_view->HavePoint(m_doc->nodeEdit.clre() != -1);
if (m_doc->nodeEdit.clre() != -1)
{
cli.point(m_doc->nodeEdit.clre(), &nx, &ny);
emit m_view->ClipPo(nx, ny);
}
p->restore();
}
QMap<int, int> CanvasMode_NodeEdit::selectedNodesList(FPointArray points)
{
QMap<int, int> selList;
const auto& selNodes = m_doc->nodeEdit.selNode();
if (points.size() > 3)
{
for (int i = 0; i < points.size() - 1; i += 2)
{
if (points.isMarker(i))
continue;
// i = path start;
// i + 2 = path end OR left part of a node
// i - 2 = right part of a node
if (selNodes.contains(i) || selNodes.contains(i + 2) || selNodes.contains(i - 2) || selNodes.contains(i - 4))
{
selList.insert(i, i + 1);
}
}
}
return selList;
}
void CanvasMode_NodeEdit::activate(bool fromGesture)
{
CanvasMode::activate(fromGesture);
if (fromGesture && m_rectangleSelect)
{
m_canvas->m_viewMode.m_MouseButtonPressed = false;
// handle rectangle select
PageItem* currItem = m_doc->m_Selection->itemAt(0);
m_doc->nodeEdit.selNode().clear();
QRectF Sele = m_rectangleSelect->result();
FPointArray Clip;
bool firstPoint = false;
QTransform pm2 = currItem->getTransform();
if (m_doc->nodeEdit.isContourLine())
Clip = currItem->ContourLine;
else
{
Clip = currItem->PoLine;
if (currItem->isSymbol() || currItem->isGroup())
{
if (currItem->imageFlippedH())
{
pm2.translate(currItem->width(), 0);
pm2.scale(-1, 1);
}
if (currItem->imageFlippedV())
{
pm2.translate(0, currItem->height());
pm2.scale(1, -1);
}
}
}
for (int i = 0; i < Clip.size(); ++i)
{
if (Clip.isMarker(i))
continue;
const FPoint& np = Clip.point(i);
FPoint npf2 = np.transformPoint(pm2, false);
if ((Sele.contains(npf2.x(), npf2.y())) && ((i == 0) || (((i - 2) % 4) == 0)))
{
if (i == 0)
firstPoint = true;
if ((firstPoint) && (i == Clip.size() - 2) && (!currItem->asPolyLine()))
continue;
m_doc->nodeEdit.selectNode(i);
m_doc->nodeEdit.update(Clip.pointQF(i));
}
}
currItem->update();
m_doc->nodeEdit.finishTransaction(currItem);
delete m_rectangleSelect;
m_rectangleSelect = nullptr;
}
else
{
m_Mxp = -1; // last mouse position
m_Myp = -1;
m_Dxp = -1; // last mouse press position for rectangle select
m_Dyp = -1;
m_GxM = -1; // guide position
m_GyM = -1;
m_MoveGX = m_MoveGY = false;
}
}
void CanvasMode_NodeEdit::deactivate(bool forGesture)
{
if (!forGesture && m_rectangleSelect)
{
m_rectangleSelect->clear();
delete m_rectangleSelect;
m_rectangleSelect = nullptr;
}
CanvasMode::deactivate(forGesture);
}
inline bool CanvasMode_NodeEdit::GetItem(PageItem** pi)
{
*pi = m_doc->m_Selection->itemAt(0);
return (*pi) != nullptr;
}
FPoint CanvasMode_NodeEdit::constraintControlAngle(FPoint anchor, FPoint mouse, double angleStep)
{
QLineF line(anchor.toQPointF(), mouse.toQPointF());
line.setAngle(constrainAngle(line.angle(), angleStep));
return line.p2();
}
FPoint CanvasMode_NodeEdit::snapControlAngle(FPoint anchor, FPoint mouse, double angle)
{
double snap = m_doc->prefsData().guidesPrefs.guideRad / m_canvas->scale();
QLineF lineAM(anchor.x(), anchor.y(), mouse.x(), mouse.y()); // anchor to mouse
QLineF lineAC = lineAM; // anchor to opposite control point
lineAC.setAngle(360.0 - angle); // invert angle, Qt angle is counter-clockwise
if (qMax(lineAM.angle(), lineAC.angle()) - qMin(lineAM.angle(), lineAC.angle()) <= 0.00001)
return lineAC.p2();
return QLineF(lineAM.p2(), lineAC.p2()).length() <= snap ? lineAC.p2() : mouse;
}
FPoint CanvasMode_NodeEdit::snapControlLength(FPoint anchor, FPoint mouse, double length)
{
double snap = m_doc->prefsData().guidesPrefs.guideRad / m_canvas->scale();
QLineF line(anchor.toQPointF(), mouse.toQPointF());
if (length - snap <= line.length() && length + snap >= line.length())
line.setLength(length);
return line.p2();
}
FPoint CanvasMode_NodeEdit::snapControlOrigin(FPoint anchor, FPoint mouse)
{
double snap = m_doc->prefsData().guidesPrefs.guideRad / m_canvas->scale();
QLineF line(anchor.toQPointF(), mouse.toQPointF());
return (line.length() <= snap) ? anchor : mouse;
}
void CanvasMode_NodeEdit::mouseDoubleClickEvent(QMouseEvent *m)
{
m->accept();
m_canvas->m_viewMode.m_MouseButtonPressed = false;
m_canvas->resetRenderMode();
m_view->requestMode(submodeEndNodeEdit);
}
void CanvasMode_NodeEdit::mouseMoveEvent(QMouseEvent *m)
{
double sc = m_canvas->scale();
FPoint npf = m_canvas->globalToCanvas(m->globalPosition());
m_doc->nodeEdit.setScale(sc);
m->accept();
PageItem* currItem { nullptr };
if (GetItem(&currItem))
{
if (m_doc->DragP)
return;
//Operations run here:
//Item resize, esp after creating a new one
if (m_view->moveTimerElapsed() && m_canvas->m_viewMode.m_MouseButtonPressed
&& (m->buttons() & Qt::LeftButton) && (!currItem->locked()))
{
handleNodeEditDrag(m, currItem);
}
else if (!m_canvas->m_viewMode.m_MouseButtonPressed)
{
if (!currItem->locked())
{
QTransform p;
QRect mpo;
handleNodeEditMove(m, mpo, currItem, p);
}
}
}
else // shouldn't be in nodeedit mode now, should it??
{
npf = m_canvas->globalToCanvas(m->globalPosition());
if ((m_canvas->m_viewMode.m_MouseButtonPressed) && (m->buttons() & Qt::LeftButton) && (m_GyM == -1) && (m_GxM == -1))
{
if (!m_rectangleSelect)
{
m_rectangleSelect = new RectSelect(this);
}
m_rectangleSelect->prepare(m->globalPosition());
m_view->startGesture(m_rectangleSelect);
return;
}
if ((m_doc->guidesPrefs().guidesShown) && (!m_doc->GuideLock)
&& (m_doc->OnPage(npf.x(), npf.y()) != -1))
{
Guides::iterator it;
Guides tmpGuides = m_doc->currentPage()->guides.horizontals(GuideManagerCore::Standard);
for (it = tmpGuides.begin(); it != tmpGuides.end(); ++it)
{
if ( (*it) + m_doc->currentPage()->yOffset() < npf.y() + m_doc->guidesPrefs().grabRadius * sc &&
(*it) + m_doc->currentPage()->yOffset() > npf.y() - m_doc->guidesPrefs().grabRadius * sc )
{
if ((m_canvas->m_viewMode.m_MouseButtonPressed) && (m_GyM != -1))
m_MoveGY = true;
if (npf.x() < m_doc->currentPage()->xOffset() ||
npf.x() >= m_doc->currentPage()->xOffset() + m_doc->currentPage()->width() - 1)
m_view->setCursor(QCursor(Qt::ArrowCursor));
else
m_view->setCursor(QCursor(Qt::SplitVCursor));
return;
}
}
tmpGuides = m_doc->currentPage()->guides.verticals(GuideManagerCore::Standard);
for (it = tmpGuides.begin(); it!= tmpGuides.end(); ++it)
{
if ( (*it) + m_doc->currentPage()->xOffset() < npf.x() + m_doc->guidesPrefs().grabRadius * sc &&
(*it) + m_doc->currentPage()->xOffset() > npf.x() - m_doc->guidesPrefs().grabRadius * sc)
{
if ((m_canvas->m_viewMode.m_MouseButtonPressed) && (m_GxM != -1))
m_MoveGX = true;
if (npf.y() < m_doc->currentPage()->yOffset() ||
npf.y() >= m_doc->currentPage()->yOffset() + m_doc->currentPage()->height() - 1)
m_view->setCursor(QCursor(Qt::ArrowCursor));
else
m_view->setCursor(QCursor(Qt::SplitHCursor));
return;
}
}
m_view->setCursor(QCursor(Qt::ArrowCursor));
}
}
}
void CanvasMode_NodeEdit::mousePressEvent(QMouseEvent *m)
{
m_canvas->m_viewMode.m_MouseButtonPressed = true;
m_canvas->m_viewMode.operItemMoving = false;
m_doc->DragP = false;
m_doc->leaveDrag = false;
m_doc->nodeEdit.setScale(m_canvas->scale());
m_MoveGX = m_MoveGY = false;
m->accept();
m_view->registerMousePress(m->globalPosition());
QRect mpo;
m_Mxp = m->position().x();
m_Myp = m->position().y();
if (((m->buttons() & Qt::RightButton) && (m->modifiers() & Qt::ControlModifier)) || ((!(m->modifiers() & Qt::ControlModifier)) && (m->buttons() & Qt::MiddleButton)))
{
if (!m_panGesture)
{
m_panGesture = new PanGesture(this);
}
m_view->startGesture(m_panGesture);
m_panGesture->mousePressEvent(m); // Not an error, this is used to register current canvas point
return;
}
if (m_doc->m_Selection->count() != 0)
{
handleNodeEditPress(m, mpo);
}
if ((m_doc->m_Selection->count() != 0) && (m->button() == Qt::RightButton))
{
m_canvas->m_viewMode.m_MouseButtonPressed = true;
m_Dxp = m_Mxp;
m_Dyp = m_Myp;
}
}
void CanvasMode_NodeEdit::mouseReleaseEvent(QMouseEvent *m)
{
PageItem *currItem;
m_canvas->m_viewMode.m_MouseButtonPressed = false;
m_canvas->resetRenderMode();
m->accept();
// Deselect selected node if SHIFT is pressed
if (state.clickedOn == NodeHandle &&
m_doc->nodeEdit.submode() == NodeEditContext::MOVE_POINT &&
!state.wasNewlySelected &&
m->modifiers() == Qt::ShiftModifier)
{
m_doc->nodeEdit.deselectNode(m_doc->nodeEdit.clre());
m_doc->regionsChanged()->update(QRectF());
}
if (m_view->moveTimerElapsed())
{
// Path Segment
if (m_doc->nodeEdit.segP1() != -1 && m_doc->nodeEdit.segP2() != -1)
{
m_doc->nodeEdit.deselect();
m_doc->nodeEdit.setSegP1(-1);
m_doc->nodeEdit.setSegP2(-1);
}
m_canvas->m_viewMode.operItemMoving = false;
currItem = m_doc->m_Selection->itemAt(0);
double xposOrig = currItem->xPos();
double yposOrig = currItem->yPos();
ScOldNewState<FPointArray> *onState = m_doc->nodeEdit.finishTransaction1(currItem);
if (m_doc->nodeEdit.hasNodeSelected() && (m_doc->nodeEdit.selectionCount() == 1))
{
//FIXME:av
// FPoint newP = m_canvas->globalToCanvas(m->globalPosition());
currItem->OldB2 = currItem->width();
currItem->OldH2 = currItem->height();
// currItem->ClipEdited = true;
// FPointArray Clip;
// FPoint npf;
// double nx = newP.x();
// double ny = newP.y();
// if (!m_doc->ApplyGuides(&nx, &ny) && !m_doc->ApplyGuides(&nx, &ny,true))
// npf = m_doc->ApplyGridF(FPoint(nx, ny));
// else
// npf = FPoint(nx, ny);
// QTransform pp = currItem->getTransform();
// npf = npf.transformPoint(pp, true);
// if (m_doc->nodeEdit.isContourLine())
// Clip = currItem->ContourLine;
// else
// {
// Clip = currItem->PoLine;
// if (currItem->isSymbol() || currItem->isGroup())
// {
// if (currItem->imageFlippedH())
// {
// QTransform p;
// p.translate(currItem->width(), 0);
// p.scale(-1, 1);
// npf = npf.transformPoint(p, false);
// }
// if (currItem->imageFlippedV())
// {
// QTransform p;
// p.translate(0, currItem->height());
// p.scale(1, -1);
// npf = npf.transformPoint(p, false);
// }
// }
// }
// m_doc->nodeEdit.moveClipPoint(currItem, npf);
}
// nitramr: Scribus crashes if ESC key is pressed during mouse drag operation
m_doc->adjustItemSize(currItem, true);
if (!m_doc->nodeEdit.isContourLine())
{
// Move the contour accordingly in the item's coordinate space
QTransform m = QTransform().rotate(-currItem->rotation());
QPointF delta = m.map(QPointF(xposOrig, yposOrig)) - m.map(QPointF(currItem->xPos(), currItem->yPos()));
currItem->ContourLine.translate(delta.x(), delta.y());
}
m_doc->regionsChanged()->update(QRectF());
if (onState)
m_doc->nodeEdit.finishTransaction2(currItem, onState);
}
else
{
m_canvas->setRenderModeUseBuffer(false);
m_doc->DragP = false;
m_doc->leaveDrag = false;
m_canvas->m_viewMode.operItemMoving = false;
m_canvas->m_viewMode.operItemResizing = false;
m_view->MidButt = false;
if (m_view->groupTransactionStarted())
{
for (int i = 0; i < m_doc->m_Selection->count(); ++i)
m_doc->m_Selection->itemAt(i)->checkChanges(true);
m_view->endGroupTransaction();
}
//Commit drag created items to undo manager.
if (m_doc->m_Selection->itemAt(0) != nullptr)
m_doc->itemAddCommit(m_doc->m_Selection->itemAt(0));
//Make sure the Zoom spinbox and page selector don't have focus if we click on the canvas
m_view->m_ScMW->zoomSpinBox->clearFocus();
m_view->m_ScMW->pageSelector->clearFocus();
if (m_doc->m_Selection->itemAt(0) != nullptr) // is there the old clip stored for the undo action
{
currItem = m_doc->m_Selection->itemAt(0);
m_doc->nodeEdit.finishTransaction(currItem);
}
}
m_doc->nodeEdit.setMoveMode(state.originalMoveMode);
state.reset();
}
void CanvasMode_NodeEdit::keyPressEvent(QKeyEvent *e)
{
commonkeyPressEvent_NormalNodeEdit(e);
int kk = e->key();
PageItem* currItem = m_doc->m_Selection->itemAt(0);
if (kk == Qt::Key_Delete || kk == Qt::Key_Backspace)
{
if (currItem && m_doc->nodeEdit.hasNodeSelected())
{
// delete Nodes
refreshPath(deleteNodes());
}
}
}
void CanvasMode_NodeEdit::keyReleaseEvent(QKeyEvent *e)
{
commonkeyReleaseEvent(e);
}
void CanvasMode_NodeEdit::handleNodeEditPress(QMouseEvent* m, QRect r)
{
PageItem* currItem = m_doc->m_Selection->itemAt(0);
FPointArray Clip = m_doc->nodeEdit.beginTransaction(currItem);
QTransform pm2 = currItem->getTransform();
FPoint npf2 = m_canvas->globalToCanvas(m->globalPosition()).transformPoint(pm2, true);
bool edited = false;
state.originalMoveMode = m_doc->nodeEdit.moveMode();
state.moveMode = state.originalMoveMode;
state.clickedOn = None;
m_doc->nodeEdit.oppositeControl.isValid = false;
switch (state.hoveredOver)
{
case ControlHandle:
case NodeHandle:
{
if ((currItem->isSymbol() || currItem->isGroup()) && (!m_doc->nodeEdit.isContourLine()))
{
if (currItem->imageFlippedH())
{
pm2.translate(currItem->width(), 0);
pm2.scale(-1, 1);
}
if (currItem->imageFlippedV())
{
pm2.translate(0, currItem->height());
pm2.scale(1, -1);
}
}
for (int i = 0; i < Clip.size(); ++i)
{
if (m_canvas->hitsCanvasPoint(m->globalPosition(), pm2.map(Clip.pointQF(i))))
{
if (state.originalMoveMode == NodeEditContext::Auto)
{
bool sameAngle = m_doc->nodeEdit.sameAngle(currItem, i);
bool sameLength = m_doc->nodeEdit.sameLength(currItem, i);
if (sameLength && sameAngle)
state.moveMode = NodeEditContext::Symmetric;
else if (!sameLength && sameAngle)
state.moveMode = NodeEditContext::Asymmetric;
else if (!sameLength && !sameAngle)
state.moveMode = NodeEditContext::Independent;
}
state.clickedOn = NodeEditContext::isNode(i) ? NodeHandle : ControlHandle;
if (state.clickedOn == NodeHandle)
{
// Deselect all controls
m_doc->nodeEdit.deselectNodeControls();
if (!m_doc->nodeEdit.selNode().contains(i) && m->modifiers() != Qt::ShiftModifier)
m_doc->nodeEdit.selNode().clear();
state.wasNewlySelected = !m_doc->nodeEdit.selNode().contains(i);
m_doc->nodeEdit.selectNode(i);
if (m->modifiers() == Qt::AltModifier || m_doc->nodeEdit.submode() == NodeEditContext::EDIT_POINT)
{
// Reset Controls
if (state.moveMode != NodeEditContext::Independent || !NodeEditContext::hasTwoNodes(i, Clip))
m_doc->nodeEdit.resetControl(currItem);
else // Make Control Handles symetric
m_doc->nodeEdit.equalizeControls(currItem);
}
}
if (state.clickedOn == ControlHandle)
{
m_doc->nodeEdit.selNode().clear();
m_doc->nodeEdit.selectNode(i);
}
m_doc->nodeEdit.setClre(i);
// Save position of opposite control handle
int indexNode = NodeEditContext::indexOfNode(i, NodeEditContext::OppositeSibling, Clip.size());
int indexControl = NodeEditContext::indexOfNode(i, NodeEditContext::Opposite, Clip.size());
double dx = Clip.point(indexControl).x() - Clip.point(indexNode).x();
double dy = Clip.point(indexControl).y() - Clip.point(indexNode).y();
m_doc->nodeEdit.oppositeControl.isValid = NodeEditContext::hasTwoNodes(i, Clip);
m_doc->nodeEdit.oppositeControl.relativePos = Clip.point(indexControl) - Clip.point(indexNode);
m_doc->nodeEdit.oppositeControl.controlIndex = indexControl;
m_doc->nodeEdit.oppositeControl.nodeIndex = indexNode;
m_doc->nodeEdit.oppositeControl.distance = distance(dx, dy);
m_doc->nodeEdit.oppositeControl.angle = xy2Deg(dx, dy);
m_doc->nodeEdit.update(Clip.pointQF(i));
edited = true;
break;
}
//deselect all if no node or control has hit
if (i == Clip.size() - 1)
m_doc->nodeEdit.deselect();
}
break;
}
case PathSegment:
m_doc->nodeEdit.deselect();
state.clickedOn = PathSegment;
break;
case None:
default:
m_doc->nodeEdit.deselect();
state.clickedOn = None;
break;
}
if (m_doc->nodeEdit.submode() == NodeEditContext::MOVE_POINT && !m_doc->nodeEdit.hasNodeSelected() && state.clickedOn == PathSegment)
{
m_doc->nodeEdit.setSegP1(m_doc->nodeEdit.clre2());
m_doc->nodeEdit.setSegP2(m_doc->nodeEdit.clre2()+2);
m_doc->nodeEdit.setClre(m_doc->nodeEdit.clre2());
}
FPointArray cli;
int EndInd = Clip.size();
int StartInd = 0;
if (m_doc->nodeEdit.clre() > 0)
{
for (int n = m_doc->nodeEdit.clre(); n < Clip.size(); ++n)
{
if (Clip.isMarker(n))
{
EndInd = n;
break;
}
}
for (int n2 = m_doc->nodeEdit.clre(); n2 > 0; n2--)
{
if (Clip.isMarker(n2))
{
StartInd = n2 + 1;
break;
}
}
}
if (m_doc->nodeEdit.submode() == NodeEditContext::SPLIT_PATH && (state.clickedOn == NodeHandle || state.clickedOn == PathSegment))
{
// Path Segment
if ((!m_doc->nodeEdit.hasNodeSelected()) && (m_doc->nodeEdit.clre2() != -1)) // We don't have a Point, try to add one onto the current curve segment
{
bool foundP = false;
int seg = 0;
double absDist = 9999999999.9;
FPoint point(0, 0);
FPoint normal(0, 0);
FPoint tangent(0, 0);
FPoint nearPoint(0, 0);
double nearT = 0.0;
QRect mpo2(0, 0, m_doc->guidesPrefs().grabRadius * 3, m_doc->guidesPrefs().grabRadius * 3);
mpo2.moveCenter(QPoint(qRound(npf2.x()), qRound(npf2.y())));
for (int poi = 0; poi < Clip.size() - 3; poi += 4)
{
const FPoint& a1 = Clip.point(poi);
const FPoint& a2 = Clip.point(poi + 1);
const FPoint& a3 = Clip.point(poi + 3);
const FPoint& a4 = Clip.point(poi + 2);
QPainterPath Bez;
Bez.moveTo(a1.x(), a1.y());
Bez.cubicTo(a2.x(), a2.y(), a3.x(), a3.y(), a4.x(), a4.y());
QPolygon cli2 = Bez.toFillPolygon().toPolygon();
for (int clp = 0; clp < cli2.size() - 1; ++clp)
{
if (m_view->PointOnLine(cli2.point(clp), cli2.point(clp + 1), mpo2))
{
seg = poi;
double sp = 0.0;
double spadd = 1.0 / (Clip.lenPathSeg(seg) * m_canvas->scale());
while (sp < 1.0)
{
Clip.pointTangentNormalAt(seg, sp, &point, &tangent, &normal );
double d1 = fabs(sqrt(pow(point.x() - npf2.x(), 2) + pow(point.y() - npf2.y(), 2)));
if (d1 < absDist)
{
foundP = true;
nearPoint = point;
nearT = sp;
absDist = d1;
}
sp += spadd;
}
}
}
}
cli.putPoints(0, m_doc->nodeEdit.clre2() + 2, Clip);
if (foundP)
{
npf2 = nearPoint;
FPoint base = cli.point(cli.size() - 2);
FPoint c1 = cli.point(cli.size() - 1);
FPoint base2 = Clip.point(m_doc->nodeEdit.clre2() + 2);
FPoint c2 = Clip.point(m_doc->nodeEdit.clre2() + 3);
if ((base == c1) && (base2 == c2))
{
cli.resize(cli.size()+4);
cli.putPoints(cli.size() - 4, 4, npf2.x(), npf2.y(), npf2.x(), npf2.y(), npf2.x(), npf2.y(), npf2.x(), npf2.y());
cli.putPoints(cli.size(), Clip.size() - (m_doc->nodeEdit.clre2() + 2), Clip, m_doc->nodeEdit.clre2() + 2);
}
else
{
FPoint cn1 = (1.0 - nearT) * base + nearT * c1;
FPoint cn2 = (1.0 - nearT) * cn1 + nearT * ((1.0 - nearT) * c1 + nearT * c2);
FPoint cn3 = (1.0 - nearT) * ((1.0 - nearT) * c1 + nearT * c2) + nearT * ((1.0 - nearT) * c2 + nearT * base2);
FPoint cn4 = (1.0 - nearT) * c2 + nearT * base2;
cli.setPoint(cli.size()-1, cn1);
cli.resize(cli.size() + 4);
int basind = cli.size() + 1;
cli.putPoints(cli.size() - 4, 4, npf2.x(), npf2.y(), cn2.x(), cn2.y(), npf2.x(), npf2.y(), cn3.x(), cn3.y());
cli.putPoints(cli.size(), Clip.size() - (m_doc->nodeEdit.clre2() + 2), Clip, m_doc->nodeEdit.clre2() + 2);
cli.setPoint(basind, cn4);
}
Clip = cli.copy();
cli.resize(0);
m_doc->nodeEdit.setClre(m_doc->nodeEdit.clre2() + 2);
m_doc->nodeEdit.setClre2(-1);
EndInd = Clip.size();
StartInd = 0;
for (int n = m_doc->nodeEdit.clre(); n < Clip.size(); ++n)
{
if (Clip.isMarker(n))
{
EndInd = n;
break;
}
}
if (m_doc->nodeEdit.clre() > 0)
{
for (int n2 = m_doc->nodeEdit.clre(); n2 > 0; n2--)
{
if (Clip.isMarker(n2))
{
StartInd = n2 + 1;
break;
}
}
}
}
else
m_doc->nodeEdit.deselect();
}
// Node Handle
if (m_doc->nodeEdit.hasNodeSelected())
{
if (currItem->isPolygon())
{
if ((m_doc->nodeEdit.clre() != 0) && (m_doc->nodeEdit.clre() != (EndInd - 2)))
{
if (currItem->Segments.count() == 0)
{
cli.putPoints(0, EndInd - (m_doc->nodeEdit.clre() + 2), Clip, m_doc->nodeEdit.clre() + 2);
cli.putPoints(cli.size(), m_doc->nodeEdit.clre() + 2, Clip);
}
else
{
cli.putPoints(0, EndInd-StartInd, Clip, StartInd);
int z = m_doc->itemAdd(PageItem::Polygon, PageItem::Unspecified, currItem->xPos(), currItem->yPos(), currItem->width(), currItem->height(), currItem->lineWidth(), currItem->fillColor(), currItem->lineColor());
PageItem* bb = m_doc->Items->takeAt(z);
if (m_doc->nodeEdit.isContourLine())
bb->ContourLine.resize(0);
else
bb->PoLine.resize(0);
if (StartInd != 0)
{
if (m_doc->nodeEdit.isContourLine())
{
bb->ContourLine.putPoints(0, StartInd - 4, Clip);
bb->ContourLine.putPoints(bb->ContourLine.size(), Clip.size() -EndInd, Clip, EndInd);
}
else
{
bb->PoLine.putPoints(0, StartInd - 4, Clip);
bb->PoLine.putPoints(bb->PoLine.size(), Clip.size() - EndInd, Clip, EndInd);
}
}
else
{
if (m_doc->nodeEdit.isContourLine())
bb->ContourLine.putPoints(0, Clip.size() - EndInd - 4, Clip, EndInd + 4);
else
bb->PoLine.putPoints(0, Clip.size() - EndInd - 4, Clip, EndInd + 4);
}
bb->setRotation(currItem->rotation());
bb->ClipEdited = true;
m_doc->Items->insert(m_doc->Items->indexOf(currItem), bb);
m_doc->adjustItemSize(bb, true);
}
currItem->PoLine = cli.copy();
}
m_doc->nodeEdit.deselect();
currItem->ClipEdited = true;
edited = true;
m_doc->nodeEdit.setSubMode(NodeEditContext::MOVE_POINT);
PageItem* newItem = m_doc->convertItemTo(currItem, PageItem::PolyLine);
currItem = newItem;
m_doc->m_Selection->clear();
m_doc->m_Selection->addItem(currItem);
emit m_view->PolyOpen();
}
else
{
if ((currItem->isPolyLine()) || (currItem->isPathText()))
{
if ((m_doc->nodeEdit.clre() > 1) && (m_doc->nodeEdit.clre() < (Clip.size() - 2)))
{
int z = m_doc->itemAdd(PageItem::PolyLine, PageItem::Unspecified, currItem->xPos(), currItem->yPos(), currItem->width(), currItem->height(), currItem->lineWidth(), currItem->fillColor(), currItem->lineColor());
PageItem* bb = m_doc->Items->at(z);
if (m_doc->nodeEdit.isContourLine())
bb->ContourLine.putPoints(0, Clip.size() - (m_doc->nodeEdit.clre() + 2), Clip, m_doc->nodeEdit.clre() + 2);
else
bb->PoLine.putPoints(0, Clip.size() - (m_doc->nodeEdit.clre() + 2), Clip, m_doc->nodeEdit.clre() + 2);
bb->setRotation(currItem->rotation());
m_doc->adjustItemSize(bb, true);
bb->ClipEdited = true;
bb->setFrameType(currItem->frameType());
cli.resize(0);
cli.putPoints(0, m_doc->nodeEdit.clre() + 2, Clip);
currItem->PoLine = cli.copy();
m_doc->m_Selection->clear();
m_doc->m_Selection->addItem(currItem);
}
m_doc->nodeEdit.deselect();
currItem->ClipEdited = true;
edited = true;
m_doc->nodeEdit.setSubMode(NodeEditContext::MOVE_POINT);
currItem->setPolyClip(qRound(qMax(currItem->lineWidth() / 2.0, 1.0)));
emit m_view->PolyOpen();
}
}
}
}
if ((m_doc->nodeEdit.submode() == NodeEditContext::DEL_POINT || (m_doc->nodeEdit.submode() == NodeEditContext::MOVE_POINT && m->modifiers() == Qt::ControlModifier)) && m_doc->nodeEdit.hasNodeSelected() && state.clickedOn == NodeHandle)
{
edited = deleteNodes();
}
// Add node id "add point" mode OR "move point" mode + CTRL
if ((m_doc->nodeEdit.submode() == NodeEditContext::ADD_POINT || (m_doc->nodeEdit.submode() == NodeEditContext::MOVE_POINT && m->modifiers() == Qt::ControlModifier)) && (m_doc->nodeEdit.clre2() != -1))
{
bool foundP = false;
uint seg = 0;
double absDist = 9999999999.9;
FPoint point(0, 0);
FPoint normal(0, 0);
FPoint tangent(0, 0);
FPoint nearPoint(0, 0);
double nearT = 0.0;
QRect mpo2(0, 0, m_doc->guidesPrefs().grabRadius * 3, m_doc->guidesPrefs().grabRadius * 3);
mpo2.moveCenter(QPoint(qRound(npf2.x()), qRound(npf2.y())));
for (int poi = 0; poi < Clip.size() - 3; poi += 4)
{
const FPoint& a1 = Clip.point(poi);
const FPoint& a2 = Clip.point(poi + 1);
const FPoint& a3 = Clip.point(poi + 3);
const FPoint& a4 = Clip.point(poi + 2);
QPainterPath Bez;
Bez.moveTo(a1.x(), a1.y());
Bez.cubicTo(a2.x(), a2.y(), a3.x(), a3.y(), a4.x(), a4.y());
QPolygon cli2 = Bez.toFillPolygon().toPolygon();
for (int clp = 0; clp < cli2.size() - 1; ++clp)
{
if (m_view->PointOnLine(cli2.point(clp), cli2.point(clp + 1), mpo2))
{
seg = poi;
double sp = 0.0;
double spadd = 1.0 / (Clip.lenPathSeg(seg) * m_canvas->scale());
while (sp < 1.0)
{
Clip.pointTangentNormalAt(seg, sp, &point, &tangent, &normal );
double d1 = fabs(sqrt(pow(point.x() - npf2.x(), 2) + pow(point.y() - npf2.y(), 2)));
if (d1 < absDist)
{
foundP = true;
nearPoint = point;
nearT = sp;
absDist = d1;
}
sp += spadd;
}
}
}
}
cli.putPoints(0, m_doc->nodeEdit.clre2() + 2, Clip);