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