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