forked from OpenBoard-org/OpenBoard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UBGraphicsScene.cpp
3157 lines (2524 loc) · 102 KB
/
UBGraphicsScene.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
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
/*
* Copyright (C) 2015-2022 Département de l'Instruction Publique (DIP-SEM)
*
* Copyright (C) 2013 Open Education Foundation
*
* Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
* l'Education Numérique en Afrique (GIP ENA)
*
* This file is part of OpenBoard.
*
* OpenBoard 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, version 3 of the License,
* with a specific linking exception for the OpenSSL project's
* "OpenSSL" library (or with modified versions of it that use the
* same license as the "OpenSSL" library).
*
* OpenBoard is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
*/
#include "UBGraphicsScene.h"
#include <QtGui>
#include <QtSvg>
#include <QGraphicsView>
#include <QGraphicsVideoItem>
#include "frameworks/UBGeometryUtils.h"
#include "core/UBApplication.h"
#include "core/UBSettings.h"
#include "core/UBApplicationController.h"
#include "core/UBPersistenceManager.h"
#include "core/UBTextTools.h"
#include "gui/UBMagnifer.h"
#include "gui/UBResources.h"
#include "tools/UBGraphicsRuler.h"
#include "tools/UBGraphicsAxes.h"
#include "tools/UBGraphicsProtractor.h"
#include "tools/UBGraphicsCompass.h"
#include "tools/UBGraphicsTriangle.h"
#include "tools/UBGraphicsCurtainItem.h"
#include "tools/UBGraphicsCache.h"
#include "document/UBDocumentProxy.h"
#include "board/UBBoardController.h"
#include "board/UBDrawingController.h"
#include "board/UBBoardView.h"
#include "UBGraphicsItemUndoCommand.h"
#include "UBGraphicsItemGroupUndoCommand.h"
#include "UBGraphicsTextItemUndoCommand.h"
#include "UBGraphicsPixmapItem.h"
#include "UBGraphicsSvgItem.h"
#include "UBGraphicsPolygonItem.h"
#include "UBGraphicsMediaItem.h"
#include "UBGraphicsWidgetItem.h"
#include "UBGraphicsPDFItem.h"
#include "UBGraphicsTextItem.h"
#include "UBGraphicsStrokesGroup.h"
#include "UBSelectionFrame.h"
#include "UBGraphicsItemZLevelUndoCommand.h"
#include "domain/UBGraphicsGroupContainerItem.h"
#include "UBGraphicsStroke.h"
#include "core/memcheck.h"
#define DEFAULT_Z_VALUE 0.0
qreal UBZLayerController::errorNumber = -20000001.0;
UBZLayerController::UBZLayerController(QGraphicsScene *scene) :
mScene(scene)
{
scopeMap.insert(itemLayerType::NoLayer, ItemLayerTypeData( errorNumber, errorNumber));
scopeMap.insert(itemLayerType::BackgroundItem, ItemLayerTypeData(-1000000.0, -1000000.0 ));
// DEFAULT_Z_VALUE isn't used because it allows to easily identify new objects
scopeMap.insert(itemLayerType::ObjectItem, ItemLayerTypeData(-1000000.0, DEFAULT_Z_VALUE - 1.0));
scopeMap.insert(itemLayerType::DrawingItem, ItemLayerTypeData( DEFAULT_Z_VALUE + 1.0, 1000000.0 ));
scopeMap.insert(itemLayerType::ToolItem, ItemLayerTypeData( 1000000.0, 1000100.0 ));
scopeMap.insert(itemLayerType::CppTool, ItemLayerTypeData( 1000100.0, 1000200.0 ));
scopeMap.insert(itemLayerType::Curtain, ItemLayerTypeData( 1000200.0, 1001000.0 ));
scopeMap.insert(itemLayerType::Eraiser, ItemLayerTypeData( 1001000.0, 1001100.0 ));
scopeMap.insert(itemLayerType::Pointer, ItemLayerTypeData( 1001100.0, 1001200.0 ));
scopeMap.insert(itemLayerType::Cache, ItemLayerTypeData( 1001300.0, 1001400.0 ));
scopeMap.insert(itemLayerType::SelectedItem, ItemLayerTypeData( 1001000.0, 1001000.0 ));
scopeMap.insert(itemLayerType::SelectionFrame, ItemLayerTypeData( 1010000.0, 1010000.0 ));
}
qreal UBZLayerController::generateZLevel(itemLayerType::Enum key)
{
if (!scopeMap.contains(key)) {
qDebug() << "Number is out of layer scope";
return errorNumber;
}
qreal result = scopeMap.value(key).curValue;
qreal top = scopeMap.value(key).topLimit;
qreal incrementalStep = scopeMap.value(key).incStep;
result += incrementalStep;
if (result >= top) {
// If not only one variable presents in the scope, notify that values for scope are over
if (scopeMap.value(key).topLimit != scopeMap.value(key).bottomLimit) {
qDebug() << "new values are over for the scope" << key;
}
result = top - incrementalStep;
}
scopeMap[key].curValue = result;
return result;
}
qreal UBZLayerController::generateZLevel(QGraphicsItem *item)
{
qreal result = errorNumber;
itemLayerType::Enum type = static_cast<itemLayerType::Enum>(item->data(UBGraphicsItemData::itemLayerType).toInt());
if (validLayerType(type)) {
result = generateZLevel(type);
}
return result;
}
qreal UBZLayerController::changeZLevelTo(QGraphicsItem *item, moveDestination dest)
{
itemLayerType::Enum curItemLayerType = typeForData(item);
if (curItemLayerType == itemLayerType::NoLayer) {
qDebug() << "item's layer is out of the scope. Can't implement z-layer changing operation";
return errorNum();
}
//select only items wiht the same z-level as item's one and push it to sortedItems QMultiMap
QMultiMap<qreal, QGraphicsItem*> sortedItems;
if (mScene->items().count()) {
foreach (QGraphicsItem *tmpItem, mScene->items()) {
if (typeForData(tmpItem) == curItemLayerType) {
sortedItems.insert(tmpItem->data(UBGraphicsItemData::ItemOwnZValue).toReal(), tmpItem);
}
}
}
//If only one item itself - do nothing, return it's z-value
if (sortedItems.count() == 1 && sortedItems.first() == item) {
qDebug() << "only one item exists in layer. Have nothing to change";
return item->data(UBGraphicsItemData::ItemOwnZValue).toReal();
}
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
QMultiMapIterator<qreal, QGraphicsItem*>iCurElement(sortedItems);
#else
QMapIterator<qreal, QGraphicsItem*>iCurElement(sortedItems);
#endif
if (dest == up) {
qDebug() << "item data zvalue= " << item->data(UBGraphicsItemData::ItemOwnZValue).toReal();
if (iCurElement.findNext(item)) {
if (iCurElement.hasNext()) {
qreal nextZ = iCurElement.peekNext().value()->data(UBGraphicsItemData::ItemOwnZValue).toReal();
UBGraphicsItem::assignZValue(iCurElement.peekNext().value(), item->data(UBGraphicsItemData::ItemOwnZValue).toReal());
UBGraphicsItem::assignZValue(item, nextZ);
iCurElement.next();
while (iCurElement.hasNext() && iCurElement.peekNext().value()->data(UBGraphicsItemData::ItemOwnZValue).toReal() == nextZ) {
UBGraphicsItem::assignZValue(iCurElement.next().value(), nextZ);
}
}
}
} else if (dest == top) {
if (iCurElement.findNext(item)) {
if (iCurElement.hasNext()) {
UBGraphicsItem::assignZValue(item, generateZLevel(item));
}
}
} else if (dest == down) {
iCurElement.toBack();
if (iCurElement.findPrevious(item)) {
if (iCurElement.hasPrevious()) {
qreal nextZ = iCurElement.peekPrevious().value()->data(UBGraphicsItemData::ItemOwnZValue).toReal();
UBGraphicsItem::assignZValue(iCurElement.peekPrevious().value(), item->data(UBGraphicsItemData::ItemOwnZValue).toReal());
UBGraphicsItem::assignZValue(item, nextZ);
while (iCurElement.hasNext() && iCurElement.peekNext().value()->data(UBGraphicsItemData::ItemOwnZValue).toReal() == nextZ) {
UBGraphicsItem::assignZValue(iCurElement.next().value(), nextZ);
}
}
}
} else if (dest == bottom) {
iCurElement.toBack();
if (iCurElement.findPrevious(item)) {
if (iCurElement.hasPrevious()) {
qreal oldz = item->data(UBGraphicsItemData::ItemOwnZValue).toReal();
iCurElement.toFront();
qreal nextZ = iCurElement.next().value()->data(UBGraphicsItemData::ItemOwnZValue).toReal();
ItemLayerTypeData curItemLayerTypeData = scopeMap.value(curItemLayerType);
//if we have some free space between lowest graphics item and layer's bottom bound,
//insert element close to first element in layer
if (nextZ > curItemLayerTypeData.bottomLimit + curItemLayerTypeData.incStep) {
qreal result = nextZ - curItemLayerTypeData.incStep;
UBGraphicsItem::assignZValue(item, result);
} else {
UBGraphicsItem::assignZValue(item, nextZ);
bool doubleGap = false; //to detect if we can finish rundown since we can insert item to the free space
while (iCurElement.peekNext().value() != item) {
qreal curZ = iCurElement.value()->data(UBGraphicsItemData::ItemOwnZValue).toReal();
qreal curNextZ = iCurElement.peekNext().value()->data(UBGraphicsItemData::ItemOwnZValue).toReal();
if (curNextZ - curZ >= 2 * curItemLayerTypeData.incStep) {
UBGraphicsItem::assignZValue(iCurElement.value(), curZ + curItemLayerTypeData.incStep);
doubleGap = true;
break;
} else {
UBGraphicsItem::assignZValue(iCurElement.value(), curNextZ);
iCurElement.next();
}
}
if (!doubleGap) {
UBGraphicsItem::assignZValue(iCurElement.value(), oldz);
while (iCurElement.hasNext() && (iCurElement.peekNext().value()->data(UBGraphicsItemData::ItemOwnZValue).toReal() == oldz)) {
UBGraphicsItem::assignZValue(iCurElement.next().value(), oldz);
}
}
}
}
}
}
//clear selection of the item and then select it again to activate selectionChangeProcessing()
item->scene()->clearSelection();
item->setSelected(true);
foreach (QGraphicsItem *iitem, sortedItems.values()) {
if (iitem)
iitem != item ? qDebug() << "current value" << iitem->zValue() : qDebug() << "marked value" << QString::number(iitem->zValue(), 'f');
}
//Return new z value assigned to item
// experimental
item->setZValue(item->data(UBGraphicsItemData::ItemOwnZValue).toReal());
return item->data(UBGraphicsItemData::ItemOwnZValue).toReal();
}
itemLayerType::Enum UBZLayerController::typeForData(QGraphicsItem *item) const
{
itemLayerType::Enum result = static_cast<itemLayerType::Enum>(item->data(UBGraphicsItemData::itemLayerType).toInt());
if (!scopeMap.contains(result)) {
result = itemLayerType::NoLayer;
}
return result;
}
void UBZLayerController::setLayerType(QGraphicsItem *pItem, itemLayerType::Enum pNewType)
{
pItem->setData(UBGraphicsItemData::itemLayerType, QVariant(pNewType));
}
void UBZLayerController::shiftStoredZValue(QGraphicsItem *item, qreal zValue)
{
itemLayerType::Enum type = typeForData(item);
if (validLayerType(type)) {
ItemLayerTypeData typeData = scopeMap.value(type);
if (typeData.curValue < zValue) {
scopeMap[type].curValue = zValue;
}
}
}
/**
* @brief Returns true if the zLevel is not used by any item on the scene, or false if so.
*/
bool UBZLayerController::zLevelAvailable(qreal z)
{
foreach(QGraphicsItem* it, mScene->items()) {
if (it->zValue() == z)
return false;
}
return true;
}
UBGraphicsScene::UBGraphicsScene(std::shared_ptr<UBDocumentProxy> document, bool enableUndoRedoStack)
: mEraser(0)
, mPointer(0)
, mMarkerCircle(0)
, mPenCircle(0)
, mDocument(document)
, mDarkBackground(false)
, mPageBackground(UBPageBackground::plain)
, mIsDesktopMode(false)
, mZoomFactor(1)
, mBackgroundObject(0)
, mPreviousWidth(0)
, mDistanceFromLastStrokePoint(0)
, mInputDeviceIsPressed(false)
, mArcPolygonItem(0)
, mRenderingContext(Screen)
, mCurrentStroke(0)
, mItemCount(0)
, mUndoRedoStackEnabled(enableUndoRedoStack)
, magniferControlViewWidget(0)
, magniferDisplayViewWidget(0)
, mZLayerController(new UBZLayerController(this))
, mpLastPolygon(NULL)
, mTempPolygon(NULL)
, mDrawWithCompass(false)
, mCurrentPolygon(0)
, mSelectionFrame(0)
, mGraphicsCache(nullptr)
{
UBCoreGraphicsScene::setObjectName("BoardScene");
setItemIndexMethod(BspTreeIndex);
setUuid(QUuid::createUuid());
setDocument(document);
createEraiser();
createPointer();
createMarkerCircle();
createPenCircle();
if (UBApplication::applicationController)
{
setViewState(SceneViewState(1,
UBApplication::applicationController->initialHScroll(),
UBApplication::applicationController->initialVScroll()));
}
mBackgroundGridSize = UBSettings::settings()->crossSize;
mIntermediateLines = UBSettings::settings()->intermediateLines;
// Just for debug. Do not delete please
// connect(this, SIGNAL(selectionChanged()), this, SLOT(selectionChangedProcessing()));
connect(UBApplication::undoStack.data(), SIGNAL(indexChanged(int)), this, SLOT(updateSelectionFrameWrapper(int)));
connect(UBDrawingController::drawingController(), SIGNAL(stylusToolChanged(int,int)), this, SLOT(stylusToolChanged(int,int)));
}
UBGraphicsScene::~UBGraphicsScene()
{
if (mCurrentStroke && mCurrentStroke->polygons().empty()){
delete mCurrentStroke;
mCurrentStroke = NULL;
}
if (mZLayerController)
delete mZLayerController;
if (mGraphicsCache)
{
delete mGraphicsCache;
}
}
void UBGraphicsScene::selectionChangedProcessing()
{
if (selectedItems().count()){
UBApplication::showMessage("ZValue is " + QString::number(selectedItems().constFirst()->zValue(), 'f') + "own z value is "
+ QString::number(selectedItems().constFirst()->data(UBGraphicsItemData::ItemOwnZValue).toReal(), 'f'));
}
}
void UBGraphicsScene::setLastCenter(QPointF center)
{
mViewState.setLastSceneCenter(center);
}
QPointF UBGraphicsScene::lastCenter()
{
return mViewState.lastSceneCenter();
}
bool UBGraphicsScene::inputDevicePress(const QPointF& scenePos, const qreal& pressure)
{
bool accepted = false;
if (mInputDeviceIsPressed) {
qWarning() << "scene received input device pressed, without input device release, muting event as input device move";
accepted = inputDeviceMove(scenePos, pressure);
}
else {
mInputDeviceIsPressed = true;
UBStylusTool::Enum currentTool = (UBStylusTool::Enum)UBDrawingController::drawingController()->stylusTool();
if (UBDrawingController::drawingController()->isDrawingTool()) {
// -----------------------------------------------------------------
// We fall here if we are using the Pen, the Marker or the Line tool
// -----------------------------------------------------------------
qreal width = 0;
// delete current stroke, if not assigned to any polygon
if (mCurrentStroke && mCurrentStroke->polygons().empty()){
delete mCurrentStroke;
mCurrentStroke = NULL;
}
// hide the marker preview circle
if (currentTool == UBStylusTool::Marker)
hideMarkerCircle();
// hide the pen preview circle
if (currentTool == UBStylusTool::Pen)
hidePenCircle();
// ---------------------------------------------------------------
// Create a new Stroke. A Stroke is a collection of QGraphicsLines
// ---------------------------------------------------------------
mCurrentStroke = new UBGraphicsStroke(shared_from_this());
if (currentTool != UBStylusTool::Line){
// Handle the pressure
width = UBDrawingController::drawingController()->currentToolWidth() * pressure;
}
else{
// Ignore pressure for the line tool
width = UBDrawingController::drawingController()->currentToolWidth();
}
width /= UBApplication::boardController->systemScaleFactor();
width /= UBApplication::boardController->currentZoom();
mAddedItems.clear();
mRemovedItems.clear();
if (UBDrawingController::drawingController()->activeRuler())
UBDrawingController::drawingController()->activeRuler()->StartLine(scenePos, width);
else {
moveTo(scenePos);
drawLineTo(scenePos, width, UBDrawingController::drawingController()->stylusTool() == UBStylusTool::Line);
mCurrentStroke->addPoint(scenePos, width);
}
accepted = true;
}
else if (currentTool == UBStylusTool::Eraser) {
mAddedItems.clear();
mRemovedItems.clear();
moveTo(scenePos);
qreal eraserWidth = UBSettings::settings()->currentEraserWidth();
eraserWidth /= UBApplication::boardController->systemScaleFactor();
eraserWidth /= UBApplication::boardController->currentZoom();
eraseLineTo(scenePos, eraserWidth);
drawEraser(scenePos, mInputDeviceIsPressed);
accepted = true;
}
else if (currentTool == UBStylusTool::Pointer) {
drawPointer(scenePos, true);
accepted = true;
}
}
if (mCurrentStroke && mCurrentStroke->polygons().empty()){
delete mCurrentStroke;
mCurrentStroke = NULL;
}
return accepted;
}
bool UBGraphicsScene::inputDeviceMove(const QPointF& scenePos, const qreal& pressure)
{
bool accepted = false;
UBDrawingController *dc = UBDrawingController::drawingController();
UBStylusTool::Enum currentTool = (UBStylusTool::Enum)dc->stylusTool();
QPointF position = QPointF(scenePos);
if (currentTool == UBStylusTool::Eraser)
{
drawEraser(position, mInputDeviceIsPressed);
accepted = true;
}
else if (currentTool == UBStylusTool::Marker) {
if (mInputDeviceIsPressed)
hideMarkerCircle();
else {
drawMarkerCircle(position);
accepted = true;
}
}
else if (currentTool == UBStylusTool::Pen) {
if (mInputDeviceIsPressed)
hidePenCircle();
else {
drawPenCircle(position);
accepted = true;
}
}
if (mInputDeviceIsPressed)
{
if (dc->isDrawingTool())
{
qreal width = 0;
if (currentTool != UBStylusTool::Line){
// Handle the pressure
width = dc->currentToolWidth() * qMax(pressure, 0.2);
}else{
// Ignore pressure for line tool
width = dc->currentToolWidth();
}
width /= UBApplication::boardController->systemScaleFactor();
width /= UBApplication::boardController->currentZoom();
if (currentTool == UBStylusTool::Line || dc->activeRuler())
{
if (UBDrawingController::drawingController()->stylusTool() != UBStylusTool::Marker)
if(NULL != mpLastPolygon && NULL != mCurrentStroke && mAddedItems.size() > 0){
UBCoreGraphicsScene::removeItemFromDeletion(mpLastPolygon);
mAddedItems.remove(mpLastPolygon);
mCurrentStroke->remove(mpLastPolygon);
if (mCurrentStroke->polygons().empty()){
delete mCurrentStroke;
mCurrentStroke = NULL;
}
removeItem(mpLastPolygon);
mPreviousPolygonItems.removeAll(mpLastPolygon);
}
// ------------------------------------------------------------------------
// Here we wanna make sure that the Line will 'grip' at i*45, i*90 degrees
// ------------------------------------------------------------------------
QLineF radius(mPreviousPoint, position);
qreal angle = radius.angle();
angle = qRound(angle / 45) * 45;
qreal radiusLength = radius.length();
QPointF newPosition(
mPreviousPoint.x() + radiusLength * cos((angle * PI) / 180),
mPreviousPoint.y() - radiusLength * sin((angle * PI) / 180));
QLineF chord(position, newPosition);
if (chord.length() < qMin((int)16, (int)(radiusLength / 20)))
position = newPosition;
}
if (!mCurrentStroke)
mCurrentStroke = new UBGraphicsStroke(shared_from_this());
if(dc->activeRuler()){
dc->activeRuler()->DrawLine(position, width);
}
else if (currentTool == UBStylusTool::Line) {
drawLineTo(position, width, true);
}
else {
bool interpolate = false;
if ((currentTool == UBStylusTool::Pen && UBSettings::settings()->boardInterpolatePenStrokes->get().toBool())
|| (currentTool == UBStylusTool::Marker && UBSettings::settings()->boardInterpolateMarkerStrokes->get().toBool()))
{
interpolate = true;
}
// Don't draw segments smaller than a certain length. This can help with performance
// (less polygons to draw) but mostly with making the curve look smooth.
qreal antiScaleRatio = 1./(UBApplication::boardController->systemScaleFactor() * UBApplication::boardController->currentZoom());
qreal MIN_DISTANCE = 10*antiScaleRatio; // arbitrary. Move to settings if relevant.
qreal distance = QLineF(mPreviousPoint, scenePos).length();
mDistanceFromLastStrokePoint += distance;
if (mDistanceFromLastStrokePoint > MIN_DISTANCE) {
QList<QPair<QPointF, qreal> > newPoints = mCurrentStroke->addPoint(scenePos, width, interpolate);
if (newPoints.length() > 1)
drawCurve(newPoints);
mDistanceFromLastStrokePoint = 0;
}
if (interpolate) {
// Bezier curves aren't drawn all the way to the scenePos (they stop halfway between the previous and
// current scenePos), so we add a line from the last drawn position in the stroke and the
// scenePos, to make the drawing feel more responsive. This line is then deleted if a new segment is
// added to the stroke. (Or it is added to the stroke when we stop drawing)
if (mTempPolygon) {
removeItem(mTempPolygon);
mTempPolygon = NULL;
}
if (!mCurrentStroke->points().empty())
{
QPointF lastDrawnPoint = mCurrentStroke->points().last().first;
mTempPolygon = lineToPolygonItem(QLineF(lastDrawnPoint, scenePos), mPreviousWidth, width);
addItem(mTempPolygon);
}
}
}
}
else if (currentTool == UBStylusTool::Eraser)
{
qreal eraserWidth = UBSettings::settings()->currentEraserWidth();
eraserWidth /= UBApplication::boardController->systemScaleFactor();
eraserWidth /= UBApplication::boardController->currentZoom();
eraseLineTo(position, eraserWidth);
}
else if (currentTool == UBStylusTool::Pointer)
{
drawPointer(position);
}
accepted = true;
}
return accepted;
}
bool UBGraphicsScene::inputDeviceRelease(int tool)
{
bool accepted = false;
if (mPointer)
{
mPointer->hide();
accepted = true;
}
if (tool < 0)
{
tool = UBDrawingController::drawingController()->stylusTool();
}
UBStylusTool::Enum currentTool = (UBStylusTool::Enum)tool;
if (currentTool == UBStylusTool::Eraser)
hideEraser();
UBDrawingController *dc = UBDrawingController::drawingController();
if (dc->isDrawingTool() || mDrawWithCompass)
{
if(mArcPolygonItem){
UBGraphicsStrokesGroup* pStrokes = new UBGraphicsStrokesGroup();
// Add the arc
mAddedItems.remove(mArcPolygonItem);
removeItem(mArcPolygonItem);
UBCoreGraphicsScene::removeItemFromDeletion(mArcPolygonItem);
mArcPolygonItem->setStrokesGroup(pStrokes);
pStrokes->addToGroup(mArcPolygonItem);
// Add the center cross
foreach(QGraphicsItem* item, mAddedItems){
mAddedItems.remove(item);
removeItem(item);
UBCoreGraphicsScene::removeItemFromDeletion(item);
UBGraphicsPolygonItem* pi = qgraphicsitem_cast<UBGraphicsPolygonItem*>(item);
if (pi)
pi->setStrokesGroup(pStrokes);
pStrokes->addToGroup(item);
}
mAddedItems.clear();
mAddedItems << pStrokes;
addItem(pStrokes);
mDrawWithCompass = false;
}
else if (mCurrentStroke){
if (mTempPolygon) {
UBGraphicsPolygonItem * poly = dynamic_cast<UBGraphicsPolygonItem*>(mTempPolygon->deepCopy());
removeItem(mTempPolygon);
mTempPolygon = NULL;
addPolygonItemToCurrentStroke(poly);
}
// replace the stroke by a simplified version of it
if ((currentTool == UBStylusTool::Pen && UBSettings::settings()->boardSimplifyPenStrokes->get().toBool())
|| (currentTool == UBStylusTool::Marker && UBSettings::settings()->boardSimplifyMarkerStrokes->get().toBool()))
{
simplifyCurrentStroke();
}
UBGraphicsStrokesGroup* pStrokes = new UBGraphicsStrokesGroup();
// Remove the strokes that were just drawn here and replace them by a stroke item
foreach(UBGraphicsPolygonItem* poly, mCurrentStroke->polygons()){
mPreviousPolygonItems.removeAll(poly);
removeItem(poly);
UBCoreGraphicsScene::removeItemFromDeletion(poly);
poly->setStrokesGroup(pStrokes);
pStrokes->addToGroup(poly);
}
// TODO LATER : Generate well pressure-interpolated polygons and create the line group with them
mAddedItems.clear();
mAddedItems << pStrokes;
addItem(pStrokes);
if (mCurrentStroke->polygons().empty()){
delete mCurrentStroke;
mCurrentStroke = 0;
}
mCurrentPolygon = 0;
}
}
if (mRemovedItems.size() > 0 || mAddedItems.size() > 0)
{
if (mUndoRedoStackEnabled) { //should be deleted after scene own undo stack implemented
if (UBApplication::undoStack)
{
UBGraphicsItemUndoCommand* udcmd = new UBGraphicsItemUndoCommand(shared_from_this(), mRemovedItems, mAddedItems); //deleted by the undoStack
UBApplication::undoStack->push(udcmd);
}
}
mRemovedItems.clear();
mAddedItems.clear();
accepted = true;
}
mInputDeviceIsPressed = false;
setDocumentUpdated();
if (mCurrentStroke && mCurrentStroke->polygons().empty()){
delete mCurrentStroke;
}
mCurrentStroke = NULL;
return accepted;
}
void UBGraphicsScene::drawEraser(const QPointF &pPoint, bool pressed)
{
if (mEraser) {
qreal eraserWidth = UBSettings::settings()->currentEraserWidth();
eraserWidth /= UBApplication::boardController->systemScaleFactor();
eraserWidth /= UBApplication::boardController->currentZoom();
qreal eraserRadius = eraserWidth / 2;
// TODO UB 4.x optimize - no need to do that every time we move it
mEraser->setRect(QRectF(pPoint.x() - eraserRadius, pPoint.y() - eraserRadius, eraserWidth, eraserWidth));
redrawEraser(pressed);
}
}
void UBGraphicsScene::redrawEraser(bool pressed)
{
if (mEraser) {
QPen pen = mEraser->pen();
if(pressed)
pen.setStyle(Qt::SolidLine);
else
pen.setStyle(Qt::DotLine);
mEraser->setPen(pen);
mEraser->show();
}
}
void UBGraphicsScene::hideEraser()
{
if (mEraser)
mEraser->hide();
}
void UBGraphicsScene::drawPointer(const QPointF &pPoint, bool isFirstDraw)
{
qreal pointerDiameter = UBSettings::pointerDiameter / UBApplication::boardController->currentZoom();
pointerDiameter /= UBApplication::boardController->systemScaleFactor();
qreal pointerRadius = pointerDiameter / 2;
// TODO UB 4.x optimize - no need to do that every time we move it
if (mPointer) {
mPointer->setRect(QRectF(pPoint.x() - pointerRadius,
pPoint.y() - pointerRadius,
pointerDiameter,
pointerDiameter));
if(isFirstDraw) {
mPointer->show();
}
}
}
void UBGraphicsScene::drawMarkerCircle(const QPointF &pPoint)
{
if (mMarkerCircle) {
qreal markerDiameter = UBSettings::settings()->currentMarkerWidth();
markerDiameter /= UBApplication::boardController->systemScaleFactor();
markerDiameter /= UBApplication::boardController->currentZoom();
qreal markerRadius = markerDiameter/2;
mMarkerCircle->setRect(QRectF(pPoint.x() - markerRadius, pPoint.y() - markerRadius,
markerDiameter, markerDiameter));
mMarkerCircle->show();
}
}
void UBGraphicsScene::drawPenCircle(const QPointF &pPoint)
{
QCursor cursor;
if (mPenCircle && UBSettings::settings()->showPenPreviewCircle->get().toBool() &&
UBSettings::settings()->currentPenWidth() >= UBSettings::settings()->penPreviewFromSize->get().toInt()) {
qreal penDiameter = UBSettings::settings()->currentPenWidth();
penDiameter /= UBApplication::boardController->systemScaleFactor();
penDiameter /= UBApplication::boardController->currentZoom();
qreal penRadius = penDiameter/2;
mPenCircle->setRect(QRectF(pPoint.x() - penRadius, pPoint.y() - penRadius,
penDiameter, penDiameter));
mPenCircle->show();
cursor = Qt::BlankCursor;
}
else
{
cursor = UBResources::resources()->penCursor;
}
if (!UBDrawingController::drawingController()->activeRuler())
{
// set cursor only if no active ruler
if (controlView() && controlView()->viewport())
controlView()->viewport()->setCursor(cursor);
}
}
void UBGraphicsScene::hideMarkerCircle()
{
if (mMarkerCircle) {
mMarkerCircle->hide();
}
}
void UBGraphicsScene::hidePenCircle()
{
if (mPenCircle)
mPenCircle->hide();
}
// call this function when user release mouse button in Magnifier mode
void UBGraphicsScene::DisposeMagnifierQWidgets()
{
if(magniferControlViewWidget)
{
magniferControlViewWidget->hide();
magniferControlViewWidget->setParent(0);
delete magniferControlViewWidget;
magniferControlViewWidget = NULL;
}
if(magniferDisplayViewWidget)
{
magniferDisplayViewWidget->hide();
magniferDisplayViewWidget->setParent(0);
delete magniferDisplayViewWidget;
magniferDisplayViewWidget = NULL;
}
// some time have crash here on access to app (when call from destructor when close OpenBoard app)
// so i just add try/catch section here
try
{
UBApplication::app()->restoreOverrideCursor();
}
catch (...)
{
}
}
void UBGraphicsScene::moveTo(const QPointF &pPoint)
{
mPreviousPoint = pPoint;
mPreviousWidth = -1.0;
mPreviousPolygonItems.clear();
mArcPolygonItem = 0;
mDrawWithCompass = false;
}
void UBGraphicsScene::drawLineTo(const QPointF &pEndPoint, const qreal &pWidth, bool bLineStyle)
{
drawLineTo(pEndPoint, pWidth, pWidth, bLineStyle);
}
void UBGraphicsScene::drawLineTo(const QPointF &pEndPoint, const qreal &startWidth, const qreal &endWidth, bool bLineStyle)
{
if (mPreviousWidth == -1.0)
mPreviousWidth = startWidth;
qreal initialWidth = startWidth;
if (initialWidth == endWidth)
initialWidth = mPreviousWidth;
if (bLineStyle) {
QSetIterator<QGraphicsItem*> itItems(mAddedItems);
while (itItems.hasNext()) {
QGraphicsItem* item = itItems.next();
removeItem(item);
}
mAddedItems.clear();
}
UBGraphicsPolygonItem *polygonItem = lineToPolygonItem(QLineF(mPreviousPoint, pEndPoint), initialWidth, endWidth);
addPolygonItemToCurrentStroke(polygonItem);
if (!bLineStyle) {
mPreviousPoint = pEndPoint;
mPreviousWidth = endWidth;
}
}
void UBGraphicsScene::drawCurve(const QList<QPair<QPointF, qreal> >& points)
{
UBGraphicsPolygonItem* polygonItem = curveToPolygonItem(points);
addPolygonItemToCurrentStroke(polygonItem);
mPreviousPoint = points.last().first;
mPreviousWidth = points.last().second;
}
void UBGraphicsScene::drawCurve(const QList<QPointF>& points, qreal startWidth, qreal endWidth)
{
UBGraphicsPolygonItem* polygonItem = curveToPolygonItem(points, startWidth, endWidth);
addPolygonItemToCurrentStroke(polygonItem);
mPreviousWidth = endWidth;
mPreviousPoint = points.last();
}
void UBGraphicsScene::addPolygonItemToCurrentStroke(UBGraphicsPolygonItem* polygonItem)
{
if (!polygonItem->brush().isOpaque())
{
// -------------------------------------------------------------------------------------
// Here we substract the polygons that are overlapping in order to keep the transparency
// -------------------------------------------------------------------------------------
for (int i = 0; i < mPreviousPolygonItems.size(); i++)
{
UBGraphicsPolygonItem* previous = mPreviousPolygonItems.value(i);
polygonItem->subtract(previous);
}
}
mpLastPolygon = polygonItem;
mAddedItems.insert(polygonItem);
// Here we add the item to the scene
addItem(polygonItem);
if (!mCurrentStroke)
mCurrentStroke = new UBGraphicsStroke(shared_from_this());
polygonItem->setStroke(mCurrentStroke);