qgraphicsview.h 13 KB

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