File indexing completed on 2025-01-18 10:09:35
0001
0002
0003
0004 #ifndef QGRAPHICSVIEW_H
0005 #define QGRAPHICSVIEW_H
0006
0007 #include <QtWidgets/qtwidgetsglobal.h>
0008 #include <QtCore/qmetatype.h>
0009 #include <QtGui/qpainter.h>
0010 #include <QtWidgets/qscrollarea.h>
0011 #include <QtWidgets/qgraphicsscene.h>
0012
0013 QT_REQUIRE_CONFIG(graphicsview);
0014
0015 QT_BEGIN_NAMESPACE
0016
0017 class QGraphicsItem;
0018 class QPainterPath;
0019 class QPolygonF;
0020 class QStyleOptionGraphicsItem;
0021
0022 class QGraphicsViewPrivate;
0023 class Q_WIDGETS_EXPORT QGraphicsView : public QAbstractScrollArea
0024 {
0025 Q_OBJECT
0026 Q_FLAGS(QPainter::RenderHints CacheMode OptimizationFlags)
0027 Q_PROPERTY(QBrush backgroundBrush READ backgroundBrush WRITE setBackgroundBrush)
0028 Q_PROPERTY(QBrush foregroundBrush READ foregroundBrush WRITE setForegroundBrush)
0029 Q_PROPERTY(bool interactive READ isInteractive WRITE setInteractive)
0030 Q_PROPERTY(QRectF sceneRect READ sceneRect WRITE setSceneRect)
0031 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
0032 Q_PROPERTY(QPainter::RenderHints renderHints READ renderHints WRITE setRenderHints)
0033 Q_PROPERTY(DragMode dragMode READ dragMode WRITE setDragMode)
0034 Q_PROPERTY(CacheMode cacheMode READ cacheMode WRITE setCacheMode)
0035 Q_PROPERTY(ViewportAnchor transformationAnchor READ transformationAnchor
0036 WRITE setTransformationAnchor)
0037 Q_PROPERTY(ViewportAnchor resizeAnchor READ resizeAnchor WRITE setResizeAnchor)
0038 Q_PROPERTY(ViewportUpdateMode viewportUpdateMode READ viewportUpdateMode
0039 WRITE setViewportUpdateMode)
0040 #if QT_CONFIG(rubberband)
0041 Q_PROPERTY(Qt::ItemSelectionMode rubberBandSelectionMode READ rubberBandSelectionMode
0042 WRITE setRubberBandSelectionMode)
0043 #endif
0044 Q_PROPERTY(OptimizationFlags optimizationFlags READ optimizationFlags
0045 WRITE setOptimizationFlags)
0046
0047 public:
0048 enum ViewportAnchor {
0049 NoAnchor,
0050 AnchorViewCenter,
0051 AnchorUnderMouse
0052 };
0053 Q_ENUM(ViewportAnchor)
0054
0055 enum CacheModeFlag {
0056 CacheNone = 0x0,
0057 CacheBackground = 0x1
0058 };
0059 Q_DECLARE_FLAGS(CacheMode, CacheModeFlag)
0060
0061 enum DragMode {
0062 NoDrag,
0063 ScrollHandDrag,
0064 RubberBandDrag
0065 };
0066 Q_ENUM(DragMode)
0067
0068 enum ViewportUpdateMode {
0069 FullViewportUpdate,
0070 MinimalViewportUpdate,
0071 SmartViewportUpdate,
0072 NoViewportUpdate,
0073 BoundingRectViewportUpdate
0074 };
0075 Q_ENUM(ViewportUpdateMode)
0076
0077 enum OptimizationFlag {
0078 DontSavePainterState = 0x1,
0079 DontAdjustForAntialiasing = 0x2,
0080 IndirectPainting = 0x4
0081 };
0082 Q_DECLARE_FLAGS(OptimizationFlags, OptimizationFlag)
0083
0084 QGraphicsView(QWidget *parent = nullptr);
0085 QGraphicsView(QGraphicsScene *scene, QWidget *parent = nullptr);
0086 ~QGraphicsView();
0087
0088 QSize sizeHint() const override;
0089
0090 QPainter::RenderHints renderHints() const;
0091 void setRenderHint(QPainter::RenderHint hint, bool enabled = true);
0092 void setRenderHints(QPainter::RenderHints hints);
0093
0094 Qt::Alignment alignment() const;
0095 void setAlignment(Qt::Alignment alignment);
0096
0097 ViewportAnchor transformationAnchor() const;
0098 void setTransformationAnchor(ViewportAnchor anchor);
0099
0100 ViewportAnchor resizeAnchor() const;
0101 void setResizeAnchor(ViewportAnchor anchor);
0102
0103 ViewportUpdateMode viewportUpdateMode() const;
0104 void setViewportUpdateMode(ViewportUpdateMode mode);
0105
0106 OptimizationFlags optimizationFlags() const;
0107 void setOptimizationFlag(OptimizationFlag flag, bool enabled = true);
0108 void setOptimizationFlags(OptimizationFlags flags);
0109
0110 DragMode dragMode() const;
0111 void setDragMode(DragMode mode);
0112
0113 #if QT_CONFIG(rubberband)
0114 Qt::ItemSelectionMode rubberBandSelectionMode() const;
0115 void setRubberBandSelectionMode(Qt::ItemSelectionMode mode);
0116 QRect rubberBandRect() const;
0117 #endif
0118
0119 CacheMode cacheMode() const;
0120 void setCacheMode(CacheMode mode);
0121 void resetCachedContent();
0122
0123 bool isInteractive() const;
0124 void setInteractive(bool allowed);
0125
0126 QGraphicsScene *scene() const;
0127 void setScene(QGraphicsScene *scene);
0128
0129 QRectF sceneRect() const;
0130 void setSceneRect(const QRectF &rect);
0131 inline void setSceneRect(qreal x, qreal y, qreal w, qreal h);
0132
0133 QTransform transform() const;
0134 QTransform viewportTransform() const;
0135 bool isTransformed() const;
0136 void setTransform(const QTransform &matrix, bool combine = false);
0137 void resetTransform();
0138 void rotate(qreal angle);
0139 void scale(qreal sx, qreal sy);
0140 void shear(qreal sh, qreal sv);
0141 void translate(qreal dx, qreal dy);
0142
0143 void centerOn(const QPointF &pos);
0144 inline void centerOn(qreal x, qreal y);
0145 void centerOn(const QGraphicsItem *item);
0146 void ensureVisible(const QRectF &rect, int xmargin = 50, int ymargin = 50);
0147 inline void ensureVisible(qreal x, qreal y, qreal w, qreal h, int xmargin = 50, int ymargin = 50);
0148 void ensureVisible(const QGraphicsItem *item, int xmargin = 50, int ymargin = 50);
0149 void fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRadioMode = Qt::IgnoreAspectRatio);
0150 inline void fitInView(qreal x, qreal y, qreal w, qreal h,
0151 Qt::AspectRatioMode aspectRadioMode = Qt::IgnoreAspectRatio);
0152 void fitInView(const QGraphicsItem *item,
0153 Qt::AspectRatioMode aspectRadioMode = Qt::IgnoreAspectRatio);
0154
0155 void render(QPainter *painter, const QRectF &target = QRectF(), const QRect &source = QRect(),
0156 Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio);
0157
0158 QList<QGraphicsItem *> items() const;
0159 QList<QGraphicsItem *> items(const QPoint &pos) const;
0160 inline QList<QGraphicsItem *> items(int x, int y) const;
0161 QList<QGraphicsItem *> items(const QRect &rect, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
0162 inline QList<QGraphicsItem *> items(int x, int y, int w, int h, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
0163 QList<QGraphicsItem *> items(const QPolygon &polygon, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
0164 QList<QGraphicsItem *> items(const QPainterPath &path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
0165 QGraphicsItem *itemAt(const QPoint &pos) const;
0166 inline QGraphicsItem *itemAt(int x, int y) const;
0167
0168 QPointF mapToScene(const QPoint &point) const;
0169 QPolygonF mapToScene(const QRect &rect) const;
0170 QPolygonF mapToScene(const QPolygon &polygon) const;
0171 QPainterPath mapToScene(const QPainterPath &path) const;
0172 QPoint mapFromScene(const QPointF &point) const;
0173 QPolygon mapFromScene(const QRectF &rect) const;
0174 QPolygon mapFromScene(const QPolygonF &polygon) const;
0175 QPainterPath mapFromScene(const QPainterPath &path) const;
0176 inline QPointF mapToScene(int x, int y) const;
0177 inline QPolygonF mapToScene(int x, int y, int w, int h) const;
0178 inline QPoint mapFromScene(qreal x, qreal y) const;
0179 inline QPolygon mapFromScene(qreal x, qreal y, qreal w, qreal h) const;
0180
0181 QVariant inputMethodQuery(Qt::InputMethodQuery query) const override;
0182
0183 QBrush backgroundBrush() const;
0184 void setBackgroundBrush(const QBrush &brush);
0185
0186 QBrush foregroundBrush() const;
0187 void setForegroundBrush(const QBrush &brush);
0188
0189 public Q_SLOTS:
0190 void updateScene(const QList<QRectF> &rects);
0191 void invalidateScene(const QRectF &rect = QRectF(), QGraphicsScene::SceneLayers layers = QGraphicsScene::AllLayers);
0192 void updateSceneRect(const QRectF &rect);
0193
0194 #if QT_CONFIG(rubberband)
0195 Q_SIGNALS:
0196 void rubberBandChanged(QRect viewportRect, QPointF fromScenePoint, QPointF toScenePoint);
0197 #endif
0198
0199 protected Q_SLOTS:
0200 void setupViewport(QWidget *widget) override;
0201
0202 protected:
0203 QGraphicsView(QGraphicsViewPrivate &, QWidget *parent = nullptr);
0204 bool event(QEvent *event) override;
0205 bool viewportEvent(QEvent *event) override;
0206
0207 #ifndef QT_NO_CONTEXTMENU
0208 void contextMenuEvent(QContextMenuEvent *event) override;
0209 #endif
0210 #if QT_CONFIG(draganddrop)
0211 void dragEnterEvent(QDragEnterEvent *event) override;
0212 void dragLeaveEvent(QDragLeaveEvent *event) override;
0213 void dragMoveEvent(QDragMoveEvent *event) override;
0214 void dropEvent(QDropEvent *event) override;
0215 #endif
0216 void focusInEvent(QFocusEvent *event) override;
0217 bool focusNextPrevChild(bool next) override;
0218 void focusOutEvent(QFocusEvent *event) override;
0219 void keyPressEvent(QKeyEvent *event) override;
0220 void keyReleaseEvent(QKeyEvent *event) override;
0221 void mouseDoubleClickEvent(QMouseEvent *event) override;
0222 void mousePressEvent(QMouseEvent *event) override;
0223 void mouseMoveEvent(QMouseEvent *event) override;
0224 void mouseReleaseEvent(QMouseEvent *event) override;
0225 #if QT_CONFIG(wheelevent)
0226 void wheelEvent(QWheelEvent *event) override;
0227 #endif
0228 void paintEvent(QPaintEvent *event) override;
0229 void resizeEvent(QResizeEvent *event) override;
0230 void scrollContentsBy(int dx, int dy) override;
0231 void showEvent(QShowEvent *event) override;
0232 void inputMethodEvent(QInputMethodEvent *event) override;
0233
0234 virtual void drawBackground(QPainter *painter, const QRectF &rect);
0235 virtual void drawForeground(QPainter *painter, const QRectF &rect);
0236 virtual void drawItems(QPainter *painter, int numItems,
0237 QGraphicsItem *items[],
0238 const QStyleOptionGraphicsItem options[]);
0239
0240 private:
0241 Q_DECLARE_PRIVATE(QGraphicsView)
0242 Q_DISABLE_COPY(QGraphicsView)
0243 #ifndef QT_NO_CURSOR
0244 Q_PRIVATE_SLOT(d_func(), void _q_setViewportCursor(const QCursor &))
0245 Q_PRIVATE_SLOT(d_func(), void _q_unsetViewportCursor())
0246 #endif
0247 friend class QGraphicsSceneWidget;
0248 friend class QGraphicsScene;
0249 friend class QGraphicsScenePrivate;
0250 friend class QGraphicsItemPrivate;
0251 };
0252
0253 Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsView::CacheMode)
0254 Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsView::OptimizationFlags)
0255
0256 inline void QGraphicsView::setSceneRect(qreal ax, qreal ay, qreal aw, qreal ah)
0257 { setSceneRect(QRectF(ax, ay, aw, ah)); }
0258 inline void QGraphicsView::centerOn(qreal ax, qreal ay)
0259 { centerOn(QPointF(ax, ay)); }
0260 inline void QGraphicsView::ensureVisible(qreal ax, qreal ay, qreal aw, qreal ah, int xmargin, int ymargin)
0261 { ensureVisible(QRectF(ax, ay, aw, ah), xmargin, ymargin); }
0262 inline void QGraphicsView::fitInView(qreal ax, qreal ay, qreal w, qreal h, Qt::AspectRatioMode mode)
0263 { fitInView(QRectF(ax, ay, w, h), mode); }
0264 inline QList<QGraphicsItem *> QGraphicsView::items(int ax, int ay) const
0265 { return items(QPoint(ax, ay)); }
0266 inline QList<QGraphicsItem *> QGraphicsView::items(int ax, int ay, int w, int h, Qt::ItemSelectionMode mode) const
0267 { return items(QRect(ax, ay, w, h), mode); }
0268 inline QGraphicsItem *QGraphicsView::itemAt(int ax, int ay) const
0269 { return itemAt(QPoint(ax, ay)); }
0270 inline QPointF QGraphicsView::mapToScene(int ax, int ay) const
0271 { return mapToScene(QPoint(ax, ay)); }
0272 inline QPolygonF QGraphicsView::mapToScene(int ax, int ay, int w, int h) const
0273 { return mapToScene(QRect(ax, ay, w, h)); }
0274 inline QPoint QGraphicsView::mapFromScene(qreal ax, qreal ay) const
0275 { return mapFromScene(QPointF(ax, ay)); }
0276 inline QPolygon QGraphicsView::mapFromScene(qreal ax, qreal ay, qreal w, qreal h) const
0277 { return mapFromScene(QRectF(ax, ay, w, h)); }
0278
0279 QT_END_NAMESPACE
0280
0281 #endif