1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QGRAPHICSITEM_H
5#define QGRAPHICSITEM_H
6
7#include <QtWidgets/qtwidgetsglobal.h>
8#include <QtCore/qobject.h>
9#include <QtCore/qvariant.h>
10#include <QtCore/qrect.h>
11#include <QtCore/qscopedpointer.h>
12#include <QtGui/qpainterpath.h>
13#include <QtGui/qpixmap.h>
14
15class tst_QGraphicsItem;
16
17QT_REQUIRE_CONFIG(graphicsview);
18
19QT_BEGIN_NAMESPACE
20
21class QBrush;
22class QCursor;
23class QFocusEvent;
24class QGraphicsEffect;
25class QGraphicsItemGroup;
26class QGraphicsObject;
27class QGraphicsSceneContextMenuEvent;
28class QGraphicsSceneDragDropEvent;
29class QGraphicsSceneEvent;
30class QGraphicsSceneHoverEvent;
31class QGraphicsSceneMouseEvent;
32class QGraphicsSceneWheelEvent;
33class QGraphicsScene;
34class QGraphicsTransform;
35class QGraphicsWidget;
36class QInputMethodEvent;
37class QKeyEvent;
38class QMenu;
39class QPainter;
40class QPen;
41class QPointF;
42class QRectF;
43class QStyleOptionGraphicsItem;
44
45class QGraphicsItemPrivate;
46class Q_WIDGETS_EXPORT QGraphicsItem
47{
48public:
49 enum GraphicsItemFlag {
50 ItemIsMovable = 0x1,
51 ItemIsSelectable = 0x2,
52 ItemIsFocusable = 0x4,
53 ItemClipsToShape = 0x8,
54 ItemClipsChildrenToShape = 0x10,
55 ItemIgnoresTransformations = 0x20,
56 ItemIgnoresParentOpacity = 0x40,
57 ItemDoesntPropagateOpacityToChildren = 0x80,
58 ItemStacksBehindParent = 0x100,
59 ItemUsesExtendedStyleOption = 0x200,
60 ItemHasNoContents = 0x400,
61 ItemSendsGeometryChanges = 0x800,
62 ItemAcceptsInputMethod = 0x1000,
63 ItemNegativeZStacksBehindParent = 0x2000,
64 ItemIsPanel = 0x4000,
65 ItemIsFocusScope = 0x8000, // internal
66 ItemSendsScenePositionChanges = 0x10000,
67 ItemStopsClickFocusPropagation = 0x20000,
68 ItemStopsFocusHandling = 0x40000,
69 ItemContainsChildrenInShape = 0x80000
70 // NB! Don't forget to increase the d_ptr->flags bit field by 1 when adding a new flag.
71 };
72 Q_DECLARE_FLAGS(GraphicsItemFlags, GraphicsItemFlag)
73
74 enum GraphicsItemChange {
75 ItemPositionChange,
76 ItemVisibleChange = 2,
77 ItemEnabledChange,
78 ItemSelectedChange,
79 ItemParentChange,
80 ItemChildAddedChange,
81 ItemChildRemovedChange,
82 ItemTransformChange,
83 ItemPositionHasChanged,
84 ItemTransformHasChanged,
85 ItemSceneChange,
86 ItemVisibleHasChanged,
87 ItemEnabledHasChanged,
88 ItemSelectedHasChanged,
89 ItemParentHasChanged,
90 ItemSceneHasChanged,
91 ItemCursorChange,
92 ItemCursorHasChanged,
93 ItemToolTipChange,
94 ItemToolTipHasChanged,
95 ItemFlagsChange,
96 ItemFlagsHaveChanged,
97 ItemZValueChange,
98 ItemZValueHasChanged,
99 ItemOpacityChange,
100 ItemOpacityHasChanged,
101 ItemScenePositionHasChanged,
102 ItemRotationChange,
103 ItemRotationHasChanged,
104 ItemScaleChange,
105 ItemScaleHasChanged,
106 ItemTransformOriginPointChange,
107 ItemTransformOriginPointHasChanged
108 };
109
110 enum CacheMode {
111 NoCache,
112 ItemCoordinateCache,
113 DeviceCoordinateCache
114 };
115
116 enum PanelModality
117 {
118 NonModal,
119 PanelModal,
120 SceneModal
121 };
122
123 explicit QGraphicsItem(QGraphicsItem *parent = nullptr);
124 virtual ~QGraphicsItem();
125
126 QGraphicsScene *scene() const;
127
128 QGraphicsItem *parentItem() const;
129 QGraphicsItem *topLevelItem() const;
130 QGraphicsObject *parentObject() const;
131 QGraphicsWidget *parentWidget() const;
132 QGraphicsWidget *topLevelWidget() const;
133 QGraphicsWidget *window() const;
134 QGraphicsItem *panel() const;
135 void setParentItem(QGraphicsItem *parent);
136 QList<QGraphicsItem *> childItems() const;
137 bool isWidget() const;
138 bool isWindow() const;
139 bool isPanel() const;
140
141 QGraphicsObject *toGraphicsObject();
142 const QGraphicsObject *toGraphicsObject() const;
143
144 QGraphicsItemGroup *group() const;
145 void setGroup(QGraphicsItemGroup *group);
146
147 GraphicsItemFlags flags() const;
148 void setFlag(GraphicsItemFlag flag, bool enabled = true);
149 void setFlags(GraphicsItemFlags flags);
150
151 CacheMode cacheMode() const;
152 void setCacheMode(CacheMode mode, const QSize &cacheSize = QSize());
153
154 PanelModality panelModality() const;
155 void setPanelModality(PanelModality panelModality);
156 bool isBlockedByModalPanel(QGraphicsItem **blockingPanel = nullptr) const;
157
158#if QT_CONFIG(tooltip)
159 QString toolTip() const;
160 void setToolTip(const QString &toolTip);
161#endif
162
163#ifndef QT_NO_CURSOR
164 QCursor cursor() const;
165 void setCursor(const QCursor &cursor);
166 bool hasCursor() const;
167 void unsetCursor();
168#endif
169
170 bool isVisible() const;
171 bool isVisibleTo(const QGraphicsItem *parent) const;
172 void setVisible(bool visible);
173 inline void hide() { setVisible(false); }
174 inline void show() { setVisible(true); }
175
176 bool isEnabled() const;
177 void setEnabled(bool enabled);
178
179 bool isSelected() const;
180 void setSelected(bool selected);
181
182 bool acceptDrops() const;
183 void setAcceptDrops(bool on);
184
185 qreal opacity() const;
186 qreal effectiveOpacity() const;
187 void setOpacity(qreal opacity);
188
189#if QT_CONFIG(graphicseffect)
190 // Effect
191 QGraphicsEffect *graphicsEffect() const;
192 void setGraphicsEffect(QGraphicsEffect *effect);
193#endif // QT_CONFIG(graphicseffect)
194
195 Qt::MouseButtons acceptedMouseButtons() const;
196 void setAcceptedMouseButtons(Qt::MouseButtons buttons);
197 bool acceptHoverEvents() const;
198 void setAcceptHoverEvents(bool enabled);
199 bool acceptTouchEvents() const;
200 void setAcceptTouchEvents(bool enabled);
201
202 bool filtersChildEvents() const;
203 void setFiltersChildEvents(bool enabled);
204
205 bool handlesChildEvents() const;
206 void setHandlesChildEvents(bool enabled);
207
208 bool isActive() const;
209 void setActive(bool active);
210
211 bool hasFocus() const;
212 void setFocus(Qt::FocusReason focusReason = Qt::OtherFocusReason);
213 void clearFocus();
214
215 QGraphicsItem *focusProxy() const;
216 void setFocusProxy(QGraphicsItem *item);
217
218 QGraphicsItem *focusItem() const;
219 QGraphicsItem *focusScopeItem() const;
220
221 void grabMouse();
222 void ungrabMouse();
223 void grabKeyboard();
224 void ungrabKeyboard();
225
226 // Positioning in scene coordinates
227 QPointF pos() const;
228 inline qreal x() const { return pos().x(); }
229 void setX(qreal x);
230 inline qreal y() const { return pos().y(); }
231 void setY(qreal y);
232 QPointF scenePos() const;
233 void setPos(const QPointF &pos);
234 inline void setPos(qreal x, qreal y);
235 inline void moveBy(qreal dx, qreal dy) { setPos(x: pos().x() + dx, y: pos().y() + dy); }
236
237 void ensureVisible(const QRectF &rect = QRectF(), int xmargin = 50, int ymargin = 50);
238 inline void ensureVisible(qreal x, qreal y, qreal w, qreal h, int xmargin = 50, int ymargin = 50);
239
240 // Local transformation
241 QTransform transform() const;
242 QTransform sceneTransform() const;
243 QTransform deviceTransform(const QTransform &viewportTransform) const;
244 QTransform itemTransform(const QGraphicsItem *other, bool *ok = nullptr) const;
245 void setTransform(const QTransform &matrix, bool combine = false);
246 void resetTransform();
247 void setRotation(qreal angle);
248 qreal rotation() const;
249
250 void setScale(qreal scale);
251 qreal scale() const;
252
253 QList<QGraphicsTransform *> transformations() const;
254 void setTransformations(const QList<QGraphicsTransform *> &transformations);
255
256 QPointF transformOriginPoint() const;
257 void setTransformOriginPoint(const QPointF &origin);
258 inline void setTransformOriginPoint(qreal ax, qreal ay)
259 { setTransformOriginPoint(QPointF(ax,ay)); }
260
261 virtual void advance(int phase);
262
263 // Stacking order
264 qreal zValue() const;
265 void setZValue(qreal z);
266 void stackBefore(const QGraphicsItem *sibling);
267
268 // Hit test
269 virtual QRectF boundingRect() const = 0;
270 QRectF childrenBoundingRect() const;
271 QRectF sceneBoundingRect() const;
272 virtual QPainterPath shape() const;
273 bool isClipped() const;
274 QPainterPath clipPath() const;
275 virtual bool contains(const QPointF &point) const;
276 virtual bool collidesWithItem(const QGraphicsItem *other, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
277 virtual bool collidesWithPath(const QPainterPath &path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
278 QList<QGraphicsItem *> collidingItems(Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
279 bool isObscured(const QRectF &rect = QRectF()) const;
280 inline bool isObscured(qreal x, qreal y, qreal w, qreal h) const;
281 virtual bool isObscuredBy(const QGraphicsItem *item) const;
282 virtual QPainterPath opaqueArea() const;
283
284 QRegion boundingRegion(const QTransform &itemToDeviceTransform) const;
285 qreal boundingRegionGranularity() const;
286 void setBoundingRegionGranularity(qreal granularity);
287
288 // Drawing
289 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) = 0;
290 void update(const QRectF &rect = QRectF());
291 inline void update(qreal x, qreal y, qreal width, qreal height);
292 void scroll(qreal dx, qreal dy, const QRectF &rect = QRectF());
293
294 // Coordinate mapping
295 QPointF mapToItem(const QGraphicsItem *item, const QPointF &point) const;
296 QPointF mapToParent(const QPointF &point) const;
297 QPointF mapToScene(const QPointF &point) const;
298 QPolygonF mapToItem(const QGraphicsItem *item, const QRectF &rect) const;
299 QPolygonF mapToParent(const QRectF &rect) const;
300 QPolygonF mapToScene(const QRectF &rect) const;
301 QRectF mapRectToItem(const QGraphicsItem *item, const QRectF &rect) const;
302 QRectF mapRectToParent(const QRectF &rect) const;
303 QRectF mapRectToScene(const QRectF &rect) const;
304 QPolygonF mapToItem(const QGraphicsItem *item, const QPolygonF &polygon) const;
305 QPolygonF mapToParent(const QPolygonF &polygon) const;
306 QPolygonF mapToScene(const QPolygonF &polygon) const;
307 QPainterPath mapToItem(const QGraphicsItem *item, const QPainterPath &path) const;
308 QPainterPath mapToParent(const QPainterPath &path) const;
309 QPainterPath mapToScene(const QPainterPath &path) const;
310 QPointF mapFromItem(const QGraphicsItem *item, const QPointF &point) const;
311 QPointF mapFromParent(const QPointF &point) const;
312 QPointF mapFromScene(const QPointF &point) const;
313 QPolygonF mapFromItem(const QGraphicsItem *item, const QRectF &rect) const;
314 QPolygonF mapFromParent(const QRectF &rect) const;
315 QPolygonF mapFromScene(const QRectF &rect) const;
316 QRectF mapRectFromItem(const QGraphicsItem *item, const QRectF &rect) const;
317 QRectF mapRectFromParent(const QRectF &rect) const;
318 QRectF mapRectFromScene(const QRectF &rect) const;
319 QPolygonF mapFromItem(const QGraphicsItem *item, const QPolygonF &polygon) const;
320 QPolygonF mapFromParent(const QPolygonF &polygon) const;
321 QPolygonF mapFromScene(const QPolygonF &polygon) const;
322 QPainterPath mapFromItem(const QGraphicsItem *item, const QPainterPath &path) const;
323 QPainterPath mapFromParent(const QPainterPath &path) const;
324 QPainterPath mapFromScene(const QPainterPath &path) const;
325
326 inline QPointF mapToItem(const QGraphicsItem *item, qreal x, qreal y) const;
327 inline QPointF mapToParent(qreal x, qreal y) const;
328 inline QPointF mapToScene(qreal x, qreal y) const;
329 inline QPolygonF mapToItem(const QGraphicsItem *item, qreal x, qreal y, qreal w, qreal h) const;
330 inline QPolygonF mapToParent(qreal x, qreal y, qreal w, qreal h) const;
331 inline QPolygonF mapToScene(qreal x, qreal y, qreal w, qreal h) const;
332 inline QRectF mapRectToItem(const QGraphicsItem *item, qreal x, qreal y, qreal w, qreal h) const;
333 inline QRectF mapRectToParent(qreal x, qreal y, qreal w, qreal h) const;
334 inline QRectF mapRectToScene(qreal x, qreal y, qreal w, qreal h) const;
335 inline QPointF mapFromItem(const QGraphicsItem *item, qreal x, qreal y) const;
336 inline QPointF mapFromParent(qreal x, qreal y) const;
337 inline QPointF mapFromScene(qreal x, qreal y) const;
338 inline QPolygonF mapFromItem(const QGraphicsItem *item, qreal x, qreal y, qreal w, qreal h) const;
339 inline QPolygonF mapFromParent(qreal x, qreal y, qreal w, qreal h) const;
340 inline QPolygonF mapFromScene(qreal x, qreal y, qreal w, qreal h) const;
341 inline QRectF mapRectFromItem(const QGraphicsItem *item, qreal x, qreal y, qreal w, qreal h) const;
342 inline QRectF mapRectFromParent(qreal x, qreal y, qreal w, qreal h) const;
343 inline QRectF mapRectFromScene(qreal x, qreal y, qreal w, qreal h) const;
344
345 bool isAncestorOf(const QGraphicsItem *child) const;
346 QGraphicsItem *commonAncestorItem(const QGraphicsItem *other) const;
347 bool isUnderMouse() const;
348
349 // Custom data
350 QVariant data(int key) const;
351 void setData(int key, const QVariant &value);
352
353 Qt::InputMethodHints inputMethodHints() const;
354 void setInputMethodHints(Qt::InputMethodHints hints);
355
356 enum {
357 Type = 1,
358 UserType = 65536
359 };
360 virtual int type() const;
361
362 void installSceneEventFilter(QGraphicsItem *filterItem);
363 void removeSceneEventFilter(QGraphicsItem *filterItem);
364
365protected:
366 void updateMicroFocus();
367 virtual bool sceneEventFilter(QGraphicsItem *watched, QEvent *event);
368 virtual bool sceneEvent(QEvent *event);
369 virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
370 virtual void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
371 virtual void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
372 virtual void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
373 virtual void dropEvent(QGraphicsSceneDragDropEvent *event);
374 virtual void focusInEvent(QFocusEvent *event);
375 virtual void focusOutEvent(QFocusEvent *event);
376 virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
377 virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
378 virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
379 virtual void keyPressEvent(QKeyEvent *event);
380 virtual void keyReleaseEvent(QKeyEvent *event);
381 virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
382 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
383 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
384 virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
385 virtual void wheelEvent(QGraphicsSceneWheelEvent *event);
386 virtual void inputMethodEvent(QInputMethodEvent *event);
387 virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
388
389 virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value);
390
391 enum Extension {
392 UserExtension = 0x80000000
393 };
394 virtual bool supportsExtension(Extension extension) const;
395 virtual void setExtension(Extension extension, const QVariant &variant);
396 virtual QVariant extension(const QVariant &variant) const;
397
398protected:
399 QGraphicsItem(QGraphicsItemPrivate &dd, QGraphicsItem *parent);
400 QScopedPointer<QGraphicsItemPrivate> d_ptr;
401
402 void addToIndex();
403 void removeFromIndex();
404 void prepareGeometryChange();
405
406private:
407 Q_DISABLE_COPY(QGraphicsItem)
408 Q_DECLARE_PRIVATE(QGraphicsItem)
409 friend class QGraphicsItemGroup;
410 friend class QGraphicsScene;
411 friend class QGraphicsScenePrivate;
412 friend class QGraphicsSceneFindItemBspTreeVisitor;
413 friend class QGraphicsSceneBspTree;
414 friend class QGraphicsView;
415 friend class QGraphicsViewPrivate;
416 friend class QGraphicsObject;
417 friend class QGraphicsWidget;
418 friend class QGraphicsWidgetPrivate;
419 friend class QGraphicsProxyWidgetPrivate;
420 friend class QGraphicsSceneIndex;
421 friend class QGraphicsSceneIndexPrivate;
422 friend class QGraphicsSceneBspTreeIndex;
423 friend class QGraphicsSceneBspTreeIndexPrivate;
424 friend class QGraphicsItemEffectSourcePrivate;
425 friend class QGraphicsTransformPrivate;
426#ifndef QT_NO_GESTURES
427 friend class QGestureManager;
428#endif
429 friend class ::tst_QGraphicsItem;
430 friend bool qt_closestLeaf(const QGraphicsItem *, const QGraphicsItem *);
431 friend bool qt_closestItemFirst(const QGraphicsItem *, const QGraphicsItem *);
432};
433
434Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsItem::GraphicsItemFlags)
435#ifndef Q_QDOC
436Q_DECLARE_INTERFACE(QGraphicsItem, "org.qt-project.Qt.QGraphicsItem")
437#endif
438
439inline void QGraphicsItem::setPos(qreal ax, qreal ay)
440{ setPos(QPointF(ax, ay)); }
441inline void QGraphicsItem::ensureVisible(qreal ax, qreal ay, qreal w, qreal h, int xmargin, int ymargin)
442{ ensureVisible(rect: QRectF(ax, ay, w, h), xmargin, ymargin); }
443inline void QGraphicsItem::update(qreal ax, qreal ay, qreal width, qreal height)
444{ update(rect: QRectF(ax, ay, width, height)); }
445inline bool QGraphicsItem::isObscured(qreal ax, qreal ay, qreal w, qreal h) const
446{ return isObscured(rect: QRectF(ax, ay, w, h)); }
447inline QPointF QGraphicsItem::mapToItem(const QGraphicsItem *item, qreal ax, qreal ay) const
448{ return mapToItem(item, point: QPointF(ax, ay)); }
449inline QPointF QGraphicsItem::mapToParent(qreal ax, qreal ay) const
450{ return mapToParent(point: QPointF(ax, ay)); }
451inline QPointF QGraphicsItem::mapToScene(qreal ax, qreal ay) const
452{ return mapToScene(point: QPointF(ax, ay)); }
453inline QPointF QGraphicsItem::mapFromItem(const QGraphicsItem *item, qreal ax, qreal ay) const
454{ return mapFromItem(item, point: QPointF(ax, ay)); }
455inline QPointF QGraphicsItem::mapFromParent(qreal ax, qreal ay) const
456{ return mapFromParent(point: QPointF(ax, ay)); }
457inline QPointF QGraphicsItem::mapFromScene(qreal ax, qreal ay) const
458{ return mapFromScene(point: QPointF(ax, ay)); }
459inline QPolygonF QGraphicsItem::mapToItem(const QGraphicsItem *item, qreal ax, qreal ay, qreal w, qreal h) const
460{ return mapToItem(item, rect: QRectF(ax, ay, w, h)); }
461inline QPolygonF QGraphicsItem::mapToParent(qreal ax, qreal ay, qreal w, qreal h) const
462{ return mapToParent(rect: QRectF(ax, ay, w, h)); }
463inline QPolygonF QGraphicsItem::mapToScene(qreal ax, qreal ay, qreal w, qreal h) const
464{ return mapToScene(rect: QRectF(ax, ay, w, h)); }
465inline QRectF QGraphicsItem::mapRectToItem(const QGraphicsItem *item, qreal ax, qreal ay, qreal w, qreal h) const
466{ return mapRectToItem(item, rect: QRectF(ax, ay, w, h)); }
467inline QRectF QGraphicsItem::mapRectToParent(qreal ax, qreal ay, qreal w, qreal h) const
468{ return mapRectToParent(rect: QRectF(ax, ay, w, h)); }
469inline QRectF QGraphicsItem::mapRectToScene(qreal ax, qreal ay, qreal w, qreal h) const
470{ return mapRectToScene(rect: QRectF(ax, ay, w, h)); }
471inline QPolygonF QGraphicsItem::mapFromItem(const QGraphicsItem *item, qreal ax, qreal ay, qreal w, qreal h) const
472{ return mapFromItem(item, rect: QRectF(ax, ay, w, h)); }
473inline QPolygonF QGraphicsItem::mapFromParent(qreal ax, qreal ay, qreal w, qreal h) const
474{ return mapFromParent(rect: QRectF(ax, ay, w, h)); }
475inline QPolygonF QGraphicsItem::mapFromScene(qreal ax, qreal ay, qreal w, qreal h) const
476{ return mapFromScene(rect: QRectF(ax, ay, w, h)); }
477inline QRectF QGraphicsItem::mapRectFromItem(const QGraphicsItem *item, qreal ax, qreal ay, qreal w, qreal h) const
478{ return mapRectFromItem(item, rect: QRectF(ax, ay, w, h)); }
479inline QRectF QGraphicsItem::mapRectFromParent(qreal ax, qreal ay, qreal w, qreal h) const
480{ return mapRectFromParent(rect: QRectF(ax, ay, w, h)); }
481inline QRectF QGraphicsItem::mapRectFromScene(qreal ax, qreal ay, qreal w, qreal h) const
482{ return mapRectFromScene(rect: QRectF(ax, ay, w, h)); }
483
484
485class Q_WIDGETS_EXPORT QGraphicsObject : public QObject, public QGraphicsItem
486{
487 Q_OBJECT
488 Q_PROPERTY(QGraphicsObject* parent READ parentObject WRITE setParentItem NOTIFY parentChanged
489 DESIGNABLE false)
490 Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity NOTIFY opacityChanged FINAL)
491 Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged)
492 Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged FINAL)
493 Q_PROPERTY(QPointF pos READ pos WRITE setPos FINAL)
494 Q_PROPERTY(qreal x READ x WRITE setX NOTIFY xChanged FINAL)
495 Q_PROPERTY(qreal y READ y WRITE setY NOTIFY yChanged FINAL)
496 Q_PROPERTY(qreal z READ zValue WRITE setZValue NOTIFY zChanged FINAL)
497 Q_PROPERTY(qreal rotation READ rotation WRITE setRotation NOTIFY rotationChanged)
498 Q_PROPERTY(qreal scale READ scale WRITE setScale NOTIFY scaleChanged)
499 Q_PROPERTY(QPointF transformOriginPoint READ transformOriginPoint WRITE setTransformOriginPoint)
500#if QT_CONFIG(graphicseffect)
501 Q_PROPERTY(QGraphicsEffect *effect READ graphicsEffect WRITE setGraphicsEffect)
502#endif
503 Q_PRIVATE_PROPERTY(QGraphicsItem::d_func(), qreal width READ width WRITE setWidth
504 NOTIFY widthChanged RESET resetWidth FINAL)
505 Q_PRIVATE_PROPERTY(QGraphicsItem::d_func(), qreal height READ height WRITE setHeight
506 NOTIFY heightChanged RESET resetHeight FINAL)
507 Q_INTERFACES(QGraphicsItem)
508public:
509 explicit QGraphicsObject(QGraphicsItem *parent = nullptr);
510 ~QGraphicsObject();
511
512 using QObject::children;
513
514#ifndef QT_NO_GESTURES
515 void grabGesture(Qt::GestureType type, Qt::GestureFlags flags = Qt::GestureFlags());
516 void ungrabGesture(Qt::GestureType type);
517#endif
518
519protected Q_SLOTS:
520 void updateMicroFocus();
521
522Q_SIGNALS:
523 void parentChanged();
524 void opacityChanged();
525 void visibleChanged();
526 void enabledChanged();
527 void xChanged();
528 void yChanged();
529 void zChanged();
530 void rotationChanged();
531 void scaleChanged();
532 void childrenChanged();
533 void widthChanged();
534 void heightChanged();
535
536protected:
537 QGraphicsObject(QGraphicsItemPrivate &dd, QGraphicsItem *parent);
538
539 bool event(QEvent *ev) override;
540
541private:
542 friend class QGraphicsItem;
543 friend class QGraphicsItemPrivate;
544};
545
546
547class QAbstractGraphicsShapeItemPrivate;
548class Q_WIDGETS_EXPORT QAbstractGraphicsShapeItem : public QGraphicsItem
549{
550public:
551 explicit QAbstractGraphicsShapeItem(QGraphicsItem *parent = nullptr);
552 ~QAbstractGraphicsShapeItem();
553
554 QPen pen() const;
555 void setPen(const QPen &pen);
556
557 QBrush brush() const;
558 void setBrush(const QBrush &brush);
559
560 bool isObscuredBy(const QGraphicsItem *item) const override;
561 QPainterPath opaqueArea() const override;
562
563protected:
564 QAbstractGraphicsShapeItem(QAbstractGraphicsShapeItemPrivate &dd,
565 QGraphicsItem *parent);
566
567private:
568 Q_DISABLE_COPY(QAbstractGraphicsShapeItem)
569 Q_DECLARE_PRIVATE(QAbstractGraphicsShapeItem)
570};
571
572class QGraphicsPathItemPrivate;
573class Q_WIDGETS_EXPORT QGraphicsPathItem : public QAbstractGraphicsShapeItem
574{
575public:
576 explicit QGraphicsPathItem(QGraphicsItem *parent = nullptr);
577 explicit QGraphicsPathItem(const QPainterPath &path, QGraphicsItem *parent = nullptr);
578 ~QGraphicsPathItem();
579
580 QPainterPath path() const;
581 void setPath(const QPainterPath &path);
582
583 QRectF boundingRect() const override;
584 QPainterPath shape() const override;
585 bool contains(const QPointF &point) const override;
586
587 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
588
589 bool isObscuredBy(const QGraphicsItem *item) const override;
590 QPainterPath opaqueArea() const override;
591
592 enum { Type = 2 };
593 int type() const override;
594
595protected:
596 bool supportsExtension(Extension extension) const override;
597 void setExtension(Extension extension, const QVariant &variant) override;
598 QVariant extension(const QVariant &variant) const override;
599
600private:
601 Q_DISABLE_COPY(QGraphicsPathItem)
602 Q_DECLARE_PRIVATE(QGraphicsPathItem)
603};
604
605class QGraphicsRectItemPrivate;
606class Q_WIDGETS_EXPORT QGraphicsRectItem : public QAbstractGraphicsShapeItem
607{
608public:
609 explicit QGraphicsRectItem(QGraphicsItem *parent = nullptr);
610 explicit QGraphicsRectItem(const QRectF &rect, QGraphicsItem *parent = nullptr);
611 explicit QGraphicsRectItem(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent = nullptr);
612 ~QGraphicsRectItem();
613
614 QRectF rect() const;
615 void setRect(const QRectF &rect);
616 inline void setRect(qreal x, qreal y, qreal w, qreal h);
617
618 QRectF boundingRect() const override;
619 QPainterPath shape() const override;
620 bool contains(const QPointF &point) const override;
621
622 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
623
624 bool isObscuredBy(const QGraphicsItem *item) const override;
625 QPainterPath opaqueArea() const override;
626
627 enum { Type = 3 };
628 int type() const override;
629
630protected:
631 bool supportsExtension(Extension extension) const override;
632 void setExtension(Extension extension, const QVariant &variant) override;
633 QVariant extension(const QVariant &variant) const override;
634
635private:
636 Q_DISABLE_COPY(QGraphicsRectItem)
637 Q_DECLARE_PRIVATE(QGraphicsRectItem)
638};
639
640inline void QGraphicsRectItem::setRect(qreal ax, qreal ay, qreal w, qreal h)
641{ setRect(QRectF(ax, ay, w, h)); }
642
643class QGraphicsEllipseItemPrivate;
644class Q_WIDGETS_EXPORT QGraphicsEllipseItem : public QAbstractGraphicsShapeItem
645{
646public:
647 explicit QGraphicsEllipseItem(QGraphicsItem *parent = nullptr);
648 explicit QGraphicsEllipseItem(const QRectF &rect, QGraphicsItem *parent = nullptr);
649 explicit QGraphicsEllipseItem(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent = nullptr);
650 ~QGraphicsEllipseItem();
651
652 QRectF rect() const;
653 void setRect(const QRectF &rect);
654 inline void setRect(qreal x, qreal y, qreal w, qreal h);
655
656 int startAngle() const;
657 void setStartAngle(int angle);
658
659 int spanAngle() const;
660 void setSpanAngle(int angle);
661
662 QRectF boundingRect() const override;
663 QPainterPath shape() const override;
664 bool contains(const QPointF &point) const override;
665
666 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
667
668 bool isObscuredBy(const QGraphicsItem *item) const override;
669 QPainterPath opaqueArea() const override;
670
671 enum { Type = 4 };
672 int type() const override;
673
674protected:
675 bool supportsExtension(Extension extension) const override;
676 void setExtension(Extension extension, const QVariant &variant) override;
677 QVariant extension(const QVariant &variant) const override;
678
679private:
680 Q_DISABLE_COPY(QGraphicsEllipseItem)
681 Q_DECLARE_PRIVATE(QGraphicsEllipseItem)
682};
683
684inline void QGraphicsEllipseItem::setRect(qreal ax, qreal ay, qreal w, qreal h)
685{ setRect(QRectF(ax, ay, w, h)); }
686
687class QGraphicsPolygonItemPrivate;
688class Q_WIDGETS_EXPORT QGraphicsPolygonItem : public QAbstractGraphicsShapeItem
689{
690public:
691 explicit QGraphicsPolygonItem(QGraphicsItem *parent = nullptr);
692 explicit QGraphicsPolygonItem(const QPolygonF &polygon,
693 QGraphicsItem *parent = nullptr);
694 ~QGraphicsPolygonItem();
695
696 QPolygonF polygon() const;
697 void setPolygon(const QPolygonF &polygon);
698
699 Qt::FillRule fillRule() const;
700 void setFillRule(Qt::FillRule rule);
701
702 QRectF boundingRect() const override;
703 QPainterPath shape() const override;
704 bool contains(const QPointF &point) const override;
705
706 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
707
708 bool isObscuredBy(const QGraphicsItem *item) const override;
709 QPainterPath opaqueArea() const override;
710
711 enum { Type = 5 };
712 int type() const override;
713
714protected:
715 bool supportsExtension(Extension extension) const override;
716 void setExtension(Extension extension, const QVariant &variant) override;
717 QVariant extension(const QVariant &variant) const override;
718
719private:
720 Q_DISABLE_COPY(QGraphicsPolygonItem)
721 Q_DECLARE_PRIVATE(QGraphicsPolygonItem)
722};
723
724class QGraphicsLineItemPrivate;
725class Q_WIDGETS_EXPORT QGraphicsLineItem : public QGraphicsItem
726{
727public:
728 explicit QGraphicsLineItem(QGraphicsItem *parent = nullptr);
729 explicit QGraphicsLineItem(const QLineF &line, QGraphicsItem *parent = nullptr);
730 explicit QGraphicsLineItem(qreal x1, qreal y1, qreal x2, qreal y2, QGraphicsItem *parent = nullptr);
731 ~QGraphicsLineItem();
732
733 QPen pen() const;
734 void setPen(const QPen &pen);
735
736 QLineF line() const;
737 void setLine(const QLineF &line);
738 inline void setLine(qreal x1, qreal y1, qreal x2, qreal y2)
739 { setLine(QLineF(x1, y1, x2, y2)); }
740
741 QRectF boundingRect() const override;
742 QPainterPath shape() const override;
743 bool contains(const QPointF &point) const override;
744
745 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
746
747 bool isObscuredBy(const QGraphicsItem *item) const override;
748 QPainterPath opaqueArea() const override;
749
750 enum { Type = 6 };
751 int type() const override;
752
753protected:
754 bool supportsExtension(Extension extension) const override;
755 void setExtension(Extension extension, const QVariant &variant) override;
756 QVariant extension(const QVariant &variant) const override;
757
758private:
759 Q_DISABLE_COPY(QGraphicsLineItem)
760 Q_DECLARE_PRIVATE(QGraphicsLineItem)
761};
762
763class QGraphicsPixmapItemPrivate;
764class Q_WIDGETS_EXPORT QGraphicsPixmapItem : public QGraphicsItem
765{
766public:
767 enum ShapeMode {
768 MaskShape,
769 BoundingRectShape,
770 HeuristicMaskShape
771 };
772
773 explicit QGraphicsPixmapItem(QGraphicsItem *parent = nullptr);
774 explicit QGraphicsPixmapItem(const QPixmap &pixmap, QGraphicsItem *parent = nullptr);
775 ~QGraphicsPixmapItem();
776
777 QPixmap pixmap() const;
778 void setPixmap(const QPixmap &pixmap);
779
780 Qt::TransformationMode transformationMode() const;
781 void setTransformationMode(Qt::TransformationMode mode);
782
783 QPointF offset() const;
784 void setOffset(const QPointF &offset);
785 inline void setOffset(qreal x, qreal y);
786
787 QRectF boundingRect() const override;
788 QPainterPath shape() const override;
789 bool contains(const QPointF &point) const override;
790
791 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
792
793 bool isObscuredBy(const QGraphicsItem *item) const override;
794 QPainterPath opaqueArea() const override;
795
796 enum { Type = 7 };
797 int type() const override;
798
799 ShapeMode shapeMode() const;
800 void setShapeMode(ShapeMode mode);
801
802protected:
803 bool supportsExtension(Extension extension) const override;
804 void setExtension(Extension extension, const QVariant &variant) override;
805 QVariant extension(const QVariant &variant) const override;
806
807private:
808 Q_DISABLE_COPY(QGraphicsPixmapItem)
809 Q_DECLARE_PRIVATE(QGraphicsPixmapItem)
810};
811
812inline void QGraphicsPixmapItem::setOffset(qreal ax, qreal ay)
813{ setOffset(QPointF(ax, ay)); }
814
815class QGraphicsTextItemPrivate;
816class QTextDocument;
817class QTextCursor;
818class Q_WIDGETS_EXPORT QGraphicsTextItem : public QGraphicsObject
819{
820 Q_OBJECT
821 QDOC_PROPERTY(bool openExternalLinks READ openExternalLinks WRITE setOpenExternalLinks)
822 QDOC_PROPERTY(QTextCursor textCursor READ textCursor WRITE setTextCursor)
823
824public:
825 explicit QGraphicsTextItem(QGraphicsItem *parent = nullptr);
826 explicit QGraphicsTextItem(const QString &text, QGraphicsItem *parent = nullptr);
827 ~QGraphicsTextItem();
828
829 QString toHtml() const;
830 void setHtml(const QString &html);
831
832 QString toPlainText() const;
833 void setPlainText(const QString &text);
834
835 QFont font() const;
836 void setFont(const QFont &font);
837
838 void setDefaultTextColor(const QColor &c);
839 QColor defaultTextColor() const;
840
841 QRectF boundingRect() const override;
842 QPainterPath shape() const override;
843 bool contains(const QPointF &point) const override;
844
845 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
846
847 bool isObscuredBy(const QGraphicsItem *item) const override;
848 QPainterPath opaqueArea() const override;
849
850 enum { Type = 8 };
851 int type() const override;
852
853 void setTextWidth(qreal width);
854 qreal textWidth() const;
855
856 void adjustSize();
857
858 void setDocument(QTextDocument *document);
859 QTextDocument *document() const;
860
861 void setTextInteractionFlags(Qt::TextInteractionFlags flags);
862 Qt::TextInteractionFlags textInteractionFlags() const;
863
864 void setTabChangesFocus(bool b);
865 bool tabChangesFocus() const;
866
867 void setOpenExternalLinks(bool open);
868 bool openExternalLinks() const;
869
870 void setTextCursor(const QTextCursor &cursor);
871 QTextCursor textCursor() const;
872
873Q_SIGNALS:
874 void linkActivated(const QString &);
875 void linkHovered(const QString &);
876
877protected:
878 bool sceneEvent(QEvent *event) override;
879 void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
880 void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
881 void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
882 void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;
883 void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
884 void keyPressEvent(QKeyEvent *event) override;
885 void keyReleaseEvent(QKeyEvent *event) override;
886 void focusInEvent(QFocusEvent *event) override;
887 void focusOutEvent(QFocusEvent *event) override;
888 void dragEnterEvent(QGraphicsSceneDragDropEvent *event) override;
889 void dragLeaveEvent(QGraphicsSceneDragDropEvent *event) override;
890 void dragMoveEvent(QGraphicsSceneDragDropEvent *event) override;
891 void dropEvent(QGraphicsSceneDragDropEvent *event) override;
892 void inputMethodEvent(QInputMethodEvent *event) override;
893 void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
894 void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override;
895 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
896
897 QVariant inputMethodQuery(Qt::InputMethodQuery query) const override;
898
899 bool supportsExtension(Extension extension) const override;
900 void setExtension(Extension extension, const QVariant &variant) override;
901 QVariant extension(const QVariant &variant) const override;
902
903private:
904 Q_DISABLE_COPY(QGraphicsTextItem)
905 QGraphicsTextItemPrivate *dd;
906 friend class QGraphicsTextItemPrivate;
907};
908
909class QGraphicsSimpleTextItemPrivate;
910class Q_WIDGETS_EXPORT QGraphicsSimpleTextItem : public QAbstractGraphicsShapeItem
911{
912public:
913 explicit QGraphicsSimpleTextItem(QGraphicsItem *parent = nullptr);
914 explicit QGraphicsSimpleTextItem(const QString &text, QGraphicsItem *parent = nullptr);
915 ~QGraphicsSimpleTextItem();
916
917 void setText(const QString &text);
918 QString text() const;
919
920 void setFont(const QFont &font);
921 QFont font() const;
922
923 QRectF boundingRect() const override;
924 QPainterPath shape() const override;
925 bool contains(const QPointF &point) const override;
926
927 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
928
929 bool isObscuredBy(const QGraphicsItem *item) const override;
930 QPainterPath opaqueArea() const override;
931
932 enum { Type = 9 };
933 int type() const override;
934
935protected:
936 bool supportsExtension(Extension extension) const override;
937 void setExtension(Extension extension, const QVariant &variant) override;
938 QVariant extension(const QVariant &variant) const override;
939
940private:
941 Q_DISABLE_COPY(QGraphicsSimpleTextItem)
942 Q_DECLARE_PRIVATE(QGraphicsSimpleTextItem)
943};
944
945class QGraphicsItemGroupPrivate;
946class Q_WIDGETS_EXPORT QGraphicsItemGroup : public QGraphicsItem
947{
948public:
949 explicit QGraphicsItemGroup(QGraphicsItem *parent = nullptr);
950 ~QGraphicsItemGroup();
951
952 void addToGroup(QGraphicsItem *item);
953 void removeFromGroup(QGraphicsItem *item);
954
955 QRectF boundingRect() const override;
956 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
957
958 bool isObscuredBy(const QGraphicsItem *item) const override;
959 QPainterPath opaqueArea() const override;
960
961 enum { Type = 10 };
962 int type() const override;
963
964private:
965 Q_DISABLE_COPY(QGraphicsItemGroup)
966 Q_DECLARE_PRIVATE(QGraphicsItemGroup)
967};
968
969template <class T> inline T qgraphicsitem_cast(QGraphicsItem *item)
970{
971 typedef typename std::remove_cv<typename std::remove_pointer<T>::type>::type Item;
972 return int(Item::Type) == int(QGraphicsItem::Type)
973 || (item && int(Item::Type) == item->type()) ? static_cast<T>(item) : nullptr;
974}
975
976template <class T> inline T qgraphicsitem_cast(const QGraphicsItem *item)
977{
978 typedef typename std::remove_cv<typename std::remove_pointer<T>::type>::type Item;
979 return int(Item::Type) == int(QGraphicsItem::Type)
980 || (item && int(Item::Type) == item->type()) ? static_cast<T>(item) : nullptr;
981}
982
983#ifndef QT_NO_DEBUG_STREAM
984Q_WIDGETS_EXPORT QDebug operator<<(QDebug debug, const QGraphicsItem *item);
985Q_WIDGETS_EXPORT QDebug operator<<(QDebug debug, const QGraphicsObject *item);
986Q_WIDGETS_EXPORT QDebug operator<<(QDebug debug, QGraphicsItem::GraphicsItemChange change);
987Q_WIDGETS_EXPORT QDebug operator<<(QDebug debug, QGraphicsItem::GraphicsItemFlag flag);
988Q_WIDGETS_EXPORT QDebug operator<<(QDebug debug, QGraphicsItem::GraphicsItemFlags flags);
989#endif
990
991QT_END_NAMESPACE
992
993QT_DECL_METATYPE_EXTERN_TAGGED(QGraphicsItem*, QGraphicsItem_ptr, Q_WIDGETS_EXPORT)
994
995#endif // QGRAPHICSITEM_H
996

source code of qtbase/src/widgets/graphicsview/qgraphicsitem.h