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 DontSavePainterState = 0x1,
111 DontAdjustForAntialiasing = 0x2,
112 IndirectPainting = 0x4
113 };
114 Q_DECLARE_FLAGS(OptimizationFlags, OptimizationFlag)
115
116 QGraphicsView(QWidget *parent = nullptr);
117 QGraphicsView(QGraphicsScene *scene, QWidget *parent = nullptr);
118 ~QGraphicsView();
119
120 QSize sizeHint() const override;
121
122 QPainter::RenderHints renderHints() const;
123 void setRenderHint(QPainter::RenderHint hint, bool enabled = true);
124 void setRenderHints(QPainter::RenderHints hints);
125
126 Qt::Alignment alignment() const;
127 void setAlignment(Qt::Alignment alignment);
128
129 ViewportAnchor transformationAnchor() const;
130 void setTransformationAnchor(ViewportAnchor anchor);
131
132 ViewportAnchor resizeAnchor() const;
133 void setResizeAnchor(ViewportAnchor anchor);
134
135 ViewportUpdateMode viewportUpdateMode() const;
136 void setViewportUpdateMode(ViewportUpdateMode mode);
137
138 OptimizationFlags optimizationFlags() const;
139 void setOptimizationFlag(OptimizationFlag flag, bool enabled = true);
140 void setOptimizationFlags(OptimizationFlags flags);
141
142 DragMode dragMode() const;
143 void setDragMode(DragMode mode);
144
145#if QT_CONFIG(rubberband)
146 Qt::ItemSelectionMode rubberBandSelectionMode() const;
147 void setRubberBandSelectionMode(Qt::ItemSelectionMode mode);
148 QRect rubberBandRect() const;
149#endif
150
151 CacheMode cacheMode() const;
152 void setCacheMode(CacheMode mode);
153 void resetCachedContent();
154
155 bool isInteractive() const;
156 void setInteractive(bool allowed);
157
158 QGraphicsScene *scene() const;
159 void setScene(QGraphicsScene *scene);
160
161 QRectF sceneRect() const;
162 void setSceneRect(const QRectF &rect);
163 inline void setSceneRect(qreal x, qreal y, qreal w, qreal h);
164
165 QTransform transform() const;
166 QTransform viewportTransform() const;
167 bool isTransformed() const;
168 void setTransform(const QTransform &matrix, bool combine = false);
169 void resetTransform();
170 void rotate(qreal angle);
171 void scale(qreal sx, qreal sy);
172 void shear(qreal sh, qreal sv);
173 void translate(qreal dx, qreal dy);
174
175 void centerOn(const QPointF &pos);
176 inline void centerOn(qreal x, qreal y);
177 void centerOn(const QGraphicsItem *item);
178 void ensureVisible(const QRectF &rect, int xmargin = 50, int ymargin = 50);
179 inline void ensureVisible(qreal x, qreal y, qreal w, qreal h, int xmargin = 50, int ymargin = 50);
180 void ensureVisible(const QGraphicsItem *item, int xmargin = 50, int ymargin = 50);
181 void fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRadioMode = Qt::IgnoreAspectRatio);
182 inline void fitInView(qreal x, qreal y, qreal w, qreal h,
183 Qt::AspectRatioMode aspectRadioMode = Qt::IgnoreAspectRatio);
184 void fitInView(const QGraphicsItem *item,
185 Qt::AspectRatioMode aspectRadioMode = Qt::IgnoreAspectRatio);
186
187 void render(QPainter *painter, const QRectF &target = QRectF(), const QRect &source = QRect(),
188 Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio);
189
190 QList<QGraphicsItem *> items() const;
191 QList<QGraphicsItem *> items(const QPoint &pos) const;
192 inline QList<QGraphicsItem *> items(int x, int y) const;
193 QList<QGraphicsItem *> items(const QRect &rect, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
194 inline QList<QGraphicsItem *> items(int x, int y, int w, int h, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
195 QList<QGraphicsItem *> items(const QPolygon &polygon, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
196 QList<QGraphicsItem *> items(const QPainterPath &path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
197 QGraphicsItem *itemAt(const QPoint &pos) const;
198 inline QGraphicsItem *itemAt(int x, int y) const;
199
200 QPointF mapToScene(const QPoint &point) const;
201 QPolygonF mapToScene(const QRect &rect) const;
202 QPolygonF mapToScene(const QPolygon &polygon) const;
203 QPainterPath mapToScene(const QPainterPath &path) const;
204 QPoint mapFromScene(const QPointF &point) const;
205 QPolygon mapFromScene(const QRectF &rect) const;
206 QPolygon mapFromScene(const QPolygonF &polygon) const;
207 QPainterPath mapFromScene(const QPainterPath &path) const;
208 inline QPointF mapToScene(int x, int y) const;
209 inline QPolygonF mapToScene(int x, int y, int w, int h) const;
210 inline QPoint mapFromScene(qreal x, qreal y) const;
211 inline QPolygon mapFromScene(qreal x, qreal y, qreal w, qreal h) const;
212
213 QVariant inputMethodQuery(Qt::InputMethodQuery query) const override;
214
215 QBrush backgroundBrush() const;
216 void setBackgroundBrush(const QBrush &brush);
217
218 QBrush foregroundBrush() const;
219 void setForegroundBrush(const QBrush &brush);
220
221public Q_SLOTS:
222 void updateScene(const QList<QRectF> &rects);
223 void invalidateScene(const QRectF &rect = QRectF(), QGraphicsScene::SceneLayers layers = QGraphicsScene::AllLayers);
224 void updateSceneRect(const QRectF &rect);
225
226#if QT_CONFIG(rubberband)
227Q_SIGNALS:
228 void rubberBandChanged(QRect viewportRect, QPointF fromScenePoint, QPointF toScenePoint);
229#endif
230
231protected Q_SLOTS:
232 void setupViewport(QWidget *widget) override;
233
234protected:
235 QGraphicsView(QGraphicsViewPrivate &, QWidget *parent = nullptr);
236 bool event(QEvent *event) override;
237 bool viewportEvent(QEvent *event) override;
238
239#ifndef QT_NO_CONTEXTMENU
240 void contextMenuEvent(QContextMenuEvent *event) override;
241#endif
242#if QT_CONFIG(draganddrop)
243 void dragEnterEvent(QDragEnterEvent *event) override;
244 void dragLeaveEvent(QDragLeaveEvent *event) override;
245 void dragMoveEvent(QDragMoveEvent *event) override;
246 void dropEvent(QDropEvent *event) override;
247#endif
248 void focusInEvent(QFocusEvent *event) override;
249 bool focusNextPrevChild(bool next) override;
250 void focusOutEvent(QFocusEvent *event) override;
251 void keyPressEvent(QKeyEvent *event) override;
252 void keyReleaseEvent(QKeyEvent *event) override;
253 void mouseDoubleClickEvent(QMouseEvent *event) override;
254 void mousePressEvent(QMouseEvent *event) override;
255 void mouseMoveEvent(QMouseEvent *event) override;
256 void mouseReleaseEvent(QMouseEvent *event) override;
257#if QT_CONFIG(wheelevent)
258 void wheelEvent(QWheelEvent *event) override;
259#endif
260 void paintEvent(QPaintEvent *event) override;
261 void resizeEvent(QResizeEvent *event) override;
262 void scrollContentsBy(int dx, int dy) override;
263 void showEvent(QShowEvent *event) override;
264 void inputMethodEvent(QInputMethodEvent *event) override;
265
266 virtual void drawBackground(QPainter *painter, const QRectF &rect);
267 virtual void drawForeground(QPainter *painter, const QRectF &rect);
268 virtual void drawItems(QPainter *painter, int numItems,
269 QGraphicsItem *items[],
270 const QStyleOptionGraphicsItem options[]);
271
272private:
273 Q_DECLARE_PRIVATE(QGraphicsView)
274 Q_DISABLE_COPY(QGraphicsView)
275#ifndef QT_NO_CURSOR
276 Q_PRIVATE_SLOT(d_func(), void _q_setViewportCursor(const QCursor &))
277 Q_PRIVATE_SLOT(d_func(), void _q_unsetViewportCursor())
278#endif
279 friend class QGraphicsSceneWidget;
280 friend class QGraphicsScene;
281 friend class QGraphicsScenePrivate;
282 friend class QGraphicsItemPrivate;
283};
284
285Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsView::CacheMode)
286Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsView::OptimizationFlags)
287
288inline void QGraphicsView::setSceneRect(qreal ax, qreal ay, qreal aw, qreal ah)
289{ setSceneRect(QRectF(ax, ay, aw, ah)); }
290inline void QGraphicsView::centerOn(qreal ax, qreal ay)
291{ centerOn(QPointF(ax, ay)); }
292inline void QGraphicsView::ensureVisible(qreal ax, qreal ay, qreal aw, qreal ah, int xmargin, int ymargin)
293{ ensureVisible(QRectF(ax, ay, aw, ah), xmargin, ymargin); }
294inline void QGraphicsView::fitInView(qreal ax, qreal ay, qreal w, qreal h, Qt::AspectRatioMode mode)
295{ fitInView(QRectF(ax, ay, w, h), mode); }
296inline QList<QGraphicsItem *> QGraphicsView::items(int ax, int ay) const
297{ return items(QPoint(ax, ay)); }
298inline QList<QGraphicsItem *> QGraphicsView::items(int ax, int ay, int w, int h, Qt::ItemSelectionMode mode) const
299{ return items(QRect(ax, ay, w, h), mode); }
300inline QGraphicsItem *QGraphicsView::itemAt(int ax, int ay) const
301{ return itemAt(QPoint(ax, ay)); }
302inline QPointF QGraphicsView::mapToScene(int ax, int ay) const
303{ return mapToScene(QPoint(ax, ay)); }
304inline QPolygonF QGraphicsView::mapToScene(int ax, int ay, int w, int h) const
305{ return mapToScene(QRect(ax, ay, w, h)); }
306inline QPoint QGraphicsView::mapFromScene(qreal ax, qreal ay) const
307{ return mapFromScene(QPointF(ax, ay)); }
308inline QPolygon QGraphicsView::mapFromScene(qreal ax, qreal ay, qreal w, qreal h) const
309{ return mapFromScene(QRectF(ax, ay, w, h)); }
310
311QT_END_NAMESPACE
312
313#endif // QGRAPHICSVIEW_H
314