qgraphicsscene.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 QGRAPHICSSCENE_H
  40. #define QGRAPHICSSCENE_H
  41. #include <QtCore/qobject.h>
  42. #include <QtCore/qpoint.h>
  43. #include <QtCore/qrect.h>
  44. #include <QtGui/qbrush.h>
  45. #include <QtGui/qfont.h>
  46. #include <QtGui/qtransform.h>
  47. #include <QtGui/qmatrix.h>
  48. #include <QtGui/qpen.h>
  49. QT_BEGIN_NAMESPACE
  50. #if !defined(QT_NO_GRAPHICSVIEW)
  51. template<typename T> class QList;
  52. class QFocusEvent;
  53. class QFont;
  54. class QFontMetrics;
  55. class QGraphicsEllipseItem;
  56. class QGraphicsItem;
  57. class QGraphicsItemGroup;
  58. class QGraphicsLineItem;
  59. class QGraphicsPathItem;
  60. class QGraphicsPixmapItem;
  61. class QGraphicsPolygonItem;
  62. class QGraphicsProxyWidget;
  63. class QGraphicsRectItem;
  64. class QGraphicsSceneContextMenuEvent;
  65. class QGraphicsSceneDragDropEvent;
  66. class QGraphicsSceneEvent;
  67. class QGraphicsSceneHelpEvent;
  68. class QGraphicsSceneHoverEvent;
  69. class QGraphicsSceneMouseEvent;
  70. class QGraphicsSceneWheelEvent;
  71. class QGraphicsSimpleTextItem;
  72. class QGraphicsTextItem;
  73. class QGraphicsView;
  74. class QGraphicsWidget;
  75. class QGraphicsSceneIndex;
  76. class QHelpEvent;
  77. class QInputMethodEvent;
  78. class QKeyEvent;
  79. class QLineF;
  80. class QPainterPath;
  81. class QPixmap;
  82. class QPointF;
  83. class QPolygonF;
  84. class QRectF;
  85. class QSizeF;
  86. class QStyle;
  87. class QStyleOptionGraphicsItem;
  88. class QGraphicsScenePrivate;
  89. class Q_WIDGETS_EXPORT QGraphicsScene : public QObject
  90. {
  91. Q_OBJECT
  92. Q_PROPERTY(QBrush backgroundBrush READ backgroundBrush WRITE setBackgroundBrush)
  93. Q_PROPERTY(QBrush foregroundBrush READ foregroundBrush WRITE setForegroundBrush)
  94. Q_PROPERTY(ItemIndexMethod itemIndexMethod READ itemIndexMethod WRITE setItemIndexMethod)
  95. Q_PROPERTY(QRectF sceneRect READ sceneRect WRITE setSceneRect)
  96. Q_PROPERTY(int bspTreeDepth READ bspTreeDepth WRITE setBspTreeDepth)
  97. Q_PROPERTY(QPalette palette READ palette WRITE setPalette)
  98. Q_PROPERTY(QFont font READ font WRITE setFont)
  99. Q_PROPERTY(bool sortCacheEnabled READ isSortCacheEnabled WRITE setSortCacheEnabled)
  100. Q_PROPERTY(bool stickyFocus READ stickyFocus WRITE setStickyFocus)
  101. Q_PROPERTY(qreal minimumRenderSize READ minimumRenderSize WRITE setMinimumRenderSize)
  102. public:
  103. enum ItemIndexMethod {
  104. BspTreeIndex,
  105. NoIndex = -1
  106. };
  107. enum SceneLayer {
  108. ItemLayer = 0x1,
  109. BackgroundLayer = 0x2,
  110. ForegroundLayer = 0x4,
  111. AllLayers = 0xffff
  112. };
  113. Q_DECLARE_FLAGS(SceneLayers, SceneLayer)
  114. QGraphicsScene(QObject *parent = Q_NULLPTR);
  115. QGraphicsScene(const QRectF &sceneRect, QObject *parent = Q_NULLPTR);
  116. QGraphicsScene(qreal x, qreal y, qreal width, qreal height, QObject *parent = Q_NULLPTR);
  117. virtual ~QGraphicsScene();
  118. QRectF sceneRect() const;
  119. inline qreal width() const { return sceneRect().width(); }
  120. inline qreal height() const { return sceneRect().height(); }
  121. void setSceneRect(const QRectF &rect);
  122. inline void setSceneRect(qreal x, qreal y, qreal w, qreal h)
  123. { setSceneRect(QRectF(x, y, w, h)); }
  124. void render(QPainter *painter,
  125. const QRectF &target = QRectF(), const QRectF &source = QRectF(),
  126. Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio);
  127. ItemIndexMethod itemIndexMethod() const;
  128. void setItemIndexMethod(ItemIndexMethod method);
  129. bool isSortCacheEnabled() const;
  130. void setSortCacheEnabled(bool enabled);
  131. int bspTreeDepth() const;
  132. void setBspTreeDepth(int depth);
  133. QRectF itemsBoundingRect() const;
  134. QList<QGraphicsItem *> items(Qt::SortOrder order = Qt::DescendingOrder) const;
  135. QList<QGraphicsItem *> items(const QPointF &pos, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, Qt::SortOrder order = Qt::DescendingOrder, const QTransform &deviceTransform = QTransform()) const;
  136. QList<QGraphicsItem *> items(const QRectF &rect, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, Qt::SortOrder order = Qt::DescendingOrder, const QTransform &deviceTransform = QTransform()) const;
  137. QList<QGraphicsItem *> items(const QPolygonF &polygon, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, Qt::SortOrder order = Qt::DescendingOrder, const QTransform &deviceTransform = QTransform()) const;
  138. QList<QGraphicsItem *> items(const QPainterPath &path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, Qt::SortOrder order = Qt::DescendingOrder, const QTransform &deviceTransform = QTransform()) const;
  139. QList<QGraphicsItem *> collidingItems(const QGraphicsItem *item, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
  140. #if QT_DEPRECATED_SINCE(5, 0)
  141. QT_DEPRECATED inline QGraphicsItem *itemAt(const QPointF &position) const {
  142. QList<QGraphicsItem *> itemsAtPoint = items(position);
  143. return itemsAtPoint.isEmpty() ? Q_NULLPTR : itemsAtPoint.first();
  144. }
  145. #endif
  146. QGraphicsItem *itemAt(const QPointF &pos, const QTransform &deviceTransform) const;
  147. #if QT_DEPRECATED_SINCE(5, 0)
  148. QT_DEPRECATED inline QList<QGraphicsItem *> items(qreal x, qreal y, qreal w, qreal h, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const
  149. { return items(QRectF(x, y, w, h), mode); }
  150. #endif
  151. inline QList<QGraphicsItem *> items(qreal x, qreal y, qreal w, qreal h, Qt::ItemSelectionMode mode, Qt::SortOrder order,
  152. const QTransform &deviceTransform = QTransform()) const
  153. { return items(QRectF(x, y, w, h), mode, order, deviceTransform); }
  154. #if QT_DEPRECATED_SINCE(5, 0)
  155. QT_DEPRECATED inline QGraphicsItem *itemAt(qreal x, qreal y) const {
  156. QList<QGraphicsItem *> itemsAtPoint = items(QPointF(x, y));
  157. return itemsAtPoint.isEmpty() ? Q_NULLPTR : itemsAtPoint.first();
  158. }
  159. #endif
  160. inline QGraphicsItem *itemAt(qreal x, qreal y, const QTransform &deviceTransform) const
  161. { return itemAt(QPointF(x, y), deviceTransform); }
  162. QList<QGraphicsItem *> selectedItems() const;
  163. QPainterPath selectionArea() const;
  164. void setSelectionArea(const QPainterPath &path, const QTransform &deviceTransform);
  165. void setSelectionArea(const QPainterPath &path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, const QTransform &deviceTransform = QTransform());
  166. void setSelectionArea(const QPainterPath &path, Qt::ItemSelectionOperation selectionOperation, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, const QTransform &deviceTransform = QTransform());
  167. // ### Qt6 merge the last 2 functions and add a default: Qt::ItemSelectionOperation selectionOperation = Qt::ReplaceSelection
  168. QGraphicsItemGroup *createItemGroup(const QList<QGraphicsItem *> &items);
  169. void destroyItemGroup(QGraphicsItemGroup *group);
  170. void addItem(QGraphicsItem *item);
  171. QGraphicsEllipseItem *addEllipse(const QRectF &rect, const QPen &pen = QPen(), const QBrush &brush = QBrush());
  172. QGraphicsLineItem *addLine(const QLineF &line, const QPen &pen = QPen());
  173. QGraphicsPathItem *addPath(const QPainterPath &path, const QPen &pen = QPen(), const QBrush &brush = QBrush());
  174. QGraphicsPixmapItem *addPixmap(const QPixmap &pixmap);
  175. QGraphicsPolygonItem *addPolygon(const QPolygonF &polygon, const QPen &pen = QPen(), const QBrush &brush = QBrush());
  176. QGraphicsRectItem *addRect(const QRectF &rect, const QPen &pen = QPen(), const QBrush &brush = QBrush());
  177. QGraphicsTextItem *addText(const QString &text, const QFont &font = QFont());
  178. QGraphicsSimpleTextItem *addSimpleText(const QString &text, const QFont &font = QFont());
  179. QGraphicsProxyWidget *addWidget(QWidget *widget, Qt::WindowFlags wFlags = Qt::WindowFlags());
  180. inline QGraphicsEllipseItem *addEllipse(qreal x, qreal y, qreal w, qreal h, const QPen &pen = QPen(), const QBrush &brush = QBrush())
  181. { return addEllipse(QRectF(x, y, w, h), pen, brush); }
  182. inline QGraphicsLineItem *addLine(qreal x1, qreal y1, qreal x2, qreal y2, const QPen &pen = QPen())
  183. { return addLine(QLineF(x1, y1, x2, y2), pen); }
  184. inline QGraphicsRectItem *addRect(qreal x, qreal y, qreal w, qreal h, const QPen &pen = QPen(), const QBrush &brush = QBrush())
  185. { return addRect(QRectF(x, y, w, h), pen, brush); }
  186. void removeItem(QGraphicsItem *item);
  187. QGraphicsItem *focusItem() const;
  188. void setFocusItem(QGraphicsItem *item, Qt::FocusReason focusReason = Qt::OtherFocusReason);
  189. bool hasFocus() const;
  190. void setFocus(Qt::FocusReason focusReason = Qt::OtherFocusReason);
  191. void clearFocus();
  192. void setStickyFocus(bool enabled);
  193. bool stickyFocus() const;
  194. QGraphicsItem *mouseGrabberItem() const;
  195. QBrush backgroundBrush() const;
  196. void setBackgroundBrush(const QBrush &brush);
  197. QBrush foregroundBrush() const;
  198. void setForegroundBrush(const QBrush &brush);
  199. virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
  200. QList <QGraphicsView *> views() const;
  201. inline void update(qreal x, qreal y, qreal w, qreal h)
  202. { update(QRectF(x, y, w, h)); }
  203. inline void invalidate(qreal x, qreal y, qreal w, qreal h, SceneLayers layers = AllLayers)
  204. { invalidate(QRectF(x, y, w, h), layers); }
  205. QStyle *style() const;
  206. void setStyle(QStyle *style);
  207. QFont font() const;
  208. void setFont(const QFont &font);
  209. QPalette palette() const;
  210. void setPalette(const QPalette &palette);
  211. bool isActive() const;
  212. QGraphicsItem *activePanel() const;
  213. void setActivePanel(QGraphicsItem *item);
  214. QGraphicsWidget *activeWindow() const;
  215. void setActiveWindow(QGraphicsWidget *widget);
  216. bool sendEvent(QGraphicsItem *item, QEvent *event);
  217. qreal minimumRenderSize() const;
  218. void setMinimumRenderSize(qreal minSize);
  219. public Q_SLOTS:
  220. void update(const QRectF &rect = QRectF());
  221. void invalidate(const QRectF &rect = QRectF(), SceneLayers layers = AllLayers);
  222. void advance();
  223. void clearSelection();
  224. void clear();
  225. protected:
  226. bool event(QEvent *event) Q_DECL_OVERRIDE;
  227. bool eventFilter(QObject *watched, QEvent *event) Q_DECL_OVERRIDE;
  228. virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
  229. virtual void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
  230. virtual void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
  231. virtual void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
  232. virtual void dropEvent(QGraphicsSceneDragDropEvent *event);
  233. virtual void focusInEvent(QFocusEvent *event);
  234. virtual void focusOutEvent(QFocusEvent *event);
  235. virtual void helpEvent(QGraphicsSceneHelpEvent *event);
  236. virtual void keyPressEvent(QKeyEvent *event);
  237. virtual void keyReleaseEvent(QKeyEvent *event);
  238. virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
  239. virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
  240. virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
  241. virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
  242. virtual void wheelEvent(QGraphicsSceneWheelEvent *event);
  243. virtual void inputMethodEvent(QInputMethodEvent *event);
  244. virtual void drawBackground(QPainter *painter, const QRectF &rect);
  245. virtual void drawForeground(QPainter *painter, const QRectF &rect);
  246. virtual void drawItems(QPainter *painter, int numItems,
  247. QGraphicsItem *items[],
  248. const QStyleOptionGraphicsItem options[],
  249. QWidget *widget = Q_NULLPTR);
  250. protected Q_SLOTS:
  251. // ### Qt 6: make unconditional
  252. #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
  253. virtual
  254. #endif
  255. bool focusNextPrevChild(bool next);
  256. Q_SIGNALS:
  257. void changed(const QList<QRectF> &region);
  258. void sceneRectChanged(const QRectF &rect);
  259. void selectionChanged();
  260. void focusItemChanged(QGraphicsItem *newFocus, QGraphicsItem *oldFocus, Qt::FocusReason reason);
  261. private:
  262. Q_DECLARE_PRIVATE(QGraphicsScene)
  263. Q_DISABLE_COPY(QGraphicsScene)
  264. Q_PRIVATE_SLOT(d_func(), void _q_emitUpdated())
  265. Q_PRIVATE_SLOT(d_func(), void _q_polishItems())
  266. Q_PRIVATE_SLOT(d_func(), void _q_processDirtyItems())
  267. Q_PRIVATE_SLOT(d_func(), void _q_updateScenePosDescendants())
  268. friend class QGraphicsItem;
  269. friend class QGraphicsItemPrivate;
  270. friend class QGraphicsObject;
  271. friend class QGraphicsView;
  272. friend class QGraphicsViewPrivate;
  273. friend class QGraphicsWidget;
  274. friend class QGraphicsWidgetPrivate;
  275. friend class QGraphicsEffect;
  276. friend class QGraphicsSceneIndex;
  277. friend class QGraphicsSceneIndexPrivate;
  278. friend class QGraphicsSceneBspTreeIndex;
  279. friend class QGraphicsSceneBspTreeIndexPrivate;
  280. friend class QGraphicsItemEffectSourcePrivate;
  281. #ifndef QT_NO_GESTURES
  282. friend class QGesture;
  283. #endif
  284. };
  285. Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsScene::SceneLayers)
  286. #endif // QT_NO_GRAPHICSVIEW
  287. QT_END_NAMESPACE
  288. #endif