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 documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
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 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 **   * Redistributions of source code must retain the above copyright
25 **     notice, this list of conditions and the following disclaimer.
26 **   * Redistributions in binary form must reproduce the above copyright
27 **     notice, this list of conditions and the following disclaimer in
28 **     the documentation and/or other materials provided with the
29 **     distribution.
30 **   * Neither the name of The Qt Company Ltd nor the names of its
31 **     contributors may be used to endorse or promote products derived
32 **     from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 //! [0]
52 class SimpleItem : public QGraphicsItem
53 {
54 public:
boundingRect() const55     QRectF boundingRect() const override
56     {
57         qreal penWidth = 1;
58         return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
59                       20 + penWidth, 20 + penWidth);
60     }
61 
paint(QPainter * painter,const QStyleOptionGraphicsItem * option,QWidget * widget)62     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
63                QWidget *widget) override
64     {
65         painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);
66     }
67 };
68 //! [0]
69 
70 
71 //! [1]
72 class CustomItem : public QGraphicsItem
73 {
74 public:
75    enum { Type = UserType + 1 };
76 
type() const77    int type() const override
78    {
79        // Enable the use of qgraphicsitem_cast with this item.
80        return Type;
81    }
82    ...
83 };
84 //! [1]
85 
86 
87 //! [2]
88 item->setCursor(Qt::IBeamCursor);
89 //! [2]
90 
91 
92 //! [3]
93 item->setCursor(Qt::IBeamCursor);
94 //! [3]
95 
96 
97 //! [4]
98 QGraphicsRectItem rect;
99 rect.setPos(100, 100);
100 
101 rect.sceneTransform().map(QPointF(0, 0));
102 // returns QPointF(100, 100);
103 
104 rect.sceneTransform().inverted().map(QPointF(100, 100));
105 // returns QPointF(0, 0);
106 //! [4]
107 
108 
109 //! [5]
110 QGraphicsRectItem rect;
111 rect.setPos(100, 100);
112 
113 rect.deviceTransform(view->viewportTransform()).map(QPointF(0, 0));
114 // returns the item's (0, 0) point in view's viewport coordinates
115 
116 rect.deviceTransform(view->viewportTransform()).inverted().map(QPointF(100, 100));
117 // returns view's viewport's (100, 100) coordinate in item coordinates
118 //! [5]
119 
120 
121 //! [6]
122 // Rotate an item 45 degrees around (0, 0).
123 item->rotate(45);
124 
125 // Rotate an item 45 degrees around (x, y).
126 item->setTransform(QTransform().translate(x, y).rotate(45).translate(-x, -y));
127 //! [6]
128 
129 
130 //! [7]
131 // Scale an item by 3x2 from its origin
132 item->scale(3, 2);
133 
134 // Scale an item by 3x2 from (x, y)
135 item->setTransform(QTransform().translate(x, y).scale(3, 2).translate(-x, -y));
136 //! [7]
137 
138 
139 //! [8]
boundingRect() const140 QRectF CircleItem::boundingRect() const
141 {
142     qreal penWidth = 1;
143     return QRectF(-radius - penWidth / 2, -radius - penWidth / 2,
144                   diameter + penWidth, diameter + penWidth);
145 }
146 //! [8]
147 
148 
149 //! [9]
shape() const150 QPainterPath RoundItem::shape() const
151 {
152     QPainterPath path;
153     path.addEllipse(boundingRect());
154     return path;
155 }
156 //! [9]
157 
158 
159 //! [10]
paint(QPainter * painter,const QStyleOptionGraphicsItem * option,QWidget * widget)160 void RoundRectItem::paint(QPainter *painter,
161                           const QStyleOptionGraphicsItem *option,
162                           QWidget *widget)
163 {
164     painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);
165 }
166 //! [10]
167 
168 
169 //! [11]
170 static const int ObjectName = 0;
171 
172 QGraphicsItem *item = scene.itemAt(100, 50);
173 if (item->data(ObjectName).toString().isEmpty()) {
174     if (qgraphicsitem_cast<ButtonItem *>(item))
175         item->setData(ObjectName, "Button");
176 }
177 //! [11]
178 
179 
180 //! [12]
181 QGraphicsScene scene;
182 QGraphicsEllipseItem *ellipse = scene.addEllipse(QRectF(-10, -10, 20, 20));
183 QGraphicsLineItem *line = scene.addLine(QLineF(-10, -10, 20, 20));
184 
185 line->installSceneEventFilter(ellipse);
186 // line's events are filtered by ellipse's sceneEventFilter() function.
187 
188 ellipse->installSceneEventFilter(line);
189 // ellipse's events are filtered by line's sceneEventFilter() function.
190 //! [12]
191 
192 
193 //! [13]
contextMenuEvent(QGraphicsSceneContextMenuEvent * event)194 void CustomItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
195 {
196     QMenu menu;
197     QAction *removeAction = menu.addAction("Remove");
198     QAction *markAction = menu.addAction("Mark");
199     QAction *selectedAction = menu.exec(event->screenPos());
200     // ...
201 }
202 //! [13]
203 
204 
205 //! [14]
CustomItem()206 CustomItem::CustomItem()
207 {
208     setAcceptDrops(true);
209     ...
210 }
211 
dragEnterEvent(QGraphicsSceneDragDropEvent * event)212 void CustomItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
213 {
214     event->setAccepted(event->mimeData()->hasFormat("text/plain"));
215 }
216 //! [14]
217 
218 
219 //! [15]
itemChange(GraphicsItemChange change,const QVariant & value)220 QVariant Component::itemChange(GraphicsItemChange change, const QVariant &value)
221 {
222     if (change == ItemPositionChange && scene()) {
223         // value is the new position.
224         QPointF newPos = value.toPointF();
225         QRectF rect = scene()->sceneRect();
226         if (!rect.contains(newPos)) {
227             // Keep the item inside the scene rect.
228             newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left())));
229             newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top())));
230             return newPos;
231         }
232     }
233     return QGraphicsItem::itemChange(change, value);
234 }
235 //! [15]
236 
237 
238 //! [16]
setRadius(qreal newRadius)239 void CircleItem::setRadius(qreal newRadius)
240 {
241     if (radius != newRadius) {
242         prepareGeometryChange();
243         radius = newRadius;
244     }
245 }
246 //! [16]
247 
248 
249 //! [17]
250 // Group all selected items together
251 QGraphicsItemGroup *group = scene->createItemGroup(scene->selecteditems());
252 
253 // Destroy the group, and delete the group item
254 scene->destroyItemGroup(group);
255 //! [17]
256 
257 
258 //! [18]
259 class QGraphicsPathItem : public QAbstractGraphicsShapeItem
260 {
261  public:
262   enum { Type = 2 };
type() const263     int type() const override { return Type; }
264   ...
265 };
266 //! [18]
267 
268 //! [19]
269 QTransform xform = item->deviceTransform(view->viewportTransform());
270 QRect deviceRect = xform.mapRect(rect).toAlignedRect();
271 view->viewport()->scroll(dx, dy, deviceRect);
272 //! [19]
273 
274 //! [20]
275 item->setTransform(QTransform().rotate(angle), true);
276 //! [20]
277 
278 //! [21]
279 setTransform(QTransform::fromScale(sx, sy), true);
280 //! [21]
281 
282 //! [22]
283 setTransform(QTransform().shear(sh, sv), true);
284 //! [22]
285 
286 //! [23]
287 setTransform(QTransform::fromTranslate(dx, dy), true);
288 //! [23]
289