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