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