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 QGRAPHICSVIEW_H
41#define QGRAPHICSVIEW_H
42
43#include <QtWidgets/qtwidgetsglobal.h>
44#include <QtCore/qmetatype.h>
45#include <QtGui/qpainter.h>
46#include <QtWidgets/qscrollarea.h>
47#include <QtWidgets/qgraphicsscene.h>
48
49QT_REQUIRE_CONFIG(graphicsview);
50
51QT_BEGIN_NAMESPACE
52
53class QGraphicsItem;
54class QPainterPath;
55class QPolygonF;
56class QStyleOptionGraphicsItem;
57
58class QGraphicsViewPrivate;
59class Q_WIDGETS_EXPORT QGraphicsView : public QAbstractScrollArea
60{
61 Q_OBJECT
62 Q_FLAGS(QPainter::RenderHints CacheMode OptimizationFlags)
63 Q_PROPERTY(QBrush backgroundBrush READ backgroundBrush WRITE setBackgroundBrush)
64 Q_PROPERTY(QBrush foregroundBrush READ foregroundBrush WRITE setForegroundBrush)
65 Q_PROPERTY(bool interactive READ isInteractive WRITE setInteractive)
66 Q_PROPERTY(QRectF sceneRect READ sceneRect WRITE setSceneRect)
67 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
68 Q_PROPERTY(QPainter::RenderHints renderHints READ renderHints WRITE setRenderHints)
69 Q_PROPERTY(DragMode dragMode READ dragMode WRITE setDragMode)
70 Q_PROPERTY(CacheMode cacheMode READ cacheMode WRITE setCacheMode)
71 Q_PROPERTY(ViewportAnchor transformationAnchor READ transformationAnchor WRITE setTransformationAnchor)
72 Q_PROPERTY(ViewportAnchor resizeAnchor READ resizeAnchor WRITE setResizeAnchor)
73 Q_PROPERTY(ViewportUpdateMode viewportUpdateMode READ viewportUpdateMode WRITE setViewportUpdateMode)
74#if QT_CONFIG(rubberband)
75 Q_PROPERTY(Qt::ItemSelectionMode rubberBandSelectionMode READ rubberBandSelectionMode WRITE setRubberBandSelectionMode)
76#endif
77 Q_PROPERTY(OptimizationFlags optimizationFlags READ optimizationFlags WRITE setOptimizationFlags)
78
79public:
80 enum ViewportAnchor {
81 NoAnchor,
82 AnchorViewCenter,
83 AnchorUnderMouse
84 };
85 Q_ENUM(ViewportAnchor)
86
87 enum CacheModeFlag {
88 CacheNone = 0x0,
89 CacheBackground = 0x1
90 };
91 Q_DECLARE_FLAGS(CacheMode, CacheModeFlag)
92
93 enum DragMode {
94 NoDrag,
95 ScrollHandDrag,
96 RubberBandDrag
97 };
98 Q_ENUM(DragMode)
99
100 enum ViewportUpdateMode {
101 FullViewportUpdate,
102 MinimalViewportUpdate,
103 SmartViewportUpdate,
104 NoViewportUpdate,
105 BoundingRectViewportUpdate
106 };
107 Q_ENUM(ViewportUpdateMode)
108
109 enum OptimizationFlag {
110 DontClipPainter = 0x1, // obsolete
111 DontSavePainterState = 0x2,
112 DontAdjustForAntialiasing = 0x4,
113 IndirectPainting = 0x8
114 };
115 Q_DECLARE_FLAGS(OptimizationFlags, OptimizationFlag)
116
117 QGraphicsView(QWidget *parent = nullptr);
118 QGraphicsView(QGraphicsScene *scene, QWidget *parent = nullptr);
119 ~QGraphicsView();
120
121 QSize sizeHint() const override;
122
123 QPainter::RenderHints renderHints() const;
124 void setRenderHint(QPainter::RenderHint hint, bool enabled = true);
125 void setRenderHints(QPainter::RenderHints hints);
126
127 Qt::Alignment alignment() const;
128 void setAlignment(Qt::Alignment alignment);
129
130 ViewportAnchor transformationAnchor() const;
131 void setTransformationAnchor(ViewportAnchor anchor);
132
133 ViewportAnchor resizeAnchor() const;
134 void setResizeAnchor(ViewportAnchor anchor);
135
136 ViewportUpdateMode viewportUpdateMode() const;
137 void setViewportUpdateMode(ViewportUpdateMode mode);
138
139 OptimizationFlags optimizationFlags() const;
140 void setOptimizationFlag(OptimizationFlag flag, bool enabled = true);
141 void setOptimizationFlags(OptimizationFlags flags);
142
143 DragMode dragMode() const;
144 void setDragMode(DragMode mode);
145
146#if QT_CONFIG(rubberband)
147 Qt::ItemSelectionMode rubberBandSelectionMode() const;
148 void setRubberBandSelectionMode(Qt::ItemSelectionMode mode);
149 QRect rubberBandRect() const;
150#endif
151
152 CacheMode cacheMode() const;
153 void setCacheMode(CacheMode mode);
154 void resetCachedContent();
155
156 bool isInteractive() const;
157 void setInteractive(bool allowed);
158
159 QGraphicsScene *scene() const;
160 void setScene(QGraphicsScene *scene);
161
162 QRectF sceneRect() const;
163 void setSceneRect(const QRectF &rect);
164 inline void setSceneRect(qreal x, qreal y, qreal w, qreal h);
165
166 QMatrix matrix() const;
167 void setMatrix(const QMatrix &matrix, bool combine = false);
168 void resetMatrix();
169 QTransform transform() const;
170 QTransform viewportTransform() const;
171 bool isTransformed() const;
172 void setTransform(const QTransform &matrix, bool combine = false);
173 void resetTransform();
174 void rotate(qreal angle);
175 void scale(qreal sx, qreal sy);
176 void shear(qreal sh, qreal sv);
177 void translate(qreal dx, qreal dy);
178
179 void centerOn(const QPointF &pos);
180 inline void centerOn(qreal x, qreal y);
181 void centerOn(const QGraphicsItem *item);
182 void ensureVisible(const QRectF &rect, int xmargin = 50, int ymargin = 50);
183 inline void ensureVisible(qreal x, qreal y, qreal w, qreal h, int xmargin = 50, int ymargin = 50);
184 void ensureVisible(const QGraphicsItem *item, int xmargin = 50, int ymargin = 50);
185 void fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRadioMode = Qt::IgnoreAspectRatio);
186 inline void fitInView(qreal x, qreal y, qreal w, qreal h,
187 Qt::AspectRatioMode aspectRadioMode = Qt::IgnoreAspectRatio);
188 void fitInView(const QGraphicsItem *item,
189 Qt::AspectRatioMode aspectRadioMode = Qt::IgnoreAspectRatio);
190
191 void render(QPainter *painter, const QRectF &target = QRectF(), const QRect &source = QRect(),
192 Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio);
193
194 QList<QGraphicsItem *> items() const;
195 QList<QGraphicsItem *> items(const QPoint &pos) const;
196 inline QList<QGraphicsItem *> items(int x, int y) const;
197 QList<QGraphicsItem *> items(const QRect &rect, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
198 inline QList<QGraphicsItem *> items(int x, int y, int w, int h, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
199 QList<QGraphicsItem *> items(const QPolygon &polygon, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
200 QList<QGraphicsItem *> items(const QPainterPath &path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
201 QGraphicsItem *itemAt(const QPoint &pos) const;
202 inline QGraphicsItem *itemAt(int x, int y) const;
203
204 QPointF mapToScene(const QPoint &point) const;
205 QPolygonF mapToScene(const QRect &rect) const;
206 QPolygonF mapToScene(const QPolygon &polygon) const;
207 QPainterPath mapToScene(const QPainterPath &path) const;
208 QPoint mapFromScene(const QPointF &point) const;
209 QPolygon mapFromScene(const QRectF &rect) const;
210 QPolygon mapFromScene(const QPolygonF &polygon) const;
211 QPainterPath mapFromScene(const QPainterPath &path) const;
212 inline QPointF mapToScene(int x, int y) const;
213 inline QPolygonF mapToScene(int x, int y, int w, int h) const;
214 inline QPoint mapFromScene(qreal x, qreal y) const;
215 inline QPolygon mapFromScene(qreal x, qreal y, qreal w, qreal h) const;
216
217 QVariant inputMethodQuery(Qt::InputMethodQuery query) const override;
218
219 QBrush backgroundBrush() const;
220 void setBackgroundBrush(const QBrush &brush);
221
222 QBrush foregroundBrush() const;
223 void setForegroundBrush(const QBrush &brush);
224
225public Q_SLOTS:
226 void updateScene(const QList<QRectF> &rects);
227 void invalidateScene(const QRectF &rect = QRectF(), QGraphicsScene::SceneLayers layers = QGraphicsScene::AllLayers);
228 void updateSceneRect(const QRectF &rect);
229
230#if QT_CONFIG(rubberband)
231Q_SIGNALS:
232 void rubberBandChanged(QRect viewportRect, QPointF fromScenePoint, QPointF toScenePoint);
233#endif
234
235protected Q_SLOTS:
236 void setupViewport(QWidget *widget) override;
237
238protected:
239 QGraphicsView(QGraphicsViewPrivate &, QWidget *parent = nullptr);
240 bool event(QEvent *event) override;
241 bool viewportEvent(QEvent *event) override;
242
243#ifndef QT_NO_CONTEXTMENU
244 void contextMenuEvent(QContextMenuEvent *event) override;
245#endif
246#if QT_CONFIG(draganddrop)
247 void dragEnterEvent(QDragEnterEvent *event) override;
248 void dragLeaveEvent(QDragLeaveEvent *event) override;
249 void dragMoveEvent(QDragMoveEvent *event) override;
250 void dropEvent(QDropEvent *event) override;
251#endif
252 void focusInEvent(QFocusEvent *event) override;
253 bool focusNextPrevChild(bool next) override;
254 void focusOutEvent(QFocusEvent *event) override;
255 void keyPressEvent(QKeyEvent *event) override;
256 void keyReleaseEvent(QKeyEvent *event) override;
257 void mouseDoubleClickEvent(QMouseEvent *event) override;
258 void mousePressEvent(QMouseEvent *event) override;
259 void mouseMoveEvent(QMouseEvent *event) override;
260 void mouseReleaseEvent(QMouseEvent *event) override;
261#if QT_CONFIG(wheelevent)
262 void wheelEvent(QWheelEvent *event) override;
263#endif
264 void paintEvent(QPaintEvent *event) override;
265 void resizeEvent(QResizeEvent *event) override;
266 void scrollContentsBy(int dx, int dy) override;
267 void showEvent(QShowEvent *event) override;
268 void inputMethodEvent(QInputMethodEvent *event) override;
269
270 virtual void drawBackground(QPainter *painter, const QRectF &rect);
271 virtual void drawForeground(QPainter *painter, const QRectF &rect);
272 virtual void drawItems(QPainter *painter, int numItems,
273 QGraphicsItem *items[],
274 const QStyleOptionGraphicsItem options[]);
275
276private:
277 Q_DECLARE_PRIVATE(QGraphicsView)
278 Q_DISABLE_COPY(QGraphicsView)
279#ifndef QT_NO_CURSOR
280 Q_PRIVATE_SLOT(d_func(), void _q_setViewportCursor(const QCursor &))
281 Q_PRIVATE_SLOT(d_func(), void _q_unsetViewportCursor())
282#endif
283 friend class QGraphicsSceneWidget;
284 friend class QGraphicsScene;
285 friend class QGraphicsScenePrivate;
286 friend class QGraphicsItemPrivate;
287};
288
289Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsView::CacheMode)
290Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsView::OptimizationFlags)
291
292inline void QGraphicsView::setSceneRect(qreal ax, qreal ay, qreal aw, qreal ah)
293{ setSceneRect(QRectF(ax, ay, aw, ah)); }
294inline void QGraphicsView::centerOn(qreal ax, qreal ay)
295{ centerOn(QPointF(ax, ay)); }
296inline void QGraphicsView::ensureVisible(qreal ax, qreal ay, qreal aw, qreal ah, int xmargin, int ymargin)
297{ ensureVisible(QRectF(ax, ay, aw, ah), xmargin, ymargin); }
298inline void QGraphicsView::fitInView(qreal ax, qreal ay, qreal w, qreal h, Qt::AspectRatioMode mode)
299{ fitInView(QRectF(ax, ay, w, h), mode); }
300inline QList<QGraphicsItem *> QGraphicsView::items(int ax, int ay) const
301{ return items(QPoint(ax, ay)); }
302inline QList<QGraphicsItem *> QGraphicsView::items(int ax, int ay, int w, int h, Qt::ItemSelectionMode mode) const
303{ return items(QRect(ax, ay, w, h), mode); }
304inline QGraphicsItem *QGraphicsView::itemAt(int ax, int ay) const
305{ return itemAt(QPoint(ax, ay)); }
306inline QPointF QGraphicsView::mapToScene(int ax, int ay) const
307{ return mapToScene(QPoint(ax, ay)); }
308inline QPolygonF QGraphicsView::mapToScene(int ax, int ay, int w, int h) const
309{ return mapToScene(QRect(ax, ay, w, h)); }
310inline QPoint QGraphicsView::mapFromScene(qreal ax, qreal ay) const
311{ return mapFromScene(QPointF(ax, ay)); }
312inline QPolygon QGraphicsView::mapFromScene(qreal ax, qreal ay, qreal w, qreal h) const
313{ return mapFromScene(QRectF(ax, ay, w, h)); }
314
315QT_END_NAMESPACE
316
317#endif // QGRAPHICSVIEW_H
318