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 QMatrix matrix() const;
287 QMatrix sceneMatrix() const;
288 void setMatrix(const QMatrix &matrix, bool combine = false);
289 void resetMatrix();
290 QTransform transform() const;
291 QTransform sceneTransform() const;
292 QTransform deviceTransform(const QTransform &viewportTransform) const;
293 QTransform itemTransform(const QGraphicsItem *other, bool *ok = nullptr) const;
294 void setTransform(const QTransform &matrix, bool combine = false);
295 void resetTransform();
296#if QT_DEPRECATED_SINCE(5, 0)
297 QT_DEPRECATED inline void rotate(qreal angle) { setTransform(QTransform().rotate(angle), true); }
298 QT_DEPRECATED inline void scale(qreal sx, qreal sy) { setTransform(QTransform::fromScale(sx, sy), true); }
299 QT_DEPRECATED inline void shear(qreal sh, qreal sv) { setTransform(QTransform().shear(sh, sv), true); }
300 QT_DEPRECATED inline void translate(qreal dx, qreal dy) { setTransform(QTransform::fromTranslate(dx, dy), true); }
301#endif
302 void setRotation(qreal angle);
303 qreal rotation() const;
304
305 void setScale(qreal scale);
306 qreal scale() const;
307
308 QList<QGraphicsTransform *> transformations() const;
309 void setTransformations(const QList<QGraphicsTransform *> &transformations);
310
311 QPointF transformOriginPoint() const;
312 void setTransformOriginPoint(const QPointF &origin);
313 inline void setTransformOriginPoint(qreal ax, qreal ay)
314 { setTransformOriginPoint(QPointF(ax,ay)); }
315
316 virtual void advance(int phase);
317
318 // Stacking order
319 qreal zValue() const;
320 void setZValue(qreal z);
321 void stackBefore(const QGraphicsItem *sibling);
322
323 // Hit test
324 virtual QRectF boundingRect() const = 0;
325 QRectF childrenBoundingRect() const;
326 QRectF sceneBoundingRect() const;
327 virtual QPainterPath shape() const;
328 bool isClipped() const;
329 QPainterPath clipPath() const;
330 virtual bool contains(const QPointF &point) const;
331 virtual bool collidesWithItem(const QGraphicsItem *other, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
332 virtual bool collidesWithPath(const QPainterPath &path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
333 QList<QGraphicsItem *> collidingItems(Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
334 bool isObscured(const QRectF &rect = QRectF()) const;
335 inline bool isObscured(qreal x, qreal y, qreal w, qreal h) const;
336 virtual bool isObscuredBy(const QGraphicsItem *item) const;
337 virtual QPainterPath opaqueArea() const;
338
339 QRegion boundingRegion(const QTransform &itemToDeviceTransform) const;
340 qreal boundingRegionGranularity() const;
341 void setBoundingRegionGranularity(qreal granularity);
342
343 // Drawing
344 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) = 0;
345 void update(const QRectF &rect = QRectF());
346 inline void update(qreal x, qreal y, qreal width, qreal height);
347 void scroll(qreal dx, qreal dy, const QRectF &rect = QRectF());
348
349 // Coordinate mapping
350 QPointF mapToItem(const QGraphicsItem *item, const QPointF &point) const;
351 QPointF mapToParent(const QPointF &point) const;
352 QPointF mapToScene(const QPointF &point) const;
353 QPolygonF mapToItem(const QGraphicsItem *item, const QRectF &rect) const;
354 QPolygonF mapToParent(const QRectF &rect) const;
355 QPolygonF mapToScene(const QRectF &rect) const;
356 QRectF mapRectToItem(const QGraphicsItem *item, const QRectF &rect) const;
357 QRectF mapRectToParent(const QRectF &rect) const;
358 QRectF mapRectToScene(const QRectF &rect) const;
359 QPolygonF mapToItem(const QGraphicsItem *item, const QPolygonF &polygon) const;
360 QPolygonF mapToParent(const QPolygonF &polygon) const;
361 QPolygonF mapToScene(const QPolygonF &polygon) const;
362 QPainterPath mapToItem(const QGraphicsItem *item, const QPainterPath &path) const;
363 QPainterPath mapToParent(const QPainterPath &path) const;
364 QPainterPath mapToScene(const QPainterPath &path) const;
365 QPointF mapFromItem(const QGraphicsItem *item, const QPointF &point) const;
366 QPointF mapFromParent(const QPointF &point) const;
367 QPointF mapFromScene(const QPointF &point) const;
368 QPolygonF mapFromItem(const QGraphicsItem *item, const QRectF &rect) const;
369 QPolygonF mapFromParent(const QRectF &rect) const;
370 QPolygonF mapFromScene(const QRectF &rect) const;
371 QRectF mapRectFromItem(const QGraphicsItem *item, const QRectF &rect) const;
372 QRectF mapRectFromParent(const QRectF &rect) const;
373 QRectF mapRectFromScene(const QRectF &rect) const;
374 QPolygonF mapFromItem(const QGraphicsItem *item, const QPolygonF &polygon) const;
375 QPolygonF mapFromParent(const QPolygonF &polygon) const;
376 QPolygonF mapFromScene(const QPolygonF &polygon) const;
377 QPainterPath mapFromItem(const QGraphicsItem *item, const QPainterPath &path) const;
378 QPainterPath mapFromParent(const QPainterPath &path) const;
379 QPainterPath mapFromScene(const QPainterPath &path) const;
380
381 inline QPointF mapToItem(const QGraphicsItem *item, qreal x, qreal y) const;
382 inline QPointF mapToParent(qreal x, qreal y) const;
383 inline QPointF mapToScene(qreal x, qreal y) const;
384 inline QPolygonF mapToItem(const QGraphicsItem *item, qreal x, qreal y, qreal w, qreal h) const;
385 inline QPolygonF mapToParent(qreal x, qreal y, qreal w, qreal h) const;
386 inline QPolygonF mapToScene(qreal x, qreal y, qreal w, qreal h) const;
387 inline QRectF mapRectToItem(const QGraphicsItem *item, qreal x, qreal y, qreal w, qreal h) const;
388 inline QRectF mapRectToParent(qreal x, qreal y, qreal w, qreal h) const;
389 inline QRectF mapRectToScene(qreal x, qreal y, qreal w, qreal h) const;
390 inline QPointF mapFromItem(const QGraphicsItem *item, qreal x, qreal y) const;
391 inline QPointF mapFromParent(qreal x, qreal y) const;
392 inline QPointF mapFromScene(qreal x, qreal y) const;
393 inline QPolygonF mapFromItem(const QGraphicsItem *item, qreal x, qreal y, qreal w, qreal h) const;
394 inline QPolygonF mapFromParent(qreal x, qreal y, qreal w, qreal h) const;
395 inline QPolygonF mapFromScene(qreal x, qreal y, qreal w, qreal h) const;
396 inline QRectF mapRectFromItem(const QGraphicsItem *item, qreal x, qreal y, qreal w, qreal h) const;
397 inline QRectF mapRectFromParent(qreal x, qreal y, qreal w, qreal h) const;
398 inline QRectF mapRectFromScene(qreal x, qreal y, qreal w, qreal h) const;
399
400 bool isAncestorOf(const QGraphicsItem *child) const;
401 QGraphicsItem *commonAncestorItem(const QGraphicsItem *other) const;
402 bool isUnderMouse() const;
403
404 // Custom data
405 QVariant data(int key) const;
406 void setData(int key, const QVariant &value);
407
408 Qt::InputMethodHints inputMethodHints() const;
409 void setInputMethodHints(Qt::InputMethodHints hints);
410
411 enum {
412 Type = 1,
413 UserType = 65536
414 };
415 virtual int type() const;
416
417 void installSceneEventFilter(QGraphicsItem *filterItem);
418 void removeSceneEventFilter(QGraphicsItem *filterItem);
419
420protected:
421 void updateMicroFocus();
422 virtual bool sceneEventFilter(QGraphicsItem *watched, QEvent *event);
423 virtual bool sceneEvent(QEvent *event);
424 virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
425 virtual void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
426 virtual void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
427 virtual void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
428 virtual void dropEvent(QGraphicsSceneDragDropEvent *event);
429 virtual void focusInEvent(QFocusEvent *event);
430 virtual void focusOutEvent(QFocusEvent *event);
431 virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
432 virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
433 virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
434 virtual void keyPressEvent(QKeyEvent *event);
435 virtual void keyReleaseEvent(QKeyEvent *event);
436 virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
437 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
438 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
439 virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
440 virtual void wheelEvent(QGraphicsSceneWheelEvent *event);
441 virtual void inputMethodEvent(QInputMethodEvent *event);
442 virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
443
444 virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value);
445
446 enum Extension {
447 UserExtension = 0x80000000
448 };
449 virtual bool supportsExtension(Extension extension) const;
450 virtual void setExtension(Extension extension, const QVariant &variant);
451 virtual QVariant extension(const QVariant &variant) const;
452
453protected:
454 QGraphicsItem(QGraphicsItemPrivate &dd, QGraphicsItem *parent);
455 QScopedPointer<QGraphicsItemPrivate> d_ptr;
456
457 void addToIndex();
458 void removeFromIndex();
459 void prepareGeometryChange();
460
461private:
462 Q_DISABLE_COPY(QGraphicsItem)
463 Q_DECLARE_PRIVATE(QGraphicsItem)
464 friend class QGraphicsItemGroup;
465 friend class QGraphicsScene;
466 friend class QGraphicsScenePrivate;
467 friend class QGraphicsSceneFindItemBspTreeVisitor;
468 friend class QGraphicsSceneBspTree;
469 friend class QGraphicsView;
470 friend class QGraphicsViewPrivate;
471 friend class QGraphicsObject;
472 friend class QGraphicsWidget;
473 friend class QGraphicsWidgetPrivate;
474 friend class QGraphicsProxyWidgetPrivate;
475 friend class QGraphicsSceneIndex;
476 friend class QGraphicsSceneIndexPrivate;
477 friend class QGraphicsSceneBspTreeIndex;
478 friend class QGraphicsSceneBspTreeIndexPrivate;
479 friend class QGraphicsItemEffectSourcePrivate;
480 friend class QGraphicsTransformPrivate;
481#ifndef QT_NO_GESTURES
482 friend class QGestureManager;
483#endif
484 friend class ::tst_QGraphicsItem;
485 friend bool qt_closestLeaf(const QGraphicsItem *, const QGraphicsItem *);
486 friend bool qt_closestItemFirst(const QGraphicsItem *, const QGraphicsItem *);
487};
488
489Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsItem::GraphicsItemFlags)
490Q_DECLARE_INTERFACE(QGraphicsItem, "org.qt-project.Qt.QGraphicsItem")
491
492inline void QGraphicsItem::setPos(qreal ax, qreal ay)
493{ setPos(QPointF(ax, ay)); }
494inline void QGraphicsItem::ensureVisible(qreal ax, qreal ay, qreal w, qreal h, int xmargin, int ymargin)
495{ ensureVisible(QRectF(ax, ay, w, h), xmargin, ymargin); }
496inline void QGraphicsItem::update(qreal ax, qreal ay, qreal width, qreal height)
497{ update(QRectF(ax, ay, width, height)); }
498inline bool QGraphicsItem::isObscured(qreal ax, qreal ay, qreal w, qreal h) const
499{ return isObscured(QRectF(ax, ay, w, h)); }
500inline QPointF QGraphicsItem::mapToItem(const QGraphicsItem *item, qreal ax, qreal ay) const
501{ return mapToItem(item, QPointF(ax, ay)); }
502inline QPointF QGraphicsItem::mapToParent(qreal ax, qreal ay) const
503{ return mapToParent(QPointF(ax, ay)); }
504inline QPointF QGraphicsItem::mapToScene(qreal ax, qreal ay) const
505{ return mapToScene(QPointF(ax, ay)); }
506inline QPointF QGraphicsItem::mapFromItem(const QGraphicsItem *item, qreal ax, qreal ay) const
507{ return mapFromItem(item, QPointF(ax, ay)); }
508inline QPointF QGraphicsItem::mapFromParent(qreal ax, qreal ay) const
509{ return mapFromParent(QPointF(ax, ay)); }
510inline QPointF QGraphicsItem::mapFromScene(qreal ax, qreal ay) const
511{ return mapFromScene(QPointF(ax, ay)); }
512inline QPolygonF QGraphicsItem::mapToItem(const QGraphicsItem *item, qreal ax, qreal ay, qreal w, qreal h) const
513{ return mapToItem(item, QRectF(ax, ay, w, h)); }
514inline QPolygonF QGraphicsItem::mapToParent(qreal ax, qreal ay, qreal w, qreal h) const
515{ return mapToParent(QRectF(ax, ay, w, h)); }
516inline QPolygonF QGraphicsItem::mapToScene(qreal ax, qreal ay, qreal w, qreal h) const
517{ return mapToScene(QRectF(ax, ay, w, h)); }
518inline QRectF QGraphicsItem::mapRectToItem(const QGraphicsItem *item, qreal ax, qreal ay, qreal w, qreal h) const
519{ return mapRectToItem(item, QRectF(ax, ay, w, h)); }
520inline QRectF QGraphicsItem::mapRectToParent(qreal ax, qreal ay, qreal w, qreal h) const
521{ return mapRectToParent(QRectF(ax, ay, w, h)); }
522inline QRectF QGraphicsItem::mapRectToScene(qreal ax, qreal ay, qreal w, qreal h) const
523{ return mapRectToScene(QRectF(ax, ay, w, h)); }
524inline QPolygonF QGraphicsItem::mapFromItem(const QGraphicsItem *item, qreal ax, qreal ay, qreal w, qreal h) const
525{ return mapFromItem(item, QRectF(ax, ay, w, h)); }
526inline QPolygonF QGraphicsItem::mapFromParent(qreal ax, qreal ay, qreal w, qreal h) const
527{ return mapFromParent(QRectF(ax, ay, w, h)); }
528inline QPolygonF QGraphicsItem::mapFromScene(qreal ax, qreal ay, qreal w, qreal h) const
529{ return mapFromScene(QRectF(ax, ay, w, h)); }
530inline QRectF QGraphicsItem::mapRectFromItem(const QGraphicsItem *item, qreal ax, qreal ay, qreal w, qreal h) const
531{ return mapRectFromItem(item, QRectF(ax, ay, w, h)); }
532inline QRectF QGraphicsItem::mapRectFromParent(qreal ax, qreal ay, qreal w, qreal h) const
533{ return mapRectFromParent(QRectF(ax, ay, w, h)); }
534inline QRectF QGraphicsItem::mapRectFromScene(qreal ax, qreal ay, qreal w, qreal h) const
535{ return mapRectFromScene(QRectF(ax, ay, w, h)); }
536
537
538class Q_WIDGETS_EXPORT QGraphicsObject : public QObject, public QGraphicsItem
539{
540 Q_OBJECT
541 Q_PROPERTY(QGraphicsObject* parent READ parentObject WRITE setParentItem NOTIFY parentChanged DESIGNABLE false)
542 Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity NOTIFY opacityChanged FINAL)
543 Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged)
544 Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged FINAL)
545 Q_PROPERTY(QPointF pos READ pos WRITE setPos FINAL)
546 Q_PROPERTY(qreal x READ x WRITE setX NOTIFY xChanged FINAL)
547 Q_PROPERTY(qreal y READ y WRITE setY NOTIFY yChanged FINAL)
548 Q_PROPERTY(qreal z READ zValue WRITE setZValue NOTIFY zChanged FINAL)
549 Q_PROPERTY(qreal rotation READ rotation WRITE setRotation NOTIFY rotationChanged)
550 Q_PROPERTY(qreal scale READ scale WRITE setScale NOTIFY scaleChanged)
551 Q_PROPERTY(QPointF transformOriginPoint READ transformOriginPoint WRITE setTransformOriginPoint)
552#if QT_CONFIG(graphicseffect)
553 Q_PROPERTY(QGraphicsEffect *effect READ graphicsEffect WRITE setGraphicsEffect)
554#endif
555 Q_PRIVATE_PROPERTY(QGraphicsItem::d_func(), QDeclarativeListProperty<QGraphicsObject> children READ childrenList DESIGNABLE false NOTIFY childrenChanged)
556 Q_PRIVATE_PROPERTY(QGraphicsItem::d_func(), qreal width READ width WRITE setWidth NOTIFY widthChanged RESET resetWidth FINAL)
557 Q_PRIVATE_PROPERTY(QGraphicsItem::d_func(), qreal height READ height WRITE setHeight NOTIFY heightChanged RESET resetHeight FINAL)
558 Q_CLASSINFO("DefaultProperty", "children")
559 Q_INTERFACES(QGraphicsItem)
560public:
561 explicit QGraphicsObject(QGraphicsItem *parent = nullptr);
562 ~QGraphicsObject();
563
564 using QObject::children;
565
566#ifndef QT_NO_GESTURES
567 void grabGesture(Qt::GestureType type, Qt::GestureFlags flags = Qt::GestureFlags());
568 void ungrabGesture(Qt::GestureType type);
569#endif
570
571protected Q_SLOTS:
572 void updateMicroFocus();
573
574Q_SIGNALS:
575 void parentChanged();
576 void opacityChanged();
577 void visibleChanged();
578 void enabledChanged();
579 void xChanged();
580 void yChanged();
581 void zChanged();
582 void rotationChanged();
583 void scaleChanged();
584 void childrenChanged();
585 void widthChanged();
586 void heightChanged();
587
588protected:
589 QGraphicsObject(QGraphicsItemPrivate &dd, QGraphicsItem *parent);
590
591 bool event(QEvent *ev) override;
592
593private:
594 friend class QGraphicsItem;
595 friend class QGraphicsItemPrivate;
596};
597
598
599class QAbstractGraphicsShapeItemPrivate;
600class Q_WIDGETS_EXPORT QAbstractGraphicsShapeItem : public QGraphicsItem
601{
602public:
603 explicit QAbstractGraphicsShapeItem(QGraphicsItem *parent = nullptr);
604 ~QAbstractGraphicsShapeItem();
605
606 QPen pen() const;
607 void setPen(const QPen &pen);
608
609 QBrush brush() const;
610 void setBrush(const QBrush &brush);
611
612 bool isObscuredBy(const QGraphicsItem *item) const override;
613 QPainterPath opaqueArea() const override;
614
615protected:
616 QAbstractGraphicsShapeItem(QAbstractGraphicsShapeItemPrivate &dd,
617 QGraphicsItem *parent);
618
619private:
620 Q_DISABLE_COPY(QAbstractGraphicsShapeItem)
621 Q_DECLARE_PRIVATE(QAbstractGraphicsShapeItem)
622};
623
624class QGraphicsPathItemPrivate;
625class Q_WIDGETS_EXPORT QGraphicsPathItem : public QAbstractGraphicsShapeItem
626{
627public:
628 explicit QGraphicsPathItem(QGraphicsItem *parent = nullptr);
629 explicit QGraphicsPathItem(const QPainterPath &path, QGraphicsItem *parent = nullptr);
630 ~QGraphicsPathItem();
631
632 QPainterPath path() const;
633 void setPath(const QPainterPath &path);
634
635 QRectF boundingRect() const override;
636 QPainterPath shape() const override;
637 bool contains(const QPointF &point) const override;
638
639 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
640
641 bool isObscuredBy(const QGraphicsItem *item) const override;
642 QPainterPath opaqueArea() const override;
643
644 enum { Type = 2 };
645 int type() const override;
646
647protected:
648 bool supportsExtension(Extension extension) const override;
649 void setExtension(Extension extension, const QVariant &variant) override;
650 QVariant extension(const QVariant &variant) const override;
651
652private:
653 Q_DISABLE_COPY(QGraphicsPathItem)
654 Q_DECLARE_PRIVATE(QGraphicsPathItem)
655};
656
657class QGraphicsRectItemPrivate;
658class Q_WIDGETS_EXPORT QGraphicsRectItem : public QAbstractGraphicsShapeItem
659{
660public:
661 explicit QGraphicsRectItem(QGraphicsItem *parent = nullptr);
662 explicit QGraphicsRectItem(const QRectF &rect, QGraphicsItem *parent = nullptr);
663 explicit QGraphicsRectItem(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent = nullptr);
664 ~QGraphicsRectItem();
665
666 QRectF rect() const;
667 void setRect(const QRectF &rect);
668 inline void setRect(qreal x, qreal y, qreal w, qreal h);
669
670 QRectF boundingRect() const override;
671 QPainterPath shape() const override;
672 bool contains(const QPointF &point) const override;
673
674 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
675
676 bool isObscuredBy(const QGraphicsItem *item) const override;
677 QPainterPath opaqueArea() const override;
678
679 enum { Type = 3 };
680 int type() const override;
681
682protected:
683 bool supportsExtension(Extension extension) const override;
684 void setExtension(Extension extension, const QVariant &variant) override;
685 QVariant extension(const QVariant &variant) const override;
686
687private:
688 Q_DISABLE_COPY(QGraphicsRectItem)
689 Q_DECLARE_PRIVATE(QGraphicsRectItem)
690};
691
692inline void QGraphicsRectItem::setRect(qreal ax, qreal ay, qreal w, qreal h)
693{ setRect(QRectF(ax, ay, w, h)); }
694
695class QGraphicsEllipseItemPrivate;
696class Q_WIDGETS_EXPORT QGraphicsEllipseItem : public QAbstractGraphicsShapeItem
697{
698public:
699 explicit QGraphicsEllipseItem(QGraphicsItem *parent = nullptr);
700 explicit QGraphicsEllipseItem(const QRectF &rect, QGraphicsItem *parent = nullptr);
701 explicit QGraphicsEllipseItem(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent = nullptr);
702 ~QGraphicsEllipseItem();
703
704 QRectF rect() const;
705 void setRect(const QRectF &rect);
706 inline void setRect(qreal x, qreal y, qreal w, qreal h);
707
708 int startAngle() const;
709 void setStartAngle(int angle);
710
711 int spanAngle() const;
712 void setSpanAngle(int angle);
713
714 QRectF boundingRect() const override;
715 QPainterPath shape() const override;
716 bool contains(const QPointF &point) const override;
717
718 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
719
720 bool isObscuredBy(const QGraphicsItem *item) const override;
721 QPainterPath opaqueArea() const override;
722
723 enum { Type = 4 };
724 int type() const override;
725
726protected:
727 bool supportsExtension(Extension extension) const override;
728 void setExtension(Extension extension, const QVariant &variant) override;
729 QVariant extension(const QVariant &variant) const override;
730
731private:
732 Q_DISABLE_COPY(QGraphicsEllipseItem)
733 Q_DECLARE_PRIVATE(QGraphicsEllipseItem)
734};
735
736inline void QGraphicsEllipseItem::setRect(qreal ax, qreal ay, qreal w, qreal h)
737{ setRect(QRectF(ax, ay, w, h)); }
738
739class QGraphicsPolygonItemPrivate;
740class Q_WIDGETS_EXPORT QGraphicsPolygonItem : public QAbstractGraphicsShapeItem
741{
742public:
743 explicit QGraphicsPolygonItem(QGraphicsItem *parent = nullptr);
744 explicit QGraphicsPolygonItem(const QPolygonF &polygon,
745 QGraphicsItem *parent = nullptr);
746 ~QGraphicsPolygonItem();
747
748 QPolygonF polygon() const;
749 void setPolygon(const QPolygonF &polygon);
750
751 Qt::FillRule fillRule() const;
752 void setFillRule(Qt::FillRule rule);
753
754 QRectF boundingRect() const override;
755 QPainterPath shape() const override;
756 bool contains(const QPointF &point) const override;
757
758 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
759
760 bool isObscuredBy(const QGraphicsItem *item) const override;
761 QPainterPath opaqueArea() const override;
762
763 enum { Type = 5 };
764 int type() const override;
765
766protected:
767 bool supportsExtension(Extension extension) const override;
768 void setExtension(Extension extension, const QVariant &variant) override;
769 QVariant extension(const QVariant &variant) const override;
770
771private:
772 Q_DISABLE_COPY(QGraphicsPolygonItem)
773 Q_DECLARE_PRIVATE(QGraphicsPolygonItem)
774};
775
776class QGraphicsLineItemPrivate;
777class Q_WIDGETS_EXPORT QGraphicsLineItem : public QGraphicsItem
778{
779public:
780 explicit QGraphicsLineItem(QGraphicsItem *parent = nullptr);
781 explicit QGraphicsLineItem(const QLineF &line, QGraphicsItem *parent = nullptr);
782 explicit QGraphicsLineItem(qreal x1, qreal y1, qreal x2, qreal y2, QGraphicsItem *parent = nullptr);
783 ~QGraphicsLineItem();
784
785 QPen pen() const;
786 void setPen(const QPen &pen);
787
788 QLineF line() const;
789 void setLine(const QLineF &line);
790 inline void setLine(qreal x1, qreal y1, qreal x2, qreal y2)
791 { setLine(QLineF(x1, y1, x2, y2)); }
792
793 QRectF boundingRect() const override;
794 QPainterPath shape() const override;
795 bool contains(const QPointF &point) const override;
796
797 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
798
799 bool isObscuredBy(const QGraphicsItem *item) const override;
800 QPainterPath opaqueArea() const override;
801
802 enum { Type = 6 };
803 int type() const override;
804
805protected:
806 bool supportsExtension(Extension extension) const override;
807 void setExtension(Extension extension, const QVariant &variant) override;
808 QVariant extension(const QVariant &variant) const override;
809
810private:
811 Q_DISABLE_COPY(QGraphicsLineItem)
812 Q_DECLARE_PRIVATE(QGraphicsLineItem)
813};
814
815class QGraphicsPixmapItemPrivate;
816class Q_WIDGETS_EXPORT QGraphicsPixmapItem : public QGraphicsItem
817{
818public:
819 enum ShapeMode {
820 MaskShape,
821 BoundingRectShape,
822 HeuristicMaskShape
823 };
824
825 explicit QGraphicsPixmapItem(QGraphicsItem *parent = nullptr);
826 explicit QGraphicsPixmapItem(const QPixmap &pixmap, QGraphicsItem *parent = nullptr);
827 ~QGraphicsPixmapItem();
828
829 QPixmap pixmap() const;
830 void setPixmap(const QPixmap &pixmap);
831
832 Qt::TransformationMode transformationMode() const;
833 void setTransformationMode(Qt::TransformationMode mode);
834
835 QPointF offset() const;
836 void setOffset(const QPointF &offset);
837 inline void setOffset(qreal x, qreal y);
838
839 QRectF boundingRect() const override;
840 QPainterPath shape() const override;
841 bool contains(const QPointF &point) const override;
842
843 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
844
845 bool isObscuredBy(const QGraphicsItem *item) const override;
846 QPainterPath opaqueArea() const override;
847
848 enum { Type = 7 };
849 int type() const override;
850
851 ShapeMode shapeMode() const;
852 void setShapeMode(ShapeMode mode);
853
854protected:
855 bool supportsExtension(Extension extension) const override;
856 void setExtension(Extension extension, const QVariant &variant) override;
857 QVariant extension(const QVariant &variant) const override;
858
859private:
860 Q_DISABLE_COPY(QGraphicsPixmapItem)
861 Q_DECLARE_PRIVATE(QGraphicsPixmapItem)
862};
863
864inline void QGraphicsPixmapItem::setOffset(qreal ax, qreal ay)
865{ setOffset(QPointF(ax, ay)); }
866
867class QGraphicsTextItemPrivate;
868class QTextDocument;
869class QTextCursor;
870class Q_WIDGETS_EXPORT QGraphicsTextItem : public QGraphicsObject
871{
872 Q_OBJECT
873 QDOC_PROPERTY(bool openExternalLinks READ openExternalLinks WRITE setOpenExternalLinks)
874 QDOC_PROPERTY(QTextCursor textCursor READ textCursor WRITE setTextCursor)
875
876public:
877 explicit QGraphicsTextItem(QGraphicsItem *parent = nullptr);
878 explicit QGraphicsTextItem(const QString &text, QGraphicsItem *parent = nullptr);
879 ~QGraphicsTextItem();
880
881 QString toHtml() const;
882 void setHtml(const QString &html);
883
884 QString toPlainText() const;
885 void setPlainText(const QString &text);
886
887 QFont font() const;
888 void setFont(const QFont &font);
889
890 void setDefaultTextColor(const QColor &c);
891 QColor defaultTextColor() const;
892
893 QRectF boundingRect() const override;
894 QPainterPath shape() const override;
895 bool contains(const QPointF &point) const override;
896
897 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
898
899 bool isObscuredBy(const QGraphicsItem *item) const override;
900 QPainterPath opaqueArea() const override;
901
902 enum { Type = 8 };
903 int type() const override;
904
905 void setTextWidth(qreal width);
906 qreal textWidth() const;
907
908 void adjustSize();
909
910 void setDocument(QTextDocument *document);
911 QTextDocument *document() const;
912
913 void setTextInteractionFlags(Qt::TextInteractionFlags flags);
914 Qt::TextInteractionFlags textInteractionFlags() const;
915
916 void setTabChangesFocus(bool b);
917 bool tabChangesFocus() const;
918
919 void setOpenExternalLinks(bool open);
920 bool openExternalLinks() const;
921
922 void setTextCursor(const QTextCursor &cursor);
923 QTextCursor textCursor() const;
924
925Q_SIGNALS:
926 void linkActivated(const QString &);
927 void linkHovered(const QString &);
928
929protected:
930 bool sceneEvent(QEvent *event) override;
931 void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
932 void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
933 void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
934 void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;
935 void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
936 void keyPressEvent(QKeyEvent *event) override;
937 void keyReleaseEvent(QKeyEvent *event) override;
938 void focusInEvent(QFocusEvent *event) override;
939 void focusOutEvent(QFocusEvent *event) override;
940 void dragEnterEvent(QGraphicsSceneDragDropEvent *event) override;
941 void dragLeaveEvent(QGraphicsSceneDragDropEvent *event) override;
942 void dragMoveEvent(QGraphicsSceneDragDropEvent *event) override;
943 void dropEvent(QGraphicsSceneDragDropEvent *event) override;
944 void inputMethodEvent(QInputMethodEvent *event) override;
945 void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
946 void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override;
947 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
948
949 QVariant inputMethodQuery(Qt::InputMethodQuery query) const override;
950
951 bool supportsExtension(Extension extension) const override;
952 void setExtension(Extension extension, const QVariant &variant) override;
953 QVariant extension(const QVariant &variant) const override;
954
955private:
956 Q_DISABLE_COPY(QGraphicsTextItem)
957 Q_PRIVATE_SLOT(dd, void _q_updateBoundingRect(const QSizeF &))
958 Q_PRIVATE_SLOT(dd, void _q_update(QRectF))
959 Q_PRIVATE_SLOT(dd, void _q_ensureVisible(QRectF))
960 QGraphicsTextItemPrivate *dd;
961 friend class QGraphicsTextItemPrivate;
962};
963
964class QGraphicsSimpleTextItemPrivate;
965class Q_WIDGETS_EXPORT QGraphicsSimpleTextItem : public QAbstractGraphicsShapeItem
966{
967public:
968 explicit QGraphicsSimpleTextItem(QGraphicsItem *parent = nullptr);
969 explicit QGraphicsSimpleTextItem(const QString &text, QGraphicsItem *parent = nullptr);
970 ~QGraphicsSimpleTextItem();
971
972 void setText(const QString &text);
973 QString text() const;
974
975 void setFont(const QFont &font);
976 QFont font() const;
977
978 QRectF boundingRect() const override;
979 QPainterPath shape() const override;
980 bool contains(const QPointF &point) const override;
981
982 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
983
984 bool isObscuredBy(const QGraphicsItem *item) const override;
985 QPainterPath opaqueArea() const override;
986
987 enum { Type = 9 };
988 int type() const override;
989
990protected:
991 bool supportsExtension(Extension extension) const override;
992 void setExtension(Extension extension, const QVariant &variant) override;
993 QVariant extension(const QVariant &variant) const override;
994
995private:
996 Q_DISABLE_COPY(QGraphicsSimpleTextItem)
997 Q_DECLARE_PRIVATE(QGraphicsSimpleTextItem)
998};
999
1000class QGraphicsItemGroupPrivate;
1001class Q_WIDGETS_EXPORT QGraphicsItemGroup : public QGraphicsItem
1002{
1003public:
1004 explicit QGraphicsItemGroup(QGraphicsItem *parent = nullptr);
1005 ~QGraphicsItemGroup();
1006
1007 void addToGroup(QGraphicsItem *item);
1008 void removeFromGroup(QGraphicsItem *item);
1009
1010 QRectF boundingRect() const override;
1011 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
1012
1013 bool isObscuredBy(const QGraphicsItem *item) const override;
1014 QPainterPath opaqueArea() const override;
1015
1016 enum { Type = 10 };
1017 int type() const override;
1018
1019private:
1020 Q_DISABLE_COPY(QGraphicsItemGroup)
1021 Q_DECLARE_PRIVATE(QGraphicsItemGroup)
1022};
1023
1024template <class T> inline T qgraphicsitem_cast(QGraphicsItem *item)
1025{
1026 typedef typename std::remove_cv<typename std::remove_pointer<T>::type>::type Item;
1027 return int(Item::Type) == int(QGraphicsItem::Type)
1028 || (item && int(Item::Type) == item->type()) ? static_cast<T>(item) : 0;
1029}
1030
1031template <class T> inline T qgraphicsitem_cast(const QGraphicsItem *item)
1032{
1033 typedef typename std::remove_cv<typename std::remove_pointer<T>::type>::type Item;
1034 return int(Item::Type) == int(QGraphicsItem::Type)
1035 || (item && int(Item::Type) == item->type()) ? static_cast<T>(item) : 0;
1036}
1037
1038#ifndef QT_NO_DEBUG_STREAM
1039Q_WIDGETS_EXPORT QDebug operator<<(QDebug debug, QGraphicsItem *item);
1040Q_WIDGETS_EXPORT QDebug operator<<(QDebug debug, QGraphicsObject *item);
1041Q_WIDGETS_EXPORT QDebug operator<<(QDebug debug, QGraphicsItem::GraphicsItemChange change);
1042Q_WIDGETS_EXPORT QDebug operator<<(QDebug debug, QGraphicsItem::GraphicsItemFlag flag);
1043Q_WIDGETS_EXPORT QDebug operator<<(QDebug debug, QGraphicsItem::GraphicsItemFlags flags);
1044#endif
1045
1046QT_END_NAMESPACE
1047
1048Q_DECLARE_METATYPE(QGraphicsItem *)
1049
1050QT_BEGIN_NAMESPACE
1051
1052QT_END_NAMESPACE
1053
1054#endif // QGRAPHICSITEM_H
1055