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 <QtWidgets/qgesture.h>
264#include <QtGui/qevent.h>
265#include <QtWidgets/qlayout.h>
266#include <QtGui/qtransform.h>
267#include <QtGui/qpainter.h>
268#include <QtGui/qpainterpath.h>
269#include <QtWidgets/qscrollbar.h>
270#include <QtWidgets/qstyleoption.h>
272#include <private/qevent_p.h>
273#include <QtGui/private/qeventpoint_p.h>
281 if (d <= (qreal) INT_MIN)
283 else if (d >= (qreal) INT_MAX)
285 return d >= 0.0 ?
int(d + 0.5) :
int(d -
int(d-1) + 0.5) +
int(d-1);
288void QGraphicsViewPrivate::translateTouchEvent(QGraphicsViewPrivate *d, QTouchEvent *touchEvent)
290 for (
int i = 0; i < touchEvent->pointCount(); ++i) {
291 auto &pt = touchEvent->point(i);
294 QMutableEventPoint::setScenePosition(pt, d->mapToScene(pt.position()));
300
301
302QGraphicsViewPrivate::QGraphicsViewPrivate()
303 : renderHints(QPainter::TextAntialiasing),
304 dragMode(QGraphicsView::NoDrag),
305 sceneInteractionAllowed(
true), hasSceneRect(
false),
306 connectedToScene(
false),
307 useLastMouseEvent(
false),
308 identityMatrix(
true),
310 accelerateScrolling(
true),
311 keepLastCenterPoint(
true),
313 handScrolling(
false),
314 mustAllocateStyleOptions(
false),
315 mustResizeBackgroundPixmap(
true),
316 fullUpdatePending(
true),
317 hasUpdateClip(
false),
318 mousePressButton(Qt::NoButton),
319 leftIndent(0), topIndent(0),
320 alignment(Qt::AlignCenter),
321 transformationAnchor(QGraphicsView::AnchorViewCenter), resizeAnchor(QGraphicsView::NoAnchor),
322 viewportUpdateMode(QGraphicsView::MinimalViewportUpdate),
324#if QT_CONFIG(rubberband)
325 rubberBanding(
false),
326 rubberBandSelectionMode(Qt::IntersectsItemShape),
327 rubberBandSelectionOperation(Qt::ReplaceSelection),
329 handScrollMotions(0),
331 hasStoredOriginalCursor(
false),
333 lastDragDropEvent(
nullptr),
334 updateSceneSlotReimplementedChecked(
false)
336 styleOptions.reserve(QGRAPHICSVIEW_PREALLOC_STYLE_OPTIONS);
339QGraphicsViewPrivate::~QGraphicsViewPrivate()
344
345
346void QGraphicsViewPrivate::recalculateContentSize()
350 const QSize maxSize = q->maximumViewportSize();
351 int width = maxSize.width();
352 int height = maxSize.height();
353 const QRectF viewRect = matrix.mapRect(q->sceneRect());
355 bool frameOnlyAround = (q->style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents,
nullptr, q));
356 if (frameOnlyAround) {
357 if (hbarpolicy == Qt::ScrollBarAlwaysOn)
358 height -= frameWidth * 2;
359 if (vbarpolicy == Qt::ScrollBarAlwaysOn)
360 width -= frameWidth * 2;
365 const int scrollBarExtent = q->style()->pixelMetric(QStyle::PM_ScrollBarExtent,
nullptr, q)
366 + (frameOnlyAround ? frameWidth * 2 : 0);
370 bool useHorizontalScrollBar = (viewRect.width() > width) && hbarpolicy == Qt::ScrollBarAsNeeded;
371 bool useVerticalScrollBar = (viewRect.height() > height) && vbarpolicy == Qt::ScrollBarAsNeeded;
372 if (useHorizontalScrollBar && vbarpolicy == Qt::ScrollBarAsNeeded) {
373 if (viewRect.height() > height - scrollBarExtent)
374 useVerticalScrollBar =
true;
376 if (useVerticalScrollBar && hbarpolicy == Qt::ScrollBarAsNeeded) {
377 if (viewRect.width() > width - scrollBarExtent)
378 useHorizontalScrollBar =
true;
380 if (useHorizontalScrollBar)
381 height -= scrollBarExtent;
382 if (useVerticalScrollBar)
383 width -= scrollBarExtent;
388 const QPointF savedLastCenterPoint = lastCenterPoint;
391 const qreal oldLeftIndent = leftIndent;
392 const qreal oldTopIndent = topIndent;
394 const auto singleStep = defaultSingleStep();
398 const int left = q_round_bound(viewRect.left());
399 const int right = q_round_bound(viewRect.right() - width);
401 switch (alignment & Qt::AlignHorizontal_Mask) {
403 leftIndent = -viewRect.left();
406 leftIndent = maxSize.width() - viewRect.width() - viewRect.left() - 1;
408 case Qt::AlignHCenter:
410 leftIndent = maxSize.width() / 2 - (viewRect.left() + viewRect.right()) / 2;
414 hbar->setRange(0, 0);
418 hbar->setRange(left, right);
419 hbar->setPageStep(width);
420 hbar->setSingleStep(width / singleStep);
422 if (oldLeftIndent != 0)
423 hbar->setValue(-oldLeftIndent);
428 const int top = q_round_bound(viewRect.top());
429 const int bottom = q_round_bound(viewRect.bottom() - height);
431 switch (alignment & Qt::AlignVertical_Mask) {
433 topIndent = -viewRect.top();
435 case Qt::AlignBottom:
436 topIndent = maxSize.height() - viewRect.height() - viewRect.top() - 1;
438 case Qt::AlignVCenter:
440 topIndent = maxSize.height() / 2 - (viewRect.top() + viewRect.bottom()) / 2;
444 vbar->setRange(0, 0);
448 vbar->setRange(top, bottom);
449 vbar->setPageStep(height);
450 vbar->setSingleStep(height / singleStep);
452 if (oldTopIndent != 0)
453 vbar->setValue(-oldTopIndent);
457 lastCenterPoint = savedLastCenterPoint;
462 if (oldLeftIndent != leftIndent || oldTopIndent != topIndent) {
465 }
else if (q->isRightToLeft() && !leftIndent) {
473 if (cacheMode & QGraphicsView::CacheBackground) {
475 mustResizeBackgroundPixmap =
true;
480
481
482void QGraphicsViewPrivate::centerView(QGraphicsView::ViewportAnchor anchor)
486 case QGraphicsView::AnchorUnderMouse: {
487 if (q->underMouse()) {
490 QPointF transformationDiff = mapToScene(viewport->rect().toRectF().center())
491 - mapToScene(viewport->mapFromGlobal(QCursor::pos().toPointF()));
492 q->centerOn(lastMouseMoveScenePoint + transformationDiff);
494 q->centerOn(lastCenterPoint);
498 case QGraphicsView::AnchorViewCenter:
499 q->centerOn(lastCenterPoint);
501 case QGraphicsView::NoAnchor:
507
508
509void QGraphicsViewPrivate::updateLastCenterPoint()
511 lastCenterPoint = mapToScene(viewport->rect().toRectF().center());
515
516
517
518
519
520qint64 QGraphicsViewPrivate::horizontalScroll()
const
523 const_cast<QGraphicsViewPrivate *>(
this)->updateScroll();
528
529
530
531
532
533qint64 QGraphicsViewPrivate::verticalScroll()
const
536 const_cast<QGraphicsViewPrivate *>(
this)->updateScroll();
541
542
543
544
545QRectF QGraphicsViewPrivate::mapRectToScene(
const QRect &rect)
const
548 const_cast<QGraphicsViewPrivate *>(
this)->updateScroll();
549 QRectF scrolled = QRectF(rect.translated(scrollX, scrollY));
550 return identityMatrix ? scrolled : matrix.inverted().mapRect(scrolled);
555
556
557
558
559QRectF QGraphicsViewPrivate::mapRectFromScene(
const QRectF &rect)
const
562 const_cast<QGraphicsViewPrivate *>(
this)->updateScroll();
563 return (identityMatrix ? rect : matrix.mapRect(rect)).translated(-scrollX, -scrollY);
567
568
569void QGraphicsViewPrivate::updateScroll()
572 scrollX = qint64(-leftIndent);
573 if (q->isRightToLeft()) {
575 scrollX += hbar->minimum();
576 scrollX += hbar->maximum();
577 scrollX -= hbar->value();
580 scrollX += hbar->value();
583 scrollY = qint64(vbar->value() - topIndent);
589
590
591
592
593
594bool QGraphicsViewPrivate::canStartScrollingAt(
const QPoint &startPos)
const
596 Q_Q(
const QGraphicsView);
597 if (q->dragMode() != QGraphicsView::NoDrag)
600 const QGraphicsItem *childItem = q->itemAt(startPos);
602 if (!startPos.isNull() && childItem && (childItem->flags() & QGraphicsItem::ItemIsMovable))
605 return QAbstractScrollAreaPrivate::canStartScrollingAt(startPos);
609
610
611void QGraphicsViewPrivate::replayLastMouseEvent()
613 if (!useLastMouseEvent || !scene)
615 mouseMoveEventHandler(&*lastMouseEvent);
619
620
621void QGraphicsViewPrivate::storeMouseEvent(QMouseEvent *event)
623 useLastMouseEvent =
true;
625 lastMouseEvent.storeUnlessAlias(*event);
628void QGraphicsViewPrivate::mouseMoveEventHandler(QMouseEvent *event)
632#if QT_CONFIG(rubberband)
633 updateRubberBand(event);
636 storeMouseEvent(event);
637 lastMouseEvent->setAccepted(
false);
639 if (!sceneInteractionAllowed)
646 QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseMove);
647 mouseEvent.setWidget(viewport);
648 mouseEvent.setButtonDownScenePos(mousePressButton, mousePressScenePoint);
649 mouseEvent.setButtonDownScreenPos(mousePressButton, mousePressScreenPoint);
650 mouseEvent.setScenePos(q->mapToScene(event->position().toPoint()));
651 mouseEvent.setScreenPos(event->globalPosition().toPoint());
652 mouseEvent.setLastScenePos(lastMouseMoveScenePoint);
653 mouseEvent.setLastScreenPos(lastMouseMoveScreenPoint);
654 mouseEvent.setButtons(event->buttons());
655 mouseEvent.setButton(event->button());
656 mouseEvent.setModifiers(event->modifiers());
657 mouseEvent.setSource(event->source());
658 mouseEvent.setFlags(event->flags());
659 mouseEvent.setTimestamp(event->timestamp());
660 lastMouseMoveScenePoint = mouseEvent.scenePos();
661 lastMouseMoveScreenPoint = mouseEvent.screenPos();
662 mouseEvent.setAccepted(
false);
663 if (event->spontaneous())
664 qt_sendSpontaneousEvent(scene, &mouseEvent);
666 QCoreApplication::sendEvent(scene, &mouseEvent);
669 lastMouseEvent->setAccepted(mouseEvent.isAccepted());
671 if (mouseEvent.isAccepted() && mouseEvent.buttons() != 0) {
682 if (scene->d_func()->allItemsIgnoreHoverEvents && !scene->d_func()->allItemsUseDefaultCursor
683 && scene->d_func()->cachedItemsUnderMouse.isEmpty()) {
684 scene->d_func()->cachedItemsUnderMouse = scene->d_func()->itemsAtPosition(mouseEvent.screenPos(),
685 mouseEvent.scenePos(),
686 mouseEvent.widget());
689 for (QGraphicsItem *item : std::as_const(scene->d_func()->cachedItemsUnderMouse)) {
690 if (item->isEnabled() && item->hasCursor()) {
691 _q_setViewportCursor(item->cursor());
697 if (hasStoredOriginalCursor) {
699 hasStoredOriginalCursor =
false;
700 viewport->setCursor(originalCursor);
706
707
708#if QT_CONFIG(rubberband)
709QRegion QGraphicsViewPrivate::rubberBandRegion(
const QWidget *widget,
const QRect &rect)
const
711 QStyleHintReturnMask mask;
712 QStyleOptionRubberBand option;
713 option.initFrom(widget);
715 option.opaque =
false;
716 option.shape = QRubberBand::Rectangle;
719 tmp += rect.adjusted(-1, -1, 1, 1);
720 if (widget->style()->styleHint(QStyle::SH_RubberBand_Mask, &option, widget, &mask))
725void QGraphicsViewPrivate::updateRubberBand(
const QMouseEvent *event)
728 if (dragMode != QGraphicsView::RubberBandDrag || !sceneInteractionAllowed || !rubberBanding)
731 if ((mousePressViewPoint - event->position().toPoint()).manhattanLength() < QApplication::startDragDistance())
735 if (viewportUpdateMode != QGraphicsView::NoViewportUpdate && !rubberBandRect.isEmpty()) {
736 if (viewportUpdateMode != QGraphicsView::FullViewportUpdate)
737 q->viewport()->update(rubberBandRegion(q->viewport(), rubberBandRect));
744 if (!event->buttons()) {
745 rubberBanding =
false;
746 rubberBandSelectionOperation = Qt::ReplaceSelection;
747 if (!rubberBandRect.isNull()) {
748 rubberBandRect = QRect();
749 emit q->rubberBandChanged(rubberBandRect, QPointF(), QPointF());
754 QRect oldRubberband = rubberBandRect;
757 const QPoint mp = q->mapFromScene(mousePressScenePoint);
758 const QPoint ep = event->position().toPoint();
759 rubberBandRect = QRect(qMin(mp.x(), ep.x()), qMin(mp.y(), ep.y()),
760 qAbs(mp.x() - ep.x()) + 1, qAbs(mp.y() - ep.y()) + 1);
762 if (rubberBandRect != oldRubberband || lastRubberbandScenePoint != lastMouseMoveScenePoint) {
763 lastRubberbandScenePoint = lastMouseMoveScenePoint;
764 oldRubberband = rubberBandRect;
765 emit q->rubberBandChanged(rubberBandRect, mousePressScenePoint, lastRubberbandScenePoint);
769 if (viewportUpdateMode != QGraphicsView::NoViewportUpdate) {
770 if (viewportUpdateMode != QGraphicsView::FullViewportUpdate)
771 q->viewport()->update(rubberBandRegion(q->viewport(), rubberBandRect));
776 QPainterPath selectionArea;
777 selectionArea.addPolygon(q->mapToScene(rubberBandRect));
778 selectionArea.closeSubpath();
780 scene->setSelectionArea(selectionArea, rubberBandSelectionOperation, rubberBandSelectionMode, q->viewportTransform());
783void QGraphicsViewPrivate::clearRubberBand()
786 if (dragMode != QGraphicsView::RubberBandDrag || !sceneInteractionAllowed || !rubberBanding)
789 if (viewportUpdateMode != QGraphicsView::NoViewportUpdate) {
790 if (viewportUpdateMode != QGraphicsView::FullViewportUpdate)
791 q->viewport()->update(rubberBandRegion(q->viewport(), rubberBandRect));
796 rubberBanding =
false;
797 rubberBandSelectionOperation = Qt::ReplaceSelection;
798 if (!rubberBandRect.isNull()) {
799 rubberBandRect = QRect();
800 emit q->rubberBandChanged(rubberBandRect, QPointF(), QPointF());
806
807
809void QGraphicsViewPrivate::_q_setViewportCursor(
const QCursor &cursor)
811 if (!hasStoredOriginalCursor) {
812 hasStoredOriginalCursor =
true;
813 originalCursor = viewport->cursor();
815 viewport->setCursor(cursor);
820
821
823void QGraphicsViewPrivate::_q_unsetViewportCursor()
826 const auto items = q->items(lastMouseEvent->position().toPoint());
827 for (QGraphicsItem *item : items) {
828 if (item->isEnabled() && item->hasCursor()) {
829 _q_setViewportCursor(item->cursor());
835 if (hasStoredOriginalCursor) {
836 hasStoredOriginalCursor =
false;
837 if (dragMode == QGraphicsView::ScrollHandDrag)
838 viewport->setCursor(Qt::OpenHandCursor);
840 viewport->setCursor(originalCursor);
846
847
848void QGraphicsViewPrivate::storeDragDropEvent(
const QGraphicsSceneDragDropEvent *event)
850 delete lastDragDropEvent;
851 lastDragDropEvent =
new QGraphicsSceneDragDropEvent(event->type());
852 lastDragDropEvent->setScenePos(event->scenePos());
853 lastDragDropEvent->setScreenPos(event->screenPos());
854 lastDragDropEvent->setButtons(event->buttons());
855 lastDragDropEvent->setModifiers(event->modifiers());
856 lastDragDropEvent->setPossibleActions(event->possibleActions());
857 lastDragDropEvent->setProposedAction(event->proposedAction());
858 lastDragDropEvent->setDropAction(event->dropAction());
859 lastDragDropEvent->setMimeData(event->mimeData());
860 lastDragDropEvent->setWidget(event->widget());
861 lastDragDropEvent->setSource(event->source());
862 lastDragDropEvent->setTimestamp(event->timestamp());
866
867
868void QGraphicsViewPrivate::populateSceneDragDropEvent(QGraphicsSceneDragDropEvent *dest,
871#if QT_CONFIG(draganddrop)
873 dest->setScenePos(q->mapToScene(source->position().toPoint()));
874 dest->setScreenPos(q->mapToGlobal(source->position()).toPoint());
875 dest->setButtons(source->buttons());
876 dest->setModifiers(source->modifiers());
877 dest->setPossibleActions(source->possibleActions());
878 dest->setProposedAction(source->proposedAction());
879 dest->setDropAction(source->dropAction());
880 dest->setMimeData(source->mimeData());
881 dest->setWidget(viewport);
882 dest->setSource(qobject_cast<QWidget *>(source->source()));
890
891
892QTransform QGraphicsViewPrivate::mapToViewTransform(
const QGraphicsItem *item)
const
894 Q_Q(
const QGraphicsView);
896 const_cast<QGraphicsViewPrivate *>(
this)->updateScroll();
898 if (item->d_ptr->itemIsUntransformable())
899 return item->deviceTransform(q->viewportTransform());
904 const QGraphicsItem *parentItem = item;
905 const QGraphicsItemPrivate *itemd;
907 itemd = parentItem->d_ptr.data();
908 if (itemd->transformData)
910 offset += itemd->pos;
911 }
while ((parentItem = itemd->parent));
913 QTransform move = QTransform::fromTranslate(offset.x(), offset.y());
915 move.translate(-scrollX, -scrollY);
916 return identityMatrix ? move : matrix * move;
918 QTransform tr = parentItem->sceneTransform();
921 return move * tr * QTransform::fromTranslate(-scrollX, -scrollY);
924QRect QGraphicsViewPrivate::mapToViewRect(
const QGraphicsItem *item,
const QRectF &rect)
const
926 return mapToViewTransform(item).mapRect(rect).toAlignedRect();
930
931
932QRegion QGraphicsViewPrivate::mapToViewRegion(
const QGraphicsItem *item,
const QRectF &rect)
const
934 Q_Q(
const QGraphicsView);
936 const_cast<QGraphicsViewPrivate *>(
this)->updateScroll();
939 QTransform itv = item->deviceTransform(q->viewportTransform());
940 return item->boundingRegion(itv) & itv.mapRect(rect).toAlignedRect();
944
945
946void QGraphicsViewPrivate::processPendingUpdates()
951 if (fullUpdatePending) {
953 }
else if (viewportUpdateMode == QGraphicsView::BoundingRectViewportUpdate) {
954 viewport->update(dirtyBoundingRect);
956 viewport->update(dirtyRegion);
959 dirtyBoundingRect = QRect();
960 dirtyRegion = QRegion();
964{
return !(r.left() > width) && !(r.right() < 0) && !(r.top() >= height) && !(r.bottom() < 0); }
967{
return r.left() <= 0 && r.top() <= 0 && r.right() >= width - 1 && r.bottom() >= height - 1; }
971 if (rect->isEmpty()) {
974 rect->setCoords(qMin(rect->left(), other.left()), qMin(rect->top(), other.top()),
975 qMax(rect->right(), other.right()), qMax(rect->bottom(), other.bottom()));
980
981
982
983
984void QGraphicsViewPrivate::setUpdateClip(QGraphicsItem *item)
989 if (!item || viewportUpdateMode == QGraphicsView::NoViewportUpdate
990 || viewportUpdateMode == QGraphicsView::FullViewportUpdate) {
991 hasUpdateClip =
false;
1000 if (item->d_ptr->itemIsUntransformable()) {
1001 QTransform xform = item->deviceTransform(q->viewportTransform());
1002 clip = xform.mapRect(item->boundingRect()).toAlignedRect();
1003 }
else if (item->d_ptr->sceneTransformTranslateOnly && identityMatrix) {
1004 QRectF r(item->boundingRect());
1005 r.translate(item->d_ptr->sceneTransform.dx() - horizontalScroll(),
1006 item->d_ptr->sceneTransform.dy() - verticalScroll());
1007 clip = r.toAlignedRect();
1008 }
else if (!q->isTransformed()) {
1009 clip = item->d_ptr->sceneTransform.mapRect(item->boundingRect()).toAlignedRect();
1011 QTransform xform = item->d_ptr->sceneTransform;
1012 xform *= q->viewportTransform();
1013 clip = xform.mapRect(item->boundingRect()).toAlignedRect();
1016 if (hasUpdateClip) {
1021 hasUpdateClip =
true;
1025bool QGraphicsViewPrivate::updateRegion(
const QRectF &rect,
const QTransform &xform)
1030 if (viewportUpdateMode != QGraphicsView::MinimalViewportUpdate
1031 && viewportUpdateMode != QGraphicsView::SmartViewportUpdate) {
1033 return updateRectF(xform.mapRect(rect));
1038 const QRegion region = xform.map(QRegion(rect.toAlignedRect()));
1039 QRect viewRect = region.boundingRect();
1040 const bool dontAdjustForAntialiasing = optimizationFlags & QGraphicsView::DontAdjustForAntialiasing;
1041 if (dontAdjustForAntialiasing)
1042 viewRect.adjust(-1, -1, 1, 1);
1044 viewRect.adjust(-2, -2, 2, 2);
1045 if (!intersectsViewport(viewRect, viewport->width(), viewport->height()))
1048 for (QRect viewRect : region) {
1049 if (dontAdjustForAntialiasing)
1050 viewRect.adjust(-1, -1, 1, 1);
1052 viewRect.adjust(-2, -2, 2, 2);
1054 viewRect &= updateClip;
1055 dirtyRegion += viewRect;
1063bool QGraphicsViewPrivate::updateRect(
const QRect &r)
1065 if (fullUpdatePending || viewportUpdateMode == QGraphicsView::NoViewportUpdate
1066 || !intersectsViewport(r, viewport->width(), viewport->height())) {
1070 switch (viewportUpdateMode) {
1071 case QGraphicsView::FullViewportUpdate:
1072 fullUpdatePending =
true;
1075 case QGraphicsView::BoundingRectViewportUpdate:
1077 QRect_unite(&dirtyBoundingRect, r & updateClip);
1079 QRect_unite(&dirtyBoundingRect, r);
1080 if (containsViewport(dirtyBoundingRect, viewport->width(), viewport->height())) {
1081 fullUpdatePending =
true;
1085 case QGraphicsView::SmartViewportUpdate:
1086 case QGraphicsView::MinimalViewportUpdate:
1088 dirtyRegion += r & updateClip;
1099QStyleOptionGraphicsItem *QGraphicsViewPrivate::allocStyleOptionsArray(
int numItems)
1101 if (mustAllocateStyleOptions || (numItems > styleOptions.capacity()))
1103 return new QStyleOptionGraphicsItem[numItems];
1106 if (numItems > styleOptions.size())
1107 styleOptions.resize(numItems);
1109 mustAllocateStyleOptions =
true;
1110 return styleOptions.data();
1113void QGraphicsViewPrivate::freeStyleOptionsArray(QStyleOptionGraphicsItem *array)
1115 mustAllocateStyleOptions =
false;
1116 if (array != styleOptions.data())
1120Q_GUI_EXPORT
extern QPainterPath qt_regionToPath(
const QRegion ®ion);
1123
1124
1125
1126
1129
1130
1131
1132
1133
1134
1135
1136
1137QList<QGraphicsItem *> QGraphicsViewPrivate::findItems(
const QRegion &exposedRegion,
bool *allItems,
1138 const QTransform &viewTransform)
const
1140 Q_Q(
const QGraphicsView);
1145 const QRectF exposedRegionSceneBounds = q->mapToScene(exposedRegion.boundingRect().adjusted(-1, -1, 1, 1))
1147 if (exposedRegionSceneBounds.contains(scene->sceneRect())) {
1152 return scene->items(Qt::AscendingOrder);
1158 bool simpleRectLookup = exposedRegion.rectCount() == 1 && matrix.type() <= QTransform::TxScale;
1159 if (simpleRectLookup) {
1160 return scene->items(exposedRegionSceneBounds,
1161 Qt::IntersectsItemBoundingRect,
1162 Qt::AscendingOrder, viewTransform);
1168 QRegion adjustedRegion;
1169 for (
const QRect &r : exposedRegion)
1170 adjustedRegion += r.adjusted(-1, -1, 1, 1);
1172 const QPainterPath exposedScenePath(q->mapToScene(qt_regionToPath(adjustedRegion)));
1173 return scene->items(exposedScenePath, Qt::IntersectsItemBoundingRect,
1174 Qt::AscendingOrder, viewTransform);
1178
1179
1180
1181
1182
1183
1184void QGraphicsViewPrivate::updateInputMethodSensitivity()
1187 QGraphicsItem *focusItem =
nullptr;
1188 bool enabled = scene && (focusItem = scene->focusItem())
1189 && (focusItem->d_ptr->flags & QGraphicsItem::ItemAcceptsInputMethod);
1190 q->setAttribute(Qt::WA_InputMethodEnabled, enabled);
1191 q->viewport()->setAttribute(Qt::WA_InputMethodEnabled, enabled);
1194 q->setInputMethodHints({ });
1198 QGraphicsProxyWidget *proxy = focusItem->d_ptr->isWidget && focusItem->d_ptr->isProxyWidget()
1199 ?
static_cast<QGraphicsProxyWidget *>(focusItem) :
nullptr;
1201 q->setInputMethodHints(focusItem->inputMethodHints());
1202 }
else if (QWidget *widget = proxy->widget()) {
1203 if (QWidget *fw = widget->focusWidget())
1205 q->setInputMethodHints(widget->inputMethodHints());
1207 q->setInputMethodHints({ });
1212
1213
1214QGraphicsView::QGraphicsView(QWidget *parent)
1215 : QAbstractScrollArea(*
new QGraphicsViewPrivate, parent)
1217 setViewport(
nullptr);
1218 setAcceptDrops(
true);
1219 setBackgroundRole(QPalette::Base);
1221 setAttribute(Qt::WA_InputMethodEnabled);
1222 viewport()->setAttribute(Qt::WA_InputMethodEnabled);
1226
1227
1228
1229QGraphicsView::QGraphicsView(QGraphicsScene *scene, QWidget *parent)
1230 : QAbstractScrollArea(*
new QGraphicsViewPrivate, parent)
1233 setViewport(
nullptr);
1234 setAcceptDrops(
true);
1235 setBackgroundRole(QPalette::Base);
1237 setAttribute(Qt::WA_InputMethodEnabled);
1238 viewport()->setAttribute(Qt::WA_InputMethodEnabled);
1242
1243
1244QGraphicsView::QGraphicsView(QGraphicsViewPrivate &dd, QWidget *parent)
1245 : QAbstractScrollArea(dd, parent)
1247 setViewport(
nullptr);
1248 setAcceptDrops(
true);
1249 setBackgroundRole(QPalette::Base);
1251 setAttribute(Qt::WA_InputMethodEnabled);
1252 viewport()->setAttribute(Qt::WA_InputMethodEnabled);
1256
1257
1258QGraphicsView::~QGraphicsView()
1262 d->scene->d_func()->views.removeAll(
this);
1263 delete d->lastDragDropEvent;
1267
1268
1269QSize QGraphicsView::sizeHint()
const
1271 Q_D(
const QGraphicsView);
1273 QSizeF baseSize = d->matrix.mapRect(sceneRect()).size();
1274 baseSize += QSizeF(d->frameWidth * 2, d->frameWidth * 2);
1275 return baseSize.boundedTo((3 * QGuiApplication::primaryScreen()->virtualSize()) / 4).toSize();
1277 return QAbstractScrollArea::sizeHint();
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295QPainter::RenderHints QGraphicsView::renderHints()
const
1297 Q_D(
const QGraphicsView);
1298 return d->renderHints;
1300void QGraphicsView::setRenderHints(QPainter::RenderHints hints)
1303 if (hints == d->renderHints)
1305 d->renderHints = hints;
1310
1311
1312
1313
1314
1315void QGraphicsView::setRenderHint(QPainter::RenderHint hint,
bool enabled)
1318 QPainter::RenderHints oldHints = d->renderHints;
1319 d->renderHints.setFlag(hint, enabled);
1320 if (oldHints != d->renderHints)
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336Qt::Alignment QGraphicsView::alignment()
const
1338 Q_D(
const QGraphicsView);
1339 return d->alignment;
1341void QGraphicsView::setAlignment(Qt::Alignment alignment)
1344 if (d->alignment != alignment) {
1345 d->alignment = alignment;
1346 d->recalculateContentSize();
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368QGraphicsView::ViewportAnchor QGraphicsView::transformationAnchor()
const
1370 Q_D(
const QGraphicsView);
1371 return d->transformationAnchor;
1373void QGraphicsView::setTransformationAnchor(ViewportAnchor anchor)
1376 d->transformationAnchor = anchor;
1380 if (d->transformationAnchor == AnchorUnderMouse)
1381 d->viewport->setMouseTracking(
true);
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400QGraphicsView::ViewportAnchor QGraphicsView::resizeAnchor()
const
1402 Q_D(
const QGraphicsView);
1403 return d->resizeAnchor;
1405void QGraphicsView::setResizeAnchor(ViewportAnchor anchor)
1408 d->resizeAnchor = anchor;
1412 if (d->resizeAnchor == AnchorUnderMouse)
1413 d->viewport->setMouseTracking(
true);
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434QGraphicsView::ViewportUpdateMode QGraphicsView::viewportUpdateMode()
const
1436 Q_D(
const QGraphicsView);
1437 return d->viewportUpdateMode;
1439void QGraphicsView::setViewportUpdateMode(ViewportUpdateMode mode)
1442 d->viewportUpdateMode = mode;
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463QGraphicsView::OptimizationFlags QGraphicsView::optimizationFlags()
const
1465 Q_D(
const QGraphicsView);
1466 return d->optimizationFlags;
1468void QGraphicsView::setOptimizationFlags(OptimizationFlags flags)
1471 d->optimizationFlags = flags;
1475
1476
1477
1478
1479void QGraphicsView::setOptimizationFlag(OptimizationFlag flag,
bool enabled)
1482 d->optimizationFlags.setFlag(flag, enabled);
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499QGraphicsView::DragMode QGraphicsView::dragMode()
const
1501 Q_D(
const QGraphicsView);
1504void QGraphicsView::setDragMode(DragMode mode)
1507 if (d->dragMode == mode)
1510#if QT_CONFIG(rubberband)
1511 d->clearRubberBand();
1515 if (d->dragMode == ScrollHandDrag)
1516 viewport()->unsetCursor();
1523 if (d->dragMode == ScrollHandDrag && mode == NoDrag && d->handScrolling)
1524 d->handScrolling =
false;
1529 if (d->dragMode == ScrollHandDrag) {
1531 d->hasStoredOriginalCursor =
false;
1532 viewport()->setCursor(Qt::OpenHandCursor);
1537#if QT_CONFIG(rubberband)
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551Qt::ItemSelectionMode QGraphicsView::rubberBandSelectionMode()
const
1553 Q_D(
const QGraphicsView);
1554 return d->rubberBandSelectionMode;
1556void QGraphicsView::setRubberBandSelectionMode(Qt::ItemSelectionMode mode)
1559 d->rubberBandSelectionMode = mode;
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1574QRect QGraphicsView::rubberBandRect()
const
1576 Q_D(
const QGraphicsView);
1577 if (d->dragMode != QGraphicsView::RubberBandDrag || !d->sceneInteractionAllowed || !d->rubberBanding)
1580 return d->rubberBandRect;
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604QGraphicsView::CacheMode QGraphicsView::cacheMode()
const
1606 Q_D(
const QGraphicsView);
1607 return d->cacheMode;
1609void QGraphicsView::setCacheMode(CacheMode mode)
1612 if (mode == d->cacheMode)
1614 d->cacheMode = mode;
1615 resetCachedContent();
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631void QGraphicsView::resetCachedContent()
1634 if (d->cacheMode == CacheNone)
1637 if (d->cacheMode & CacheBackground) {
1639 d->mustResizeBackgroundPixmap =
true;
1641 }
else if (d->mustResizeBackgroundPixmap) {
1644 d->mustResizeBackgroundPixmap =
false;
1645 d->backgroundPixmap = QPixmap();
1646 d->backgroundPixmapExposed = QRegion();
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666void QGraphicsView::invalidateScene(
const QRectF &rect, QGraphicsScene::SceneLayers layers)
1669 if ((layers & QGraphicsScene::BackgroundLayer) && !d->mustResizeBackgroundPixmap) {
1670 QRect viewRect = mapFromScene(rect).boundingRect();
1671 if (viewport()->rect().intersects(viewRect)) {
1674 d->backgroundPixmapExposed += viewRect;
1676 d->scene->update(rect);
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691bool QGraphicsView::isInteractive()
const
1693 Q_D(
const QGraphicsView);
1694 return d->sceneInteractionAllowed;
1696void QGraphicsView::setInteractive(
bool allowed)
1699 d->sceneInteractionAllowed = allowed;
1703
1704
1705
1706
1707
1708QGraphicsScene *QGraphicsView::scene()
const
1710 Q_D(
const QGraphicsView);
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724void QGraphicsView::setScene(QGraphicsScene *scene)
1727 if (d->scene == scene)
1735 disconnect(d->scene, SIGNAL(changed(QList<QRectF>)),
1736 this, SLOT(updateScene(QList<QRectF>)));
1737 disconnect(d->scene, SIGNAL(sceneRectChanged(QRectF)),
1738 this, SLOT(updateSceneRect(QRectF)));
1739 d->scene->d_func()->removeView(
this);
1740 d->connectedToScene =
false;
1742 if (isActiveWindow() && isVisible()) {
1743 QEvent windowDeactivate(QEvent::WindowDeactivate);
1744 QCoreApplication::sendEvent(d->scene, &windowDeactivate);
1747 d->scene->clearFocus();
1751 if ((d->scene = scene)) {
1752 connect(d->scene, SIGNAL(sceneRectChanged(QRectF)),
1753 this, SLOT(updateSceneRect(QRectF)));
1754 d->updateSceneSlotReimplementedChecked =
false;
1755 d->scene->d_func()->addView(
this);
1756 d->recalculateContentSize();
1757 d->lastCenterPoint = sceneRect().center();
1758 d->keepLastCenterPoint =
true;
1761 if (!d->scene->d_func()->allItemsIgnoreHoverEvents
1762 || !d->scene->d_func()->allItemsUseDefaultCursor) {
1763 d->viewport->setMouseTracking(
true);
1767 if (!d->scene->d_func()->allItemsIgnoreTouchEvents)
1768 d->viewport->setAttribute(Qt::WA_AcceptTouchEvents);
1770 if (isActiveWindow() && isVisible()) {
1771 QEvent windowActivate(QEvent::WindowActivate);
1772 QCoreApplication::sendEvent(d->scene, &windowActivate);
1775 d->recalculateContentSize();
1778 d->updateInputMethodSensitivity();
1780 if (d->scene && hasFocus())
1781 d->scene->setFocus();
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807QRectF QGraphicsView::sceneRect()
const
1809 Q_D(
const QGraphicsView);
1810 if (d->hasSceneRect)
1811 return d->sceneRect;
1813 return d->scene->sceneRect();
1816void QGraphicsView::setSceneRect(
const QRectF &rect)
1819 d->hasSceneRect = !rect.isNull();
1820 d->sceneRect = rect;
1821 d->recalculateContentSize();
1825
1826
1827
1828
1829void QGraphicsView::rotate(qreal angle)
1832 QTransform matrix = d->matrix;
1833 matrix.rotate(angle);
1834 setTransform(matrix);
1838
1839
1840
1841
1842void QGraphicsView::scale(qreal sx, qreal sy)
1845 QTransform matrix = d->matrix;
1846 matrix.scale(sx, sy);
1847 setTransform(matrix);
1851
1852
1853
1854
1855void QGraphicsView::shear(qreal sh, qreal sv)
1858 QTransform matrix = d->matrix;
1859 matrix.shear(sh, sv);
1860 setTransform(matrix);
1864
1865
1866
1867
1868void QGraphicsView::translate(qreal dx, qreal dy)
1871 QTransform matrix = d->matrix;
1872 matrix.translate(dx, dy);
1873 setTransform(matrix);
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888void QGraphicsView::centerOn(
const QPointF &pos)
1891 qreal width = viewport()->width();
1892 qreal height = viewport()->height();
1893 QPointF viewPoint = d->matrix.map(pos);
1894 QPointF oldCenterPoint = pos;
1896 if (!d->leftIndent) {
1897 if (isRightToLeft()) {
1898 qint64 horizontal = 0;
1899 horizontal += horizontalScrollBar()->minimum();
1900 horizontal += horizontalScrollBar()->maximum();
1901 horizontal -= qRound(viewPoint.x() - width / 2.0);
1902 horizontalScrollBar()->setValue(horizontal);
1904 horizontalScrollBar()->setValue(qRound(viewPoint.x() - width / 2.0));
1908 verticalScrollBar()->setValue(qRound(viewPoint.y() - height / 2.0));
1909 d->lastCenterPoint = oldCenterPoint;
1913
1914
1915
1916
1917
1918
1921
1922
1923
1924
1925
1926
1927
1928void QGraphicsView::centerOn(
const QGraphicsItem *item)
1930 centerOn(item->sceneBoundingRect().center());
1934
1935
1936
1937
1938
1939
1940
1941
1942void QGraphicsView::ensureVisible(
const QRectF &rect,
int xmargin,
int ymargin)
1945 qreal width = viewport()->width();
1946 qreal height = viewport()->height();
1947 QRectF viewRect = d->matrix.mapRect(rect);
1949 qreal left = d->horizontalScroll();
1950 qreal right = left + width;
1951 qreal top = d->verticalScroll();
1952 qreal bottom = top + height;
1954 if (viewRect.left() <= left + xmargin) {
1957 horizontalScrollBar()->setValue(
int(viewRect.left() - xmargin - 0.5));
1959 if (viewRect.right() >= right - xmargin) {
1962 horizontalScrollBar()->setValue(
int(viewRect.right() - width + xmargin + 0.5));
1964 if (viewRect.top() <= top + ymargin) {
1967 verticalScrollBar()->setValue(
int(viewRect.top() - ymargin - 0.5));
1969 if (viewRect.bottom() >= bottom - ymargin) {
1972 verticalScrollBar()->setValue(
int(viewRect.bottom() - height + ymargin + 0.5));
1977
1978
1979
1980
1981
1982
1983
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995void QGraphicsView::ensureVisible(
const QGraphicsItem *item,
int xmargin,
int ymargin)
1997 ensureVisible(item->sceneBoundingRect(), xmargin, ymargin);
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024void QGraphicsView::fitInView(
const QRectF &rect, Qt::AspectRatioMode aspectRatioMode)
2027 if (!d->scene || rect.isNull())
2031 QRectF unity = d->matrix.mapRect(QRectF(0, 0, 1, 1));
2032 if (unity.isEmpty())
2034 scale(1 / unity.width(), 1 / unity.height());
2038 QRectF viewRect = viewport()->rect().adjusted(margin, margin, -margin, -margin);
2039 if (viewRect.isEmpty())
2041 QRectF sceneRect = d->matrix.mapRect(rect);
2042 if (sceneRect.isEmpty())
2044 qreal xratio = viewRect.width() / sceneRect.width();
2045 qreal yratio = viewRect.height() / sceneRect.height();
2048 switch (aspectRatioMode) {
2049 case Qt::KeepAspectRatio:
2050 xratio = yratio = qMin(xratio, yratio);
2052 case Qt::KeepAspectRatioByExpanding:
2053 xratio = yratio = qMax(xratio, yratio);
2055 case Qt::IgnoreAspectRatio:
2060 scale(xratio, yratio);
2061 centerOn(rect.center());
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2077
2078
2079
2080
2081
2082
2083
2084void QGraphicsView::fitInView(
const QGraphicsItem *item, Qt::AspectRatioMode aspectRatioMode)
2086 QPainterPath path = item->isClipped() ? item->clipPath() : item->shape();
2087 if (item->d_ptr->hasTranslateOnlySceneTransform()) {
2088 path.translate(item->d_ptr->sceneTransform.dx(), item->d_ptr->sceneTransform.dy());
2089 fitInView(path.boundingRect(), aspectRatioMode);
2091 fitInView(item->d_ptr->sceneTransform.map(path).boundingRect(), aspectRatioMode);
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115void QGraphicsView::render(QPainter *painter,
const QRectF &target,
const QRect &source,
2116 Qt::AspectRatioMode aspectRatioMode)
2121 if (!d->scene || !(painter && painter->isActive()))
2125 QRect sourceRect = source;
2126 if (source.isNull())
2127 sourceRect = viewport()->rect();
2130 QRectF targetRect = target;
2131 if (target.isNull()) {
2132 if (painter->device()->devType() == QInternal::Picture)
2133 targetRect = sourceRect;
2135 targetRect.setRect(0, 0, painter->device()->width(), painter->device()->height());
2139 qreal xratio = targetRect.width() / sourceRect.width();
2140 qreal yratio = targetRect.height() / sourceRect.height();
2143 switch (aspectRatioMode) {
2144 case Qt::KeepAspectRatio:
2145 xratio = yratio = qMin(xratio, yratio);
2147 case Qt::KeepAspectRatioByExpanding:
2148 xratio = yratio = qMax(xratio, yratio);
2150 case Qt::IgnoreAspectRatio:
2156 QPolygonF sourceScenePoly = mapToScene(sourceRect.adjusted(-1, -1, 1, 1));
2157 QList<QGraphicsItem *> itemList = d->scene->items(sourceScenePoly,
2158 Qt::IntersectsItemBoundingRect);
2159 QGraphicsItem **itemArray =
new QGraphicsItem *[itemList.size()];
2160 int numItems = itemList.size();
2161 for (
int i = 0; i < numItems; ++i)
2162 itemArray[numItems - i - 1] = itemList.at(i);
2166 QTransform moveMatrix = QTransform::fromTranslate(-d->horizontalScroll(), -d->verticalScroll());
2167 QTransform painterMatrix = d->matrix * moveMatrix;
2168 painterMatrix *= QTransform()
2169 .translate(targetRect.left(), targetRect.top())
2170 .scale(xratio, yratio)
2171 .translate(-sourceRect.left(), -sourceRect.top());
2174 QStyleOptionGraphicsItem *styleOptionArray = d->allocStyleOptionsArray(numItems);
2175 for (
int i = 0; i < numItems; ++i)
2176 itemArray[i]->d_ptr->initStyleOption(&styleOptionArray[i], painterMatrix, targetRect.toRect());
2181 painter->setClipRect(targetRect);
2183 path.addPolygon(sourceScenePoly);
2184 path.closeSubpath();
2185 painter->setClipPath(painterMatrix.map(path), Qt::IntersectClip);
2188 painter->setTransform(painterMatrix,
true);
2191 QRectF sourceSceneRect = sourceScenePoly.boundingRect();
2192 drawBackground(painter, sourceSceneRect);
2193 drawItems(painter, numItems, itemArray, styleOptionArray);
2194 drawForeground(painter, sourceSceneRect);
2196 delete [] itemArray;
2197 d->freeStyleOptionsArray(styleOptionArray);
2203
2204
2205
2206
2207
2208
2209QList<QGraphicsItem *> QGraphicsView::items()
const
2211 Q_D(
const QGraphicsView);
2213 return QList<QGraphicsItem *>();
2214 return d->scene->items();
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233QList<QGraphicsItem *> QGraphicsView::items(
const QPoint &pos)
const
2235 Q_D(
const QGraphicsView);
2237 return QList<QGraphicsItem *>();
2241 if ((d->identityMatrix || d->matrix.type() <= QTransform::TxScale)) {
2243 QTransform xinv = viewportTransform().inverted();
2244 return d->scene->items(xinv.mapRect(QRectF(pos.x(), pos.y(), 1, 1)),
2245 Qt::IntersectsItemShape,
2246 Qt::DescendingOrder,
2247 viewportTransform());
2250 return d->scene->items(mapToScene(pos.x(), pos.y(), 1, 1),
2251 Qt::IntersectsItemShape,
2252 Qt::DescendingOrder,
2253 viewportTransform());
2257
2258
2259
2260
2261
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278QList<QGraphicsItem *> QGraphicsView::items(
const QRect &rect, Qt::ItemSelectionMode mode)
const
2280 Q_D(
const QGraphicsView);
2282 return QList<QGraphicsItem *>();
2283 return d->scene->items(mapToScene(rect), mode, Qt::DescendingOrder, viewportTransform());
2287
2288
2289
2290
2291
2292
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309QList<QGraphicsItem *> QGraphicsView::items(
const QPolygon &polygon, Qt::ItemSelectionMode mode)
const
2311 Q_D(
const QGraphicsView);
2313 return QList<QGraphicsItem *>();
2314 return d->scene->items(mapToScene(polygon), mode, Qt::DescendingOrder, viewportTransform());
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329QList<QGraphicsItem *> QGraphicsView::items(
const QPainterPath &path, Qt::ItemSelectionMode mode)
const
2331 Q_D(
const QGraphicsView);
2333 return QList<QGraphicsItem *>();
2334 return d->scene->items(mapToScene(path), mode, Qt::DescendingOrder, viewportTransform());
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350QGraphicsItem *QGraphicsView::itemAt(
const QPoint &pos)
const
2352 Q_D(
const QGraphicsView);
2355 const QList<QGraphicsItem *> itemsAtPos = items(pos);
2356 return itemsAtPos.isEmpty() ?
nullptr : itemsAtPos.first();
2360
2361
2362
2363
2364
2365
2368
2369
2370
2371
2372
2373
2374
2375
2376QPointF QGraphicsView::mapToScene(
const QPoint &point)
const
2378 Q_D(
const QGraphicsView);
2380 p.rx() += d->horizontalScroll();
2381 p.ry() += d->verticalScroll();
2382 return d->identityMatrix ? p : d->matrix.inverted().map(p);
2386
2387
2388
2389
2390
2393
2394
2395
2396
2397
2398QPolygonF QGraphicsView::mapToScene(
const QRect &rect)
const
2400 Q_D(
const QGraphicsView);
2401 if (!rect.isValid())
2404 QPointF scrollOffset(d->horizontalScroll(), d->verticalScroll());
2405 QRect r = rect.adjusted(0, 0, 1, 1);
2406 QPointF tl = scrollOffset + r.topLeft();
2407 QPointF tr = scrollOffset + r.topRight();
2408 QPointF br = scrollOffset + r.bottomRight();
2409 QPointF bl = scrollOffset + r.bottomLeft();
2412 if (!d->identityMatrix) {
2413 QTransform x = d->matrix.inverted();
2414 poly[0] = x.map(tl);
2415 poly[1] = x.map(tr);
2416 poly[2] = x.map(br);
2417 poly[3] = x.map(bl);
2428
2429
2430
2431
2432
2435
2436
2437
2438
2439
2440QPolygonF QGraphicsView::mapToScene(
const QPolygon &polygon)
const
2443 poly.reserve(polygon.size());
2444 for (
const QPoint &point : polygon)
2445 poly << mapToScene(point);
2450
2451
2452
2453
2454
2455QPainterPath QGraphicsView::mapToScene(
const QPainterPath &path)
const
2457 Q_D(
const QGraphicsView);
2458 QTransform matrix = QTransform::fromTranslate(d->horizontalScroll(), d->verticalScroll());
2459 matrix *= d->matrix.inverted();
2460 return matrix.map(path);
2464
2465
2466
2467
2468QPoint QGraphicsView::mapFromScene(
const QPointF &point)
const
2470 Q_D(
const QGraphicsView);
2471 QPointF p = d->identityMatrix ? point : d->matrix.map(point);
2472 p.rx() -= d->horizontalScroll();
2473 p.ry() -= d->verticalScroll();
2478
2479
2480
2481
2482
2485
2486
2487
2488
2489
2490QPolygon QGraphicsView::mapFromScene(
const QRectF &rect)
const
2492 Q_D(
const QGraphicsView);
2497 if (!d->identityMatrix) {
2498 const QTransform &x = d->matrix;
2499 tl = x.map(rect.topLeft());
2500 tr = x.map(rect.topRight());
2501 br = x.map(rect.bottomRight());
2502 bl = x.map(rect.bottomLeft());
2504 tl = rect.topLeft();
2505 tr = rect.topRight();
2506 br = rect.bottomRight();
2507 bl = rect.bottomLeft();
2509 QPointF scrollOffset(d->horizontalScroll(), d->verticalScroll());
2516 poly[0] = tl.toPoint();
2517 poly[1] = tr.toPoint();
2518 poly[2] = br.toPoint();
2519 poly[3] = bl.toPoint();
2524
2525
2526
2527
2528
2531
2532
2533
2534
2535
2536QPolygon QGraphicsView::mapFromScene(
const QPolygonF &polygon)
const
2539 poly.reserve(polygon.size());
2540 for (
const QPointF &point : polygon)
2541 poly << mapFromScene(point);
2546
2547
2548
2549
2550
2551QPainterPath QGraphicsView::mapFromScene(
const QPainterPath &path)
const
2553 Q_D(
const QGraphicsView);
2554 QTransform matrix = d->matrix;
2555 matrix *= QTransform::fromTranslate(-d->horizontalScroll(), -d->verticalScroll());
2556 return matrix.map(path);
2560
2561
2562QVariant QGraphicsView::inputMethodQuery(Qt::InputMethodQuery query)
const
2564 Q_D(
const QGraphicsView);
2568 QVariant value = d->scene->inputMethodQuery(query);
2569 if (value.userType() == QMetaType::QRectF)
2570 value = d->mapRectFromScene(value.toRectF());
2571 else if (value.userType() == QMetaType::QPointF)
2572 value = mapFromScene(value.toPointF());
2573 else if (value.userType() == QMetaType::QRect)
2574 value = d->mapRectFromScene(value.toRect()).toRect();
2575 else if (value.userType() == QMetaType::QPoint)
2576 value = mapFromScene(value.toPoint());
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593QBrush QGraphicsView::backgroundBrush()
const
2595 Q_D(
const QGraphicsView);
2596 return d->backgroundBrush;
2598void QGraphicsView::setBackgroundBrush(
const QBrush &brush)
2601 d->backgroundBrush = brush;
2604 if (d->cacheMode & CacheBackground) {
2606 d->mustResizeBackgroundPixmap =
true;
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623QBrush QGraphicsView::foregroundBrush()
const
2625 Q_D(
const QGraphicsView);
2626 return d->foregroundBrush;
2628void QGraphicsView::setForegroundBrush(
const QBrush &brush)
2631 d->foregroundBrush = brush;
2636
2637
2638
2639
2640void QGraphicsView::updateScene(
const QList<QRectF> &rects)
2648 if (d->fullUpdatePending || d->viewportUpdateMode == QGraphicsView::NoViewportUpdate)
2652 QList<QRect> dirtyViewportRects;
2653 dirtyViewportRects.reserve(d->dirtyRegion.rectCount() + rects.size());
2654 for (
const QRect &dirtyRect : d->dirtyRegion)
2655 dirtyViewportRects += dirtyRect;
2656 d->dirtyRegion = QRegion();
2657 d->dirtyBoundingRect = QRect();
2659 bool fullUpdate = !d->accelerateScrolling || d->viewportUpdateMode == QGraphicsView::FullViewportUpdate;
2660 bool boundingRectUpdate = (d->viewportUpdateMode == QGraphicsView::BoundingRectViewportUpdate)
2661 || (d->viewportUpdateMode == QGraphicsView::SmartViewportUpdate
2662 && ((dirtyViewportRects.size() + rects.size()) >= QGRAPHICSVIEW_REGION_RECT_THRESHOLD));
2664 QRegion updateRegion;
2666 QRect viewportRect = viewport()->rect();
2667 bool redraw =
false;
2668 QTransform transform = viewportTransform();
2671 for (
const QRectF &rect : rects) {
2672 QRect xrect = transform.mapRect(rect).toAlignedRect();
2673 if (!(d->optimizationFlags & DontAdjustForAntialiasing))
2674 xrect.adjust(-2, -2, 2, 2);
2676 xrect.adjust(-1, -1, 1, 1);
2677 if (!viewportRect.intersects(xrect))
2679 dirtyViewportRects << xrect;
2682 for (
const QRect &rect : std::as_const(dirtyViewportRects)) {
2685 if (!boundingRectUpdate) {
2686 updateRegion += rect;
2688 boundingRect |= rect;
2702 viewport()->update();
2703 else if (boundingRectUpdate)
2704 viewport()->update(boundingRect);
2706 viewport()->update(updateRegion);
2710
2711
2712
2713
2714
2715
2716void QGraphicsView::updateSceneRect(
const QRectF &rect)
2719 if (!d->hasSceneRect) {
2720 d->sceneRect = rect;
2721 d->recalculateContentSize();
2726
2727
2728
2729
2730
2731
2732void QGraphicsView::setupViewport(QWidget *widget)
2737 qWarning(
"QGraphicsView::setupViewport: cannot initialize null widget");
2741 const bool isGLWidget = widget->inherits(
"QOpenGLWidget");
2743 d->accelerateScrolling = !(isGLWidget);
2745 widget->setFocusPolicy(Qt::StrongFocus);
2748 d->stereoEnabled = QWidgetPrivate::get(widget)->isStereoEnabled();
2752 widget->setAutoFillBackground(
true);
2758 if ((d->scene && (!d->scene->d_func()->allItemsIgnoreHoverEvents
2759 || !d->scene->d_func()->allItemsUseDefaultCursor))
2760 || d->transformationAnchor == AnchorUnderMouse
2761 || d->resizeAnchor == AnchorUnderMouse) {
2762 widget->setMouseTracking(
true);
2766 if (d->scene && !d->scene->d_func()->allItemsIgnoreTouchEvents)
2767 widget->setAttribute(Qt::WA_AcceptTouchEvents);
2769#ifndef QT_NO_GESTURES
2771 const auto gestures = d->scene->d_func()->grabbedGestures.keys();
2772 for (Qt::GestureType gesture : gestures)
2773 widget->grabGesture(gesture);
2777 widget->setAcceptDrops(acceptDrops());
2781
2782
2783bool QGraphicsView::event(QEvent *event)
2787 if (d->sceneInteractionAllowed) {
2788 switch (event->type()) {
2789 case QEvent::ShortcutOverride:
2791 return QCoreApplication::sendEvent(d->scene, event);
2793 case QEvent::KeyPress:
2795 QKeyEvent *k =
static_cast<QKeyEvent *>(event);
2796 if (k->key() == Qt::Key_Tab || k->key() == Qt::Key_Backtab) {
2803 QCoreApplication::sendEvent(d->scene, event);
2804 if (event->isAccepted())
2818 return QAbstractScrollArea::event(event);
2822
2823
2824bool QGraphicsView::viewportEvent(QEvent *event)
2828 return QAbstractScrollArea::viewportEvent(event);
2830 switch (event->type()) {
2832 QCoreApplication::sendEvent(d->scene, event);
2834 case QEvent::WindowActivate:
2835 QCoreApplication::sendEvent(d->scene, event);
2837 case QEvent::WindowDeactivate:
2842 if (!d->scene->d_func()->popupWidgets.isEmpty())
2843 d->scene->d_func()->removePopup(d->scene->d_func()->popupWidgets.constFirst());
2844 QCoreApplication::sendEvent(d->scene, event);
2847 if (d->scene && isActiveWindow()) {
2848 QEvent windowActivate(QEvent::WindowActivate);
2849 QCoreApplication::sendEvent(d->scene, &windowActivate);
2854 if (!event->spontaneous() && d->scene && isActiveWindow()) {
2855 QEvent windowDeactivate(QEvent::WindowDeactivate);
2856 QCoreApplication::sendEvent(d->scene, &windowDeactivate);
2859 case QEvent::Leave: {
2863 if ((QApplication::activePopupWidget() && QApplication::activePopupWidget() != window())
2864 || (QApplication::activeModalWidget() && QApplication::activeModalWidget() != window())
2865 || (QApplication::activeWindow() != window())) {
2866 if (!d->scene->d_func()->popupWidgets.isEmpty())
2867 d->scene->d_func()->removePopup(d->scene->d_func()->popupWidgets.constFirst());
2869 d->useLastMouseEvent =
false;
2870 QGraphicsSceneEvent leaveEvent(QEvent::GraphicsSceneLeave);
2871 leaveEvent.setWidget(viewport());
2872 QCoreApplication::sendEvent(d->scene, &leaveEvent);
2873 event->setAccepted(leaveEvent.isAccepted());
2876#if QT_CONFIG(tooltip)
2877 case QEvent::ToolTip: {
2878 QHelpEvent *toolTip =
static_cast<QHelpEvent *>(event);
2879 QGraphicsSceneHelpEvent helpEvent(QEvent::GraphicsSceneHelp);
2880 helpEvent.setWidget(viewport());
2881 helpEvent.setScreenPos(toolTip->globalPos());
2882 helpEvent.setScenePos(mapToScene(toolTip->pos()));
2883 QCoreApplication::sendEvent(d->scene, &helpEvent);
2884 toolTip->setAccepted(helpEvent.isAccepted());
2890 d->fullUpdatePending =
false;
2891 d->dirtyScrollOffset = QPoint();
2896 if (!d->updateSceneSlotReimplementedChecked) {
2897 d->updateSceneSlotReimplementedChecked =
true;
2898 const QMetaObject *mo = metaObject();
2899 if (mo != &QGraphicsView::staticMetaObject) {
2900 if (mo->indexOfSlot(
"updateScene(QList<QRectF>)")
2901 != QGraphicsView::staticMetaObject.indexOfSlot(
"updateScene(QList<QRectF>)")) {
2902 connect(d->scene, SIGNAL(changed(QList<QRectF>)),
2903 this, SLOT(updateScene(QList<QRectF>)));
2909 case QEvent::TouchBegin:
2910 case QEvent::TouchUpdate:
2911 case QEvent::TouchEnd:
2916 if (d->scene && d->sceneInteractionAllowed) {
2918 QTouchEvent *touchEvent =
static_cast<QTouchEvent *>(event);
2919 QMutableTouchEvent::setTarget(touchEvent, viewport());
2920 QGraphicsViewPrivate::translateTouchEvent(d, touchEvent);
2921 QCoreApplication::sendEvent(d->scene, touchEvent);
2928#ifndef QT_NO_GESTURES
2929 case QEvent::Gesture:
2930 case QEvent::GestureOverride:
2935 if (d->scene && d->sceneInteractionAllowed) {
2936 QGestureEvent *gestureEvent =
static_cast<QGestureEvent *>(event);
2937 gestureEvent->setWidget(viewport());
2938 QCoreApplication::sendEvent(d->scene, gestureEvent);
2947 return QAbstractScrollArea::viewportEvent(event);
2950#ifndef QT_NO_CONTEXTMENU
2952
2953
2954void QGraphicsView::contextMenuEvent(QContextMenuEvent *event)
2957 if (!d->scene || !d->sceneInteractionAllowed)
2960 d->mousePressViewPoint = event->pos();
2961 d->mousePressScenePoint = mapToScene(d->mousePressViewPoint);
2962 d->mousePressScreenPoint = event->globalPos();
2963 d->lastMouseMoveScenePoint = d->mousePressScenePoint;
2964 d->lastMouseMoveScreenPoint = d->mousePressScreenPoint;
2966 QGraphicsSceneContextMenuEvent contextEvent(QEvent::GraphicsSceneContextMenu);
2967 contextEvent.setWidget(viewport());
2968 contextEvent.setScenePos(d->mousePressScenePoint);
2969 contextEvent.setScreenPos(d->mousePressScreenPoint);
2970 contextEvent.setModifiers(event->modifiers());
2971 contextEvent.setReason((QGraphicsSceneContextMenuEvent::Reason)(event->reason()));
2972 contextEvent.setAccepted(event->isAccepted());
2973 contextEvent.setTimestamp(event->timestamp());
2974 QCoreApplication::sendEvent(d->scene, &contextEvent);
2975 event->setAccepted(contextEvent.isAccepted());
2979#if QT_CONFIG(draganddrop)
2981
2982
2983void QGraphicsView::dropEvent(QDropEvent *event)
2986 if (!d->scene || !d->sceneInteractionAllowed)
2990 QGraphicsSceneDragDropEvent sceneEvent(QEvent::GraphicsSceneDrop);
2991 d->populateSceneDragDropEvent(&sceneEvent, event);
2994 QCoreApplication::sendEvent(d->scene, &sceneEvent);
2997 event->setAccepted(sceneEvent.isAccepted());
2998 if (sceneEvent.isAccepted())
2999 event->setDropAction(sceneEvent.dropAction());
3001 delete d->lastDragDropEvent;
3002 d->lastDragDropEvent =
nullptr;
3006
3007
3008void QGraphicsView::dragEnterEvent(QDragEnterEvent *event)
3011 if (!d->scene || !d->sceneInteractionAllowed)
3015 d->useLastMouseEvent =
false;
3018 QGraphicsSceneDragDropEvent sceneEvent(QEvent::GraphicsSceneDragEnter);
3019 d->populateSceneDragDropEvent(&sceneEvent, event);
3022 d->storeDragDropEvent(&sceneEvent);
3025 QCoreApplication::sendEvent(d->scene, &sceneEvent);
3028 if (sceneEvent.isAccepted()) {
3029 event->setAccepted(
true);
3030 event->setDropAction(sceneEvent.dropAction());
3035
3036
3037void QGraphicsView::dragLeaveEvent(QDragLeaveEvent *event)
3040 if (!d->scene || !d->sceneInteractionAllowed)
3042 if (!d->lastDragDropEvent) {
3043 qWarning(
"QGraphicsView::dragLeaveEvent: drag leave received before drag enter");
3048 QGraphicsSceneDragDropEvent sceneEvent(QEvent::GraphicsSceneDragLeave);
3049 sceneEvent.setScenePos(d->lastDragDropEvent->scenePos());
3050 sceneEvent.setScreenPos(d->lastDragDropEvent->screenPos());
3051 sceneEvent.setButtons(d->lastDragDropEvent->buttons());
3052 sceneEvent.setModifiers(d->lastDragDropEvent->modifiers());
3053 sceneEvent.setPossibleActions(d->lastDragDropEvent->possibleActions());
3054 sceneEvent.setProposedAction(d->lastDragDropEvent->proposedAction());
3055 sceneEvent.setDropAction(d->lastDragDropEvent->dropAction());
3056 sceneEvent.setMimeData(d->lastDragDropEvent->mimeData());
3057 sceneEvent.setWidget(d->lastDragDropEvent->widget());
3058 sceneEvent.setSource(d->lastDragDropEvent->source());
3059 delete d->lastDragDropEvent;
3060 d->lastDragDropEvent =
nullptr;
3063 QCoreApplication::sendEvent(d->scene, &sceneEvent);
3066 if (sceneEvent.isAccepted())
3067 event->setAccepted(
true);
3071
3072
3073void QGraphicsView::dragMoveEvent(QDragMoveEvent *event)
3076 if (!d->scene || !d->sceneInteractionAllowed)
3080 QGraphicsSceneDragDropEvent sceneEvent(QEvent::GraphicsSceneDragMove);
3081 d->populateSceneDragDropEvent(&sceneEvent, event);
3084 d->storeDragDropEvent(&sceneEvent);
3087 QCoreApplication::sendEvent(d->scene, &sceneEvent);
3090 event->setAccepted(sceneEvent.isAccepted());
3091 if (sceneEvent.isAccepted())
3092 event->setDropAction(sceneEvent.dropAction());
3097
3098
3099void QGraphicsView::focusInEvent(QFocusEvent *event)
3102 d->updateInputMethodSensitivity();
3103 QAbstractScrollArea::focusInEvent(event);
3105 QCoreApplication::sendEvent(d->scene, event);
3107 if (!d->scene || !event->isAccepted())
3108 QAbstractScrollArea::focusInEvent(event);
3112
3113
3114bool QGraphicsView::focusNextPrevChild(
bool next)
3116 return QAbstractScrollArea::focusNextPrevChild(next);
3120
3121
3122void QGraphicsView::focusOutEvent(QFocusEvent *event)
3125 QAbstractScrollArea::focusOutEvent(event);
3127 QCoreApplication::sendEvent(d->scene, event);
3131
3132
3133void QGraphicsView::keyPressEvent(QKeyEvent *event)
3136 if (!d->scene || !d->sceneInteractionAllowed) {
3137 QAbstractScrollArea::keyPressEvent(event);
3140 QCoreApplication::sendEvent(d->scene, event);
3141 if (!event->isAccepted())
3142 QAbstractScrollArea::keyPressEvent(event);
3146
3147
3148void QGraphicsView::keyReleaseEvent(QKeyEvent *event)
3151 if (!d->scene || !d->sceneInteractionAllowed)
3153 QCoreApplication::sendEvent(d->scene, event);
3154 if (!event->isAccepted())
3155 QAbstractScrollArea::keyReleaseEvent(event);
3159
3160
3161void QGraphicsView::mouseDoubleClickEvent(QMouseEvent *event)
3164 if (!d->scene || !d->sceneInteractionAllowed)
3167 d->storeMouseEvent(event);
3168 d->mousePressViewPoint = event->position().toPoint();
3169 d->mousePressScenePoint = mapToScene(d->mousePressViewPoint);
3170 d->mousePressScreenPoint = event->globalPosition().toPoint();
3171 d->lastMouseMoveScenePoint = d->mousePressScenePoint;
3172 d->lastMouseMoveScreenPoint = d->mousePressScreenPoint;
3173 d->mousePressButton = event->button();
3175 QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseDoubleClick);
3176 mouseEvent.setWidget(viewport());
3177 mouseEvent.setButtonDownScenePos(d->mousePressButton, d->mousePressScenePoint);
3178 mouseEvent.setButtonDownScreenPos(d->mousePressButton, d->mousePressScreenPoint);
3179 mouseEvent.setScenePos(mapToScene(d->mousePressViewPoint));
3180 mouseEvent.setScreenPos(d->mousePressScreenPoint);
3181 mouseEvent.setLastScenePos(d->lastMouseMoveScenePoint);
3182 mouseEvent.setLastScreenPos(d->lastMouseMoveScreenPoint);
3183 mouseEvent.setButtons(event->buttons());
3184 mouseEvent.setAccepted(
false);
3185 mouseEvent.setButton(event->button());
3186 mouseEvent.setModifiers(event->modifiers());
3187 mouseEvent.setSource(event->source());
3188 mouseEvent.setFlags(event->flags());
3189 mouseEvent.setTimestamp(event->timestamp());
3190 if (event->spontaneous())
3191 qt_sendSpontaneousEvent(d->scene, &mouseEvent);
3193 QCoreApplication::sendEvent(d->scene, &mouseEvent);
3196 const bool isAccepted = mouseEvent.isAccepted();
3197 event->setAccepted(isAccepted);
3200 d->lastMouseEvent->setAccepted(isAccepted);
3204
3205
3206void QGraphicsView::mousePressEvent(QMouseEvent *event)
3213 d->storeMouseEvent(event);
3214 d->lastMouseEvent->setAccepted(
false);
3216 if (d->sceneInteractionAllowed) {
3218 d->mousePressViewPoint = event->position().toPoint();
3219 d->mousePressScenePoint = mapToScene(d->mousePressViewPoint);
3220 d->mousePressScreenPoint = event->globalPosition().toPoint();
3221 d->lastMouseMoveScenePoint = d->mousePressScenePoint;
3222 d->lastMouseMoveScreenPoint = d->mousePressScreenPoint;
3223 d->mousePressButton = event->button();
3227 QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMousePress);
3228 mouseEvent.setWidget(viewport());
3229 mouseEvent.setButtonDownScenePos(d->mousePressButton, d->mousePressScenePoint);
3230 mouseEvent.setButtonDownScreenPos(d->mousePressButton, d->mousePressScreenPoint);
3231 mouseEvent.setScenePos(d->mousePressScenePoint);
3232 mouseEvent.setScreenPos(d->mousePressScreenPoint);
3233 mouseEvent.setLastScenePos(d->lastMouseMoveScenePoint);
3234 mouseEvent.setLastScreenPos(d->lastMouseMoveScreenPoint);
3235 mouseEvent.setButtons(event->buttons());
3236 mouseEvent.setButton(event->button());
3237 mouseEvent.setModifiers(event->modifiers());
3238 mouseEvent.setSource(event->source());
3239 mouseEvent.setFlags(event->flags());
3240 mouseEvent.setAccepted(
false);
3241 mouseEvent.setTimestamp(event->timestamp());
3242 if (event->spontaneous())
3243 qt_sendSpontaneousEvent(d->scene, &mouseEvent);
3245 QCoreApplication::sendEvent(d->scene, &mouseEvent);
3248 bool isAccepted = mouseEvent.isAccepted();
3249 event->setAccepted(isAccepted);
3252 d->lastMouseEvent->setAccepted(isAccepted);
3259#if QT_CONFIG(rubberband)
3260 if (d->dragMode == QGraphicsView::RubberBandDrag && !d->rubberBanding) {
3261 if (d->sceneInteractionAllowed) {
3264 d->rubberBanding =
true;
3265 d->rubberBandRect = QRect();
3267 bool extendSelection = (event->modifiers() & Qt::ControlModifier) != 0;
3269 if (extendSelection) {
3270 d->rubberBandSelectionOperation = Qt::AddToSelection;
3272 d->rubberBandSelectionOperation = Qt::ReplaceSelection;
3273 d->scene->clearSelection();
3279 if (d->dragMode == QGraphicsView::ScrollHandDrag && event->button() == Qt::LeftButton) {
3282 d->handScrolling =
true;
3283 d->handScrollMotions = 0;
3285 viewport()->setCursor(Qt::ClosedHandCursor);
3291
3292
3293void QGraphicsView::mouseMoveEvent(QMouseEvent *event)
3297 if (d->dragMode == QGraphicsView::ScrollHandDrag) {
3298 if (d->handScrolling) {
3299 QScrollBar *hBar = horizontalScrollBar();
3300 QScrollBar *vBar = verticalScrollBar();
3301 QPoint delta = (event->position() - d->lastMouseEvent->position()).toPoint();
3302 hBar->setValue(hBar->value() + (isRightToLeft() ? delta.x() : -delta.x()));
3303 vBar->setValue(vBar->value() - delta.y());
3307 ++d->handScrollMotions;
3311 d->mouseMoveEventHandler(event);
3315
3316
3317void QGraphicsView::mouseReleaseEvent(QMouseEvent *event)
3321#if QT_CONFIG(rubberband)
3322 if (d->dragMode == QGraphicsView::RubberBandDrag && d->sceneInteractionAllowed && !event->buttons()) {
3323 d->clearRubberBand();
3326 if (d->dragMode == QGraphicsView::ScrollHandDrag && event->button() == Qt::LeftButton) {
3331 viewport()->setCursor(Qt::OpenHandCursor);
3333 d->handScrolling =
false;
3335 if (d->scene && d->sceneInteractionAllowed && !d->lastMouseEvent->isAccepted() && d->handScrollMotions <= 6) {
3339 d->scene->clearSelection();
3343 d->storeMouseEvent(event);
3345 if (!d->sceneInteractionAllowed)
3351 QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseRelease);
3352 mouseEvent.setWidget(viewport());
3353 mouseEvent.setButtonDownScenePos(d->mousePressButton, d->mousePressScenePoint);
3354 mouseEvent.setButtonDownScreenPos(d->mousePressButton, d->mousePressScreenPoint);
3355 mouseEvent.setScenePos(mapToScene(event->position().toPoint()));
3356 mouseEvent.setScreenPos(event->globalPosition().toPoint());
3357 mouseEvent.setLastScenePos(d->lastMouseMoveScenePoint);
3358 mouseEvent.setLastScreenPos(d->lastMouseMoveScreenPoint);
3359 mouseEvent.setButtons(event->buttons());
3360 mouseEvent.setButton(event->button());
3361 mouseEvent.setModifiers(event->modifiers());
3362 mouseEvent.setSource(event->source());
3363 mouseEvent.setFlags(event->flags());
3364 mouseEvent.setAccepted(
false);
3365 mouseEvent.setTimestamp(event->timestamp());
3366 if (event->spontaneous())
3367 qt_sendSpontaneousEvent(d->scene, &mouseEvent);
3369 QCoreApplication::sendEvent(d->scene, &mouseEvent);
3372 d->lastMouseEvent->setAccepted(mouseEvent.isAccepted());
3373 event->setAccepted(mouseEvent.isAccepted());
3376 if (mouseEvent.isAccepted() && mouseEvent.buttons() == 0 && viewport()->testAttribute(Qt::WA_SetCursor)) {
3378 d->_q_unsetViewportCursor();
3383#if QT_CONFIG(wheelevent)
3385
3386
3387void QGraphicsView::wheelEvent(QWheelEvent *event)
3390 if (!d->scene || !d->sceneInteractionAllowed) {
3391 QAbstractScrollArea::wheelEvent(event);
3397 QGraphicsSceneWheelEvent wheelEvent(QEvent::GraphicsSceneWheel);
3398 wheelEvent.setWidget(viewport());
3399 wheelEvent.setScenePos(mapToScene(event->position().toPoint()));
3400 wheelEvent.setScreenPos(event->globalPosition().toPoint());
3401 wheelEvent.setButtons(event->buttons());
3402 wheelEvent.setModifiers(event->modifiers());
3403 const bool horizontal = qAbs(event->angleDelta().x()) > qAbs(event->angleDelta().y());
3404 wheelEvent.setDelta(horizontal ? event->angleDelta().x() : event->angleDelta().y());
3405 wheelEvent.setPixelDelta(event->pixelDelta());
3406 wheelEvent.setPhase(event->phase());
3407 wheelEvent.setInverted(event->isInverted());
3408 wheelEvent.setOrientation(horizontal ? Qt::Horizontal : Qt::Vertical);
3409 wheelEvent.setAccepted(
false);
3410 wheelEvent.setTimestamp(event->timestamp());
3411 QCoreApplication::sendEvent(d->scene, &wheelEvent);
3412 event->setAccepted(wheelEvent.isAccepted());
3413 if (!event->isAccepted())
3414 QAbstractScrollArea::wheelEvent(event);
3419
3420
3421void QGraphicsView::paintEvent(QPaintEvent *event)
3425 QAbstractScrollArea::paintEvent(event);
3430 d->scene->d_func()->painterStateProtection = !(d->optimizationFlags & DontSavePainterState);
3433 d->exposedRegion = event->region();
3434 QRectF exposedSceneRect = mapToScene(d->exposedRegion.boundingRect()).boundingRect();
3437 QPainter painter(viewport());
3438#if QT_CONFIG(rubberband)
3439 if (d->rubberBanding && !d->rubberBandRect.isEmpty())
3443 painter.setRenderHints(painter.renderHints(),
false);
3444 painter.setRenderHints(d->renderHints,
true);
3447 const bool viewTransformed = isTransformed();
3448 if (viewTransformed)
3449 painter.setWorldTransform(viewportTransform());
3450 const QTransform viewTransform = painter.worldTransform();
3452 const auto actuallyDraw = [&]() {
3454 if (d->cacheMode & CacheBackground) {
3457 if (d->mustResizeBackgroundPixmap) {
3458 const qreal dpr = d->viewport->devicePixelRatio();
3459 d->backgroundPixmap = QPixmap(viewport()->size() * dpr);
3460 d->backgroundPixmap.setDevicePixelRatio(dpr);
3461 QBrush bgBrush = viewport()->palette().brush(viewport()->backgroundRole());
3462 if (!bgBrush.isOpaque())
3463 d->backgroundPixmap.fill(Qt::transparent);
3464 QPainter p(&d->backgroundPixmap);
3465 p.fillRect(0, 0, d->backgroundPixmap.width(), d->backgroundPixmap.height(), bgBrush);
3466 d->backgroundPixmapExposed = QRegion(viewport()->rect());
3467 d->mustResizeBackgroundPixmap =
false;
3471 if (!d->backgroundPixmapExposed.isEmpty()) {
3472 QPainter backgroundPainter(&d->backgroundPixmap);
3473 backgroundPainter.setClipRegion(d->backgroundPixmapExposed, Qt::ReplaceClip);
3474 if (viewTransformed)
3475 backgroundPainter.setTransform(viewTransform);
3476 QRectF backgroundExposedSceneRect = mapToScene(d->backgroundPixmapExposed.boundingRect()).boundingRect();
3477 drawBackground(&backgroundPainter, backgroundExposedSceneRect);
3478 d->backgroundPixmapExposed = QRegion();
3482 if (viewTransformed) {
3483 painter.setWorldTransform(QTransform());
3484 painter.drawPixmap(QPoint(), d->backgroundPixmap);
3485 painter.setWorldTransform(viewTransform);
3487 painter.drawPixmap(QPoint(), d->backgroundPixmap);
3490 if (!(d->optimizationFlags & DontSavePainterState))
3493 drawBackground(&painter, exposedSceneRect);
3494 if (!(d->optimizationFlags & DontSavePainterState))
3499 if (!(d->optimizationFlags & IndirectPainting)) {
3500 const quint32 oldRectAdjust = d->scene->d_func()->rectAdjust;
3501 if (d->optimizationFlags & QGraphicsView::DontAdjustForAntialiasing)
3502 d->scene->d_func()->rectAdjust = 1;
3504 d->scene->d_func()->rectAdjust = 2;
3505 d->scene->d_func()->drawItems(&painter, viewTransformed ? &viewTransform :
nullptr,
3506 &d->exposedRegion, viewport());
3507 d->scene->d_func()->rectAdjust = oldRectAdjust;
3514 if (!d->scene->d_func()->painterStateProtection)
3515 painter.setOpacity(1.0);
3516 painter.setWorldTransform(viewTransform);
3519 if (!d->scene->d_func()->unpolishedItems.isEmpty())
3520 d->scene->d_func()->_q_polishItems();
3523 d->scene->d_func()->updateAll =
false;
3526 bool allItems =
false;
3527 QList<QGraphicsItem *> itemList = d->findItems(d->exposedRegion, &allItems, viewTransform);
3528 if (!itemList.isEmpty()) {
3530 const int numItems = itemList.size();
3531 QGraphicsItem **itemArray = &itemList[0];
3532 QStyleOptionGraphicsItem *styleOptionArray = d->allocStyleOptionsArray(numItems);
3533 QTransform transform(Qt::Uninitialized);
3534 for (
int i = 0; i < numItems; ++i) {
3535 QGraphicsItem *item = itemArray[i];
3536 QGraphicsItemPrivate *itemd = item->d_ptr.data();
3537 itemd->initStyleOption(&styleOptionArray[i], viewTransform, d->exposedRegion, allItems);
3543 const QRectF brect = adjustedItemEffectiveBoundingRect(item);
3544 if (!itemd->itemIsUntransformable()) {
3545 transform = item->sceneTransform();
3546 if (viewTransformed)
3547 transform *= viewTransform;
3549 transform = item->deviceTransform(viewTransform);
3551 itemd->paintedViewBoundingRects.insert(d->viewport, transform.mapRect(brect).toRect());
3554 drawItems(&painter, numItems, itemArray, styleOptionArray);
3555 d->freeStyleOptionsArray(styleOptionArray);
3560 drawForeground(&painter, exposedSceneRect);
3562 #if QT_CONFIG(rubberband)
3564 if (d->rubberBanding && !d->rubberBandRect.isEmpty()) {
3566 QStyleOptionRubberBand option;
3567 option.initFrom(viewport());
3568 option.rect = d->rubberBandRect;
3569 option.shape = QRubberBand::Rectangle;
3571 QStyleHintReturnMask mask;
3572 if (viewport()->style()->styleHint(QStyle::SH_RubberBand_Mask, &option, viewport(), &mask)) {
3574 painter.setClipRegion(mask.region, Qt::IntersectClip);
3577 viewport()->style()->drawControl(QStyle::CE_RubberBand, &option, &painter, viewport());
3585 if (d->stereoEnabled) {
3586 QWidgetPrivate* w = QWidgetPrivate::get(viewport());
3587 if (w->toggleStereoTargetBuffer()) {
3589 w->toggleStereoTargetBuffer();
3596 d->scene->d_func()->painterStateProtection =
true;
3600
3601
3602void QGraphicsView::resizeEvent(QResizeEvent *event)
3607 QPointF oldLastCenterPoint = d->lastCenterPoint;
3609 QAbstractScrollArea::resizeEvent(event);
3610 d->recalculateContentSize();
3613 if (d->resizeAnchor == NoAnchor && !d->keepLastCenterPoint) {
3614 d->updateLastCenterPoint();
3616 d->lastCenterPoint = oldLastCenterPoint;
3618 d->centerView(d->resizeAnchor);
3619 d->keepLastCenterPoint =
false;
3621 if (d->cacheMode & CacheBackground) {
3623 d->mustResizeBackgroundPixmap =
true;
3628
3629
3630void QGraphicsView::scrollContentsBy(
int dx,
int dy)
3633 d->dirtyScroll =
true;
3634 if (d->transforming)
3636 if (isRightToLeft())
3639 if (d->viewportUpdateMode != QGraphicsView::NoViewportUpdate) {
3640 if (d->viewportUpdateMode != QGraphicsView::FullViewportUpdate) {
3641 if (d->accelerateScrolling) {
3642#if QT_CONFIG(rubberband)
3644 if (!d->rubberBandRect.isEmpty()) {
3645 QRegion rubberBandRegion(d->rubberBandRegion(viewport(), d->rubberBandRect));
3646 rubberBandRegion += rubberBandRegion.translated(-dx, -dy);
3647 viewport()->update(rubberBandRegion);
3650 d->dirtyScrollOffset.rx() += dx;
3651 d->dirtyScrollOffset.ry() += dy;
3652 d->dirtyRegion.translate(dx, dy);
3653 viewport()->scroll(dx, dy);
3662 d->updateLastCenterPoint();
3664 if (d->cacheMode & CacheBackground) {
3667 const qreal dpr = d->backgroundPixmap.devicePixelRatio();
3668 if (dpr == std::trunc(dpr)) {
3669 const qreal inverseDpr = qreal(1) / dpr;
3673 if (!d->backgroundPixmap.isNull())
3674 d->backgroundPixmap.scroll(dx * dpr, dy * dpr, d->backgroundPixmap.rect(), &exposed);
3677 d->backgroundPixmapExposed.translate(dx, dy);
3678 const QRegion exposedScaled = QTransform::fromScale(inverseDpr, inverseDpr).map(exposed);
3679 d->backgroundPixmapExposed += exposedScaled;
3682 d->backgroundPixmapExposed += QRegion(viewport()->rect());
3687 if (d->sceneInteractionAllowed)
3688 d->replayLastMouseEvent();
3692
3693
3694void QGraphicsView::showEvent(QShowEvent *event)
3697 d->recalculateContentSize();
3698 d->centerView(d->transformationAnchor);
3699 QAbstractScrollArea::showEvent(event);
3703
3704
3705void QGraphicsView::inputMethodEvent(QInputMethodEvent *event)
3709 QCoreApplication::sendEvent(d->scene, event);
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729void QGraphicsView::drawBackground(QPainter *painter,
const QRectF &rect)
3732 if (d->scene && d->backgroundBrush.style() == Qt::NoBrush) {
3733 d->scene->drawBackground(painter, rect);
3737 const bool wasAa = painter->testRenderHint(QPainter::Antialiasing);
3739 painter->setRenderHints(QPainter::Antialiasing,
false);
3740 painter->fillRect(rect, d->backgroundBrush);
3742 painter->setRenderHints(QPainter::Antialiasing,
true);
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762void QGraphicsView::drawForeground(QPainter *painter,
const QRectF &rect)
3765 if (d->scene && d->foregroundBrush.style() == Qt::NoBrush) {
3766 d->scene->drawForeground(painter, rect);
3770 painter->fillRect(rect, d->foregroundBrush);
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790void QGraphicsView::drawItems(QPainter *painter,
int numItems,
3791 QGraphicsItem *items[],
3792 const QStyleOptionGraphicsItem options[])
3796 QWidget *widget = painter->device() == viewport() ? viewport() :
nullptr;
3797 d->scene->drawItems(painter, numItems, items, options, widget);
3802
3803
3804
3805
3806
3807QTransform QGraphicsView::transform()
const
3809 Q_D(
const QGraphicsView);
3814
3815
3816
3817
3818QTransform QGraphicsView::viewportTransform()
const
3820 Q_D(
const QGraphicsView);
3821 QTransform moveMatrix = QTransform::fromTranslate(-d->horizontalScroll(), -d->verticalScroll());
3822 return d->identityMatrix ? moveMatrix : d->matrix * moveMatrix;
3826
3827
3828
3829
3830
3831
3832
3833bool QGraphicsView::isTransformed()
const
3835 Q_D(
const QGraphicsView);
3836 return !d->identityMatrix || d->horizontalScroll() || d->verticalScroll();
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865void QGraphicsView::setTransform(
const QTransform &matrix,
bool combine )
3868 QTransform oldMatrix = d->matrix;
3872 d->matrix = matrix * d->matrix;
3873 if (oldMatrix == d->matrix)
3876 d->identityMatrix = d->matrix.isIdentity();
3877 d->transforming =
true;
3879 d->recalculateContentSize();
3880 d->centerView(d->transformationAnchor);
3882 d->updateLastCenterPoint();
3885 if (d->sceneInteractionAllowed)
3886 d->replayLastMouseEvent();
3887 d->transforming =
false;
3894
3895
3896
3897
3898void QGraphicsView::resetTransform()
3900 setTransform(QTransform());
3903QPointF QGraphicsViewPrivate::mapToScene(
const QPointF &point)
const
3906 p.rx() += horizontalScroll();
3907 p.ry() += verticalScroll();
3908 return identityMatrix ? p : matrix.inverted().map(p);
3911QRectF QGraphicsViewPrivate::mapToScene(
const QRectF &rect)
const
3913 QPointF scrollOffset(horizontalScroll(), verticalScroll());
3914 QPointF tl = scrollOffset + rect.topLeft();
3915 QPointF tr = scrollOffset + rect.topRight();
3916 QPointF br = scrollOffset + rect.bottomRight();
3917 QPointF bl = scrollOffset + rect.bottomLeft();
3920 if (!identityMatrix) {
3921 QTransform x = matrix.inverted();
3922 poly[0] = x.map(tl);
3923 poly[1] = x.map(tr);
3924 poly[2] = x.map(br);
3925 poly[3] = x.map(bl);
3932 return poly.boundingRect();
3937#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)