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
4#ifndef QGRAPHICSSCENE_H
5#define QGRAPHICSSCENE_H
6
7#include <QtWidgets/qtwidgetsglobal.h>
8#include <QtCore/qobject.h>
9#include <QtCore/qpoint.h>
10#include <QtCore/qrect.h>
11#include <QtGui/qbrush.h>
12#include <QtGui/qfont.h>
13#include <QtGui/qtransform.h>
14#include <QtGui/qpen.h>
15
16QT_REQUIRE_CONFIG(graphicsview);
17
18QT_BEGIN_NAMESPACE
19
20class QFocusEvent;
21class QFont;
22class QFontMetrics;
23class QGraphicsEllipseItem;
24class QGraphicsItem;
25class QGraphicsItemGroup;
26class QGraphicsLineItem;
27class QGraphicsPathItem;
28class QGraphicsPixmapItem;
29class QGraphicsPolygonItem;
30class QGraphicsProxyWidget;
31class QGraphicsRectItem;
32class QGraphicsSceneContextMenuEvent;
33class QGraphicsSceneDragDropEvent;
34class QGraphicsSceneEvent;
35class QGraphicsSceneHelpEvent;
36class QGraphicsSceneHoverEvent;
37class QGraphicsSceneMouseEvent;
38class QGraphicsSceneWheelEvent;
39class QGraphicsSimpleTextItem;
40class QGraphicsTextItem;
41class QGraphicsView;
42class QGraphicsWidget;
43class QGraphicsSceneIndex;
44class QHelpEvent;
45class QInputMethodEvent;
46class QKeyEvent;
47class QLineF;
48class QPainterPath;
49class QPixmap;
50class QPointF;
51class QPolygonF;
52class QRectF;
53class QSizeF;
54class QStyle;
55class QStyleOptionGraphicsItem;
56
57class QGraphicsScenePrivate;
58class Q_WIDGETS_EXPORT QGraphicsScene : public QObject
59{
60 Q_OBJECT
61 Q_PROPERTY(QBrush backgroundBrush READ backgroundBrush WRITE setBackgroundBrush)
62 Q_PROPERTY(QBrush foregroundBrush READ foregroundBrush WRITE setForegroundBrush)
63 Q_PROPERTY(ItemIndexMethod itemIndexMethod READ itemIndexMethod WRITE setItemIndexMethod)
64 Q_PROPERTY(QRectF sceneRect READ sceneRect WRITE setSceneRect)
65 Q_PROPERTY(int bspTreeDepth READ bspTreeDepth WRITE setBspTreeDepth)
66 Q_PROPERTY(QPalette palette READ palette WRITE setPalette)
67 Q_PROPERTY(QFont font READ font WRITE setFont)
68 Q_PROPERTY(bool stickyFocus READ stickyFocus WRITE setStickyFocus)
69 Q_PROPERTY(qreal minimumRenderSize READ minimumRenderSize WRITE setMinimumRenderSize)
70 Q_PROPERTY(bool focusOnTouch READ focusOnTouch WRITE setFocusOnTouch)
71
72public:
73 enum ItemIndexMethod {
74 BspTreeIndex,
75 NoIndex = -1
76 };
77 Q_ENUM(ItemIndexMethod)
78 enum SceneLayer {
79 ItemLayer = 0x1,
80 BackgroundLayer = 0x2,
81 ForegroundLayer = 0x4,
82 AllLayers = 0xffff
83 };
84 Q_DECLARE_FLAGS(SceneLayers, SceneLayer)
85
86 QGraphicsScene(QObject *parent = nullptr);
87 QGraphicsScene(const QRectF &sceneRect, QObject *parent = nullptr);
88 QGraphicsScene(qreal x, qreal y, qreal width, qreal height, QObject *parent = nullptr);
89 virtual ~QGraphicsScene();
90
91 QRectF sceneRect() const;
92 inline qreal width() const { return sceneRect().width(); }
93 inline qreal height() const { return sceneRect().height(); }
94 void setSceneRect(const QRectF &rect);
95 inline void setSceneRect(qreal x, qreal y, qreal w, qreal h)
96 { setSceneRect(QRectF(x, y, w, h)); }
97
98 void render(QPainter *painter,
99 const QRectF &target = QRectF(), const QRectF &source = QRectF(),
100 Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio);
101
102 ItemIndexMethod itemIndexMethod() const;
103 void setItemIndexMethod(ItemIndexMethod method);
104
105 int bspTreeDepth() const;
106 void setBspTreeDepth(int depth);
107
108 QRectF itemsBoundingRect() const;
109
110 QList<QGraphicsItem *> items(Qt::SortOrder order = Qt::DescendingOrder) const;
111
112 QList<QGraphicsItem *> items(const QPointF &pos, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, Qt::SortOrder order = Qt::DescendingOrder, const QTransform &deviceTransform = QTransform()) const;
113 QList<QGraphicsItem *> items(const QRectF &rect, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, Qt::SortOrder order = Qt::DescendingOrder, const QTransform &deviceTransform = QTransform()) const;
114 QList<QGraphicsItem *> items(const QPolygonF &polygon, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, Qt::SortOrder order = Qt::DescendingOrder, const QTransform &deviceTransform = QTransform()) const;
115 QList<QGraphicsItem *> items(const QPainterPath &path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, Qt::SortOrder order = Qt::DescendingOrder, const QTransform &deviceTransform = QTransform()) const;
116 inline QList<QGraphicsItem *> items(qreal x, qreal y, qreal w, qreal h, Qt::ItemSelectionMode mode, Qt::SortOrder order,
117 const QTransform &deviceTransform = QTransform()) const
118 { return items(rect: QRectF(x, y, w, h), mode, order, deviceTransform); }
119
120 QList<QGraphicsItem *> collidingItems(const QGraphicsItem *item, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
121 QGraphicsItem *itemAt(const QPointF &pos, const QTransform &deviceTransform) const;
122 inline QGraphicsItem *itemAt(qreal x, qreal y, const QTransform &deviceTransform) const
123 { return itemAt(pos: QPointF(x, y), deviceTransform); }
124
125 QList<QGraphicsItem *> selectedItems() const;
126 QPainterPath selectionArea() const;
127 void setSelectionArea(const QPainterPath &path, const QTransform &deviceTransform);
128 void setSelectionArea(const QPainterPath &path, Qt::ItemSelectionOperation selectionOperation = Qt::ReplaceSelection, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, const QTransform &deviceTransform = QTransform());
129
130 QGraphicsItemGroup *createItemGroup(const QList<QGraphicsItem *> &items);
131 void destroyItemGroup(QGraphicsItemGroup *group);
132
133 void addItem(QGraphicsItem *item);
134 QGraphicsEllipseItem *addEllipse(const QRectF &rect, const QPen &pen = QPen(), const QBrush &brush = QBrush());
135 QGraphicsLineItem *addLine(const QLineF &line, const QPen &pen = QPen());
136 QGraphicsPathItem *addPath(const QPainterPath &path, const QPen &pen = QPen(), const QBrush &brush = QBrush());
137 QGraphicsPixmapItem *addPixmap(const QPixmap &pixmap);
138 QGraphicsPolygonItem *addPolygon(const QPolygonF &polygon, const QPen &pen = QPen(), const QBrush &brush = QBrush());
139 QGraphicsRectItem *addRect(const QRectF &rect, const QPen &pen = QPen(), const QBrush &brush = QBrush());
140 QGraphicsTextItem *addText(const QString &text, const QFont &font = QFont());
141 QGraphicsSimpleTextItem *addSimpleText(const QString &text, const QFont &font = QFont());
142 QGraphicsProxyWidget *addWidget(QWidget *widget, Qt::WindowFlags wFlags = Qt::WindowFlags());
143 inline QGraphicsEllipseItem *addEllipse(qreal x, qreal y, qreal w, qreal h, const QPen &pen = QPen(), const QBrush &brush = QBrush())
144 { return addEllipse(rect: QRectF(x, y, w, h), pen, brush); }
145 inline QGraphicsLineItem *addLine(qreal x1, qreal y1, qreal x2, qreal y2, const QPen &pen = QPen())
146 { return addLine(line: QLineF(x1, y1, x2, y2), pen); }
147 inline QGraphicsRectItem *addRect(qreal x, qreal y, qreal w, qreal h, const QPen &pen = QPen(), const QBrush &brush = QBrush())
148 { return addRect(rect: QRectF(x, y, w, h), pen, brush); }
149 void removeItem(QGraphicsItem *item);
150
151 QGraphicsItem *focusItem() const;
152 void setFocusItem(QGraphicsItem *item, Qt::FocusReason focusReason = Qt::OtherFocusReason);
153 bool hasFocus() const;
154 void setFocus(Qt::FocusReason focusReason = Qt::OtherFocusReason);
155 void clearFocus();
156
157 void setStickyFocus(bool enabled);
158 bool stickyFocus() const;
159
160 QGraphicsItem *mouseGrabberItem() const;
161
162 QBrush backgroundBrush() const;
163 void setBackgroundBrush(const QBrush &brush);
164
165 QBrush foregroundBrush() const;
166 void setForegroundBrush(const QBrush &brush);
167
168 virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
169
170 QList <QGraphicsView *> views() const;
171
172 inline void update(qreal x, qreal y, qreal w, qreal h)
173 { update(rect: QRectF(x, y, w, h)); }
174 inline void invalidate(qreal x, qreal y, qreal w, qreal h, SceneLayers layers = AllLayers)
175 { invalidate(rect: QRectF(x, y, w, h), layers); }
176
177 QStyle *style() const;
178 void setStyle(QStyle *style);
179
180 QFont font() const;
181 void setFont(const QFont &font);
182
183 QPalette palette() const;
184 void setPalette(const QPalette &palette);
185
186 bool isActive() const;
187 QGraphicsItem *activePanel() const;
188 void setActivePanel(QGraphicsItem *item);
189 QGraphicsWidget *activeWindow() const;
190 void setActiveWindow(QGraphicsWidget *widget);
191
192 bool sendEvent(QGraphicsItem *item, QEvent *event);
193
194 qreal minimumRenderSize() const;
195 void setMinimumRenderSize(qreal minSize);
196
197 bool focusOnTouch() const;
198 void setFocusOnTouch(bool enabled);
199
200public Q_SLOTS:
201 void update(const QRectF &rect = QRectF());
202 void invalidate(const QRectF &rect = QRectF(), SceneLayers layers = AllLayers);
203 void advance();
204 void clearSelection();
205 void clear();
206
207protected:
208 bool event(QEvent *event) override;
209 bool eventFilter(QObject *watched, QEvent *event) override;
210 virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
211 virtual void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
212 virtual void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
213 virtual void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
214 virtual void dropEvent(QGraphicsSceneDragDropEvent *event);
215 virtual void focusInEvent(QFocusEvent *event);
216 virtual void focusOutEvent(QFocusEvent *event);
217 virtual void helpEvent(QGraphicsSceneHelpEvent *event);
218 virtual void keyPressEvent(QKeyEvent *event);
219 virtual void keyReleaseEvent(QKeyEvent *event);
220 virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
221 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
222 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
223 virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
224 virtual void wheelEvent(QGraphicsSceneWheelEvent *event);
225 virtual void inputMethodEvent(QInputMethodEvent *event);
226
227 virtual void drawBackground(QPainter *painter, const QRectF &rect);
228 virtual void drawForeground(QPainter *painter, const QRectF &rect);
229 virtual void drawItems(QPainter *painter, int numItems,
230 QGraphicsItem *items[],
231 const QStyleOptionGraphicsItem options[],
232 QWidget *widget = nullptr);
233
234protected Q_SLOTS:
235 virtual bool focusNextPrevChild(bool next);
236
237Q_SIGNALS:
238 void changed(const QList<QRectF> &region);
239 void sceneRectChanged(const QRectF &rect);
240 void selectionChanged();
241 void focusItemChanged(QGraphicsItem *newFocus, QGraphicsItem *oldFocus, Qt::FocusReason reason);
242
243private:
244 Q_DECLARE_PRIVATE(QGraphicsScene)
245 Q_DISABLE_COPY(QGraphicsScene)
246 Q_PRIVATE_SLOT(d_func(), void _q_emitUpdated())
247 Q_PRIVATE_SLOT(d_func(), void _q_polishItems())
248 Q_PRIVATE_SLOT(d_func(), void _q_processDirtyItems())
249 Q_PRIVATE_SLOT(d_func(), void _q_updateScenePosDescendants())
250 friend class QGraphicsItem;
251 friend class QGraphicsItemPrivate;
252 friend class QGraphicsObject;
253 friend class QGraphicsView;
254 friend class QGraphicsViewPrivate;
255 friend class QGraphicsWidget;
256 friend class QGraphicsWidgetPrivate;
257 friend class QGraphicsEffect;
258 friend class QGraphicsSceneIndex;
259 friend class QGraphicsSceneIndexPrivate;
260 friend class QGraphicsSceneBspTreeIndex;
261 friend class QGraphicsSceneBspTreeIndexPrivate;
262 friend class QGraphicsItemEffectSourcePrivate;
263#ifndef QT_NO_GESTURES
264 friend class QGesture;
265#endif
266};
267
268Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsScene::SceneLayers)
269
270QT_END_NAMESPACE
271
272#endif
273

source code of qtbase/src/widgets/graphicsview/qgraphicsscene.h