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