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

source code of qtbase/src/widgets/graphicsview/qgraphicsview.h