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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
235
236
237
238
239
240
241
242
243
244
245
258#include <QtCore/qdatetime.h>
259#include <QtCore/qdebug.h>
260#include <QtCore/qmath.h>
261#include <QtCore/qscopedvaluerollback.h>
262#include <QtWidgets/qapplication.h>
263#include <QtGui/qevent.h>
264#include <QtWidgets/qlayout.h>
265#include <QtGui/qtransform.h>
266#include <QtGui/qpainter.h>
267#include <QtGui/qpainterpath.h>
268#include <QtWidgets/qscrollbar.h>
269#include <QtWidgets/qstyleoption.h>
271#include <private/qevent_p.h>
272#include <QtGui/private/qeventpoint_p.h>
280 if (d <= (qreal) INT_MIN)
282 else if (d >= (qreal) INT_MAX)
284 return d >= 0.0 ?
int(d + 0.5) :
int(d -
int(d-1) + 0.5) +
int(d-1);
287void QGraphicsViewPrivate::translateTouchEvent(QGraphicsViewPrivate *d, QTouchEvent *touchEvent)
289 for (
int i = 0; i < touchEvent->pointCount(); ++i) {
290 auto &pt = touchEvent->point(i);
293 QMutableEventPoint::setScenePosition(pt, d->mapToScene(pt.position()));
299
300
301QGraphicsViewPrivate::QGraphicsViewPrivate()
302 : renderHints(QPainter::TextAntialiasing),
303 dragMode(QGraphicsView::NoDrag),
304 sceneInteractionAllowed(
true), hasSceneRect(
false),
305 connectedToScene(
false),
306 useLastMouseEvent(
false),
307 identityMatrix(
true),
309 accelerateScrolling(
true),
310 keepLastCenterPoint(
true),
312 handScrolling(
false),
313 mustAllocateStyleOptions(
false),
314 mustResizeBackgroundPixmap(
true),
315 fullUpdatePending(
true),
316 hasUpdateClip(
false),
317 mousePressButton(Qt::NoButton),
318 leftIndent(0), topIndent(0),
319 alignment(Qt::AlignCenter),
320 transformationAnchor(QGraphicsView::AnchorViewCenter), resizeAnchor(QGraphicsView::NoAnchor),
321 viewportUpdateMode(QGraphicsView::MinimalViewportUpdate),
323#if QT_CONFIG(rubberband)
324 rubberBanding(
false),
325 rubberBandSelectionMode(Qt::IntersectsItemShape),
326 rubberBandSelectionOperation(Qt::ReplaceSelection),
328 handScrollMotions(0),
330 hasStoredOriginalCursor(
false),
332 lastDragDropEvent(
nullptr),
333 updateSceneSlotReimplementedChecked(
false)
335 styleOptions.reserve(QGRAPHICSVIEW_PREALLOC_STYLE_OPTIONS);
338QGraphicsViewPrivate::~QGraphicsViewPrivate()
343
344
345void QGraphicsViewPrivate::recalculateContentSize()
349 const QSize maxSize = q->maximumViewportSize();
350 int width = maxSize.width();
351 int height = maxSize.height();
352 const QRectF viewRect = matrix.mapRect(q->sceneRect());
354 bool frameOnlyAround = (q->style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents,
nullptr, q));
355 if (frameOnlyAround) {
356 if (hbarpolicy == Qt::ScrollBarAlwaysOn)
357 height -= frameWidth * 2;
358 if (vbarpolicy == Qt::ScrollBarAlwaysOn)
359 width -= frameWidth * 2;
364 const int scrollBarExtent = q->style()->pixelMetric(QStyle::PM_ScrollBarExtent,
nullptr, q)
365 + (frameOnlyAround ? frameWidth * 2 : 0);
369 bool useHorizontalScrollBar = (viewRect.width() > width) && hbarpolicy == Qt::ScrollBarAsNeeded;
370 bool useVerticalScrollBar = (viewRect.height() > height) && vbarpolicy == Qt::ScrollBarAsNeeded;
371 if (useHorizontalScrollBar && vbarpolicy == Qt::ScrollBarAsNeeded) {
372 if (viewRect.height() > height - scrollBarExtent)
373 useVerticalScrollBar =
true;
375 if (useVerticalScrollBar && hbarpolicy == Qt::ScrollBarAsNeeded) {
376 if (viewRect.width() > width - scrollBarExtent)
377 useHorizontalScrollBar =
true;
379 if (useHorizontalScrollBar)
380 height -= scrollBarExtent;
381 if (useVerticalScrollBar)
382 width -= scrollBarExtent;
387 const QPointF savedLastCenterPoint = lastCenterPoint;
390 const qreal oldLeftIndent = leftIndent;
391 const qreal oldTopIndent = topIndent;
393 const auto singleStep = defaultSingleStep();
397 const int left = q_round_bound(viewRect.left());
398 const int right = q_round_bound(viewRect.right() - width);
400 switch (alignment & Qt::AlignHorizontal_Mask) {
402 leftIndent = -viewRect.left();
405 leftIndent = maxSize.width() - viewRect.width() - viewRect.left() - 1;
407 case Qt::AlignHCenter:
409 leftIndent = maxSize.width() / 2 - (viewRect.left() + viewRect.right()) / 2;
413 hbar->setRange(0, 0);
417 hbar->setRange(left, right);
418 hbar->setPageStep(width);
419 hbar->setSingleStep(width / singleStep);
421 if (oldLeftIndent != 0)
422 hbar->setValue(-oldLeftIndent);
427 const int top = q_round_bound(viewRect.top());
428 const int bottom = q_round_bound(viewRect.bottom() - height);
430 switch (alignment & Qt::AlignVertical_Mask) {
432 topIndent = -viewRect.top();
434 case Qt::AlignBottom:
435 topIndent = maxSize.height() - viewRect.height() - viewRect.top() - 1;
437 case Qt::AlignVCenter:
439 topIndent = maxSize.height() / 2 - (viewRect.top() + viewRect.bottom()) / 2;
443 vbar->setRange(0, 0);
447 vbar->setRange(top, bottom);
448 vbar->setPageStep(height);
449 vbar->setSingleStep(height / singleStep);
451 if (oldTopIndent != 0)
452 vbar->setValue(-oldTopIndent);
456 lastCenterPoint = savedLastCenterPoint;
461 if (oldLeftIndent != leftIndent || oldTopIndent != topIndent) {
464 }
else if (q->isRightToLeft() && !leftIndent) {
472 if (cacheMode & QGraphicsView::CacheBackground) {
474 mustResizeBackgroundPixmap =
true;
479
480
481void QGraphicsViewPrivate::centerView(QGraphicsView::ViewportAnchor anchor)
485 case QGraphicsView::AnchorUnderMouse: {
486 if (q->underMouse()) {
489 QPointF transformationDiff = mapToScene(viewport->rect().toRectF().center())
490 - mapToScene(viewport->mapFromGlobal(QCursor::pos().toPointF()));
491 q->centerOn(lastMouseMoveScenePoint + transformationDiff);
493 q->centerOn(lastCenterPoint);
497 case QGraphicsView::AnchorViewCenter:
498 q->centerOn(lastCenterPoint);
500 case QGraphicsView::NoAnchor:
506
507
508void QGraphicsViewPrivate::updateLastCenterPoint()
510 lastCenterPoint = mapToScene(viewport->rect().toRectF().center());
514
515
516
517
518
519qint64 QGraphicsViewPrivate::horizontalScroll()
const
522 const_cast<QGraphicsViewPrivate *>(
this)->updateScroll();
527
528
529
530
531
532qint64 QGraphicsViewPrivate::verticalScroll()
const
535 const_cast<QGraphicsViewPrivate *>(
this)->updateScroll();
540
541
542
543
544QRectF QGraphicsViewPrivate::mapRectToScene(
const QRect &rect)
const
547 const_cast<QGraphicsViewPrivate *>(
this)->updateScroll();
548 QRectF scrolled = QRectF(rect.translated(scrollX, scrollY));
549 return identityMatrix ? scrolled : matrix.inverted().mapRect(scrolled);
554
555
556
557
558QRectF QGraphicsViewPrivate::mapRectFromScene(
const QRectF &rect)
const
561 const_cast<QGraphicsViewPrivate *>(
this)->updateScroll();
562 return (identityMatrix ? rect : matrix.mapRect(rect)).translated(-scrollX, -scrollY);
566
567
568void QGraphicsViewPrivate::updateScroll()
571 scrollX = qint64(-leftIndent);
572 if (q->isRightToLeft()) {
574 scrollX += hbar->minimum();
575 scrollX += hbar->maximum();
576 scrollX -= hbar->value();
579 scrollX += hbar->value();
582 scrollY = qint64(vbar->value() - topIndent);
588
589
590
591
592
593bool QGraphicsViewPrivate::canStartScrollingAt(
const QPoint &startPos)
const
595 Q_Q(
const QGraphicsView);
596 if (q->dragMode() != QGraphicsView::NoDrag)
599 const QGraphicsItem *childItem = q->itemAt(startPos);
601 if (!startPos.isNull() && childItem && (childItem->flags() & QGraphicsItem::ItemIsMovable))
604 return QAbstractScrollAreaPrivate::canStartScrollingAt(startPos);
608
609
610void QGraphicsViewPrivate::replayLastMouseEvent()
612 if (!useLastMouseEvent || !scene)
614 mouseMoveEventHandler(&*lastMouseEvent);
618
619
620void QGraphicsViewPrivate::storeMouseEvent(QMouseEvent *event)
622 useLastMouseEvent =
true;
624 lastMouseEvent.storeUnlessAlias(*event);
627void QGraphicsViewPrivate::mouseMoveEventHandler(QMouseEvent *event)
631#if QT_CONFIG(rubberband)
632 updateRubberBand(event);
635 storeMouseEvent(event);
636 lastMouseEvent->setAccepted(
false);
638 if (!sceneInteractionAllowed)
645 QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseMove);
646 mouseEvent.setWidget(viewport);
647 mouseEvent.setButtonDownScenePos(mousePressButton, mousePressScenePoint);
648 mouseEvent.setButtonDownScreenPos(mousePressButton, mousePressScreenPoint);
649 mouseEvent.setScenePos(q->mapToScene(event->position().toPoint()));
650 mouseEvent.setScreenPos(event->globalPosition().toPoint());
651 mouseEvent.setLastScenePos(lastMouseMoveScenePoint);
652 mouseEvent.setLastScreenPos(lastMouseMoveScreenPoint);
653 mouseEvent.setButtons(event->buttons());
654 mouseEvent.setButton(event->button());
655 mouseEvent.setModifiers(event->modifiers());
656 mouseEvent.setSource(event->source());
657 mouseEvent.setFlags(event->flags());
658 mouseEvent.setTimestamp(event->timestamp());
659 lastMouseMoveScenePoint = mouseEvent.scenePos();
660 lastMouseMoveScreenPoint = mouseEvent.screenPos();
661 mouseEvent.setAccepted(
false);
662 if (event->spontaneous())
663 qt_sendSpontaneousEvent(scene, &mouseEvent);
665 QCoreApplication::sendEvent(scene, &mouseEvent);
668 lastMouseEvent->setAccepted(mouseEvent.isAccepted());
670 if (mouseEvent.isAccepted() && mouseEvent.buttons() != 0) {
681 if (scene->d_func()->allItemsIgnoreHoverEvents && !scene->d_func()->allItemsUseDefaultCursor
682 && scene->d_func()->cachedItemsUnderMouse.isEmpty()) {
683 scene->d_func()->cachedItemsUnderMouse = scene->d_func()->itemsAtPosition(mouseEvent.screenPos(),
684 mouseEvent.scenePos(),
685 mouseEvent.widget());
688 for (QGraphicsItem *item : std::as_const(scene->d_func()->cachedItemsUnderMouse)) {
689 if (item->isEnabled() && item->hasCursor()) {
690 _q_setViewportCursor(item->cursor());
696 if (hasStoredOriginalCursor) {
698 hasStoredOriginalCursor =
false;
699 viewport->setCursor(originalCursor);
705
706
707#if QT_CONFIG(rubberband)
708QRegion QGraphicsViewPrivate::rubberBandRegion(
const QWidget *widget,
const QRect &rect)
const
710 QStyleHintReturnMask mask;
711 QStyleOptionRubberBand option;
712 option.initFrom(widget);
714 option.opaque =
false;
715 option.shape = QRubberBand::Rectangle;
718 tmp += rect.adjusted(-1, -1, 1, 1);
719 if (widget->style()->styleHint(QStyle::SH_RubberBand_Mask, &option, widget, &mask))
724void QGraphicsViewPrivate::updateRubberBand(
const QMouseEvent *event)
727 if (dragMode != QGraphicsView::RubberBandDrag || !sceneInteractionAllowed || !rubberBanding)
730 if ((mousePressViewPoint - event->position().toPoint()).manhattanLength() < QApplication::startDragDistance())
734 if (viewportUpdateMode != QGraphicsView::NoViewportUpdate && !rubberBandRect.isEmpty()) {
735 if (viewportUpdateMode != QGraphicsView::FullViewportUpdate)
736 q->viewport()->update(rubberBandRegion(q->viewport(), rubberBandRect));
743 if (!event->buttons()) {
744 rubberBanding =
false;
745 rubberBandSelectionOperation = Qt::ReplaceSelection;
746 if (!rubberBandRect.isNull()) {
747 rubberBandRect = QRect();
748 emit q->rubberBandChanged(rubberBandRect, QPointF(), QPointF());
753 QRect oldRubberband = rubberBandRect;
756 const QPoint mp = q->mapFromScene(mousePressScenePoint);
757 const QPoint ep = event->position().toPoint();
758 rubberBandRect = QRect(qMin(mp.x(), ep.x()), qMin(mp.y(), ep.y()),
759 qAbs(mp.x() - ep.x()) + 1, qAbs(mp.y() - ep.y()) + 1);
761 if (rubberBandRect != oldRubberband || lastRubberbandScenePoint != lastMouseMoveScenePoint) {
762 lastRubberbandScenePoint = lastMouseMoveScenePoint;
763 oldRubberband = rubberBandRect;
764 emit q->rubberBandChanged(rubberBandRect, mousePressScenePoint, lastRubberbandScenePoint);
768 if (viewportUpdateMode != QGraphicsView::NoViewportUpdate) {
769 if (viewportUpdateMode != QGraphicsView::FullViewportUpdate)
770 q->viewport()->update(rubberBandRegion(q->viewport(), rubberBandRect));
775 QPainterPath selectionArea;
776 selectionArea.addPolygon(q->mapToScene(rubberBandRect));
777 selectionArea.closeSubpath();
779 scene->setSelectionArea(selectionArea, rubberBandSelectionOperation, rubberBandSelectionMode, q->viewportTransform());
782void QGraphicsViewPrivate::clearRubberBand()
785 if (dragMode != QGraphicsView::RubberBandDrag || !sceneInteractionAllowed || !rubberBanding)
788 if (viewportUpdateMode != QGraphicsView::NoViewportUpdate) {
789 if (viewportUpdateMode != QGraphicsView::FullViewportUpdate)
790 q->viewport()->update(rubberBandRegion(q->viewport(), rubberBandRect));
795 rubberBanding =
false;
796 rubberBandSelectionOperation = Qt::ReplaceSelection;
797 if (!rubberBandRect.isNull()) {
798 rubberBandRect = QRect();
799 emit q->rubberBandChanged(rubberBandRect, QPointF(), QPointF());
805
806
808void QGraphicsViewPrivate::_q_setViewportCursor(
const QCursor &cursor)
810 if (!hasStoredOriginalCursor) {
811 hasStoredOriginalCursor =
true;
812 originalCursor = viewport->cursor();
814 viewport->setCursor(cursor);
819
820
822void QGraphicsViewPrivate::_q_unsetViewportCursor()
825 const auto items = q->items(lastMouseEvent->position().toPoint());
826 for (QGraphicsItem *item : items) {
827 if (item->isEnabled() && item->hasCursor()) {
828 _q_setViewportCursor(item->cursor());
834 if (hasStoredOriginalCursor) {
835 hasStoredOriginalCursor =
false;
836 if (dragMode == QGraphicsView::ScrollHandDrag)
837 viewport->setCursor(Qt::OpenHandCursor);
839 viewport->setCursor(originalCursor);
845
846
847void QGraphicsViewPrivate::storeDragDropEvent(
const QGraphicsSceneDragDropEvent *event)
849 delete lastDragDropEvent;
850 lastDragDropEvent =
new QGraphicsSceneDragDropEvent(event->type());
851 lastDragDropEvent->setScenePos(event->scenePos());
852 lastDragDropEvent->setScreenPos(event->screenPos());
853 lastDragDropEvent->setButtons(event->buttons());
854 lastDragDropEvent->setModifiers(event->modifiers());
855 lastDragDropEvent->setPossibleActions(event->possibleActions());
856 lastDragDropEvent->setProposedAction(event->proposedAction());
857 lastDragDropEvent->setDropAction(event->dropAction());
858 lastDragDropEvent->setMimeData(event->mimeData());
859 lastDragDropEvent->setWidget(event->widget());
860 lastDragDropEvent->setSource(event->source());
861 lastDragDropEvent->setTimestamp(event->timestamp());
865
866
867void QGraphicsViewPrivate::populateSceneDragDropEvent(QGraphicsSceneDragDropEvent *dest,
870#if QT_CONFIG(draganddrop)
872 dest->setScenePos(q->mapToScene(source->position().toPoint()));
873 dest->setScreenPos(q->mapToGlobal(source->position().toPoint()));
874 dest->setButtons(source->buttons());
875 dest->setModifiers(source->modifiers());
876 dest->setPossibleActions(source->possibleActions());
877 dest->setProposedAction(source->proposedAction());
878 dest->setDropAction(source->dropAction());
879 dest->setMimeData(source->mimeData());
880 dest->setWidget(viewport);
881 dest->setSource(qobject_cast<QWidget *>(source->source()));
889
890
891QTransform QGraphicsViewPrivate::mapToViewTransform(
const QGraphicsItem *item)
const
893 Q_Q(
const QGraphicsView);
895 const_cast<QGraphicsViewPrivate *>(
this)->updateScroll();
897 if (item->d_ptr->itemIsUntransformable())
898 return item->deviceTransform(q->viewportTransform());
903 const QGraphicsItem *parentItem = item;
904 const QGraphicsItemPrivate *itemd;
906 itemd = parentItem->d_ptr.data();
907 if (itemd->transformData)
909 offset += itemd->pos;
910 }
while ((parentItem = itemd->parent));
912 QTransform move = QTransform::fromTranslate(offset.x(), offset.y());
914 move.translate(-scrollX, -scrollY);
915 return identityMatrix ? move : matrix * move;
917 QTransform tr = parentItem->sceneTransform();
920 return move * tr * QTransform::fromTranslate(-scrollX, -scrollY);
923QRect QGraphicsViewPrivate::mapToViewRect(
const QGraphicsItem *item,
const QRectF &rect)
const
925 return mapToViewTransform(item).mapRect(rect).toAlignedRect();
929
930
931QRegion QGraphicsViewPrivate::mapToViewRegion(
const QGraphicsItem *item,
const QRectF &rect)
const
933 Q_Q(
const QGraphicsView);
935 const_cast<QGraphicsViewPrivate *>(
this)->updateScroll();
938 QTransform itv = item->deviceTransform(q->viewportTransform());
939 return item->boundingRegion(itv) & itv.mapRect(rect).toAlignedRect();
943
944
945void QGraphicsViewPrivate::processPendingUpdates()
950 if (fullUpdatePending) {
952 }
else if (viewportUpdateMode == QGraphicsView::BoundingRectViewportUpdate) {
953 viewport->update(dirtyBoundingRect);
955 viewport->update(dirtyRegion);
958 dirtyBoundingRect = QRect();
959 dirtyRegion = QRegion();
963{
return !(r.left() > width) && !(r.right() < 0) && !(r.top() >= height) && !(r.bottom() < 0); }
966{
return r.left() <= 0 && r.top() <= 0 && r.right() >= width - 1 && r.bottom() >= height - 1; }
970 if (rect->isEmpty()) {
973 rect->setCoords(qMin(rect->left(), other.left()), qMin(rect->top(), other.top()),
974 qMax(rect->right(), other.right()), qMax(rect->bottom(), other.bottom()));
979
980
981
982
983void QGraphicsViewPrivate::setUpdateClip(QGraphicsItem *item)
988 if (!item || viewportUpdateMode == QGraphicsView::NoViewportUpdate
989 || viewportUpdateMode == QGraphicsView::FullViewportUpdate) {
990 hasUpdateClip =
false;
999 if (item->d_ptr->itemIsUntransformable()) {
1000 QTransform xform = item->deviceTransform(q->viewportTransform());
1001 clip = xform.mapRect(item->boundingRect()).toAlignedRect();
1002 }
else if (item->d_ptr->sceneTransformTranslateOnly && identityMatrix) {
1003 QRectF r(item->boundingRect());
1004 r.translate(item->d_ptr->sceneTransform.dx() - horizontalScroll(),
1005 item->d_ptr->sceneTransform.dy() - verticalScroll());
1006 clip = r.toAlignedRect();
1007 }
else if (!q->isTransformed()) {
1008 clip = item->d_ptr->sceneTransform.mapRect(item->boundingRect()).toAlignedRect();
1010 QTransform xform = item->d_ptr->sceneTransform;
1011 xform *= q->viewportTransform();
1012 clip = xform.mapRect(item->boundingRect()).toAlignedRect();
1015 if (hasUpdateClip) {
1020 hasUpdateClip =
true;
1024bool QGraphicsViewPrivate::updateRegion(
const QRectF &rect,
const QTransform &xform)
1029 if (viewportUpdateMode != QGraphicsView::MinimalViewportUpdate
1030 && viewportUpdateMode != QGraphicsView::SmartViewportUpdate) {
1032 return updateRectF(xform.mapRect(rect));
1037 const QRegion region = xform.map(QRegion(rect.toAlignedRect()));
1038 QRect viewRect = region.boundingRect();
1039 const bool dontAdjustForAntialiasing = optimizationFlags & QGraphicsView::DontAdjustForAntialiasing;
1040 if (dontAdjustForAntialiasing)
1041 viewRect.adjust(-1, -1, 1, 1);
1043 viewRect.adjust(-2, -2, 2, 2);
1044 if (!intersectsViewport(viewRect, viewport->width(), viewport->height()))
1047 for (QRect viewRect : region) {
1048 if (dontAdjustForAntialiasing)
1049 viewRect.adjust(-1, -1, 1, 1);
1051 viewRect.adjust(-2, -2, 2, 2);
1053 viewRect &= updateClip;
1054 dirtyRegion += viewRect;
1062bool QGraphicsViewPrivate::updateRect(
const QRect &r)
1064 if (fullUpdatePending || viewportUpdateMode == QGraphicsView::NoViewportUpdate
1065 || !intersectsViewport(r, viewport->width(), viewport->height())) {
1069 switch (viewportUpdateMode) {
1070 case QGraphicsView::FullViewportUpdate:
1071 fullUpdatePending =
true;
1074 case QGraphicsView::BoundingRectViewportUpdate:
1076 QRect_unite(&dirtyBoundingRect, r & updateClip);
1078 QRect_unite(&dirtyBoundingRect, r);
1079 if (containsViewport(dirtyBoundingRect, viewport->width(), viewport->height())) {
1080 fullUpdatePending =
true;
1084 case QGraphicsView::SmartViewportUpdate:
1085 case QGraphicsView::MinimalViewportUpdate:
1087 dirtyRegion += r & updateClip;
1098QStyleOptionGraphicsItem *QGraphicsViewPrivate::allocStyleOptionsArray(
int numItems)
1100 if (mustAllocateStyleOptions || (numItems > styleOptions.capacity()))
1102 return new QStyleOptionGraphicsItem[numItems];
1105 if (numItems > styleOptions.size())
1106 styleOptions.resize(numItems);
1108 mustAllocateStyleOptions =
true;
1109 return styleOptions.data();
1112void QGraphicsViewPrivate::freeStyleOptionsArray(QStyleOptionGraphicsItem *array)
1114 mustAllocateStyleOptions =
false;
1115 if (array != styleOptions.data())
1119Q_GUI_EXPORT
extern QPainterPath qt_regionToPath(
const QRegion ®ion);
1122
1123
1124
1125
1128
1129
1130
1131
1132
1133
1134
1135
1136QList<QGraphicsItem *> QGraphicsViewPrivate::findItems(
const QRegion &exposedRegion,
bool *allItems,
1137 const QTransform &viewTransform)
const
1139 Q_Q(
const QGraphicsView);
1144 const QRectF exposedRegionSceneBounds = q->mapToScene(exposedRegion.boundingRect().adjusted(-1, -1, 1, 1))
1146 if (exposedRegionSceneBounds.contains(scene->sceneRect())) {
1151 return scene->items(Qt::AscendingOrder);
1157 bool simpleRectLookup = exposedRegion.rectCount() == 1 && matrix.type() <= QTransform::TxScale;
1158 if (simpleRectLookup) {
1159 return scene->items(exposedRegionSceneBounds,
1160 Qt::IntersectsItemBoundingRect,
1161 Qt::AscendingOrder, viewTransform);
1167 QRegion adjustedRegion;
1168 for (
const QRect &r : exposedRegion)
1169 adjustedRegion += r.adjusted(-1, -1, 1, 1);
1171 const QPainterPath exposedScenePath(q->mapToScene(qt_regionToPath(adjustedRegion)));
1172 return scene->items(exposedScenePath, Qt::IntersectsItemBoundingRect,
1173 Qt::AscendingOrder, viewTransform);
1177
1178
1179
1180
1181
1182
1183void QGraphicsViewPrivate::updateInputMethodSensitivity()
1186 QGraphicsItem *focusItem =
nullptr;
1187 bool enabled = scene && (focusItem = scene->focusItem())
1188 && (focusItem->d_ptr->flags & QGraphicsItem::ItemAcceptsInputMethod);
1189 q->setAttribute(Qt::WA_InputMethodEnabled, enabled);
1190 q->viewport()->setAttribute(Qt::WA_InputMethodEnabled, enabled);
1193 q->setInputMethodHints({ });
1197 QGraphicsProxyWidget *proxy = focusItem->d_ptr->isWidget && focusItem->d_ptr->isProxyWidget()
1198 ?
static_cast<QGraphicsProxyWidget *>(focusItem) :
nullptr;
1200 q->setInputMethodHints(focusItem->inputMethodHints());
1201 }
else if (QWidget *widget = proxy->widget()) {
1202 if (QWidget *fw = widget->focusWidget())
1204 q->setInputMethodHints(widget->inputMethodHints());
1206 q->setInputMethodHints({ });
1211
1212
1213QGraphicsView::QGraphicsView(QWidget *parent)
1214 : QAbstractScrollArea(*
new QGraphicsViewPrivate, parent)
1216 setViewport(
nullptr);
1217 setAcceptDrops(
true);
1218 setBackgroundRole(QPalette::Base);
1220 setAttribute(Qt::WA_InputMethodEnabled);
1221 viewport()->setAttribute(Qt::WA_InputMethodEnabled);
1225
1226
1227
1228QGraphicsView::QGraphicsView(QGraphicsScene *scene, QWidget *parent)
1229 : QAbstractScrollArea(*
new QGraphicsViewPrivate, parent)
1232 setViewport(
nullptr);
1233 setAcceptDrops(
true);
1234 setBackgroundRole(QPalette::Base);
1236 setAttribute(Qt::WA_InputMethodEnabled);
1237 viewport()->setAttribute(Qt::WA_InputMethodEnabled);
1241
1242
1243QGraphicsView::QGraphicsView(QGraphicsViewPrivate &dd, QWidget *parent)
1244 : QAbstractScrollArea(dd, parent)
1246 setViewport(
nullptr);
1247 setAcceptDrops(
true);
1248 setBackgroundRole(QPalette::Base);
1250 setAttribute(Qt::WA_InputMethodEnabled);
1251 viewport()->setAttribute(Qt::WA_InputMethodEnabled);
1255
1256
1257QGraphicsView::~QGraphicsView()
1261 d->scene->d_func()->views.removeAll(
this);
1262 delete d->lastDragDropEvent;
1266
1267
1268QSize QGraphicsView::sizeHint()
const
1270 Q_D(
const QGraphicsView);
1272 QSizeF baseSize = d->matrix.mapRect(sceneRect()).size();
1273 baseSize += QSizeF(d->frameWidth * 2, d->frameWidth * 2);
1274 return baseSize.boundedTo((3 * QGuiApplication::primaryScreen()->virtualSize()) / 4).toSize();
1276 return QAbstractScrollArea::sizeHint();
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294QPainter::RenderHints QGraphicsView::renderHints()
const
1296 Q_D(
const QGraphicsView);
1297 return d->renderHints;
1299void QGraphicsView::setRenderHints(QPainter::RenderHints hints)
1302 if (hints == d->renderHints)
1304 d->renderHints = hints;
1309
1310
1311
1312
1313
1314void QGraphicsView::setRenderHint(QPainter::RenderHint hint,
bool enabled)
1317 QPainter::RenderHints oldHints = d->renderHints;
1318 d->renderHints.setFlag(hint, enabled);
1319 if (oldHints != d->renderHints)
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335Qt::Alignment QGraphicsView::alignment()
const
1337 Q_D(
const QGraphicsView);
1338 return d->alignment;
1340void QGraphicsView::setAlignment(Qt::Alignment alignment)
1343 if (d->alignment != alignment) {
1344 d->alignment = alignment;
1345 d->recalculateContentSize();
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367QGraphicsView::ViewportAnchor QGraphicsView::transformationAnchor()
const
1369 Q_D(
const QGraphicsView);
1370 return d->transformationAnchor;
1372void QGraphicsView::setTransformationAnchor(ViewportAnchor anchor)
1375 d->transformationAnchor = anchor;
1379 if (d->transformationAnchor == AnchorUnderMouse)
1380 d->viewport->setMouseTracking(
true);
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399QGraphicsView::ViewportAnchor QGraphicsView::resizeAnchor()
const
1401 Q_D(
const QGraphicsView);
1402 return d->resizeAnchor;
1404void QGraphicsView::setResizeAnchor(ViewportAnchor anchor)
1407 d->resizeAnchor = anchor;
1411 if (d->resizeAnchor == AnchorUnderMouse)
1412 d->viewport->setMouseTracking(
true);
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433QGraphicsView::ViewportUpdateMode QGraphicsView::viewportUpdateMode()
const
1435 Q_D(
const QGraphicsView);
1436 return d->viewportUpdateMode;
1438void QGraphicsView::setViewportUpdateMode(ViewportUpdateMode mode)
1441 d->viewportUpdateMode = mode;
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462QGraphicsView::OptimizationFlags QGraphicsView::optimizationFlags()
const
1464 Q_D(
const QGraphicsView);
1465 return d->optimizationFlags;
1467void QGraphicsView::setOptimizationFlags(OptimizationFlags flags)
1470 d->optimizationFlags = flags;
1474
1475
1476
1477
1478void QGraphicsView::setOptimizationFlag(OptimizationFlag flag,
bool enabled)
1481 d->optimizationFlags.setFlag(flag, enabled);
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498QGraphicsView::DragMode QGraphicsView::dragMode()
const
1500 Q_D(
const QGraphicsView);
1503void QGraphicsView::setDragMode(DragMode mode)
1506 if (d->dragMode == mode)
1509#if QT_CONFIG(rubberband)
1510 d->clearRubberBand();
1514 if (d->dragMode == ScrollHandDrag)
1515 viewport()->unsetCursor();
1522 if (d->dragMode == ScrollHandDrag && mode == NoDrag && d->handScrolling)
1523 d->handScrolling =
false;
1528 if (d->dragMode == ScrollHandDrag) {
1530 d->hasStoredOriginalCursor =
false;
1531 viewport()->setCursor(Qt::OpenHandCursor);
1536#if QT_CONFIG(rubberband)
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550Qt::ItemSelectionMode QGraphicsView::rubberBandSelectionMode()
const
1552 Q_D(
const QGraphicsView);
1553 return d->rubberBandSelectionMode;
1555void QGraphicsView::setRubberBandSelectionMode(Qt::ItemSelectionMode mode)
1558 d->rubberBandSelectionMode = mode;
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1573QRect QGraphicsView::rubberBandRect()
const
1575 Q_D(
const QGraphicsView);
1576 if (d->dragMode != QGraphicsView::RubberBandDrag || !d->sceneInteractionAllowed || !d->rubberBanding)
1579 return d->rubberBandRect;
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603QGraphicsView::CacheMode QGraphicsView::cacheMode()
const
1605 Q_D(
const QGraphicsView);
1606 return d->cacheMode;
1608void QGraphicsView::setCacheMode(CacheMode mode)
1611 if (mode == d->cacheMode)
1613 d->cacheMode = mode;
1614 resetCachedContent();
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630void QGraphicsView::resetCachedContent()
1633 if (d->cacheMode == CacheNone)
1636 if (d->cacheMode & CacheBackground) {
1638 d->mustResizeBackgroundPixmap =
true;
1640 }
else if (d->mustResizeBackgroundPixmap) {
1643 d->mustResizeBackgroundPixmap =
false;
1644 d->backgroundPixmap = QPixmap();
1645 d->backgroundPixmapExposed = QRegion();
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665void QGraphicsView::invalidateScene(
const QRectF &rect, QGraphicsScene::SceneLayers layers)
1668 if ((layers & QGraphicsScene::BackgroundLayer) && !d->mustResizeBackgroundPixmap) {
1669 QRect viewRect = mapFromScene(rect).boundingRect();
1670 if (viewport()->rect().intersects(viewRect)) {
1673 d->backgroundPixmapExposed += viewRect;
1675 d->scene->update(rect);
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690bool QGraphicsView::isInteractive()
const
1692 Q_D(
const QGraphicsView);
1693 return d->sceneInteractionAllowed;
1695void QGraphicsView::setInteractive(
bool allowed)
1698 d->sceneInteractionAllowed = allowed;
1702
1703
1704
1705
1706
1707QGraphicsScene *QGraphicsView::scene()
const
1709 Q_D(
const QGraphicsView);
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723void QGraphicsView::setScene(QGraphicsScene *scene)
1726 if (d->scene == scene)
1734 disconnect(d->scene, SIGNAL(changed(QList<QRectF>)),
1735 this, SLOT(updateScene(QList<QRectF>)));
1736 disconnect(d->scene, SIGNAL(sceneRectChanged(QRectF)),
1737 this, SLOT(updateSceneRect(QRectF)));
1738 d->scene->d_func()->removeView(
this);
1739 d->connectedToScene =
false;
1741 if (isActiveWindow() && isVisible()) {
1742 QEvent windowDeactivate(QEvent::WindowDeactivate);
1743 QCoreApplication::sendEvent(d->scene, &windowDeactivate);
1746 d->scene->clearFocus();
1750 if ((d->scene = scene)) {
1751 connect(d->scene, SIGNAL(sceneRectChanged(QRectF)),
1752 this, SLOT(updateSceneRect(QRectF)));
1753 d->updateSceneSlotReimplementedChecked =
false;
1754 d->scene->d_func()->addView(
this);
1755 d->recalculateContentSize();
1756 d->lastCenterPoint = sceneRect().center();
1757 d->keepLastCenterPoint =
true;
1760 if (!d->scene->d_func()->allItemsIgnoreHoverEvents
1761 || !d->scene->d_func()->allItemsUseDefaultCursor) {
1762 d->viewport->setMouseTracking(
true);
1766 if (!d->scene->d_func()->allItemsIgnoreTouchEvents)
1767 d->viewport->setAttribute(Qt::WA_AcceptTouchEvents);
1769 if (isActiveWindow() && isVisible()) {
1770 QEvent windowActivate(QEvent::WindowActivate);
1771 QCoreApplication::sendEvent(d->scene, &windowActivate);
1774 d->recalculateContentSize();
1777 d->updateInputMethodSensitivity();
1779 if (d->scene && hasFocus())
1780 d->scene->setFocus();
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806QRectF QGraphicsView::sceneRect()
const
1808 Q_D(
const QGraphicsView);
1809 if (d->hasSceneRect)
1810 return d->sceneRect;
1812 return d->scene->sceneRect();
1815void QGraphicsView::setSceneRect(
const QRectF &rect)
1818 d->hasSceneRect = !rect.isNull();
1819 d->sceneRect = rect;
1820 d->recalculateContentSize();
1824
1825
1826
1827
1828void QGraphicsView::rotate(qreal angle)
1831 QTransform matrix = d->matrix;
1832 matrix.rotate(angle);
1833 setTransform(matrix);
1837
1838
1839
1840
1841void QGraphicsView::scale(qreal sx, qreal sy)
1844 QTransform matrix = d->matrix;
1845 matrix.scale(sx, sy);
1846 setTransform(matrix);
1850
1851
1852
1853
1854void QGraphicsView::shear(qreal sh, qreal sv)
1857 QTransform matrix = d->matrix;
1858 matrix.shear(sh, sv);
1859 setTransform(matrix);
1863
1864
1865
1866
1867void QGraphicsView::translate(qreal dx, qreal dy)
1870 QTransform matrix = d->matrix;
1871 matrix.translate(dx, dy);
1872 setTransform(matrix);
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887void QGraphicsView::centerOn(
const QPointF &pos)
1890 qreal width = viewport()->width();
1891 qreal height = viewport()->height();
1892 QPointF viewPoint = d->matrix.map(pos);
1893 QPointF oldCenterPoint = pos;
1895 if (!d->leftIndent) {
1896 if (isRightToLeft()) {
1897 qint64 horizontal = 0;
1898 horizontal += horizontalScrollBar()->minimum();
1899 horizontal += horizontalScrollBar()->maximum();
1900 horizontal -= qRound(viewPoint.x() - width / 2.0);
1901 horizontalScrollBar()->setValue(horizontal);
1903 horizontalScrollBar()->setValue(qRound(viewPoint.x() - width / 2.0));
1907 verticalScrollBar()->setValue(qRound(viewPoint.y() - height / 2.0));
1908 d->lastCenterPoint = oldCenterPoint;
1912
1913
1914
1915
1916
1917
1920
1921
1922
1923
1924
1925
1926
1927void QGraphicsView::centerOn(
const QGraphicsItem *item)
1929 centerOn(item->sceneBoundingRect().center());
1933
1934
1935
1936
1937
1938
1939
1940
1941void QGraphicsView::ensureVisible(
const QRectF &rect,
int xmargin,
int ymargin)
1944 qreal width = viewport()->width();
1945 qreal height = viewport()->height();
1946 QRectF viewRect = d->matrix.mapRect(rect);
1948 qreal left = d->horizontalScroll();
1949 qreal right = left + width;
1950 qreal top = d->verticalScroll();
1951 qreal bottom = top + height;
1953 if (viewRect.left() <= left + xmargin) {
1956 horizontalScrollBar()->setValue(
int(viewRect.left() - xmargin - 0.5));
1958 if (viewRect.right() >= right - xmargin) {
1961 horizontalScrollBar()->setValue(
int(viewRect.right() - width + xmargin + 0.5));
1963 if (viewRect.top() <= top + ymargin) {
1966 verticalScrollBar()->setValue(
int(viewRect.top() - ymargin - 0.5));
1968 if (viewRect.bottom() >= bottom - ymargin) {
1971 verticalScrollBar()->setValue(
int(viewRect.bottom() - height + ymargin + 0.5));
1976
1977
1978
1979
1980
1981
1982
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994void QGraphicsView::ensureVisible(
const QGraphicsItem *item,
int xmargin,
int ymargin)
1996 ensureVisible(item->sceneBoundingRect(), xmargin, ymargin);
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023void QGraphicsView::fitInView(
const QRectF &rect, Qt::AspectRatioMode aspectRatioMode)
2026 if (!d->scene || rect.isNull())
2030 QRectF unity = d->matrix.mapRect(QRectF(0, 0, 1, 1));
2031 if (unity.isEmpty())
2033 scale(1 / unity.width(), 1 / unity.height());
2037 QRectF viewRect = viewport()->rect().adjusted(margin, margin, -margin, -margin);
2038 if (viewRect.isEmpty())
2040 QRectF sceneRect = d->matrix.mapRect(rect);
2041 if (sceneRect.isEmpty())
2043 qreal xratio = viewRect.width() / sceneRect.width();
2044 qreal yratio = viewRect.height() / sceneRect.height();
2047 switch (aspectRatioMode) {
2048 case Qt::KeepAspectRatio:
2049 xratio = yratio = qMin(xratio, yratio);
2051 case Qt::KeepAspectRatioByExpanding:
2052 xratio = yratio = qMax(xratio, yratio);
2054 case Qt::IgnoreAspectRatio:
2059 scale(xratio, yratio);
2060 centerOn(rect.center());
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2076
2077
2078
2079
2080
2081
2082
2083void QGraphicsView::fitInView(
const QGraphicsItem *item, Qt::AspectRatioMode aspectRatioMode)
2085 QPainterPath path = item->isClipped() ? item->clipPath() : item->shape();
2086 if (item->d_ptr->hasTranslateOnlySceneTransform()) {
2087 path.translate(item->d_ptr->sceneTransform.dx(), item->d_ptr->sceneTransform.dy());
2088 fitInView(path.boundingRect(), aspectRatioMode);
2090 fitInView(item->d_ptr->sceneTransform.map(path).boundingRect(), aspectRatioMode);
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114void QGraphicsView::render(QPainter *painter,
const QRectF &target,
const QRect &source,
2115 Qt::AspectRatioMode aspectRatioMode)
2120 if (!d->scene || !(painter && painter->isActive()))
2124 QRect sourceRect = source;
2125 if (source.isNull())
2126 sourceRect = viewport()->rect();
2129 QRectF targetRect = target;
2130 if (target.isNull()) {
2131 if (painter->device()->devType() == QInternal::Picture)
2132 targetRect = sourceRect;
2134 targetRect.setRect(0, 0, painter->device()->width(), painter->device()->height());
2138 qreal xratio = targetRect.width() / sourceRect.width();
2139 qreal yratio = targetRect.height() / sourceRect.height();
2142 switch (aspectRatioMode) {
2143 case Qt::KeepAspectRatio:
2144 xratio = yratio = qMin(xratio, yratio);
2146 case Qt::KeepAspectRatioByExpanding:
2147 xratio = yratio = qMax(xratio, yratio);
2149 case Qt::IgnoreAspectRatio:
2155 QPolygonF sourceScenePoly = mapToScene(sourceRect.adjusted(-1, -1, 1, 1));
2156 QList<QGraphicsItem *> itemList = d->scene->items(sourceScenePoly,
2157 Qt::IntersectsItemBoundingRect);
2158 QGraphicsItem **itemArray =
new QGraphicsItem *[itemList.size()];
2159 int numItems = itemList.size();
2160 for (
int i = 0; i < numItems; ++i)
2161 itemArray[numItems - i - 1] = itemList.at(i);
2165 QTransform moveMatrix = QTransform::fromTranslate(-d->horizontalScroll(), -d->verticalScroll());
2166 QTransform painterMatrix = d->matrix * moveMatrix;
2167 painterMatrix *= QTransform()
2168 .translate(targetRect.left(), targetRect.top())
2169 .scale(xratio, yratio)
2170 .translate(-sourceRect.left(), -sourceRect.top());
2173 QStyleOptionGraphicsItem *styleOptionArray = d->allocStyleOptionsArray(numItems);
2174 for (
int i = 0; i < numItems; ++i)
2175 itemArray[i]->d_ptr->initStyleOption(&styleOptionArray[i], painterMatrix, targetRect.toRect());
2180 painter->setClipRect(targetRect);
2182 path.addPolygon(sourceScenePoly);
2183 path.closeSubpath();
2184 painter->setClipPath(painterMatrix.map(path), Qt::IntersectClip);
2187 painter->setTransform(painterMatrix,
true);
2190 QRectF sourceSceneRect = sourceScenePoly.boundingRect();
2191 drawBackground(painter, sourceSceneRect);
2192 drawItems(painter, numItems, itemArray, styleOptionArray);
2193 drawForeground(painter, sourceSceneRect);
2195 delete [] itemArray;
2196 d->freeStyleOptionsArray(styleOptionArray);
2202
2203
2204
2205
2206
2207
2208QList<QGraphicsItem *> QGraphicsView::items()
const
2210 Q_D(
const QGraphicsView);
2212 return QList<QGraphicsItem *>();
2213 return d->scene->items();
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232QList<QGraphicsItem *> QGraphicsView::items(
const QPoint &pos)
const
2234 Q_D(
const QGraphicsView);
2236 return QList<QGraphicsItem *>();
2240 if ((d->identityMatrix || d->matrix.type() <= QTransform::TxScale)) {
2242 QTransform xinv = viewportTransform().inverted();
2243 return d->scene->items(xinv.mapRect(QRectF(pos.x(), pos.y(), 1, 1)),
2244 Qt::IntersectsItemShape,
2245 Qt::DescendingOrder,
2246 viewportTransform());
2249 return d->scene->items(mapToScene(pos.x(), pos.y(), 1, 1),
2250 Qt::IntersectsItemShape,
2251 Qt::DescendingOrder,
2252 viewportTransform());
2256
2257
2258
2259
2260
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277QList<QGraphicsItem *> QGraphicsView::items(
const QRect &rect, Qt::ItemSelectionMode mode)
const
2279 Q_D(
const QGraphicsView);
2281 return QList<QGraphicsItem *>();
2282 return d->scene->items(mapToScene(rect), mode, Qt::DescendingOrder, viewportTransform());
2286
2287
2288
2289
2290
2291
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308QList<QGraphicsItem *> QGraphicsView::items(
const QPolygon &polygon, Qt::ItemSelectionMode mode)
const
2310 Q_D(
const QGraphicsView);
2312 return QList<QGraphicsItem *>();
2313 return d->scene->items(mapToScene(polygon), mode, Qt::DescendingOrder, viewportTransform());
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328QList<QGraphicsItem *> QGraphicsView::items(
const QPainterPath &path, Qt::ItemSelectionMode mode)
const
2330 Q_D(
const QGraphicsView);
2332 return QList<QGraphicsItem *>();
2333 return d->scene->items(mapToScene(path), mode, Qt::DescendingOrder, viewportTransform());
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349QGraphicsItem *QGraphicsView::itemAt(
const QPoint &pos)
const
2351 Q_D(
const QGraphicsView);
2354 const QList<QGraphicsItem *> itemsAtPos = items(pos);
2355 return itemsAtPos.isEmpty() ?
nullptr : itemsAtPos.first();
2359
2360
2361
2362
2363
2364
2367
2368
2369
2370
2371
2372
2373
2374
2375QPointF QGraphicsView::mapToScene(
const QPoint &point)
const
2377 Q_D(
const QGraphicsView);
2379 p.rx() += d->horizontalScroll();
2380 p.ry() += d->verticalScroll();
2381 return d->identityMatrix ? p : d->matrix.inverted().map(p);
2385
2386
2387
2388
2389
2392
2393
2394
2395
2396
2397QPolygonF QGraphicsView::mapToScene(
const QRect &rect)
const
2399 Q_D(
const QGraphicsView);
2400 if (!rect.isValid())
2403 QPointF scrollOffset(d->horizontalScroll(), d->verticalScroll());
2404 QRect r = rect.adjusted(0, 0, 1, 1);
2405 QPointF tl = scrollOffset + r.topLeft();
2406 QPointF tr = scrollOffset + r.topRight();
2407 QPointF br = scrollOffset + r.bottomRight();
2408 QPointF bl = scrollOffset + r.bottomLeft();
2411 if (!d->identityMatrix) {
2412 QTransform x = d->matrix.inverted();
2413 poly[0] = x.map(tl);
2414 poly[1] = x.map(tr);
2415 poly[2] = x.map(br);
2416 poly[3] = x.map(bl);
2427
2428
2429
2430
2431
2434
2435
2436
2437
2438
2439QPolygonF QGraphicsView::mapToScene(
const QPolygon &polygon)
const
2442 poly.reserve(polygon.size());
2443 for (
const QPoint &point : polygon)
2444 poly << mapToScene(point);
2449
2450
2451
2452
2453
2454QPainterPath QGraphicsView::mapToScene(
const QPainterPath &path)
const
2456 Q_D(
const QGraphicsView);
2457 QTransform matrix = QTransform::fromTranslate(d->horizontalScroll(), d->verticalScroll());
2458 matrix *= d->matrix.inverted();
2459 return matrix.map(path);
2463
2464
2465
2466
2467QPoint QGraphicsView::mapFromScene(
const QPointF &point)
const
2469 Q_D(
const QGraphicsView);
2470 QPointF p = d->identityMatrix ? point : d->matrix.map(point);
2471 p.rx() -= d->horizontalScroll();
2472 p.ry() -= d->verticalScroll();
2477
2478
2479
2480
2481
2484
2485
2486
2487
2488
2489QPolygon QGraphicsView::mapFromScene(
const QRectF &rect)
const
2491 Q_D(
const QGraphicsView);
2496 if (!d->identityMatrix) {
2497 const QTransform &x = d->matrix;
2498 tl = x.map(rect.topLeft());
2499 tr = x.map(rect.topRight());
2500 br = x.map(rect.bottomRight());
2501 bl = x.map(rect.bottomLeft());
2503 tl = rect.topLeft();
2504 tr = rect.topRight();
2505 br = rect.bottomRight();
2506 bl = rect.bottomLeft();
2508 QPointF scrollOffset(d->horizontalScroll(), d->verticalScroll());
2515 poly[0] = tl.toPoint();
2516 poly[1] = tr.toPoint();
2517 poly[2] = br.toPoint();
2518 poly[3] = bl.toPoint();
2523
2524
2525
2526
2527
2530
2531
2532
2533
2534
2535QPolygon QGraphicsView::mapFromScene(
const QPolygonF &polygon)
const
2538 poly.reserve(polygon.size());
2539 for (
const QPointF &point : polygon)
2540 poly << mapFromScene(point);
2545
2546
2547
2548
2549
2550QPainterPath QGraphicsView::mapFromScene(
const QPainterPath &path)
const
2552 Q_D(
const QGraphicsView);
2553 QTransform matrix = d->matrix;
2554 matrix *= QTransform::fromTranslate(-d->horizontalScroll(), -d->verticalScroll());
2555 return matrix.map(path);
2559
2560
2561QVariant QGraphicsView::inputMethodQuery(Qt::InputMethodQuery query)
const
2563 Q_D(
const QGraphicsView);
2567 QVariant value = d->scene->inputMethodQuery(query);
2568 if (value.userType() == QMetaType::QRectF)
2569 value = d->mapRectFromScene(value.toRectF());
2570 else if (value.userType() == QMetaType::QPointF)
2571 value = mapFromScene(value.toPointF());
2572 else if (value.userType() == QMetaType::QRect)
2573 value = d->mapRectFromScene(value.toRect()).toRect();
2574 else if (value.userType() == QMetaType::QPoint)
2575 value = mapFromScene(value.toPoint());
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592QBrush QGraphicsView::backgroundBrush()
const
2594 Q_D(
const QGraphicsView);
2595 return d->backgroundBrush;
2597void QGraphicsView::setBackgroundBrush(
const QBrush &brush)
2600 d->backgroundBrush = brush;
2603 if (d->cacheMode & CacheBackground) {
2605 d->mustResizeBackgroundPixmap =
true;
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622QBrush QGraphicsView::foregroundBrush()
const
2624 Q_D(
const QGraphicsView);
2625 return d->foregroundBrush;
2627void QGraphicsView::setForegroundBrush(
const QBrush &brush)
2630 d->foregroundBrush = brush;
2635
2636
2637
2638
2639void QGraphicsView::updateScene(
const QList<QRectF> &rects)
2647 if (d->fullUpdatePending || d->viewportUpdateMode == QGraphicsView::NoViewportUpdate)
2651 QList<QRect> dirtyViewportRects;
2652 dirtyViewportRects.reserve(d->dirtyRegion.rectCount() + rects.size());
2653 for (
const QRect &dirtyRect : d->dirtyRegion)
2654 dirtyViewportRects += dirtyRect;
2655 d->dirtyRegion = QRegion();
2656 d->dirtyBoundingRect = QRect();
2658 bool fullUpdate = !d->accelerateScrolling || d->viewportUpdateMode == QGraphicsView::FullViewportUpdate;
2659 bool boundingRectUpdate = (d->viewportUpdateMode == QGraphicsView::BoundingRectViewportUpdate)
2660 || (d->viewportUpdateMode == QGraphicsView::SmartViewportUpdate
2661 && ((dirtyViewportRects.size() + rects.size()) >= QGRAPHICSVIEW_REGION_RECT_THRESHOLD));
2663 QRegion updateRegion;
2665 QRect viewportRect = viewport()->rect();
2666 bool redraw =
false;
2667 QTransform transform = viewportTransform();
2670 for (
const QRectF &rect : rects) {
2671 QRect xrect = transform.mapRect(rect).toAlignedRect();
2672 if (!(d->optimizationFlags & DontAdjustForAntialiasing))
2673 xrect.adjust(-2, -2, 2, 2);
2675 xrect.adjust(-1, -1, 1, 1);
2676 if (!viewportRect.intersects(xrect))
2678 dirtyViewportRects << xrect;
2681 for (
const QRect &rect : std::as_const(dirtyViewportRects)) {
2684 if (!boundingRectUpdate) {
2685 updateRegion += rect;
2687 boundingRect |= rect;
2701 viewport()->update();
2702 else if (boundingRectUpdate)
2703 viewport()->update(boundingRect);
2705 viewport()->update(updateRegion);
2709
2710
2711
2712
2713
2714
2715void QGraphicsView::updateSceneRect(
const QRectF &rect)
2718 if (!d->hasSceneRect) {
2719 d->sceneRect = rect;
2720 d->recalculateContentSize();
2725
2726
2727
2728
2729
2730
2731void QGraphicsView::setupViewport(QWidget *widget)
2736 qWarning(
"QGraphicsView::setupViewport: cannot initialize null widget");
2740 const bool isGLWidget = widget->inherits(
"QOpenGLWidget");
2742 d->accelerateScrolling = !(isGLWidget);
2744 widget->setFocusPolicy(Qt::StrongFocus);
2747 d->stereoEnabled = QWidgetPrivate::get(widget)->isStereoEnabled();
2751 widget->setAutoFillBackground(
true);
2757 if ((d->scene && (!d->scene->d_func()->allItemsIgnoreHoverEvents
2758 || !d->scene->d_func()->allItemsUseDefaultCursor))
2759 || d->transformationAnchor == AnchorUnderMouse
2760 || d->resizeAnchor == AnchorUnderMouse) {
2761 widget->setMouseTracking(
true);
2765 if (d->scene && !d->scene->d_func()->allItemsIgnoreTouchEvents)
2766 widget->setAttribute(Qt::WA_AcceptTouchEvents);
2768#ifndef QT_NO_GESTURES
2770 const auto gestures = d->scene->d_func()->grabbedGestures.keys();
2771 for (Qt::GestureType gesture : gestures)
2772 widget->grabGesture(gesture);
2776 widget->setAcceptDrops(acceptDrops());
2780
2781
2782bool QGraphicsView::event(QEvent *event)
2786 if (d->sceneInteractionAllowed) {
2787 switch (event->type()) {
2788 case QEvent::ShortcutOverride:
2790 return QCoreApplication::sendEvent(d->scene, event);
2792 case QEvent::KeyPress:
2794 QKeyEvent *k =
static_cast<QKeyEvent *>(event);
2795 if (k->key() == Qt::Key_Tab || k->key() == Qt::Key_Backtab) {
2802 QCoreApplication::sendEvent(d->scene, event);
2803 if (event->isAccepted())
2817 return QAbstractScrollArea::event(event);
2821
2822
2823bool QGraphicsView::viewportEvent(QEvent *event)
2827 return QAbstractScrollArea::viewportEvent(event);
2829 switch (event->type()) {
2831 QCoreApplication::sendEvent(d->scene, event);
2833 case QEvent::WindowActivate:
2834 QCoreApplication::sendEvent(d->scene, event);
2836 case QEvent::WindowDeactivate:
2841 if (!d->scene->d_func()->popupWidgets.isEmpty())
2842 d->scene->d_func()->removePopup(d->scene->d_func()->popupWidgets.constFirst());
2843 QCoreApplication::sendEvent(d->scene, event);
2846 if (d->scene && isActiveWindow()) {
2847 QEvent windowActivate(QEvent::WindowActivate);
2848 QCoreApplication::sendEvent(d->scene, &windowActivate);
2853 if (!event->spontaneous() && d->scene && isActiveWindow()) {
2854 QEvent windowDeactivate(QEvent::WindowDeactivate);
2855 QCoreApplication::sendEvent(d->scene, &windowDeactivate);
2858 case QEvent::Leave: {
2862 if ((QApplication::activePopupWidget() && QApplication::activePopupWidget() != window())
2863 || (QApplication::activeModalWidget() && QApplication::activeModalWidget() != window())
2864 || (QApplication::activeWindow() != window())) {
2865 if (!d->scene->d_func()->popupWidgets.isEmpty())
2866 d->scene->d_func()->removePopup(d->scene->d_func()->popupWidgets.constFirst());
2868 d->useLastMouseEvent =
false;
2869 QGraphicsSceneEvent leaveEvent(QEvent::GraphicsSceneLeave);
2870 leaveEvent.setWidget(viewport());
2871 QCoreApplication::sendEvent(d->scene, &leaveEvent);
2872 event->setAccepted(leaveEvent.isAccepted());
2875#if QT_CONFIG(tooltip)
2876 case QEvent::ToolTip: {
2877 QHelpEvent *toolTip =
static_cast<QHelpEvent *>(event);
2878 QGraphicsSceneHelpEvent helpEvent(QEvent::GraphicsSceneHelp);
2879 helpEvent.setWidget(viewport());
2880 helpEvent.setScreenPos(toolTip->globalPos());
2881 helpEvent.setScenePos(mapToScene(toolTip->pos()));
2882 QCoreApplication::sendEvent(d->scene, &helpEvent);
2883 toolTip->setAccepted(helpEvent.isAccepted());
2889 d->fullUpdatePending =
false;
2890 d->dirtyScrollOffset = QPoint();
2895 if (!d->updateSceneSlotReimplementedChecked) {
2896 d->updateSceneSlotReimplementedChecked =
true;
2897 const QMetaObject *mo = metaObject();
2898 if (mo != &QGraphicsView::staticMetaObject) {
2899 if (mo->indexOfSlot(
"updateScene(QList<QRectF>)")
2900 != QGraphicsView::staticMetaObject.indexOfSlot(
"updateScene(QList<QRectF>)")) {
2901 connect(d->scene, SIGNAL(changed(QList<QRectF>)),
2902 this, SLOT(updateScene(QList<QRectF>)));
2908 case QEvent::TouchBegin:
2909 case QEvent::TouchUpdate:
2910 case QEvent::TouchEnd:
2915 if (d->scene && d->sceneInteractionAllowed) {
2917 QTouchEvent *touchEvent =
static_cast<QTouchEvent *>(event);
2918 QMutableTouchEvent::setTarget(touchEvent, viewport());
2919 QGraphicsViewPrivate::translateTouchEvent(d, touchEvent);
2920 QCoreApplication::sendEvent(d->scene, touchEvent);
2927#ifndef QT_NO_GESTURES
2928 case QEvent::Gesture:
2929 case QEvent::GestureOverride:
2934 if (d->scene && d->sceneInteractionAllowed) {
2935 QGestureEvent *gestureEvent =
static_cast<QGestureEvent *>(event);
2936 gestureEvent->setWidget(viewport());
2937 QCoreApplication::sendEvent(d->scene, gestureEvent);
2946 return QAbstractScrollArea::viewportEvent(event);
2949#ifndef QT_NO_CONTEXTMENU
2951
2952
2953void QGraphicsView::contextMenuEvent(QContextMenuEvent *event)
2956 if (!d->scene || !d->sceneInteractionAllowed)
2959 d->mousePressViewPoint = event->pos();
2960 d->mousePressScenePoint = mapToScene(d->mousePressViewPoint);
2961 d->mousePressScreenPoint = event->globalPos();
2962 d->lastMouseMoveScenePoint = d->mousePressScenePoint;
2963 d->lastMouseMoveScreenPoint = d->mousePressScreenPoint;
2965 QGraphicsSceneContextMenuEvent contextEvent(QEvent::GraphicsSceneContextMenu);
2966 contextEvent.setWidget(viewport());
2967 contextEvent.setScenePos(d->mousePressScenePoint);
2968 contextEvent.setScreenPos(d->mousePressScreenPoint);
2969 contextEvent.setModifiers(event->modifiers());
2970 contextEvent.setReason((QGraphicsSceneContextMenuEvent::Reason)(event->reason()));
2971 contextEvent.setAccepted(event->isAccepted());
2972 contextEvent.setTimestamp(event->timestamp());
2973 QCoreApplication::sendEvent(d->scene, &contextEvent);
2974 event->setAccepted(contextEvent.isAccepted());
2978#if QT_CONFIG(draganddrop)
2980
2981
2982void QGraphicsView::dropEvent(QDropEvent *event)
2985 if (!d->scene || !d->sceneInteractionAllowed)
2989 QGraphicsSceneDragDropEvent sceneEvent(QEvent::GraphicsSceneDrop);
2990 d->populateSceneDragDropEvent(&sceneEvent, event);
2993 QCoreApplication::sendEvent(d->scene, &sceneEvent);
2996 event->setAccepted(sceneEvent.isAccepted());
2997 if (sceneEvent.isAccepted())
2998 event->setDropAction(sceneEvent.dropAction());
3000 delete d->lastDragDropEvent;
3001 d->lastDragDropEvent =
nullptr;
3005
3006
3007void QGraphicsView::dragEnterEvent(QDragEnterEvent *event)
3010 if (!d->scene || !d->sceneInteractionAllowed)
3014 d->useLastMouseEvent =
false;
3017 QGraphicsSceneDragDropEvent sceneEvent(QEvent::GraphicsSceneDragEnter);
3018 d->populateSceneDragDropEvent(&sceneEvent, event);
3021 d->storeDragDropEvent(&sceneEvent);
3024 QCoreApplication::sendEvent(d->scene, &sceneEvent);
3027 if (sceneEvent.isAccepted()) {
3028 event->setAccepted(
true);
3029 event->setDropAction(sceneEvent.dropAction());
3034
3035
3036void QGraphicsView::dragLeaveEvent(QDragLeaveEvent *event)
3039 if (!d->scene || !d->sceneInteractionAllowed)
3041 if (!d->lastDragDropEvent) {
3042 qWarning(
"QGraphicsView::dragLeaveEvent: drag leave received before drag enter");
3047 QGraphicsSceneDragDropEvent sceneEvent(QEvent::GraphicsSceneDragLeave);
3048 sceneEvent.setScenePos(d->lastDragDropEvent->scenePos());
3049 sceneEvent.setScreenPos(d->lastDragDropEvent->screenPos());
3050 sceneEvent.setButtons(d->lastDragDropEvent->buttons());
3051 sceneEvent.setModifiers(d->lastDragDropEvent->modifiers());
3052 sceneEvent.setPossibleActions(d->lastDragDropEvent->possibleActions());
3053 sceneEvent.setProposedAction(d->lastDragDropEvent->proposedAction());
3054 sceneEvent.setDropAction(d->lastDragDropEvent->dropAction());
3055 sceneEvent.setMimeData(d->lastDragDropEvent->mimeData());
3056 sceneEvent.setWidget(d->lastDragDropEvent->widget());
3057 sceneEvent.setSource(d->lastDragDropEvent->source());
3058 delete d->lastDragDropEvent;
3059 d->lastDragDropEvent =
nullptr;
3062 QCoreApplication::sendEvent(d->scene, &sceneEvent);
3065 if (sceneEvent.isAccepted())
3066 event->setAccepted(
true);
3070
3071
3072void QGraphicsView::dragMoveEvent(QDragMoveEvent *event)
3075 if (!d->scene || !d->sceneInteractionAllowed)
3079 QGraphicsSceneDragDropEvent sceneEvent(QEvent::GraphicsSceneDragMove);
3080 d->populateSceneDragDropEvent(&sceneEvent, event);
3083 d->storeDragDropEvent(&sceneEvent);
3086 QCoreApplication::sendEvent(d->scene, &sceneEvent);
3089 event->setAccepted(sceneEvent.isAccepted());
3090 if (sceneEvent.isAccepted())
3091 event->setDropAction(sceneEvent.dropAction());
3096
3097
3098void QGraphicsView::focusInEvent(QFocusEvent *event)
3101 d->updateInputMethodSensitivity();
3102 QAbstractScrollArea::focusInEvent(event);
3104 QCoreApplication::sendEvent(d->scene, event);
3106 if (!d->scene || !event->isAccepted())
3107 QAbstractScrollArea::focusInEvent(event);
3111
3112
3113bool QGraphicsView::focusNextPrevChild(
bool next)
3115 return QAbstractScrollArea::focusNextPrevChild(next);
3119
3120
3121void QGraphicsView::focusOutEvent(QFocusEvent *event)
3124 QAbstractScrollArea::focusOutEvent(event);
3126 QCoreApplication::sendEvent(d->scene, event);
3130
3131
3132void QGraphicsView::keyPressEvent(QKeyEvent *event)
3135 if (!d->scene || !d->sceneInteractionAllowed) {
3136 QAbstractScrollArea::keyPressEvent(event);
3139 QCoreApplication::sendEvent(d->scene, event);
3140 if (!event->isAccepted())
3141 QAbstractScrollArea::keyPressEvent(event);
3145
3146
3147void QGraphicsView::keyReleaseEvent(QKeyEvent *event)
3150 if (!d->scene || !d->sceneInteractionAllowed)
3152 QCoreApplication::sendEvent(d->scene, event);
3153 if (!event->isAccepted())
3154 QAbstractScrollArea::keyReleaseEvent(event);
3158
3159
3160void QGraphicsView::mouseDoubleClickEvent(QMouseEvent *event)
3163 if (!d->scene || !d->sceneInteractionAllowed)
3166 d->storeMouseEvent(event);
3167 d->mousePressViewPoint = event->position().toPoint();
3168 d->mousePressScenePoint = mapToScene(d->mousePressViewPoint);
3169 d->mousePressScreenPoint = event->globalPosition().toPoint();
3170 d->lastMouseMoveScenePoint = d->mousePressScenePoint;
3171 d->lastMouseMoveScreenPoint = d->mousePressScreenPoint;
3172 d->mousePressButton = event->button();
3174 QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseDoubleClick);
3175 mouseEvent.setWidget(viewport());
3176 mouseEvent.setButtonDownScenePos(d->mousePressButton, d->mousePressScenePoint);
3177 mouseEvent.setButtonDownScreenPos(d->mousePressButton, d->mousePressScreenPoint);
3178 mouseEvent.setScenePos(mapToScene(d->mousePressViewPoint));
3179 mouseEvent.setScreenPos(d->mousePressScreenPoint);
3180 mouseEvent.setLastScenePos(d->lastMouseMoveScenePoint);
3181 mouseEvent.setLastScreenPos(d->lastMouseMoveScreenPoint);
3182 mouseEvent.setButtons(event->buttons());
3183 mouseEvent.setAccepted(
false);
3184 mouseEvent.setButton(event->button());
3185 mouseEvent.setModifiers(event->modifiers());
3186 mouseEvent.setSource(event->source());
3187 mouseEvent.setFlags(event->flags());
3188 mouseEvent.setTimestamp(event->timestamp());
3189 if (event->spontaneous())
3190 qt_sendSpontaneousEvent(d->scene, &mouseEvent);
3192 QCoreApplication::sendEvent(d->scene, &mouseEvent);
3195 const bool isAccepted = mouseEvent.isAccepted();
3196 event->setAccepted(isAccepted);
3199 d->lastMouseEvent->setAccepted(isAccepted);
3203
3204
3205void QGraphicsView::mousePressEvent(QMouseEvent *event)
3212 d->storeMouseEvent(event);
3213 d->lastMouseEvent->setAccepted(
false);
3215 if (d->sceneInteractionAllowed) {
3217 d->mousePressViewPoint = event->position().toPoint();
3218 d->mousePressScenePoint = mapToScene(d->mousePressViewPoint);
3219 d->mousePressScreenPoint = event->globalPosition().toPoint();
3220 d->lastMouseMoveScenePoint = d->mousePressScenePoint;
3221 d->lastMouseMoveScreenPoint = d->mousePressScreenPoint;
3222 d->mousePressButton = event->button();
3226 QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMousePress);
3227 mouseEvent.setWidget(viewport());
3228 mouseEvent.setButtonDownScenePos(d->mousePressButton, d->mousePressScenePoint);
3229 mouseEvent.setButtonDownScreenPos(d->mousePressButton, d->mousePressScreenPoint);
3230 mouseEvent.setScenePos(d->mousePressScenePoint);
3231 mouseEvent.setScreenPos(d->mousePressScreenPoint);
3232 mouseEvent.setLastScenePos(d->lastMouseMoveScenePoint);
3233 mouseEvent.setLastScreenPos(d->lastMouseMoveScreenPoint);
3234 mouseEvent.setButtons(event->buttons());
3235 mouseEvent.setButton(event->button());
3236 mouseEvent.setModifiers(event->modifiers());
3237 mouseEvent.setSource(event->source());
3238 mouseEvent.setFlags(event->flags());
3239 mouseEvent.setAccepted(
false);
3240 mouseEvent.setTimestamp(event->timestamp());
3241 if (event->spontaneous())
3242 qt_sendSpontaneousEvent(d->scene, &mouseEvent);
3244 QCoreApplication::sendEvent(d->scene, &mouseEvent);
3247 bool isAccepted = mouseEvent.isAccepted();
3248 event->setAccepted(isAccepted);
3251 d->lastMouseEvent->setAccepted(isAccepted);
3258#if QT_CONFIG(rubberband)
3259 if (d->dragMode == QGraphicsView::RubberBandDrag && !d->rubberBanding) {
3260 if (d->sceneInteractionAllowed) {
3263 d->rubberBanding =
true;
3264 d->rubberBandRect = QRect();
3266 bool extendSelection = (event->modifiers() & Qt::ControlModifier) != 0;
3268 if (extendSelection) {
3269 d->rubberBandSelectionOperation = Qt::AddToSelection;
3271 d->rubberBandSelectionOperation = Qt::ReplaceSelection;
3272 d->scene->clearSelection();
3278 if (d->dragMode == QGraphicsView::ScrollHandDrag && event->button() == Qt::LeftButton) {
3281 d->handScrolling =
true;
3282 d->handScrollMotions = 0;
3284 viewport()->setCursor(Qt::ClosedHandCursor);
3290
3291
3292void QGraphicsView::mouseMoveEvent(QMouseEvent *event)
3296 if (d->dragMode == QGraphicsView::ScrollHandDrag) {
3297 if (d->handScrolling) {
3298 QScrollBar *hBar = horizontalScrollBar();
3299 QScrollBar *vBar = verticalScrollBar();
3300 QPoint delta = event->position().toPoint() - d->lastMouseEvent->position().toPoint();
3301 hBar->setValue(hBar->value() + (isRightToLeft() ? delta.x() : -delta.x()));
3302 vBar->setValue(vBar->value() - delta.y());
3306 ++d->handScrollMotions;
3310 d->mouseMoveEventHandler(event);
3314
3315
3316void QGraphicsView::mouseReleaseEvent(QMouseEvent *event)
3320#if QT_CONFIG(rubberband)
3321 if (d->dragMode == QGraphicsView::RubberBandDrag && d->sceneInteractionAllowed && !event->buttons()) {
3322 d->clearRubberBand();
3325 if (d->dragMode == QGraphicsView::ScrollHandDrag && event->button() == Qt::LeftButton) {
3330 viewport()->setCursor(Qt::OpenHandCursor);
3332 d->handScrolling =
false;
3334 if (d->scene && d->sceneInteractionAllowed && !d->lastMouseEvent->isAccepted() && d->handScrollMotions <= 6) {
3338 d->scene->clearSelection();
3342 d->storeMouseEvent(event);
3344 if (!d->sceneInteractionAllowed)
3350 QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseRelease);
3351 mouseEvent.setWidget(viewport());
3352 mouseEvent.setButtonDownScenePos(d->mousePressButton, d->mousePressScenePoint);
3353 mouseEvent.setButtonDownScreenPos(d->mousePressButton, d->mousePressScreenPoint);
3354 mouseEvent.setScenePos(mapToScene(event->position().toPoint()));
3355 mouseEvent.setScreenPos(event->globalPosition().toPoint());
3356 mouseEvent.setLastScenePos(d->lastMouseMoveScenePoint);
3357 mouseEvent.setLastScreenPos(d->lastMouseMoveScreenPoint);
3358 mouseEvent.setButtons(event->buttons());
3359 mouseEvent.setButton(event->button());
3360 mouseEvent.setModifiers(event->modifiers());
3361 mouseEvent.setSource(event->source());
3362 mouseEvent.setFlags(event->flags());
3363 mouseEvent.setAccepted(
false);
3364 mouseEvent.setTimestamp(event->timestamp());
3365 if (event->spontaneous())
3366 qt_sendSpontaneousEvent(d->scene, &mouseEvent);
3368 QCoreApplication::sendEvent(d->scene, &mouseEvent);
3371 d->lastMouseEvent->setAccepted(mouseEvent.isAccepted());
3372 event->setAccepted(mouseEvent.isAccepted());
3375 if (mouseEvent.isAccepted() && mouseEvent.buttons() == 0 && viewport()->testAttribute(Qt::WA_SetCursor)) {
3377 d->_q_unsetViewportCursor();
3382#if QT_CONFIG(wheelevent)
3384
3385
3386void QGraphicsView::wheelEvent(QWheelEvent *event)
3389 if (!d->scene || !d->sceneInteractionAllowed) {
3390 QAbstractScrollArea::wheelEvent(event);
3396 QGraphicsSceneWheelEvent wheelEvent(QEvent::GraphicsSceneWheel);
3397 wheelEvent.setWidget(viewport());
3398 wheelEvent.setScenePos(mapToScene(event->position().toPoint()));
3399 wheelEvent.setScreenPos(event->globalPosition().toPoint());
3400 wheelEvent.setButtons(event->buttons());
3401 wheelEvent.setModifiers(event->modifiers());
3402 const bool horizontal = qAbs(event->angleDelta().x()) > qAbs(event->angleDelta().y());
3403 wheelEvent.setDelta(horizontal ? event->angleDelta().x() : event->angleDelta().y());
3404 wheelEvent.setPixelDelta(event->pixelDelta());
3405 wheelEvent.setPhase(event->phase());
3406 wheelEvent.setInverted(event->isInverted());
3407 wheelEvent.setOrientation(horizontal ? Qt::Horizontal : Qt::Vertical);
3408 wheelEvent.setAccepted(
false);
3409 wheelEvent.setTimestamp(event->timestamp());
3410 QCoreApplication::sendEvent(d->scene, &wheelEvent);
3411 event->setAccepted(wheelEvent.isAccepted());
3412 if (!event->isAccepted())
3413 QAbstractScrollArea::wheelEvent(event);
3418
3419
3420void QGraphicsView::paintEvent(QPaintEvent *event)
3424 QAbstractScrollArea::paintEvent(event);
3429 d->scene->d_func()->painterStateProtection = !(d->optimizationFlags & DontSavePainterState);
3432 d->exposedRegion = event->region();
3433 QRectF exposedSceneRect = mapToScene(d->exposedRegion.boundingRect()).boundingRect();
3436 QPainter painter(viewport());
3437#if QT_CONFIG(rubberband)
3438 if (d->rubberBanding && !d->rubberBandRect.isEmpty())
3442 painter.setRenderHints(painter.renderHints(),
false);
3443 painter.setRenderHints(d->renderHints,
true);
3446 const bool viewTransformed = isTransformed();
3447 if (viewTransformed)
3448 painter.setWorldTransform(viewportTransform());
3449 const QTransform viewTransform = painter.worldTransform();
3451 const auto actuallyDraw = [&]() {
3453 if (d->cacheMode & CacheBackground) {
3456 if (d->mustResizeBackgroundPixmap) {
3457 const qreal dpr = d->viewport->devicePixelRatio();
3458 d->backgroundPixmap = QPixmap(viewport()->size() * dpr);
3459 d->backgroundPixmap.setDevicePixelRatio(dpr);
3460 QBrush bgBrush = viewport()->palette().brush(viewport()->backgroundRole());
3461 if (!bgBrush.isOpaque())
3462 d->backgroundPixmap.fill(Qt::transparent);
3463 QPainter p(&d->backgroundPixmap);
3464 p.fillRect(0, 0, d->backgroundPixmap.width(), d->backgroundPixmap.height(), bgBrush);
3465 d->backgroundPixmapExposed = QRegion(viewport()->rect());
3466 d->mustResizeBackgroundPixmap =
false;
3470 if (!d->backgroundPixmapExposed.isEmpty()) {
3471 QPainter backgroundPainter(&d->backgroundPixmap);
3472 backgroundPainter.setClipRegion(d->backgroundPixmapExposed, Qt::ReplaceClip);
3473 if (viewTransformed)
3474 backgroundPainter.setTransform(viewTransform);
3475 QRectF backgroundExposedSceneRect = mapToScene(d->backgroundPixmapExposed.boundingRect()).boundingRect();
3476 drawBackground(&backgroundPainter, backgroundExposedSceneRect);
3477 d->backgroundPixmapExposed = QRegion();
3481 if (viewTransformed) {
3482 painter.setWorldTransform(QTransform());
3483 painter.drawPixmap(QPoint(), d->backgroundPixmap);
3484 painter.setWorldTransform(viewTransform);
3486 painter.drawPixmap(QPoint(), d->backgroundPixmap);
3489 if (!(d->optimizationFlags & DontSavePainterState))
3492 drawBackground(&painter, exposedSceneRect);
3493 if (!(d->optimizationFlags & DontSavePainterState))
3498 if (!(d->optimizationFlags & IndirectPainting)) {
3499 const quint32 oldRectAdjust = d->scene->d_func()->rectAdjust;
3500 if (d->optimizationFlags & QGraphicsView::DontAdjustForAntialiasing)
3501 d->scene->d_func()->rectAdjust = 1;
3503 d->scene->d_func()->rectAdjust = 2;
3504 d->scene->d_func()->drawItems(&painter, viewTransformed ? &viewTransform :
nullptr,
3505 &d->exposedRegion, viewport());
3506 d->scene->d_func()->rectAdjust = oldRectAdjust;
3513 if (!d->scene->d_func()->painterStateProtection)
3514 painter.setOpacity(1.0);
3515 painter.setWorldTransform(viewTransform);
3518 if (!d->scene->d_func()->unpolishedItems.isEmpty())
3519 d->scene->d_func()->_q_polishItems();
3522 d->scene->d_func()->updateAll =
false;
3525 bool allItems =
false;
3526 QList<QGraphicsItem *> itemList = d->findItems(d->exposedRegion, &allItems, viewTransform);
3527 if (!itemList.isEmpty()) {
3529 const int numItems = itemList.size();
3530 QGraphicsItem **itemArray = &itemList[0];
3531 QStyleOptionGraphicsItem *styleOptionArray = d->allocStyleOptionsArray(numItems);
3532 QTransform transform(Qt::Uninitialized);
3533 for (
int i = 0; i < numItems; ++i) {
3534 QGraphicsItem *item = itemArray[i];
3535 QGraphicsItemPrivate *itemd = item->d_ptr.data();
3536 itemd->initStyleOption(&styleOptionArray[i], viewTransform, d->exposedRegion, allItems);
3542 const QRectF brect = adjustedItemEffectiveBoundingRect(item);
3543 if (!itemd->itemIsUntransformable()) {
3544 transform = item->sceneTransform();
3545 if (viewTransformed)
3546 transform *= viewTransform;
3548 transform = item->deviceTransform(viewTransform);
3550 itemd->paintedViewBoundingRects.insert(d->viewport, transform.mapRect(brect).toRect());
3553 drawItems(&painter, numItems, itemArray, styleOptionArray);
3554 d->freeStyleOptionsArray(styleOptionArray);
3559 drawForeground(&painter, exposedSceneRect);
3561 #if QT_CONFIG(rubberband)
3563 if (d->rubberBanding && !d->rubberBandRect.isEmpty()) {
3565 QStyleOptionRubberBand option;
3566 option.initFrom(viewport());
3567 option.rect = d->rubberBandRect;
3568 option.shape = QRubberBand::Rectangle;
3570 QStyleHintReturnMask mask;
3571 if (viewport()->style()->styleHint(QStyle::SH_RubberBand_Mask, &option, viewport(), &mask)) {
3573 painter.setClipRegion(mask.region, Qt::IntersectClip);
3576 viewport()->style()->drawControl(QStyle::CE_RubberBand, &option, &painter, viewport());
3584 if (d->stereoEnabled) {
3585 QWidgetPrivate* w = QWidgetPrivate::get(viewport());
3586 if (w->toggleStereoTargetBuffer()) {
3588 w->toggleStereoTargetBuffer();
3595 d->scene->d_func()->painterStateProtection =
true;
3599
3600
3601void QGraphicsView::resizeEvent(QResizeEvent *event)
3606 QPointF oldLastCenterPoint = d->lastCenterPoint;
3608 QAbstractScrollArea::resizeEvent(event);
3609 d->recalculateContentSize();
3612 if (d->resizeAnchor == NoAnchor && !d->keepLastCenterPoint) {
3613 d->updateLastCenterPoint();
3615 d->lastCenterPoint = oldLastCenterPoint;
3617 d->centerView(d->resizeAnchor);
3618 d->keepLastCenterPoint =
false;
3620 if (d->cacheMode & CacheBackground) {
3622 d->mustResizeBackgroundPixmap =
true;
3627
3628
3629void QGraphicsView::scrollContentsBy(
int dx,
int dy)
3632 d->dirtyScroll =
true;
3633 if (d->transforming)
3635 if (isRightToLeft())
3638 if (d->viewportUpdateMode != QGraphicsView::NoViewportUpdate) {
3639 if (d->viewportUpdateMode != QGraphicsView::FullViewportUpdate) {
3640 if (d->accelerateScrolling) {
3641#if QT_CONFIG(rubberband)
3643 if (!d->rubberBandRect.isEmpty()) {
3644 QRegion rubberBandRegion(d->rubberBandRegion(viewport(), d->rubberBandRect));
3645 rubberBandRegion += rubberBandRegion.translated(-dx, -dy);
3646 viewport()->update(rubberBandRegion);
3649 d->dirtyScrollOffset.rx() += dx;
3650 d->dirtyScrollOffset.ry() += dy;
3651 d->dirtyRegion.translate(dx, dy);
3652 viewport()->scroll(dx, dy);
3661 d->updateLastCenterPoint();
3663 if (d->cacheMode & CacheBackground) {
3666 const qreal dpr = d->backgroundPixmap.devicePixelRatio();
3667 const qreal inverseDpr = qreal(1) / dpr;
3671 if (!d->backgroundPixmap.isNull())
3672 d->backgroundPixmap.scroll(dx * dpr, dy * dpr, d->backgroundPixmap.rect(), &exposed);
3675 d->backgroundPixmapExposed.translate(dx, dy);
3676 const QRegion exposedScaled = QTransform::fromScale(inverseDpr, inverseDpr).map(exposed);
3677 d->backgroundPixmapExposed += exposedScaled;
3681 if (d->sceneInteractionAllowed)
3682 d->replayLastMouseEvent();
3686
3687
3688void QGraphicsView::showEvent(QShowEvent *event)
3691 d->recalculateContentSize();
3692 d->centerView(d->transformationAnchor);
3693 QAbstractScrollArea::showEvent(event);
3697
3698
3699void QGraphicsView::inputMethodEvent(QInputMethodEvent *event)
3703 QCoreApplication::sendEvent(d->scene, event);
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723void QGraphicsView::drawBackground(QPainter *painter,
const QRectF &rect)
3726 if (d->scene && d->backgroundBrush.style() == Qt::NoBrush) {
3727 d->scene->drawBackground(painter, rect);
3731 const bool wasAa = painter->testRenderHint(QPainter::Antialiasing);
3733 painter->setRenderHints(QPainter::Antialiasing,
false);
3734 painter->fillRect(rect, d->backgroundBrush);
3736 painter->setRenderHints(QPainter::Antialiasing,
true);
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756void QGraphicsView::drawForeground(QPainter *painter,
const QRectF &rect)
3759 if (d->scene && d->foregroundBrush.style() == Qt::NoBrush) {
3760 d->scene->drawForeground(painter, rect);
3764 painter->fillRect(rect, d->foregroundBrush);
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784void QGraphicsView::drawItems(QPainter *painter,
int numItems,
3785 QGraphicsItem *items[],
3786 const QStyleOptionGraphicsItem options[])
3790 QWidget *widget = painter->device() == viewport() ? viewport() :
nullptr;
3791 d->scene->drawItems(painter, numItems, items, options, widget);
3796
3797
3798
3799
3800
3801QTransform QGraphicsView::transform()
const
3803 Q_D(
const QGraphicsView);
3808
3809
3810
3811
3812QTransform QGraphicsView::viewportTransform()
const
3814 Q_D(
const QGraphicsView);
3815 QTransform moveMatrix = QTransform::fromTranslate(-d->horizontalScroll(), -d->verticalScroll());
3816 return d->identityMatrix ? moveMatrix : d->matrix * moveMatrix;
3820
3821
3822
3823
3824
3825
3826
3827bool QGraphicsView::isTransformed()
const
3829 Q_D(
const QGraphicsView);
3830 return !d->identityMatrix || d->horizontalScroll() || d->verticalScroll();
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859void QGraphicsView::setTransform(
const QTransform &matrix,
bool combine )
3862 QTransform oldMatrix = d->matrix;
3866 d->matrix = matrix * d->matrix;
3867 if (oldMatrix == d->matrix)
3870 d->identityMatrix = d->matrix.isIdentity();
3871 d->transforming =
true;
3873 d->recalculateContentSize();
3874 d->centerView(d->transformationAnchor);
3876 d->updateLastCenterPoint();
3879 if (d->sceneInteractionAllowed)
3880 d->replayLastMouseEvent();
3881 d->transforming =
false;
3888
3889
3890
3891
3892void QGraphicsView::resetTransform()
3894 setTransform(QTransform());
3897QPointF QGraphicsViewPrivate::mapToScene(
const QPointF &point)
const
3900 p.rx() += horizontalScroll();
3901 p.ry() += verticalScroll();
3902 return identityMatrix ? p : matrix.inverted().map(p);
3905QRectF QGraphicsViewPrivate::mapToScene(
const QRectF &rect)
const
3907 QPointF scrollOffset(horizontalScroll(), verticalScroll());
3908 QPointF tl = scrollOffset + rect.topLeft();
3909 QPointF tr = scrollOffset + rect.topRight();
3910 QPointF br = scrollOffset + rect.bottomRight();
3911 QPointF bl = scrollOffset + rect.bottomLeft();
3914 if (!identityMatrix) {
3915 QTransform x = matrix.inverted();
3916 poly[0] = x.map(tl);
3917 poly[1] = x.map(tr);
3918 poly[2] = x.map(br);
3919 poly[3] = x.map(bl);
3926 return poly.boundingRect();
3931#include "moc_qgraphicsview.cpp"
Combined button and popup list for selecting options.
static bool intersectsViewport(const QRect &r, int width, int height)
static bool containsViewport(const QRect &r, int width, int height)
static const int QGRAPHICSVIEW_REGION_RECT_THRESHOLD
static const int QGRAPHICSVIEW_PREALLOC_STYLE_OPTIONS
static void QRect_unite(QRect *rect, const QRect &other)
int q_round_bound(qreal d)