Qt 4.8
qgraphicsview.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 QGRAPHICSVIEW_H
43 #define QGRAPHICSVIEW_H
44 
45 #include <QtCore/qmetatype.h>
46 #include <QtGui/qpainter.h>
47 #include <QtGui/qscrollarea.h>
48 #include <QtGui/qgraphicsscene.h>
49 
51 
53 
54 QT_MODULE(Gui)
55 
56 #if !defined(QT_NO_GRAPHICSVIEW) || (QT_EDITION & QT_MODULE_GRAPHICSVIEW) != QT_MODULE_GRAPHICSVIEW
57 
58 class QGraphicsItem;
59 class QPainterPath;
60 class QPolygonF;
62 
65 {
66  Q_OBJECT
67  Q_FLAGS(QPainter::RenderHints CacheMode OptimizationFlags)
69  QBrush backgroundBrush;
70  QBrush foregroundBrush;
71  bool interactive;
72  QRectF sceneRect;
73  Qt::Alignment alignment;
74  QPainter::RenderHints renderHints;
75  DragMode dragMode;
76  CacheMode cacheMode;
77  ViewportAnchor transformationAnchor;
78  ViewportAnchor resizeAnchor;
79  ViewportUpdateMode viewportUpdateMode;
80 #ifndef QT_NO_RUBBERBAND
82 #endif
83  OptimizationFlags optimizationFlags;
84 
85 public:
89  AnchorUnderMouse
90  };
91 
93  CacheNone = 0x0,
94  CacheBackground = 0x1
95  };
96  Q_DECLARE_FLAGS(CacheMode, CacheModeFlag)
97 
98  enum DragMode {
101  RubberBandDrag
102  };
103 
104  enum ViewportUpdateMode {
109  BoundingRectViewportUpdate
110  };
111 
113  DontClipPainter = 0x1, // obsolete
114  DontSavePainterState = 0x2,
115  DontAdjustForAntialiasing = 0x4,
116  IndirectPainting = 0x8
117  };
118  Q_DECLARE_FLAGS(OptimizationFlags, OptimizationFlag)
119 
120  QGraphicsView(QWidget *parent = 0);
121  QGraphicsView(QGraphicsScene *scene, QWidget *parent = 0);
122  ~QGraphicsView();
123 
124  QSize sizeHint() const;
125 
126  QPainter::RenderHints renderHints() const;
127  void setRenderHint(QPainter::RenderHint hint, bool enabled = true);
128  void setRenderHints(QPainter::RenderHints hints);
129 
130  Qt::Alignment alignment() const;
131  void setAlignment(Qt::Alignment alignment);
132 
133  ViewportAnchor transformationAnchor() const;
134  void setTransformationAnchor(ViewportAnchor anchor);
135 
136  ViewportAnchor resizeAnchor() const;
137  void setResizeAnchor(ViewportAnchor anchor);
138 
139  ViewportUpdateMode viewportUpdateMode() const;
140  void setViewportUpdateMode(ViewportUpdateMode mode);
141 
142  OptimizationFlags optimizationFlags() const;
143  void setOptimizationFlag(OptimizationFlag flag, bool enabled = true);
144  void setOptimizationFlags(OptimizationFlags flags);
145 
146  DragMode dragMode() const;
147  void setDragMode(DragMode mode);
148 
149 #ifndef QT_NO_RUBBERBAND
150  Qt::ItemSelectionMode rubberBandSelectionMode() const;
151  void setRubberBandSelectionMode(Qt::ItemSelectionMode mode);
152 #endif
153 
154  CacheMode cacheMode() const;
155  void setCacheMode(CacheMode mode);
156  void resetCachedContent();
157 
158  bool isInteractive() const;
159  void setInteractive(bool allowed);
160 
161  QGraphicsScene *scene() const;
162  void setScene(QGraphicsScene *scene);
163 
164  QRectF sceneRect() const;
165  void setSceneRect(const QRectF &rect);
166  inline void setSceneRect(qreal x, qreal y, qreal w, qreal h);
167 
168  QMatrix matrix() const;
169  void setMatrix(const QMatrix &matrix, bool combine = false);
170  void resetMatrix();
171  QTransform transform() const;
172  QTransform viewportTransform() const;
173  bool isTransformed() const;
174  void setTransform(const QTransform &matrix, bool combine = false);
175  void resetTransform();
176  void rotate(qreal angle);
177  void scale(qreal sx, qreal sy);
178  void shear(qreal sh, qreal sv);
179  void translate(qreal dx, qreal dy);
180 
181  void centerOn(const QPointF &pos);
182  inline void centerOn(qreal x, qreal y);
183  void centerOn(const QGraphicsItem *item);
184  void ensureVisible(const QRectF &rect, int xmargin = 50, int ymargin = 50);
185  inline void ensureVisible(qreal x, qreal y, qreal w, qreal h, int xmargin = 50, int ymargin = 50);
186  void ensureVisible(const QGraphicsItem *item, int xmargin = 50, int ymargin = 50);
187  void fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRadioMode = Qt::IgnoreAspectRatio);
188  inline void fitInView(qreal x, qreal y, qreal w, qreal h,
189  Qt::AspectRatioMode aspectRadioMode = Qt::IgnoreAspectRatio);
190  void fitInView(const QGraphicsItem *item,
191  Qt::AspectRatioMode aspectRadioMode = Qt::IgnoreAspectRatio);
192 
193  void render(QPainter *painter, const QRectF &target = QRectF(), const QRect &source = QRect(),
194  Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio);
195 
196  QList<QGraphicsItem *> items() const;
197  QList<QGraphicsItem *> items(const QPoint &pos) const;
198  inline QList<QGraphicsItem *> items(int x, int y) const;
200  inline QList<QGraphicsItem *> items(int x, int y, int w, int h, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
203  QGraphicsItem *itemAt(const QPoint &pos) const;
204  inline QGraphicsItem *itemAt(int x, int y) const;
205 
206  QPointF mapToScene(const QPoint &point) const;
207  QPolygonF mapToScene(const QRect &rect) const;
208  QPolygonF mapToScene(const QPolygon &polygon) const;
209  QPainterPath mapToScene(const QPainterPath &path) const;
210  QPoint mapFromScene(const QPointF &point) const;
211  QPolygon mapFromScene(const QRectF &rect) const;
212  QPolygon mapFromScene(const QPolygonF &polygon) const;
213  QPainterPath mapFromScene(const QPainterPath &path) const;
214  inline QPointF mapToScene(int x, int y) const;
215  inline QPolygonF mapToScene(int x, int y, int w, int h) const;
216  inline QPoint mapFromScene(qreal x, qreal y) const;
217  inline QPolygon mapFromScene(qreal x, qreal y, qreal w, qreal h) const;
218 
220 
221  QBrush backgroundBrush() const;
222  void setBackgroundBrush(const QBrush &brush);
223 
224  QBrush foregroundBrush() const;
225  void setForegroundBrush(const QBrush &brush);
226 
227 public Q_SLOTS:
228  void updateScene(const QList<QRectF> &rects);
229  void invalidateScene(const QRectF &rect = QRectF(), QGraphicsScene::SceneLayers layers = QGraphicsScene::AllLayers);
230  void updateSceneRect(const QRectF &rect);
231 
232 protected Q_SLOTS:
234 
235 protected:
237  bool event(QEvent *event);
238  bool viewportEvent(QEvent *event);
239 
240 #ifndef QT_NO_CONTEXTMENU
242 #endif
246  void dropEvent(QDropEvent *event);
248  bool focusNextPrevChild(bool next);
256 #ifndef QT_NO_WHEELEVENT
258 #endif
261  void scrollContentsBy(int dx, int dy);
262  void showEvent(QShowEvent *event);
264 
265  virtual void drawBackground(QPainter *painter, const QRectF &rect);
266  virtual void drawForeground(QPainter *painter, const QRectF &rect);
267  virtual void drawItems(QPainter *painter, int numItems,
268  QGraphicsItem *items[],
269  const QStyleOptionGraphicsItem options[]);
270 
271 private:
274 #ifndef QT_NO_CURSOR
275  Q_PRIVATE_SLOT(d_func(), void _q_setViewportCursor(const QCursor &))
276  Q_PRIVATE_SLOT(d_func(), void _q_unsetViewportCursor())
277 #endif
278  friend class QGraphicsSceneWidget;
279  friend class QGraphicsScene;
280  friend class QGraphicsScenePrivate;
281  friend class QGraphicsItemPrivate;
282 };
283 
284 Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsView::CacheMode)
285 Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsView::OptimizationFlags)
286 
287 inline void QGraphicsView::setSceneRect(qreal ax, qreal ay, qreal aw, qreal ah)
288 { setSceneRect(QRectF(ax, ay, aw, ah)); }
290 { centerOn(QPointF(ax, ay)); }
291 inline void QGraphicsView::ensureVisible(qreal ax, qreal ay, qreal aw, qreal ah, int xmargin, int ymargin)
292 { ensureVisible(QRectF(ax, ay, aw, ah), xmargin, ymargin); }
294 { fitInView(QRectF(ax, ay, w, h), mode); }
295 inline QList<QGraphicsItem *> QGraphicsView::items(int ax, int ay) const
296 { return items(QPoint(ax, ay)); }
297 inline QList<QGraphicsItem *> QGraphicsView::items(int ax, int ay, int w, int h, Qt::ItemSelectionMode mode) const
298 { return items(QRect(ax, ay, w, h), mode); }
299 inline QGraphicsItem *QGraphicsView::itemAt(int ax, int ay) const
300 { return itemAt(QPoint(ax, ay)); }
301 inline QPointF QGraphicsView::mapToScene(int ax, int ay) const
302 { return mapToScene(QPoint(ax, ay)); }
303 inline QPolygonF QGraphicsView::mapToScene(int ax, int ay, int w, int h) const
304 { return mapToScene(QRect(ax, ay, w, h)); }
306 { return mapFromScene(QPointF(ax, ay)); }
308 { return mapFromScene(QRectF(ax, ay, w, h)); }
309 
310 #endif // QT_NO_GRAPHICSVIEW
311 
313 
315 
316 #endif // QGRAPHICSVIEW_H
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
The QGraphicsScene class provides a surface for managing a large number of 2D graphical items...
DragMode
This enum describes the default action for the view when pressing and dragging the mouse over the vie...
Definition: qgraphicsview.h:98
The QKeyEvent class describes a key event.
Definition: qevent.h:224
virtual void showEvent(QShowEvent *)
This event handler can be reimplemented in a subclass to receive widget show events which are passed ...
Definition: qwidget.cpp:9842
double qreal
Definition: qglobal.h:1193
The QCursor class provides a mouse cursor with an arbitrary shape.
Definition: qcursor.h:89
QPointF mapToScene(const QPoint &point) const
Returns the viewport coordinate point mapped to scene coordinates.
OptimizationFlag
This enum describes flags that you can enable to improve rendering performance in QGraphicsView...
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
EventRef event
QPointer< QWidget > widget
virtual void inputMethodEvent(QInputMethodEvent *)
This event handler, for event event, can be reimplemented in a subclass to receive Input Method compo...
Definition: qwidget.cpp:9668
#define QT_MODULE(x)
Definition: qglobal.h:2783
void dragMoveEvent(QDragMoveEvent *)
This event handler can be reimplemented in a subclass to receive drag move events (passed in event)...
#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 QMatrix class specifies 2D transformations of a coordinate system.
Definition: qmatrix.h:61
The QPainterPath class provides a container for painting operations, enabling graphical shapes to be ...
Definition: qpainterpath.h:67
The QWheelEvent class contains parameters that describe a wheel event.
Definition: qevent.h:139
The QContextMenuEvent class contains parameters that describe a context menu event.
Definition: qevent.h:396
void mouseReleaseEvent(QMouseEvent *)
This event handler can be reimplemented in a subclass to receive mouse release events for the viewpor...
#define Q_GUI_EXPORT
Definition: qglobal.h:1450
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 QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
void dragEnterEvent(QDragEnterEvent *)
This event handler can be reimplemented in a subclass to receive drag enter events (passed in event)...
Qt::ItemSelectionMode rubberBandSelectionMode
the behavior for selecting items with a rubber band selection rectangle.
Definition: qgraphicsview.h:81
#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
virtual void keyReleaseEvent(QKeyEvent *)
This event handler, for event event, can be reimplemented in a subclass to receive key release events...
Definition: qwidget.cpp:9407
ItemSelectionMode
Definition: qnamespace.h:1503
The QAbstractScrollArea widget provides a scrolling area with on-demand scroll bars.
The QPolygon class provides a vector of points using integer precision.
Definition: qpolygon.h:60
#define Q_SLOTS
Definition: qobjectdefs.h:71
virtual QVariant inputMethodQuery(Qt::InputMethodQuery) const
This method is only relevant for input widgets.
Definition: qwidget.cpp:9683
The QDragMoveEvent class provides an event which is sent while a drag and drop action is in progress...
Definition: qevent.h:530
void setupViewport(QWidget *viewport)
This slot is called by QAbstractScrollArea after setViewport(viewport) has been called.
virtual void focusOutEvent(QFocusEvent *)
This event handler can be reimplemented in a subclass to receive keyboard focus events (focus lost) f...
Definition: qwidget.cpp:9457
void fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRadioMode=Qt::IgnoreAspectRatio)
Scales the view matrix and scrolls the scroll bars to ensure that the scene rectangle rect fits insid...
#define Q_ENUMS(x)
Definition: qobjectdefs.h:84
void ensureVisible(const QRectF &rect, int xmargin=50, int ymargin=50)
Scrolls the contents of the viewport so that the scene rectangle rect is visible, with margins specif...
void wheelEvent(QWheelEvent *)
This event handler can be reimplemented in a subclass to receive wheel events for the viewport() widg...
void resizeEvent(QResizeEvent *)
This event handler can be reimplemented in a subclass to receive resize events (passed in event)...
#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
void contextMenuEvent(QContextMenuEvent *)
This event handler can be reimplemented in a subclass to receive context menu events for the viewport...
void paintEvent(QPaintEvent *)
This event handler can be reimplemented in a subclass to receive paint events (passed in event)...
void mousePressEvent(QMouseEvent *)
This event handler can be reimplemented in a subclass to receive mouse press events for the viewport(...
The QPolygonF class provides a vector of points using floating point precision.
Definition: qpolygon.h:134
The QResizeEvent class contains event parameters for resize events.
Definition: qevent.h:349
QGraphicsItem * itemAt(const QPoint &pos) const
Returns the item at position pos, which is in viewport coordinates.
RenderHint
Renderhints are used to specify flags to QPainter that may or may not be respected by any given engin...
Definition: qpainter.h:93
The QDragLeaveEvent class provides an event that is sent to a widget when a drag and drop action leav...
Definition: qevent.h:577
CacheModeFlag
This enum describes the flags that you can set for a QGraphicsView&#39;s cache mode.
Definition: qgraphicsview.h:92
The QShowEvent class provides an event that is sent when a widget is shown.
Definition: qevent.h:380
#define Q_OBJECT
Definition: qobjectdefs.h:157
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:85
virtual bool viewportEvent(QEvent *)
The main event handler for the scrolling area (the viewport() widget).
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
QPoint mapFromScene(const QPointF &point) const
Returns the scene coordinate point to viewport coordinates.
OptimizationFlags optimizationFlags
flags that can be used to tune QGraphicsView&#39;s performance.
Definition: qgraphicsview.h:83
qreal angle(const QPointF &p1, const QPointF &p2)
The QDropEvent class provides an event which is sent when a drag and drop action is completed...
Definition: qevent.h:476
The QDragEnterEvent class provides an event which is sent to a widget when a drag and drop action ent...
Definition: qevent.h:555
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
virtual bool focusNextPrevChild(bool next)
Finds a new widget to give the keyboard focus to, as appropriate for Tab and Shift+Tab, and returns true if it can find a new widget, or false if it can&#39;t.
Definition: qwidget.cpp:6836
void dropEvent(QDropEvent *)
This event handler can be reimplemented in a subclass to receive drop events (passed in event)...
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
Definition: qnamespace.h:54
void dragLeaveEvent(QDragLeaveEvent *)
This event handler can be reimplemented in a subclass to receive drag leave events (passed in event)...
virtual void scrollContentsBy(int dx, int dy)
This virtual handler is called when the scroll bars are moved by dx, dy, and consequently the viewpor...
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
Definition: qgraphicsview.h:64
bool event(QEvent *)
Reimplemented Function
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
void keyPressEvent(QKeyEvent *)
This function is called with key event e when key presses occur.
#define Q_FLAGS(x)
Definition: qobjectdefs.h:85
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
QList< QGraphicsItem * > items() const
Returns a list of all the items in the associated scene, in descending stacking order (i...
void centerOn(const QPointF &pos)
Scrolls the contents of the viewport to ensure that the scene coordinate pos, is centered in the view...
The QPaintEvent class contains event parameters for paint events.
Definition: qevent.h:298
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
void render(QPaintDevice *target, const QPoint &targetOffset=QPoint(), const QRegion &sourceRegion=QRegion(), RenderFlags renderFlags=RenderFlags(DrawWindowBackground|DrawChildren))
Renders the sourceRegion of this widget into the target using renderFlags to determine how to render...
Definition: qwidget.cpp:5372
#define QT_END_HEADER
Definition: qglobal.h:137
The QStyleOptionGraphicsItem class is used to describe the parameters needed to draw a QGraphicsItem...
Definition: qstyleoption.h:867
QSize sizeHint() const
Reimplemented Function
virtual void focusInEvent(QFocusEvent *)
This event handler can be reimplemented in a subclass to receive keyboard focus events (focus receive...
Definition: qwidget.cpp:9431
void mouseDoubleClickEvent(QMouseEvent *)
This event handler can be reimplemented in a subclass to receive mouse double click events for the vi...
#define enabled
The QFocusEvent class contains event parameters for widget focus events.
Definition: qevent.h:275
ViewportUpdateMode
This enum describes how QGraphicsView updates its viewport when the scene contents change or are expo...
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
void mouseMoveEvent(QMouseEvent *)
This event handler can be reimplemented in a subclass to receive mouse move events for the viewport()...
ViewportAnchor
This enums describe the possible anchors that QGraphicsView can use when the user resizes the view or...
Definition: qgraphicsview.h:86
AspectRatioMode
Definition: qnamespace.h:1317