-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathUBBoardView.cpp
More file actions
1933 lines (1636 loc) · 62.9 KB
/
Copy pathUBBoardView.cpp
File metadata and controls
1933 lines (1636 loc) · 62.9 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
/*
* 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 "UBBoardView.h"
#include <QtGlobal>
#include <QtGui>
#include <QtXml>
#include <QListView>
#include "UBDrawingController.h"
#include "frameworks/UBGeometryUtils.h"
#include "frameworks/UBPlatformUtils.h"
#include "core/UBSettings.h"
#include "core/UBMimeData.h"
#include "core/UBApplication.h"
#include "core/UBSetting.h"
#include "core/UBPersistenceManager.h"
#include "core/UB.h"
#include "network/UBHttpGet.h"
#include "gui/UBStylusPalette.h"
#include "gui/UBRubberBand.h"
#include "gui/UBToolWidget.h"
#include "gui/UBResources.h"
#include "gui/UBMainWindow.h"
#include "gui/UBThumbnailWidget.h"
#include "board/UBBoardController.h"
#include "board/UBBoardPaletteManager.h"
#ifdef Q_OS_OSX
#include "core/UBApplicationController.h"
#include "desktop/UBDesktopAnnotationController.h"
#endif
#include "domain/UBGraphicsTextItem.h"
#include "domain/UBGraphicsPixmapItem.h"
#include "domain/UBGraphicsWidgetItem.h"
#include "domain/UBGraphicsPDFItem.h"
#include "domain/UBGraphicsPolygonItem.h"
#include "domain/UBItem.h"
#include "domain/UBGraphicsMediaItem.h"
#include "domain/UBGraphicsSvgItem.h"
#include "domain/UBGraphicsGroupContainerItem.h"
#include "domain/UBGraphicsStrokesGroup.h"
#include "domain/UBGraphicsItemDelegate.h"
#include "domain/UBGraphicsTextItemDelegate.h"
#include "document/UBDocumentProxy.h"
#include "tools/UBGraphicsRuler.h"
#include "tools/UBGraphicsAxes.h"
#include "tools/UBGraphicsCurtainItem.h"
#include "tools/UBGraphicsCompass.h"
#include "tools/UBGraphicsCache.h"
#include "tools/UBGraphicsTriangle.h"
#include "tools/UBGraphicsProtractor.h"
#include "core/memcheck.h"
UBBoardView::UBBoardView (UBBoardController* pController, QWidget* pParent, bool isControl, bool isDesktop)
: QGraphicsView (pParent)
, mController (pController)
, mIsCreatingTextZone (false)
, mIsCreatingSceneGrabZone (false)
, mOkOnWidget(false)
, _movingItem(nullptr)
, suspendedMousePressEvent(NULL)
, mLongPressInterval(350)
, mIsDragInProgress(false)
, mMultipleSelectionIsEnabled(false)
, bIsControl(isControl)
, bIsDesktop(isDesktop)
{
init ();
mFilterZIndex = false;
/*
mFilterZIndex = true;
mStartLayer = UBItemLayerType::FixedBackground;
mEndLayer = UBItemLayerType::Control;
*/
mLongPressTimer.setInterval(mLongPressInterval);
mLongPressTimer.setSingleShot(true);
}
UBBoardView::UBBoardView (UBBoardController* pController, int pStartLayer, int pEndLayer, QWidget* pParent, bool isControl, bool isDesktop)
: QGraphicsView (pParent)
, mController (pController)
, _movingItem(nullptr)
, suspendedMousePressEvent(NULL)
, mLongPressInterval(350)
, mIsDragInProgress(false)
, mMultipleSelectionIsEnabled(false)
, bIsControl(isControl)
, bIsDesktop(isDesktop)
{
init ();
mStartLayer = pStartLayer;
mEndLayer = pEndLayer;
mFilterZIndex = true;
mLongPressTimer.setInterval(mLongPressInterval);
mLongPressTimer.setSingleShot(true);
}
UBBoardView::~UBBoardView ()
{
if (suspendedMousePressEvent){
delete suspendedMousePressEvent;
suspendedMousePressEvent = NULL;
}
}
void UBBoardView::init ()
{
connect (UBSettings::settings ()->boardPenPressureSensitive, SIGNAL (changed (QVariant)),
this, SLOT (settingChanged (QVariant)));
connect (UBSettings::settings ()->boardMarkerPressureSensitive, SIGNAL (changed (QVariant)),
this, SLOT (settingChanged (QVariant)));
connect (UBSettings::settings ()->boardUseHighResTabletEvent, SIGNAL (changed (QVariant)),
this, SLOT (settingChanged (QVariant)));
connect(mController, &UBBoardController::controlViewportChanged, this, [this](){
if (scene())
{
scene()->controlViewportChanged();
}
});
setOptimizationFlags (QGraphicsView::IndirectPainting | QGraphicsView::DontSavePainterState); // enable UBBoardView::drawItems filter
setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
setWindowFlags (Qt::FramelessWindowHint);
setFrameStyle (QFrame::NoFrame);
setRenderHints (QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing);
setVerticalScrollBarPolicy (Qt::ScrollBarAlwaysOff);
setHorizontalScrollBarPolicy (Qt::ScrollBarAlwaysOff);
setAcceptDrops (true);
mTabletStylusIsPressed = false;
mMouseButtonIsPressed = false;
mPendingStylusReleaseEvent = false;
setCacheMode (QGraphicsView::CacheBackground);
mUsingTabletEraser = false;
mIsCreatingTextZone = false;
mRubberBand = 0;
mUBRubberBand = 0;
mVirtualKeyboardActive = false;
settingChanged (QVariant ());
unsetCursor();
setMovingItem(NULL);
mWidgetMoved = false;
}
std::shared_ptr<UBGraphicsScene> UBBoardView::scene ()
{
auto currentScene = dynamic_cast<UBGraphicsScene*>(QGraphicsView::scene());
return currentScene ? currentScene->shared_from_this() : nullptr;
}
void UBBoardView::keyPressEvent (QKeyEvent *event)
{
// send to the scene anyway
QApplication::sendEvent (scene().get(), event);
if (!event->isAccepted ())
{
switch (event->key ())
{
case Qt::Key_Up:
case Qt::Key_PageUp:
case Qt::Key_Left:
{
mController->previousScene ();
break;
}
case Qt::Key_Down:
case Qt::Key_PageDown:
case Qt::Key_Right:
case Qt::Key_Space:
{
mController->nextScene ();
break;
}
case Qt::Key_Home:
{
mController->firstScene ();
break;
}
case Qt::Key_End:
{
mController->lastScene ();
break;
}
case Qt::Key_Insert:
{
mController->addScene ();
break;
}
}
if (event->modifiers () & Qt::ControlModifier) // keep only ctrl/cmd keys
{
switch (event->key ())
{
case Qt::Key_Plus:
{
mController->zoomIn ();
event->accept ();
break;
}
case Qt::Key_Minus:
{
mController->zoomOut ();
event->accept ();
break;
}
case Qt::Key_0:
{
mController->zoomRestore ();
event->accept ();
break;
}
case Qt::Key_Left:
{
mController->handScroll (-100, 0);
event->accept ();
break;
}
case Qt::Key_Right:
{
mController->handScroll (100, 0);
event->accept ();
break;
}
case Qt::Key_Up:
{
mController->handScroll (0, -100);
event->accept ();
break;
}
case Qt::Key_Down:
{
mController->handScroll (0, 100);
event->accept ();
break;
}
default:
{
// NOOP
}
}
}
}
}
bool UBBoardView::event (QEvent * e)
{
if (e->type () == QEvent::Gesture)
{
QGestureEvent *gestureEvent = dynamic_cast<QGestureEvent *> (e);
if (gestureEvent)
{
QSwipeGesture* swipe = dynamic_cast<QSwipeGesture*> (gestureEvent->gesture (Qt::SwipeGesture));
if (swipe)
{
if (swipe->horizontalDirection () == QSwipeGesture::Left)
{
mController->previousScene ();
gestureEvent->setAccepted (swipe, true);
}
if (swipe->horizontalDirection () == QSwipeGesture::Right)
{
mController->nextScene ();
gestureEvent->setAccepted (swipe, true);
}
}
}
}
return QGraphicsView::event (e);
}
void UBBoardView::tabletEvent (QTabletEvent * event)
{
if (!mUseHighResTabletEvent) {
event->setAccepted (false);
return;
}
UBDrawingController *dc = UBDrawingController::drawingController ();
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
QPointF tabletPos = event->position();
#else
QPointF tabletPos = event->posF();
#endif
UBStylusTool::Enum currentTool = (UBStylusTool::Enum)dc->stylusTool ();
if (event->type () == QEvent::TabletPress || event->type () == QEvent::TabletEnterProximity) {
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
if (event->pointerType () == QPointingDevice::PointerType::Eraser) {
#else
if (event->pointerType () == QTabletEvent::Eraser) {
#endif
dc->setStylusTool (UBStylusTool::Eraser);
mUsingTabletEraser = true;
}
else {
if (mUsingTabletEraser && currentTool == UBStylusTool::Eraser)
dc->setStylusTool (dc->latestDrawingTool ());
mUsingTabletEraser = false;
}
}
QPointF scenePos = viewportTransform ().inverted ().map (tabletPos);
qreal pressure = 1.0;
if (((currentTool == UBStylusTool::Pen || currentTool == UBStylusTool::Line) && mPenPressureSensitive) ||
(currentTool == UBStylusTool::Marker && mMarkerPressureSensitive))
pressure = event->pressure ();
else{
//Explanation: rerouting to mouse event
event->setAccepted (false);
return;
}
bool acceptEvent = true;
#ifdef Q_OS_OSX
//Work around #1388. After selecting annotation tool in desktop mode, annotation view appears on top when
//using Mac OS X. In this case tablet event should send mouse event so as to let user interact with
//stylus palette.
Q_ASSERT(UBApplication::applicationController->uninotesController());
if (UBApplication::applicationController->uninotesController()->drawingView() == this) {
if (UBApplication::applicationController->uninotesController()->desktopPalettePath().contains(event->pos())) {
acceptEvent = false;
}
}
#endif
switch (event->type ()) {
case QEvent::TabletPress: {
mTabletStylusIsPressed = true;
scene()->inputDevicePress (scenePos, pressure);
break;
}
case QEvent::TabletMove: {
if (mTabletStylusIsPressed)
scene ()->inputDeviceMove (scenePos, pressure);
acceptEvent = false; // rerouted to mouse move
break;
}
case QEvent::TabletRelease: {
UBStylusTool::Enum currentTool = (UBStylusTool::Enum)dc->stylusTool ();
scene ()->setToolCursor (currentTool);
setToolCursor (currentTool);
scene ()->inputDeviceRelease ();
mPendingStylusReleaseEvent = false;
mTabletStylusIsPressed = false;
mMouseButtonIsPressed = false;
break;
}
default: {
//NOOP - avoid compiler warning
}
}
// ignore mouse press and mouse move tablet event so that it is rerouted to mouse events,
// documented in QTabletEvent Class Reference:
/* The event handler QWidget::tabletEvent() receives all three types of tablet events.
Qt will first send a tabletEvent then, if it is not accepted, it will send a mouse event. */
//
// This is a workaround to the fact that tablet event are not delivered to child widget (like palettes)
//
event->setAccepted (acceptEvent);
}
bool UBBoardView::itemIsLocked(QGraphicsItem *item)
{
if (!item)
return false;
return item->data(UBGraphicsItemData::ItemLocked).toBool();
}
bool UBBoardView::itemHaveParentWithType(QGraphicsItem *item, int type)
{
if (!item)
return false;
if (type == item->type())
return true;
return itemHaveParentWithType(item->parentItem(), type);
}
bool UBBoardView::isUBItem(QGraphicsItem *item)
{
if ((UBGraphicsItemType::UserTypesCount > item->type()) && (item->type() > QGraphicsItem::UserType))
return true;
return false;
}
bool UBBoardView::isCppTool(QGraphicsItem *item)
{
return (item->type() == UBGraphicsItemType::CompassItemType
|| item->type() == UBGraphicsItemType::RulerItemType
|| item->type() == UBGraphicsItemType::AxesItemType
|| item->type() == UBGraphicsItemType::ProtractorItemType
|| item->type() == UBGraphicsItemType::TriangleItemType
|| item->type() == UBGraphicsItemType::CurtainItemType);
}
void UBBoardView::handleItemsSelection(QGraphicsItem *item)
{
// we need to select new pressed itemOnBoard and deselect all other items.
// the trouble is in:
// some items can has parents (groupped items or strokes, or strokes in groups).
// some items is already selected and we don't need to reselect them
//
// item selection managed by QGraphicsView::mousePressEvent(). It should be called later.
if (item)
{
// item has group as first parent - it is any item or UBGraphicsStrokesGroup.
if (getMovingItem())
{
if (getMovingItem()->parentItem())
{
if(item->parentItem() && UBGraphicsGroupContainerItem::Type == getMovingItem()->parentItem()->type())
return;
}
}
// delegate buttons shouldn't selected
if (DelegateButton::Type == item->type())
return;
// click on svg items (images on Frame) shouldn't change selection.
if (QGraphicsSvgItem::Type == item->type())
return;
// Delegate frame shouldn't selected
if (UBGraphicsDelegateFrame::Type == item->type())
return;
// if we need to uwe multiple selection - we shouldn't deselect other items.
if (!isMultipleSelectionEnabled())
{
// here we need to determine what item is pressed. We should work
// only with UB items.
if ((UBGraphicsItemType::UserTypesCount > item->type()) && (item->type() > QGraphicsItem::UserType))
{
scene()->deselectAllItemsExcept(item);
scene()->updateSelectionFrame();
}
}
}
}
bool UBBoardView::itemShouldReceiveMousePressEvent(QGraphicsItem *item)
{
/*
Some items should receive mouse press events averytime,
some items should receive that events when they are selected,
some items shouldn't receive mouse press events at mouse press, but should receive them at mouse release (suspended mouse press event)
Here we determines cases when items should to get mouse press event at pressing on mouse.
*/
if (!item)
return true;
// for now background objects is not interactable, but it can be deprecated for some items in the future.
if (item == scene()->backgroundObject())
return false;
// some behavior depends on current tool.
UBStylusTool::Enum currentTool = (UBStylusTool::Enum)UBDrawingController::drawingController()->stylusTool();
switch(item->type())
{
case UBGraphicsProtractor::Type:
case UBGraphicsRuler::Type:
case UBGraphicsAxes::Type:
case UBGraphicsTriangle::Type:
case UBGraphicsCompass::Type:
case UBGraphicsCache::Type:
return true;
case UBGraphicsDelegateFrame::Type:
if (currentTool == UBStylusTool::Play)
return false;
return true;
case UBGraphicsPixmapItem::Type:
case UBGraphicsSvgItem::Type:
if (currentTool == UBStylusTool::Play)
return true;
if (item->isSelected())
return true;
else
return false;
case DelegateButton::Type:
return true;
case UBGraphicsMediaItem::Type:
case UBGraphicsVideoItem::Type:
case UBGraphicsAudioItem::Type:
return false;
case UBGraphicsTextItem::Type:
if (currentTool == UBStylusTool::Play)
return true;
if ((currentTool == UBStylusTool::Selector) && item->isSelected())
return true;
if ((currentTool == UBStylusTool::Selector) && item->parentItem() && item->parentItem()->isSelected())
return true;
if (currentTool != UBStylusTool::Selector)
return false;
break;
case UBGraphicsItemType::StrokeItemType:
if (currentTool == UBStylusTool::Play || currentTool == UBStylusTool::Selector)
return true;
break;
// Groups shouldn't reacts on any presses and moves for Play tool.
case UBGraphicsGroupContainerItem::Type:
if(currentTool == UBStylusTool::Play)
{
setMovingItem(NULL);
return true;
}
return false;
break;
case QGraphicsProxyWidget::Type:
return true;
case UBGraphicsWidgetItem::Type:
if (currentTool == UBStylusTool::Selector && item->parentItem() && item->parentItem()->isSelected())
return true;
if (currentTool == UBStylusTool::Selector && item->isSelected())
return true;
if (currentTool == UBStylusTool::Play)
return true;
return false;
break;
}
return !isUBItem(item); // standard behavior of QGraphicsScene for not UB items. UB items should be managed upper.
}
bool UBBoardView::itemShouldReceiveSuspendedMousePressEvent(QGraphicsItem *item)
{
if (!item)
return false;
if (item == scene()->backgroundObject())
return false;
UBStylusTool::Enum currentTool = (UBStylusTool::Enum)UBDrawingController::drawingController()->stylusTool();
switch(item->type())
{
case UBGraphicsPixmapItem::Type:
case UBGraphicsSvgItem::Type:
case UBGraphicsTextItem::Type:
case UBGraphicsWidgetItem::Type:
if (currentTool == UBStylusTool::Selector && !item->isSelected() && item->parentItem())
return true;
if (currentTool == UBStylusTool::Selector && item->isSelected())
return true;
break;
case DelegateButton::Type:
case UBGraphicsMediaItem::Type:
case UBGraphicsVideoItem::Type:
case UBGraphicsAudioItem::Type:
return true;
}
return false;
}
bool UBBoardView::itemShouldBeMoved(QGraphicsItem *item)
{
if (!item)
return false;
if (item == scene()->backgroundObject())
return false;
if (!(mMouseButtonIsPressed || mTabletStylusIsPressed))
return false;
if (getMovingItem())
{
if (getMovingItem()->data(UBGraphicsItemData::ItemLocked).toBool())
return false;
if (getMovingItem()->parentItem())
{
if (UBGraphicsGroupContainerItem::Type == getMovingItem()->parentItem()->type() && !getMovingItem()->isSelected() && getMovingItem()->parentItem()->isSelected())
return false;
}
}
UBStylusTool::Enum currentTool = (UBStylusTool::Enum)UBDrawingController::drawingController()->stylusTool();
switch(item->type())
{
case UBGraphicsCurtainItem::Type:
case UBGraphicsGroupContainerItem::Type:
return true;
case UBGraphicsWidgetItem::Type:
if(currentTool == UBStylusTool::Selector && item->isSelected())
return false;
if(currentTool == UBStylusTool::Play)
return false;
Q_FALLTHROUGH();
case UBGraphicsSvgItem::Type:
case UBGraphicsPixmapItem::Type:
if (currentTool == UBStylusTool::Play || !item->isSelected())
return true;
if (item->isSelected())
return false;
Q_FALLTHROUGH();
case UBGraphicsMediaItem::Type:
case UBGraphicsVideoItem::Type:
case UBGraphicsAudioItem::Type:
return true;
case UBGraphicsStrokesGroup::Type:
case UBGraphicsTextItem::Type:
if (currentTool == UBStylusTool::Play)
return true;
else
return !item->isSelected();
}
return false;
}
QGraphicsItem* UBBoardView::determineItemToPress(QGraphicsItem *item)
{
if(item)
{
UBStylusTool::Enum currentTool = (UBStylusTool::Enum)UBDrawingController::drawingController()->stylusTool();
// if item is on group and group is not selected - group should take press.
if (UBStylusTool::Selector == currentTool
&& item->parentItem()
&& UBGraphicsGroupContainerItem::Type == item->parentItem()->type()
&& !item->parentItem()->isSelected())
return item->parentItem();
// items like polygons placed in two groups nested, so we need to recursive call.
if(item->parentItem() && UBGraphicsStrokesGroup::Type == item->parentItem()->type())
return determineItemToPress(item->parentItem());
}
return item;
}
// determine item to interacts: item self or it's container.
QGraphicsItem* UBBoardView::determineItemToMove(QGraphicsItem *item)
{
if(item)
{
UBStylusTool::Enum currentTool = (UBStylusTool::Enum)UBDrawingController::drawingController()->stylusTool();
//W3C widgets should take mouse move events from play tool.
if ((UBStylusTool::Play == currentTool) && (UBGraphicsWidgetItem::Type == item->type()))
return item;
// if item is in group
if(item->parentItem() && UBGraphicsGroupContainerItem::Type == item->parentItem()->type())
{
// play tool should move groups by any element
if (UBStylusTool::Play == currentTool && item->parentItem()->isSelected())
return item->parentItem();
// groups should should be moved instead of strokes groups
if (UBGraphicsStrokesGroup::Type == item->type())
return item->parentItem();
// selected groups should be moved by moving any element
if (item->parentItem()->isSelected())
return item;
if (item->isSelected())
return NULL;
return item->parentItem();
}
// items like polygons placed in two groups nested, so we need to recursive call.
if(item->parentItem() && UBGraphicsStrokesGroup::Type == item->parentItem()->type())
return determineItemToMove(item->parentItem());
}
return item;
}
void UBBoardView::handleItemMousePress(QMouseEvent *event)
{
mLastPressedMousePos = mapToScene(event->pos());
// Determining item who will take mouse press event
//all other items will be deselected and if all item will be deselected, then
// wrong item can catch mouse press. because selected items placed on the top
setMovingItem(determineItemToPress(getMovingItem()));
handleItemsSelection(getMovingItem());
if (isMultipleSelectionEnabled())
return;
if (itemShouldReceiveMousePressEvent(getMovingItem())){
QGraphicsView::mousePressEvent (event);
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
QPointF eventPosition = event->position();
#else
QPointF eventPosition = event->localPos();
#endif
QGraphicsItem* item = determineItemToPress(scene()->itemAt(this->mapToScene(eventPosition.toPoint()), transform()));
//use QGraphicsView::transform() to use not deprecated QGraphicsScene::itemAt() method
// NOTE @letsfindaway obsolete, probably from UBThumbnailProxyWidget
if (item && (item->type() == QGraphicsProxyWidget::Type) && item->parentObject() && item->parentObject()->type() != QGraphicsProxyWidget::Type)
{
//Clean up children
QList<QGraphicsItem*> children = item->childItems();
for( QList<QGraphicsItem*>::iterator it = children.begin(); it != children.end(); ++it )
if ((*it)->pos().x() < 0 || (*it)->pos().y() < 0)
(*it)->setPos(0,item->boundingRect().size().height());
}
}
else
{
if (getMovingItem())
{
UBGraphicsItem *graphicsItem = dynamic_cast<UBGraphicsItem*>(getMovingItem());
if (graphicsItem)
graphicsItem->Delegate()->startUndoStep();
getMovingItem()->clearFocus();
}
if (suspendedMousePressEvent)
{
delete suspendedMousePressEvent;
suspendedMousePressEvent = NULL;
}
if (itemShouldReceiveSuspendedMousePressEvent(getMovingItem()))
{
suspendedMousePressEvent = new QMouseEvent(event->type(), event->pos(), event->button(), event->buttons(), event->modifiers());
}
}
}
void UBBoardView::handleItemMouseMove(QMouseEvent *event)
{
// determine item to move (maybee we need to move group of item or his parent.
setMovingItem(determineItemToMove(getMovingItem()));
// items should be moved not every mouse move.
if (getMovingItem() && itemShouldBeMoved(getMovingItem()) && (mMouseButtonIsPressed || mTabletStylusIsPressed))
{
QPointF scenePos = mapToScene(event->pos());
QPointF newPos = getMovingItem()->pos() + scenePos - mLastPressedMousePos;
getMovingItem()->setPos(newPos);
mLastPressedMousePos = scenePos;
mWidgetMoved = true;
event->accept();
}
else
{
QPointF posBeforeMove;
QPointF posAfterMove;
if (getMovingItem())
{
posBeforeMove = getMovingItem()->pos();
QGraphicsView::mouseMoveEvent (event);
// At the end of a d'n'd, QGraphicsView::mouseMoveEvent triggers dropEvent, setting moving item to null
// so we must check movingItem again
if (getMovingItem())
posAfterMove = getMovingItem()->pos();
}
else
{
if (!mMouseButtonIsPressed)
{
QGraphicsView::mouseMoveEvent(event);
}
}
mWidgetMoved = ((posAfterMove-posBeforeMove).manhattanLength() != 0);
// a cludge for terminate moving of w3c widgets.
// in some cases w3c widgets catches mouse move and doesn't sends that events to web page,
// at simple - in google map widget - mouse move events doesn't comes to web page from rectangle of wearch bar on bottom right corner of widget.
if (getMovingItem())
{
if (mWidgetMoved && UBGraphicsW3CWidgetItem::Type == getMovingItem()->type())
getMovingItem()->setPos(posBeforeMove);
}
}
}
void UBBoardView::rubberItems()
{
if (mUBRubberBand)
mRubberedItems = items(mUBRubberBand->geometry());
foreach(QGraphicsItem *item, mRubberedItems)
{
if (item->parentItem() && UBGraphicsGroupContainerItem::Type == item->parentItem()->type())
mRubberedItems.removeOne(item);
}
}
void UBBoardView::moveRubberedItems(QPointF movingVector)
{
QRectF invalidateRect = scene()->itemsBoundingRect();
foreach (QGraphicsItem *item, mRubberedItems)
{
if (item->type() == UBGraphicsW3CWidgetItem::Type
|| item->type() == UBGraphicsPixmapItem::Type
|| item->type() == UBGraphicsMediaItem::Type
|| item->type() == UBGraphicsVideoItem::Type
|| item->type() == UBGraphicsAudioItem::Type
|| item->type() == UBGraphicsSvgItem::Type
|| item->type() == UBGraphicsTextItem::Type
|| item->type() == UBGraphicsStrokesGroup::Type
|| item->type() == UBGraphicsGroupContainerItem::Type)
{
item->setPos(item->pos()+movingVector);
}
}
scene()->invalidate(invalidateRect);
}
void UBBoardView::setMultiselection(bool enable)
{
mMultipleSelectionIsEnabled = enable;
}
// work around for handling tablet events on MAC OS with Qt 4.8.0 and above
#if defined(Q_OS_OSX)
bool UBBoardView::directTabletEvent(QEvent *event)
{
QTabletEvent *tEvent = static_cast<QTabletEvent *>(event);
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
const QPointingDevice *device = dynamic_cast<const QPointingDevice*>(tEvent->device());
tEvent = new QTabletEvent(tEvent->type()
, device
, mapFromGlobal(tEvent->pos())
, tEvent->globalPos()
, tEvent->pressure()
, tEvent->xTilt()
, tEvent->yTilt()
, tEvent->tangentialPressure()
, tEvent->rotation()
, tEvent->z()
, tEvent->modifiers()
, tEvent->button()
, tEvent->buttons());
#else
tEvent = new QTabletEvent(tEvent->type()
, mapFromGlobal(tEvent->pos())
, tEvent->globalPos()
, tEvent->device()
, tEvent->pointerType()
, tEvent->pressure()
, tEvent->xTilt()
, tEvent->yTilt()
, tEvent->tangentialPressure()
, tEvent->rotation()
, tEvent->z()
, tEvent->modifiers()
, tEvent->uniqueId());
#endif
if (geometry().contains(tEvent->pos()))
{
if (NULL == widgetForTabletEvent(this->parentWidget(), tEvent->pos()))
{
tabletEvent(tEvent);
return true;
}
}
return false;
}
QWidget *UBBoardView::widgetForTabletEvent(QWidget *w, const QPoint &pos)
{
Q_ASSERT(w);
// it should work that, but it doesn't. So we check if it is control view.
//UBBoardView *board = qobject_cast<UBBoardView *>(w);
UBBoardView *board = UBApplication::boardController->controlView();
QWidget *childAtPos = NULL;
QList<QObject *> childs = w->children();
foreach(QObject *child, childs)
{
QWidget *childWidget = qobject_cast<QWidget *>(child);
if (childWidget)
{
if (childWidget->isVisible() && childWidget->geometry().contains(pos))
{
QWidget *lastChild = widgetForTabletEvent(childWidget, pos);
if (board && board->viewport() == lastChild)
continue;
if (NULL != lastChild)
childAtPos = lastChild;
else
childAtPos = childWidget;
break;
}
else
childAtPos = NULL;
}
}