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