Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qgraphicsscene_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QGRAPHICSSCENE_P_H
6#define QGRAPHICSSCENE_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists for the convenience
13// of other Qt classes. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtWidgets/private/qtwidgetsglobal_p.h>
20#include "qgraphicsscene.h"
21
23#include "qgraphicsview.h"
26
27#include <private/qobject_p.h>
28#include <QtCore/qbitarray.h>
29#include <QtCore/qlist.h>
30#include <QtCore/qmap.h>
31#include <QtCore/qset.h>
32#include <QtGui/qfont.h>
33#include <QtGui/qpalette.h>
34#include <QtWidgets/qstyle.h>
35#include <QtWidgets/qstyleoption.h>
36
37#include <set>
38#include <tuple>
39
41
42QT_BEGIN_NAMESPACE
43
44class QGestureEvent;
45class QGraphicsSceneIndex;
46class QGraphicsView;
47class QGraphicsWidget;
48
49class Q_AUTOTEST_EXPORT QGraphicsScenePrivate : public QObjectPrivate
50{
51 Q_DECLARE_PUBLIC(QGraphicsScene)
52public:
53 QGraphicsScenePrivate();
54 void init();
55
56 static QGraphicsScenePrivate *get(QGraphicsScene *q);
57
58 int changedSignalIndex;
59 int processDirtyItemsIndex;
60 int polishItemsIndex;
61
62 QGraphicsScene::ItemIndexMethod indexMethod;
63 QGraphicsSceneIndex *index;
64
65 int lastItemCount;
66
67 QRectF sceneRect;
68
69 quint32 hasSceneRect : 1;
70 quint32 dirtyGrowingItemsBoundingRect : 1;
71 quint32 updateAll : 1;
72 quint32 calledEmitUpdated : 1;
73 quint32 processDirtyItemsEmitted : 1;
74 quint32 needSortTopLevelItems : 1;
75 quint32 holesInTopLevelSiblingIndex : 1;
76 quint32 topLevelSequentialOrdering : 1;
77 quint32 scenePosDescendantsUpdatePending : 1;
78 quint32 stickyFocus : 1;
79 quint32 hasFocus : 1;
80 quint32 lastMouseGrabberItemHasImplicitMouseGrab : 1;
81 quint32 allItemsIgnoreHoverEvents : 1;
82 quint32 allItemsUseDefaultCursor : 1;
83 quint32 painterStateProtection : 1;
84 quint32 sortCacheEnabled : 1; // for compatibility
85 quint32 allItemsIgnoreTouchEvents : 1;
86 quint32 focusOnTouch : 1;
87 quint32 padding : 14;
88
89 qreal minimumRenderSize;
90
91 QRectF growingItemsBoundingRect;
92
93 void _q_emitUpdated();
94
95 struct UpdatedRectsCmp
96 {
97 bool operator() (const QRectF &a, const QRectF &b) const noexcept
98 {
99 return std::make_tuple(a.y(), a.x(), a.height(), a.width())
100 < std::make_tuple(b.y(), b.x(), b.height(), b.width());
101 }
102 };
103
104 // std::set was used here instead of std::unordered_set due to requiring only a comparator and
105 // showing equivalent performance in empirical measurements within the ranges of interest...
106 std::set<QRectF, UpdatedRectsCmp> updatedRects;
107
108 QPainterPath selectionArea;
109 int selectionChanging;
110 QSet<QGraphicsItem *> selectedItems;
111 QList<QGraphicsItem *> unpolishedItems;
112 QList<QGraphicsItem *> topLevelItems;
113
114 QHash<QGraphicsItem *, QPointF> movingItemsInitialPositions;
115 void registerTopLevelItem(QGraphicsItem *item);
116 void unregisterTopLevelItem(QGraphicsItem *item);
117 void _q_updateLater();
118 void _q_polishItems();
119
120 void _q_processDirtyItems();
121
122 QSet<QGraphicsItem *> scenePosItems;
123 void setScenePosItemEnabled(QGraphicsItem *item, bool enabled);
124 void registerScenePosItem(QGraphicsItem *item);
125 void unregisterScenePosItem(QGraphicsItem *item);
126 void _q_updateScenePosDescendants();
127
128 void removeItemHelper(QGraphicsItem *item);
129
130 QBrush backgroundBrush;
131 QBrush foregroundBrush;
132
133 quint32 rectAdjust;
134 QGraphicsItem *focusItem;
135 QGraphicsItem *lastFocusItem;
136 QGraphicsItem *passiveFocusItem;
137 QGraphicsWidget *tabFocusFirst;
138 QGraphicsItem *activePanel;
139 QGraphicsItem *lastActivePanel;
140 int activationRefCount;
141 int childExplicitActivation;
142 void setActivePanelHelper(QGraphicsItem *item, bool duringActivationEvent);
143 void setFocusItemHelper(QGraphicsItem *item, Qt::FocusReason focusReason,
144 bool emitFocusChanged = true);
145
146 QList<QGraphicsWidget *> popupWidgets;
147 void addPopup(QGraphicsWidget *widget);
148 void removePopup(QGraphicsWidget *widget, bool itemIsDying = false);
149
150 QGraphicsItem *lastMouseGrabberItem;
151 QList<QGraphicsItem *> mouseGrabberItems;
152 void grabMouse(QGraphicsItem *item, bool implicit = false);
153 void ungrabMouse(QGraphicsItem *item, bool itemIsDying = false);
154 void clearMouseGrabber();
155
156 QList<QGraphicsItem *> keyboardGrabberItems;
157 void grabKeyboard(QGraphicsItem *item);
158 void ungrabKeyboard(QGraphicsItem *item, bool itemIsDying = false);
159 void clearKeyboardGrabber();
160
161 QGraphicsItem *dragDropItem;
162 QGraphicsWidget *enterWidget;
163 Qt::DropAction lastDropAction;
164 QList<QGraphicsItem *> cachedItemsUnderMouse;
165 QList<QGraphicsItem *> hoverItems;
166 QPointF lastSceneMousePos;
167 void enableMouseTrackingOnViews();
168 QMap<Qt::MouseButton, QPointF> mouseGrabberButtonDownPos;
169 QMap<Qt::MouseButton, QPointF> mouseGrabberButtonDownScenePos;
170 QMap<Qt::MouseButton, QPoint> mouseGrabberButtonDownScreenPos;
171 QList<QGraphicsItem *> itemsAtPosition(const QPoint &screenPos,
172 const QPointF &scenePos,
173 QWidget *widget) const;
174 void storeMouseButtonsForMouseGrabber(QGraphicsSceneMouseEvent *event);
175
176 QList<QGraphicsView *> views;
177 void addView(QGraphicsView *view);
178 void removeView(QGraphicsView *view);
179
180 QMultiMap<QGraphicsItem *, QGraphicsItem *> sceneEventFilters;
181 void installSceneEventFilter(QGraphicsItem *watched, QGraphicsItem *filter);
182 void removeSceneEventFilter(QGraphicsItem *watched, QGraphicsItem *filter);
183 bool filterDescendantEvent(QGraphicsItem *item, QEvent *event);
184 bool filterEvent(QGraphicsItem *item, QEvent *event);
185 bool sendEvent(QGraphicsItem *item, QEvent *event);
186
187 bool dispatchHoverEvent(QGraphicsSceneHoverEvent *hoverEvent);
188 bool itemAcceptsHoverEvents_helper(const QGraphicsItem *item) const;
189 void leaveScene(QWidget *viewport);
190
191 void cloneDragDropEvent(QGraphicsSceneDragDropEvent *dest,
192 QGraphicsSceneDragDropEvent *source);
193 void sendDragDropEvent(QGraphicsItem *item,
194 QGraphicsSceneDragDropEvent *dragDropEvent);
195 void sendHoverEvent(QEvent::Type type, QGraphicsItem *item,
196 QGraphicsSceneHoverEvent *hoverEvent);
197 void sendMouseEvent(QGraphicsSceneMouseEvent *mouseEvent);
198 void mousePressEventHandler(QGraphicsSceneMouseEvent *mouseEvent);
199 QGraphicsWidget *windowForItem(const QGraphicsItem *item) const;
200
201 void drawItemHelper(QGraphicsItem *item, QPainter *painter,
202 const QStyleOptionGraphicsItem *option, QWidget *widget,
203 bool painterStateProtection);
204
205 void drawItems(QPainter *painter, const QTransform *const viewTransform,
206 QRegion *exposedRegion, QWidget *widget);
207
208 void drawSubtreeRecursive(QGraphicsItem *item, QPainter *painter, const QTransform *const,
209 QRegion *exposedRegion, QWidget *widget, qreal parentOpacity = qreal(1.0),
210 const QTransform *const effectTransform = nullptr);
211 void draw(QGraphicsItem *, QPainter *, const QTransform *const, const QTransform *const,
212 QRegion *, QWidget *, qreal, const QTransform *const, bool, bool);
213
214 void markDirty(QGraphicsItem *item, const QRectF &rect = QRectF(), bool invalidateChildren = false,
215 bool force = false, bool ignoreOpacity = false, bool removingItemFromScene = false,
216 bool updateBoundingRect = false);
217 void processDirtyItemsRecursive(QGraphicsItem *item, bool dirtyAncestorContainsChildren = false,
218 qreal parentOpacity = qreal(1.0));
219
220 inline void resetDirtyItem(QGraphicsItem *item, bool recursive = false)
221 {
222 Q_ASSERT(item);
223 item->d_ptr->dirty = 0;
224 item->d_ptr->paintedViewBoundingRectsNeedRepaint = 0;
225 item->d_ptr->geometryChanged = 0;
226 if (!item->d_ptr->dirtyChildren)
227 recursive = false;
228 item->d_ptr->dirtyChildren = 0;
229 item->d_ptr->needsRepaint = QRectF();
230 item->d_ptr->allChildrenDirty = 0;
231 item->d_ptr->fullUpdatePending = 0;
232 item->d_ptr->ignoreVisible = 0;
233 item->d_ptr->ignoreOpacity = 0;
234#if QT_CONFIG(graphicseffect)
235 QGraphicsEffect::ChangeFlags flags;
236 if (item->d_ptr->notifyBoundingRectChanged) {
237 flags |= QGraphicsEffect::SourceBoundingRectChanged;
238 item->d_ptr->notifyBoundingRectChanged = 0;
239 }
240 if (item->d_ptr->notifyInvalidated) {
241 flags |= QGraphicsEffect::SourceInvalidated;
242 item->d_ptr->notifyInvalidated = 0;
243 }
244#endif // QT_CONFIG(graphicseffect)
245 if (recursive) {
246 for (int i = 0; i < item->d_ptr->children.size(); ++i)
247 resetDirtyItem(item->d_ptr->children.at(i), recursive);
248 }
249#if QT_CONFIG(graphicseffect)
250 if (flags && item->d_ptr->graphicsEffect)
251 item->d_ptr->graphicsEffect->sourceChanged(flags);
252#endif // QT_CONFIG(graphicseffect)
253 }
254
255 inline void ensureSortedTopLevelItems()
256 {
257 if (needSortTopLevelItems) {
258 std::sort(topLevelItems.begin(), topLevelItems.end(), qt_notclosestLeaf);
259 topLevelSequentialOrdering = false;
260 needSortTopLevelItems = false;
261 }
262 }
263
264 void ensureSequentialTopLevelSiblingIndexes();
265
266 QStyle *style;
267 QFont font;
268 void setFont_helper(const QFont &font);
269 void resolveFont();
270 void updateFont(const QFont &font);
271 QPalette palette;
272 void setPalette_helper(const QPalette &palette);
273 void resolvePalette();
274 void updatePalette(const QPalette &palette);
275
276 QStyleOptionGraphicsItem styleOptionTmp;
277
278 QMap<int, QEventPoint> sceneCurrentTouchPoints;
279 QMap<int, QGraphicsItem *> itemForTouchPointId;
280 static void updateTouchPointsForItem(QGraphicsItem *item, QTouchEvent *touchEvent);
281 int findClosestTouchPointId(const QPointF &scenePos);
282 void touchEventHandler(QTouchEvent *touchEvent);
283 bool sendTouchBeginEvent(QGraphicsItem *item, QTouchEvent *touchEvent);
284 void enableTouchEventsOnViews();
285
286 QList<QGraphicsObject *> cachedTargetItems;
287#ifndef QT_NO_GESTURES
288 QHash<QGraphicsObject *, QSet<QGesture *> > cachedItemGestures;
289 QHash<QGraphicsObject *, QSet<QGesture *> > cachedAlreadyDeliveredGestures;
290 QHash<QGesture *, QGraphicsObject *> gestureTargets;
291 QHash<Qt::GestureType, int> grabbedGestures;
292 void gestureEventHandler(QGestureEvent *event);
293 void gestureTargetsAtHotSpots(const QSet<QGesture *> &gestures,
294 Qt::GestureFlag flag,
295 QHash<QGraphicsObject *, QSet<QGesture *> > *targets,
296 QSet<QGraphicsObject *> *itemsSet = nullptr,
297 QSet<QGesture *> *normal = nullptr,
298 QSet<QGesture *> *conflicts = nullptr);
299 void cancelGesturesForChildren(QGesture *original);
300 void grabGesture(QGraphicsItem *, Qt::GestureType gesture);
301 void ungrabGesture(QGraphicsItem *, Qt::GestureType gesture);
302#endif // QT_NO_GESTURES
303
304 void updateInputMethodSensitivityInViews();
305
306 QList<QGraphicsItem *> modalPanels;
307 void enterModal(QGraphicsItem *item,
308 QGraphicsItem::PanelModality panelModality = QGraphicsItem::NonModal);
309 void leaveModal(QGraphicsItem *item);
310};
311
312// QRectF::intersects() returns false always if either the source or target
313// rectangle's width or height are 0. This works around that problem.
314static inline void _q_adjustRect(QRectF *rect)
315{
316 Q_ASSERT(rect);
317 if (!rect->width())
318 rect->adjust(qreal(-0.00001), 0, qreal(0.00001), 0);
319 if (!rect->height())
320 rect->adjust(0, qreal(-0.00001), 0, qreal(0.00001));
321}
322
323static inline QRectF adjustedItemBoundingRect(const QGraphicsItem *item)
324{
325 Q_ASSERT(item);
326 QRectF boundingRect(item->boundingRect());
327 _q_adjustRect(&boundingRect);
328 return boundingRect;
329}
330
331static inline QRectF adjustedItemEffectiveBoundingRect(const QGraphicsItem *item)
332{
333 Q_ASSERT(item);
334 QRectF boundingRect(QGraphicsItemPrivate::get(item)->effectiveBoundingRect());
335 _q_adjustRect(&boundingRect);
336 return boundingRect;
337}
338
339QT_END_NAMESPACE
340
341#endif
The QAbstractGraphicsShapeItem class provides a common base for all path items.
The QGraphicsEllipseItem class provides an ellipse item that you can add to a QGraphicsScene.
QHash< const QGraphicsItem *, QMap< int, QVariant > > data
The QGraphicsItemGroup class provides a container that treats a group of items as a single item.
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
The QGraphicsLineItem class provides a line item that you can add to a QGraphicsScene.
The QGraphicsObject class provides a base class for all graphics items that require signals,...
The QGraphicsPathItem class provides a path item that you can add to a QGraphicsScene.
QGraphicsPixmapItem::ShapeMode shapeMode
Qt::TransformationMode transformationMode
The QGraphicsPixmapItem class provides a pixmap item that you can add to a QGraphicsScene.
The QGraphicsPolygonItem class provides a polygon item that you can add to a QGraphicsScene.
The QGraphicsRectItem class provides a rectangle item that you can add to a QGraphicsScene.
\inmodule QtWidgets
The QGraphicsScene class provides a surface for managing a large number of 2D graphical items.
The QGraphicsSimpleTextItem class provides a simple text item that you can add to a QGraphicsScene.
void _q_updateBoundingRect(const QSizeF &)
bool _q_mouseOnEdge(QGraphicsSceneMouseEvent *)
QWidgetTextControl * control
QWidgetTextControl * textControl() const
void sendControlEvent(QEvent *e)
The QGraphicsTextItem class provides a text item that you can add to a QGraphicsScene to display form...
friend class QPainter
\inmodule QtGui
friend class QWidget
Definition qpainter.h:432
\inmodule QtCore\reentrant
Definition qpoint.h:232
The QPolygonF class provides a list of points using floating point precision.
Definition qpolygon.h:97
Combined button and popup list for selecting options.
QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2620
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2620
static void formatGraphicsItemHelper(QDebug debug, const QGraphicsItem *item)
bool _qt_movableAncestorIsSelected(const QGraphicsItem *item)
static QRectF setupTextLayout(QTextLayout *layout)
QDebug operator<<(QDebug debug, const QGraphicsObject *item)
static QPainterPath qt_graphicsItem_shapeFromPath(const QPainterPath &path, const QPen &pen)
QDebug operator<<(QDebug debug, const QGraphicsItem *item)
static bool qt_QGraphicsItem_isObscured(const QGraphicsItem *item, const QGraphicsItem *other, const QRectF &rect)
QT_REQUIRE_CONFIG(graphicsview)
static QRectF adjustedItemBoundingRect(const QGraphicsItem *item)
static QRectF adjustedItemEffectiveBoundingRect(const QGraphicsItem *item)
static void _q_adjustRect(QRectF *rect)