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

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