QtGui/qgraphicsscene.h Source File

qgraphicsscene.h
Go to the documentation of this file.
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 
55 
57 
58 QT_MODULE(Gui)
59 
60 #if !defined(QT_NO_GRAPHICSVIEW) || (QT_EDITION & QT_MODULE_GRAPHICSVIEW) != QT_MODULE_GRAPHICSVIEW
61 
62 template<typename T> class QList;
63 class QFocusEvent;
64 class QFont;
65 class QFontMetrics;
67 class QGraphicsItem;
68 class QGraphicsItemGroup;
69 class QGraphicsLineItem;
70 class QGraphicsPathItem;
74 class QGraphicsRectItem;
83 class QGraphicsTextItem;
84 class QGraphicsView;
85 class QGraphicsWidget;
86 class QGraphicsSceneIndex;
87 class QHelpEvent;
88 class QInputMethodEvent;
89 class QKeyEvent;
90 class QLineF;
91 class QPainterPath;
92 class QPixmap;
93 class QPointF;
94 class QPolygonF;
95 class QRectF;
96 class QSizeF;
97 class QStyle;
99 
100 class QGraphicsScenePrivate;
101 class 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 
114 public:
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 
254 public 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 
261 protected:
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 
288 protected Q_SLOTS:
289  bool focusNextPrevChild(bool next);
290 
291 Q_SIGNALS:
292  void changed(const QList<QRectF> &region);
293  void sceneRectChanged(const QRectF &rect);
294  void selectionChanged();
295 
296 private:
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 
321 Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsScene::SceneLayers)
322 
323 #endif // QT_NO_GRAPHICSVIEW
324 
326 
328 
329 #endif
void setSceneRect(qreal x, qreal y, qreal w, qreal h)
qreal height() const
GLsizei GLsizei GLchar * source
Definition: GLee.h:1721
GLenum GLint GLint y
Definition: GLee.h:876
GLint mode
Definition: GLee.h:4479
virtual bool eventFilter(QObject *, QEvent *)
#define QT_END_NAMESPACE
Definition: qglobal.h:128
#define Q_PROPERTY(text)
Definition: qobjectdefs.h:80
#define QT_BEGIN_HEADER
Definition: qglobal.h:141
QList< QGraphicsItem * > items(qreal x, qreal y, qreal w, qreal h, Qt::ItemSelectionMode mode=Qt::IntersectsItemShape) const
GLXFBConfig Pixmap pixmap
Definition: GLee.h:10237
GLenum GLsizei width
Definition: GLee.h:873
#define Q_PRIVATE_SLOT(d, signature)
Definition: qobjectdefs.h:73
#define Q_SLOTS
Definition: qobjectdefs.h:71
ItemSelectionMode
Definition: qnamespace.h:1503
#define inline
Definition: image.h:2490
#define Q_SIGNALS
Definition: qobjectdefs.h:72
QGraphicsItem * itemAt(qreal x, qreal y, const QTransform &deviceTransform) const
#define QT_BEGIN_NAMESPACE
Definition: qglobal.h:127
Definition: qfont.h:64
GLenum GLsizei GLsizei height
Definition: GLee.h:883
Definition: qsize.h:202
QGraphicsLineItem * addLine(qreal x1, qreal y1, qreal x2, qreal y2, const QPen &pen=QPen())
GLenum GLint x
Definition: GLee.h:876
QGraphicsRectItem * addRect(qreal x, qreal y, qreal w, qreal h, const QPen &pen=QPen(), const QBrush &brush=QBrush())
InputMethodQuery
Definition: qnamespace.h:1541
Definition: qstyle.h:68
#define Q_OBJECT
Definition: qobjectdefs.h:157
FocusReason
Definition: qnamespace.h:1521
QGraphicsItem * itemAt(qreal x, qreal y) const
Definition: qline.h:212
GLint GLint GLsizei GLsizei GLsizei depth
Definition: GLee.h:905
Definition: qrect.h:511
Definition: qpen.h:64
virtual bool event(QEvent *)
int int int int int int h
Definition: GLee.h:10534
AspectRatioMode
Definition: qnamespace.h:1317
Definition: qbrush.h:76
void update(qreal x, qreal y, qreal w, qreal h)
GLubyte GLubyte GLubyte GLubyte w
Definition: GLee.h:1775
#define QT_END_HEADER
Definition: qglobal.h:142
QGraphicsEllipseItem * addEllipse(qreal x, qreal y, qreal w, qreal h, const QPen &pen=QPen(), const QBrush &brush=QBrush())
QList< QGraphicsItem * > items(qreal x, qreal y, qreal w, qreal h, Qt::ItemSelectionMode mode, Qt::SortOrder order, const QTransform &deviceTransform=QTransform()) const
void invalidate(qreal x, qreal y, qreal w, qreal h, SceneLayers layers=AllLayers)
SortOrder
Definition: qnamespace.h:189