1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtWidgets module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QGRAPHICSSCENE_H
41#define QGRAPHICSSCENE_H
42
43#include <QtWidgets/qtwidgetsglobal.h>
44#include <QtCore/qobject.h>
45#include <QtCore/qpoint.h>
46#include <QtCore/qrect.h>
47#include <QtGui/qbrush.h>
48#include <QtGui/qfont.h>
49#include <QtGui/qtransform.h>
50#include <QtGui/qmatrix.h>
51#include <QtGui/qpen.h>
52
53QT_REQUIRE_CONFIG(graphicsview);
54
55QT_BEGIN_NAMESPACE
56
57template<typename T> class QList;
58class QFocusEvent;
59class QFont;
60class QFontMetrics;
61class QGraphicsEllipseItem;
62class QGraphicsItem;
63class QGraphicsItemGroup;
64class QGraphicsLineItem;
65class QGraphicsPathItem;
66class QGraphicsPixmapItem;
67class QGraphicsPolygonItem;
68class QGraphicsProxyWidget;
69class QGraphicsRectItem;
70class QGraphicsSceneContextMenuEvent;
71class QGraphicsSceneDragDropEvent;
72class QGraphicsSceneEvent;
73class QGraphicsSceneHelpEvent;
74class QGraphicsSceneHoverEvent;
75class QGraphicsSceneMouseEvent;
76class QGraphicsSceneWheelEvent;
77class QGraphicsSimpleTextItem;
78class QGraphicsTextItem;
79class QGraphicsView;
80class QGraphicsWidget;
81class QGraphicsSceneIndex;
82class QHelpEvent;
83class QInputMethodEvent;
84class QKeyEvent;
85class QLineF;
86class QPainterPath;
87class QPixmap;
88class QPointF;
89class QPolygonF;
90class QRectF;
91class QSizeF;
92class QStyle;
93class QStyleOptionGraphicsItem;
94
95class QGraphicsScenePrivate;
96class Q_WIDGETS_EXPORT QGraphicsScene : public QObject
97{
98 Q_OBJECT
99 Q_PROPERTY(QBrush backgroundBrush READ backgroundBrush WRITE setBackgroundBrush)
100 Q_PROPERTY(QBrush foregroundBrush READ foregroundBrush WRITE setForegroundBrush)
101 Q_PROPERTY(ItemIndexMethod itemIndexMethod READ itemIndexMethod WRITE setItemIndexMethod)
102 Q_PROPERTY(QRectF sceneRect READ sceneRect WRITE setSceneRect)
103 Q_PROPERTY(int bspTreeDepth READ bspTreeDepth WRITE setBspTreeDepth)
104 Q_PROPERTY(QPalette palette READ palette WRITE setPalette)
105 Q_PROPERTY(QFont font READ font WRITE setFont)
106 Q_PROPERTY(bool sortCacheEnabled READ isSortCacheEnabled WRITE setSortCacheEnabled)
107 Q_PROPERTY(bool stickyFocus READ stickyFocus WRITE setStickyFocus)
108 Q_PROPERTY(qreal minimumRenderSize READ minimumRenderSize WRITE setMinimumRenderSize)
109
110public:
111 enum ItemIndexMethod {
112 BspTreeIndex,
113 NoIndex = -1
114 };
115
116 enum SceneLayer {
117 ItemLayer = 0x1,
118 BackgroundLayer = 0x2,
119 ForegroundLayer = 0x4,
120 AllLayers = 0xffff
121 };
122 Q_DECLARE_FLAGS(SceneLayers, SceneLayer)
123
124 QGraphicsScene(QObject *parent = nullptr);
125 QGraphicsScene(const QRectF &sceneRect, QObject *parent = nullptr);
126 QGraphicsScene(qreal x, qreal y, qreal width, qreal height, QObject *parent = nullptr);
127 virtual ~QGraphicsScene();
128
129 QRectF sceneRect() const;
130 inline qreal width() const { return sceneRect().width(); }
131 inline qreal height() const { return sceneRect().height(); }
132 void setSceneRect(const QRectF &rect);
133 inline void setSceneRect(qreal x, qreal y, qreal w, qreal h)
134 { setSceneRect(QRectF(x, y, w, h)); }
135
136 void render(QPainter *painter,
137 const QRectF &target = QRectF(), const QRectF &source = QRectF(),
138 Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio);
139
140 ItemIndexMethod itemIndexMethod() const;
141 void setItemIndexMethod(ItemIndexMethod method);
142
143 bool isSortCacheEnabled() const;
144 void setSortCacheEnabled(bool enabled);
145
146 int bspTreeDepth() const;
147 void setBspTreeDepth(int depth);
148
149 QRectF itemsBoundingRect() const;
150
151 QList<QGraphicsItem *> items(Qt::SortOrder order = Qt::DescendingOrder) const;
152
153 QList<QGraphicsItem *> items(const QPointF &pos, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, Qt::SortOrder order = Qt::DescendingOrder, const QTransform &deviceTransform = QTransform()) const;
154 QList<QGraphicsItem *> items(const QRectF &rect, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, Qt::SortOrder order = Qt::DescendingOrder, const QTransform &deviceTransform = QTransform()) const;
155 QList<QGraphicsItem *> items(const QPolygonF &polygon, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, Qt::SortOrder order = Qt::DescendingOrder, const QTransform &deviceTransform = QTransform()) const;
156 QList<QGraphicsItem *> items(const QPainterPath &path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, Qt::SortOrder order = Qt::DescendingOrder, const QTransform &deviceTransform = QTransform()) const;
157
158 QList<QGraphicsItem *> collidingItems(const QGraphicsItem *item, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
159#if QT_DEPRECATED_SINCE(5, 0)
160 QT_DEPRECATED inline QGraphicsItem *itemAt(const QPointF &position) const {
161 QList<QGraphicsItem *> itemsAtPoint = items(position);
162 return itemsAtPoint.isEmpty() ? nullptr : itemsAtPoint.first();
163 }
164#endif
165 QGraphicsItem *itemAt(const QPointF &pos, const QTransform &deviceTransform) const;
166#if QT_DEPRECATED_SINCE(5, 0)
167 QT_DEPRECATED inline QList<QGraphicsItem *> items(qreal x, qreal y, qreal w, qreal h, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const
168 { return items(QRectF(x, y, w, h), mode); }
169#endif
170 inline QList<QGraphicsItem *> items(qreal x, qreal y, qreal w, qreal h, Qt::ItemSelectionMode mode, Qt::SortOrder order,
171 const QTransform &deviceTransform = QTransform()) const
172 { return items(QRectF(x, y, w, h), mode, order, deviceTransform); }
173#if QT_DEPRECATED_SINCE(5, 0)
174 QT_DEPRECATED inline QGraphicsItem *itemAt(qreal x, qreal y) const {
175 QList<QGraphicsItem *> itemsAtPoint = items(QPointF(x, y));
176 return itemsAtPoint.isEmpty() ? nullptr : itemsAtPoint.first();
177 }
178#endif
179 inline QGraphicsItem *itemAt(qreal x, qreal y, const QTransform &deviceTransform) const
180 { return itemAt(QPointF(x, y), deviceTransform); }
181
182 QList<QGraphicsItem *> selectedItems() const;
183 QPainterPath selectionArea() const;
184 void setSelectionArea(const QPainterPath &path, const QTransform &deviceTransform);
185 void setSelectionArea(const QPainterPath &path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, const QTransform &deviceTransform = QTransform());
186 void setSelectionArea(const QPainterPath &path, Qt::ItemSelectionOperation selectionOperation, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, const QTransform &deviceTransform = QTransform());
187 // ### Qt6 merge the last 2 functions and add a default: Qt::ItemSelectionOperation selectionOperation = Qt::ReplaceSelection
188
189 QGraphicsItemGroup *createItemGroup(const QList<QGraphicsItem *> &items);
190 void destroyItemGroup(QGraphicsItemGroup *group);
191
192 void addItem(QGraphicsItem *item);
193 QGraphicsEllipseItem *addEllipse(const QRectF &rect, const QPen &pen = QPen(), const QBrush &brush = QBrush());
194 QGraphicsLineItem *addLine(const QLineF &line, const QPen &pen = QPen());
195 QGraphicsPathItem *addPath(const QPainterPath &path, const QPen &pen = QPen(), const QBrush &brush = QBrush());
196 QGraphicsPixmapItem *addPixmap(const QPixmap &pixmap);
197 QGraphicsPolygonItem *addPolygon(const QPolygonF &polygon, const QPen &pen = QPen(), const QBrush &brush = QBrush());
198 QGraphicsRectItem *addRect(const QRectF &rect, const QPen &pen = QPen(), const QBrush &brush = QBrush());
199 QGraphicsTextItem *addText(const QString &text, const QFont &font = QFont());
200 QGraphicsSimpleTextItem *addSimpleText(const QString &text, const QFont &font = QFont());
201 QGraphicsProxyWidget *addWidget(QWidget *widget, Qt::WindowFlags wFlags = Qt::WindowFlags());
202 inline QGraphicsEllipseItem *addEllipse(qreal x, qreal y, qreal w, qreal h, const QPen &pen = QPen(), const QBrush &brush = QBrush())
203 { return addEllipse(QRectF(x, y, w, h), pen, brush); }
204 inline QGraphicsLineItem *addLine(qreal x1, qreal y1, qreal x2, qreal y2, const QPen &pen = QPen())
205 { return addLine(QLineF(x1, y1, x2, y2), pen); }
206 inline QGraphicsRectItem *addRect(qreal x, qreal y, qreal w, qreal h, const QPen &pen = QPen(), const QBrush &brush = QBrush())
207 { return addRect(QRectF(x, y, w, h), pen, brush); }
208 void removeItem(QGraphicsItem *item);
209
210 QGraphicsItem *focusItem() const;
211 void setFocusItem(QGraphicsItem *item, Qt::FocusReason focusReason = Qt::OtherFocusReason);
212 bool hasFocus() const;
213 void setFocus(Qt::FocusReason focusReason = Qt::OtherFocusReason);
214 void clearFocus();
215
216 void setStickyFocus(bool enabled);
217 bool stickyFocus() const;
218
219 QGraphicsItem *mouseGrabberItem() const;
220
221 QBrush backgroundBrush() const;
222 void setBackgroundBrush(const QBrush &brush);
223
224 QBrush foregroundBrush() const;
225 void setForegroundBrush(const QBrush &brush);
226
227 virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
228
229 QList <QGraphicsView *> views() const;
230
231 inline void update(qreal x, qreal y, qreal w, qreal h)
232 { update(QRectF(x, y, w, h)); }
233 inline void invalidate(qreal x, qreal y, qreal w, qreal h, SceneLayers layers = AllLayers)
234 { invalidate(QRectF(x, y, w, h), layers); }
235
236 QStyle *style() const;
237 void setStyle(QStyle *style);
238
239 QFont font() const;
240 void setFont(const QFont &font);
241
242 QPalette palette() const;
243 void setPalette(const QPalette &palette);
244
245 bool isActive() const;
246 QGraphicsItem *activePanel() const;
247 void setActivePanel(QGraphicsItem *item);
248 QGraphicsWidget *activeWindow() const;
249 void setActiveWindow(QGraphicsWidget *widget);
250
251 bool sendEvent(QGraphicsItem *item, QEvent *event);
252
253 qreal minimumRenderSize() const;
254 void setMinimumRenderSize(qreal minSize);
255
256public Q_SLOTS:
257 void update(const QRectF &rect = QRectF());
258 void invalidate(const QRectF &rect = QRectF(), SceneLayers layers = AllLayers);
259 void advance();
260 void clearSelection();
261 void clear();
262
263protected:
264 bool event(QEvent *event) override;
265 bool eventFilter(QObject *watched, QEvent *event) override;
266 virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
267 virtual void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
268 virtual void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
269 virtual void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
270 virtual void dropEvent(QGraphicsSceneDragDropEvent *event);
271 virtual void focusInEvent(QFocusEvent *event);
272 virtual void focusOutEvent(QFocusEvent *event);
273 virtual void helpEvent(QGraphicsSceneHelpEvent *event);
274 virtual void keyPressEvent(QKeyEvent *event);
275 virtual void keyReleaseEvent(QKeyEvent *event);
276 virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
277 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
278 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
279 virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
280 virtual void wheelEvent(QGraphicsSceneWheelEvent *event);
281 virtual void inputMethodEvent(QInputMethodEvent *event);
282
283 virtual void drawBackground(QPainter *painter, const QRectF &rect);
284 virtual void drawForeground(QPainter *painter, const QRectF &rect);
285 virtual void drawItems(QPainter *painter, int numItems,
286 QGraphicsItem *items[],
287 const QStyleOptionGraphicsItem options[],
288 QWidget *widget = nullptr);
289
290protected Q_SLOTS:
291 QT6_VIRTUAL bool focusNextPrevChild(bool next);
292
293Q_SIGNALS:
294 void changed(const QList<QRectF> &region);
295 void sceneRectChanged(const QRectF &rect);
296 void selectionChanged();
297 void focusItemChanged(QGraphicsItem *newFocus, QGraphicsItem *oldFocus, Qt::FocusReason reason);
298
299private:
300 Q_DECLARE_PRIVATE(QGraphicsScene)
301 Q_DISABLE_COPY(QGraphicsScene)
302 Q_PRIVATE_SLOT(d_func(), void _q_emitUpdated())
303 Q_PRIVATE_SLOT(d_func(), void _q_polishItems())
304 Q_PRIVATE_SLOT(d_func(), void _q_processDirtyItems())
305 Q_PRIVATE_SLOT(d_func(), void _q_updateScenePosDescendants())
306 friend class QGraphicsItem;
307 friend class QGraphicsItemPrivate;
308 friend class QGraphicsObject;
309 friend class QGraphicsView;
310 friend class QGraphicsViewPrivate;
311 friend class QGraphicsWidget;
312 friend class QGraphicsWidgetPrivate;
313 friend class QGraphicsEffect;
314 friend class QGraphicsSceneIndex;
315 friend class QGraphicsSceneIndexPrivate;
316 friend class QGraphicsSceneBspTreeIndex;
317 friend class QGraphicsSceneBspTreeIndexPrivate;
318 friend class QGraphicsItemEffectSourcePrivate;
319#ifndef QT_NO_GESTURES
320 friend class QGesture;
321#endif
322};
323
324Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsScene::SceneLayers)
325
326QT_END_NAMESPACE
327
328#endif
329