Qt 4.8
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;
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 
102 {
103  Q_OBJECT
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 
174  { return items(QRectF(x, y, w, h), mode); } // ### obsolete
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:
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())
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
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
qreal height() const
This convenience function is equivalent to calling sceneRect().
bool sortCacheEnabled
whether sort caching is enabled
The QGraphicsScene class provides a surface for managing a large number of 2D graphical items...
The QKeyEvent class describes a key event.
Definition: qevent.h:224
double qreal
Definition: qglobal.h:1193
The QFontMetrics class provides font metrics information.
Definition: qfontmetrics.h:65
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QGraphicsRectItem class provides a rectangle item that you can add to a QGraphicsScene.
EventRef event
QPointer< QWidget > widget
#define QT_MODULE(x)
Definition: qglobal.h:2783
QGraphicsItem * itemAt(qreal x, qreal y) const
Returns the topmost item at the position specified by (x, y), or 0 if there are no items at this posi...
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
#define Q_DECLARE_FLAGS(Flags, Enum)
The Q_DECLARE_FLAGS() macro expands to.
Definition: qglobal.h:2348
The QPainterPath class provides a container for painting operations, enabling graphical shapes to be ...
Definition: qpainterpath.h:67
int bspTreeDepth
the depth of QGraphicsScene&#39;s BSP index tree
QGraphicsEllipseItem * addEllipse(qreal x, qreal y, qreal w, qreal h, const QPen &pen=QPen(), const QBrush &brush=QBrush())
#define Q_GUI_EXPORT
Definition: qglobal.h:1450
SceneLayer
This enum describes the rendering layers in a QGraphicsScene.
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
Definition: qgraphicsitem.h:89
The QGraphicsSceneMouseEvent class provides mouse events in the graphics view framework.
void invalidate(qreal x, qreal y, qreal w, qreal h, SceneLayers layers=AllLayers)
This is an overloaded member function, provided for convenience. It differs from the above function o...
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
QPalette palette
the scene&#39;s default palette
static void clear(QVariant::Private *d)
Definition: qvariant.cpp:197
The QGraphicsEllipseItem class provides an ellipse item that you can add to a QGraphicsScene.
The QGraphicsSceneBspTreeIndex class provides an implementation of a BSP indexing algorithm for disco...
bool stickyFocus
whether clicking into the scene background will clear focus
#define Q_DISABLE_COPY(Class)
Disables the use of copy constructors and assignment operators for the given Class.
Definition: qglobal.h:2523
#define Q_PRIVATE_SLOT(d, signature)
Definition: qobjectdefs.h:73
ItemSelectionMode
Definition: qnamespace.h:1503
#define Q_SLOTS
Definition: qobjectdefs.h:71
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QGraphicsTextItem class provides a text item that you can add to a QGraphicsScene to display form...
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
virtual bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
Definition: qobject.cpp:1200
The QGraphicsPolygonItem class provides a polygon item that you can add to a QGraphicsScene.
#define Q_SIGNALS
Definition: qobjectdefs.h:72
The QPen class defines how a QPainter should draw lines and outlines of shapes.
Definition: qpen.h:64
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
The QLineF class provides a two-dimensional vector using floating point precision.
Definition: qline.h:212
SortOrder
Definition: qnamespace.h:189
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
The Q_DECLARE_OPERATORS_FOR_FLAGS() macro declares global operator|() functions for Flags...
Definition: qglobal.h:2355
QList< QGraphicsItem * > items(qreal x, qreal y, qreal w, qreal h, Qt::ItemSelectionMode mode=Qt::IntersectsItemShape) const
qreal height() const
Returns the height of the rectangle.
Definition: qrect.h:710
The QPolygonF class provides a vector of points using floating point precision.
Definition: qpolygon.h:134
qreal width() const
Returns the width of the rectangle.
Definition: qrect.h:707
QRectF sceneRect
the scene rectangle; the bounding rectangle of the scene
The QGraphicsSceneEvent class provides a base class for all graphics view related events...
The QGraphicsSceneIndex class provides a base class to implement a custom indexing algorithm for disc...
#define Q_OBJECT
Definition: qobjectdefs.h:157
QList< QGraphicsItem * > items(qreal x, qreal y, qreal w, qreal h, Qt::ItemSelectionMode mode, Qt::SortOrder order, const QTransform &deviceTransform=QTransform()) const
Returns all visible items that, depending on mode, are either inside or intersect with the rectangle ...
virtual bool eventFilter(QObject *, QEvent *)
Filters events if this object has been installed as an event filter for the watched object...
Definition: qobject.cpp:1375
The QGraphicsProxyWidget class provides a proxy layer for embedding a QWidget in a QGraphicsScene...
void update(qreal x, qreal y, qreal w, qreal h)
This is an overloaded member function, provided for convenience. It differs from the above function o...
InputMethodQuery
Definition: qnamespace.h:1541
The QBrush class defines the fill pattern of shapes drawn by QPainter.
Definition: qbrush.h:76
The QInputMethodEvent class provides parameters for input method events.
Definition: qevent.h:431
The QGraphicsSimpleTextItem class provides a simple text path item that you can add to a QGraphicsSce...
The QGraphicsLineItem class provides a line item that you can add to a QGraphicsScene.
The QGraphicsSceneHelpEvent class provides events when a tooltip is requested.
The QGraphicsSceneDragDropEvent class provides events for drag and drop in the graphics view framewor...
FocusReason
Definition: qnamespace.h:1521
QBrush foregroundBrush
the foreground brush of the scene.
The QFont class specifies a font used for drawing text.
Definition: qfont.h:64
The QGesture class represents a gesture, containing properties that describe the corresponding user i...
Definition: qgesture.h:64
The QStyle class is an abstract base class that encapsulates the look and feel of a GUI...
Definition: qstyle.h:68
ItemIndexMethod itemIndexMethod
the item indexing method.
ItemIndexMethod
This enum describes the indexing algorithms QGraphicsScene provides for managing positional informati...
The QGraphicsItemGroup class provides a container that treats a group of items as a single item...
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
Definition: qgraphicsview.h:64
void setSceneRect(qreal x, qreal y, qreal w, qreal h)
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
QGraphicsItem * itemAt(qreal x, qreal y, const QTransform &deviceTransform) const
Returns the topmost item at the position specified by (x, y), or 0 if there are no items at this posi...
The QGraphicsSceneWheelEvent class provides wheel events in the graphics view framework.
QFont font
the scene&#39;s default font
QBrush backgroundBrush
the background brush of the scene.
#define class
QGraphicsLineItem * addLine(qreal x1, qreal y1, qreal x2, qreal y2, const QPen &pen=QPen())
The QGraphicsObject class provides a base class for all graphics items that require signals...
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
#define QT_END_HEADER
Definition: qglobal.h:137
The QGraphicsPixmapItem class provides a pixmap item that you can add to a QGraphicsScene.
The QStyleOptionGraphicsItem class is used to describe the parameters needed to draw a QGraphicsItem...
Definition: qstyleoption.h:867
qreal width() const
This convenience function is equivalent to calling sceneRect().
#define enabled
The QFocusEvent class contains event parameters for widget focus events.
Definition: qevent.h:275
#define text
Definition: qobjectdefs.h:80
The QGraphicsEffect class is the base class for all graphics effects.
The QTransform class specifies 2D transformations of a coordinate system.
Definition: qtransform.h:65
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
The QGraphicsPathItem class provides a path item that you can add to a QGraphicsScene.
The QGraphicsSceneHoverEvent class provides hover events in the graphics view framework.
QGraphicsRectItem * addRect(qreal x, qreal y, qreal w, qreal h, const QPen &pen=QPen(), const QBrush &brush=QBrush())
The QHelpEvent class provides an event that is used to request helpful information about a particular...
Definition: qevent.h:586
The QGraphicsWidget class is the base class for all widget items in a QGraphicsScene.
AspectRatioMode
Definition: qnamespace.h:1317
The QGraphicsSceneContextMenuEvent class provides context menu events in the graphics view framework...
The QPalette class contains color groups for each widget state.
Definition: qpalette.h:61