graphicsview/qgraphicsitem.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/****************************************************************************-
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/****************************************************************************
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtGui 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 Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42/*! -
43 \class QGraphicsItem -
44 \brief The QGraphicsItem class is the base class for all graphical -
45 items in a QGraphicsScene. -
46 \since 4.2 -
47 -
48 \ingroup graphicsview-api -
49 \inmodule QtWidgets -
50 -
51 It provides a light-weight foundation for writing your own custom items. -
52 This includes defining the item's geometry, collision detection, its -
53 painting implementation and item interaction through its event handlers. -
54 QGraphicsItem is part of the \l{Graphics View Framework} -
55 -
56 \image graphicsview-items.png -
57 -
58 For convenience, Qt provides a set of standard graphics items for the most -
59 common shapes. These are: -
60 -
61 \list -
62 \li QGraphicsEllipseItem provides an ellipse item -
63 \li QGraphicsLineItem provides a line item -
64 \li QGraphicsPathItem provides an arbitrary path item -
65 \li QGraphicsPixmapItem provides a pixmap item -
66 \li QGraphicsPolygonItem provides a polygon item -
67 \li QGraphicsRectItem provides a rectangular item -
68 \li QGraphicsSimpleTextItem provides a simple text label item -
69 \li QGraphicsTextItem provides an advanced text browser item -
70 \endlist -
71 -
72 All of an item's geometric information is based on its local coordinate -
73 system. The item's position, pos(), is the only function that does not -
74 operate in local coordinates, as it returns a position in parent -
75 coordinates. \l {The Graphics View Coordinate System} describes the coordinate -
76 system in detail. -
77 -
78 You can set whether an item should be visible (i.e., drawn, and accepting -
79 events), by calling setVisible(). Hiding an item will also hide its -
80 children. Similarly, you can enable or disable an item by calling -
81 setEnabled(). If you disable an item, all its children will also be -
82 disabled. By default, items are both visible and enabled. To toggle -
83 whether an item is selected or not, first enable selection by setting -
84 the ItemIsSelectable flag, and then call setSelected(). Normally, -
85 selection is toggled by the scene, as a result of user interaction. -
86 -
87 To write your own graphics item, you first create a subclass of -
88 QGraphicsItem, and then start by implementing its two pure virtual public -
89 functions: boundingRect(), which returns an estimate of the area painted -
90 by the item, and paint(), which implements the actual painting. For -
91 example: -
92 -
93 \snippet code/src_gui_graphicsview_qgraphicsitem.cpp 0 -
94 -
95 The boundingRect() function has many different purposes. -
96 QGraphicsScene bases its item index on boundingRect(), and -
97 QGraphicsView uses it both for culling invisible items, and for -
98 determining the area that needs to be recomposed when drawing -
99 overlapping items. In addition, QGraphicsItem's collision -
100 detection mechanisms use boundingRect() to provide an efficient -
101 cut-off. The fine grained collision algorithm in -
102 collidesWithItem() is based on calling shape(), which returns an -
103 accurate outline of the item's shape as a QPainterPath. -
104 -
105 QGraphicsScene expects all items boundingRect() and shape() to -
106 remain unchanged unless it is notified. If you want to change an -
107 item's geometry in any way, you must first call -
108 prepareGeometryChange() to allow QGraphicsScene to update its -
109 bookkeeping. -
110 -
111 Collision detection can be done in two ways: -
112 -
113 \list 1 -
114 -
115 \li Reimplement shape() to return an accurate shape for your item, -
116 and rely on the default implementation of collidesWithItem() to do -
117 shape-shape intersection. This can be rather expensive if the -
118 shapes are complex. -
119 -
120 \li Reimplement collidesWithItem() to provide your own custom item -
121 and shape collision algorithm. -
122 -
123 \endlist -
124 -
125 The contains() function can be called to determine whether the item \e -
126 contains a point or not. This function can also be reimplemented by the -
127 item. The default behavior of contains() is based on calling shape(). -
128 -
129 Items can contain other items, and also be contained by other items. All -
130 items can have a parent item and a list of children. Unless the item has -
131 no parent, its position is in \e parent coordinates (i.e., the parent's -
132 local coordinates). Parent items propagate both their position and their -
133 transformation to all children. -
134 -
135 \image graphicsview-parentchild.png -
136 -
137 \target Transformations -
138 \section1 Transformations -
139 -
140 QGraphicsItem supports projective transformations in addition to its base -
141 position, pos(). There are several ways to change an item's transformation. -
142 For simple transformations, you can call either of the convenience -
143 functions setRotation() or setScale(), or you can pass any transformation -
144 matrix to setTransform(). For advanced transformation control you also have -
145 the option of setting several combined transformations by calling -
146 setTransformations(). -
147 -
148 Item transformations accumulate from parent to child, so if both a parent -
149 and child item are rotated 90 degrees, the child's total transformation -
150 will be 180 degrees. Similarly, if the item's parent is scaled to 2x its -
151 original size, its children will also be twice as large. An item's -
152 transformation does not affect its own local geometry; all geometry -
153 functions (e.g., contains(), update(), and all the mapping functions) still -
154 operate in local coordinates. For convenience, QGraphicsItem provides the -
155 functions sceneTransform(), which returns the item's total transformation -
156 matrix (including its position and all parents' positions and -
157 transformations), and scenePos(), which returns its position in scene -
158 coordinates. To reset an item's matrix, call resetTransform(). -
159 -
160 Certain transformation operations produce a different outcome depending on -
161 the order in which they are applied. For example, if you scale an -
162 transform, and then rotate it, you may get a different result than if the -
163 transform was rotated first. However, the order you set the transformation -
164 properties on QGraphicsItem does not affect the resulting transformation; -
165 QGraphicsItem always applies the properties in a fixed, defined order: -
166 -
167 \list -
168 \li The item's base transform is applied (transform()) -
169 \li The item's transformations list is applied in order (transformations()) -
170 \li The item is rotated relative to its transform origin point (rotation(), transformOriginPoint()) -
171 \li The item is scaled relative to its transform origin point (scale(), transformOriginPoint()) -
172 \endlist -
173 -
174 \section1 Painting -
175 -
176 The paint() function is called by QGraphicsView to paint the item's -
177 contents. The item has no background or default fill of its own; whatever -
178 is behind the item will shine through all areas that are not explicitly -
179 painted in this function. You can call update() to schedule a repaint, -
180 optionally passing the rectangle that needs a repaint. Depending on -
181 whether or not the item is visible in a view, the item may or may not be -
182 repainted; there is no equivalent to QWidget::repaint() in QGraphicsItem. -
183 -
184 Items are painted by the view, starting with the parent items and then -
185 drawing children, in ascending stacking order. You can set an item's -
186 stacking order by calling setZValue(), and test it by calling -
187 zValue(), where items with low z-values are painted before items with -
188 high z-values. Stacking order applies to sibling items; parents are always -
189 drawn before their children. -
190 -
191 \section1 Sorting -
192 -
193 All items are drawn in a defined, stable order, and this same order decides -
194 which items will receive mouse input first when you click on the scene. -
195 Normally you don't have to worry about sorting, as the items follow a -
196 "natural order", following the logical structure of the scene. -
197 -
198 An item's children are stacked on top of the parent, and sibling items are -
199 stacked by insertion order (i.e., in the same order that they were either -
200 added to the scene, or added to the same parent). If you add item A, and -
201 then B, then B will be on top of A. If you then add C, the items' stacking -
202 order will be A, then B, then C. -
203 -
204 \image graphicsview-zorder.png -
205 -
206 This example shows the stacking order of all limbs of the robot from the -
207 \l{graphicsview/dragdroprobot}{Drag and Drop Robot} example. The torso is -
208 the root item (all other items are children or descendants of the torso), -
209 so it is drawn first. Next, the head is drawn, as it is the first item in -
210 the torso's list of children. Then the upper left arm is drawn. As the -
211 lower arm is a child of the upper arm, the lower arm is then drawn, -
212 followed by the upper arm's next sibling, which is the upper right arm, and -
213 so on. -
214 -
215 For advanced users, there are ways to alter how your items are sorted: -
216 -
217 \list -
218 \li You can call setZValue() on an item to explicitly stack it on top of, or -
219 under, other sibling items. The default Z value for an item is 0. Items -
220 with the same Z value are stacked by insertion order. -
221 -
222 \li You can call stackBefore() to reorder the list of children. This will -
223 directly modify the insertion order. -
224 -
225 \li You can set the ItemStacksBehindParent flag to stack a child item behind -
226 its parent. -
227 \endlist -
228 -
229 The stacking order of two sibling items also counts for each item's -
230 children and descendant items. So if one item is on top of another, then -
231 all its children will also be on top of all the other item's children as -
232 well. -
233 -
234 \section1 Events -
235 -
236 QGraphicsItem receives events from QGraphicsScene through the virtual -
237 function sceneEvent(). This function distributes the most common events -
238 to a set of convenience event handlers: -
239 -
240 \list -
241 \li contextMenuEvent() handles context menu events -
242 \li focusInEvent() and focusOutEvent() handle focus in and out events -
243 \li hoverEnterEvent(), hoverMoveEvent(), and hoverLeaveEvent() handles -
244 hover enter, move and leave events -
245 \li inputMethodEvent() handles input events, for accessibility support -
246 \li keyPressEvent() and keyReleaseEvent() handle key press and release events -
247 \li mousePressEvent(), mouseMoveEvent(), mouseReleaseEvent(), and -
248 mouseDoubleClickEvent() handles mouse press, move, release, click and -
249 doubleclick events -
250 \endlist -
251 -
252 You can filter events for any other item by installing event filters. This -
253 functionality is separate from Qt's regular event filters (see -
254 QObject::installEventFilter()), which only work on subclasses of QObject. After -
255 installing your item as an event filter for another item by calling -
256 installSceneEventFilter(), the filtered events will be received by the virtual -
257 function sceneEventFilter(). You can remove item event filters by calling -
258 removeSceneEventFilter(). -
259 -
260 \section1 Custom Data -
261 -
262 Sometimes it's useful to register custom data with an item, be it a custom -
263 item, or a standard item. You can call setData() on any item to store data -
264 in it using a key-value pair (the key being an integer, and the value is a -
265 QVariant). To get custom data from an item, call data(). This -
266 functionality is completely untouched by Qt itself; it is provided for the -
267 user's convenience. -
268 -
269 \sa QGraphicsScene, QGraphicsView, {Graphics View Framework} -
270*/ -
271 -
272/*! -
273 \variable QGraphicsItem::Type -
274 -
275 The type value returned by the virtual type() function in standard -
276 graphics item classes in Qt. All such standard graphics item -
277 classes in Qt are associated with a unique value for Type, -
278 e.g. the value returned by QGraphicsPathItem::type() is 2. -
279 -
280 \snippet code/src_gui_graphicsview_qgraphicsitem.cpp 18 -
281*/ -
282 -
283/*! -
284 \variable QGraphicsItem::UserType -
285 -
286 The lowest permitted type value for custom items (subclasses -
287 of QGraphicsItem or any of the standard items). This value is -
288 used in conjunction with a reimplementation of QGraphicsItem::type() -
289 and declaring a Type enum value. Example: -
290 -
291 \snippet code/src_gui_graphicsview_qgraphicsitem.cpp 1 -
292 -
293 \note UserType = 65536 -
294*/ -
295 -
296/*! -
297 \enum QGraphicsItem::GraphicsItemFlag -
298 -
299 This enum describes different flags that you can set on an item to -
300 toggle different features in the item's behavior. -
301 -
302 All flags are disabled by default. -
303 -
304 \value ItemIsMovable The item supports interactive movement using -
305 the mouse. By clicking on the item and then dragging, the item -
306 will move together with the mouse cursor. If the item has -
307 children, all children are also moved. If the item is part of a -
308 selection, all selected items are also moved. This feature is -
309 provided as a convenience through the base implementation of -
310 QGraphicsItem's mouse event handlers. -
311 -
312 \value ItemIsSelectable The item supports selection. Enabling this -
313 feature will enable setSelected() to toggle selection for the -
314 item. It will also let the item be selected automatically as a -
315 result of calling QGraphicsScene::setSelectionArea(), by clicking -
316 on an item, or by using rubber band selection in QGraphicsView. -
317 -
318 \value ItemIsFocusable The item supports keyboard input focus (i.e., it is -
319 an input item). Enabling this flag will allow the item to accept focus, -
320 which again allows the delivery of key events to -
321 QGraphicsItem::keyPressEvent() and QGraphicsItem::keyReleaseEvent(). -
322 -
323 \value ItemClipsToShape The item clips to its own shape. The item cannot -
324 draw or receive mouse, tablet, drag and drop or hover events outside its -
325 shape. It is disabled by default. This behavior is enforced by -
326 QGraphicsView::drawItems() or QGraphicsScene::drawItems(). This flag was -
327 introduced in Qt 4.3. -
328 -
329 \value ItemClipsChildrenToShape The item clips the painting of all its -
330 descendants to its own shape. Items that are either direct or indirect -
331 children of this item cannot draw outside this item's shape. By default, -
332 this flag is disabled; children can draw anywhere. This behavior is -
333 enforced by QGraphicsView::drawItems() or -
334 QGraphicsScene::drawItems(). This flag was introduced in Qt 4.3. -
335 -
336 \value ItemIgnoresTransformations The item ignores inherited -
337 transformations (i.e., its position is still anchored to its parent, but -
338 the parent or view rotation, zoom or shear transformations are ignored). -
339 This flag is useful for keeping text label items horizontal and unscaled, -
340 so they will still be readable if the view is transformed. When set, the -
341 item's view geometry and scene geometry will be maintained separately. You -
342 must call deviceTransform() to map coordinates and detect collisions in -
343 the view. By default, this flag is disabled. This flag was introduced in -
344 Qt 4.3. \note With this flag set you can still scale the item itself, and -
345 that scale transformation will influence the item's children. -
346 -
347 \value ItemIgnoresParentOpacity The item ignores its parent's opacity. The -
348 item's effective opacity is the same as its own; it does not combine with -
349 the parent's opacity. This flags allows your item to keep its absolute -
350 opacity even if the parent is semitransparent. This flag was introduced in -
351 Qt 4.5. -
352 -
353 \value ItemDoesntPropagateOpacityToChildren The item doesn't propagate its -
354 opacity to its children. This flag allows you to create a semitransparent -
355 item that does not affect the opacity of its children. This flag was -
356 introduced in Qt 4.5. -
357 -
358 \value ItemStacksBehindParent The item is stacked behind its parent. By -
359 default, child items are stacked on top of the parent item. But setting -
360 this flag, the child will be stacked behind it. This flag is useful for -
361 drop shadow effects and for decoration objects that follow the parent -
362 item's geometry without drawing on top of it. This flag was introduced -
363 in Qt 4.5. -
364 -
365 \value ItemUsesExtendedStyleOption The item makes use of either -
366 \l{QStyleOptionGraphicsItem::} {exposedRect} or -
367 \l{QStyleOptionGraphicsItem::} {matrix} in -
368 QStyleOptionGraphicsItem. By default, the -
369 \l{QStyleOptionGraphicsItem::} {exposedRect} is initialized to the -
370 item's boundingRect() and the -
371 \l{QStyleOptionGraphicsItem::}{matrix} is untransformed. You can -
372 enable this flag for the style options to be set up with more -
373 fine-grained values. Note that -
374 QStyleOptionGraphicsItem::levelOfDetail is unaffected by this flag -
375 and always initialized to 1. Use -
376 QStyleOptionGraphicsItem::levelOfDetailFromTransform() if you need -
377 a higher value. This flag was introduced in Qt 4.6. -
378 -
379 \value ItemHasNoContents The item does not paint anything (i.e., calling -
380 paint() on the item has no effect). You should set this flag on items that -
381 do not need to be painted to ensure that Graphics View avoids unnecessary -
382 painting preparations. This flag was introduced in Qt 4.6. -
383 -
384 \value ItemSendsGeometryChanges The item enables itemChange() -
385 notifications for ItemPositionChange, ItemPositionHasChanged, -
386 ItemMatrixChange, ItemTransformChange, ItemTransformHasChanged, -
387 ItemRotationChange, ItemRotationHasChanged, ItemScaleChange, ItemScaleHasChanged, -
388 ItemTransformOriginPointChange, and ItemTransformOriginPointHasChanged. For -
389 performance reasons, these notifications are disabled by default. You must -
390 enable this flag to receive notifications for position and transform -
391 changes. This flag was introduced in Qt 4.6. -
392 -
393 \value ItemAcceptsInputMethod The item supports input methods typically -
394 used for Asian languages. -
395 This flag was introduced in Qt 4.6. -
396 -
397 \value ItemNegativeZStacksBehindParent The item automatically -
398 stacks behind it's parent if it's z-value is negative. This flag -
399 enables setZValue() to toggle ItemStacksBehindParent. This flag -
400 was introduced in Qt 4.6. -
401 -
402 \value ItemIsPanel The item is a panel. A panel provides activation and -
403 contained focus handling. Only one panel can be active at a time (see -
404 QGraphicsItem::isActive()). When no panel is active, QGraphicsScene -
405 activates all non-panel items. Window items (i.e., -
406 QGraphicsItem::isWindow() returns true) are panels. This flag was -
407 introduced in Qt 4.6. -
408 -
409 \omitvalue ItemIsFocusScope \omit Internal only (for now). \endomit -
410 -
411 \value ItemSendsScenePositionChanges The item enables itemChange() -
412 notifications for ItemScenePositionHasChanged. For performance reasons, -
413 these notifications are disabled by default. You must enable this flag -
414 to receive notifications for scene position changes. This flag was -
415 introduced in Qt 4.6. -
416 -
417 \omitvalue ItemStopsClickFocusPropagation \omit The item stops propagating -
418 click focus to items underneath when being clicked on. This flag -
419 allows you create a non-focusable item that can be clicked on without -
420 changing the focus. \endomit -
421 -
422 \omitvalue ItemStopsFocusHandling \omit Same as -
423 ItemStopsClickFocusPropagation, but also suppresses focus-out. This flag -
424 allows you to completely take over focus handling. -
425 This flag was introduced in Qt 4.7. \endomit -
426*/ -
427 -
428/*! -
429 \enum QGraphicsItem::GraphicsItemChange -
430 -
431 This enum describes the state changes that are notified by -
432 QGraphicsItem::itemChange(). The notifications are sent as the state -
433 changes, and in some cases, adjustments can be made (see the documentation -
434 for each change for details). -
435 -
436 Note: Be careful with calling functions on the QGraphicsItem itself inside -
437 itemChange(), as certain function calls can lead to unwanted -
438 recursion. For example, you cannot call setPos() in itemChange() on an -
439 ItemPositionChange notification, as the setPos() function will again call -
440 itemChange(ItemPositionChange). Instead, you can return the new, adjusted -
441 position from itemChange(). -
442 -
443 \value ItemEnabledChange The item's enabled state changes. If the item is -
444 presently enabled, it will become disabled, and vice verca. The value -
445 argument is the new enabled state (i.e., true or false). Do not call -
446 setEnabled() in itemChange() as this notification is delivered. Instead, -
447 you can return the new state from itemChange(). -
448 -
449 \value ItemEnabledHasChanged The item's enabled state has changed. The -
450 value argument is the new enabled state (i.e., true or false). Do not call -
451 setEnabled() in itemChange() as this notification is delivered. The return -
452 value is ignored. -
453 -
454 \value ItemMatrixChange The item's affine transformation matrix is -
455 changing. This value is obsolete; you can use ItemTransformChange instead. -
456 -
457 \value ItemPositionChange The item's position changes. This notification -
458 is sent if the ItemSendsGeometryChanges flag is enabled, and when the -
459 item's local position changes, relative to its parent (i.e., as a result -
460 of calling setPos() or moveBy()). The value argument is the new position -
461 (i.e., a QPointF). You can call pos() to get the original position. Do -
462 not call setPos() or moveBy() in itemChange() as this notification is -
463 delivered; instead, you can return the new, adjusted position from -
464 itemChange(). After this notification, QGraphicsItem immediately sends the -
465 ItemPositionHasChanged notification if the position changed. -
466 -
467 \value ItemPositionHasChanged The item's position has changed. This -
468 notification is sent if the ItemSendsGeometryChanges flag is enabled, and -
469 after the item's local position, relative to its parent, has changed. The -
470 value argument is the new position (the same as pos()), and QGraphicsItem -
471 ignores the return value for this notification (i.e., a read-only -
472 notification). -
473 -
474 \value ItemTransformChange The item's transformation matrix changes. This -
475 notification is send if the ItemSendsGeometryChanges flag is enabled, and -
476 when the item's local transformation matrix changes (i.e., as a result of -
477 calling setTransform(). The value argument is the new matrix (i.e., a -
478 QTransform); to get the old matrix, call transform(). Do not call -
479 setTransform() or set any of the transformation properties in itemChange() -
480 as this notification is delivered; instead, you can return the new matrix -
481 from itemChange(). This notification is not sent if you change the -
482 transformation properties. -
483 -
484 \value ItemTransformHasChanged The item's transformation matrix has -
485 changed either because setTransform is called, or one of the -
486 transformation properties is changed. This notification is sent if the -
487 ItemSendsGeometryChanges flag is enabled, and after the item's local -
488 transformation matrix has changed. The value argument is the new matrix -
489 (same as transform()), and QGraphicsItem ignores the return value for this -
490 notification (i.e., a read-only notification). -
491 -
492 \value ItemRotationChange The item's rotation property changes. This -
493 notification is sent if the ItemSendsGeometryChanges flag is enabled, and -
494 when the item's rotation property changes (i.e., as a result of calling -
495 setRotation()). The value argument is the new rotation (i.e., a double); -
496 to get the old rotation, call rotation(). Do not call setRotation() in -
497 itemChange() as this notification is delivered; instead, you can return -
498 the new rotation from itemChange(). -
499 -
500 \value ItemRotationHasChanged The item's rotation property has changed. -
501 This notification is sent if the ItemSendsGeometryChanges flag is enabled, -
502 and after the item's rotation property has changed. The value argument is -
503 the new rotation (i.e., a double), and QGraphicsItem ignores the return -
504 value for this notification (i.e., a read-only notification). Do not call -
505 setRotation() in itemChange() as this notification is delivered. -
506 -
507 \value ItemScaleChange The item's scale property changes. This notification -
508 is sent if the ItemSendsGeometryChanges flag is enabled, and when the item's -
509 scale property changes (i.e., as a result of calling setScale()). The value -
510 argument is the new scale (i.e., a double); to get the old scale, call -
511 scale(). Do not call setScale() in itemChange() as this notification is -
512 delivered; instead, you can return the new scale from itemChange(). -
513 -
514 \value ItemScaleHasChanged The item's scale property has changed. This -
515 notification is sent if the ItemSendsGeometryChanges flag is enabled, and -
516 after the item's scale property has changed. The value argument is the new -
517 scale (i.e., a double), and QGraphicsItem ignores the return value for this -
518 notification (i.e., a read-only notification). Do not call setScale() in -
519 itemChange() as this notification is delivered. -
520 -
521 \value ItemTransformOriginPointChange The item's transform origin point -
522 property changes. This notification is sent if the ItemSendsGeometryChanges -
523 flag is enabled, and when the item's transform origin point property changes -
524 (i.e., as a result of calling setTransformOriginPoint()). The value argument -
525 is the new origin point (i.e., a QPointF); to get the old origin point, call -
526 transformOriginPoint(). Do not call setTransformOriginPoint() in itemChange() -
527 as this notification is delivered; instead, you can return the new transform -
528 origin point from itemChange(). -
529 -
530 \value ItemTransformOriginPointHasChanged The item's transform origin point -
531 property has changed. This notification is sent if the ItemSendsGeometryChanges -
532 flag is enabled, and after the item's transform origin point property has -
533 changed. The value argument is the new origin point (i.e., a QPointF), and -
534 QGraphicsItem ignores the return value for this notification (i.e., a read-only -
535 notification). Do not call setTransformOriginPoint() in itemChange() as this -
536 notification is delivered. -
537 -
538 \value ItemSelectedChange The item's selected state changes. If the item is -
539 presently selected, it will become unselected, and vice verca. The value -
540 argument is the new selected state (i.e., true or false). Do not call -
541 setSelected() in itemChange() as this notification is delivered; instead, you -
542 can return the new selected state from itemChange(). -
543 -
544 \value ItemSelectedHasChanged The item's selected state has changed. The -
545 value argument is the new selected state (i.e., true or false). Do not -
546 call setSelected() in itemChange() as this notification is delivered. The -
547 return value is ignored. -
548 -
549 \value ItemVisibleChange The item's visible state changes. If the item is -
550 presently visible, it will become invisible, and vice verca. The value -
551 argument is the new visible state (i.e., true or false). Do not call -
552 setVisible() in itemChange() as this notification is delivered; instead, -
553 you can return the new visible state from itemChange(). -
554 -
555 \value ItemVisibleHasChanged The item's visible state has changed. The -
556 value argument is the new visible state (i.e., true or false). Do not call -
557 setVisible() in itemChange() as this notification is delivered. The return -
558 value is ignored. -
559 -
560 \value ItemParentChange The item's parent changes. The value argument is -
561 the new parent item (i.e., a QGraphicsItem pointer). Do not call -
562 setParentItem() in itemChange() as this notification is delivered; -
563 instead, you can return the new parent from itemChange(). -
564 -
565 \value ItemParentHasChanged The item's parent has changed. The value -
566 argument is the new parent (i.e., a pointer to a QGraphicsItem). Do not -
567 call setParentItem() in itemChange() as this notification is -
568 delivered. The return value is ignored. -
569 -
570 \value ItemChildAddedChange A child is added to this item. The value -
571 argument is the new child item (i.e., a QGraphicsItem pointer). Do not -
572 pass this item to any item's setParentItem() function as this notification -
573 is delivered. The return value is unused; you cannot adjust anything in -
574 this notification. Note that the new child might not be fully constructed -
575 when this notification is sent; calling pure virtual functions on -
576 the child can lead to a crash. -
577 -
578 \value ItemChildRemovedChange A child is removed from this item. The value -
579 argument is the child item that is about to be removed (i.e., a -
580 QGraphicsItem pointer). The return value is unused; you cannot adjust -
581 anything in this notification. -
582 -
583 \value ItemSceneChange The item is moved to a new scene. This notification is -
584 also sent when the item is added to its initial scene, and when it is removed. -
585 The item's scene() is the old scene (or 0 if the item has not been added to a -
586 scene yet). The value argument is the new scene (i.e., a QGraphicsScene -
587 pointer), or a null pointer if the item is removed from a scene. Do not -
588 override this change by passing this item to QGraphicsScene::addItem() as this -
589 notification is delivered; instead, you can return the new scene from -
590 itemChange(). Use this feature with caution; objecting to a scene change can -
591 quickly lead to unwanted recursion. -
592 -
593 \value ItemSceneHasChanged The item's scene has changed. The item's scene() is -
594 the new scene. This notification is also sent when the item is added to its -
595 initial scene, and when it is removed.The value argument is the new scene -
596 (i.e., a pointer to a QGraphicsScene). Do not call setScene() in itemChange() -
597 as this notification is delivered. The return value is ignored. -
598 -
599 \value ItemCursorChange The item's cursor changes. The value argument is -
600 the new cursor (i.e., a QCursor). Do not call setCursor() in itemChange() -
601 as this notification is delivered. Instead, you can return a new cursor -
602 from itemChange(). -
603 -
604 \value ItemCursorHasChanged The item's cursor has changed. The value -
605 argument is the new cursor (i.e., a QCursor). Do not call setCursor() as -
606 this notification is delivered. The return value is ignored. -
607 -
608 \value ItemToolTipChange The item's tooltip changes. The value argument is -
609 the new tooltip (i.e., a QToolTip). Do not call setToolTip() in -
610 itemChange() as this notification is delivered. Instead, you can return a -
611 new tooltip from itemChange(). -
612 -
613 \value ItemToolTipHasChanged The item's tooltip has changed. The value -
614 argument is the new tooltip (i.e., a QToolTip). Do not call setToolTip() -
615 as this notification is delivered. The return value is ignored. -
616 -
617 \value ItemFlagsChange The item's flags change. The value argument is the -
618 new flags (i.e., a quint32). Do not call setFlags() in itemChange() as -
619 this notification is delivered. Instead, you can return the new flags from -
620 itemChange(). -
621 -
622 \value ItemFlagsHaveChanged The item's flags have changed. The value -
623 argument is the new flags (i.e., a quint32). Do not call setFlags() in -
624 itemChange() as this notification is delivered. The return value is -
625 ignored. -
626 -
627 \value ItemZValueChange The item's Z-value changes. The value argument is -
628 the new Z-value (i.e., a double). Do not call setZValue() in itemChange() -
629 as this notification is delivered. Instead, you can return a new Z-value -
630 from itemChange(). -
631 -
632 \value ItemZValueHasChanged The item's Z-value has changed. The value -
633 argument is the new Z-value (i.e., a double). Do not call setZValue() as -
634 this notification is delivered. The return value is ignored. -
635 -
636 \value ItemOpacityChange The item's opacity changes. The value argument is -
637 the new opacity (i.e., a double). Do not call setOpacity() in itemChange() -
638 as this notification is delivered. Instead, you can return a new opacity -
639 from itemChange(). -
640 -
641 \value ItemOpacityHasChanged The item's opacity has changed. The value -
642 argument is the new opacity (i.e., a double). Do not call setOpacity() as -
643 this notification is delivered. The return value is ignored. -
644 -
645 \value ItemScenePositionHasChanged The item's scene position has changed. -
646 This notification is sent if the ItemSendsScenePositionChanges flag is -
647 enabled, and after the item's scene position has changed (i.e., the -
648 position or transformation of the item itself or the position or -
649 transformation of any ancestor has changed). The value argument is the -
650 new scene position (the same as scenePos()), and QGraphicsItem ignores -
651 the return value for this notification (i.e., a read-only notification). -
652*/ -
653 -
654/*! -
655 \enum QGraphicsItem::CacheMode -
656 \since 4.4 -
657 -
658 This enum describes QGraphicsItem's cache modes. Caching is used to speed -
659 up rendering by allocating and rendering to an off-screen pixel buffer, -
660 which can be reused when the item requires redrawing. For some paint -
661 devices, the cache is stored directly in graphics memory, which makes -
662 rendering very quick. -
663 -
664 \value NoCache The default; all item caching is -
665 disabled. QGraphicsItem::paint() is called every time the item needs -
666 redrawing. -
667 -
668 \value ItemCoordinateCache Caching is enabled for the item's logical -
669 (local) coordinate system. QGraphicsItem creates an off-screen pixel -
670 buffer with a configurable size / resolution that you can pass to -
671 QGraphicsItem::setCacheMode(). Rendering quality will typically degrade, -
672 depending on the resolution of the cache and the item transformation. The -
673 first time the item is redrawn, it will render itself into the cache, and -
674 the cache is then reused for every subsequent expose. The cache is also -
675 reused as the item is transformed. To adjust the resolution of the cache, -
676 you can call setCacheMode() again. -
677 -
678 \value DeviceCoordinateCache Caching is enabled at the paint device level, -
679 in device coordinates. This mode is for items that can move, but are not -
680 rotated, scaled or sheared. If the item is transformed directly or -
681 indirectly, the cache will be regenerated automatically. Unlike -
682 ItemCoordinateCacheMode, DeviceCoordinateCache always renders at maximum -
683 quality. -
684 -
685 \sa QGraphicsItem::setCacheMode() -
686*/ -
687 -
688/*! -
689 \enum QGraphicsItem::Extension -
690 \internal -
691 -
692 Note: This is provided as a hook to avoid future problems related -
693 to adding virtual functions. See also extension(), -
694 supportsExtension() and setExtension(). -
695*/ -
696 -
697/*! -
698 \enum QGraphicsItem::PanelModality -
699 \since 4.6 -
700 -
701 This enum specifies the behavior of a modal panel. A modal panel -
702 is one that blocks input to other panels. Note that items that -
703 are children of a modal panel are not blocked. -
704 -
705 The values are: -
706 -
707 \value NonModal The panel is not modal and does not block input to -
708 other panels. This is the default value for panels. -
709 -
710 \value PanelModal The panel is modal to a single item hierarchy -
711 and blocks input to its parent pane, all grandparent panels, and -
712 all siblings of its parent and grandparent panels. -
713 -
714 \value SceneModal The window is modal to the entire scene and -
715 blocks input to all panels. -
716 -
717 \sa QGraphicsItem::setPanelModality(), QGraphicsItem::panelModality(), QGraphicsItem::ItemIsPanel -
718*/ -
719 -
720#include "qgraphicsitem.h" -
721 -
722#ifndef QT_NO_GRAPHICSVIEW -
723 -
724#include "qgraphicsscene.h" -
725#include "qgraphicsscene_p.h" -
726#include "qgraphicssceneevent.h" -
727#include "qgraphicsview.h" -
728#include "qgraphicswidget.h" -
729#include "qgraphicsproxywidget.h" -
730#include "qgraphicsscenebsptreeindex_p.h" -
731#include <QtCore/qbitarray.h> -
732#include <QtCore/qdebug.h> -
733#include <QtCore/qpoint.h> -
734#include <QtCore/qstack.h> -
735#include <QtCore/qtimer.h> -
736#include <QtCore/qvariant.h> -
737#include <QtCore/qvarlengtharray.h> -
738#include <QtCore/qnumeric.h> -
739#include <QtWidgets/qapplication.h> -
740#include <QtGui/qbitmap.h> -
741#include <QtGui/qpainter.h> -
742#include <QtGui/qpainterpath.h> -
743#include <QtGui/qpixmapcache.h> -
744#include <QtWidgets/qstyleoption.h> -
745#include <QtGui/qevent.h> -
746#include <QtGui/qinputmethod.h> -
747#include <QtWidgets/qgraphicseffect.h> -
748 -
749#ifndef QT_NO_ACCESSIBILITY-
# include "qaccessible.h"
#endif#include <private/qgraphicsitem_p.h>
750#include <private/qgraphicswidget_p.h> -
751#include <private/qwidgettextcontrol_p.h> -
752#include <private/qtextdocumentlayout_p.h> -
753#include <private/qtextengine_p.h> -
754#include <private/qwidget_p.h> -
755#include <private/qapplication_p.h> -
756#include <private/qgesturemanager_p.h> -
757 -
758#include <math.h> -
759 -
760QT_BEGIN_NAMESPACE -
761 -
762static inline void _q_adjustRect(QRect *rect) -
763{ -
764 Q_ASSERT(rect); -
765 if (!rect->width()) -
766 rect->adjust(0, 0, 1, 0); -
767 if (!rect->height()) -
768 rect->adjust(0, 0, 0, 1); -
769} -
770 -
771/* -
772 ### Move this into QGraphicsItemPrivate -
773 */ -
774class QGraphicsItemCustomDataStore -
775{ -
776public: -
777 QHash<const QGraphicsItem *, QMap<int, QVariant> > data; -
778}; -
779Q_GLOBAL_STATIC(QGraphicsItemCustomDataStore, qt_dataStore) -
780 -
781/*! -
782 \internal -
783 -
784 Returns a QPainterPath of \a path when stroked with the \a pen. -
785 Ignoring dash pattern. -
786*/ -
787static QPainterPath qt_graphicsItem_shapeFromPath(const QPainterPath &path, const QPen &pen) -
788{ -
789 // We unfortunately need this hack as QPainterPathStroker will set a width of 1.0 -
790 // if we pass a value of 0.0 to QPainterPathStroker::setWidth() -
791 const qreal penWidthZero = qreal(0.00000001); -
792 -
793 if (path == QPainterPath()) -
794 return path; -
795 QPainterPathStroker ps; -
796 ps.setCapStyle(pen.capStyle()); -
797 if (pen.widthF() <= 0.0) -
798 ps.setWidth(penWidthZero); -
799 else -
800 ps.setWidth(pen.widthF()); -
801 ps.setJoinStyle(pen.joinStyle()); -
802 ps.setMiterLimit(pen.miterLimit()); -
803 QPainterPath p = ps.createStroke(path); -
804 p.addPath(path); -
805 return p; -
806} -
807 -
808/*! -
809 \internal -
810 -
811 Propagates the ancestor flag \a flag with value \a enabled to all this -
812 item's children. If \a root is false, the flag is also set on this item -
813 (default is true). -
814*/ -
815void QGraphicsItemPrivate::updateAncestorFlag(QGraphicsItem::GraphicsItemFlag childFlag, -
816 AncestorFlag flag, bool enabled, bool root) -
817{ -
818 Q_Q(QGraphicsItem); -
819 if (root) { -
820 // For root items only. This is the item that has either enabled or -
821 // disabled \a childFlag, or has been reparented. -
822 switch (int(childFlag)) { -
823 case -2: -
824 flag = AncestorFiltersChildEvents; -
825 enabled = q->filtersChildEvents(); -
826 break; -
827 case -1: -
828 flag = AncestorHandlesChildEvents; -
829 enabled = q->handlesChildEvents(); -
830 break; -
831 case QGraphicsItem::ItemClipsChildrenToShape: -
832 flag = AncestorClipsChildren; -
833 enabled = flags & QGraphicsItem::ItemClipsChildrenToShape; -
834 break; -
835 case QGraphicsItem::ItemIgnoresTransformations: -
836 flag = AncestorIgnoresTransformations; -
837 enabled = flags & QGraphicsItem::ItemIgnoresTransformations; -
838 break; -
839 default: -
840 return; -
841 } -
842 -
843 if (parent) { -
844 // Inherit the enabled-state from our parents. -
845 if ((parent->d_ptr->ancestorFlags & flag) -
846 || (int(parent->d_ptr->flags & childFlag) == childFlag) -
847 || (childFlag == -1 && parent->d_ptr->handlesChildEvents) -
848 || (childFlag == -2 && parent->d_ptr->filtersDescendantEvents)) { -
849 enabled = true; -
850 ancestorFlags |= flag; -
851 } else { -
852 ancestorFlags &= ~flag; -
853 } -
854 } else { -
855 // Top-level root items don't have any ancestors, so there are no -
856 // ancestor flags either. -
857 ancestorFlags = 0; -
858 } -
859 } else { -
860 // Don't set or propagate the ancestor flag if it's already correct. -
861 if (((ancestorFlags & flag) && enabled) || (!(ancestorFlags & flag) && !enabled)) -
862 return; -
863 -
864 // Set the flag. -
865 if (enabled) -
866 ancestorFlags |= flag; -
867 else -
868 ancestorFlags &= ~flag; -
869 -
870 // Don't process children if the item has the main flag set on itself. -
871 if ((childFlag != -1 && int(flags & childFlag) == childFlag) -
872 || (int(childFlag) == -1 && handlesChildEvents) -
873 || (int(childFlag) == -2 && filtersDescendantEvents)) -
874 return; -
875 } -
876 -
877 for (int i = 0; i < children.size(); ++i) -
878 children.at(i)->d_ptr->updateAncestorFlag(childFlag, flag, enabled, false); -
879} -
880 -
881void QGraphicsItemPrivate::updateAncestorFlags() -
882{ -
883 int flags = 0; -
884 if (parent) { -
885 // Inherit the parent's ancestor flags. -
886 QGraphicsItemPrivate *pd = parent->d_ptr.data(); -
887 flags = pd->ancestorFlags; -
888 -
889 // Add in flags from the parent. -
890 if (pd->filtersDescendantEvents) -
891 flags |= AncestorFiltersChildEvents; -
892 if (pd->handlesChildEvents) -
893 flags |= AncestorHandlesChildEvents; -
894 if (pd->flags & QGraphicsItem::ItemClipsChildrenToShape) -
895 flags |= AncestorClipsChildren; -
896 if (pd->flags & QGraphicsItem::ItemIgnoresTransformations) -
897 flags |= AncestorIgnoresTransformations; -
898 } -
899 -
900 if (ancestorFlags == flags) -
901 return; // No change; stop propagation. -
902 ancestorFlags = flags; -
903 -
904 // Propagate to children recursively. -
905 for (int i = 0; i < children.size(); ++i) -
906 children.at(i)->d_ptr->updateAncestorFlags(); -
907} -
908 -
909/*! -
910 \internal -
911 -
912 Propagates item group membership. -
913*/ -
914void QGraphicsItemPrivate::setIsMemberOfGroup(bool enabled) -
915{ -
916 Q_Q(QGraphicsItem); -
917 isMemberOfGroup = enabled; -
918 if (!qgraphicsitem_cast<QGraphicsItemGroup *>(q)) { -
919 foreach (QGraphicsItem *child, children) -
920 child->d_func()->setIsMemberOfGroup(enabled); -
921 } -
922} -
923 -
924/*! -
925 \internal -
926 -
927 Maps any item pos properties of \a event to \a item's coordinate system. -
928*/ -
929void QGraphicsItemPrivate::remapItemPos(QEvent *event, QGraphicsItem *item) -
930{ -
931 Q_Q(QGraphicsItem); -
932 switch (event->type()) { -
933 case QEvent::GraphicsSceneMouseMove: -
934 case QEvent::GraphicsSceneMousePress: -
935 case QEvent::GraphicsSceneMouseRelease: -
936 case QEvent::GraphicsSceneMouseDoubleClick: { -
937 QGraphicsSceneMouseEvent *mouseEvent = static_cast<QGraphicsSceneMouseEvent *>(event); -
938 mouseEvent->setPos(item->mapFromItem(q, mouseEvent->pos())); -
939 mouseEvent->setLastPos(item->mapFromItem(q, mouseEvent->pos())); -
940 for (int i = 0x1; i <= 0x10; i <<= 1) { -
941 if (mouseEvent->buttons() & i) { -
942 Qt::MouseButton button = Qt::MouseButton(i); -
943 mouseEvent->setButtonDownPos(button, item->mapFromItem(q, mouseEvent->buttonDownPos(button))); -
944 } -
945 } -
946 break; -
947 } -
948 case QEvent::GraphicsSceneWheel: { -
949 QGraphicsSceneWheelEvent *wheelEvent = static_cast<QGraphicsSceneWheelEvent *>(event); -
950 wheelEvent->setPos(item->mapFromItem(q, wheelEvent->pos())); -
951 break; -
952 } -
953 case QEvent::GraphicsSceneContextMenu: { -
954 QGraphicsSceneContextMenuEvent *contextEvent = static_cast<QGraphicsSceneContextMenuEvent *>(event); -
955 contextEvent->setPos(item->mapFromItem(q, contextEvent->pos())); -
956 break; -
957 } -
958 case QEvent::GraphicsSceneHoverMove: { -
959 QGraphicsSceneHoverEvent *hoverEvent = static_cast<QGraphicsSceneHoverEvent *>(event); -
960 hoverEvent->setPos(item->mapFromItem(q, hoverEvent->pos())); -
961 break; -
962 } -
963 default: -
964 break; -
965 } -
966} -
967 -
968/*! -
969 \internal -
970 -
971 Maps the point \a pos from scene to item coordinates. If \a view is passed and the item -
972 is untransformable, this function will correctly map \a pos from the scene using the -
973 view's transformation. -
974*/ -
975QPointF QGraphicsItemPrivate::genericMapFromScene(const QPointF &pos, -
976 const QWidget *viewport) const -
977{ -
978 Q_Q(const QGraphicsItem); -
979 if (!itemIsUntransformable()) -
980 return q->mapFromScene(pos); -
981 QGraphicsView *view = 0; -
982 if (viewport) -
983 view = qobject_cast<QGraphicsView *>(viewport->parentWidget()); -
984 if (!view) -
985 return q->mapFromScene(pos); -
986 // ### More ping pong than needed. -
987 return q->deviceTransform(view->viewportTransform()).inverted().map(view->mapFromScene(pos)); -
988} -
989 -
990/*! -
991 \internal -
992 -
993 Combines this item's position and transform onto \a transform. -
994 -
995 If you need to change this function (e.g., adding more transformation -
996 modes / options), make sure to change all places marked with COMBINE. -
997*/ -
998void QGraphicsItemPrivate::combineTransformToParent(QTransform *x, const QTransform *viewTransform) const -
999{ -
1000 // COMBINE -
1001 if (viewTransform && itemIsUntransformable()) { -
1002 *x = q_ptr->deviceTransform(*viewTransform); -
1003 } else { -
1004 if (transformData) -
1005 *x *= transformData->computedFullTransform(); -
1006 if (!pos.isNull()) -
1007 *x *= QTransform::fromTranslate(pos.x(), pos.y()); -
1008 } -
1009} -
1010 -
1011/*! -
1012 \internal -
1013 -
1014 Combines this item's position and transform onto \a transform. -
1015 -
1016 If you need to change this function (e.g., adding more transformation -
1017 modes / options), make sure to change QGraphicsItem::deviceTransform() as -
1018 well. -
1019*/ -
1020void QGraphicsItemPrivate::combineTransformFromParent(QTransform *x, const QTransform *viewTransform) const -
1021{ -
1022 // COMBINE -
1023 if (viewTransform && itemIsUntransformable()) { -
1024 *x = q_ptr->deviceTransform(*viewTransform); -
1025 } else { -
1026 x->translate(pos.x(), pos.y()); -
1027 if (transformData) -
1028 *x = transformData->computedFullTransform(x); -
1029 } -
1030} -
1031 -
1032void QGraphicsItemPrivate::updateSceneTransformFromParent() -
1033{ -
1034 if (parent) { -
1035 Q_ASSERT(!parent->d_ptr->dirtySceneTransform); -
1036 if (parent->d_ptr->sceneTransformTranslateOnly) { -
1037 sceneTransform = QTransform::fromTranslate(parent->d_ptr->sceneTransform.dx() + pos.x(), -
1038 parent->d_ptr->sceneTransform.dy() + pos.y()); -
1039 } else { -
1040 sceneTransform = parent->d_ptr->sceneTransform; -
1041 sceneTransform.translate(pos.x(), pos.y()); -
1042 } -
1043 if (transformData) { -
1044 sceneTransform = transformData->computedFullTransform(&sceneTransform); -
1045 sceneTransformTranslateOnly = (sceneTransform.type() <= QTransform::TxTranslate); -
1046 } else { -
1047 sceneTransformTranslateOnly = parent->d_ptr->sceneTransformTranslateOnly; -
1048 } -
1049 } else if (!transformData) { -
1050 sceneTransform = QTransform::fromTranslate(pos.x(), pos.y()); -
1051 sceneTransformTranslateOnly = 1; -
1052 } else if (transformData->onlyTransform) { -
1053 sceneTransform = transformData->transform; -
1054 if (!pos.isNull()) -
1055 sceneTransform *= QTransform::fromTranslate(pos.x(), pos.y()); -
1056 sceneTransformTranslateOnly = (sceneTransform.type() <= QTransform::TxTranslate); -
1057 } else if (pos.isNull()) { -
1058 sceneTransform = transformData->computedFullTransform(); -
1059 sceneTransformTranslateOnly = (sceneTransform.type() <= QTransform::TxTranslate); -
1060 } else { -
1061 sceneTransform = QTransform::fromTranslate(pos.x(), pos.y()); -
1062 sceneTransform = transformData->computedFullTransform(&sceneTransform); -
1063 sceneTransformTranslateOnly = (sceneTransform.type() <= QTransform::TxTranslate); -
1064 } -
1065 dirtySceneTransform = 0; -
1066} -
1067 -
1068/*! -
1069 \internal -
1070 -
1071 Make sure not to trigger any pure virtual function calls (e.g., -
1072 prepareGeometryChange) if the item is in its destructor, i.e. -
1073 inDestructor is 1. -
1074*/ -
1075void QGraphicsItemPrivate::setParentItemHelper(QGraphicsItem *newParent, const QVariant *newParentVariant, -
1076 const QVariant *thisPointerVariant) -
1077{ -
1078 Q_Q(QGraphicsItem); -
1079 if (newParent == parent) -
1080 return; -
1081 -
1082 if (isWidget) -
1083 static_cast<QGraphicsWidgetPrivate *>(this)->fixFocusChainBeforeReparenting((newParent && -
1084 newParent->isWidget()) ? static_cast<QGraphicsWidget *>(newParent) : 0, -
1085 scene); -
1086 if (scene) { -
1087 // Deliver the change to the index -
1088 if (scene->d_func()->indexMethod != QGraphicsScene::NoIndex) -
1089 scene->d_func()->index->itemChange(q, QGraphicsItem::ItemParentChange, newParent); -
1090 -
1091 // Disable scene pos notifications for old ancestors -
1092 if (scenePosDescendants || (flags & QGraphicsItem::ItemSendsScenePositionChanges)) -
1093 scene->d_func()->setScenePosItemEnabled(q, false); -
1094 } -
1095 -
1096 if (subFocusItem && parent) { -
1097 // Make sure none of the old parents point to this guy. -
1098 subFocusItem->d_ptr->clearSubFocus(parent); -
1099 } -
1100 -
1101 // We anticipate geometry changes. If the item is deleted, it will be -
1102 // removed from the index at a later stage, and the whole scene will be -
1103 // updated. -
1104 if (!inDestructor) -
1105 q_ptr->prepareGeometryChange(); -
1106 -
1107 if (parent) { -
1108 // Remove from current parent -
1109 parent->d_ptr->removeChild(q); -
1110 if (thisPointerVariant) -
1111 parent->itemChange(QGraphicsItem::ItemChildRemovedChange, *thisPointerVariant); -
1112 } -
1113 -
1114 // Update toplevelitem list. If this item is being deleted, its parent -
1115 // will be 0 but we don't want to register/unregister it in the TLI list. -
1116 if (scene && !inDestructor) { -
1117 if (parent && !newParent) { -
1118 scene->d_func()->registerTopLevelItem(q); -
1119 } else if (!parent && newParent) { -
1120 scene->d_func()->unregisterTopLevelItem(q); -
1121 } -
1122 } -
1123 -
1124 // Ensure any last parent focus scope does not point to this item or any of -
1125 // its descendents. -
1126 QGraphicsItem *p = parent; -
1127 QGraphicsItem *parentFocusScopeItem = 0; -
1128 while (p) { -
1129 if (p->d_ptr->flags & QGraphicsItem::ItemIsFocusScope) { -
1130 // If this item's focus scope's focus scope item points -
1131 // to this item or a descendent, then clear it. -
1132 QGraphicsItem *fsi = p->d_ptr->focusScopeItem; -
1133 if (q_ptr == fsi || q_ptr->isAncestorOf(fsi)) { -
1134 parentFocusScopeItem = fsi; -
1135 p->d_ptr->focusScopeItem = 0; -
1136 fsi->d_ptr->focusScopeItemChange(false); -
1137 } -
1138 break; -
1139 } -
1140 p = p->d_ptr->parent; -
1141 } -
1142 -
1143 // Update graphics effect optimization flag -
1144 if (newParent && (graphicsEffect || mayHaveChildWithGraphicsEffect)) -
1145 newParent->d_ptr->updateChildWithGraphicsEffectFlagRecursively(); -
1146 -
1147 // Update focus scope item ptr in new scope. -
1148 QGraphicsItem *newFocusScopeItem = subFocusItem ? subFocusItem : parentFocusScopeItem; -
1149 if (newFocusScopeItem && newParent) { -
1150 QGraphicsItem *p = newParent; -
1151 while (p) { -
1152 if (p->d_ptr->flags & QGraphicsItem::ItemIsFocusScope) { -
1153 if (subFocusItem && subFocusItem != q_ptr) { -
1154 // Find the subFocusItem's topmost focus scope within the new parent's focusscope -
1155 QGraphicsItem *ancestorScope = 0; -
1156 QGraphicsItem *p2 = subFocusItem->d_ptr->parent; -
1157 while (p2 && p2 != p) { -
1158 if (p2->d_ptr->flags & QGraphicsItem::ItemIsFocusScope) -
1159 ancestorScope = p2; -
1160 if (p2->d_ptr->flags & QGraphicsItem::ItemIsPanel) -
1161 break; -
1162 if (p2 == q_ptr) -
1163 break; -
1164 p2 = p2->d_ptr->parent; -
1165 } -
1166 if (ancestorScope) -
1167 newFocusScopeItem = ancestorScope; -
1168 } -
1169 -
1170 p->d_ptr->focusScopeItem = newFocusScopeItem; -
1171 newFocusScopeItem->d_ptr->focusScopeItemChange(true); -
1172 // Ensure the new item is no longer the subFocusItem. The -
1173 // only way to set focus on a child of a focus scope is -
1174 // by setting focus on the scope itself. -
1175 if (subFocusItem && !p->focusItem()) -
1176 subFocusItem->d_ptr->clearSubFocus(); -
1177 break; -
1178 } -
1179 p = p->d_ptr->parent; -
1180 } -
1181 } -
1182 -
1183 // Resolve depth. -
1184 invalidateDepthRecursively(); -
1185 -
1186 if ((parent = newParent)) { -
1187 if (parent->d_func()->scene && parent->d_func()->scene != scene) { -
1188 // Move this item to its new parent's scene -
1189 parent->d_func()->scene->addItem(q); -
1190 } else if (!parent->d_func()->scene && scene) { -
1191 // Remove this item from its former scene -
1192 scene->removeItem(q); -
1193 } -
1194 -
1195 parent->d_ptr->addChild(q); -
1196 if (thisPointerVariant) -
1197 parent->itemChange(QGraphicsItem::ItemChildAddedChange, *thisPointerVariant); -
1198 if (scene) { -
1199 // Re-enable scene pos notifications for new ancestors -
1200 if (scenePosDescendants || (flags & QGraphicsItem::ItemSendsScenePositionChanges)) -
1201 scene->d_func()->setScenePosItemEnabled(q, true); -
1202 } -
1203 -
1204 // Propagate dirty flags to the new parent -
1205 markParentDirty(/*updateBoundingRect=*/true); -
1206 -
1207 // Inherit ancestor flags from the new parent. -
1208 updateAncestorFlags(); -
1209 -
1210 // Update item visible / enabled. -
1211 if (parent->d_ptr->visible != visible) { -
1212 if (!parent->d_ptr->visible || !explicitlyHidden) -
1213 setVisibleHelper(parent->d_ptr->visible, /* explicit = */ false, /* update = */ false); -
1214 } -
1215 if (parent->isEnabled() != enabled) { -
1216 if (!parent->d_ptr->enabled || !explicitlyDisabled) -
1217 setEnabledHelper(parent->d_ptr->enabled, /* explicit = */ false, /* update = */ false); -
1218 } -
1219 -
1220 // Auto-activate if visible and the parent is active. -
1221 if (visible && parent->isActive()) -
1222 q->setActive(true); -
1223 } else { -
1224 // Inherit ancestor flags from the new parent. -
1225 updateAncestorFlags(); -
1226 -
1227 if (!inDestructor) { -
1228 // Update item visible / enabled. -
1229 if (!visible && !explicitlyHidden) -
1230 setVisibleHelper(true, /* explicit = */ false); -
1231 if (!enabled && !explicitlyDisabled) -
1232 setEnabledHelper(true, /* explicit = */ false); -
1233 } -
1234 } -
1235 -
1236 dirtySceneTransform = 1; -
1237 if (!inDestructor && (transformData || (newParent && newParent->d_ptr->transformData))) -
1238 transformChanged(); -
1239 -
1240 // Restore the sub focus chain. -
1241 if (subFocusItem) { -
1242 subFocusItem->d_ptr->setSubFocus(newParent); -
1243 if (parent && parent->isActive()) -
1244 subFocusItem->setFocus(); -
1245 } -
1246 -
1247 // Deliver post-change notification -
1248 if (newParentVariant) -
1249 q->itemChange(QGraphicsItem::ItemParentHasChanged, *newParentVariant); -
1250 -
1251 if (isObject) -
1252 emit static_cast<QGraphicsObject *>(q)->parentChanged(); -
1253} -
1254 -
1255/*! -
1256 \internal -
1257 -
1258 Returns the bounding rect of this item's children (excluding itself). -
1259*/ -
1260void QGraphicsItemPrivate::childrenBoundingRectHelper(QTransform *x, QRectF *rect, QGraphicsItem *topMostEffectItem) -
1261{ -
1262 Q_Q(QGraphicsItem); -
1263 -
1264 QRectF childrenRect; -
1265 QRectF *result = rect; -
1266 rect = &childrenRect; -
1267 const bool setTopMostEffectItem = !topMostEffectItem; -
1268 -
1269 for (int i = 0; i < children.size(); ++i) { -
1270 QGraphicsItem *child = children.at(i); -
1271 QGraphicsItemPrivate *childd = child->d_ptr.data(); -
1272 if (setTopMostEffectItem) -
1273 topMostEffectItem = child; -
1274 bool hasPos = !childd->pos.isNull(); -
1275 if (hasPos || childd->transformData) { -
1276 // COMBINE -
1277 QTransform matrix = childd->transformToParent(); -
1278 if (x) -
1279 matrix *= *x; -
1280 *rect |= matrix.mapRect(child->d_ptr->effectiveBoundingRect(topMostEffectItem)); -
1281 if (!childd->children.isEmpty()) -
1282 childd->childrenBoundingRectHelper(&matrix, rect, topMostEffectItem); -
1283 } else { -
1284 if (x) -
1285 *rect |= x->mapRect(child->d_ptr->effectiveBoundingRect(topMostEffectItem)); -
1286 else -
1287 *rect |= child->d_ptr->effectiveBoundingRect(topMostEffectItem); -
1288 if (!childd->children.isEmpty()) -
1289 childd->childrenBoundingRectHelper(x, rect, topMostEffectItem); -
1290 } -
1291 } -
1292 -
1293 if (flags & QGraphicsItem::ItemClipsChildrenToShape){ -
1294 if (x) -
1295 *rect &= x->mapRect(q->boundingRect()); -
1296 else -
1297 *rect &= q->boundingRect(); -
1298 } -
1299 -
1300 *result |= *rect; -
1301} -
1302 -
1303void QGraphicsItemPrivate::initStyleOption(QStyleOptionGraphicsItem *option, const QTransform &worldTransform, -
1304 const QRegion &exposedRegion, bool allItems) const -
1305{ -
1306 Q_ASSERT(option); -
1307 Q_Q(const QGraphicsItem); -
1308 -
1309 // Initialize standard QStyleOption values. -
1310 const QRectF brect = q->boundingRect(); -
1311 option->state = QStyle::State_None; -
1312 option->rect = brect.toRect(); -
1313 option->levelOfDetail = 1; -
1314 option->exposedRect = brect; -
1315 -
1316 // Style animations require a QObject-based animation target. -
1317 // If a plain QGraphicsItem is used to draw animated controls, -
1318 // QStyle is let to send animation updates to the whole scene. -
1319 option->styleObject = q_ptr->toGraphicsObject(); -
1320 if (!option->styleObject) -
1321 option->styleObject = scene; -
1322 -
1323 if (selected) -
1324 option->state |= QStyle::State_Selected; -
1325 if (enabled) -
1326 option->state |= QStyle::State_Enabled; -
1327 if (q->hasFocus()) -
1328 option->state |= QStyle::State_HasFocus; -
1329 if (scene) { -
1330 if (scene->d_func()->hoverItems.contains(q_ptr)) -
1331 option->state |= QStyle::State_MouseOver; -
1332 if (q == scene->mouseGrabberItem()) -
1333 option->state |= QStyle::State_Sunken; -
1334 } -
1335 -
1336 if (!(flags & QGraphicsItem::ItemUsesExtendedStyleOption)) -
1337 return; -
1338 -
1339 // Initialize QStyleOptionGraphicsItem specific values (matrix, exposedRect). -
1340 option->matrix = worldTransform.toAffine(); //### discards perspective -
1341 -
1342 if (!allItems) { -
1343 // Determine the item's exposed area -
1344 option->exposedRect = QRectF(); -
1345 const QTransform reverseMap = worldTransform.inverted(); -
1346 const QVector<QRect> exposedRects(exposedRegion.rects()); -
1347 for (int i = 0; i < exposedRects.size(); ++i) { -
1348 option->exposedRect |= reverseMap.mapRect(QRectF(exposedRects.at(i))); -
1349 if (option->exposedRect.contains(brect)) -
1350 break; -
1351 } -
1352 option->exposedRect &= brect; -
1353 } -
1354} -
1355 -
1356/*! -
1357 \internal -
1358 -
1359 Empty all cached pixmaps from the pixmap cache. -
1360*/ -
1361void QGraphicsItemCache::purge() -
1362{ -
1363 QPixmapCache::remove(key); -
1364 key = QPixmapCache::Key(); -
1365 QMutableHashIterator<QPaintDevice *, DeviceData> it(deviceData); -
1366 while (it.hasNext()) { -
1367 DeviceData &data = it.next().value(); -
1368 QPixmapCache::remove(data.key); -
1369 data.cacheIndent = QPoint(); -
1370 } -
1371 deviceData.clear(); -
1372 allExposed = true; -
1373 exposed.clear(); -
1374} -
1375 -
1376/*! -
1377 Constructs a QGraphicsItem with the given \a parent item. -
1378 It does not modify the parent object returned by QObject::parent(). -
1379 -
1380 If \a parent is 0, you can add the item to a scene by calling -
1381 QGraphicsScene::addItem(). The item will then become a top-level item. -
1382 -
1383 \sa QGraphicsScene::addItem(), setParentItem() -
1384*/ -
1385QGraphicsItem::QGraphicsItem(QGraphicsItem *parent) -
1386 : d_ptr(new QGraphicsItemPrivate) -
1387{ -
1388 d_ptr->q_ptr = this; -
1389 setParentItem(parent); -
1390} -
1391 -
1392/*! -
1393 \internal -
1394*/ -
1395QGraphicsItem::QGraphicsItem(QGraphicsItemPrivate &dd, QGraphicsItem *parent) -
1396 : d_ptr(&dd) -
1397{ -
1398 d_ptr->q_ptr = this; -
1399 setParentItem(parent); -
1400} -
1401 -
1402/*! -
1403 Destroys the QGraphicsItem and all its children. If this item is currently -
1404 associated with a scene, the item will be removed from the scene before it -
1405 is deleted. -
1406 -
1407 \note It is more efficient to remove the item from the QGraphicsScene before -
1408 destroying the item. -
1409*/ -
1410QGraphicsItem::~QGraphicsItem() -
1411{ -
1412 if (d_ptr->isObject) { -
1413 QGraphicsObject *o = static_cast<QGraphicsObject *>(this); -
1414 QObjectPrivate *p = QObjectPrivate::get(o); -
1415 p->wasDeleted = true; -
1416 if (p->declarativeData) { -
1417 QAbstractDeclarativeData::destroyed(p->declarativeData, o); -
1418 p->declarativeData = 0; -
1419 } -
1420 } -
1421 -
1422 d_ptr->inDestructor = 1; -
1423 d_ptr->removeExtraItemCache(); -
1424 -
1425#ifndef QT_NO_GESTURES -
1426 if (d_ptr->isObject && !d_ptr->gestureContext.isEmpty()) { -
1427 QGraphicsObject *o = static_cast<QGraphicsObject *>(this); -
1428 if (QGestureManager *manager = QGestureManager::instance()) { -
1429 foreach (Qt::GestureType type, d_ptr->gestureContext.keys()) -
1430 manager->cleanupCachedGestures(o, type); -
1431 } -
1432 } -
1433#endif -
1434 -
1435 clearFocus(); -
1436 setFocusProxy(0); -
1437 -
1438 // Update focus scope item ptr. -
1439 QGraphicsItem *p = d_ptr->parent; -
1440 while (p) { -
1441 if (p->flags() & ItemIsFocusScope) { -
1442 if (p->d_ptr->focusScopeItem == this) -
1443 p->d_ptr->focusScopeItem = 0; -
1444 break; -
1445 } -
1446 p = p->d_ptr->parent; -
1447 } -
1448 -
1449 if (!d_ptr->children.isEmpty()) { -
1450 while (!d_ptr->children.isEmpty()) -
1451 delete d_ptr->children.first(); -
1452 Q_ASSERT(d_ptr->children.isEmpty()); -
1453 } -
1454 -
1455 if (d_ptr->scene) { -
1456 d_ptr->scene->d_func()->removeItemHelper(this); -
1457 } else { -
1458 d_ptr->resetFocusProxy(); -
1459 setParentItem(0); -
1460 } -
1461 -
1462#ifndef QT_NO_GRAPHICSEFFECT -
1463 delete d_ptr->graphicsEffect; -
1464#endif //QT_NO_GRAPHICSEFFECT -
1465 if (d_ptr->transformData) { -
1466 for(int i = 0; i < d_ptr->transformData->graphicsTransforms.size(); ++i) { -
1467 QGraphicsTransform *t = d_ptr->transformData->graphicsTransforms.at(i); -
1468 static_cast<QGraphicsTransformPrivate *>(t->d_ptr.data())->item = 0; -
1469 delete t; -
1470 } -
1471 } -
1472 delete d_ptr->transformData; -
1473 -
1474 if (QGraphicsItemCustomDataStore *dataStore = qt_dataStore()) -
1475 dataStore->data.remove(this); -
1476} -
1477 -
1478/*! -
1479 Returns the current scene for the item, or 0 if the item is not stored in -
1480 a scene. -
1481 -
1482 To add or move an item to a scene, call QGraphicsScene::addItem(). -
1483*/ -
1484QGraphicsScene *QGraphicsItem::scene() const -
1485{ -
1486 return d_ptr->scene; -
1487} -
1488 -
1489/*! -
1490 Returns a pointer to this item's item group, or 0 if this item is not -
1491 member of a group. -
1492 -
1493 \sa QGraphicsItemGroup, QGraphicsScene::createItemGroup() -
1494*/ -
1495QGraphicsItemGroup *QGraphicsItem::group() const -
1496{ -
1497 if (!d_ptr->isMemberOfGroup) -
1498 return 0; -
1499 QGraphicsItem *parent = const_cast<QGraphicsItem *>(this); -
1500 while ((parent = parent->d_ptr->parent)) { -
1501 if (QGraphicsItemGroup *group = qgraphicsitem_cast<QGraphicsItemGroup *>(parent)) -
1502 return group; -
1503 } -
1504 // Unreachable; if d_ptr->isMemberOfGroup is != 0, then one parent of this -
1505 // item is a group item. -
1506 return 0; -
1507} -
1508 -
1509/*! -
1510 Adds this item to the item group \a group. If \a group is 0, this item is -
1511 removed from any current group and added as a child of the previous -
1512 group's parent. -
1513 -
1514 \sa group(), QGraphicsScene::createItemGroup() -
1515*/ -
1516void QGraphicsItem::setGroup(QGraphicsItemGroup *group) -
1517{ -
1518 if (!group) { -
1519 if (QGraphicsItemGroup *group = this->group()) -
1520 group->removeFromGroup(this); -
1521 } else { -
1522 group->addToGroup(this); -
1523 } -
1524} -
1525 -
1526/*! -
1527 Returns a pointer to this item's parent item. If this item does not have a -
1528 parent, 0 is returned. -
1529 -
1530 \sa setParentItem(), childItems() -
1531*/ -
1532QGraphicsItem *QGraphicsItem::parentItem() const -
1533{ -
1534 return d_ptr->parent; -
1535} -
1536 -
1537/*! -
1538 Returns this item's top-level item. The top-level item is the item's -
1539 topmost ancestor item whose parent is 0. If an item has no parent, its own -
1540 pointer is returned (i.e., a top-level item is its own top-level item). -
1541 -
1542 \sa parentItem() -
1543*/ -
1544QGraphicsItem *QGraphicsItem::topLevelItem() const -
1545{ -
1546 QGraphicsItem *parent = const_cast<QGraphicsItem *>(this); -
1547 while (QGraphicsItem *grandPa = parent->parentItem()) -
1548 parent = grandPa; -
1549 return parent; -
1550} -
1551 -
1552/*! -
1553 \since 4.6 -
1554 -
1555 Returns a pointer to the item's parent, cast to a QGraphicsObject. returns 0 if the parent item -
1556 is not a QGraphicsObject. -
1557 -
1558 \sa parentItem(), childItems() -
1559*/ -
1560QGraphicsObject *QGraphicsItem::parentObject() const -
1561{ -
1562 QGraphicsItem *p = d_ptr->parent; -
1563 return (p && p->d_ptr->isObject) ? static_cast<QGraphicsObject *>(p) : 0; -
1564} -
1565 -
1566/*! -
1567 \since 4.4 -
1568 -
1569 Returns a pointer to the item's parent widget. The item's parent widget is -
1570 the closest parent item that is a widget. -
1571 -
1572 \sa parentItem(), childItems() -
1573*/ -
1574QGraphicsWidget *QGraphicsItem::parentWidget() const -
1575{ -
1576 QGraphicsItem *p = parentItem(); -
1577 while (p && !p->isWidget()) -
1578 p = p->parentItem(); -
1579 return (p && p->isWidget()) ? static_cast<QGraphicsWidget *>(p) : 0; -
1580} -
1581 -
1582/*! -
1583 \since 4.4 -
1584 -
1585 Returns a pointer to the item's top level widget (i.e., the item's -
1586 ancestor whose parent is 0, or whose parent is not a widget), or 0 if this -
1587 item does not have a top level widget. If the item is its own top level -
1588 widget, this function returns a pointer to the item itself. -
1589*/ -
1590QGraphicsWidget *QGraphicsItem::topLevelWidget() const -
1591{ -
1592 if (const QGraphicsWidget *p = parentWidget()) -
1593 return p->topLevelWidget(); -
1594 return isWidget() ? static_cast<QGraphicsWidget *>(const_cast<QGraphicsItem *>(this)) : 0; -
1595} -
1596 -
1597/*! -
1598 \since 4.4 -
1599 -
1600 Returns the item's window, or 0 if this item does not have a window. If -
1601 the item is a window, it will return itself. Otherwise it will return the -
1602 closest ancestor that is a window. -
1603 -
1604 \sa QGraphicsWidget::isWindow() -
1605*/ -
1606QGraphicsWidget *QGraphicsItem::window() const -
1607{ -
1608 QGraphicsItem *p = panel(); -
1609 if (p && p->isWindow()) -
1610 return static_cast<QGraphicsWidget *>(p); -
1611 return 0; -
1612} -
1613 -
1614/*! -
1615 \since 4.6 -
1616 -
1617 Returns the item's panel, or 0 if this item does not have a panel. If the -
1618 item is a panel, it will return itself. Otherwise it will return the -
1619 closest ancestor that is a panel. -
1620 -
1621 \sa isPanel(), ItemIsPanel -
1622*/ -
1623QGraphicsItem *QGraphicsItem::panel() const -
1624{ -
1625 if (d_ptr->flags & ItemIsPanel) -
1626 return const_cast<QGraphicsItem *>(this); -
1627 return d_ptr->parent ? d_ptr->parent->panel() : 0; -
1628} -
1629 -
1630/*! -
1631 \since 4.6 -
1632 -
1633 Return the graphics item cast to a QGraphicsObject, if the class is actually a -
1634 graphics object, 0 otherwise. -
1635*/ -
1636QGraphicsObject *QGraphicsItem::toGraphicsObject() -
1637{ -
1638 return d_ptr->isObject ? static_cast<QGraphicsObject *>(this) : 0; -
1639} -
1640 -
1641/*! -
1642 \since 4.6 -
1643 -
1644 Return the graphics item cast to a QGraphicsObject, if the class is actually a -
1645 graphics object, 0 otherwise. -
1646*/ -
1647const QGraphicsObject *QGraphicsItem::toGraphicsObject() const -
1648{ -
1649 return d_ptr->isObject ? static_cast<const QGraphicsObject *>(this) : 0; -
1650} -
1651 -
1652/*! -
1653 Sets this item's parent item to \a newParent. If this item already -
1654 has a parent, it is first removed from the previous parent. If \a -
1655 newParent is 0, this item will become a top-level item. -
1656 -
1657 Note that this implicitly adds this graphics item to the scene of -
1658 the parent. You should not \l{QGraphicsScene::addItem()}{add} the -
1659 item to the scene yourself. -
1660 -
1661 Calling this function on an item that is an ancestor of \a newParent -
1662 have undefined behaviour. -
1663 -
1664 \sa parentItem(), childItems() -
1665*/ -
1666void QGraphicsItem::setParentItem(QGraphicsItem *newParent) -
1667{ -
1668 if (newParent == this) { -
1669 qWarning("QGraphicsItem::setParentItem: cannot assign %p as a parent of itself", this); -
1670 return; -
1671 } -
1672 if (newParent == d_ptr->parent) -
1673 return; -
1674 -
1675 const QVariant newParentVariant(itemChange(QGraphicsItem::ItemParentChange, -
1676 QVariant::fromValue<QGraphicsItem *>(newParent))); -
1677 newParent = qvariant_cast<QGraphicsItem *>(newParentVariant); -
1678 if (newParent == d_ptr->parent) -
1679 return; -
1680 -
1681 const QVariant thisPointerVariant(QVariant::fromValue<QGraphicsItem *>(this)); -
1682 d_ptr->setParentItemHelper(newParent, &newParentVariant, &thisPointerVariant); -
1683} -
1684 -
1685/*! -
1686 \fn QList<QGraphicsItem *> QGraphicsItem::children() const -
1687 \obsolete -
1688 -
1689 Use childItems() instead. -
1690 -
1691 \sa setParentItem() -
1692*/ -
1693 -
1694/*! -
1695 \since 4.4 -
1696 -
1697 Returns a list of this item's children. -
1698 -
1699 The items are sorted by stacking order. This takes into account both the -
1700 items' insertion order and their Z-values. -
1701 -
1702 \sa setParentItem(), zValue(), {QGraphicsItem#Sorting}{Sorting} -
1703*/ -
1704QList<QGraphicsItem *> QGraphicsItem::childItems() const -
1705{ -
1706 const_cast<QGraphicsItem *>(this)->d_ptr->ensureSortedChildren(); -
1707 return d_ptr->children; -
1708} -
1709 -
1710/*! -
1711 \since 4.4 -
1712 Returns true if this item is a widget (i.e., QGraphicsWidget); otherwise, -
1713 returns false. -
1714*/ -
1715bool QGraphicsItem::isWidget() const -
1716{ -
1717 return d_ptr->isWidget; -
1718} -
1719 -
1720/*! -
1721 \since 4.4 -
1722 Returns true if the item is a QGraphicsWidget window, otherwise returns -
1723 false. -
1724 -
1725 \sa QGraphicsWidget::windowFlags() -
1726*/ -
1727bool QGraphicsItem::isWindow() const -
1728{ -
1729 return d_ptr->isWidget && (static_cast<const QGraphicsWidget *>(this)->windowType() & Qt::Window); -
1730} -
1731 -
1732/*! -
1733 \since 4.6 -
1734 Returns true if the item is a panel; otherwise returns false. -
1735 -
1736 \sa QGraphicsItem::panel(), ItemIsPanel -
1737*/ -
1738bool QGraphicsItem::isPanel() const -
1739{ -
1740 return d_ptr->flags & ItemIsPanel; -
1741} -
1742 -
1743/*! -
1744 Returns this item's flags. The flags describe what configurable features -
1745 of the item are enabled and not. For example, if the flags include -
1746 ItemIsFocusable, the item can accept input focus. -
1747 -
1748 By default, no flags are enabled. -
1749 -
1750 \sa setFlags(), setFlag() -
1751*/ -
1752QGraphicsItem::GraphicsItemFlags QGraphicsItem::flags() const -
1753{ -
1754 return GraphicsItemFlags(d_ptr->flags); -
1755} -
1756 -
1757/*! -
1758 If \a enabled is true, the item flag \a flag is enabled; otherwise, it is -
1759 disabled. -
1760 -
1761 \sa flags(), setFlags() -
1762*/ -
1763void QGraphicsItem::setFlag(GraphicsItemFlag flag, bool enabled) -
1764{ -
1765 if (enabled) -
1766 setFlags(GraphicsItemFlags(d_ptr->flags) | flag); -
1767 else -
1768 setFlags(GraphicsItemFlags(d_ptr->flags) & ~flag); -
1769} -
1770 -
1771/*! -
1772 Sets the item flags to \a flags. All flags in \a flags are enabled; all -
1773 flags not in \a flags are disabled. -
1774 -
1775 If the item had focus and \a flags does not enable ItemIsFocusable, the -
1776 item loses focus as a result of calling this function. Similarly, if the -
1777 item was selected, and \a flags does not enabled ItemIsSelectable, the -
1778 item is automatically unselected. -
1779 -
1780 By default, no flags are enabled. (QGraphicsWidget enables the -
1781 ItemSendsGeometryChanges flag by default in order to track position -
1782 changes.) -
1783 -
1784 \sa flags(), setFlag() -
1785*/ -
1786void QGraphicsItem::setFlags(GraphicsItemFlags flags) -
1787{ -
1788 // Notify change and check for adjustment. -
1789 if (quint32(d_ptr->flags) == quint32(flags)) -
1790 return; -
1791 flags = GraphicsItemFlags(itemChange(ItemFlagsChange, quint32(flags)).toUInt()); -
1792 if (quint32(d_ptr->flags) == quint32(flags)) -
1793 return; -
1794 if (d_ptr->scene && d_ptr->scene->d_func()->indexMethod != QGraphicsScene::NoIndex) -
1795 d_ptr->scene->d_func()->index->itemChange(this, ItemFlagsChange, &flags); -
1796 -
1797 // Flags that alter the geometry of the item (or its children). -
1798 const quint32 geomChangeFlagsMask = (ItemClipsChildrenToShape | ItemClipsToShape | ItemIgnoresTransformations | ItemIsSelectable); -
1799 bool fullUpdate = (quint32(flags) & geomChangeFlagsMask) != (d_ptr->flags & geomChangeFlagsMask); -
1800 if (fullUpdate) -
1801 d_ptr->updatePaintedViewBoundingRects(/*children=*/true); -
1802 -
1803 // Keep the old flags to compare the diff. -
1804 GraphicsItemFlags oldFlags = GraphicsItemFlags(d_ptr->flags); -
1805 -
1806 // Update flags. -
1807 d_ptr->flags = flags; -
1808 -
1809 if (!(d_ptr->flags & ItemIsFocusable) && hasFocus()) { -
1810 // Clear focus on the item if it has focus when the focusable flag -
1811 // is unset. -
1812 clearFocus(); -
1813 } -
1814 -
1815 if (!(d_ptr->flags & ItemIsSelectable) && isSelected()) { -
1816 // Unselect the item if it is selected when the selectable flag is -
1817 // unset. -
1818 setSelected(false); -
1819 } -
1820 -
1821 if ((flags & ItemClipsChildrenToShape) != (oldFlags & ItemClipsChildrenToShape)) { -
1822 // Item children clipping changes. Propagate the ancestor flag to -
1823 // all children. -
1824 d_ptr->updateAncestorFlag(ItemClipsChildrenToShape); -
1825 // The childrenBoundingRect is clipped to the boundingRect in case of ItemClipsChildrenToShape, -
1826 // which means we have to invalidate the cached childrenBoundingRect whenever this flag changes. -
1827 d_ptr->dirtyChildrenBoundingRect = 1; -
1828 d_ptr->markParentDirty(true); -
1829 } -
1830 -
1831 if ((flags & ItemIgnoresTransformations) != (oldFlags & ItemIgnoresTransformations)) { -
1832 // Item children clipping changes. Propagate the ancestor flag to -
1833 // all children. -
1834 d_ptr->updateAncestorFlag(ItemIgnoresTransformations); -
1835 } -
1836 -
1837 if ((flags & ItemNegativeZStacksBehindParent) != (oldFlags & ItemNegativeZStacksBehindParent)) { -
1838 // NB! We change the flags directly here, so we must also update d_ptr->flags. -
1839 // Note that this has do be done before the ItemStacksBehindParent check -
1840 // below; otherwise we will loose the change. -
1841 -
1842 // Update stack-behind. -
1843 if (d_ptr->z < qreal(0.0)) -
1844 flags |= ItemStacksBehindParent; -
1845 else -
1846 flags &= ~ItemStacksBehindParent; -
1847 d_ptr->flags = flags; -
1848 } -
1849 -
1850 if ((flags & ItemStacksBehindParent) != (oldFlags & ItemStacksBehindParent)) { -
1851 // NB! This check has to come after the ItemNegativeZStacksBehindParent -
1852 // check above. Be careful. -
1853 -
1854 // Ensure child item sorting is up to date when toggling this flag. -
1855 if (d_ptr->parent) -
1856 d_ptr->parent->d_ptr->needSortChildren = 1; -
1857 else if (d_ptr->scene) -
1858 d_ptr->scene->d_func()->needSortTopLevelItems = 1; -
1859 } -
1860 -
1861 if ((flags & ItemAcceptsInputMethod) != (oldFlags & ItemAcceptsInputMethod)) { -
1862 // Update input method sensitivity in any views. -
1863 if (d_ptr->scene) -
1864 d_ptr->scene->d_func()->updateInputMethodSensitivityInViews(); -
1865 } -
1866 -
1867 -
1868 if ((d_ptr->panelModality != NonModal) -
1869 && d_ptr->scene -
1870 && (flags & ItemIsPanel) != (oldFlags & ItemIsPanel)) { -
1871 // update the panel's modal state -
1872 if (flags & ItemIsPanel) -
1873 d_ptr->scene->d_func()->enterModal(this); -
1874 else -
1875 d_ptr->scene->d_func()->leaveModal(this); -
1876 } -
1877 -
1878 if (d_ptr->scene) { -
1879 if ((flags & ItemSendsScenePositionChanges) != (oldFlags & ItemSendsScenePositionChanges)) { -
1880 if (flags & ItemSendsScenePositionChanges) -
1881 d_ptr->scene->d_func()->registerScenePosItem(this); -
1882 else -
1883 d_ptr->scene->d_func()->unregisterScenePosItem(this); -
1884 } -
1885 d_ptr->scene->d_func()->markDirty(this, QRectF(), /*invalidateChildren=*/true); -
1886 } -
1887 -
1888 // Notify change. -
1889 itemChange(ItemFlagsHaveChanged, quint32(flags)); -
1890} -
1891 -
1892/*! -
1893 \since 4.4 -
1894 Returns the cache mode for this item. The default mode is NoCache (i.e., -
1895 cache is disabled and all painting is immediate). -
1896 -
1897 \sa setCacheMode() -
1898*/ -
1899QGraphicsItem::CacheMode QGraphicsItem::cacheMode() const -
1900{ -
1901 return QGraphicsItem::CacheMode(d_ptr->cacheMode); -
1902} -
1903 -
1904/*! -
1905 \since 4.4 -
1906 Sets the item's cache mode to \a mode. -
1907 -
1908 The optional \a logicalCacheSize argument is used only by -
1909 ItemCoordinateCache mode, and describes the resolution of the cache -
1910 buffer; if \a logicalCacheSize is (100, 100), QGraphicsItem will fit the -
1911 item into 100x100 pixels in graphics memory, regardless of the logical -
1912 size of the item itself. By default QGraphicsItem uses the size of -
1913 boundingRect(). For all other cache modes than ItemCoordinateCache, \a -
1914 logicalCacheSize is ignored. -
1915 -
1916 Caching can speed up rendering if your item spends a significant time -
1917 redrawing itself. In some cases the cache can also slow down rendering, in -
1918 particular when the item spends less time redrawing than QGraphicsItem -
1919 spends redrawing from the cache. When enabled, the item's paint() function -
1920 will be called only once for each call to update(); for any subsequent -
1921 repaint requests, the Graphics View framework will redraw from the -
1922 cache. This approach works particularly well with QGLWidget, which stores -
1923 all the cache as OpenGL textures. -
1924 -
1925 Be aware that QPixmapCache's cache limit may need to be changed to obtain -
1926 optimal performance. -
1927 -
1928 You can read more about the different cache modes in the CacheMode -
1929 documentation. -
1930 -
1931 \sa CacheMode, QPixmapCache::setCacheLimit() -
1932*/ -
1933void QGraphicsItem::setCacheMode(CacheMode mode, const QSize &logicalCacheSize) -
1934{ -
1935 CacheMode lastMode = CacheMode(d_ptr->cacheMode); -
1936 d_ptr->cacheMode = mode; -
1937 bool noVisualChange = (mode == NoCache && lastMode == NoCache) -
1938 || (mode == NoCache && lastMode == DeviceCoordinateCache) -
1939 || (mode == DeviceCoordinateCache && lastMode == NoCache) -
1940 || (mode == DeviceCoordinateCache && lastMode == DeviceCoordinateCache); -
1941 if (mode == NoCache) { -
1942 d_ptr->removeExtraItemCache(); -
1943 } else { -
1944 QGraphicsItemCache *cache = d_ptr->extraItemCache(); -
1945 -
1946 // Reset old cache -
1947 cache->purge(); -
1948 -
1949 if (mode == ItemCoordinateCache) { -
1950 if (lastMode == mode && cache->fixedSize == logicalCacheSize) -
1951 noVisualChange = true; -
1952 cache->fixedSize = logicalCacheSize; -
1953 } -
1954 } -
1955 if (!noVisualChange) -
1956 update(); -
1957} -
1958 -
1959/*! -
1960 \since 4.6 -
1961 -
1962 Returns the modality for this item. -
1963*/ -
1964QGraphicsItem::PanelModality QGraphicsItem::panelModality() const -
1965{ -
1966 return d_ptr->panelModality; -
1967} -
1968 -
1969/*! -
1970 \since 4.6 -
1971 -
1972 Sets the modality for this item to \a panelModality. -
1973 -
1974 Changing the modality of a visible item takes effect immediately. -
1975*/ -
1976void QGraphicsItem::setPanelModality(PanelModality panelModality) -
1977{ -
1978 if (d_ptr->panelModality == panelModality) -
1979 return; -
1980 -
1981 PanelModality previousModality = d_ptr->panelModality; -
1982 bool enterLeaveModal = (isPanel() && d_ptr->scene && isVisible()); -
1983 if (enterLeaveModal && panelModality == NonModal) -
1984 d_ptr->scene->d_func()->leaveModal(this); -
1985 d_ptr->panelModality = panelModality; -
1986 if (enterLeaveModal && d_ptr->panelModality != NonModal) -
1987 d_ptr->scene->d_func()->enterModal(this, previousModality); -
1988} -
1989 -
1990/*! -
1991 \since 4.6 -
1992 -
1993 Returns true if this item is blocked by a modal panel, false otherwise. If \a blockingPanel is -
1994 non-zero, \a blockingPanel will be set to the modal panel that is blocking this item. If this -
1995 item is not blocked, \a blockingPanel will not be set by this function. -
1996 -
1997 This function always returns false for items not in a scene. -
1998 -
1999 \sa panelModality(), setPanelModality(), PanelModality -
2000*/ -
2001bool QGraphicsItem::isBlockedByModalPanel(QGraphicsItem **blockingPanel) const -
2002{ -
2003 if (!d_ptr->scene) -
2004 return false; -
2005 -
2006 -
2007 QGraphicsItem *dummy = 0; -
2008 if (!blockingPanel) -
2009 blockingPanel = &dummy; -
2010 -
2011 QGraphicsScenePrivate *scene_d = d_ptr->scene->d_func(); -
2012 if (scene_d->modalPanels.isEmpty()) -
2013 return false; -
2014 -
2015 // ### -
2016 if (!scene_d->popupWidgets.isEmpty() && scene_d->popupWidgets.first() == this) -
2017 return false; -
2018 -
2019 for (int i = 0; i < scene_d->modalPanels.count(); ++i) { -
2020 QGraphicsItem *modalPanel = scene_d->modalPanels.at(i); -
2021 if (modalPanel->panelModality() == QGraphicsItem::SceneModal) { -
2022 // Scene modal panels block all non-descendents. -
2023 if (modalPanel != this && !modalPanel->isAncestorOf(this)) { -
2024 *blockingPanel = modalPanel; -
2025 return true; -
2026 } -
2027 } else { -
2028 // Window modal panels block ancestors and siblings/cousins. -
2029 if (modalPanel != this -
2030 && !modalPanel->isAncestorOf(this) -
2031 && commonAncestorItem(modalPanel)) { -
2032 *blockingPanel = modalPanel; -
2033 return true; -
2034 } -
2035 } -
2036 } -
2037 return false; -
2038} -
2039 -
2040#ifndef QT_NO_TOOLTIP -
2041/*! -
2042 Returns the item's tool tip, or an empty QString if no tool tip has been -
2043 set. -
2044 -
2045 \sa setToolTip(), QToolTip -
2046*/ -
2047QString QGraphicsItem::toolTip() const -
2048{ -
2049 return d_ptr->extra(QGraphicsItemPrivate::ExtraToolTip).toString(); -
2050} -
2051 -
2052/*! -
2053 Sets the item's tool tip to \a toolTip. If \a toolTip is empty, the item's -
2054 tool tip is cleared. -
2055 -
2056 \sa toolTip(), QToolTip -
2057*/ -
2058void QGraphicsItem::setToolTip(const QString &toolTip) -
2059{ -
2060 const QVariant toolTipVariant(itemChange(ItemToolTipChange, toolTip)); -
2061 d_ptr->setExtra(QGraphicsItemPrivate::ExtraToolTip, toolTipVariant.toString()); -
2062 itemChange(ItemToolTipHasChanged, toolTipVariant); -
2063} -
2064#endif // QT_NO_TOOLTIP -
2065 -
2066#ifndef QT_NO_CURSOR -
2067/*! -
2068 Returns the current cursor shape for the item. The mouse cursor -
2069 will assume this shape when it's over this item. -
2070 See the \l{Qt::CursorShape}{list of predefined cursor objects} for a -
2071 range of useful shapes. -
2072 -
2073 An editor item might want to use an I-beam cursor: -
2074 -
2075 \snippet code/src_gui_graphicsview_qgraphicsitem.cpp 2 -
2076 -
2077 If no cursor has been set, the cursor of the item beneath is used. -
2078 -
2079 \sa setCursor(), hasCursor(), unsetCursor(), QWidget::cursor, -
2080 QApplication::overrideCursor() -
2081*/ -
2082QCursor QGraphicsItem::cursor() const -
2083{ -
2084 return qvariant_cast<QCursor>(d_ptr->extra(QGraphicsItemPrivate::ExtraCursor)); -
2085} -
2086 -
2087/*! -
2088 Sets the current cursor shape for the item to \a cursor. The mouse cursor -
2089 will assume this shape when it's over this item. -
2090 See the \l{Qt::CursorShape}{list of predefined cursor objects} for a -
2091 range of useful shapes. -
2092 -
2093 An editor item might want to use an I-beam cursor: -
2094 -
2095 \snippet code/src_gui_graphicsview_qgraphicsitem.cpp 3 -
2096 -
2097 If no cursor has been set, the cursor of the item beneath is used. -
2098 -
2099 \sa cursor(), hasCursor(), unsetCursor(), QWidget::cursor, -
2100 QApplication::overrideCursor() -
2101*/ -
2102void QGraphicsItem::setCursor(const QCursor &cursor) -
2103{ -
2104 const QVariant cursorVariant(itemChange(ItemCursorChange, QVariant::fromValue<QCursor>(cursor))); -
2105 d_ptr->setExtra(QGraphicsItemPrivate::ExtraCursor, qvariant_cast<QCursor>(cursorVariant)); -
2106 d_ptr->hasCursor = 1; -
2107 if (d_ptr->scene) { -
2108 d_ptr->scene->d_func()->allItemsUseDefaultCursor = false; -
2109 foreach (QGraphicsView *view, d_ptr->scene->views()) { -
2110 view->viewport()->setMouseTracking(true); -
2111 // Note: Some of this logic is duplicated in QGraphicsView's mouse events. -
2112 if (view->underMouse()) { -
2113 foreach (QGraphicsItem *itemUnderCursor, view->items(view->mapFromGlobal(QCursor::pos()))) { -
2114 if (itemUnderCursor->hasCursor()) { -
2115 QMetaObject::invokeMethod(view, "_q_setViewportCursor", -
2116 Q_ARG(QCursor, itemUnderCursor->cursor())); -
2117 break; -
2118 } -
2119 } -
2120 break; -
2121 } -
2122 } -
2123 } -
2124 itemChange(ItemCursorHasChanged, cursorVariant); -
2125} -
2126 -
2127/*! -
2128 Returns true if this item has a cursor set; otherwise, false is returned. -
2129 -
2130 By default, items don't have any cursor set. cursor() will return a -
2131 standard pointing arrow cursor. -
2132 -
2133 \sa unsetCursor() -
2134*/ -
2135bool QGraphicsItem::hasCursor() const -
2136{ -
2137 return d_ptr->hasCursor; -
2138} -
2139 -
2140/*! -
2141 Clears the cursor from this item. -
2142 -
2143 \sa hasCursor(), setCursor() -
2144*/ -
2145void QGraphicsItem::unsetCursor() -
2146{ -
2147 if (!d_ptr->hasCursor)
never evaluated: !d_ptr->hasCursor
0
2148 return;
never executed: return;
0
2149 d_ptr->unsetExtra(QGraphicsItemPrivate::ExtraCursor);
never executed (the execution status of this line is deduced): d_ptr->unsetExtra(QGraphicsItemPrivate::ExtraCursor);
-
2150 d_ptr->hasCursor = 0;
never executed (the execution status of this line is deduced): d_ptr->hasCursor = 0;
-
2151 if (d_ptr->scene) {
never evaluated: d_ptr->scene
0
2152 foreach (QGraphicsView *view, d_ptr->scene->views()) {
never executed (the execution status of this line is deduced): for (QForeachContainer<__typeof__(d_ptr->scene->views())> _container_(d_ptr->scene->views()); !_container_.brk && _container_.i != _container_.e; __extension__ ({ ++_container_.brk; ++_container_.i; })) for (QGraphicsView *view = *_container_.i;; __extension__ ({--_container_.brk; break;})) {
-
2153 if (view->underMouse() && view->itemAt(view->mapFromGlobal(QCursor::pos())) == this) {
never evaluated: view->underMouse()
never evaluated: view->itemAt(view->mapFromGlobal(QCursor::pos())) == this
0
2154 QMetaObject::invokeMethod(view, "_q_unsetViewportCursor");
never executed (the execution status of this line is deduced): QMetaObject::invokeMethod(view, "_q_unsetViewportCursor");
-
2155 break;
never executed: break;
0
2156 } -
2157 }
never executed: }
0
2158 }
never executed: }
0
2159}
never executed: }
0
2160 -
2161#endif // QT_NO_CURSOR -
2162 -
2163/*! -
2164 Returns true if the item is visible; otherwise, false is returned. -
2165 -
2166 Note that the item's general visibility is unrelated to whether or not it -
2167 is actually being visualized by a QGraphicsView. -
2168 -
2169 \sa setVisible() -
2170*/ -
2171bool QGraphicsItem::isVisible() const -
2172{ -
2173 return d_ptr->visible; -
2174} -
2175 -
2176/*! -
2177 \since 4.4 -
2178 Returns true if the item is visible to \a parent; otherwise, false is -
2179 returned. \a parent can be 0, in which case this function will return -
2180 whether the item is visible to the scene or not. -
2181 -
2182 An item may not be visible to its ancestors even if isVisible() is true. It -
2183 may also be visible to its ancestors even if isVisible() is false. If -
2184 any ancestor is hidden, the item itself will be implicitly hidden, in which -
2185 case this function will return false. -
2186 -
2187 \sa isVisible(), setVisible() -
2188*/ -
2189bool QGraphicsItem::isVisibleTo(const QGraphicsItem *parent) const -
2190{ -
2191 const QGraphicsItem *p = this; -
2192 if (d_ptr->explicitlyHidden) -
2193 return false; -
2194 do { -
2195 if (p == parent) -
2196 return true; -
2197 if (p->d_ptr->explicitlyHidden) -
2198 return false; -
2199 } while ((p = p->d_ptr->parent)); -
2200 return parent == 0; -
2201} -
2202 -
2203/*! -
2204 \internal -
2205 -
2206 Sets this item's visibility to \a newVisible. If \a explicitly is true, -
2207 this item will be "explicitly" \a newVisible; otherwise, it.. will not be. -
2208*/ -
2209void QGraphicsItemPrivate::setVisibleHelper(bool newVisible, bool explicitly, -
2210 bool update, bool hiddenByPanel) -
2211{ -
2212 Q_Q(QGraphicsItem); -
2213 -
2214 // Update explicit bit. -
2215 if (explicitly) -
2216 explicitlyHidden = newVisible ? 0 : 1; -
2217 -
2218 // Check if there's nothing to do. -
2219 if (visible == quint32(newVisible)) -
2220 return; -
2221 -
2222 // Don't show child if parent is not visible -
2223 if (parent && newVisible && !parent->d_ptr->visible) -
2224 return; -
2225 -
2226 // Modify the property. -
2227 const QVariant newVisibleVariant(q_ptr->itemChange(QGraphicsItem::ItemVisibleChange, -
2228 quint32(newVisible))); -
2229 newVisible = newVisibleVariant.toBool(); -
2230 if (visible == quint32(newVisible)) -
2231 return; -
2232 visible = newVisible; -
2233 -
2234 // Schedule redrawing -
2235 if (update) { -
2236 QGraphicsItemCache *c = (QGraphicsItemCache *)qvariant_cast<void *>(extra(ExtraCacheData)); -
2237 if (c) -
2238 c->purge(); -
2239 if (scene) { -
2240#ifndef QT_NO_GRAPHICSEFFECT -
2241 invalidateParentGraphicsEffectsRecursively(); -
2242#endif //QT_NO_GRAPHICSEFFECT -
2243 scene->d_func()->markDirty(q_ptr, QRectF(), /*invalidateChildren=*/false, /*force=*/true); -
2244 } -
2245 } -
2246 -
2247 // Certain properties are dropped as an item becomes invisible. -
2248 bool hasFocus = q_ptr->hasFocus(); -
2249 if (!newVisible) { -
2250 if (scene) { -
2251 if (scene->d_func()->mouseGrabberItems.contains(q)) -
2252 q->ungrabMouse(); -
2253 if (scene->d_func()->keyboardGrabberItems.contains(q)) -
2254 q->ungrabKeyboard(); -
2255 if (q->isPanel() && panelModality != QGraphicsItem::NonModal) -
2256 scene->d_func()->leaveModal(q_ptr); -
2257 } -
2258 if (hasFocus && scene) { -
2259 // Hiding the closest non-panel ancestor of the focus item -
2260 QGraphicsItem *focusItem = scene->focusItem(); -
2261 bool clear = true; -
2262 if (isWidget && !focusItem->isPanel()) { -
2263 do { -
2264 if (focusItem == q_ptr) { -
2265 clear = !static_cast<QGraphicsWidget *>(q_ptr)->focusNextPrevChild(true); -
2266 break; -
2267 } -
2268 } while ((focusItem = focusItem->parentWidget()) && !focusItem->isPanel()); -
2269 } -
2270 if (clear) -
2271 clearFocusHelper(/* giveFocusToParent = */ false, hiddenByPanel); -
2272 } -
2273 if (q_ptr->isSelected()) -
2274 q_ptr->setSelected(false); -
2275 } else { -
2276 geometryChanged = 1; -
2277 paintedViewBoundingRectsNeedRepaint = 1; -
2278 if (scene) { -
2279 if (isWidget) { -
2280 QGraphicsWidget *widget = static_cast<QGraphicsWidget *>(q_ptr); -
2281 if (widget->windowType() == Qt::Popup) -
2282 scene->d_func()->addPopup(widget); -
2283 } -
2284 if (q->isPanel() && panelModality != QGraphicsItem::NonModal) { -
2285 scene->d_func()->enterModal(q_ptr); -
2286 } -
2287 } -
2288 } -
2289 -
2290 // Update children with explicitly = false. -
2291 const bool updateChildren = update && !((flags & QGraphicsItem::ItemClipsChildrenToShape) -
2292 && !(flags & QGraphicsItem::ItemHasNoContents)); -
2293 foreach (QGraphicsItem *child, children) { -
2294 if (!newVisible || !child->d_ptr->explicitlyHidden) -
2295 child->d_ptr->setVisibleHelper(newVisible, false, updateChildren, hiddenByPanel); -
2296 } -
2297 -
2298 // Update activation -
2299 if (scene && q->isPanel()) { -
2300 if (newVisible) { -
2301 if (parent && parent->isActive()) -
2302 q->setActive(true); -
2303 } else { -
2304 if (q->isActive()) -
2305 scene->setActivePanel(parent); -
2306 } -
2307 } -
2308 -
2309 // Enable subfocus -
2310 if (scene) { -
2311 if (newVisible) { -
2312 // Item is shown -
2313 QGraphicsItem *p = parent; -
2314 bool done = false; -
2315 while (p) { -
2316 if (p->flags() & QGraphicsItem::ItemIsFocusScope) { -
2317 QGraphicsItem *fsi = p->d_ptr->focusScopeItem; -
2318 if (q_ptr == fsi || q_ptr->isAncestorOf(fsi)) { -
2319 done = true; -
2320 while (fsi->d_ptr->focusScopeItem && fsi->d_ptr->focusScopeItem->isVisible()) -
2321 fsi = fsi->d_ptr->focusScopeItem; -
2322 fsi->d_ptr->setFocusHelper(Qt::OtherFocusReason, /* climb = */ true, -
2323 /* focusFromHide = */ false); -
2324 } -
2325 break; -
2326 } -
2327 p = p->d_ptr->parent; -
2328 } -
2329 if (!done) { -
2330 QGraphicsItem *fi = subFocusItem; -
2331 if (fi && fi != scene->focusItem()) { -
2332 scene->setFocusItem(fi); -
2333 } else if (flags & QGraphicsItem::ItemIsFocusScope && -
2334 !scene->focusItem() && -
2335 q->isAncestorOf(scene->d_func()->lastFocusItem)) { -
2336 q_ptr->setFocus(); -
2337 } -
2338 } -
2339 } else { -
2340 // Item is hidden -
2341 if (hasFocus) { -
2342 QGraphicsItem *p = parent; -
2343 while (p) { -
2344 if (p->flags() & QGraphicsItem::ItemIsFocusScope) { -
2345 if (p->d_ptr->visible) { -
2346 p->d_ptr->setFocusHelper(Qt::OtherFocusReason, /* climb = */ true, -
2347 /* focusFromHide = */ true); -
2348 } -
2349 break; -
2350 } -
2351 p = p->d_ptr->parent; -
2352 } -
2353 } -
2354 } -
2355 } -
2356 -
2357 // Deliver post-change notification. -
2358 q_ptr->itemChange(QGraphicsItem::ItemVisibleHasChanged, newVisibleVariant); -
2359 -
2360 if (isObject) -
2361 emit static_cast<QGraphicsObject *>(q_ptr)->visibleChanged(); -
2362} -
2363 -
2364/*! -
2365 If \a visible is true, the item is made visible. Otherwise, the item is -
2366 made invisible. Invisible items are not painted, nor do they receive any -
2367 events. In particular, mouse events pass right through invisible items, -
2368 and are delivered to any item that may be behind. Invisible items are also -
2369 unselectable, they cannot take input focus, and are not detected by -
2370 QGraphicsScene's item location functions. -
2371 -
2372 If an item becomes invisible while grabbing the mouse, (i.e., while it is -
2373 receiving mouse events,) it will automatically lose the mouse grab, and -
2374 the grab is not regained by making the item visible again; it must receive -
2375 a new mouse press to regain the mouse grab. -
2376 -
2377 Similarly, an invisible item cannot have focus, so if the item has focus -
2378 when it becomes invisible, it will lose focus, and the focus is not -
2379 regained by simply making the item visible again. -
2380 -
2381 If you hide a parent item, all its children will also be hidden. If you -
2382 show a parent item, all children will be shown, unless they have been -
2383 explicitly hidden (i.e., if you call setVisible(false) on a child, it will -
2384 not be reshown even if its parent is hidden, and then shown again). -
2385 -
2386 Items are visible by default; it is unnecessary to call -
2387 setVisible() on a new item. -
2388 -
2389 \sa isVisible(), show(), hide() -
2390*/ -
2391void QGraphicsItem::setVisible(bool visible) -
2392{ -
2393 d_ptr->setVisibleHelper(visible, -
2394 /* explicit = */ true, -
2395 /* update = */ true, -
2396 /* hiddenByPanel = */ isPanel()); -
2397} -
2398 -
2399/*! -
2400 \fn void QGraphicsItem::hide() -
2401 -
2402 Hides the item (items are visible by default). -
2403 -
2404 This convenience function is equivalent to calling \c setVisible(false). -
2405 -
2406 \sa show(), setVisible() -
2407*/ -
2408 -
2409/*! -
2410 \fn void QGraphicsItem::show() -
2411 -
2412 Shows the item (items are visible by default). -
2413 -
2414 This convenience function is equivalent to calling \c setVisible(true). -
2415 -
2416 \sa hide(), setVisible() -
2417*/ -
2418 -
2419/*! -
2420 Returns true if the item is enabled; otherwise, false is returned. -
2421 -
2422 \sa setEnabled() -
2423*/ -
2424bool QGraphicsItem::isEnabled() const -
2425{ -
2426 return d_ptr->enabled; -
2427} -
2428 -
2429/*! -
2430 \internal -
2431 -
2432 Sets this item's visibility to \a newEnabled. If \a explicitly is true, -
2433 this item will be "explicitly" \a newEnabled; otherwise, it.. will not be. -
2434*/ -
2435void QGraphicsItemPrivate::setEnabledHelper(bool newEnabled, bool explicitly, bool update) -
2436{ -
2437 // Update explicit bit. -
2438 if (explicitly) -
2439 explicitlyDisabled = newEnabled ? 0 : 1; -
2440 -
2441 // Check if there's nothing to do. -
2442 if (enabled == quint32(newEnabled)) -
2443 return; -
2444 -
2445 // Certain properties are dropped when an item is disabled. -
2446 if (!newEnabled) { -
2447 if (scene && scene->mouseGrabberItem() == q_ptr) -
2448 q_ptr->ungrabMouse(); -
2449 if (q_ptr->hasFocus()) { -
2450 // Disabling the closest non-panel ancestor of the focus item -
2451 // causes focus to pop to the next item, otherwise it's cleared. -
2452 QGraphicsItem *focusItem = scene->focusItem(); -
2453 bool clear = true; -
2454 if (isWidget && !focusItem->isPanel() && q_ptr->isAncestorOf(focusItem)) { -
2455 do { -
2456 if (focusItem == q_ptr) { -
2457 clear = !static_cast<QGraphicsWidget *>(q_ptr)->focusNextPrevChild(true); -
2458 break; -
2459 } -
2460 } while ((focusItem = focusItem->parentWidget()) && !focusItem->isPanel()); -
2461 } -
2462 if (clear) -
2463 q_ptr->clearFocus(); -
2464 } -
2465 if (q_ptr->isSelected()) -
2466 q_ptr->setSelected(false); -
2467 } -
2468 -
2469 // Modify the property. -
2470 const QVariant newEnabledVariant(q_ptr->itemChange(QGraphicsItem::ItemEnabledChange, -
2471 quint32(newEnabled))); -
2472 enabled = newEnabledVariant.toBool(); -
2473 -
2474 // Schedule redraw. -
2475 if (update) -
2476 q_ptr->update(); -
2477 -
2478 foreach (QGraphicsItem *child, children) { -
2479 if (!newEnabled || !child->d_ptr->explicitlyDisabled) -
2480 child->d_ptr->setEnabledHelper(newEnabled, /* explicitly = */ false); -
2481 } -
2482 -
2483 // Deliver post-change notification. -
2484 q_ptr->itemChange(QGraphicsItem::ItemEnabledHasChanged, newEnabledVariant); -
2485 -
2486 if (isObject) -
2487 emit static_cast<QGraphicsObject *>(q_ptr)->enabledChanged(); -
2488} -
2489 -
2490/*! -
2491 If \a enabled is true, the item is enabled; otherwise, it is disabled. -
2492 -
2493 Disabled items are visible, but they do not receive any events, and cannot -
2494 take focus nor be selected. Mouse events are discarded; they are not -
2495 propagated unless the item is also invisible, or if it does not accept -
2496 mouse events (see acceptedMouseButtons()). A disabled item cannot become the -
2497 mouse grabber, and as a result of this, an item loses the grab if it -
2498 becomes disabled when grabbing the mouse, just like it loses focus if it -
2499 had focus when it was disabled. -
2500 -
2501 Disabled items are traditionally drawn using grayed-out colors (see \l -
2502 QPalette::Disabled). -
2503 -
2504 If you disable a parent item, all its children will also be disabled. If -
2505 you enable a parent item, all children will be enabled, unless they have -
2506 been explicitly disabled (i.e., if you call setEnabled(false) on a child, -
2507 it will not be reenabled if its parent is disabled, and then enabled -
2508 again). -
2509 -
2510 Items are enabled by default. -
2511 -
2512 \note If you install an event filter, you can still intercept events -
2513 before they are delivered to items; this mechanism disregards the item's -
2514 enabled state. -
2515 -
2516 \sa isEnabled() -
2517*/ -
2518void QGraphicsItem::setEnabled(bool enabled) -
2519{ -
2520 d_ptr->setEnabledHelper(enabled, /* explicitly = */ true); -
2521} -
2522 -
2523/*! -
2524 Returns true if this item is selected; otherwise, false is returned. -
2525 -
2526 Items that are in a group inherit the group's selected state. -
2527 -
2528 Items are not selected by default. -
2529 -
2530 \sa setSelected(), QGraphicsScene::setSelectionArea() -
2531*/ -
2532bool QGraphicsItem::isSelected() const -
2533{ -
2534 if (QGraphicsItemGroup *group = this->group()) -
2535 return group->isSelected(); -
2536 return d_ptr->selected; -
2537} -
2538 -
2539/*! -
2540 If \a selected is true and this item is selectable, this item is selected; -
2541 otherwise, it is unselected. -
2542 -
2543 If the item is in a group, the whole group's selected state is toggled by -
2544 this function. If the group is selected, all items in the group are also -
2545 selected, and if the group is not selected, no item in the group is -
2546 selected. -
2547 -
2548 Only visible, enabled, selectable items can be selected. If \a selected -
2549 is true and this item is either invisible or disabled or unselectable, -
2550 this function does nothing. -
2551 -
2552 By default, items cannot be selected. To enable selection, set the -
2553 ItemIsSelectable flag. -
2554 -
2555 This function is provided for convenience, allowing individual toggling of -
2556 the selected state of an item. However, a more common way of selecting -
2557 items is to call QGraphicsScene::setSelectionArea(), which will call this -
2558 function for all visible, enabled, and selectable items within a specified -
2559 area on the scene. -
2560 -
2561 \sa isSelected(), QGraphicsScene::selectedItems() -
2562*/ -
2563void QGraphicsItem::setSelected(bool selected) -
2564{ -
2565 if (QGraphicsItemGroup *group = this->group()) { -
2566 group->setSelected(selected); -
2567 return; -
2568 } -
2569 -
2570 if (!(d_ptr->flags & ItemIsSelectable) || !d_ptr->enabled || !d_ptr->visible) -
2571 selected = false; -
2572 if (d_ptr->selected == selected) -
2573 return; -
2574 const QVariant newSelectedVariant(itemChange(ItemSelectedChange, quint32(selected))); -
2575 bool newSelected = newSelectedVariant.toBool(); -
2576 if (d_ptr->selected == newSelected) -
2577 return; -
2578 d_ptr->selected = newSelected; -
2579 -
2580 update(); -
2581 if (d_ptr->scene) { -
2582 QGraphicsScenePrivate *sceneD = d_ptr->scene->d_func(); -
2583 if (selected) { -
2584 sceneD->selectedItems << this; -
2585 } else { -
2586 // QGraphicsScene::selectedItems() lazily pulls out all items that are -
2587 // no longer selected. -
2588 } -
2589 if (!sceneD->selectionChanging) -
2590 emit d_ptr->scene->selectionChanged(); -
2591 } -
2592 -
2593 // Deliver post-change notification. -
2594 itemChange(QGraphicsItem::ItemSelectedHasChanged, newSelectedVariant); -
2595} -
2596 -
2597/*! -
2598 \since 4.5 -
2599 -
2600 Returns this item's local opacity, which is between 0.0 (transparent) and -
2601 1.0 (opaque). This value is combined with parent and ancestor values into -
2602 the effectiveOpacity(). The effective opacity decides how the item is -
2603 rendered and also affects its visibility when queried by functions such as -
2604 QGraphicsView::items(). -
2605 -
2606 The opacity property decides the state of the painter passed to the -
2607 paint() function. If the item is cached, i.e., ItemCoordinateCache or -
2608 DeviceCoordinateCache, the effective property will be applied to the item's -
2609 cache as it is rendered. -
2610 -
2611 The default opacity is 1.0; fully opaque. -
2612 -
2613 \sa setOpacity(), paint(), ItemIgnoresParentOpacity, -
2614 ItemDoesntPropagateOpacityToChildren -
2615*/ -
2616qreal QGraphicsItem::opacity() const -
2617{ -
2618 return d_ptr->opacity; -
2619} -
2620 -
2621/*! -
2622 \since 4.5 -
2623 -
2624 Returns this item's \e effective opacity, which is between 0.0 -
2625 (transparent) and 1.0 (opaque). This value is a combination of this item's -
2626 local opacity, and its parent and ancestors' opacities. The effective -
2627 opacity decides how the item is rendered. -
2628 -
2629 \sa opacity(), setOpacity(), paint(), ItemIgnoresParentOpacity, -
2630 ItemDoesntPropagateOpacityToChildren -
2631*/ -
2632qreal QGraphicsItem::effectiveOpacity() const -
2633{ -
2634 return d_ptr->effectiveOpacity(); -
2635} -
2636 -
2637/*! -
2638 \since 4.5 -
2639 -
2640 Sets this item's local \a opacity, between 0.0 (transparent) and 1.0 -
2641 (opaque). The item's local opacity is combined with parent and ancestor -
2642 opacities into the effectiveOpacity(). -
2643 -
2644 By default, opacity propagates from parent to child, so if a parent's -
2645 opacity is 0.5 and the child is also 0.5, the child's effective opacity -
2646 will be 0.25. -
2647 -
2648 The opacity property decides the state of the painter passed to the -
2649 paint() function. If the item is cached, i.e., ItemCoordinateCache or -
2650 DeviceCoordinateCache, the effective property will be applied to the -
2651 item's cache as it is rendered. -
2652 -
2653 There are two item flags that affect how the item's opacity is combined -
2654 with the parent: ItemIgnoresParentOpacity and -
2655 ItemDoesntPropagateOpacityToChildren. -
2656 -
2657 \sa opacity(), effectiveOpacity() -
2658*/ -
2659void QGraphicsItem::setOpacity(qreal opacity) -
2660{ -
2661 // Notify change. -
2662 const QVariant newOpacityVariant(itemChange(ItemOpacityChange, opacity)); -
2663 -
2664 // Normalized opacity -
2665 qreal newOpacity = qBound(qreal(0), newOpacityVariant.toReal(), qreal(1)); -
2666 -
2667 // No change? Done. -
2668 if (newOpacity == d_ptr->opacity) -
2669 return; -
2670 -
2671 bool wasFullyTransparent = d_ptr->isOpacityNull(); -
2672 d_ptr->opacity = newOpacity; -
2673 -
2674 // Notify change. -
2675 itemChange(ItemOpacityHasChanged, newOpacityVariant); -
2676 -
2677 // Update. -
2678 if (d_ptr->scene) { -
2679#ifndef QT_NO_GRAPHICSEFFECT -
2680 d_ptr->invalidateParentGraphicsEffectsRecursively(); -
2681 if (!(d_ptr->flags & ItemDoesntPropagateOpacityToChildren)) -
2682 d_ptr->invalidateChildGraphicsEffectsRecursively(QGraphicsItemPrivate::OpacityChanged); -
2683#endif //QT_NO_GRAPHICSEFFECT -
2684 d_ptr->scene->d_func()->markDirty(this, QRectF(), -
2685 /*invalidateChildren=*/true, -
2686 /*force=*/false, -
2687 /*ignoreOpacity=*/d_ptr->isOpacityNull()); -
2688 if (wasFullyTransparent) -
2689 d_ptr->paintedViewBoundingRectsNeedRepaint = 1; -
2690 } -
2691 -
2692 if (d_ptr->isObject) -
2693 emit static_cast<QGraphicsObject *>(this)->opacityChanged(); -
2694} -
2695 -
2696/*! -
2697 Returns a pointer to this item's effect if it has one; otherwise 0. -
2698 -
2699 \since 4.6 -
2700*/ -
2701#ifndef QT_NO_GRAPHICSEFFECT -
2702QGraphicsEffect *QGraphicsItem::graphicsEffect() const -
2703{ -
2704 return d_ptr->graphicsEffect; -
2705} -
2706 -
2707/*! -
2708 Sets \a effect as the item's effect. If there already is an effect installed -
2709 on this item, QGraphicsItem will delete the existing effect before installing -
2710 the new \a effect. You can delete an existing effect by calling -
2711 setGraphicsEffect(0). -
2712 -
2713 If \a effect is the installed on a different item, setGraphicsEffect() will remove -
2714 the effect from the item and install it on this item. -
2715 -
2716 QGraphicsItem takes ownership of \a effect. -
2717 -
2718 \note This function will apply the effect on itself and all its children. -
2719 -
2720 \since 4.6 -
2721*/ -
2722void QGraphicsItem::setGraphicsEffect(QGraphicsEffect *effect) -
2723{ -
2724 if (d_ptr->graphicsEffect == effect) -
2725 return; -
2726 -
2727 if (d_ptr->graphicsEffect) { -
2728 delete d_ptr->graphicsEffect; -
2729 d_ptr->graphicsEffect = 0; -
2730 } else if (d_ptr->parent) { -
2731 d_ptr->parent->d_ptr->updateChildWithGraphicsEffectFlagRecursively(); -
2732 } -
2733 -
2734 if (effect) { -
2735 // Set new effect. -
2736 QGraphicsEffectSourcePrivate *sourced = new QGraphicsItemEffectSourcePrivate(this); -
2737 QGraphicsEffectSource *source = new QGraphicsEffectSource(*sourced); -
2738 d_ptr->graphicsEffect = effect; -
2739 effect->d_func()->setGraphicsEffectSource(source); -
2740 prepareGeometryChange(); -
2741 } -
2742} -
2743#endif //QT_NO_GRAPHICSEFFECT -
2744 -
2745void QGraphicsItemPrivate::updateChildWithGraphicsEffectFlagRecursively() -
2746{ -
2747#ifndef QT_NO_GRAPHICSEFFECT -
2748 QGraphicsItemPrivate *itemPrivate = this; -
2749 do { -
2750 // parent chain already notified? -
2751 if (itemPrivate->mayHaveChildWithGraphicsEffect) -
2752 return; -
2753 itemPrivate->mayHaveChildWithGraphicsEffect = 1; -
2754 } while ((itemPrivate = itemPrivate->parent ? itemPrivate->parent->d_ptr.data() : 0)); -
2755#endif -
2756} -
2757 -
2758/*! -
2759 \internal -
2760 \since 4.6 -
2761 Returns the effective bounding rect of the given item space rect. -
2762 If the item has no effect, the rect is returned unmodified. -
2763 If the item has an effect, the effective rect can be extend beyond the -
2764 item's bounding rect, depending on the effect. -
2765 -
2766 \sa boundingRect() -
2767*/ -
2768QRectF QGraphicsItemPrivate::effectiveBoundingRect(const QRectF &rect) const -
2769{ -
2770#ifndef QT_NO_GRAPHICSEFFECT -
2771 Q_Q(const QGraphicsItem); -
2772 QGraphicsEffect *effect = graphicsEffect; -
2773 if (scene && effect && effect->isEnabled()) { -
2774 if (scene->d_func()->views.isEmpty()) -
2775 return effect->boundingRectFor(rect); -
2776 QRectF sceneRect = q->mapRectToScene(rect); -
2777 QRectF sceneEffectRect; -
2778 foreach (QGraphicsView *view, scene->views()) { -
2779 QRectF deviceRect = view->d_func()->mapRectFromScene(sceneRect); -
2780 QRect deviceEffectRect = effect->boundingRectFor(deviceRect).toAlignedRect(); -
2781 sceneEffectRect |= view->d_func()->mapRectToScene(deviceEffectRect); -
2782 } -
2783 return q->mapRectFromScene(sceneEffectRect); -
2784 } -
2785#endif //QT_NO_GRAPHICSEFFECT -
2786 return rect; -
2787} -
2788 -
2789/*! -
2790 \internal -
2791 \since 4.6 -
2792 Returns the effective bounding rect of the item. -
2793 If the item has no effect, this is the same as the item's bounding rect. -
2794 If the item has an effect, the effective rect can be larger than the item's -
2795 bouding rect, depending on the effect. -
2796 -
2797 \sa boundingRect() -
2798*/ -
2799QRectF QGraphicsItemPrivate::effectiveBoundingRect(QGraphicsItem *topMostEffectItem) const -
2800{ -
2801#ifndef QT_NO_GRAPHICSEFFECT -
2802 Q_Q(const QGraphicsItem); -
2803 QRectF brect = effectiveBoundingRect(q_ptr->boundingRect()); -
2804 if (ancestorFlags & QGraphicsItemPrivate::AncestorClipsChildren || topMostEffectItem == q) -
2805 return brect; -
2806 -
2807 const QGraphicsItem *effectParent = parent; -
2808 while (effectParent) { -
2809 QGraphicsEffect *effect = effectParent->d_ptr->graphicsEffect; -
2810 if (scene && effect && effect->isEnabled()) { -
2811 const QRectF brectInParentSpace = q->mapRectToItem(effectParent, brect); -
2812 const QRectF effectRectInParentSpace = effectParent->d_ptr->effectiveBoundingRect(brectInParentSpace); -
2813 brect = effectParent->mapRectToItem(q, effectRectInParentSpace); -
2814 } -
2815 if (effectParent->d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorClipsChildren -
2816 || topMostEffectItem == effectParent) { -
2817 return brect; -
2818 } -
2819 effectParent = effectParent->d_ptr->parent; -
2820 } -
2821 -
2822 return brect; -
2823#else //QT_NO_GRAPHICSEFFECT -
2824 return q_ptr->boundingRect(); -
2825#endif //QT_NO_GRAPHICSEFFECT -
2826 -
2827} -
2828 -
2829/*! -
2830 \internal -
2831 \since 4.6 -
2832 Returns the effective bounding rect of this item in scene coordinates, -
2833 by combining sceneTransform() with boundingRect(), taking into account -
2834 the effect that the item might have. -
2835 -
2836 If the item has no effect, this is the same as sceneBoundingRect(). -
2837 -
2838 \sa effectiveBoundingRect(), sceneBoundingRect() -
2839*/ -
2840QRectF QGraphicsItemPrivate::sceneEffectiveBoundingRect() const -
2841{ -
2842 // Find translate-only offset -
2843 // COMBINE -
2844 QPointF offset; -
2845 const QGraphicsItem *parentItem = q_ptr; -
2846 const QGraphicsItemPrivate *itemd; -
2847 do { -
2848 itemd = parentItem->d_ptr.data(); -
2849 if (itemd->transformData) -
2850 break; -
2851 offset += itemd->pos; -
2852 } while ((parentItem = itemd->parent)); -
2853 -
2854 QRectF br = effectiveBoundingRect(); -
2855 br.translate(offset); -
2856 return !parentItem ? br : parentItem->sceneTransform().mapRect(br); -
2857} -
2858 -
2859/*! -
2860 Returns true if this item can accept drag and drop events; otherwise, -
2861 returns false. By default, items do not accept drag and drop events; items -
2862 are transparent to drag and drop. -
2863 -
2864 \sa setAcceptDrops() -
2865*/ -
2866bool QGraphicsItem::acceptDrops() const -
2867{ -
2868 return d_ptr->acceptDrops; -
2869} -
2870 -
2871/*! -
2872 If \a on is true, this item will accept drag and drop events; otherwise, -
2873 it is transparent for drag and drop events. By default, items do not -
2874 accept drag and drop events. -
2875 -
2876 \sa acceptDrops() -
2877*/ -
2878void QGraphicsItem::setAcceptDrops(bool on) -
2879{ -
2880 d_ptr->acceptDrops = on; -
2881} -
2882 -
2883/*! -
2884 Returns the mouse buttons that this item accepts mouse events for. By -
2885 default, all mouse buttons are accepted. -
2886 -
2887 If an item accepts a mouse button, it will become the mouse -
2888 grabber item when a mouse press event is delivered for that mouse -
2889 button. However, if the item does not accept the button, -
2890 QGraphicsScene will forward the mouse events to the first item -
2891 beneath it that does. -
2892 -
2893 \sa setAcceptedMouseButtons(), mousePressEvent() -
2894*/ -
2895Qt::MouseButtons QGraphicsItem::acceptedMouseButtons() const -
2896{ -
2897 return Qt::MouseButtons(d_ptr->acceptedMouseButtons); -
2898} -
2899 -
2900/*! -
2901 Sets the mouse \a buttons that this item accepts mouse events for. -
2902 -
2903 By default, all mouse buttons are accepted. If an item accepts a -
2904 mouse button, it will become the mouse grabber item when a mouse -
2905 press event is delivered for that button. However, if the item -
2906 does not accept the mouse button, QGraphicsScene will forward the -
2907 mouse events to the first item beneath it that does. -
2908 -
2909 To disable mouse events for an item (i.e., make it transparent for mouse -
2910 events), call setAcceptedMouseButtons(0). -
2911 -
2912 \sa acceptedMouseButtons(), mousePressEvent() -
2913*/ -
2914void QGraphicsItem::setAcceptedMouseButtons(Qt::MouseButtons buttons) -
2915{ -
2916 if (Qt::MouseButtons(d_ptr->acceptedMouseButtons) != buttons) { -
2917 if (buttons == 0 && d_ptr->scene && d_ptr->scene->mouseGrabberItem() == this -
2918 && d_ptr->scene->d_func()->lastMouseGrabberItemHasImplicitMouseGrab) { -
2919 ungrabMouse(); -
2920 } -
2921 d_ptr->acceptedMouseButtons = quint32(buttons); -
2922 } -
2923} -
2924 -
2925/*! -
2926 \since 4.4 -
2927 -
2928 Returns true if an item accepts hover events -
2929 (QGraphicsSceneHoverEvent); otherwise, returns false. By default, -
2930 items do not accept hover events. -
2931 -
2932 \sa setAcceptedMouseButtons() -
2933*/ -
2934bool QGraphicsItem::acceptHoverEvents() const -
2935{ -
2936 return d_ptr->acceptsHover; -
2937} -
2938 -
2939/*! -
2940 \fn bool QGraphicsItem::acceptsHoverEvents() const -
2941 \obsolete -
2942 -
2943 Call acceptHoverEvents() instead. -
2944*/ -
2945 -
2946/*! -
2947 \since 4.4 -
2948 -
2949 If \a enabled is true, this item will accept hover events; -
2950 otherwise, it will ignore them. By default, items do not accept -
2951 hover events. -
2952 -
2953 Hover events are delivered when there is no current mouse grabber -
2954 item. They are sent when the mouse cursor enters an item, when it -
2955 moves around inside the item, and when the cursor leaves an -
2956 item. Hover events are commonly used to highlight an item when -
2957 it's entered, and for tracking the mouse cursor as it hovers over -
2958 the item (equivalent to QWidget::mouseTracking). -
2959 -
2960 Parent items receive hover enter events before their children, and -
2961 leave events after their children. The parent does not receive a -
2962 hover leave event if the cursor enters a child, though; the parent -
2963 stays "hovered" until the cursor leaves its area, including its -
2964 children's areas. -
2965 -
2966 If a parent item handles child events, it will receive hover move, -
2967 drag move, and drop events as the cursor passes through its -
2968 children, but it does not receive hover enter and hover leave, nor -
2969 drag enter and drag leave events on behalf of its children. -
2970 -
2971 A QGraphicsWidget with window decorations will accept hover events -
2972 regardless of the value of acceptHoverEvents(). -
2973 -
2974 \sa acceptHoverEvents(), hoverEnterEvent(), hoverMoveEvent(), -
2975 hoverLeaveEvent() -
2976*/ -
2977void QGraphicsItem::setAcceptHoverEvents(bool enabled) -
2978{ -
2979 if (d_ptr->acceptsHover == quint32(enabled)) -
2980 return; -
2981 d_ptr->acceptsHover = quint32(enabled); -
2982 if (d_ptr->acceptsHover && d_ptr->scene && d_ptr->scene->d_func()->allItemsIgnoreHoverEvents) { -
2983 d_ptr->scene->d_func()->allItemsIgnoreHoverEvents = false; -
2984 d_ptr->scene->d_func()->enableMouseTrackingOnViews(); -
2985 } -
2986} -
2987 -
2988/*! -
2989 \fn void QGraphicsItem::setAcceptsHoverEvents(bool enabled) -
2990 \obsolete -
2991 -
2992 Use setAcceptHoverEvents(\a enabled) instead. -
2993*/ -
2994 -
2995/*! \since 4.6 -
2996 -
2997 Returns true if an item accepts \l{QTouchEvent}{touch events}; -
2998 otherwise, returns false. By default, items do not accept touch events. -
2999 -
3000 \sa setAcceptTouchEvents() -
3001*/ -
3002bool QGraphicsItem::acceptTouchEvents() const -
3003{ -
3004 return d_ptr->acceptTouchEvents; -
3005} -
3006 -
3007/*! -
3008 \since 4.6 -
3009 -
3010 If \a enabled is true, this item will accept \l{QTouchEvent}{touch events}; -
3011 otherwise, it will ignore them. By default, items do not accept -
3012 touch events. -
3013*/ -
3014void QGraphicsItem::setAcceptTouchEvents(bool enabled) -
3015{ -
3016 if (d_ptr->acceptTouchEvents == quint32(enabled)) -
3017 return; -
3018 d_ptr->acceptTouchEvents = quint32(enabled); -
3019 if (d_ptr->acceptTouchEvents && d_ptr->scene && d_ptr->scene->d_func()->allItemsIgnoreTouchEvents) { -
3020 d_ptr->scene->d_func()->allItemsIgnoreTouchEvents = false; -
3021 d_ptr->scene->d_func()->enableTouchEventsOnViews(); -
3022 } -
3023} -
3024 -
3025/*! -
3026 \since 4.6 -
3027 -
3028 Returns true if this item filters child events (i.e., all events -
3029 intended for any of its children are instead sent to this item); -
3030 otherwise, false is returned. -
3031 -
3032 The default value is false; child events are not filtered. -
3033 -
3034 \sa setFiltersChildEvents() -
3035*/ -
3036bool QGraphicsItem::filtersChildEvents() const -
3037{ -
3038 return d_ptr->filtersDescendantEvents; -
3039} -
3040 -
3041/*! -
3042 \since 4.6 -
3043 -
3044 If \a enabled is true, this item is set to filter all events for -
3045 all its children (i.e., all events intented for any of its -
3046 children are instead sent to this item); otherwise, if \a enabled -
3047 is false, this item will only handle its own events. The default -
3048 value is false. -
3049 -
3050 \sa filtersChildEvents() -
3051*/ -
3052void QGraphicsItem::setFiltersChildEvents(bool enabled) -
3053{ -
3054 if (d_ptr->filtersDescendantEvents == enabled) -
3055 return; -
3056 -
3057 d_ptr->filtersDescendantEvents = enabled; -
3058 d_ptr->updateAncestorFlag(QGraphicsItem::GraphicsItemFlag(-2)); -
3059} -
3060 -
3061/*! -
3062 \obsolete -
3063 -
3064 Returns true if this item handles child events (i.e., all events -
3065 intended for any of its children are instead sent to this item); -
3066 otherwise, false is returned. -
3067 -
3068 This property is useful for item groups; it allows one item to -
3069 handle events on behalf of its children, as opposed to its -
3070 children handling their events individually. -
3071 -
3072 The default is to return false; children handle their own events. -
3073 The exception for this is if the item is a QGraphicsItemGroup, then -
3074 it defaults to return true. -
3075 -
3076 \sa setHandlesChildEvents() -
3077*/ -
3078bool QGraphicsItem::handlesChildEvents() const -
3079{ -
3080 return d_ptr->handlesChildEvents; -
3081} -
3082 -
3083/*! -
3084 \obsolete -
3085 -
3086 If \a enabled is true, this item is set to handle all events for -
3087 all its children (i.e., all events intented for any of its -
3088 children are instead sent to this item); otherwise, if \a enabled -
3089 is false, this item will only handle its own events. The default -
3090 value is false. -
3091 -
3092 This property is useful for item groups; it allows one item to -
3093 handle events on behalf of its children, as opposed to its -
3094 children handling their events individually. -
3095 -
3096 If a child item accepts hover events, its parent will receive -
3097 hover move events as the cursor passes through the child, but it -
3098 does not receive hover enter and hover leave events on behalf of -
3099 its child. -
3100 -
3101 \sa handlesChildEvents() -
3102*/ -
3103void QGraphicsItem::setHandlesChildEvents(bool enabled) -
3104{ -
3105 if (d_ptr->handlesChildEvents == enabled) -
3106 return; -
3107 -
3108 d_ptr->handlesChildEvents = enabled; -
3109 d_ptr->updateAncestorFlag(QGraphicsItem::GraphicsItemFlag(-1)); -
3110} -
3111/*! -
3112 \since 4.6 -
3113 Returns true if this item is active; otherwise returns false. -
3114 -
3115 An item can only be active if the scene is active. An item is active -
3116 if it is, or is a descendent of, an active panel. Items in non-active -
3117 panels are not active. -
3118 -
3119 Items that are not part of a panel follow scene activation when the -
3120 scene has no active panel. -
3121 -
3122 Only active items can gain input focus. -
3123 -
3124 \sa QGraphicsScene::isActive(), QGraphicsScene::activePanel(), panel(), isPanel() -
3125*/ -
3126bool QGraphicsItem::isActive() const -
3127{ -
3128 if (!d_ptr->scene || !d_ptr->scene->isActive()) -
3129 return false; -
3130 return panel() == d_ptr->scene->activePanel(); -
3131} -
3132 -
3133/*! -
3134 \since 4.6 -
3135 -
3136 If \a active is true, and the scene is active, this item's panel will be -
3137 activated. Otherwise, the panel is deactivated. -
3138 -
3139 If the item is not part of an active scene, \a active will decide what -
3140 happens to the panel when the scene becomes active or the item is added to -
3141 the scene. If true, the item's panel will be activated when the item is -
3142 either added to the scene or the scene is activated. Otherwise, the item -
3143 will stay inactive independent of the scene's activated state. -
3144 -
3145 \sa isPanel(), QGraphicsScene::setActivePanel(), QGraphicsScene::isActive() -
3146*/ -
3147void QGraphicsItem::setActive(bool active) -
3148{ -
3149 d_ptr->explicitActivate = 1; -
3150 d_ptr->wantsActive = active; -
3151 if (d_ptr->scene) { -
3152 if (active) { -
3153 // Activate this item. -
3154 d_ptr->scene->setActivePanel(this); -
3155 } else { -
3156 // Deactivate this item, and reactivate the parent panel, -
3157 // or the last active panel (if any). -
3158 QGraphicsItem *nextToActivate = 0; -
3159 if (d_ptr->parent) -
3160 nextToActivate = d_ptr->parent->panel(); -
3161 if (!nextToActivate) -
3162 nextToActivate = d_ptr->scene->d_func()->lastActivePanel; -
3163 if (nextToActivate == this || isAncestorOf(nextToActivate)) -
3164 nextToActivate = 0; -
3165 d_ptr->scene->setActivePanel(nextToActivate); -
3166 } -
3167 } -
3168} -
3169 -
3170/*! -
3171 Returns true if this item is active, and it or its \l{focusProxy()}{focus -
3172 proxy} has keyboard input focus; otherwise, returns false. -
3173 -
3174 \sa focusItem(), setFocus(), QGraphicsScene::setFocusItem(), isActive() -
3175*/ -
3176bool QGraphicsItem::hasFocus() const -
3177{ -
3178 if (!d_ptr->scene || !d_ptr->scene->isActive()) -
3179 return false; -
3180 -
3181 if (d_ptr->focusProxy) -
3182 return d_ptr->focusProxy->hasFocus(); -
3183 -
3184 if (d_ptr->scene->d_func()->focusItem != this) -
3185 return false; -
3186 -
3187 return panel() == d_ptr->scene->d_func()->activePanel; -
3188} -
3189 -
3190/*! -
3191 Gives keyboard input focus to this item. The \a focusReason argument will -
3192 be passed into any \l{QFocusEvent}{focus event} generated by this function; -
3193 it is used to give an explanation of what caused the item to get focus. -
3194 -
3195 Only enabled items that set the ItemIsFocusable flag can accept keyboard -
3196 focus. -
3197 -
3198 If this item is not visible, not active, or not associated with a scene, -
3199 it will not gain immediate input focus. However, it will be registered as -
3200 the preferred focus item for its subtree of items, should it later become -
3201 visible. -
3202 -
3203 As a result of calling this function, this item will receive a -
3204 \l{focusInEvent()}{focus in event} with \a focusReason. If another item -
3205 already has focus, that item will first receive a \l{focusOutEvent()} -
3206 {focus out event} indicating that it has lost input focus. -
3207 -
3208 \sa clearFocus(), hasFocus(), focusItem(), focusProxy() -
3209*/ -
3210void QGraphicsItem::setFocus(Qt::FocusReason focusReason) -
3211{ -
3212 d_ptr->setFocusHelper(focusReason, /* climb = */ true, /* focusFromHide = */ false); -
3213} -
3214 -
3215/*! -
3216 \internal -
3217*/ -
3218void QGraphicsItemPrivate::setFocusHelper(Qt::FocusReason focusReason, bool climb, bool focusFromHide) -
3219{ -
3220 // Disabled / unfocusable items cannot accept focus. -
3221 if (!q_ptr->isEnabled() || !(flags & QGraphicsItem::ItemIsFocusable))
partially evaluated: !q_ptr->isEnabled()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:15
evaluated: !(flags & QGraphicsItem::ItemIsFocusable)
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:14
0-15
3222 return;
executed: return;
Execution Count:1
1
3223 -
3224 // Find focus proxy. -
3225 QGraphicsItem *f = q_ptr;
executed (the execution status of this line is deduced): QGraphicsItem *f = q_ptr;
-
3226 while (f->d_ptr->focusProxy)
partially evaluated: f->d_ptr->focusProxy
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:14
0-14
3227 f = f->d_ptr->focusProxy;
never executed: f = f->d_ptr->focusProxy;
0
3228 -
3229 // Return if it already has focus. -
3230 if (scene && scene->focusItem() == f)
partially evaluated: scene
TRUEFALSE
yes
Evaluation Count:14
no
Evaluation Count:0
partially evaluated: scene->focusItem() == f
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:14
0-14
3231 return;
never executed: return;
0
3232 -
3233 // Update focus scope item ptr. -
3234 QGraphicsItem *p = parent;
executed (the execution status of this line is deduced): QGraphicsItem *p = parent;
-
3235 while (p) {
partially evaluated: p
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:14
0-14
3236 if (p->flags() & QGraphicsItem::ItemIsFocusScope) {
never evaluated: p->flags() & QGraphicsItem::ItemIsFocusScope
0
3237 QGraphicsItem *oldFocusScopeItem = p->d_ptr->focusScopeItem;
never executed (the execution status of this line is deduced): QGraphicsItem *oldFocusScopeItem = p->d_ptr->focusScopeItem;
-
3238 p->d_ptr->focusScopeItem = q_ptr;
never executed (the execution status of this line is deduced): p->d_ptr->focusScopeItem = q_ptr;
-
3239 if (!p->focusItem() && !focusFromHide) {if (oldFocusScopeItem)
never evaluated: oldFocusScopeItem
0
3240 oldFocusScopeItem->d_ptr->focusScopeItemChange(false);
never executed: oldFocusScopeItem->d_ptr->focusScopeItemChange(false);
0
3241 focusScopeItemChange(true);
never executed (the execution status of this line is deduced): focusScopeItemChange(true);
-
3242 / If you call setFocus on a child of a focus scope that0
// doesn't currently have a focus item, then stop.
if (!p->focusItem() && !focusFromHide) {
never evaluated: !p->focusItem()
never evaluated: !focusFromHide
3243 // Calling setFocus() on a child of a focus scope that does -
3244 // not have focus changes only the focus scope pointer, -
3245 // so that focus is restored the next time the scope gains -
3246 // focus. -
3247 return;
never executed: return;
0
3248 } -
3249 break;
never executed: break;
0
3250 } -
3251 p = p->d_ptr->parent;
never executed (the execution status of this line is deduced): p = p->d_ptr->parent;
-
3252 }
never executed: }
0
3253 -
3254 if (climb) {
partially evaluated: climb
TRUEFALSE
yes
Evaluation Count:14
no
Evaluation Count:0
0-14
3255 while (f->d_ptr->focusScopeItem && f->d_ptr->focusScopeItem->isVisible())
partially evaluated: f->d_ptr->focusScopeItem
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:14
never evaluated: f->d_ptr->focusScopeItem->isVisible()
0-14
3256 f = f->d_ptr->focusScopeItem;
never executed: f = f->d_ptr->focusScopeItem;
0
3257 }
executed: }
Execution Count:14
14
3258 -
3259 // Update the child focus chain. -
3260 QGraphicsItem *commonAncestor = 0;
executed (the execution status of this line is deduced): QGraphicsItem *commonAncestor = 0;
-
3261 if (scene && scene->focusItem() && scene->focusItem()->panel() == q_ptr->panel()) {
partially evaluated: scene
TRUEFALSE
yes
Evaluation Count:14
no
Evaluation Count:0
evaluated: scene->focusItem()
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:8
partially evaluated: scene->focusItem()->panel() == q_ptr->panel()
TRUEFALSE
yes
Evaluation Count:6
no
Evaluation Count:0
0-14
3262 commonAncestor = scene->focusItem()->commonAncestorItem(f);
executed (the execution status of this line is deduced): commonAncestor = scene->focusItem()->commonAncestorItem(f);
-
3263 scene->focusItem()->d_ptr->clearSubFocus(scene->focusItem(), commonAncestor);
executed (the execution status of this line is deduced): scene->focusItem()->d_ptr->clearSubFocus(scene->focusItem(), commonAncestor);
-
3264 }
executed: }
Execution Count:6
6
3265 -
3266 f->d_ptr->setSubFocus(f, commonAncestor);
executed (the execution status of this line is deduced): f->d_ptr->setSubFocus(f, commonAncestor);
-
3267 -
3268 // Update the scene's focus item. -
3269 if (scene) {
partially evaluated: scene
TRUEFALSE
yes
Evaluation Count:14
no
Evaluation Count:0
0-14
3270 QGraphicsItem *p = q_ptr->panel();
executed (the execution status of this line is deduced): QGraphicsItem *p = q_ptr->panel();
-
3271 if ((!p && scene->isActive()) || (p && p->isActive())) {
partially evaluated: !p
TRUEFALSE
yes
Evaluation Count:14
no
Evaluation Count:0
evaluated: scene->isActive()
TRUEFALSE
yes
Evaluation Count:13
yes
Evaluation Count:1
partially evaluated: p
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
never evaluated: p->isActive()
0-14
3272 // Visible items immediately gain focus from scene. -
3273 scene->d_func()->setFocusItemHelper(f, focusReason);
executed (the execution status of this line is deduced): scene->d_func()->setFocusItemHelper(f, focusReason);
-
3274 }
executed: }
Execution Count:13
13
3275 }
executed: }
Execution Count:14
14
3276}
executed: }
Execution Count:14
14
3277 -
3278/*! -
3279 Takes keyboard input focus from the item. -
3280 -
3281 If it has focus, a \l{focusOutEvent()}{focus out event} is sent to this -
3282 item to tell it that it is about to lose the focus. -
3283 -
3284 Only items that set the ItemIsFocusable flag, or widgets that set an -
3285 appropriate focus policy, can accept keyboard focus. -
3286 -
3287 \sa setFocus(), hasFocus(), QGraphicsWidget::focusPolicy -
3288*/ -
3289void QGraphicsItem::clearFocus() -
3290{ -
3291 d_ptr->clearFocusHelper(/* giveFocusToParent = */ true, -
3292 /* hiddenByParentPanel = */ false); -
3293} -
3294 -
3295/*! -
3296 \internal -
3297*/ -
3298void QGraphicsItemPrivate::clearFocusHelper(bool giveFocusToParent, bool hiddenByParentPanel) -
3299{ -
3300 QGraphicsItem *subFocusItem = q_ptr; -
3301 if (flags & QGraphicsItem::ItemIsFocusScope) { -
3302 while (subFocusItem->d_ptr->focusScopeItem) -
3303 subFocusItem = subFocusItem->d_ptr->focusScopeItem; -
3304 } -
3305 -
3306 if (giveFocusToParent) { -
3307 // Pass focus to the closest parent focus scope -
3308 if (!inDestructor) { -
3309 QGraphicsItem *p = parent; -
3310 while (p) { -
3311 if (p->flags() & QGraphicsItem::ItemIsFocusScope) { -
3312 if (p->d_ptr->focusScopeItem == q_ptr) { -
3313 p->d_ptr->focusScopeItem = 0; -
3314 if (!subFocusItem->hasFocus()) //if it has focus, focusScopeItemChange is called elsewhere -
3315 focusScopeItemChange(false); -
3316 } -
3317 if (subFocusItem->hasFocus()) -
3318 p->d_ptr->setFocusHelper(Qt::OtherFocusReason, /* climb = */ false, -
3319 /* focusFromHide = */ false); -
3320 return; -
3321 } -
3322 p = p->d_ptr->parent; -
3323 } -
3324 } -
3325 } -
3326 -
3327 if (subFocusItem->hasFocus()) { -
3328 // Invisible items with focus must explicitly clear subfocus. -
3329 if (!hiddenByParentPanel) -
3330 clearSubFocus(q_ptr); -
3331 -
3332 // If this item has the scene's input focus, clear it. -
3333 scene->setFocusItem(0); -
3334 } -
3335} -
3336 -
3337/*! -
3338 \since 4.6 -
3339 -
3340 Returns this item's focus proxy, or 0 if this item has no -
3341 focus proxy. -
3342 -
3343 \sa setFocusProxy(), setFocus(), hasFocus() -
3344*/ -
3345QGraphicsItem *QGraphicsItem::focusProxy() const -
3346{ -
3347 return d_ptr->focusProxy; -
3348} -
3349 -
3350/*! -
3351 \since 4.6 -
3352 -
3353 Sets the item's focus proxy to \a item. -
3354 -
3355 If an item has a focus proxy, the focus proxy will receive -
3356 input focus when the item gains input focus. The item itself -
3357 will still have focus (i.e., hasFocus() will return true), -
3358 but only the focus proxy will receive the keyboard input. -
3359 -
3360 A focus proxy can itself have a focus proxy, and so on. In -
3361 such case, keyboard input will be handled by the outermost -
3362 focus proxy. -
3363 -
3364 The focus proxy \a item must belong to the same scene as -
3365 this item. -
3366 -
3367 \sa focusProxy(), setFocus(), hasFocus() -
3368*/ -
3369void QGraphicsItem::setFocusProxy(QGraphicsItem *item) -
3370{ -
3371 if (item == d_ptr->focusProxy) -
3372 return; -
3373 if (item == this) { -
3374 qWarning("QGraphicsItem::setFocusProxy: cannot assign self as focus proxy"); -
3375 return; -
3376 } -
3377 if (item) { -
3378 if (item->d_ptr->scene != d_ptr->scene) { -
3379 qWarning("QGraphicsItem::setFocusProxy: focus proxy must be in same scene"); -
3380 return; -
3381 } -
3382 for (QGraphicsItem *f = item->focusProxy(); f != 0; f = f->focusProxy()) { -
3383 if (f == this) { -
3384 qWarning("QGraphicsItem::setFocusProxy: %p is already in the focus proxy chain", item); -
3385 return; -
3386 } -
3387 } -
3388 } -
3389 -
3390 QGraphicsItem *lastFocusProxy = d_ptr->focusProxy; -
3391 if (lastFocusProxy) -
3392 lastFocusProxy->d_ptr->focusProxyRefs.removeOne(&d_ptr->focusProxy); -
3393 d_ptr->focusProxy = item; -
3394 if (item) -
3395 item->d_ptr->focusProxyRefs << &d_ptr->focusProxy; -
3396} -
3397 -
3398/*! -
3399 \since 4.6 -
3400 -
3401 If this item, a child or descendant of this item currently has input -
3402 focus, this function will return a pointer to that item. If -
3403 no descendant has input focus, 0 is returned. -
3404 -
3405 \sa hasFocus(), setFocus(), QWidget::focusWidget() -
3406*/ -
3407QGraphicsItem *QGraphicsItem::focusItem() const -
3408{ -
3409 return d_ptr->subFocusItem; -
3410} -
3411 -
3412/*! -
3413 \internal -
3414 -
3415 Returns this item's focus scope item. -
3416*/ -
3417QGraphicsItem *QGraphicsItem::focusScopeItem() const -
3418{ -
3419 return d_ptr->focusScopeItem; -
3420} -
3421 -
3422/*! -
3423 \since 4.4 -
3424 Grabs the mouse input. -
3425 -
3426 This item will receive all mouse events for the scene until any of the -
3427 following events occurs: -
3428 -
3429 \list -
3430 \li The item becomes invisible -
3431 \li The item is removed from the scene -
3432 \li The item is deleted -
3433 \li The item call ungrabMouse() -
3434 \li Another item calls grabMouse(); the item will regain the mouse grab -
3435 when the other item calls ungrabMouse(). -
3436 \endlist -
3437 -
3438 When an item gains the mouse grab, it receives a QEvent::GrabMouse -
3439 event. When it loses the mouse grab, it receives a QEvent::UngrabMouse -
3440 event. These events can be used to detect when your item gains or loses -
3441 the mouse grab through other means than receiving mouse button events. -
3442 -
3443 It is almost never necessary to explicitly grab the mouse in Qt, as Qt -
3444 grabs and releases it sensibly. In particular, Qt grabs the mouse when you -
3445 press a mouse button, and keeps the mouse grabbed until you release the -
3446 last mouse button. Also, Qt::Popup widgets implicitly call grabMouse() -
3447 when shown, and ungrabMouse() when hidden. -
3448 -
3449 Note that only visible items can grab mouse input. Calling grabMouse() on -
3450 an invisible item has no effect. -
3451 -
3452 Keyboard events are not affected. -
3453 -
3454 \sa QGraphicsScene::mouseGrabberItem(), ungrabMouse(), grabKeyboard() -
3455*/ -
3456void QGraphicsItem::grabMouse() -
3457{ -
3458 if (!d_ptr->scene) { -
3459 qWarning("QGraphicsItem::grabMouse: cannot grab mouse without scene"); -
3460 return; -
3461 } -
3462 if (!d_ptr->visible) { -
3463 qWarning("QGraphicsItem::grabMouse: cannot grab mouse while invisible"); -
3464 return; -
3465 } -
3466 d_ptr->scene->d_func()->grabMouse(this); -
3467} -
3468 -
3469/*! -
3470 \since 4.4 -
3471 Releases the mouse grab. -
3472 -
3473 \sa grabMouse(), ungrabKeyboard() -
3474*/ -
3475void QGraphicsItem::ungrabMouse() -
3476{ -
3477 if (!d_ptr->scene) { -
3478 qWarning("QGraphicsItem::ungrabMouse: cannot ungrab mouse without scene"); -
3479 return; -
3480 } -
3481 d_ptr->scene->d_func()->ungrabMouse(this); -
3482} -
3483 -
3484/*! -
3485 \since 4.4 -
3486 Grabs the keyboard input. -
3487 -
3488 The item will receive all keyboard input to the scene until one of the -
3489 following events occur: -
3490 -
3491 \list -
3492 \li The item becomes invisible -
3493 \li The item is removed from the scene -
3494 \li The item is deleted -
3495 \li The item calls ungrabKeyboard() -
3496 \li Another item calls grabKeyboard(); the item will regain the keyboard grab -
3497 when the other item calls ungrabKeyboard(). -
3498 \endlist -
3499 -
3500 When an item gains the keyboard grab, it receives a QEvent::GrabKeyboard -
3501 event. When it loses the keyboard grab, it receives a -
3502 QEvent::UngrabKeyboard event. These events can be used to detect when your -
3503 item gains or loses the keyboard grab through other means than gaining -
3504 input focus. -
3505 -
3506 It is almost never necessary to explicitly grab the keyboard in Qt, as Qt -
3507 grabs and releases it sensibly. In particular, Qt grabs the keyboard when -
3508 your item gains input focus, and releases it when your item loses input -
3509 focus, or when the item is hidden. -
3510 -
3511 Note that only visible items can grab keyboard input. Calling -
3512 grabKeyboard() on an invisible item has no effect. -
3513 -
3514 Keyboard events are not affected. -
3515 -
3516 \sa ungrabKeyboard(), grabMouse(), setFocus() -
3517*/ -
3518void QGraphicsItem::grabKeyboard() -
3519{ -
3520 if (!d_ptr->scene) { -
3521 qWarning("QGraphicsItem::grabKeyboard: cannot grab keyboard without scene"); -
3522 return; -
3523 } -
3524 if (!d_ptr->visible) { -
3525 qWarning("QGraphicsItem::grabKeyboard: cannot grab keyboard while invisible"); -
3526 return; -
3527 } -
3528 d_ptr->scene->d_func()->grabKeyboard(this); -
3529} -
3530 -
3531/*! -
3532 \since 4.4 -
3533 Releases the keyboard grab. -
3534 -
3535 \sa grabKeyboard(), ungrabMouse() -
3536*/ -
3537void QGraphicsItem::ungrabKeyboard() -
3538{ -
3539 if (!d_ptr->scene) { -
3540 qWarning("QGraphicsItem::ungrabKeyboard: cannot ungrab keyboard without scene"); -
3541 return; -
3542 } -
3543 d_ptr->scene->d_func()->ungrabKeyboard(this); -
3544} -
3545 -
3546/*! -
3547 Returns the position of the item in parent coordinates. If the item has no -
3548 parent, its position is given in scene coordinates. -
3549 -
3550 The position of the item describes its origin (local coordinate -
3551 (0, 0)) in parent coordinates; this function returns the same as -
3552 mapToParent(0, 0). -
3553 -
3554 For convenience, you can also call scenePos() to determine the -
3555 item's position in scene coordinates, regardless of its parent. -
3556 -
3557 \sa x(), y(), setPos(), transform(), {The Graphics View Coordinate System} -
3558*/ -
3559QPointF QGraphicsItem::pos() const -
3560{ -
3561 return d_ptr->pos; -
3562} -
3563 -
3564/*! -
3565 \fn QGraphicsItem::x() const -
3566 -
3567 This convenience function is equivalent to calling pos().x(). -
3568 -
3569 \sa y() -
3570*/ -
3571 -
3572/*! -
3573 \since 4.6 -
3574 -
3575 Set's the \a x coordinate of the item's position. Equivalent to -
3576 calling setPos(x, y()). -
3577 -
3578 \sa x(), setPos() -
3579*/ -
3580void QGraphicsItem::setX(qreal x) -
3581{ -
3582 if (d_ptr->inDestructor) -
3583 return; -
3584 -
3585 if (qIsNaN(x)) -
3586 return; -
3587 -
3588 setPos(QPointF(x, d_ptr->pos.y())); -
3589} -
3590 -
3591/*! -
3592 \fn QGraphicsItem::y() const -
3593 -
3594 This convenience function is equivalent to calling pos().y(). -
3595 -
3596 \sa x() -
3597*/ -
3598 -
3599/*! -
3600 \since 4.6 -
3601 -
3602 Set's the \a y coordinate of the item's position. Equivalent to -
3603 calling setPos(x(), y). -
3604 -
3605 \sa x(), setPos() -
3606*/ -
3607void QGraphicsItem::setY(qreal y) -
3608{ -
3609 if (d_ptr->inDestructor) -
3610 return; -
3611 -
3612 if (qIsNaN(y)) -
3613 return; -
3614 -
3615 setPos(QPointF(d_ptr->pos.x(), y)); -
3616} -
3617 -
3618/*! -
3619 Returns the item's position in scene coordinates. This is -
3620 equivalent to calling \c mapToScene(0, 0). -
3621 -
3622 \sa pos(), sceneTransform(), {The Graphics View Coordinate System} -
3623*/ -
3624QPointF QGraphicsItem::scenePos() const -
3625{ -
3626 return mapToScene(0, 0); -
3627} -
3628 -
3629/*! -
3630 \internal -
3631 -
3632 Sets the position \a pos. -
3633*/ -
3634void QGraphicsItemPrivate::setPosHelper(const QPointF &pos) -
3635{ -
3636 Q_Q(QGraphicsItem); -
3637 inSetPosHelper = 1; -
3638 if (scene) -
3639 q->prepareGeometryChange(); -
3640 QPointF oldPos = this->pos; -
3641 this->pos = pos; -
3642 dirtySceneTransform = 1; -
3643 inSetPosHelper = 0; -
3644 if (isObject) { -
3645 if (pos.x() != oldPos.x()) -
3646 emit static_cast<QGraphicsObject *>(q_ptr)->xChanged(); -
3647 if (pos.y() != oldPos.y()) -
3648 emit static_cast<QGraphicsObject *>(q_ptr)->yChanged(); -
3649 } -
3650} -
3651 -
3652/*! -
3653 \internal -
3654 -
3655 Sets the transform \a transform. -
3656*/ -
3657void QGraphicsItemPrivate::setTransformHelper(const QTransform &transform) -
3658{ -
3659 q_ptr->prepareGeometryChange(); -
3660 transformData->transform = transform; -
3661 dirtySceneTransform = 1; -
3662 transformChanged(); -
3663} -
3664 -
3665/*! -
3666 Sets the position of the item to \a pos, which is in parent -
3667 coordinates. For items with no parent, \a pos is in scene -
3668 coordinates. -
3669 -
3670 The position of the item describes its origin (local coordinate -
3671 (0, 0)) in parent coordinates. -
3672 -
3673 \sa pos(), scenePos(), {The Graphics View Coordinate System} -
3674*/ -
3675void QGraphicsItem::setPos(const QPointF &pos) -
3676{ -
3677 if (d_ptr->pos == pos) -
3678 return; -
3679 -
3680 if (d_ptr->inDestructor) -
3681 return; -
3682 -
3683 // Update and repositition. -
3684 if (!(d_ptr->flags & (ItemSendsGeometryChanges | ItemSendsScenePositionChanges))) { -
3685 d_ptr->setPosHelper(pos); -
3686 if (d_ptr->isWidget) -
3687 static_cast<QGraphicsWidget *>(this)->d_func()->setGeometryFromSetPos(); -
3688 if (d_ptr->scenePosDescendants) -
3689 d_ptr->sendScenePosChange(); -
3690 return; -
3691 } -
3692 -
3693 // Notify the item that the position is changing. -
3694 const QVariant newPosVariant(itemChange(ItemPositionChange, QVariant::fromValue<QPointF>(pos))); -
3695 QPointF newPos = newPosVariant.toPointF(); -
3696 if (newPos == d_ptr->pos) -
3697 return; -
3698 -
3699 // Update and repositition. -
3700 d_ptr->setPosHelper(newPos); -
3701 -
3702 // Send post-notification. -
3703 itemChange(QGraphicsItem::ItemPositionHasChanged, newPosVariant); -
3704 d_ptr->sendScenePosChange(); -
3705} -
3706 -
3707/*! -
3708 \fn void QGraphicsItem::setPos(qreal x, qreal y) -
3709 \overload -
3710 -
3711 This convenience function is equivalent to calling setPos(QPointF(\a x, \a -
3712 y)). -
3713*/ -
3714 -
3715/*! -
3716 \fn void QGraphicsItem::moveBy(qreal dx, qreal dy) -
3717 -
3718 Moves the item by \a dx points horizontally, and \a dy point -
3719 vertically. This function is equivalent to calling setPos(pos() + -
3720 QPointF(\a dx, \a dy)). -
3721*/ -
3722 -
3723/*! -
3724 If this item is part of a scene that is viewed by a QGraphicsView, this -
3725 convenience function will attempt to scroll the view to ensure that \a -
3726 rect is visible inside the view's viewport. If \a rect is a null rect (the -
3727 default), QGraphicsItem will default to the item's bounding rect. \a xmargin -
3728 and \a ymargin are the number of pixels the view should use for margins. -
3729 -
3730 If the specified rect cannot be reached, the contents are scrolled to the -
3731 nearest valid position. -
3732 -
3733 If this item is not viewed by a QGraphicsView, this function does nothing. -
3734 -
3735 \sa QGraphicsView::ensureVisible() -
3736*/ -
3737void QGraphicsItem::ensureVisible(const QRectF &rect, int xmargin, int ymargin) -
3738{ -
3739 if (d_ptr->scene) { -
3740 QRectF sceneRect; -
3741 if (!rect.isNull()) -
3742 sceneRect = sceneTransform().mapRect(rect); -
3743 else -
3744 sceneRect = sceneBoundingRect(); -
3745 foreach (QGraphicsView *view, d_ptr->scene->d_func()->views) -
3746 view->ensureVisible(sceneRect, xmargin, ymargin); -
3747 } -
3748} -
3749 -
3750/*! -
3751 \fn void QGraphicsItem::ensureVisible(qreal x, qreal y, qreal w, qreal h, -
3752 int xmargin = 50, int ymargin = 50) -
3753 -
3754 This convenience function is equivalent to calling -
3755 ensureVisible(QRectF(\a x, \a y, \a w, \a h), \a xmargin, \a ymargin). -
3756*/ -
3757 -
3758/*! -
3759 \obsolete -
3760 -
3761 Returns the item's affine transformation matrix. This is a subset or the -
3762 item's full transformation matrix, and might not represent the item's full -
3763 transformation. -
3764 -
3765 Use transform() instead. -
3766 -
3767 \sa setTransform(), sceneTransform() -
3768*/ -
3769QMatrix QGraphicsItem::matrix() const -
3770{ -
3771 return transform().toAffine(); -
3772} -
3773 -
3774/*! -
3775 \since 4.3 -
3776 -
3777 Returns this item's transformation matrix. -
3778 -
3779 The transformation matrix is combined with the item's rotation(), scale() -
3780 and transformations() into a combined transformations for the item. -
3781 -
3782 The default transformation matrix is an identity matrix. -
3783 -
3784 \sa setTransform(), sceneTransform() -
3785*/ -
3786QTransform QGraphicsItem::transform() const -
3787{ -
3788 if (!d_ptr->transformData) -
3789 return QTransform(); -
3790 return d_ptr->transformData->transform; -
3791} -
3792 -
3793/*! -
3794 \since 4.6 -
3795 -
3796 Returns the clockwise rotation, in degrees, around the Z axis. The default -
3797 value is 0 (i.e., the item is not rotated). -
3798 -
3799 The rotation is combined with the item's scale(), transform() and -
3800 transformations() to map the item's coordinate system to the parent item. -
3801 -
3802 \sa setRotation(), transformOriginPoint(), {Transformations} -
3803*/ -
3804qreal QGraphicsItem::rotation() const -
3805{ -
3806 if (!d_ptr->transformData) -
3807 return 0; -
3808 return d_ptr->transformData->rotation; -
3809} -
3810 -
3811/*! -
3812 \since 4.6 -
3813 -
3814 Sets the clockwise rotation \a angle, in degrees, around the Z axis. The -
3815 default value is 0 (i.e., the item is not rotated). Assigning a negative -
3816 value will rotate the item counter-clockwise. Normally the rotation angle -
3817 is in the range (-360, 360), but it's also possible to assign values -
3818 outside of this range (e.g., a rotation of 370 degrees is the same as a -
3819 rotation of 10 degrees). -
3820 -
3821 The item is rotated around its transform origin point, which by default -
3822 is (0, 0). You can select a different transformation origin by calling -
3823 setTransformOriginPoint(). -
3824 -
3825 The rotation is combined with the item's scale(), transform() and -
3826 transformations() to map the item's coordinate system to the parent item. -
3827 -
3828 \sa rotation(), setTransformOriginPoint(), {Transformations} -
3829*/ -
3830void QGraphicsItem::setRotation(qreal angle) -
3831{ -
3832 prepareGeometryChange(); -
3833 qreal newRotation = angle; -
3834 -
3835 if (d_ptr->flags & ItemSendsGeometryChanges) { -
3836 // Notify the item that the rotation is changing. -
3837 const QVariant newRotationVariant(itemChange(ItemRotationChange, angle)); -
3838 newRotation = newRotationVariant.toReal(); -
3839 } -
3840 -
3841 if (!d_ptr->transformData) -
3842 d_ptr->transformData = new QGraphicsItemPrivate::TransformData; -
3843 -
3844 if (d_ptr->transformData->rotation == newRotation) -
3845 return; -
3846 -
3847 d_ptr->transformData->rotation = newRotation; -
3848 d_ptr->transformData->onlyTransform = false; -
3849 d_ptr->dirtySceneTransform = 1; -
3850 -
3851 // Send post-notification. -
3852 if (d_ptr->flags & ItemSendsGeometryChanges) -
3853 itemChange(ItemRotationHasChanged, newRotation); -
3854 -
3855 if (d_ptr->isObject) -
3856 emit static_cast<QGraphicsObject *>(this)->rotationChanged(); -
3857 -
3858 d_ptr->transformChanged(); -
3859} -
3860 -
3861/*! -
3862 \since 4.6 -
3863 -
3864 Returns the scale factor of the item. The default scale factor is 1.0 -
3865 (i.e., the item is not scaled). -
3866 -
3867 The scale is combined with the item's rotation(), transform() and -
3868 transformations() to map the item's coordinate system to the parent item. -
3869 -
3870 \sa setScale(), rotation(), {Transformations} -
3871*/ -
3872qreal QGraphicsItem::scale() const -
3873{ -
3874 if (!d_ptr->transformData) -
3875 return 1.; -
3876 return d_ptr->transformData->scale; -
3877} -
3878 -
3879/*! -
3880 \since 4.6 -
3881 -
3882 Sets the scale \a factor of the item. The default scale factor is 1.0 -
3883 (i.e., the item is not scaled). A scale factor of 0.0 will collapse the -
3884 item to a single point. If you provide a negative scale factor, the -
3885 item will be flipped and mirrored (i.e., rotated 180 degrees). -
3886 -
3887 The item is scaled around its transform origin point, which by default -
3888 is (0, 0). You can select a different transformation origin by calling -
3889 setTransformOriginPoint(). -
3890 -
3891 The scale is combined with the item's rotation(), transform() and -
3892 transformations() to map the item's coordinate system to the parent item. -
3893 -
3894 \sa scale(), setTransformOriginPoint(), {Transformations Example} -
3895*/ -
3896void QGraphicsItem::setScale(qreal factor) -
3897{ -
3898 prepareGeometryChange(); -
3899 qreal newScale = factor; -
3900 -
3901 if (d_ptr->flags & ItemSendsGeometryChanges) { -
3902 // Notify the item that the scale is changing. -
3903 const QVariant newScaleVariant(itemChange(ItemScaleChange, factor)); -
3904 newScale = newScaleVariant.toReal(); -
3905 } -
3906 -
3907 if (!d_ptr->transformData) -
3908 d_ptr->transformData = new QGraphicsItemPrivate::TransformData; -
3909 -
3910 if (d_ptr->transformData->scale == newScale) -
3911 return; -
3912 -
3913 d_ptr->transformData->scale = newScale; -
3914 d_ptr->transformData->onlyTransform = false; -
3915 d_ptr->dirtySceneTransform = 1; -
3916 -
3917 // Send post-notification. -
3918 if (d_ptr->flags & ItemSendsGeometryChanges) -
3919 itemChange(ItemScaleHasChanged, newScale); -
3920 -
3921 if (d_ptr->isObject) -
3922 emit static_cast<QGraphicsObject *>(this)->scaleChanged(); -
3923 -
3924 d_ptr->transformChanged(); -
3925} -
3926 -
3927 -
3928/*! -
3929 \since 4.6 -
3930 -
3931 Returns a list of graphics transforms that currently apply to this item. -
3932 -
3933 QGraphicsTransform is for applying and controlling a chain of individual -
3934 transformation operations on an item. It's particularly useful in -
3935 animations, where each transform operation needs to be interpolated -
3936 independently, or differently. -
3937 -
3938 The transformations are combined with the item's rotation(), scale() and -
3939 transform() to map the item's coordinate system to the parent item. -
3940 -
3941 \sa scale(), rotation(), transformOriginPoint(), {Transformations} -
3942*/ -
3943QList<QGraphicsTransform *> QGraphicsItem::transformations() const -
3944{ -
3945 if (!d_ptr->transformData) -
3946 return QList<QGraphicsTransform *>(); -
3947 return d_ptr->transformData->graphicsTransforms; -
3948} -
3949 -
3950/*! -
3951 \since 4.6 -
3952 -
3953 Sets a list of graphics \a transformations (QGraphicsTransform) that -
3954 currently apply to this item. -
3955 -
3956 If all you want is to rotate or scale an item, you should call setRotation() -
3957 or setScale() instead. If you want to set an arbitrary transformation on -
3958 an item, you can call setTransform(). -
3959 -
3960 QGraphicsTransform is for applying and controlling a chain of individual -
3961 transformation operations on an item. It's particularly useful in -
3962 animations, where each transform operation needs to be interpolated -
3963 independently, or differently. -
3964 -
3965 The transformations are combined with the item's rotation(), scale() and -
3966 transform() to map the item's coordinate system to the parent item. -
3967 -
3968 \sa scale(), setTransformOriginPoint(), {Transformations} -
3969*/ -
3970void QGraphicsItem::setTransformations(const QList<QGraphicsTransform *> &transformations) -
3971{ -
3972 prepareGeometryChange(); -
3973 if (!d_ptr->transformData) -
3974 d_ptr->transformData = new QGraphicsItemPrivate::TransformData; -
3975 d_ptr->transformData->graphicsTransforms = transformations; -
3976 for (int i = 0; i < transformations.size(); ++i) -
3977 transformations.at(i)->d_func()->setItem(this); -
3978 d_ptr->transformData->onlyTransform = false; -
3979 d_ptr->dirtySceneTransform = 1; -
3980 d_ptr->transformChanged(); -
3981} -
3982 -
3983/*! -
3984 \internal -
3985*/ -
3986void QGraphicsItemPrivate::prependGraphicsTransform(QGraphicsTransform *t) -
3987{ -
3988 if (!transformData) -
3989 transformData = new QGraphicsItemPrivate::TransformData; -
3990 if (!transformData->graphicsTransforms.contains(t)) -
3991 transformData->graphicsTransforms.prepend(t); -
3992 -
3993 Q_Q(QGraphicsItem); -
3994 t->d_func()->setItem(q); -
3995 transformData->onlyTransform = false; -
3996 dirtySceneTransform = 1; -
3997 transformChanged(); -
3998} -
3999 -
4000/*! -
4001 \internal -
4002*/ -
4003void QGraphicsItemPrivate::appendGraphicsTransform(QGraphicsTransform *t) -
4004{ -
4005 if (!transformData) -
4006 transformData = new QGraphicsItemPrivate::TransformData; -
4007 if (!transformData->graphicsTransforms.contains(t)) -
4008 transformData->graphicsTransforms.append(t); -
4009 -
4010 Q_Q(QGraphicsItem); -
4011 t->d_func()->setItem(q); -
4012 transformData->onlyTransform = false; -
4013 dirtySceneTransform = 1; -
4014 transformChanged(); -
4015} -
4016 -
4017/*! -
4018 \since 4.6 -
4019 -
4020 Returns the origin point for the transformation in item coordinates. -
4021 -
4022 The default is QPointF(0,0). -
4023 -
4024 \sa setTransformOriginPoint(), {Transformations} -
4025*/ -
4026QPointF QGraphicsItem::transformOriginPoint() const -
4027{ -
4028 if (!d_ptr->transformData) -
4029 return QPointF(0,0); -
4030 return QPointF(d_ptr->transformData->xOrigin, d_ptr->transformData->yOrigin); -
4031} -
4032 -
4033/*! -
4034 \since 4.6 -
4035 -
4036 Sets the \a origin point for the transformation in item coordinates. -
4037 -
4038 \sa transformOriginPoint(), {Transformations} -
4039*/ -
4040void QGraphicsItem::setTransformOriginPoint(const QPointF &origin) -
4041{ -
4042 prepareGeometryChange(); -
4043 QPointF newOrigin = origin; -
4044 -
4045 if (d_ptr->flags & ItemSendsGeometryChanges) { -
4046 // Notify the item that the origin point is changing. -
4047 const QVariant newOriginVariant(itemChange(ItemTransformOriginPointChange, -
4048 QVariant::fromValue<QPointF>(origin))); -
4049 newOrigin = newOriginVariant.toPointF(); -
4050 } -
4051 -
4052 if (!d_ptr->transformData) -
4053 d_ptr->transformData = new QGraphicsItemPrivate::TransformData; -
4054 -
4055 if (d_ptr->transformData->xOrigin == newOrigin.x() -
4056 && d_ptr->transformData->yOrigin == newOrigin.y()) { -
4057 return; -
4058 } -
4059 -
4060 d_ptr->transformData->xOrigin = newOrigin.x(); -
4061 d_ptr->transformData->yOrigin = newOrigin.y(); -
4062 d_ptr->transformData->onlyTransform = false; -
4063 d_ptr->dirtySceneTransform = 1; -
4064 -
4065 // Send post-notification. -
4066 if (d_ptr->flags & ItemSendsGeometryChanges) -
4067 itemChange(ItemTransformOriginPointHasChanged, QVariant::fromValue<QPointF>(newOrigin)); -
4068} -
4069 -
4070/*! -
4071 \fn void QGraphicsItem::setTransformOriginPoint(qreal x, qreal y) -
4072 -
4073 \since 4.6 -
4074 \overload -
4075 -
4076 Sets the origin point for the transformation in item coordinates. -
4077 This is equivalent to calling setTransformOriginPoint(QPointF(\a x, \a y)). -
4078 -
4079 \sa setTransformOriginPoint(), {Transformations} -
4080*/ -
4081 -
4082 -
4083/*! -
4084 \obsolete -
4085 -
4086 Use sceneTransform() instead. -
4087 -
4088 \sa transform(), setTransform(), scenePos(), {The Graphics View Coordinate System} -
4089*/ -
4090QMatrix QGraphicsItem::sceneMatrix() const -
4091{ -
4092 d_ptr->ensureSceneTransform(); -
4093 return d_ptr->sceneTransform.toAffine(); -
4094} -
4095 -
4096 -
4097/*! -
4098 \since 4.3 -
4099 -
4100 Returns this item's scene transformation matrix. This matrix can be used -
4101 to map coordinates and geometrical shapes from this item's local -
4102 coordinate system to the scene's coordinate system. To map coordinates -
4103 from the scene, you must first invert the returned matrix. -
4104 -
4105 Example: -
4106 -
4107 \snippet code/src_gui_graphicsview_qgraphicsitem.cpp 4 -
4108 -
4109 Unlike transform(), which returns only an item's local transformation, this -
4110 function includes the item's (and any parents') position, and all the transfomation properties. -
4111 -
4112 \sa transform(), setTransform(), scenePos(), {The Graphics View Coordinate System}, {Transformations} -
4113*/ -
4114QTransform QGraphicsItem::sceneTransform() const -
4115{ -
4116 d_ptr->ensureSceneTransform(); -
4117 return d_ptr->sceneTransform; -
4118} -
4119 -
4120/*! -
4121 \since 4.3 -
4122 -
4123 Returns this item's device transformation matrix, using \a -
4124 viewportTransform to map from scene to device coordinates. This matrix can -
4125 be used to map coordinates and geometrical shapes from this item's local -
4126 coordinate system to the viewport's (or any device's) coordinate -
4127 system. To map coordinates from the viewport, you must first invert the -
4128 returned matrix. -
4129 -
4130 Example: -
4131 -
4132 \snippet code/src_gui_graphicsview_qgraphicsitem.cpp 5 -
4133 -
4134 This function is the same as combining this item's scene transform with -
4135 the view's viewport transform, but it also understands the -
4136 ItemIgnoresTransformations flag. The device transform can be used to do -
4137 accurate coordinate mapping (and collision detection) for untransformable -
4138 items. -
4139 -
4140 \sa transform(), setTransform(), scenePos(), {The Graphics View Coordinate -
4141 System}, itemTransform() -
4142*/ -
4143QTransform QGraphicsItem::deviceTransform(const QTransform &viewportTransform) const -
4144{ -
4145 // Ensure we return the standard transform if we're not untransformable. -
4146 if (!d_ptr->itemIsUntransformable()) { -
4147 d_ptr->ensureSceneTransform(); -
4148 return d_ptr->sceneTransform * viewportTransform; -
4149 } -
4150 -
4151 // Find the topmost item that ignores view transformations. -
4152 const QGraphicsItem *untransformedAncestor = this; -
4153 QList<const QGraphicsItem *> parents; -
4154 while (untransformedAncestor && ((untransformedAncestor->d_ptr->ancestorFlags -
4155 & QGraphicsItemPrivate::AncestorIgnoresTransformations))) { -
4156 parents.prepend(untransformedAncestor); -
4157 untransformedAncestor = untransformedAncestor->parentItem(); -
4158 } -
4159 -
4160 if (!untransformedAncestor) { -
4161 // Assert in debug mode, continue in release. -
4162 Q_ASSERT_X(untransformedAncestor, "QGraphicsItem::deviceTransform", -
4163 "Invalid object structure!"); -
4164 return QTransform(); -
4165 } -
4166 -
4167 // First translate the base untransformable item. -
4168 untransformedAncestor->d_ptr->ensureSceneTransform(); -
4169 QPointF mappedPoint = (untransformedAncestor->d_ptr->sceneTransform * viewportTransform).map(QPointF(0, 0)); -
4170 -
4171 // COMBINE -
4172 QTransform matrix = QTransform::fromTranslate(mappedPoint.x(), mappedPoint.y()); -
4173 if (untransformedAncestor->d_ptr->transformData) -
4174 matrix = untransformedAncestor->d_ptr->transformData->computedFullTransform(&matrix); -
4175 -
4176 // Then transform and translate all children. -
4177 for (int i = 0; i < parents.size(); ++i) { -
4178 const QGraphicsItem *parent = parents.at(i); -
4179 parent->d_ptr->combineTransformFromParent(&matrix); -
4180 } -
4181 -
4182 return matrix; -
4183} -
4184 -
4185/*! -
4186 \since 4.5 -
4187 -
4188 Returns a QTransform that maps coordinates from this item to \a other. If -
4189 \a ok is not null, and if there is no such transform, the boolean pointed -
4190 to by \a ok will be set to false; otherwise it will be set to true. -
4191 -
4192 This transform provides an alternative to the mapToItem() or mapFromItem() -
4193 functions, by returning the appropriate transform so that you can map -
4194 shapes and coordinates yourself. It also helps you write more efficient -
4195 code when repeatedly mapping between the same two items. -
4196 -
4197 \note In rare circumstances, there is no transform that maps between two -
4198 items. -
4199 -
4200 \sa mapToItem(), mapFromItem(), deviceTransform() -
4201*/ -
4202QTransform QGraphicsItem::itemTransform(const QGraphicsItem *other, bool *ok) const -
4203{ -
4204 // Catch simple cases first. -
4205 if (other == 0) { -
4206 qWarning("QGraphicsItem::itemTransform: null pointer passed"); -
4207 return QTransform(); -
4208 } -
4209 if (other == this) { -
4210 if (ok) -
4211 *ok = true; -
4212 return QTransform(); -
4213 } -
4214 -
4215 QGraphicsItem *parent = d_ptr->parent; -
4216 const QGraphicsItem *otherParent = other->d_ptr->parent; -
4217 -
4218 // This is other's child -
4219 if (parent == other) { -
4220 if (ok) -
4221 *ok = true; -
4222 QTransform x; -
4223 d_ptr->combineTransformFromParent(&x); -
4224 return x; -
4225 } -
4226 -
4227 // This is other's parent -
4228 if (otherParent == this) { -
4229 const QPointF &otherPos = other->d_ptr->pos; -
4230 if (other->d_ptr->transformData) { -
4231 QTransform otherToParent; -
4232 other->d_ptr->combineTransformFromParent(&otherToParent); -
4233 return otherToParent.inverted(ok); -
4234 } -
4235 if (ok) -
4236 *ok = true; -
4237 return QTransform::fromTranslate(-otherPos.x(), -otherPos.y()); -
4238 } -
4239 -
4240 // Siblings -
4241 if (parent == otherParent) { -
4242 // COMBINE -
4243 const QPointF &itemPos = d_ptr->pos; -
4244 const QPointF &otherPos = other->d_ptr->pos; -
4245 if (!d_ptr->transformData && !other->d_ptr->transformData) { -
4246 QPointF delta = itemPos - otherPos; -
4247 if (ok) -
4248 *ok = true; -
4249 return QTransform::fromTranslate(delta.x(), delta.y()); -
4250 } -
4251 -
4252 QTransform itemToParent; -
4253 d_ptr->combineTransformFromParent(&itemToParent); -
4254 QTransform otherToParent; -
4255 other->d_ptr->combineTransformFromParent(&otherToParent); -
4256 return itemToParent * otherToParent.inverted(ok); -
4257 } -
4258 -
4259 // Find the closest common ancestor. If the two items don't share an -
4260 // ancestor, then the only way is to combine their scene transforms. -
4261 const QGraphicsItem *commonAncestor = commonAncestorItem(other); -
4262 if (!commonAncestor) { -
4263 d_ptr->ensureSceneTransform(); -
4264 other->d_ptr->ensureSceneTransform(); -
4265 return d_ptr->sceneTransform * other->d_ptr->sceneTransform.inverted(ok); -
4266 } -
4267 -
4268 // If the two items are cousins (in sibling branches), map both to the -
4269 // common ancestor, and combine the two transforms. -
4270 bool cousins = other != commonAncestor && this != commonAncestor; -
4271 if (cousins) { -
4272 bool good = false; -
4273 QTransform thisToScene = itemTransform(commonAncestor, &good); -
4274 QTransform otherToScene(Qt::Uninitialized); -
4275 if (good) -
4276 otherToScene = other->itemTransform(commonAncestor, &good); -
4277 if (!good) { -
4278 if (ok) -
4279 *ok = false; -
4280 return QTransform(); -
4281 } -
4282 return thisToScene * otherToScene.inverted(ok); -
4283 } -
4284 -
4285 // One is an ancestor of the other; walk the chain. -
4286 bool parentOfOther = isAncestorOf(other); -
4287 const QGraphicsItem *child = parentOfOther ? other : this; -
4288 const QGraphicsItem *root = parentOfOther ? this : other; -
4289 -
4290 QTransform x; -
4291 const QGraphicsItem *p = child; -
4292 do { -
4293 p->d_ptr.data()->combineTransformToParent(&x); -
4294 } while ((p = p->d_ptr->parent) && p != root); -
4295 if (parentOfOther) -
4296 return x.inverted(ok); -
4297 if (ok) -
4298 *ok = true; -
4299 return x; -
4300} -
4301 -
4302/*! -
4303 \obsolete -
4304 -
4305 Sets the item's affine transformation matrix. This is a subset or the -
4306 item's full transformation matrix, and might not represent the item's full -
4307 transformation. -
4308 -
4309 Use setTransform() instead. -
4310 -
4311 \sa transform(), {The Graphics View Coordinate System} -
4312*/ -
4313void QGraphicsItem::setMatrix(const QMatrix &matrix, bool combine) -
4314{ -
4315 if (!d_ptr->transformData) -
4316 d_ptr->transformData = new QGraphicsItemPrivate::TransformData; -
4317 -
4318 QTransform newTransform(combine ? QTransform(matrix) * d_ptr->transformData->transform : QTransform(matrix)); -
4319 if (d_ptr->transformData->transform == newTransform) -
4320 return; -
4321 -
4322 // Update and set the new transformation. -
4323 if (!(d_ptr->flags & ItemSendsGeometryChanges)) { -
4324 d_ptr->setTransformHelper(newTransform); -
4325 return; -
4326 } -
4327 -
4328 // Notify the item that the transformation matrix is changing. -
4329 const QVariant newMatrixVariant = QVariant::fromValue<QMatrix>(newTransform.toAffine()); -
4330 newTransform = QTransform(qvariant_cast<QMatrix>(itemChange(ItemMatrixChange, newMatrixVariant))); -
4331 if (d_ptr->transformData->transform == newTransform) -
4332 return; -
4333 -
4334 // Update and set the new transformation. -
4335 d_ptr->setTransformHelper(newTransform); -
4336 -
4337 // Send post-notification. -
4338 itemChange(ItemTransformHasChanged, QVariant::fromValue<QTransform>(newTransform)); -
4339} -
4340 -
4341/*! -
4342 \since 4.3 -
4343 -
4344 Sets the item's current transformation matrix to \a matrix. -
4345 -
4346 If \a combine is true, then \a matrix is combined with the current matrix; -
4347 otherwise, \a matrix \e replaces the current matrix. \a combine is false -
4348 by default. -
4349 -
4350 To simplify interation with items using a transformed view, QGraphicsItem -
4351 provides mapTo... and mapFrom... functions that can translate between -
4352 items' and the scene's coordinates. For example, you can call mapToScene() -
4353 to map an item coordiate to a scene coordinate, or mapFromScene() to map -
4354 from scene coordinates to item coordinates. -
4355 -
4356 The transformation matrix is combined with the item's rotation(), scale() -
4357 and transformations() into a combined transformation that maps the item's -
4358 coordinate system to its parent. -
4359 -
4360 \sa transform(), setRotation(), setScale(), setTransformOriginPoint(), {The Graphics View Coordinate System}, {Transformations} -
4361*/ -
4362void QGraphicsItem::setTransform(const QTransform &matrix, bool combine) -
4363{ -
4364 if (!d_ptr->transformData) -
4365 d_ptr->transformData = new QGraphicsItemPrivate::TransformData; -
4366 -
4367 QTransform newTransform(combine ? matrix * d_ptr->transformData->transform : matrix); -
4368 if (d_ptr->transformData->transform == newTransform) -
4369 return; -
4370 -
4371 // Update and set the new transformation. -
4372 if (!(d_ptr->flags & (ItemSendsGeometryChanges | ItemSendsScenePositionChanges))) { -
4373 d_ptr->setTransformHelper(newTransform); -
4374 if (d_ptr->scenePosDescendants) -
4375 d_ptr->sendScenePosChange(); -
4376 return; -
4377 } -
4378 -
4379 // Notify the item that the transformation matrix is changing. -
4380 const QVariant newTransformVariant(itemChange(ItemTransformChange, -
4381 QVariant::fromValue<QTransform>(newTransform))); -
4382 newTransform = qvariant_cast<QTransform>(newTransformVariant); -
4383 if (d_ptr->transformData->transform == newTransform) -
4384 return; -
4385 -
4386 // Update and set the new transformation. -
4387 d_ptr->setTransformHelper(newTransform); -
4388 -
4389 // Send post-notification. -
4390 itemChange(ItemTransformHasChanged, newTransformVariant); -
4391 d_ptr->sendScenePosChange(); -
4392} -
4393 -
4394/*! -
4395 \obsolete -
4396 -
4397 Use resetTransform() instead. -
4398*/ -
4399void QGraphicsItem::resetMatrix() -
4400{ -
4401 resetTransform(); -
4402} -
4403 -
4404/*! -
4405 \since 4.3 -
4406 -
4407 Resets this item's transformation matrix to the identity matrix or -
4408 all the transformation properties to their default values. -
4409 This is equivalent to calling \c setTransform(QTransform()). -
4410 -
4411 \sa setTransform(), transform() -
4412*/ -
4413void QGraphicsItem::resetTransform() -
4414{ -
4415 setTransform(QTransform(), false); -
4416} -
4417 -
4418/*! -
4419 \fn void QGraphicsItem::rotate(qreal angle) -
4420 \obsolete -
4421 -
4422 Use -
4423 -
4424 \code -
4425 setRotation(rotation() + angle); -
4426 \endcode -
4427 -
4428 instead. -
4429 -
4430 Rotates the current item transformation \a angle degrees clockwise around -
4431 its origin. To translate around an arbitrary point (x, y), you need to -
4432 combine translation and rotation with setTransform(). -
4433 -
4434 Example: -
4435 -
4436 \snippet code/src_gui_graphicsview_qgraphicsitem.cpp 6 -
4437 -
4438 \sa setTransform(), transform(), scale(), shear(), translate() -
4439*/ -
4440 -
4441/*! -
4442 \fn void QGraphicsItem::scale(qreal sx, qreal sy) -
4443 \obsolete -
4444 -
4445 Use -
4446 -
4447 \code -
4448 setTransform(QTransform::fromScale(sx, sy), true); -
4449 \endcode -
4450 -
4451 instead. -
4452 -
4453 Scales the current item transformation by (\a sx, \a sy) around its -
4454 origin. To scale from an arbitrary point (x, y), you need to combine -
4455 translation and scaling with setTransform(). -
4456 -
4457 Example: -
4458 -
4459 \snippet code/src_gui_graphicsview_qgraphicsitem.cpp 7 -
4460 -
4461 \sa setTransform(), transform() -
4462*/ -
4463 -
4464/*! -
4465 \fn void QGraphicsItem::shear(qreal sh, qreal sv) -
4466 \obsolete -
4467 -
4468 Use -
4469 -
4470 \code -
4471 setTransform(QTransform().shear(sh, sv), true); -
4472 \endcode -
4473 -
4474 instead. -
4475 -
4476 Shears the current item transformation by (\a sh, \a sv). -
4477 -
4478 \sa setTransform(), transform() -
4479*/ -
4480 -
4481/*! -
4482 \fn void QGraphicsItem::translate(qreal dx, qreal dy) -
4483 \obsolete -
4484 -
4485 Use setPos() or setTransformOriginPoint() instead. For identical -
4486 behavior, use -
4487 -
4488 \code -
4489 setTransform(QTransform::fromTranslate(dx, dy), true); -
4490 \endcode -
4491 -
4492 Translates the current item transformation by (\a dx, \a dy). -
4493 -
4494 If all you want is to move an item, you should call moveBy() or -
4495 setPos() instead; this function changes the item's translation, -
4496 which is conceptually separate from its position. -
4497 -
4498 \sa setTransform(), transform() -
4499*/ -
4500 -
4501/*! -
4502 This virtual function is called twice for all items by the -
4503 QGraphicsScene::advance() slot. In the first phase, all items are called -
4504 with \a phase == 0, indicating that items on the scene are about to -
4505 advance, and then all items are called with \a phase == 1. Reimplement -
4506 this function to update your item if you need simple scene-controlled -
4507 animation. -
4508 -
4509 The default implementation does nothing. -
4510 -
4511 This function is intended for animations. An alternative is to -
4512 multiple-inherit from QObject and QGraphicsItem and use the \l{The Animation -
4513 Framework}{Animation Framework}. -
4514 -
4515 \sa QGraphicsScene::advance(), QTimeLine -
4516*/ -
4517void QGraphicsItem::advance(int phase) -
4518{ -
4519 Q_UNUSED(phase); -
4520} -
4521 -
4522/*! -
4523 Returns the Z-value of the item. The Z-value affects the stacking order of -
4524 sibling (neighboring) items. -
4525 -
4526 The default Z-value is 0. -
4527 -
4528 \sa setZValue(), {QGraphicsItem#Sorting}{Sorting}, stackBefore(), ItemStacksBehindParent -
4529*/ -
4530qreal QGraphicsItem::zValue() const -
4531{ -
4532 return d_ptr->z; -
4533} -
4534 -
4535/*! -
4536 Sets the Z-value of the item to \a z. The Z value decides the stacking -
4537 order of sibling (neighboring) items. A sibling item of high Z value will -
4538 always be drawn on top of another sibling item with a lower Z value. -
4539 -
4540 If you restore the Z value, the item's insertion order will decide its -
4541 stacking order. -
4542 -
4543 The Z-value does not affect the item's size in any way. -
4544 -
4545 The default Z-value is 0. -
4546 -
4547 \sa zValue(), {QGraphicsItem#Sorting}{Sorting}, stackBefore(), ItemStacksBehindParent -
4548*/ -
4549void QGraphicsItem::setZValue(qreal z) -
4550{ -
4551 const QVariant newZVariant(itemChange(ItemZValueChange, z)); -
4552 qreal newZ = newZVariant.toReal(); -
4553 if (newZ == d_ptr->z) -
4554 return; -
4555 -
4556 if (d_ptr->scene && d_ptr->scene->d_func()->indexMethod != QGraphicsScene::NoIndex) { -
4557 // Z Value has changed, we have to notify the index. -
4558 d_ptr->scene->d_func()->index->itemChange(this, ItemZValueChange, &newZ); -
4559 } -
4560 -
4561 d_ptr->z = newZ; -
4562 if (d_ptr->parent) -
4563 d_ptr->parent->d_ptr->needSortChildren = 1; -
4564 else if (d_ptr->scene) -
4565 d_ptr->scene->d_func()->needSortTopLevelItems = 1; -
4566 -
4567 if (d_ptr->scene) -
4568 d_ptr->scene->d_func()->markDirty(this, QRectF(), /*invalidateChildren=*/true); -
4569 -
4570 itemChange(ItemZValueHasChanged, newZVariant); -
4571 -
4572 if (d_ptr->flags & ItemNegativeZStacksBehindParent) -
4573 setFlag(QGraphicsItem::ItemStacksBehindParent, z < qreal(0.0)); -
4574 -
4575 if (d_ptr->isObject) -
4576 emit static_cast<QGraphicsObject *>(this)->zChanged(); -
4577} -
4578 -
4579/*! -
4580 \internal -
4581 -
4582 Ensures that the list of children is sorted by insertion order, and that -
4583 the siblingIndexes are packed (no gaps), and start at 0. -
4584 -
4585 ### This function is almost identical to -
4586 QGraphicsScenePrivate::ensureSequentialTopLevelSiblingIndexes(). -
4587*/ -
4588void QGraphicsItemPrivate::ensureSequentialSiblingIndex() -
4589{ -
4590 if (!sequentialOrdering) { -
4591 std::sort(children.begin(), children.end(), insertionOrder); -
4592 sequentialOrdering = 1; -
4593 needSortChildren = 1; -
4594 } -
4595 if (holesInSiblingIndex) { -
4596 holesInSiblingIndex = 0; -
4597 for (int i = 0; i < children.size(); ++i) -
4598 children[i]->d_ptr->siblingIndex = i; -
4599 } -
4600} -
4601 -
4602/*! -
4603 \internal -
4604*/ -
4605inline void QGraphicsItemPrivate::sendScenePosChange() -
4606{ -
4607 Q_Q(QGraphicsItem); -
4608 if (scene) { -
4609 if (flags & QGraphicsItem::ItemSendsScenePositionChanges) -
4610 q->itemChange(QGraphicsItem::ItemScenePositionHasChanged, q->scenePos()); -
4611 if (scenePosDescendants) { -
4612 foreach (QGraphicsItem *item, scene->d_func()->scenePosItems) { -
4613 if (q->isAncestorOf(item)) -
4614 item->itemChange(QGraphicsItem::ItemScenePositionHasChanged, item->scenePos()); -
4615 } -
4616 } -
4617 } -
4618} -
4619 -
4620/*! -
4621 \since 4.6 -
4622 -
4623 Stacks this item before \a sibling, which must be a sibling item (i.e., the -
4624 two items must share the same parent item, or must both be toplevel items). -
4625 The \a sibling must have the same Z value as this item, otherwise calling -
4626 this function will have no effect. -
4627 -
4628 By default, all sibling items are stacked by insertion order (i.e., the -
4629 first item you add is drawn before the next item you add). If two items' Z -
4630 values are different, then the item with the highest Z value is drawn on -
4631 top. When the Z values are the same, the insertion order will decide the -
4632 stacking order. -
4633 -
4634 \sa setZValue(), ItemStacksBehindParent, {QGraphicsItem#Sorting}{Sorting} -
4635*/ -
4636void QGraphicsItem::stackBefore(const QGraphicsItem *sibling) -
4637{ -
4638 if (sibling == this) -
4639 return; -
4640 if (!sibling || d_ptr->parent != sibling->parentItem()) { -
4641 qWarning("QGraphicsItem::stackUnder: cannot stack under %p, which must be a sibling", sibling); -
4642 return; -
4643 } -
4644 QList<QGraphicsItem *> *siblings = d_ptr->parent -
4645 ? &d_ptr->parent->d_ptr->children -
4646 : (d_ptr->scene ? &d_ptr->scene->d_func()->topLevelItems : 0); -
4647 if (!siblings) { -
4648 qWarning("QGraphicsItem::stackUnder: cannot stack under %p, which must be a sibling", sibling); -
4649 return; -
4650 } -
4651 -
4652 // First, make sure that the sibling indexes have no holes. This also -
4653 // marks the children list for sorting. -
4654 if (d_ptr->parent) -
4655 d_ptr->parent->d_ptr->ensureSequentialSiblingIndex(); -
4656 else -
4657 d_ptr->scene->d_func()->ensureSequentialTopLevelSiblingIndexes(); -
4658 -
4659 // Only move items with the same Z value, and that need moving. -
4660 int siblingIndex = sibling->d_ptr->siblingIndex; -
4661 int myIndex = d_ptr->siblingIndex; -
4662 if (myIndex >= siblingIndex) { -
4663 siblings->move(myIndex, siblingIndex); -
4664 // Fixup the insertion ordering. -
4665 for (int i = 0; i < siblings->size(); ++i) { -
4666 int &index = siblings->at(i)->d_ptr->siblingIndex; -
4667 if (i != siblingIndex && index >= siblingIndex && index <= myIndex) -
4668 ++index; -
4669 } -
4670 d_ptr->siblingIndex = siblingIndex; -
4671 for (int i = 0; i < siblings->size(); ++i) { -
4672 int &index = siblings->at(i)->d_ptr->siblingIndex; -
4673 if (i != siblingIndex && index >= siblingIndex && index <= myIndex) -
4674 siblings->at(i)->d_ptr->siblingOrderChange(); -
4675 } -
4676 d_ptr->siblingOrderChange(); -
4677 } -
4678} -
4679 -
4680/*! -
4681 Returns the bounding rect of this item's descendants (i.e., its -
4682 children, their children, etc.) in local coordinates. The -
4683 rectangle will contain all descendants after they have been mapped -
4684 to local coordinates. If the item has no children, this function -
4685 returns an empty QRectF. -
4686 -
4687 This does not include this item's own bounding rect; it only returns -
4688 its descendants' accumulated bounding rect. If you need to include this -
4689 item's bounding rect, you can add boundingRect() to childrenBoundingRect() -
4690 using QRectF::operator|(). -
4691 -
4692 This function is linear in complexity; it determines the size of the -
4693 returned bounding rect by iterating through all descendants. -
4694 -
4695 \sa boundingRect(), sceneBoundingRect() -
4696*/ -
4697QRectF QGraphicsItem::childrenBoundingRect() const -
4698{ -
4699 if (!d_ptr->dirtyChildrenBoundingRect) -
4700 return d_ptr->childrenBoundingRect; -
4701 -
4702 d_ptr->childrenBoundingRect = QRectF(); -
4703 d_ptr->childrenBoundingRectHelper(0, &d_ptr->childrenBoundingRect, 0); -
4704 d_ptr->dirtyChildrenBoundingRect = 0; -
4705 return d_ptr->childrenBoundingRect; -
4706} -
4707 -
4708/*! -
4709 \fn virtual QRectF QGraphicsItem::boundingRect() const = 0 -
4710 -
4711 This pure virtual function defines the outer bounds of the item as -
4712 a rectangle; all painting must be restricted to inside an item's -
4713 bounding rect. QGraphicsView uses this to determine whether the -
4714 item requires redrawing. -
4715 -
4716 Although the item's shape can be arbitrary, the bounding rect is -
4717 always rectangular, and it is unaffected by the items' -
4718 transformation. -
4719 -
4720 If you want to change the item's bounding rectangle, you must first call -
4721 prepareGeometryChange(). This notifies the scene of the imminent change, -
4722 so that it can update its item geometry index; otherwise, the scene will -
4723 be unaware of the item's new geometry, and the results are undefined -
4724 (typically, rendering artifacts are left within the view). -
4725 -
4726 Reimplement this function to let QGraphicsView determine what -
4727 parts of the widget, if any, need to be redrawn. -
4728 -
4729 Note: For shapes that paint an outline / stroke, it is important -
4730 to include half the pen width in the bounding rect. It is not -
4731 necessary to compensate for antialiasing, though. -
4732 -
4733 Example: -
4734 -
4735 \snippet code/src_gui_graphicsview_qgraphicsitem.cpp 8 -
4736 -
4737 \sa boundingRegion(), shape(), contains(), {The Graphics View Coordinate -
4738 System}, prepareGeometryChange() -
4739*/ -
4740 -
4741/*! -
4742 Returns the bounding rect of this item in scene coordinates, by combining -
4743 sceneTransform() with boundingRect(). -
4744 -
4745 \sa boundingRect(), {The Graphics View Coordinate System} -
4746*/ -
4747QRectF QGraphicsItem::sceneBoundingRect() const -
4748{ -
4749 // Find translate-only offset -
4750 // COMBINE -
4751 QPointF offset; -
4752 const QGraphicsItem *parentItem = this; -
4753 const QGraphicsItemPrivate *itemd; -
4754 do { -
4755 itemd = parentItem->d_ptr.data(); -
4756 if (itemd->transformData) -
4757 break; -
4758 offset += itemd->pos; -
4759 } while ((parentItem = itemd->parent)); -
4760 -
4761 QRectF br = boundingRect(); -
4762 br.translate(offset); -
4763 if (!parentItem) -
4764 return br; -
4765 if (parentItem->d_ptr->hasTranslateOnlySceneTransform()) { -
4766 br.translate(parentItem->d_ptr->sceneTransform.dx(), parentItem->d_ptr->sceneTransform.dy()); -
4767 return br; -
4768 } -
4769 return parentItem->d_ptr->sceneTransform.mapRect(br); -
4770} -
4771 -
4772/*! -
4773 Returns the shape of this item as a QPainterPath in local -
4774 coordinates. The shape is used for many things, including collision -
4775 detection, hit tests, and for the QGraphicsScene::items() functions. -
4776 -
4777 The default implementation calls boundingRect() to return a simple -
4778 rectangular shape, but subclasses can reimplement this function to return -
4779 a more accurate shape for non-rectangular items. For example, a round item -
4780 may choose to return an elliptic shape for better collision detection. For -
4781 example: -
4782 -
4783 \snippet code/src_gui_graphicsview_qgraphicsitem.cpp 9 -
4784 -
4785 The outline of a shape can vary depending on the width and style of the -
4786 pen used when drawing. If you want to include this outline in the item's -
4787 shape, you can create a shape from the stroke using QPainterPathStroker. -
4788 -
4789 This function is called by the default implementations of contains() and -
4790 collidesWithPath(). -
4791 -
4792 \sa boundingRect(), contains(), prepareGeometryChange(), QPainterPathStroker -
4793*/ -
4794QPainterPath QGraphicsItem::shape() const -
4795{ -
4796 QPainterPath path; -
4797 path.addRect(boundingRect()); -
4798 return path; -
4799} -
4800 -
4801/*! -
4802 Returns true if this item is clipped. An item is clipped if it has either -
4803 set the \l ItemClipsToShape flag, or if it or any of its ancestors has set -
4804 the \l ItemClipsChildrenToShape flag. -
4805 -
4806 Clipping affects the item's appearance (i.e., painting), as well as mouse -
4807 and hover event delivery. -
4808 -
4809 \sa clipPath(), shape(), setFlags() -
4810*/ -
4811bool QGraphicsItem::isClipped() const -
4812{ -
4813 Q_D(const QGraphicsItem); -
4814 return (d->ancestorFlags & QGraphicsItemPrivate::AncestorClipsChildren) -
4815 || (d->flags & QGraphicsItem::ItemClipsToShape); -
4816} -
4817 -
4818/*! -
4819 \since 4.5 -
4820 -
4821 Returns this item's clip path, or an empty QPainterPath if this item is -
4822 not clipped. The clip path constrains the item's appearance and -
4823 interaction (i.e., restricts the area the item can draw within and receive -
4824 events for). -
4825 -
4826 You can enable clipping by setting the ItemClipsToShape or -
4827 ItemClipsChildrenToShape flags. The item's clip path is calculated by -
4828 intersecting all clipping ancestors' shapes. If the item sets -
4829 ItemClipsToShape, the final clip is intersected with the item's own shape. -
4830 -
4831 \note Clipping introduces a performance penalty for all items involved; -
4832 you should generally avoid using clipping if you can (e.g., if your items -
4833 always draw inside boundingRect() or shape() boundaries, clipping is not -
4834 necessary). -
4835 -
4836 \sa isClipped(), shape(), setFlags() -
4837*/ -
4838QPainterPath QGraphicsItem::clipPath() const -
4839{ -
4840 Q_D(const QGraphicsItem); -
4841 if (!isClipped()) -
4842 return QPainterPath(); -
4843 -
4844 const QRectF thisBoundingRect(boundingRect()); -
4845 if (thisBoundingRect.isEmpty()) -
4846 return QPainterPath(); -
4847 -
4848 QPainterPath clip; -
4849 // Start with the item's bounding rect. -
4850 clip.addRect(thisBoundingRect); -
4851 -
4852 if (d->ancestorFlags & QGraphicsItemPrivate::AncestorClipsChildren) { -
4853 const QGraphicsItem *parent = this; -
4854 const QGraphicsItem *lastParent = this; -
4855 -
4856 // Intersect any in-between clips starting at the top and moving downwards. -
4857 while ((parent = parent->d_ptr->parent)) { -
4858 if (parent->d_ptr->flags & ItemClipsChildrenToShape) { -
4859 // Map clip to the current parent and intersect with its shape/clipPath -
4860 clip = lastParent->itemTransform(parent).map(clip); -
4861 clip = clip.intersected(parent->shape()); -
4862 if (clip.isEmpty()) -
4863 return clip; -
4864 lastParent = parent; -
4865 } -
4866 -
4867 if (!(parent->d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorClipsChildren)) -
4868 break; -
4869 } -
4870 -
4871 if (lastParent != this) { -
4872 // Map clip back to the item's transform. -
4873 // ### what if itemtransform fails -
4874 clip = lastParent->itemTransform(this).map(clip); -
4875 } -
4876 } -
4877 -
4878 if (d->flags & ItemClipsToShape) -
4879 clip = clip.intersected(shape()); -
4880 -
4881 return clip; -
4882} -
4883 -
4884/*! -
4885 Returns true if this item contains \a point, which is in local -
4886 coordinates; otherwise, false is returned. It is most often called from -
4887 QGraphicsView to determine what item is under the cursor, and for that -
4888 reason, the implementation of this function should be as light-weight as -
4889 possible. -
4890 -
4891 By default, this function calls shape(), but you can reimplement it in a -
4892 subclass to provide a (perhaps more efficient) implementation. -
4893 -
4894 \sa shape(), boundingRect(), collidesWithPath() -
4895*/ -
4896bool QGraphicsItem::contains(const QPointF &point) const -
4897{ -
4898 return isClipped() ? clipPath().contains(point) : shape().contains(point); -
4899} -
4900 -
4901/*! -
4902 -
4903 Returns true if this item collides with \a other; otherwise -
4904 returns false. -
4905 -
4906 The \a mode is applied to \a other, and the resulting shape or -
4907 bounding rectangle is then compared to this item's shape. The -
4908 default value for \a mode is Qt::IntersectsItemShape; \a other -
4909 collides with this item if it either intersects, contains, or is -
4910 contained by this item's shape (see Qt::ItemSelectionMode for -
4911 details). -
4912 -
4913 The default implementation is based on shape intersection, and it calls -
4914 shape() on both items. Because the complexity of arbitrary shape-shape -
4915 intersection grows with an order of magnitude when the shapes are complex, -
4916 this operation can be noticably time consuming. You have the option of -
4917 reimplementing this function in a subclass of QGraphicsItem to provide a -
4918 custom algorithm. This allows you to make use of natural constraints in -
4919 the shapes of your own items, in order to improve the performance of the -
4920 collision detection. For instance, two untransformed perfectly circular -
4921 items' collision can be determined very efficiently by comparing their -
4922 positions and radii. -
4923 -
4924 Keep in mind that when reimplementing this function and calling shape() or -
4925 boundingRect() on \a other, the returned coordinates must be mapped to -
4926 this item's coordinate system before any intersection can take place. -
4927 -
4928 \sa contains(), shape() -
4929*/ -
4930bool QGraphicsItem::collidesWithItem(const QGraphicsItem *other, Qt::ItemSelectionMode mode) const -
4931{ -
4932 if (other == this) -
4933 return true; -
4934 if (!other) -
4935 return false; -
4936 // The items share the same clip if their closest clipper is the same, or -
4937 // if one clips the other. -
4938 bool clips = (d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorClipsChildren); -
4939 bool otherClips = (other->d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorClipsChildren); -
4940 if (clips || otherClips) { -
4941 const QGraphicsItem *closestClipper = isAncestorOf(other) ? this : parentItem(); -
4942 while (closestClipper && !(closestClipper->flags() & ItemClipsChildrenToShape)) -
4943 closestClipper = closestClipper->parentItem(); -
4944 const QGraphicsItem *otherClosestClipper = other->isAncestorOf(this) ? other : other->parentItem(); -
4945 while (otherClosestClipper && !(otherClosestClipper->flags() & ItemClipsChildrenToShape)) -
4946 otherClosestClipper = otherClosestClipper->parentItem(); -
4947 if (closestClipper == otherClosestClipper) { -
4948 d_ptr->localCollisionHack = 1; -
4949 bool res = collidesWithPath(mapFromItem(other, other->shape()), mode); -
4950 d_ptr->localCollisionHack = 0; -
4951 return res; -
4952 } -
4953 } -
4954 -
4955 QPainterPath otherShape = other->isClipped() ? other->clipPath() : other->shape(); -
4956 return collidesWithPath(mapFromItem(other, otherShape), mode); -
4957} -
4958 -
4959/*! -
4960 Returns true if this item collides with \a path. -
4961 -
4962 The collision is determined by \a mode. The default value for \a mode is -
4963 Qt::IntersectsItemShape; \a path collides with this item if it either -
4964 intersects, contains, or is contained by this item's shape. -
4965 -
4966 Note that this function checks whether the item's shape or -
4967 bounding rectangle (depending on \a mode) is contained within \a -
4968 path, and not whether \a path is contained within the items shape -
4969 or bounding rectangle. -
4970 -
4971 \sa collidesWithItem(), contains(), shape() -
4972*/ -
4973bool QGraphicsItem::collidesWithPath(const QPainterPath &path, Qt::ItemSelectionMode mode) const -
4974{ -
4975 if (path.isEmpty()) { -
4976 // No collision with empty paths. -
4977 return false; -
4978 } -
4979 -
4980 QRectF rectA(boundingRect()); -
4981 _q_adjustRect(&rectA); -
4982 QRectF rectB(path.controlPointRect()); -
4983 _q_adjustRect(&rectB); -
4984 if (!rectA.intersects(rectB)) { -
4985 // This we can determine efficiently. If the two rects neither -
4986 // intersect nor contain eachother, then the two items do not collide. -
4987 return false; -
4988 } -
4989 -
4990 // For further testing, we need this item's shape or bounding rect. -
4991 QPainterPath thisShape; -
4992 if (mode == Qt::IntersectsItemShape || mode == Qt::ContainsItemShape) -
4993 thisShape = (isClipped() && !d_ptr->localCollisionHack) ? clipPath() : shape(); -
4994 else -
4995 thisShape.addRect(rectA); -
4996 -
4997 if (thisShape == QPainterPath()) { -
4998 // Empty shape? No collision. -
4999 return false; -
5000 } -
5001 -
5002 // Use QPainterPath boolean operations to determine the collision, O(N*logN). -
5003 if (mode == Qt::IntersectsItemShape || mode == Qt::IntersectsItemBoundingRect) -
5004 return path.intersects(thisShape); -
5005 return path.contains(thisShape); -
5006} -
5007 -
5008/*! -
5009 Returns a list of all items that collide with this item. -
5010 -
5011 The way collisions are detected is determined by applying \a mode -
5012 to items that are compared to this item, i.e., each item's shape -
5013 or bounding rectangle is checked against this item's shape. The -
5014 default value for \a mode is Qt::IntersectsItemShape. -
5015 -
5016 \sa collidesWithItem() -
5017*/ -
5018QList<QGraphicsItem *> QGraphicsItem::collidingItems(Qt::ItemSelectionMode mode) const -
5019{ -
5020 if (d_ptr->scene) -
5021 return d_ptr->scene->collidingItems(this, mode); -
5022 return QList<QGraphicsItem *>(); -
5023} -
5024 -
5025/*! -
5026 \internal -
5027 -
5028 Item obscurity helper function. -
5029 -
5030 Returns true if the subrect \a rect of \a item's bounding rect is obscured -
5031 by \a other (i.e., \a other's opaque area covers \a item's \a rect -
5032 completely. \a other is assumed to already be "on top of" \a item -
5033 wrt. stacking order. -
5034*/ -
5035static bool qt_QGraphicsItem_isObscured(const QGraphicsItem *item, -
5036 const QGraphicsItem *other, -
5037 const QRectF &rect) -
5038{ -
5039 return other->mapToItem(item, other->opaqueArea()).contains(rect); -
5040} -
5041 -
5042/*! -
5043 \overload -
5044 \since 4.3 -
5045 -
5046 Returns true if \a rect is completely obscured by the opaque shape of any -
5047 of colliding items above it (i.e., with a higher Z value than this item). -
5048 -
5049 \sa opaqueArea() -
5050*/ -
5051bool QGraphicsItem::isObscured(const QRectF &rect) const -
5052{ -
5053 Q_D(const QGraphicsItem); -
5054 if (!d->scene) -
5055 return false; -
5056 -
5057 QRectF br = boundingRect(); -
5058 QRectF testRect = rect.isNull() ? br : rect; -
5059 -
5060 foreach (QGraphicsItem *item, d->scene->items(mapToScene(br), Qt::IntersectsItemBoundingRect)) { -
5061 if (item == this) -
5062 break; -
5063 if (qt_QGraphicsItem_isObscured(this, item, testRect)) -
5064 return true; -
5065 } -
5066 return false; -
5067} -
5068 -
5069/*! -
5070 \fn bool QGraphicsItem::isObscured(qreal x, qreal y, qreal w, qreal h) const -
5071 \overload -
5072 \since 4.3 -
5073 -
5074 This convenience function is equivalent to calling isObscured(QRectF(\a x, \a y, \a w, \a h)). -
5075*/ -
5076 -
5077/*! -
5078 Returns true if this item's bounding rect is completely obscured by the -
5079 opaque shape of \a item. -
5080 -
5081 The base implementation maps \a item's opaqueArea() to this item's -
5082 coordinate system, and then checks if this item's boundingRect() is fully -
5083 contained within the mapped shape. -
5084 -
5085 You can reimplement this function to provide a custom algorithm for -
5086 determining whether this item is obscured by \a item. -
5087 -
5088 \sa opaqueArea(), isObscured() -
5089*/ -
5090bool QGraphicsItem::isObscuredBy(const QGraphicsItem *item) const -
5091{ -
5092 if (!item) -
5093 return false; -
5094 return qt_closestItemFirst(item, this) -
5095 && qt_QGraphicsItem_isObscured(this, item, boundingRect()); -
5096} -
5097 -
5098/*! -
5099 This virtual function returns a shape representing the area where this -
5100 item is opaque. An area is opaque if it is filled using an opaque brush or -
5101 color (i.e., not transparent). -
5102 -
5103 This function is used by isObscuredBy(), which is called by underlying -
5104 items to determine if they are obscured by this item. -
5105 -
5106 The default implementation returns an empty QPainterPath, indicating that -
5107 this item is completely transparent and does not obscure any other items. -
5108 -
5109 \sa isObscuredBy(), isObscured(), shape() -
5110*/ -
5111QPainterPath QGraphicsItem::opaqueArea() const -
5112{ -
5113 return QPainterPath(); -
5114} -
5115 -
5116/*! -
5117 \since 4.4 -
5118 -
5119 Returns the bounding region for this item. The coordinate space of the -
5120 returned region depends on \a itemToDeviceTransform. If you pass an -
5121 identity QTransform as a parameter, this function will return a local -
5122 coordinate region. -
5123 -
5124 The bounding region describes a coarse outline of the item's visual -
5125 contents. Although it's expensive to calculate, it's also more precise -
5126 than boundingRect(), and it can help to avoid unnecessary repainting when -
5127 an item is updated. This is particularly efficient for thin items (e.g., -
5128 lines or simple polygons). You can tune the granularity for the bounding -
5129 region by calling setBoundingRegionGranularity(). The default granularity -
5130 is 0; in which the item's bounding region is the same as its bounding -
5131 rect. -
5132 -
5133 \a itemToDeviceTransform is the transformation from item coordinates to -
5134 device coordinates. If you want this function to return a QRegion in scene -
5135 coordinates, you can pass sceneTransform() as an argument. -
5136 -
5137 \sa boundingRegionGranularity() -
5138*/ -
5139QRegion QGraphicsItem::boundingRegion(const QTransform &itemToDeviceTransform) const -
5140{ -
5141 // ### Ideally we would have a better way to generate this region, -
5142 // preferably something in the lines of QPainterPath::toRegion(QTransform) -
5143 // coupled with a way to generate a painter path from a set of painter -
5144 // operations (e.g., QPicture::toPainterPath() or so). The current -
5145 // approach generates a bitmap with the size of the item's bounding rect -
5146 // in device coordinates, scaled by b.r.granularity, then paints the item -
5147 // into the bitmap, converts the result to a QRegion and scales the region -
5148 // back to device space with inverse granularity. -
5149 qreal granularity = boundingRegionGranularity(); -
5150 QRect deviceRect = itemToDeviceTransform.mapRect(boundingRect()).toRect(); -
5151 _q_adjustRect(&deviceRect); -
5152 if (granularity == 0.0) -
5153 return QRegion(deviceRect); -
5154 -
5155 int pad = 1; -
5156 QSize bitmapSize(qMax(1, int(deviceRect.width() * granularity) + pad * 2), -
5157 qMax(1, int(deviceRect.height() * granularity) + pad * 2)); -
5158 QImage mask(bitmapSize, QImage::Format_ARGB32_Premultiplied); -
5159 mask.fill(0); -
5160 QPainter p(&mask); -
5161 p.setRenderHints(QPainter::Antialiasing); -
5162 -
5163 // Transform painter (### this code is from QGraphicsScene::drawItemHelper -
5164 // and doesn't work properly with perspective transformations). -
5165 QPointF viewOrigo = itemToDeviceTransform.map(QPointF(0, 0)); -
5166 QPointF offset = viewOrigo - deviceRect.topLeft(); -
5167 p.scale(granularity, granularity); -
5168 p.translate(offset); -
5169 p.translate(pad, pad); -
5170 p.setWorldTransform(itemToDeviceTransform, true); -
5171 p.translate(itemToDeviceTransform.inverted().map(QPointF(0, 0))); -
5172 -
5173 // Render -
5174 QStyleOptionGraphicsItem option; -
5175 const_cast<QGraphicsItem *>(this)->paint(&p, &option, 0); -
5176 p.end(); -
5177 -
5178 // Transform QRegion back to device space -
5179 QTransform unscale = QTransform::fromScale(1 / granularity, 1 / granularity); -
5180 QRegion r; -
5181 QBitmap colorMask = QBitmap::fromImage(mask.createMaskFromColor(0)); -
5182 foreach (const QRect &rect, QRegion( colorMask ).rects()) { -
5183 QRect xrect = unscale.mapRect(rect).translated(deviceRect.topLeft() - QPoint(pad, pad)); -
5184 r += xrect.adjusted(-1, -1, 1, 1) & deviceRect; -
5185 } -
5186 return r; -
5187} -
5188 -
5189/*! -
5190 \since 4.4 -
5191 -
5192 Returns the item's bounding region granularity; a value between and -
5193 including 0 and 1. The default value is 0 (i.e., the lowest granularity, -
5194 where the bounding region corresponds to the item's bounding rectangle). -
5195 -
5196\omit -
5197### NOTE -
5198\endomit -
5199 -
5200 \sa setBoundingRegionGranularity() -
5201*/ -
5202qreal QGraphicsItem::boundingRegionGranularity() const -
5203{ -
5204 return d_ptr->hasBoundingRegionGranularity -
5205 ? qvariant_cast<qreal>(d_ptr->extra(QGraphicsItemPrivate::ExtraBoundingRegionGranularity)) -
5206 : 0; -
5207} -
5208 -
5209/*! -
5210 \since 4.4 -
5211 Sets the bounding region granularity to \a granularity; a value between -
5212 and including 0 and 1. The default value is 0 (i.e., the lowest -
5213 granularity, where the bounding region corresponds to the item's bounding -
5214 rectangle). -
5215 -
5216 The granularity is used by boundingRegion() to calculate how fine the -
5217 bounding region of the item should be. The highest achievable granularity -
5218 is 1, where boundingRegion() will return the finest outline possible for -
5219 the respective device (e.g., for a QGraphicsView viewport, this gives you -
5220 a pixel-perfect bounding region). The lowest possible granularity is -
5221 0. The value of \a granularity describes the ratio between device -
5222 resolution and the resolution of the bounding region (e.g., a value of -
5223 0.25 will provide a region where each chunk corresponds to 4x4 device -
5224 units / pixels). -
5225 -
5226 \sa boundingRegionGranularity() -
5227*/ -
5228void QGraphicsItem::setBoundingRegionGranularity(qreal granularity) -
5229{ -
5230 if (granularity < 0.0 || granularity > 1.0) { -
5231 qWarning("QGraphicsItem::setBoundingRegionGranularity: invalid granularity %g", granularity); -
5232 return; -
5233 } -
5234 if (granularity == 0.0) { -
5235 d_ptr->unsetExtra(QGraphicsItemPrivate::ExtraBoundingRegionGranularity); -
5236 d_ptr->hasBoundingRegionGranularity = 0; -
5237 return; -
5238 } -
5239 d_ptr->hasBoundingRegionGranularity = 1; -
5240 d_ptr->setExtra(QGraphicsItemPrivate::ExtraBoundingRegionGranularity, -
5241 QVariant::fromValue<qreal>(granularity)); -
5242} -
5243 -
5244/*! -
5245 \fn virtual void QGraphicsItem::paint(QPainter *painter, const -
5246 QStyleOptionGraphicsItem *option, QWidget *widget = 0) = 0 -
5247 -
5248 This function, which is usually called by QGraphicsView, paints the -
5249 contents of an item in local coordinates. -
5250 -
5251 Reimplement this function in a QGraphicsItem subclass to provide the -
5252 item's painting implementation, using \a painter. The \a option parameter -
5253 provides style options for the item, such as its state, exposed area and -
5254 its level-of-detail hints. The \a widget argument is optional. If -
5255 provided, it points to the widget that is being painted on; otherwise, it -
5256 is 0. For cached painting, \a widget is always 0. -
5257 -
5258 \snippet code/src_gui_graphicsview_qgraphicsitem.cpp 10 -
5259 -
5260 The painter's pen is 0-width by default, and its pen is initialized to the -
5261 QPalette::Text brush from the paint device's palette. The brush is -
5262 initialized to QPalette::Window. -
5263 -
5264 Make sure to constrain all painting inside the boundaries of -
5265 boundingRect() to avoid rendering artifacts (as QGraphicsView does not -
5266 clip the painter for you). In particular, when QPainter renders the -
5267 outline of a shape using an assigned QPen, half of the outline will be -
5268 drawn outside, and half inside, the shape you're rendering (e.g., with a -
5269 pen width of 2 units, you must draw outlines 1 unit inside -
5270 boundingRect()). QGraphicsItem does not support use of cosmetic pens with -
5271 a non-zero width. -
5272 -
5273 All painting is done in local coordinates. -
5274 -
5275 \sa setCacheMode(), QPen::width(), {Item Coordinates}, ItemUsesExtendedStyleOption -
5276*/ -
5277 -
5278/*! -
5279 \internal -
5280 Returns true if we can discard an update request; otherwise false. -
5281*/ -
5282bool QGraphicsItemPrivate::discardUpdateRequest(bool ignoreVisibleBit, bool ignoreDirtyBit, -
5283 bool ignoreOpacity) const -
5284{ -
5285 // No scene, or if the scene is updating everything, means we have nothing -
5286 // to do. The only exception is if the scene tracks the growing scene rect. -
5287 return !scene -
5288 || (!visible && !ignoreVisibleBit && !this->ignoreVisible) -
5289 || (!ignoreDirtyBit && fullUpdatePending) -
5290 || (!ignoreOpacity && !this->ignoreOpacity && childrenCombineOpacity() && isFullyTransparent()); -
5291} -
5292 -
5293/*! -
5294 \internal -
5295*/ -
5296int QGraphicsItemPrivate::depth() const -
5297{ -
5298 if (itemDepth == -1) -
5299 const_cast<QGraphicsItemPrivate *>(this)->resolveDepth(); -
5300 -
5301 return itemDepth; -
5302} -
5303 -
5304/*! -
5305 \internal -
5306*/ -
5307#ifndef QT_NO_GRAPHICSEFFECT -
5308void QGraphicsItemPrivate::invalidateParentGraphicsEffectsRecursively() -
5309{ -
5310 QGraphicsItemPrivate *itemPrivate = this; -
5311 do { -
5312 if (itemPrivate->graphicsEffect) { -
5313 itemPrivate->notifyInvalidated = 1; -
5314 -
5315 if (!itemPrivate->updateDueToGraphicsEffect) -
5316 static_cast<QGraphicsItemEffectSourcePrivate *>(itemPrivate->graphicsEffect->d_func()->source->d_func())->invalidateCache(); -
5317 } -
5318 } while ((itemPrivate = itemPrivate->parent ? itemPrivate->parent->d_ptr.data() : 0)); -
5319} -
5320 -
5321void QGraphicsItemPrivate::invalidateChildGraphicsEffectsRecursively(QGraphicsItemPrivate::InvalidateReason reason) -
5322{ -
5323 if (!mayHaveChildWithGraphicsEffect) -
5324 return; -
5325 -
5326 for (int i = 0; i < children.size(); ++i) { -
5327 QGraphicsItemPrivate *childPrivate = children.at(i)->d_ptr.data(); -
5328 if (reason == OpacityChanged && (childPrivate->flags & QGraphicsItem::ItemIgnoresParentOpacity)) -
5329 continue; -
5330 if (childPrivate->graphicsEffect) { -
5331 childPrivate->notifyInvalidated = 1; -
5332 static_cast<QGraphicsItemEffectSourcePrivate *>(childPrivate->graphicsEffect->d_func()->source->d_func())->invalidateCache(); -
5333 } -
5334 -
5335 childPrivate->invalidateChildGraphicsEffectsRecursively(reason); -
5336 } -
5337} -
5338#endif //QT_NO_GRAPHICSEFFECT -
5339 -
5340/*! -
5341 \internal -
5342*/ -
5343void QGraphicsItemPrivate::invalidateDepthRecursively() -
5344{ -
5345 if (itemDepth == -1) -
5346 return; -
5347 -
5348 itemDepth = -1; -
5349 for (int i = 0; i < children.size(); ++i) -
5350 children.at(i)->d_ptr->invalidateDepthRecursively(); -
5351} -
5352 -
5353/*! -
5354 \internal -
5355 -
5356 Resolves the stacking depth of this object and all its ancestors. -
5357*/ -
5358void QGraphicsItemPrivate::resolveDepth() -
5359{ -
5360 if (!parent) -
5361 itemDepth = 0; -
5362 else { -
5363 if (parent->d_ptr->itemDepth == -1) -
5364 parent->d_ptr->resolveDepth(); -
5365 itemDepth = parent->d_ptr->itemDepth + 1; -
5366 } -
5367} -
5368 -
5369/*! -
5370 \internal -
5371 -
5372 ### This function is almost identical to -
5373 QGraphicsScenePrivate::registerTopLevelItem(). -
5374*/ -
5375void QGraphicsItemPrivate::addChild(QGraphicsItem *child) -
5376{ -
5377 // Remove all holes from the sibling index list. Now the max index -
5378 // number is equal to the size of the children list. -
5379 ensureSequentialSiblingIndex(); -
5380 needSortChildren = 1; // ### maybe 0 -
5381 child->d_ptr->siblingIndex = children.size(); -
5382 children.append(child); -
5383 if (isObject) -
5384 emit static_cast<QGraphicsObject *>(q_ptr)->childrenChanged(); -
5385} -
5386 -
5387/*! -
5388 \internal -
5389 -
5390 ### This function is almost identical to -
5391 QGraphicsScenePrivate::unregisterTopLevelItem(). -
5392*/ -
5393void QGraphicsItemPrivate::removeChild(QGraphicsItem *child) -
5394{ -
5395 // When removing elements in the middle of the children list, -
5396 // there will be a "gap" in the list of sibling indexes (0,1,3,4). -
5397 if (!holesInSiblingIndex) -
5398 holesInSiblingIndex = child->d_ptr->siblingIndex != children.size() - 1; -
5399 if (sequentialOrdering && !holesInSiblingIndex) -
5400 children.removeAt(child->d_ptr->siblingIndex); -
5401 else -
5402 children.removeOne(child); -
5403 // NB! Do not use children.removeAt(child->d_ptr->siblingIndex) because -
5404 // the child is not guaranteed to be at the index after the list is sorted. -
5405 // (see ensureSortedChildren()). -
5406 child->d_ptr->siblingIndex = -1; -
5407 if (isObject) -
5408 emit static_cast<QGraphicsObject *>(q_ptr)->childrenChanged(); -
5409} -
5410 -
5411/*! -
5412 \internal -
5413*/ -
5414QGraphicsItemCache *QGraphicsItemPrivate::maybeExtraItemCache() const -
5415{ -
5416 return (QGraphicsItemCache *)qvariant_cast<void *>(extra(ExtraCacheData)); -
5417} -
5418 -
5419/*! -
5420 \internal -
5421*/ -
5422QGraphicsItemCache *QGraphicsItemPrivate::extraItemCache() const -
5423{ -
5424 QGraphicsItemCache *c = (QGraphicsItemCache *)qvariant_cast<void *>(extra(ExtraCacheData)); -
5425 if (!c) { -
5426 QGraphicsItemPrivate *that = const_cast<QGraphicsItemPrivate *>(this); -
5427 c = new QGraphicsItemCache; -
5428 that->setExtra(ExtraCacheData, QVariant::fromValue<void *>(c)); -
5429 } -
5430 return c; -
5431} -
5432 -
5433/*! -
5434 \internal -
5435*/ -
5436void QGraphicsItemPrivate::removeExtraItemCache() -
5437{ -
5438 QGraphicsItemCache *c = (QGraphicsItemCache *)qvariant_cast<void *>(extra(ExtraCacheData)); -
5439 if (c) { -
5440 c->purge(); -
5441 delete c; -
5442 } -
5443 unsetExtra(ExtraCacheData); -
5444} -
5445 -
5446void QGraphicsItemPrivate::updatePaintedViewBoundingRects(bool updateChildren) -
5447{ -
5448 if (!scene) -
5449 return; -
5450 -
5451 for (int i = 0; i < scene->d_func()->views.size(); ++i) { -
5452 QGraphicsViewPrivate *viewPrivate = scene->d_func()->views.at(i)->d_func(); -
5453 QRect rect = paintedViewBoundingRects.value(viewPrivate->viewport); -
5454 rect.translate(viewPrivate->dirtyScrollOffset); -
5455 viewPrivate->updateRect(rect); -
5456 } -
5457 -
5458 if (updateChildren) { -
5459 for (int i = 0; i < children.size(); ++i) -
5460 children.at(i)->d_ptr->updatePaintedViewBoundingRects(true); -
5461 } -
5462} -
5463 -
5464// Traverses all the ancestors up to the top-level and updates the pointer to -
5465// always point to the top-most item that has a dirty scene transform. -
5466// It then backtracks to the top-most dirty item and start calculating the -
5467// scene transform by combining the item's transform (+pos) with the parent's -
5468// cached scene transform (which we at this point know for sure is valid). -
5469void QGraphicsItemPrivate::ensureSceneTransformRecursive(QGraphicsItem **topMostDirtyItem) -
5470{ -
5471 Q_ASSERT(topMostDirtyItem); -
5472 -
5473 if (dirtySceneTransform) -
5474 *topMostDirtyItem = q_ptr; -
5475 -
5476 if (parent) -
5477 parent->d_ptr->ensureSceneTransformRecursive(topMostDirtyItem); -
5478 -
5479 if (*topMostDirtyItem == q_ptr) { -
5480 if (!dirtySceneTransform) -
5481 return; // OK, neither my ancestors nor I have dirty scene transforms. -
5482 *topMostDirtyItem = 0; -
5483 } else if (*topMostDirtyItem) { -
5484 return; // Continue backtrack. -
5485 } -
5486 -
5487 // This item and all its descendants have dirty scene transforms. -
5488 // We're about to validate this item's scene transform, so we have to -
5489 // invalidate all the children; otherwise there's no way for the descendants -
5490 // to detect that the ancestor has changed. -
5491 invalidateChildrenSceneTransform(); -
5492 -
5493 // COMBINE my transform with the parent's scene transform. -
5494 updateSceneTransformFromParent(); -
5495 Q_ASSERT(!dirtySceneTransform); -
5496} -
5497 -
5498/*! -
5499 \internal -
5500*/ -
5501void QGraphicsItemPrivate::setSubFocus(QGraphicsItem *rootItem, QGraphicsItem *stopItem) -
5502{ -
5503 // Update focus child chain. Stop at panels, or if this item -
5504 // is hidden, stop at the first item with a visible parent. -
5505 QGraphicsItem *parent = rootItem ? rootItem : q_ptr; -
5506 if (parent->panel() != q_ptr->panel()) -
5507 return; -
5508 -
5509 do { -
5510 // Clear any existing ancestor's subFocusItem. -
5511 if (parent != q_ptr && parent->d_ptr->subFocusItem) { -
5512 if (parent->d_ptr->subFocusItem == q_ptr) -
5513 break; -
5514 parent->d_ptr->subFocusItem->d_ptr->clearSubFocus(0, stopItem); -
5515 } -
5516 parent->d_ptr->subFocusItem = q_ptr; -
5517 parent->d_ptr->subFocusItemChange(); -
5518 } while (!parent->isPanel() && (parent = parent->d_ptr->parent) && (visible || !parent->d_ptr->visible)); -
5519 -
5520 if (scene && !scene->isActive()) { -
5521 scene->d_func()->passiveFocusItem = subFocusItem; -
5522 scene->d_func()->lastFocusItem = subFocusItem; -
5523 } -
5524} -
5525 -
5526/*! -
5527 \internal -
5528*/ -
5529void QGraphicsItemPrivate::clearSubFocus(QGraphicsItem *rootItem, QGraphicsItem *stopItem) -
5530{ -
5531 // Reset sub focus chain. -
5532 QGraphicsItem *parent = rootItem ? rootItem : q_ptr; -
5533 do { -
5534 if (parent->d_ptr->subFocusItem != q_ptr) -
5535 break; -
5536 parent->d_ptr->subFocusItem = 0; -
5537 if (parent != stopItem && !parent->isAncestorOf(stopItem)) -
5538 parent->d_ptr->subFocusItemChange(); -
5539 } while (!parent->isPanel() && (parent = parent->d_ptr->parent)); -
5540} -
5541 -
5542/*! -
5543 \internal -
5544 -
5545 Sets the focusProxy pointer to 0 for all items that have this item as their -
5546 focusProxy. -
5547*/ -
5548void QGraphicsItemPrivate::resetFocusProxy() -
5549{ -
5550 for (int i = 0; i < focusProxyRefs.size(); ++i) -
5551 *focusProxyRefs.at(i) = 0; -
5552 focusProxyRefs.clear(); -
5553} -
5554 -
5555/*! -
5556 \internal -
5557 -
5558 Subclasses can reimplement this function to be notified when subFocusItem -
5559 changes. -
5560*/ -
5561void QGraphicsItemPrivate::subFocusItemChange() -
5562{ -
5563} -
5564 -
5565/*! -
5566 \internal -
5567 -
5568 Subclasses can reimplement this function to be notified when an item -
5569 becomes a focusScopeItem (or is no longer a focusScopeItem). -
5570*/ -
5571void QGraphicsItemPrivate::focusScopeItemChange(bool isSubFocusItem) -
5572{ -
5573 Q_UNUSED(isSubFocusItem); -
5574} -
5575 -
5576/*! -
5577 \internal -
5578 -
5579 Subclasses can reimplement this function to be notified when its -
5580 siblingIndex order is changed. -
5581*/ -
5582void QGraphicsItemPrivate::siblingOrderChange() -
5583{ -
5584} -
5585 -
5586/*! -
5587 \internal -
5588 -
5589 Tells us if it is a proxy widget -
5590*/ -
5591bool QGraphicsItemPrivate::isProxyWidget() const -
5592{ -
5593 return false; -
5594} -
5595 -
5596/*! -
5597 Schedules a redraw of the area covered by \a rect in this item. You can -
5598 call this function whenever your item needs to be redrawn, such as if it -
5599 changes appearance or size. -
5600 -
5601 This function does not cause an immediate paint; instead it schedules a -
5602 paint request that is processed by QGraphicsView after control reaches the -
5603 event loop. The item will only be redrawn if it is visible in any -
5604 associated view. -
5605 -
5606 As a side effect of the item being repainted, other items that overlap the -
5607 area \a rect may also be repainted. -
5608 -
5609 If the item is invisible (i.e., isVisible() returns false), this function -
5610 does nothing. -
5611 -
5612 \sa paint(), boundingRect() -
5613*/ -
5614void QGraphicsItem::update(const QRectF &rect) -
5615{ -
5616 if (rect.isEmpty() && !rect.isNull()) -
5617 return; -
5618 -
5619 // Make sure we notify effects about invalidated source. -
5620#ifndef QT_NO_GRAPHICSEFFECT -
5621 d_ptr->invalidateParentGraphicsEffectsRecursively(); -
5622#endif //QT_NO_GRAPHICSEFFECT -
5623 -
5624 if (CacheMode(d_ptr->cacheMode) != NoCache) { -
5625 // Invalidate cache. -
5626 QGraphicsItemCache *cache = d_ptr->extraItemCache(); -
5627 if (!cache->allExposed) { -
5628 if (rect.isNull()) { -
5629 cache->allExposed = true; -
5630 cache->exposed.clear(); -
5631 } else { -
5632 cache->exposed.append(rect); -
5633 } -
5634 } -
5635 // Only invalidate cache; item is already dirty. -
5636 if (d_ptr->fullUpdatePending) -
5637 return; -
5638 } -
5639 -
5640 if (d_ptr->scene) -
5641 d_ptr->scene->d_func()->markDirty(this, rect); -
5642} -
5643 -
5644/*! -
5645 \since 4.4 -
5646 Scrolls the contents of \a rect by \a dx, \a dy. If \a rect is a null rect -
5647 (the default), the item's bounding rect is scrolled. -
5648 -
5649 Scrolling provides a fast alternative to simply redrawing when the -
5650 contents of the item (or parts of the item) are shifted vertically or -
5651 horizontally. Depending on the current transformation and the capabilities -
5652 of the paint device (i.e., the viewport), this operation may consist of -
5653 simply moving pixels from one location to another using memmove(). In most -
5654 cases this is faster than rerendering the entire area. -
5655 -
5656 After scrolling, the item will issue an update for the newly exposed -
5657 areas. If scrolling is not supported (e.g., you are rendering to an OpenGL -
5658 viewport, which does not benefit from scroll optimizations), this function -
5659 is equivalent to calling update(\a rect). -
5660 -
5661 \b{Note:} Scrolling is only supported when QGraphicsItem::ItemCoordinateCache -
5662 is enabled; in all other cases calling this function is equivalent to calling -
5663 update(\a rect). If you for sure know that the item is opaque and not overlapped -
5664 by other items, you can map the \a rect to viewport coordinates and scroll the -
5665 viewport. -
5666 -
5667 \snippet code/src_gui_graphicsview_qgraphicsitem.cpp 19 -
5668 -
5669 \sa boundingRect() -
5670*/ -
5671void QGraphicsItem::scroll(qreal dx, qreal dy, const QRectF &rect) -
5672{ -
5673 Q_D(QGraphicsItem); -
5674 if (dx == 0.0 && dy == 0.0) -
5675 return; -
5676 if (!d->scene) -
5677 return; -
5678 -
5679 // Accelerated scrolling means moving pixels from one location to another -
5680 // and only redraw the newly exposed area. The following requirements must -
5681 // be fulfilled in order to do that: -
5682 // -
5683 // 1) Item is opaque. -
5684 // 2) Item is not overlapped by other items. -
5685 // -
5686 // There's (yet) no way to detect whether an item is opaque or not, which means -
5687 // we cannot do accelerated scrolling unless the cache is enabled. In case of using -
5688 // DeviceCoordinate cache we also have to take the device transform into account in -
5689 // order to determine whether we can do accelerated scrolling or not. That's left out -
5690 // for simplicity here, but it is definitely something we can consider in the future -
5691 // as a performance improvement. -
5692 if (d->cacheMode != QGraphicsItem::ItemCoordinateCache -
5693 || !qFuzzyIsNull(dx - int(dx)) || !qFuzzyIsNull(dy - int(dy))) { -
5694 update(rect); -
5695 return; -
5696 } -
5697 -
5698 QGraphicsItemCache *cache = d->extraItemCache(); -
5699 if (cache->allExposed || cache->fixedSize.isValid()) { -
5700 // Cache is either invalidated or item is scaled (see QGraphicsItem::setCacheMode). -
5701 update(rect); -
5702 return; -
5703 } -
5704 -
5705 // Find pixmap in cache. -
5706 QPixmap cachedPixmap; -
5707 if (!QPixmapCache::find(cache->key, &cachedPixmap)) { -
5708 update(rect); -
5709 return; -
5710 } -
5711 -
5712 QRect scrollRect = (rect.isNull() ? boundingRect() : rect).toAlignedRect(); -
5713 if (!scrollRect.intersects(cache->boundingRect)) -
5714 return; // Nothing to scroll. -
5715 -
5716 // Remove from cache to avoid deep copy when modifying. -
5717 QPixmapCache::remove(cache->key); -
5718 -
5719 QRegion exposed; -
5720 cachedPixmap.scroll(dx, dy, scrollRect.translated(-cache->boundingRect.topLeft()), &exposed); -
5721 -
5722 // Reinsert into cache. -
5723 cache->key = QPixmapCache::insert(cachedPixmap); -
5724 -
5725 // Translate the existing expose. -
5726 for (int i = 0; i < cache->exposed.size(); ++i) { -
5727 QRectF &e = cache->exposed[i]; -
5728 if (!rect.isNull() && !e.intersects(rect)) -
5729 continue; -
5730 e.translate(dx, dy); -
5731 } -
5732 -
5733 // Append newly exposed areas. Note that the exposed region is currently -
5734 // in pixmap coordinates, so we have to translate it to item coordinates. -
5735 exposed.translate(cache->boundingRect.topLeft()); -
5736 const QVector<QRect> exposedRects = exposed.rects(); -
5737 for (int i = 0; i < exposedRects.size(); ++i) -
5738 cache->exposed += exposedRects.at(i); -
5739 -
5740 // Trigger update. This will redraw the newly exposed area and make sure -
5741 // the pixmap is re-blitted in case there are overlapping items. -
5742 d->scene->d_func()->markDirty(this, rect); -
5743} -
5744 -
5745/*! -
5746 \fn void QGraphicsItem::update(qreal x, qreal y, qreal width, qreal height) -
5747 \overload -
5748 -
5749 This convenience function is equivalent to calling update(QRectF(\a x, \a -
5750 y, \a width, \a height)). -
5751*/ -
5752 -
5753/*! -
5754 Maps the point \a point, which is in this item's coordinate system, to \a -
5755 item's coordinate system, and returns the mapped coordinate. -
5756 -
5757 If \a item is 0, this function returns the same as mapToScene(). -
5758 -
5759 \sa itemTransform(), mapToParent(), mapToScene(), transform(), mapFromItem(), {The Graphics -
5760 View Coordinate System} -
5761*/ -
5762QPointF QGraphicsItem::mapToItem(const QGraphicsItem *item, const QPointF &point) const -
5763{ -
5764 if (item) -
5765 return itemTransform(item).map(point); -
5766 return mapToScene(point); -
5767} -
5768 -
5769/*! -
5770 \fn QPointF QGraphicsItem::mapToItem(const QGraphicsItem *item, qreal x, qreal y) const -
5771 \overload -
5772 -
5773 This convenience function is equivalent to calling mapToItem(\a item, -
5774 QPointF(\a x, \a y)). -
5775*/ -
5776 -
5777/*! -
5778 Maps the point \a point, which is in this item's coordinate system, to its -
5779 parent's coordinate system, and returns the mapped coordinate. If the item -
5780 has no parent, \a point will be mapped to the scene's coordinate system. -
5781 -
5782 \sa mapToItem(), mapToScene(), transform(), mapFromParent(), {The Graphics -
5783 View Coordinate System} -
5784*/ -
5785QPointF QGraphicsItem::mapToParent(const QPointF &point) const -
5786{ -
5787 // COMBINE -
5788 if (!d_ptr->transformData) -
5789 return point + d_ptr->pos; -
5790 return d_ptr->transformToParent().map(point); -
5791} -
5792 -
5793/*! -
5794 \fn QPointF QGraphicsItem::mapToParent(qreal x, qreal y) const -
5795 \overload -
5796 -
5797 This convenience function is equivalent to calling mapToParent(QPointF(\a -
5798 x, \a y)). -
5799*/ -
5800 -
5801/*! -
5802 Maps the point \a point, which is in this item's coordinate system, to the -
5803 scene's coordinate system, and returns the mapped coordinate. -
5804 -
5805 \sa mapToItem(), mapToParent(), transform(), mapFromScene(), {The Graphics -
5806 View Coordinate System} -
5807*/ -
5808QPointF QGraphicsItem::mapToScene(const QPointF &point) const -
5809{ -
5810 if (d_ptr->hasTranslateOnlySceneTransform()) -
5811 return QPointF(point.x() + d_ptr->sceneTransform.dx(), point.y() + d_ptr->sceneTransform.dy()); -
5812 return d_ptr->sceneTransform.map(point); -
5813} -
5814 -
5815/*! -
5816 \fn QPointF QGraphicsItem::mapToScene(qreal x, qreal y) const -
5817 \overload -
5818 -
5819 This convenience function is equivalent to calling mapToScene(QPointF(\a -
5820 x, \a y)). -
5821*/ -
5822 -
5823/*! -
5824 Maps the rectangle \a rect, which is in this item's coordinate system, to -
5825 \a item's coordinate system, and returns the mapped rectangle as a polygon. -
5826 -
5827 If \a item is 0, this function returns the same as mapToScene(). -
5828 -
5829 \sa itemTransform(), mapToParent(), mapToScene(), mapFromItem(), {The -
5830 Graphics View Coordinate System} -
5831*/ -
5832QPolygonF QGraphicsItem::mapToItem(const QGraphicsItem *item, const QRectF &rect) const -
5833{ -
5834 if (item) -
5835 return itemTransform(item).map(rect); -
5836 return mapToScene(rect); -
5837} -
5838 -
5839/*! -
5840 \fn QPolygonF QGraphicsItem::mapToItem(const QGraphicsItem *item, qreal x, qreal y, qreal w, qreal h) const -
5841 \since 4.3 -
5842 -
5843 This convenience function is equivalent to calling mapToItem(item, QRectF(\a x, \a y, \a w, \a h)). -
5844*/ -
5845 -
5846/*! -
5847 Maps the rectangle \a rect, which is in this item's coordinate system, to -
5848 its parent's coordinate system, and returns the mapped rectangle as a -
5849 polygon. If the item has no parent, \a rect will be mapped to the scene's -
5850 coordinate system. -
5851 -
5852 \sa mapToScene(), mapToItem(), mapFromParent(), {The Graphics View -
5853 Coordinate System} -
5854*/ -
5855QPolygonF QGraphicsItem::mapToParent(const QRectF &rect) const -
5856{ -
5857 // COMBINE -
5858 if (!d_ptr->transformData) -
5859 return rect.translated(d_ptr->pos); -
5860 return d_ptr->transformToParent().map(rect); -
5861} -
5862 -
5863/*! -
5864 \fn QPolygonF QGraphicsItem::mapToParent(qreal x, qreal y, qreal w, qreal h) const -
5865 \since 4.3 -
5866 -
5867 This convenience function is equivalent to calling mapToParent(QRectF(\a x, \a y, \a w, \a h)). -
5868*/ -
5869 -
5870/*! -
5871 Maps the rectangle \a rect, which is in this item's coordinate system, to -
5872 the scene's coordinate system, and returns the mapped rectangle as a polygon. -
5873 -
5874 \sa mapToParent(), mapToItem(), mapFromScene(), {The Graphics View -
5875 Coordinate System} -
5876*/ -
5877QPolygonF QGraphicsItem::mapToScene(const QRectF &rect) const -
5878{ -
5879 if (d_ptr->hasTranslateOnlySceneTransform()) -
5880 return rect.translated(d_ptr->sceneTransform.dx(), d_ptr->sceneTransform.dy()); -
5881 return d_ptr->sceneTransform.map(rect); -
5882} -
5883 -
5884/*! -
5885 \fn QPolygonF QGraphicsItem::mapToScene(qreal x, qreal y, qreal w, qreal h) const -
5886 \since 4.3 -
5887 -
5888 This convenience function is equivalent to calling mapToScene(QRectF(\a x, \a y, \a w, \a h)). -
5889*/ -
5890 -
5891/*! -
5892 \since 4.5 -
5893 -
5894 Maps the rectangle \a rect, which is in this item's coordinate system, to -
5895 \a item's coordinate system, and returns the mapped rectangle as a new -
5896 rectangle (i.e., the bounding rectangle of the resulting polygon). -
5897 -
5898 If \a item is 0, this function returns the same as mapRectToScene(). -
5899 -
5900 \sa itemTransform(), mapToParent(), mapToScene(), mapFromItem(), {The -
5901 Graphics View Coordinate System} -
5902*/ -
5903QRectF QGraphicsItem::mapRectToItem(const QGraphicsItem *item, const QRectF &rect) const -
5904{ -
5905 if (item) -
5906 return itemTransform(item).mapRect(rect); -
5907 return mapRectToScene(rect); -
5908} -
5909 -
5910/*! -
5911 \fn QRectF QGraphicsItem::mapRectToItem(const QGraphicsItem *item, qreal x, qreal y, qreal w, qreal h) const -
5912 \since 4.5 -
5913 -
5914 This convenience function is equivalent to calling mapRectToItem(item, QRectF(\a x, \a y, \a w, \a h)). -
5915*/ -
5916 -
5917/*! -
5918 \since 4.5 -
5919 -
5920 Maps the rectangle \a rect, which is in this item's coordinate system, to -
5921 its parent's coordinate system, and returns the mapped rectangle as a new -
5922 rectangle (i.e., the bounding rectangle of the resulting polygon). -
5923 -
5924 \sa itemTransform(), mapToParent(), mapToScene(), mapFromItem(), {The -
5925 Graphics View Coordinate System} -
5926*/ -
5927QRectF QGraphicsItem::mapRectToParent(const QRectF &rect) const -
5928{ -
5929 // COMBINE -
5930 if (!d_ptr->transformData) -
5931 return rect.translated(d_ptr->pos); -
5932 return d_ptr->transformToParent().mapRect(rect); -
5933} -
5934 -
5935/*! -
5936 \fn QRectF QGraphicsItem::mapRectToParent(qreal x, qreal y, qreal w, qreal h) const -
5937 \since 4.5 -
5938 -
5939 This convenience function is equivalent to calling mapRectToParent(QRectF(\a x, \a y, \a w, \a h)). -
5940*/ -
5941 -
5942/*! -
5943 \since 4.5 -
5944 -
5945 Maps the rectangle \a rect, which is in this item's coordinate system, to -
5946 the scene coordinate system, and returns the mapped rectangle as a new -
5947 rectangle (i.e., the bounding rectangle of the resulting polygon). -
5948 -
5949 \sa itemTransform(), mapToParent(), mapToScene(), mapFromItem(), {The -
5950 Graphics View Coordinate System} -
5951*/ -
5952QRectF QGraphicsItem::mapRectToScene(const QRectF &rect) const -
5953{ -
5954 if (d_ptr->hasTranslateOnlySceneTransform()) -
5955 return rect.translated(d_ptr->sceneTransform.dx(), d_ptr->sceneTransform.dy()); -
5956 return d_ptr->sceneTransform.mapRect(rect); -
5957} -
5958 -
5959/*! -
5960 \fn QRectF QGraphicsItem::mapRectToScene(qreal x, qreal y, qreal w, qreal h) const -
5961 \since 4.5 -
5962 -
5963 This convenience function is equivalent to calling mapRectToScene(QRectF(\a x, \a y, \a w, \a h)). -
5964*/ -
5965 -
5966/*! -
5967 \since 4.5 -
5968 -
5969 Maps the rectangle \a rect, which is in \a item's coordinate system, to -
5970 this item's coordinate system, and returns the mapped rectangle as a new -
5971 rectangle (i.e., the bounding rectangle of the resulting polygon). -
5972 -
5973 If \a item is 0, this function returns the same as mapRectFromScene(). -
5974 -
5975 \sa itemTransform(), mapToParent(), mapToScene(), mapFromItem(), {The -
5976 Graphics View Coordinate System} -
5977*/ -
5978QRectF QGraphicsItem::mapRectFromItem(const QGraphicsItem *item, const QRectF &rect) const -
5979{ -
5980 if (item) -
5981 return item->itemTransform(this).mapRect(rect); -
5982 return mapRectFromScene(rect); -
5983} -
5984 -
5985/*! -
5986 \fn QRectF QGraphicsItem::mapRectFromItem(const QGraphicsItem *item, qreal x, qreal y, qreal w, qreal h) const -
5987 \since 4.5 -
5988 -
5989 This convenience function is equivalent to calling mapRectFromItem(item, QRectF(\a x, \a y, \a w, \a h)). -
5990*/ -
5991 -
5992/*! -
5993 \since 4.5 -
5994 -
5995 Maps the rectangle \a rect, which is in this item's parent's coordinate -
5996 system, to this item's coordinate system, and returns the mapped rectangle -
5997 as a new rectangle (i.e., the bounding rectangle of the resulting -
5998 polygon). -
5999 -
6000 \sa itemTransform(), mapToParent(), mapToScene(), mapFromItem(), {The -
6001 Graphics View Coordinate System} -
6002*/ -
6003QRectF QGraphicsItem::mapRectFromParent(const QRectF &rect) const -
6004{ -
6005 // COMBINE -
6006 if (!d_ptr->transformData) -
6007 return rect.translated(-d_ptr->pos); -
6008 return d_ptr->transformToParent().inverted().mapRect(rect); -
6009} -
6010 -
6011/*! -
6012 \fn QRectF QGraphicsItem::mapRectFromParent(qreal x, qreal y, qreal w, qreal h) const -
6013 \since 4.5 -
6014 -
6015 This convenience function is equivalent to calling mapRectFromParent(QRectF(\a x, \a y, \a w, \a h)). -
6016*/ -
6017 -
6018/*! -
6019 \since 4.5 -
6020 -
6021 Maps the rectangle \a rect, which is in scene coordinates, to this item's -
6022 coordinate system, and returns the mapped rectangle as a new rectangle -
6023 (i.e., the bounding rectangle of the resulting polygon). -
6024 -
6025 \sa itemTransform(), mapToParent(), mapToScene(), mapFromItem(), {The -
6026 Graphics View Coordinate System} -
6027*/ -
6028QRectF QGraphicsItem::mapRectFromScene(const QRectF &rect) const -
6029{ -
6030 if (d_ptr->hasTranslateOnlySceneTransform()) -
6031 return rect.translated(-d_ptr->sceneTransform.dx(), -d_ptr->sceneTransform.dy()); -
6032 return d_ptr->sceneTransform.inverted().mapRect(rect); -
6033} -
6034 -
6035/*! -
6036 \fn QRectF QGraphicsItem::mapRectFromScene(qreal x, qreal y, qreal w, qreal h) const -
6037 \since 4.5 -
6038 -
6039 This convenience function is equivalent to calling mapRectFromScene(QRectF(\a x, \a y, \a w, \a h)). -
6040*/ -
6041 -
6042/*! -
6043 Maps the polygon \a polygon, which is in this item's coordinate system, to -
6044 \a item's coordinate system, and returns the mapped polygon. -
6045 -
6046 If \a item is 0, this function returns the same as mapToScene(). -
6047 -
6048 \sa itemTransform(), mapToParent(), mapToScene(), mapFromItem(), {The -
6049 Graphics View Coordinate System} -
6050*/ -
6051QPolygonF QGraphicsItem::mapToItem(const QGraphicsItem *item, const QPolygonF &polygon) const -
6052{ -
6053 if (item) -
6054 return itemTransform(item).map(polygon); -
6055 return mapToScene(polygon); -
6056} -
6057 -
6058/*! -
6059 Maps the polygon \a polygon, which is in this item's coordinate system, to -
6060 its parent's coordinate system, and returns the mapped polygon. If the -
6061 item has no parent, \a polygon will be mapped to the scene's coordinate -
6062 system. -
6063 -
6064 \sa mapToScene(), mapToItem(), mapFromParent(), {The Graphics View -
6065 Coordinate System} -
6066*/ -
6067QPolygonF QGraphicsItem::mapToParent(const QPolygonF &polygon) const -
6068{ -
6069 // COMBINE -
6070 if (!d_ptr->transformData) -
6071 return polygon.translated(d_ptr->pos); -
6072 return d_ptr->transformToParent().map(polygon); -
6073} -
6074 -
6075/*! -
6076 Maps the polygon \a polygon, which is in this item's coordinate system, to -
6077 the scene's coordinate system, and returns the mapped polygon. -
6078 -
6079 \sa mapToParent(), mapToItem(), mapFromScene(), {The Graphics View -
6080 Coordinate System} -
6081*/ -
6082QPolygonF QGraphicsItem::mapToScene(const QPolygonF &polygon) const -
6083{ -
6084 if (d_ptr->hasTranslateOnlySceneTransform()) -
6085 return polygon.translated(d_ptr->sceneTransform.dx(), d_ptr->sceneTransform.dy()); -
6086 return d_ptr->sceneTransform.map(polygon); -
6087} -
6088 -
6089/*! -
6090 Maps the path \a path, which is in this item's coordinate system, to -
6091 \a item's coordinate system, and returns the mapped path. -
6092 -
6093 If \a item is 0, this function returns the same as mapToScene(). -
6094 -
6095 \sa itemTransform(), mapToParent(), mapToScene(), mapFromItem(), {The -
6096 Graphics View Coordinate System} -
6097*/ -
6098QPainterPath QGraphicsItem::mapToItem(const QGraphicsItem *item, const QPainterPath &path) const -
6099{ -
6100 if (item) -
6101 return itemTransform(item).map(path); -
6102 return mapToScene(path); -
6103} -
6104 -
6105/*! -
6106 Maps the path \a path, which is in this item's coordinate system, to -
6107 its parent's coordinate system, and returns the mapped path. If the -
6108 item has no parent, \a path will be mapped to the scene's coordinate -
6109 system. -
6110 -
6111 \sa mapToScene(), mapToItem(), mapFromParent(), {The Graphics View -
6112 Coordinate System} -
6113*/ -
6114QPainterPath QGraphicsItem::mapToParent(const QPainterPath &path) const -
6115{ -
6116 // COMBINE -
6117 if (!d_ptr->transformData) -
6118 return path.translated(d_ptr->pos); -
6119 return d_ptr->transformToParent().map(path); -
6120} -
6121 -
6122/*! -
6123 Maps the path \a path, which is in this item's coordinate system, to -
6124 the scene's coordinate system, and returns the mapped path. -
6125 -
6126 \sa mapToParent(), mapToItem(), mapFromScene(), {The Graphics View -
6127 Coordinate System} -
6128*/ -
6129QPainterPath QGraphicsItem::mapToScene(const QPainterPath &path) const -
6130{ -
6131 if (d_ptr->hasTranslateOnlySceneTransform()) -
6132 return path.translated(d_ptr->sceneTransform.dx(), d_ptr->sceneTransform.dy()); -
6133 return d_ptr->sceneTransform.map(path); -
6134} -
6135 -
6136/*! -
6137 Maps the point \a point, which is in \a item's coordinate system, to this -
6138 item's coordinate system, and returns the mapped coordinate. -
6139 -
6140 If \a item is 0, this function returns the same as mapFromScene(). -
6141 -
6142 \sa itemTransform(), mapFromParent(), mapFromScene(), transform(), mapToItem(), {The Graphics -
6143 View Coordinate System} -
6144*/ -
6145QPointF QGraphicsItem::mapFromItem(const QGraphicsItem *item, const QPointF &point) const -
6146{ -
6147 if (item) -
6148 return item->itemTransform(this).map(point); -
6149 return mapFromScene(point); -
6150} -
6151 -
6152/*! -
6153 \fn QPointF QGraphicsItem::mapFromItem(const QGraphicsItem *item, qreal x, qreal y) const -
6154 \overload -
6155 -
6156 This convenience function is equivalent to calling mapFromItem(\a item, -
6157 QPointF(\a x, \a y)). -
6158*/ -
6159 -
6160/*! -
6161 Maps the point \a point, which is in this item's parent's coordinate -
6162 system, to this item's coordinate system, and returns the mapped -
6163 coordinate. -
6164 -
6165 \sa mapFromItem(), mapFromScene(), transform(), mapToParent(), {The Graphics -
6166 View Coordinate System} -
6167*/ -
6168QPointF QGraphicsItem::mapFromParent(const QPointF &point) const -
6169{ -
6170 // COMBINE -
6171 if (d_ptr->transformData) -
6172 return d_ptr->transformToParent().inverted().map(point); -
6173 return point - d_ptr->pos; -
6174} -
6175 -
6176/*! -
6177 \fn QPointF QGraphicsItem::mapFromParent(qreal x, qreal y) const -
6178 \overload -
6179 -
6180 This convenience function is equivalent to calling -
6181 mapFromParent(QPointF(\a x, \a y)). -
6182*/ -
6183 -
6184/*! -
6185 Maps the point \a point, which is in this item's scene's coordinate -
6186 system, to this item's coordinate system, and returns the mapped -
6187 coordinate. -
6188 -
6189 \sa mapFromItem(), mapFromParent(), transform(), mapToScene(), {The Graphics -
6190 View Coordinate System} -
6191*/ -
6192QPointF QGraphicsItem::mapFromScene(const QPointF &point) const -
6193{ -
6194 if (d_ptr->hasTranslateOnlySceneTransform()) -
6195 return QPointF(point.x() - d_ptr->sceneTransform.dx(), point.y() - d_ptr->sceneTransform.dy()); -
6196 return d_ptr->sceneTransform.inverted().map(point); -
6197} -
6198 -
6199/*! -
6200 \fn QPointF QGraphicsItem::mapFromScene(qreal x, qreal y) const -
6201 \overload -
6202 -
6203 This convenience function is equivalent to calling mapFromScene(QPointF(\a -
6204 x, \a y)). -
6205*/ -
6206 -
6207/*! -
6208 Maps the rectangle \a rect, which is in \a item's coordinate system, to -
6209 this item's coordinate system, and returns the mapped rectangle as a -
6210 polygon. -
6211 -
6212 If \a item is 0, this function returns the same as mapFromScene() -
6213 -
6214 \sa itemTransform(), mapToItem(), mapFromParent(), transform(), {The Graphics View Coordinate -
6215 System} -
6216*/ -
6217QPolygonF QGraphicsItem::mapFromItem(const QGraphicsItem *item, const QRectF &rect) const -
6218{ -
6219 if (item) -
6220 return item->itemTransform(this).map(rect); -
6221 return mapFromScene(rect); -
6222} -
6223 -
6224/*! -
6225 \fn QPolygonF QGraphicsItem::mapFromItem(const QGraphicsItem *item, qreal x, qreal y, qreal w, qreal h) const -
6226 \since 4.3 -
6227 -
6228 This convenience function is equivalent to calling mapFromItem(item, QRectF(\a x, \a y, \a w, \a h)). -
6229*/ -
6230 -
6231/*! -
6232 Maps the rectangle \a rect, which is in this item's parent's coordinate -
6233 system, to this item's coordinate system, and returns the mapped rectangle -
6234 as a polygon. -
6235 -
6236 \sa mapToParent(), mapFromItem(), transform(), {The Graphics View Coordinate -
6237 System} -
6238*/ -
6239QPolygonF QGraphicsItem::mapFromParent(const QRectF &rect) const -
6240{ -
6241 // COMBINE -
6242 if (!d_ptr->transformData) -
6243 return rect.translated(-d_ptr->pos); -
6244 return d_ptr->transformToParent().inverted().map(rect); -
6245} -
6246 -
6247/*! -
6248 \fn QPolygonF QGraphicsItem::mapFromParent(qreal x, qreal y, qreal w, qreal h) const -
6249 \since 4.3 -
6250 -
6251 This convenience function is equivalent to calling mapFromItem(QRectF(\a x, \a y, \a w, \a h)). -
6252*/ -
6253 -
6254/*! -
6255 Maps the rectangle \a rect, which is in this item's scene's coordinate -
6256 system, to this item's coordinate system, and returns the mapped rectangle -
6257 as a polygon. -
6258 -
6259 \sa mapToScene(), mapFromItem(), transform(), {The Graphics View Coordinate -
6260 System} -
6261*/ -
6262QPolygonF QGraphicsItem::mapFromScene(const QRectF &rect) const -
6263{ -
6264 if (d_ptr->hasTranslateOnlySceneTransform()) -
6265 return rect.translated(-d_ptr->sceneTransform.dx(), -d_ptr->sceneTransform.dy()); -
6266 return d_ptr->sceneTransform.inverted().map(rect); -
6267} -
6268 -
6269/*! -
6270 \fn QPolygonF QGraphicsItem::mapFromScene(qreal x, qreal y, qreal w, qreal h) const -
6271 \since 4.3 -
6272 -
6273 This convenience function is equivalent to calling mapFromScene(QRectF(\a x, \a y, \a w, \a h)). -
6274*/ -
6275 -
6276/*! -
6277 Maps the polygon \a polygon, which is in \a item's coordinate system, to -
6278 this item's coordinate system, and returns the mapped polygon. -
6279 -
6280 If \a item is 0, this function returns the same as mapFromScene(). -
6281 -
6282 \sa itemTransform(), mapToItem(), mapFromParent(), transform(), {The -
6283 Graphics View Coordinate System} -
6284*/ -
6285QPolygonF QGraphicsItem::mapFromItem(const QGraphicsItem *item, const QPolygonF &polygon) const -
6286{ -
6287 if (item) -
6288 return item->itemTransform(this).map(polygon); -
6289 return mapFromScene(polygon); -
6290} -
6291 -
6292/*! -
6293 Maps the polygon \a polygon, which is in this item's parent's coordinate -
6294 system, to this item's coordinate system, and returns the mapped polygon. -
6295 -
6296 \sa mapToParent(), mapToItem(), transform(), {The Graphics View Coordinate -
6297 System} -
6298*/ -
6299QPolygonF QGraphicsItem::mapFromParent(const QPolygonF &polygon) const -
6300{ -
6301 // COMBINE -
6302 if (!d_ptr->transformData) -
6303 return polygon.translated(-d_ptr->pos); -
6304 return d_ptr->transformToParent().inverted().map(polygon); -
6305} -
6306 -
6307/*! -
6308 Maps the polygon \a polygon, which is in this item's scene's coordinate -
6309 system, to this item's coordinate system, and returns the mapped polygon. -
6310 -
6311 \sa mapToScene(), mapFromParent(), transform(), {The Graphics View Coordinate -
6312 System} -
6313*/ -
6314QPolygonF QGraphicsItem::mapFromScene(const QPolygonF &polygon) const -
6315{ -
6316 if (d_ptr->hasTranslateOnlySceneTransform()) -
6317 return polygon.translated(-d_ptr->sceneTransform.dx(), -d_ptr->sceneTransform.dy()); -
6318 return d_ptr->sceneTransform.inverted().map(polygon); -
6319} -
6320 -
6321/*! -
6322 Maps the path \a path, which is in \a item's coordinate system, to -
6323 this item's coordinate system, and returns the mapped path. -
6324 -
6325 If \a item is 0, this function returns the same as mapFromScene(). -
6326 -
6327 \sa itemTransform(), mapFromParent(), mapFromScene(), mapToItem(), {The -
6328 Graphics View Coordinate System} -
6329*/ -
6330QPainterPath QGraphicsItem::mapFromItem(const QGraphicsItem *item, const QPainterPath &path) const -
6331{ -
6332 if (item) -
6333 return item->itemTransform(this).map(path); -
6334 return mapFromScene(path); -
6335} -
6336 -
6337/*! -
6338 Maps the path \a path, which is in this item's parent's coordinate -
6339 system, to this item's coordinate system, and returns the mapped path. -
6340 -
6341 \sa mapFromScene(), mapFromItem(), mapToParent(), {The Graphics View -
6342 Coordinate System} -
6343*/ -
6344QPainterPath QGraphicsItem::mapFromParent(const QPainterPath &path) const -
6345{ -
6346 // COMBINE -
6347 if (!d_ptr->transformData) -
6348 return path.translated(-d_ptr->pos); -
6349 return d_ptr->transformToParent().inverted().map(path); -
6350} -
6351 -
6352/*! -
6353 Maps the path \a path, which is in this item's scene's coordinate -
6354 system, to this item's coordinate system, and returns the mapped path. -
6355 -
6356 \sa mapFromParent(), mapFromItem(), mapToScene(), {The Graphics View -
6357 Coordinate System} -
6358*/ -
6359QPainterPath QGraphicsItem::mapFromScene(const QPainterPath &path) const -
6360{ -
6361 if (d_ptr->hasTranslateOnlySceneTransform()) -
6362 return path.translated(-d_ptr->sceneTransform.dx(), -d_ptr->sceneTransform.dy()); -
6363 return d_ptr->sceneTransform.inverted().map(path); -
6364} -
6365 -
6366/*! -
6367 Returns true if this item is an ancestor of \a child (i.e., if this item -
6368 is \a child's parent, or one of \a child's parent's ancestors). -
6369 -
6370 \sa parentItem() -
6371*/ -
6372bool QGraphicsItem::isAncestorOf(const QGraphicsItem *child) const -
6373{ -
6374 if (!child || child == this) -
6375 return false; -
6376 if (child->d_ptr->depth() < d_ptr->depth()) -
6377 return false; -
6378 const QGraphicsItem *ancestor = child; -
6379 while ((ancestor = ancestor->d_ptr->parent)) { -
6380 if (ancestor == this) -
6381 return true; -
6382 } -
6383 return false; -
6384} -
6385 -
6386/*! -
6387 \since 4.4 -
6388 -
6389 Returns the closest common ancestor item of this item and \a other, or 0 -
6390 if either \a other is 0, or there is no common ancestor. -
6391 -
6392 \sa isAncestorOf() -
6393*/ -
6394QGraphicsItem *QGraphicsItem::commonAncestorItem(const QGraphicsItem *other) const -
6395{ -
6396 if (!other) -
6397 return 0; -
6398 if (other == this) -
6399 return const_cast<QGraphicsItem *>(this); -
6400 const QGraphicsItem *thisw = this; -
6401 const QGraphicsItem *otherw = other; -
6402 int thisDepth = d_ptr->depth(); -
6403 int otherDepth = other->d_ptr->depth(); -
6404 while (thisDepth > otherDepth) { -
6405 thisw = thisw->d_ptr->parent; -
6406 --thisDepth; -
6407 } -
6408 while (otherDepth > thisDepth) { -
6409 otherw = otherw->d_ptr->parent; -
6410 --otherDepth; -
6411 } -
6412 while (thisw && thisw != otherw) { -
6413 thisw = thisw->d_ptr->parent; -
6414 otherw = otherw->d_ptr->parent; -
6415 } -
6416 return const_cast<QGraphicsItem *>(thisw); -
6417} -
6418 -
6419/*! -
6420 \since 4,4 -
6421 Returns true if this item is currently under the mouse cursor in one of -
6422 the views; otherwise, false is returned. -
6423 -
6424 \sa QGraphicsScene::views(), QCursor::pos() -
6425*/ -
6426bool QGraphicsItem::isUnderMouse() const -
6427{ -
6428 Q_D(const QGraphicsItem); -
6429 if (!d->scene) -
6430 return false; -
6431 -
6432 QPoint cursorPos = QCursor::pos(); -
6433 foreach (QGraphicsView *view, d->scene->views()) { -
6434 if (contains(mapFromScene(view->mapToScene(view->mapFromGlobal(cursorPos))))) -
6435 return true; -
6436 } -
6437 return false; -
6438} -
6439 -
6440/*! -
6441 Returns this item's custom data for the key \a key as a QVariant. -
6442 -
6443 Custom item data is useful for storing arbitrary properties in any -
6444 item. Example: -
6445 -
6446 \snippet code/src_gui_graphicsview_qgraphicsitem.cpp 11 -
6447 -
6448 Qt does not use this feature for storing data; it is provided solely -
6449 for the convenience of the user. -
6450 -
6451 \sa setData() -
6452*/ -
6453QVariant QGraphicsItem::data(int key) const -
6454{ -
6455 QGraphicsItemCustomDataStore *store = qt_dataStore(); -
6456 if (!store->data.contains(this)) -
6457 return QVariant(); -
6458 return store->data.value(this).value(key); -
6459} -
6460 -
6461/*! -
6462 Sets this item's custom data for the key \a key to \a value. -
6463 -
6464 Custom item data is useful for storing arbitrary properties for any -
6465 item. Qt does not use this feature for storing data; it is provided solely -
6466 for the convenience of the user. -
6467 -
6468 \sa data() -
6469*/ -
6470void QGraphicsItem::setData(int key, const QVariant &value) -
6471{ -
6472 qt_dataStore()->data[this][key] = value; -
6473} -
6474 -
6475/*! -
6476 \fn T qgraphicsitem_cast(QGraphicsItem *item) -
6477 \relates QGraphicsItem -
6478 \since 4.2 -
6479 -
6480 Returns the given \a item cast to type T if \a item is of type T; -
6481 otherwise, 0 is returned. -
6482 -
6483 \note To make this function work correctly with custom items, reimplement -
6484 the \l{QGraphicsItem::}{type()} function for each custom QGraphicsItem -
6485 subclass. -
6486 -
6487 \sa QGraphicsItem::type(), QGraphicsItem::UserType -
6488*/ -
6489 -
6490/*! -
6491 Returns the type of an item as an int. All standard graphicsitem classes -
6492 are associated with a unique value; see QGraphicsItem::Type. This type -
6493 information is used by qgraphicsitem_cast() to distinguish between types. -
6494 -
6495 The default implementation (in QGraphicsItem) returns UserType. -
6496 -
6497 To enable use of qgraphicsitem_cast() with a custom item, reimplement this -
6498 function and declare a Type enum value equal to your custom item's type. -
6499 Custom items must return a value larger than or equal to UserType (65536). -
6500 -
6501 For example: -
6502 -
6503 \snippet code/src_gui_graphicsview_qgraphicsitem.cpp QGraphicsItem type -
6504 -
6505 \sa UserType -
6506*/ -
6507int QGraphicsItem::type() const -
6508{ -
6509 return (int)UserType; -
6510} -
6511 -
6512/*! -
6513 Installs an event filter for this item on \a filterItem, causing -
6514 all events for this item to first pass through \a filterItem's -
6515 sceneEventFilter() function. -
6516 -
6517 To filter another item's events, install this item as an event filter -
6518 for the other item. Example: -
6519 -
6520 \snippet code/src_gui_graphicsview_qgraphicsitem.cpp 12 -
6521 -
6522 An item can only filter events for other items in the same -
6523 scene. Also, an item cannot filter its own events; instead, you -
6524 can reimplement sceneEvent() directly. -
6525 -
6526 Items must belong to a scene for scene event filters to be installed and -
6527 used. -
6528 -
6529 \sa removeSceneEventFilter(), sceneEventFilter(), sceneEvent() -
6530*/ -
6531void QGraphicsItem::installSceneEventFilter(QGraphicsItem *filterItem) -
6532{ -
6533 if (!d_ptr->scene) { -
6534 qWarning("QGraphicsItem::installSceneEventFilter: event filters can only be installed" -
6535 " on items in a scene."); -
6536 return; -
6537 } -
6538 if (d_ptr->scene != filterItem->scene()) { -
6539 qWarning("QGraphicsItem::installSceneEventFilter: event filters can only be installed" -
6540 " on items in the same scene."); -
6541 return; -
6542 } -
6543 d_ptr->scene->d_func()->installSceneEventFilter(this, filterItem); -
6544} -
6545 -
6546/*! -
6547 Removes an event filter on this item from \a filterItem. -
6548 -
6549 \sa installSceneEventFilter() -
6550*/ -
6551void QGraphicsItem::removeSceneEventFilter(QGraphicsItem *filterItem) -
6552{ -
6553 if (!d_ptr->scene || d_ptr->scene != filterItem->scene()) -
6554 return; -
6555 d_ptr->scene->d_func()->removeSceneEventFilter(this, filterItem); -
6556} -
6557 -
6558/*! -
6559 Filters events for the item \a watched. \a event is the filtered -
6560 event. -
6561 -
6562 Reimplementing this function in a subclass makes it possible -
6563 for the item to be used as an event filter for other items, -
6564 intercepting all the events send to those items before they are -
6565 able to respond. -
6566 -
6567 Reimplementations must return true to prevent further processing of -
6568 a given event, ensuring that it will not be delivered to the watched -
6569 item, or return false to indicate that the event should be propagated -
6570 further by the event system. -
6571 -
6572 \sa installSceneEventFilter() -
6573*/ -
6574bool QGraphicsItem::sceneEventFilter(QGraphicsItem *watched, QEvent *event) -
6575{ -
6576 Q_UNUSED(watched); -
6577 Q_UNUSED(event); -
6578 return false; -
6579} -
6580 -
6581/*! -
6582 This virtual function receives events to this item. Reimplement -
6583 this function to intercept events before they are dispatched to -
6584 the specialized event handlers contextMenuEvent(), focusInEvent(), -
6585 focusOutEvent(), hoverEnterEvent(), hoverMoveEvent(), -
6586 hoverLeaveEvent(), keyPressEvent(), keyReleaseEvent(), -
6587 mousePressEvent(), mouseReleaseEvent(), mouseMoveEvent(), and -
6588 mouseDoubleClickEvent(). -
6589 -
6590 Returns true if the event was recognized and handled; otherwise, (e.g., if -
6591 the event type was not recognized,) false is returned. -
6592 -
6593 \a event is the intercepted event. -
6594*/ -
6595bool QGraphicsItem::sceneEvent(QEvent *event) -
6596{ -
6597 if (d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorHandlesChildEvents) { -
6598 if (event->type() == QEvent::HoverEnter || event->type() == QEvent::HoverLeave -
6599 || event->type() == QEvent::DragEnter || event->type() == QEvent::DragLeave) { -
6600 // Hover enter and hover leave events for children are ignored; -
6601 // hover move events are forwarded. -
6602 return true; -
6603 } -
6604 -
6605 QGraphicsItem *handler = this; -
6606 do { -
6607 handler = handler->d_ptr->parent; -
6608 Q_ASSERT(handler); -
6609 } while (handler->d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorHandlesChildEvents); -
6610 // Forward the event to the closest parent that handles child -
6611 // events, mapping existing item-local coordinates to its -
6612 // coordinate system. -
6613 d_ptr->remapItemPos(event, handler); -
6614 handler->sceneEvent(event); -
6615 return true; -
6616 } -
6617 -
6618 if (event->type() == QEvent::FocusOut) { -
6619 focusOutEvent(static_cast<QFocusEvent *>(event)); -
6620 return true; -
6621 } -
6622 -
6623 if (!d_ptr->visible) { -
6624 // Eaten -
6625 return true; -
6626 } -
6627 -
6628 switch (event->type()) { -
6629 case QEvent::FocusIn: -
6630 focusInEvent(static_cast<QFocusEvent *>(event)); -
6631 break; -
6632 case QEvent::GraphicsSceneContextMenu: -
6633 contextMenuEvent(static_cast<QGraphicsSceneContextMenuEvent *>(event)); -
6634 break; -
6635 case QEvent::GraphicsSceneDragEnter: -
6636 dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent *>(event)); -
6637 break; -
6638 case QEvent::GraphicsSceneDragMove: -
6639 dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent *>(event)); -
6640 break; -
6641 case QEvent::GraphicsSceneDragLeave: -
6642 dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent *>(event)); -
6643 break; -
6644 case QEvent::GraphicsSceneDrop: -
6645 dropEvent(static_cast<QGraphicsSceneDragDropEvent *>(event)); -
6646 break; -
6647 case QEvent::GraphicsSceneHoverEnter: -
6648 hoverEnterEvent(static_cast<QGraphicsSceneHoverEvent *>(event)); -
6649 break; -
6650 case QEvent::GraphicsSceneHoverMove: -
6651 hoverMoveEvent(static_cast<QGraphicsSceneHoverEvent *>(event)); -
6652 break; -
6653 case QEvent::GraphicsSceneHoverLeave: -
6654 hoverLeaveEvent(static_cast<QGraphicsSceneHoverEvent *>(event)); -
6655 break; -
6656 case QEvent::GraphicsSceneMouseMove: -
6657 mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent *>(event)); -
6658 break; -
6659 case QEvent::GraphicsSceneMousePress: -
6660 mousePressEvent(static_cast<QGraphicsSceneMouseEvent *>(event)); -
6661 break; -
6662 case QEvent::GraphicsSceneMouseRelease: -
6663 mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent *>(event)); -
6664 break; -
6665 case QEvent::GraphicsSceneMouseDoubleClick: -
6666 mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent *>(event)); -
6667 break; -
6668 case QEvent::GraphicsSceneWheel: -
6669 wheelEvent(static_cast<QGraphicsSceneWheelEvent *>(event)); -
6670 break; -
6671 case QEvent::KeyPress: { -
6672 QKeyEvent *k = static_cast<QKeyEvent *>(event); -
6673 if (k->key() == Qt::Key_Tab || k->key() == Qt::Key_Backtab) { -
6674 if (!(k->modifiers() & (Qt::ControlModifier | Qt::AltModifier))) { //### Add MetaModifier? -
6675 bool res = false; -
6676 if (k->key() == Qt::Key_Backtab -
6677 || (k->key() == Qt::Key_Tab && (k->modifiers() & Qt::ShiftModifier))) { -
6678 if (d_ptr->isWidget) { -
6679 res = static_cast<QGraphicsWidget *>(this)->focusNextPrevChild(false); -
6680 } else if (d_ptr->scene) { -
6681 res = d_ptr->scene->focusNextPrevChild(false); -
6682 } -
6683 } else if (k->key() == Qt::Key_Tab) { -
6684 if (d_ptr->isWidget) { -
6685 res = static_cast<QGraphicsWidget *>(this)->focusNextPrevChild(true); -
6686 } else if (d_ptr->scene) { -
6687 res = d_ptr->scene->focusNextPrevChild(true); -
6688 } -
6689 } -
6690 if (!res) -
6691 event->ignore(); -
6692 return true; -
6693 } -
6694 } -
6695 keyPressEvent(static_cast<QKeyEvent *>(event)); -
6696 break; -
6697 } -
6698 case QEvent::KeyRelease: -
6699 keyReleaseEvent(static_cast<QKeyEvent *>(event)); -
6700 break; -
6701 case QEvent::InputMethod: -
6702 inputMethodEvent(static_cast<QInputMethodEvent *>(event)); -
6703 break; -
6704 case QEvent::WindowActivate: -
6705 case QEvent::WindowDeactivate: -
6706 // Propagate panel activation. -
6707 if (d_ptr->scene) { -
6708 for (int i = 0; i < d_ptr->children.size(); ++i) { -
6709 QGraphicsItem *child = d_ptr->children.at(i); -
6710 if (child->isVisible() && !child->isPanel()) { -
6711 if (!(child->d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorHandlesChildEvents)) -
6712 d_ptr->scene->sendEvent(child, event); -
6713 } -
6714 } -
6715 } -
6716 break; -
6717 default: -
6718 return false; -
6719 } -
6720 -
6721 return true; -
6722} -
6723 -
6724/*! -
6725 This event handler can be reimplemented in a subclass to process context -
6726 menu events. The \a event parameter contains details about the event to -
6727 be handled. -
6728 -
6729 If you ignore the event (i.e., by calling QEvent::ignore()), \a event -
6730 will propagate to any item beneath this item. If no items accept the -
6731 event, it will be ignored by the scene and propagate to the view. -
6732 -
6733 It's common to open a QMenu in response to receiving a context menu -
6734 event. Example: -
6735 -
6736 \snippet code/src_gui_graphicsview_qgraphicsitem.cpp 13 -
6737 -
6738 The default implementation ignores the event. -
6739 -
6740 \sa sceneEvent() -
6741*/ -
6742void QGraphicsItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) -
6743{ -
6744 event->ignore(); -
6745} -
6746 -
6747/*! -
6748 This event handler, for event \a event, can be reimplemented to receive -
6749 drag enter events for this item. Drag enter events are generated as the -
6750 cursor enters the item's area. -
6751 -
6752 By accepting the event (i.e., by calling QEvent::accept()), the item will -
6753 accept drop events, in addition to receiving drag move and drag -
6754 leave. Otherwise, the event will be ignored and propagate to the item -
6755 beneath. If the event is accepted, the item will receive a drag move event -
6756 before control goes back to the event loop. -
6757 -
6758 A common implementation of dragEnterEvent accepts or ignores \a event -
6759 depending on the associated mime data in \a event. Example: -
6760 -
6761 \snippet code/src_gui_graphicsview_qgraphicsitem.cpp 14 -
6762 -
6763 Items do not receive drag and drop events by default; to enable this -
6764 feature, call \c setAcceptDrops(true). -
6765 -
6766 The default implementation does nothing. -
6767 -
6768 \sa dropEvent(), dragMoveEvent(), dragLeaveEvent() -
6769*/ -
6770void QGraphicsItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) -
6771{ -
6772 Q_D(QGraphicsItem); -
6773 // binary compatibility workaround between 4.4 and 4.5 -
6774 if (d->isProxyWidget()) -
6775 static_cast<QGraphicsProxyWidget*>(this)->dragEnterEvent(event); -
6776} -
6777 -
6778/*! -
6779 This event handler, for event \a event, can be reimplemented to receive -
6780 drag leave events for this item. Drag leave events are generated as the -
6781 cursor leaves the item's area. Most often you will not need to reimplement -
6782 this function, but it can be useful for resetting state in your item -
6783 (e.g., highlighting). -
6784 -
6785 Calling QEvent::ignore() or QEvent::accept() on \a event has no effect. -
6786 -
6787 Items do not receive drag and drop events by default; to enable this -
6788 feature, call \c setAcceptDrops(true). -
6789 -
6790 The default implementation does nothing. -
6791 -
6792 \sa dragEnterEvent(), dropEvent(), dragMoveEvent() -
6793*/ -
6794void QGraphicsItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) -
6795{ -
6796 Q_D(QGraphicsItem); -
6797 // binary compatibility workaround between 4.4 and 4.5 -
6798 if (d->isProxyWidget()) -
6799 static_cast<QGraphicsProxyWidget*>(this)->dragLeaveEvent(event); -
6800} -
6801 -
6802/*! -
6803 This event handler, for event \a event, can be reimplemented to receive -
6804 drag move events for this item. Drag move events are generated as the -
6805 cursor moves around inside the item's area. Most often you will not need -
6806 to reimplement this function; it is used to indicate that only parts of -
6807 the item can accept drops. -
6808 -
6809 Calling QEvent::ignore() or QEvent::accept() on \a event toggles whether -
6810 or not the item will accept drops at the position from the event. By -
6811 default, \a event is accepted, indicating that the item allows drops at -
6812 the specified position. -
6813 -
6814 Items do not receive drag and drop events by default; to enable this -
6815 feature, call \c setAcceptDrops(true). -
6816 -
6817 The default implementation does nothing. -
6818 -
6819 \sa dropEvent(), dragEnterEvent(), dragLeaveEvent() -
6820*/ -
6821void QGraphicsItem::dragMoveEvent(QGraphicsSceneDragDropEvent *event) -
6822{ -
6823 Q_D(QGraphicsItem); -
6824 // binary compatibility workaround between 4.4 and 4.5 -
6825 if (d->isProxyWidget()) -
6826 static_cast<QGraphicsProxyWidget*>(this)->dragMoveEvent(event); -
6827} -
6828 -
6829/*! -
6830 This event handler, for event \a event, can be reimplemented to receive -
6831 drop events for this item. Items can only receive drop events if the last -
6832 drag move event was accepted. -
6833 -
6834 Calling QEvent::ignore() or QEvent::accept() on \a event has no effect. -
6835 -
6836 Items do not receive drag and drop events by default; to enable this -
6837 feature, call \c setAcceptDrops(true). -
6838 -
6839 The default implementation does nothing. -
6840 -
6841 \sa dragEnterEvent(), dragMoveEvent(), dragLeaveEvent() -
6842*/ -
6843void QGraphicsItem::dropEvent(QGraphicsSceneDragDropEvent *event) -
6844{ -
6845 Q_D(QGraphicsItem); -
6846 // binary compatibility workaround between 4.4 and 4.5 -
6847 if (d->isProxyWidget()) -
6848 static_cast<QGraphicsProxyWidget*>(this)->dropEvent(event); -
6849} -
6850 -
6851/*! -
6852 This event handler, for event \a event, can be reimplemented to receive -
6853 focus in events for this item. The default implementation calls -
6854 ensureVisible(). -
6855 -
6856 \sa focusOutEvent(), sceneEvent(), setFocus() -
6857*/ -
6858void QGraphicsItem::focusInEvent(QFocusEvent *event) -
6859{ -
6860 Q_UNUSED(event); -
6861 update(); -
6862} -
6863 -
6864/*! -
6865 This event handler, for event \a event, can be reimplemented to receive -
6866 focus out events for this item. The default implementation does nothing. -
6867 -
6868 \sa focusInEvent(), sceneEvent(), setFocus() -
6869*/ -
6870void QGraphicsItem::focusOutEvent(QFocusEvent *event) -
6871{ -
6872 Q_UNUSED(event); -
6873 update(); -
6874} -
6875 -
6876/*! -
6877 This event handler, for event \a event, can be reimplemented to receive -
6878 hover enter events for this item. The default implementation calls -
6879 update(); otherwise it does nothing. -
6880 -
6881 Calling QEvent::ignore() or QEvent::accept() on \a event has no effect. -
6882 -
6883 \sa hoverMoveEvent(), hoverLeaveEvent(), sceneEvent(), setAcceptHoverEvents() -
6884*/ -
6885void QGraphicsItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) -
6886{ -
6887 Q_UNUSED(event); -
6888 update(); -
6889} -
6890 -
6891/*! -
6892 This event handler, for event \a event, can be reimplemented to receive -
6893 hover move events for this item. The default implementation does nothing. -
6894 -
6895 Calling QEvent::ignore() or QEvent::accept() on \a event has no effect. -
6896 -
6897 \sa hoverEnterEvent(), hoverLeaveEvent(), sceneEvent(), setAcceptHoverEvents() -
6898*/ -
6899void QGraphicsItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) -
6900{ -
6901 Q_UNUSED(event); -
6902} -
6903 -
6904/*! -
6905 This event handler, for event \a event, can be reimplemented to receive -
6906 hover leave events for this item. The default implementation calls -
6907 update(); otherwise it does nothing. -
6908 -
6909 Calling QEvent::ignore() or QEvent::accept() on \a event has no effect. -
6910 -
6911 \sa hoverEnterEvent(), hoverMoveEvent(), sceneEvent(), setAcceptHoverEvents() -
6912*/ -
6913void QGraphicsItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) -
6914{ -
6915 Q_UNUSED(event); -
6916 update(); -
6917} -
6918 -
6919/*! -
6920 This event handler, for event \a event, can be reimplemented to -
6921 receive key press events for this item. The default implementation -
6922 ignores the event. If you reimplement this handler, the event will by -
6923 default be accepted. -
6924 -
6925 Note that key events are only received for items that set the -
6926 ItemIsFocusable flag, and that have keyboard input focus. -
6927 -
6928 \sa keyReleaseEvent(), setFocus(), QGraphicsScene::setFocusItem(), -
6929 sceneEvent() -
6930*/ -
6931void QGraphicsItem::keyPressEvent(QKeyEvent *event) -
6932{ -
6933 event->ignore(); -
6934} -
6935 -
6936/*! -
6937 This event handler, for event \a event, can be reimplemented to receive -
6938 key release events for this item. The default implementation -
6939 ignores the event. If you reimplement this handler, the event will by -
6940 default be accepted. -
6941 -
6942 Note that key events are only received for items that set the -
6943 ItemIsFocusable flag, and that have keyboard input focus. -
6944 -
6945 \sa keyPressEvent(), setFocus(), QGraphicsScene::setFocusItem(), -
6946 sceneEvent() -
6947*/ -
6948void QGraphicsItem::keyReleaseEvent(QKeyEvent *event) -
6949{ -
6950 event->ignore(); -
6951} -
6952 -
6953/*! -
6954 This event handler, for event \a event, can be reimplemented to -
6955 receive mouse press events for this item. Mouse press events are -
6956 only delivered to items that accept the mouse button that is -
6957 pressed. By default, an item accepts all mouse buttons, but you -
6958 can change this by calling setAcceptedMouseButtons(). -
6959 -
6960 The mouse press event decides which item should become the mouse -
6961 grabber (see QGraphicsScene::mouseGrabberItem()). If you do not -
6962 reimplement this function, the press event will propagate to any -
6963 topmost item beneath this item, and no other mouse events will be -
6964 delivered to this item. -
6965 -
6966 If you do reimplement this function, \a event will by default be -
6967 accepted (see QEvent::accept()), and this item is then the mouse -
6968 grabber. This allows the item to receive future move, release and -
6969 doubleclick events. If you call QEvent::ignore() on \a event, this -
6970 item will lose the mouse grab, and \a event will propagate to any -
6971 topmost item beneath. No further mouse events will be delivered to -
6972 this item unless a new mouse press event is received. -
6973 -
6974 The default implementation handles basic item interaction, such as -
6975 selection and moving. If you want to keep the base implementation -
6976 when reimplementing this function, call -
6977 QGraphicsItem::mousePressEvent() in your reimplementation. -
6978 -
6979 The event is \l{QEvent::ignore()}d for items that are neither -
6980 \l{QGraphicsItem::ItemIsMovable}{movable} nor -
6981 \l{QGraphicsItem::ItemIsSelectable}{selectable}. -
6982 -
6983 \sa mouseMoveEvent(), mouseReleaseEvent(), -
6984 mouseDoubleClickEvent(), sceneEvent() -
6985*/ -
6986void QGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event) -
6987{ -
6988 if (event->button() == Qt::LeftButton && (flags() & ItemIsSelectable)) { -
6989 bool multiSelect = (event->modifiers() & Qt::ControlModifier) != 0; -
6990 if (!multiSelect) { -
6991 if (!d_ptr->selected) { -
6992 if (QGraphicsScene *scene = d_ptr->scene) { -
6993 ++scene->d_func()->selectionChanging; -
6994 scene->clearSelection(); -
6995 --scene->d_func()->selectionChanging; -
6996 } -
6997 setSelected(true); -
6998 } -
6999 } -
7000 } else if (!(flags() & ItemIsMovable)) { -
7001 event->ignore(); -
7002 } -
7003 if (d_ptr->isWidget) { -
7004 // Qt::Popup closes when you click outside. -
7005 QGraphicsWidget *w = static_cast<QGraphicsWidget *>(this); -
7006 if ((w->windowFlags() & Qt::Popup) == Qt::Popup) { -
7007 event->accept(); -
7008 if (!w->rect().contains(event->pos())) -
7009 w->close(); -
7010 } -
7011 } -
7012} -
7013 -
7014/*! -
7015 obsolete -
7016*/ -
7017bool _qt_movableAncestorIsSelected(const QGraphicsItem *item) -
7018{ -
7019 const QGraphicsItem *parent = item->parentItem(); -
7020 return parent && (((parent->flags() & QGraphicsItem::ItemIsMovable) && parent->isSelected()) || _qt_movableAncestorIsSelected(parent)); -
7021} -
7022 -
7023bool QGraphicsItemPrivate::movableAncestorIsSelected(const QGraphicsItem *item) -
7024{ -
7025 const QGraphicsItem *parent = item->d_ptr->parent; -
7026 return parent && (((parent->flags() & QGraphicsItem::ItemIsMovable) && parent->isSelected()) || _qt_movableAncestorIsSelected(parent)); -
7027} -
7028 -
7029/*! -
7030 This event handler, for event \a event, can be reimplemented to -
7031 receive mouse move events for this item. If you do receive this -
7032 event, you can be certain that this item also received a mouse -
7033 press event, and that this item is the current mouse grabber. -
7034 -
7035 Calling QEvent::ignore() or QEvent::accept() on \a event has no -
7036 effect. -
7037 -
7038 The default implementation handles basic item interaction, such as -
7039 selection and moving. If you want to keep the base implementation -
7040 when reimplementing this function, call -
7041 QGraphicsItem::mouseMoveEvent() in your reimplementation. -
7042 -
7043 Please note that mousePressEvent() decides which graphics item it -
7044 is that receives mouse events. See the mousePressEvent() -
7045 description for details. -
7046 -
7047 \sa mousePressEvent(), mouseReleaseEvent(), -
7048 mouseDoubleClickEvent(), sceneEvent() -
7049*/ -
7050void QGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) -
7051{ -
7052 if ((event->buttons() & Qt::LeftButton) && (flags() & ItemIsMovable)) { -
7053 // Determine the list of items that need to be moved. -
7054 QList<QGraphicsItem *> selectedItems; -
7055 QHash<QGraphicsItem *, QPointF> initialPositions; -
7056 if (d_ptr->scene) { -
7057 selectedItems = d_ptr->scene->selectedItems(); -
7058 initialPositions = d_ptr->scene->d_func()->movingItemsInitialPositions; -
7059 if (initialPositions.isEmpty()) { -
7060 foreach (QGraphicsItem *item, selectedItems) -
7061 initialPositions[item] = item->pos(); -
7062 initialPositions[this] = pos(); -
7063 } -
7064 d_ptr->scene->d_func()->movingItemsInitialPositions = initialPositions; -
7065 } -
7066 -
7067 // Find the active view. -
7068 QGraphicsView *view = 0; -
7069 if (event->widget()) -
7070 view = qobject_cast<QGraphicsView *>(event->widget()->parentWidget()); -
7071 -
7072 // Move all selected items -
7073 int i = 0; -
7074 bool movedMe = false; -
7075 while (i <= selectedItems.size()) { -
7076 QGraphicsItem *item = 0; -
7077 if (i < selectedItems.size()) -
7078 item = selectedItems.at(i); -
7079 else -
7080 item = this; -
7081 if (item == this) { -
7082 // Slightly clumsy-looking way to ensure that "this" is part -
7083 // of the list of items to move, this is to avoid allocations -
7084 // (appending this item to the list of selected items causes a -
7085 // detach). -
7086 if (movedMe) -
7087 break; -
7088 movedMe = true; -
7089 } -
7090 -
7091 if ((item->flags() & ItemIsMovable) && !QGraphicsItemPrivate::movableAncestorIsSelected(item)) { -
7092 QPointF currentParentPos; -
7093 QPointF buttonDownParentPos; -
7094 if (item->d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorIgnoresTransformations) { -
7095 // Items whose ancestors ignore transformations need to -
7096 // map screen coordinates to local coordinates, then map -
7097 // those to the parent. -
7098 QTransform viewToItemTransform = (item->deviceTransform(view->viewportTransform())).inverted(); -
7099 currentParentPos = mapToParent(viewToItemTransform.map(QPointF(view->mapFromGlobal(event->screenPos())))); -
7100 buttonDownParentPos = mapToParent(viewToItemTransform.map(QPointF(view->mapFromGlobal(event->buttonDownScreenPos(Qt::LeftButton))))); -
7101 } else if (item->flags() & ItemIgnoresTransformations) { -
7102 // Root items that ignore transformations need to -
7103 // calculate their diff by mapping viewport coordinates -
7104 // directly to parent coordinates. -
7105 // COMBINE -
7106 QTransform itemTransform; -
7107 if (item->d_ptr->transformData) -
7108 itemTransform = item->d_ptr->transformData->computedFullTransform(); -
7109 itemTransform.translate(item->d_ptr->pos.x(), item->d_ptr->pos.y()); -
7110 QTransform viewToParentTransform = itemTransform -
7111 * (item->sceneTransform() * view->viewportTransform()).inverted(); -
7112 currentParentPos = viewToParentTransform.map(QPointF(view->mapFromGlobal(event->screenPos()))); -
7113 buttonDownParentPos = viewToParentTransform.map(QPointF(view->mapFromGlobal(event->buttonDownScreenPos(Qt::LeftButton)))); -
7114 } else { -
7115 // All other items simply map from the scene. -
7116 currentParentPos = item->mapToParent(item->mapFromScene(event->scenePos())); -
7117 buttonDownParentPos = item->mapToParent(item->mapFromScene(event->buttonDownScenePos(Qt::LeftButton))); -
7118 } -
7119 -
7120 item->setPos(initialPositions.value(item) + currentParentPos - buttonDownParentPos); -
7121 -
7122 if (item->flags() & ItemIsSelectable) -
7123 item->setSelected(true); -
7124 } -
7125 ++i; -
7126 } -
7127 -
7128 } else { -
7129 event->ignore(); -
7130 } -
7131} -
7132 -
7133/*! -
7134 This event handler, for event \a event, can be reimplemented to -
7135 receive mouse release events for this item. -
7136 -
7137 Calling QEvent::ignore() or QEvent::accept() on \a event has no -
7138 effect. -
7139 -
7140 The default implementation handles basic item interaction, such as -
7141 selection and moving. If you want to keep the base implementation -
7142 when reimplementing this function, call -
7143 QGraphicsItem::mouseReleaseEvent() in your reimplementation. -
7144 -
7145 Please note that mousePressEvent() decides which graphics item it -
7146 is that receives mouse events. See the mousePressEvent() -
7147 description for details. -
7148 -
7149 \sa mousePressEvent(), mouseMoveEvent(), mouseDoubleClickEvent(), -
7150 sceneEvent() -
7151*/ -
7152void QGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) -
7153{ -
7154 if (flags() & ItemIsSelectable) { -
7155 bool multiSelect = (event->modifiers() & Qt::ControlModifier) != 0; -
7156 if (event->scenePos() == event->buttonDownScenePos(Qt::LeftButton)) { -
7157 // The item didn't move -
7158 if (multiSelect) { -
7159 setSelected(!isSelected()); -
7160 } else { -
7161 bool selectionChanged = false; -
7162 if (QGraphicsScene *scene = d_ptr->scene) { -
7163 ++scene->d_func()->selectionChanging; -
7164 // Clear everything but this item. Bypass -
7165 // QGraphicsScene::clearSelection()'s default behavior by -
7166 // temporarily removing this item from the selection list. -
7167 if (d_ptr->selected) { -
7168 scene->d_func()->selectedItems.remove(this); -
7169 foreach (QGraphicsItem *item, scene->d_func()->selectedItems) { -
7170 if (item->isSelected()) { -
7171 selectionChanged = true; -
7172 break; -
7173 } -
7174 } -
7175 } -
7176 scene->clearSelection(); -
7177 if (d_ptr->selected) -
7178 scene->d_func()->selectedItems.insert(this); -
7179 --scene->d_func()->selectionChanging; -
7180 if (selectionChanged) -
7181 emit d_ptr->scene->selectionChanged(); -
7182 } -
7183 setSelected(true); -
7184 } -
7185 } -
7186 } -
7187 if (d_ptr->scene && !event->buttons()) -
7188 d_ptr->scene->d_func()->movingItemsInitialPositions.clear(); -
7189} -
7190 -
7191/*! -
7192 This event handler, for event \a event, can be reimplemented to -
7193 receive mouse doubleclick events for this item. -
7194 -
7195 When doubleclicking an item, the item will first receive a mouse -
7196 press event, followed by a release event (i.e., a click), then a -
7197 doubleclick event, and finally a release event. -
7198 -
7199 Calling QEvent::ignore() or QEvent::accept() on \a event has no -
7200 effect. -
7201 -
7202 The default implementation calls mousePressEvent(). If you want to -
7203 keep the base implementation when reimplementing this function, -
7204 call QGraphicsItem::mouseDoubleClickEvent() in your -
7205 reimplementation. -
7206 -
7207 Note that an item will not receive double click events if it is -
7208 neither \l {QGraphicsItem::ItemIsSelectable}{selectable} nor -
7209 \l{QGraphicsItem::ItemIsMovable}{movable} (single mouse clicks are -
7210 ignored in this case, and that stops the generation of double -
7211 clicks). -
7212 -
7213 \sa mousePressEvent(), mouseMoveEvent(), mouseReleaseEvent(), sceneEvent() -
7214*/ -
7215void QGraphicsItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) -
7216{ -
7217 mousePressEvent(event); -
7218} -
7219 -
7220/*! -
7221 This event handler, for event \a event, can be reimplemented to receive -
7222 wheel events for this item. If you reimplement this function, \a event -
7223 will be accepted by default. -
7224 -
7225 If you ignore the event, (i.e., by calling QEvent::ignore(),) it will -
7226 propagate to any item beneath this item. If no items accept the event, it -
7227 will be ignored by the scene, and propagate to the view (e.g., the view's -
7228 vertical scroll bar). -
7229 -
7230 The default implementation ignores the event. -
7231 -
7232 \sa sceneEvent() -
7233*/ -
7234void QGraphicsItem::wheelEvent(QGraphicsSceneWheelEvent *event) -
7235{ -
7236 event->ignore(); -
7237} -
7238 -
7239/*! -
7240 This event handler, for event \a event, can be reimplemented to receive -
7241 input method events for this item. The default implementation ignores the -
7242 event. -
7243 -
7244 \sa inputMethodQuery(), sceneEvent() -
7245*/ -
7246void QGraphicsItem::inputMethodEvent(QInputMethodEvent *event) -
7247{ -
7248 event->ignore(); -
7249} -
7250 -
7251/*! -
7252 This method is only relevant for input items. It is used by the -
7253 input method to query a set of properties of the item to be able -
7254 to support complex input method operations, such as support for -
7255 surrounding text and reconversions. \a query specifies which -
7256 property is queried. -
7257 -
7258 \sa inputMethodEvent(), QInputMethodEvent -
7259*/ -
7260QVariant QGraphicsItem::inputMethodQuery(Qt::InputMethodQuery query) const -
7261{ -
7262 Q_UNUSED(query); -
7263 return QVariant(); -
7264} -
7265 -
7266/*! -
7267 Returns the current input method hints of this item. -
7268 -
7269 Input method hints are only relevant for input items. -
7270 The hints are used by the input method to indicate how it should operate. -
7271 For example, if the Qt::ImhNumbersOnly flag is set, the input method may change -
7272 its visual components to reflect that only numbers can be entered. -
7273 -
7274 The effect may vary between input method implementations. -
7275 -
7276 \since 4.6 -
7277 -
7278 \sa setInputMethodHints(), inputMethodQuery() -
7279*/ -
7280Qt::InputMethodHints QGraphicsItem::inputMethodHints() const -
7281{ -
7282 Q_D(const QGraphicsItem); -
7283 return d->imHints; -
7284} -
7285 -
7286/*! -
7287 Sets the current input method hints of this item to \a hints. -
7288 -
7289 \since 4.6 -
7290 -
7291 \sa inputMethodHints(), inputMethodQuery() -
7292*/ -
7293void QGraphicsItem::setInputMethodHints(Qt::InputMethodHints hints) -
7294{ -
7295 Q_D(QGraphicsItem); -
7296 d->imHints = hints; -
7297 if (!hasFocus()) -
7298 return; -
7299 d->scene->d_func()->updateInputMethodSensitivityInViews(); -
7300 QWidget *fw = QApplication::focusWidget(); -
7301 if (!fw) -
7302 return; -
7303 qApp->inputMethod()->update(Qt::ImHints); -
7304} -
7305 -
7306/*! -
7307 Updates the item's micro focus. -
7308 -
7309 \since 4.7 -
7310 -
7311 \sa QInputMethod -
7312*/ -
7313void QGraphicsItem::updateMicroFocus() -
7314{ -
7315#if !defined(QT_NO_IM) && defined(Q_WS_X11) -
7316 if (QWidget *fw = QApplication::focusWidget()) { -
7317 if (scene()) { -
7318 for (int i = 0 ; i < scene()->views().count() ; ++i) { -
7319 if (scene()->views().at(i) == fw) { -
7320 if (qApp) -
7321 qApp->inputMethod()->update(Qt::ImQueryAll);-
#ifndef QT_NO_ACCESSIBILITY
// ##### is this correct
if (toGraphicsObject())
QAccessible::updateAccessibility(toGraphicsObject(), 0, QAccessible::StateChanged);
7322 #endifbreak; -
7323 } -
7324 } -
7325 } -
7326 } -
7327 } -
7328#endif -
7329} -
7330 -
7331/*!-
This virtual function is called by QGraphicsItem to notify custom items
that some part of the item's state changes. By reimplementing this
function, your can react to a change, and in some cases, (depending on \a
change,) adjustments can be made.
\a change is the parameter of the item that is changing. \a value is the
new value; the type of the value depends on \a change.
Example:
\snippet code/src_gui_graphicsview_qgraphicsitem.cpp 15
The default implementation does nothing, and returns \a value.
Note: Certain QGraphicsItem functions cannot be called in a
reimplementation of this function; see the GraphicsItemChange
documentation for details.
\sa GraphicsItemChange
*/*!
7332 This virtual function is called by QGraphicsItem to notify custom items -
7333 that some part of the item's state changes. By reimplementing this -
7334 function, you can react to a change, and in some cases (depending on \a -
7335 change), adjustments can be made. -
7336 -
7337 \a change is the parameter of the item that is changing. \a value is the -
7338 new value; the type of the value depends on \a change. -
7339 -
7340 Example: -
7341 -
7342 \snippet code/src_gui_graphicsview_qgraphicsitem.cpp 15 -
7343 -
7344 The default implementation does nothing, and returns \a value. -
7345 -
7346 Note: Certain QGraphicsItem functions cannot be called in a -
7347 reimplementation of this function; see the GraphicsItemChange -
7348 documentation for details. -
7349 -
7350 \sa GraphicsItemChange -
7351*/ -
7352QVariant QGraphicsItem::itemChange(GraphicsItemChange change, const QVariant &value) -
7353{ -
7354 Q_UNUSED(change); -
7355 return value; -
7356} -
7357 -
7358/*! -
7359 \internal -
7360 -
7361 Note: This is provided as a hook to avoid future problems related -
7362 to adding virtual functions. -
7363*/ -
7364bool QGraphicsItem::supportsExtension(Extension extension) const -
7365{ -
7366 Q_UNUSED(extension); -
7367 return false; -
7368} -
7369 -
7370/*! -
7371 \internal -
7372 -
7373 Note: This is provided as a hook to avoid future problems related -
7374 to adding virtual functions. -
7375*/ -
7376void QGraphicsItem::setExtension(Extension extension, const QVariant &variant) -
7377{ -
7378 Q_UNUSED(extension); -
7379 Q_UNUSED(variant); -
7380} -
7381 -
7382/*! -
7383 \internal -
7384 -
7385 Note: This is provided as a hook to avoid future problems related -
7386 to adding virtual functions. -
7387*/ -
7388QVariant QGraphicsItem::extension(const QVariant &variant) const -
7389{ -
7390 Q_UNUSED(variant); -
7391 return QVariant(); -
7392} -
7393 -
7394/*! -
7395 \internal -
7396 -
7397 Adds this item to the scene's index. Called in conjunction with -
7398 removeFromIndex() to ensure the index bookkeeping is correct when -
7399 the item's position, transformation or shape changes. -
7400*/ -
7401void QGraphicsItem::addToIndex() -
7402{ -
7403 if (d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorClipsChildren) { -
7404 // ### add to child index only if applicable -
7405 return; -
7406 } -
7407 if (d_ptr->scene) -
7408 d_ptr->scene->d_func()->index->addItem(this); -
7409} -
7410 -
7411/*! -
7412 \internal -
7413 -
7414 Removes this item from the scene's index. Called in conjunction -
7415 with addToIndex() to ensure the index bookkeeping is correct when -
7416 the item's position, transformation or shape changes. -
7417*/ -
7418void QGraphicsItem::removeFromIndex() -
7419{ -
7420 if (d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorClipsChildren) { -
7421 // ### remove from child index only if applicable -
7422 return; -
7423 } -
7424 if (d_ptr->scene) -
7425 d_ptr->scene->d_func()->index->removeItem(this); -
7426} -
7427 -
7428/*! -
7429 Prepares the item for a geometry change. Call this function before -
7430 changing the bounding rect of an item to keep QGraphicsScene's index up to -
7431 date. -
7432 -
7433 prepareGeometryChange() will call update() if this is necessary. -
7434 -
7435 Example: -
7436 -
7437 \snippet code/src_gui_graphicsview_qgraphicsitem.cpp 16 -
7438 -
7439 \sa boundingRect() -
7440*/ -
7441void QGraphicsItem::prepareGeometryChange() -
7442{ -
7443 if (d_ptr->inDestructor) -
7444 return; -
7445 if (d_ptr->scene) { -
7446 d_ptr->scene->d_func()->dirtyGrowingItemsBoundingRect = true; -
7447 d_ptr->geometryChanged = 1; -
7448 d_ptr->paintedViewBoundingRectsNeedRepaint = 1; -
7449 d_ptr->notifyBoundingRectChanged = !d_ptr->inSetPosHelper; -
7450 -
7451 QGraphicsScenePrivate *scenePrivate = d_ptr->scene->d_func(); -
7452 scenePrivate->index->prepareBoundingRectChange(this); -
7453 scenePrivate->markDirty(this, QRectF(), /*invalidateChildren=*/true, /*force=*/false, -
7454 /*ignoreOpacity=*/ false, /*removingItemFromScene=*/ false, -
7455 /*updateBoundingRect=*/true); -
7456 -
7457 // For compatibility reasons, we have to update the item's old geometry -
7458 // if someone is connected to the changed signal or the scene has no views. -
7459 // Note that this has to be done *after* markDirty to ensure that -
7460 // _q_processDirtyItems is called before _q_emitUpdated. -
7461 if (scenePrivate->isSignalConnected(scenePrivate->changedSignalIndex) -
7462 || scenePrivate->views.isEmpty()) { -
7463 if (d_ptr->hasTranslateOnlySceneTransform()) { -
7464 d_ptr->scene->update(boundingRect().translated(d_ptr->sceneTransform.dx(), -
7465 d_ptr->sceneTransform.dy())); -
7466 } else { -
7467 d_ptr->scene->update(d_ptr->sceneTransform.mapRect(boundingRect())); -
7468 } -
7469 } -
7470 } -
7471 -
7472 d_ptr->markParentDirty(/*updateBoundingRect=*/true); -
7473} -
7474 -
7475/*! -
7476 \internal -
7477 -
7478 Highlights \a item as selected. -
7479 -
7480 NOTE: This function is a duplicate of qt_graphicsItem_highlightSelected() in -
7481 qgraphicssvgitem.cpp! -
7482*/ -
7483static void qt_graphicsItem_highlightSelected( -
7484 QGraphicsItem *item, QPainter *painter, const QStyleOptionGraphicsItem *option) -
7485{ -
7486 const QRectF murect = painter->transform().mapRect(QRectF(0, 0, 1, 1)); -
7487 if (qFuzzyIsNull(qMax(murect.width(), murect.height()))) -
7488 return; -
7489 -
7490 const QRectF mbrect = painter->transform().mapRect(item->boundingRect()); -
7491 if (qMin(mbrect.width(), mbrect.height()) < qreal(1.0)) -
7492 return; -
7493 -
7494 qreal itemPenWidth; -
7495 switch (item->type()) { -
7496 case QGraphicsEllipseItem::Type: -
7497 itemPenWidth = static_cast<QGraphicsEllipseItem *>(item)->pen().widthF(); -
7498 break; -
7499 case QGraphicsPathItem::Type: -
7500 itemPenWidth = static_cast<QGraphicsPathItem *>(item)->pen().widthF(); -
7501 break; -
7502 case QGraphicsPolygonItem::Type: -
7503 itemPenWidth = static_cast<QGraphicsPolygonItem *>(item)->pen().widthF(); -
7504 break; -
7505 case QGraphicsRectItem::Type: -
7506 itemPenWidth = static_cast<QGraphicsRectItem *>(item)->pen().widthF(); -
7507 break; -
7508 case QGraphicsSimpleTextItem::Type: -
7509 itemPenWidth = static_cast<QGraphicsSimpleTextItem *>(item)->pen().widthF(); -
7510 break; -
7511 case QGraphicsLineItem::Type: -
7512 itemPenWidth = static_cast<QGraphicsLineItem *>(item)->pen().widthF(); -
7513 break; -
7514 default: -
7515 itemPenWidth = 1.0; -
7516 } -
7517 const qreal pad = itemPenWidth / 2; -
7518 -
7519 const qreal penWidth = 0; // cosmetic pen -
7520 -
7521 const QColor fgcolor = option->palette.windowText().color(); -
7522 const QColor bgcolor( // ensure good contrast against fgcolor -
7523 fgcolor.red() > 127 ? 0 : 255, -
7524 fgcolor.green() > 127 ? 0 : 255, -
7525 fgcolor.blue() > 127 ? 0 : 255); -
7526 -
7527 painter->setPen(QPen(bgcolor, penWidth, Qt::SolidLine)); -
7528 painter->setBrush(Qt::NoBrush); -
7529 painter->drawRect(item->boundingRect().adjusted(pad, pad, -pad, -pad)); -
7530 -
7531 painter->setPen(QPen(option->palette.windowText(), 0, Qt::DashLine)); -
7532 painter->setBrush(Qt::NoBrush); -
7533 painter->drawRect(item->boundingRect().adjusted(pad, pad, -pad, -pad)); -
7534} -
7535 -
7536/*! -
7537 \class QGraphicsObject -
7538 \brief The QGraphicsObject class provides a base class for all graphics items that -
7539 require signals, slots and properties. -
7540 \since 4.6 -
7541 \ingroup graphicsview-api -
7542 \inmodule QtWidgets -
7543 -
7544 The class extends a QGraphicsItem with QObject's signal/slot and property mechanisms. -
7545 It maps many of QGraphicsItem's basic setters and getters to properties and adds notification -
7546 signals for many of them. -
7547 -
7548 \section1 Parents and Children -
7549 -
7550 Each graphics object can be constructed with a parent item. This ensures that the -
7551 item will be destroyed when its parent item is destroyed. Although QGraphicsObject -
7552 inherits from both QObject and QGraphicsItem, you should use the functions provided -
7553 by QGraphicsItem, \e not QObject, to manage the relationships between parent and -
7554 child items. -
7555 -
7556 The relationships between items can be explored using the parentItem() and childItems() -
7557 functions. In the hierarchy of items in a scene, the parentObject() and parentWidget() -
7558 functions are the equivalent of the QWidget::parent() and QWidget::parentWidget() -
7559 functions for QWidget subclasses. -
7560 -
7561 \sa QGraphicsWidget -
7562*/ -
7563 -
7564/*! -
7565 Constructs a QGraphicsObject with \a parent. -
7566*/ -
7567QGraphicsObject::QGraphicsObject(QGraphicsItem *parent) -
7568 : QGraphicsItem(parent) -
7569{ -
7570 QGraphicsItem::d_ptr->isObject = true; -
7571} -
7572 -
7573/*! -
7574 \internal -
7575*/ -
7576QGraphicsObject::QGraphicsObject(QGraphicsItemPrivate &dd, QGraphicsItem *parent) -
7577 : QGraphicsItem(dd, parent) -
7578{ -
7579 QGraphicsItem::d_ptr->isObject = true; -
7580} -
7581 -
7582/*! -
7583 Destructor. -
7584*/ -
7585QGraphicsObject::~QGraphicsObject() -
7586{ -
7587} -
7588 -
7589/*! -
7590 \reimp -
7591*/ -
7592bool QGraphicsObject::event(QEvent *ev) -
7593{ -
7594 if (ev->type() == QEvent::StyleAnimationUpdate) { -
7595 update(); -
7596 return true; -
7597 } -
7598 return QObject::event(ev); -
7599} -
7600 -
7601#ifndef QT_NO_GESTURES -
7602/*! -
7603 Subscribes the graphics object to the given \a gesture with specific \a flags. -
7604 -
7605 \sa ungrabGesture(), QGestureEvent -
7606*/ -
7607void QGraphicsObject::grabGesture(Qt::GestureType gesture, Qt::GestureFlags flags) -
7608{ -
7609 bool contains = QGraphicsItem::d_ptr->gestureContext.contains(gesture); -
7610 QGraphicsItem::d_ptr->gestureContext.insert(gesture, flags); -
7611 if (!contains && QGraphicsItem::d_ptr->scene) -
7612 QGraphicsItem::d_ptr->scene->d_func()->grabGesture(this, gesture); -
7613} -
7614 -
7615/*! -
7616 Unsubscribes the graphics object from the given \a gesture. -
7617 -
7618 \sa grabGesture(), QGestureEvent -
7619*/ -
7620void QGraphicsObject::ungrabGesture(Qt::GestureType gesture) -
7621{ -
7622 if (QGraphicsItem::d_ptr->gestureContext.remove(gesture) && QGraphicsItem::d_ptr->scene) -
7623 QGraphicsItem::d_ptr->scene->d_func()->ungrabGesture(this, gesture); -
7624} -
7625#endif // QT_NO_GESTURES -
7626 -
7627/*! -
7628 Updates the item's micro focus. This is slot for convenience. -
7629 -
7630 \since 4.7 -
7631 -
7632 \sa QInputMethod -
7633*/ -
7634void QGraphicsObject::updateMicroFocus() -
7635{ -
7636 QGraphicsItem::updateMicroFocus(); -
7637} -
7638 -
7639void QGraphicsItemPrivate::children_append(QDeclarativeListProperty<QGraphicsObject> *list, QGraphicsObject *item) -
7640{ -
7641 if (item) { -
7642 QGraphicsObject *graphicsObject = static_cast<QGraphicsObject *>(list->object); -
7643 if (QGraphicsItemPrivate::get(graphicsObject)->sendParentChangeNotification) { -
7644 item->setParentItem(graphicsObject); -
7645 } else { -
7646 QGraphicsItemPrivate::get(item)->setParentItemHelper(graphicsObject, 0, 0); -
7647 } -
7648 } -
7649} -
7650 -
7651int QGraphicsItemPrivate::children_count(QDeclarativeListProperty<QGraphicsObject> *list) -
7652{ -
7653 QGraphicsItemPrivate *d = QGraphicsItemPrivate::get(static_cast<QGraphicsObject *>(list->object)); -
7654 return d->children.count(); -
7655} -
7656 -
7657QGraphicsObject *QGraphicsItemPrivate::children_at(QDeclarativeListProperty<QGraphicsObject> *list, int index) -
7658{ -
7659 QGraphicsItemPrivate *d = QGraphicsItemPrivate::get(static_cast<QGraphicsObject *>(list->object)); -
7660 if (index >= 0 && index < d->children.count()) -
7661 return d->children.at(index)->toGraphicsObject(); -
7662 else -
7663 return 0; -
7664} -
7665 -
7666void QGraphicsItemPrivate::children_clear(QDeclarativeListProperty<QGraphicsObject> *list) -
7667{ -
7668 QGraphicsItemPrivate *d = QGraphicsItemPrivate::get(static_cast<QGraphicsObject *>(list->object)); -
7669 int childCount = d->children.count(); -
7670 if (d->sendParentChangeNotification) { -
7671 for (int index = 0; index < childCount; index++) -
7672 d->children.at(0)->setParentItem(0); -
7673 } else { -
7674 for (int index = 0; index < childCount; index++) -
7675 QGraphicsItemPrivate::get(d->children.at(0))->setParentItemHelper(0, 0, 0); -
7676 } -
7677} -
7678 -
7679/*! -
7680 Returns a list of this item's children. -
7681 -
7682 The items are sorted by stacking order. This takes into account both the -
7683 items' insertion order and their Z-values. -
7684 -
7685*/ -
7686QDeclarativeListProperty<QGraphicsObject> QGraphicsItemPrivate::childrenList() -
7687{ -
7688 Q_Q(QGraphicsItem); -
7689 if (isObject) { -
7690 QGraphicsObject *that = static_cast<QGraphicsObject *>(q); -
7691 return QDeclarativeListProperty<QGraphicsObject>(that, &children, children_append, -
7692 children_count, children_at, children_clear); -
7693 } else { -
7694 //QGraphicsItem is not supported for this property -
7695 return QDeclarativeListProperty<QGraphicsObject>(); -
7696 } -
7697} -
7698 -
7699/*! -
7700 \internal -
7701 Returns the width of the item -
7702 Reimplemented by QGraphicsWidget -
7703*/ -
7704qreal QGraphicsItemPrivate::width() const -
7705{ -
7706 return 0; -
7707} -
7708 -
7709/*! -
7710 \internal -
7711 Set the width of the item -
7712 Reimplemented by QGraphicsWidget -
7713*/ -
7714void QGraphicsItemPrivate::setWidth(qreal w) -
7715{ -
7716 Q_UNUSED(w); -
7717} -
7718 -
7719/*! -
7720 \internal -
7721 Reset the width of the item -
7722 Reimplemented by QGraphicsWidget -
7723*/ -
7724void QGraphicsItemPrivate::resetWidth() -
7725{ -
7726} -
7727 -
7728/*! -
7729 \internal -
7730 Returns the height of the item -
7731 Reimplemented by QGraphicsWidget -
7732*/ -
7733qreal QGraphicsItemPrivate::height() const -
7734{ -
7735 return 0; -
7736} -
7737 -
7738/*! -
7739 \internal -
7740 Set the height of the item -
7741 Reimplemented by QGraphicsWidget -
7742*/ -
7743void QGraphicsItemPrivate::setHeight(qreal h) -
7744{ -
7745 Q_UNUSED(h); -
7746} -
7747 -
7748/*! -
7749 \internal -
7750 Reset the height of the item -
7751 Reimplemented by QGraphicsWidget -
7752*/ -
7753void QGraphicsItemPrivate::resetHeight() -
7754{ -
7755} -
7756 -
7757/*! -
7758 \property QGraphicsObject::children -
7759 \since 4.7 -
7760 \internal -
7761*/ -
7762 -
7763/*! -
7764 \property QGraphicsObject::width -
7765 \since 4.7 -
7766 \internal -
7767*/ -
7768 -
7769/*! -
7770 \property QGraphicsObject::height -
7771 \since 4.7 -
7772 \internal -
7773*/ -
7774 -
7775/*! -
7776 \property QGraphicsObject::parent -
7777 \brief the parent of the item -
7778 -
7779 \note The item's parent is set independently of the parent object returned -
7780 by QObject::parent(). -
7781 -
7782 \sa QGraphicsItem::setParentItem(), QGraphicsItem::parentObject() -
7783*/ -
7784 -
7785/*! -
7786 \property QGraphicsObject::opacity -
7787 \brief the opacity of the item -
7788 -
7789 \sa QGraphicsItem::setOpacity(), QGraphicsItem::opacity() -
7790*/ -
7791 -
7792/*! -
7793 \fn QGraphicsObject::opacityChanged() -
7794 -
7795 This signal gets emitted whenever the opacity of the item changes -
7796 -
7797 \sa QGraphicsItem::opacity() -
7798*/ -
7799 -
7800/*! -
7801 \fn QGraphicsObject::parentChanged() -
7802 -
7803 This signal gets emitted whenever the parent of the item changes -
7804*/ -
7805 -
7806/*! -
7807 \property QGraphicsObject::pos -
7808 \brief the position of the item -
7809 -
7810 Describes the items position. -
7811 -
7812 \sa QGraphicsItem::setPos(), QGraphicsItem::pos() -
7813*/ -
7814 -
7815/*! -
7816 \property QGraphicsObject::x -
7817 \brief the x position of the item -
7818 -
7819 Describes the items x position. -
7820 -
7821 \sa QGraphicsItem::setX(), setPos() -
7822*/ -
7823 -
7824/*! -
7825 \fn QGraphicsObject::xChanged() -
7826 -
7827 This signal gets emitted whenever the x position of the item changes -
7828 -
7829 \sa pos() -
7830*/ -
7831 -
7832/*! -
7833 \property QGraphicsObject::y -
7834 \brief the y position of the item -
7835 -
7836 Describes the items y position. -
7837 -
7838 \sa QGraphicsItem::setY(), setPos() -
7839*/ -
7840 -
7841/*! -
7842 \fn QGraphicsObject::yChanged() -
7843 -
7844 This signal gets emitted whenever the y position of the item changes. -
7845 -
7846 \sa pos() -
7847*/ -
7848 -
7849/*! -
7850 \property QGraphicsObject::z -
7851 \brief the z value of the item -
7852 -
7853 Describes the items z value. -
7854 -
7855 \sa QGraphicsItem::setZValue(), zValue() -
7856*/ -
7857 -
7858/*! -
7859 \fn QGraphicsObject::zChanged() -
7860 -
7861 This signal gets emitted whenever the z value of the item changes. -
7862 -
7863 \sa pos() -
7864*/ -
7865 -
7866/*! -
7867 \property QGraphicsObject::rotation -
7868 This property holds the rotation of the item in degrees. -
7869 -
7870 This specifies how many degrees to rotate the item around its transformOrigin. -
7871 The default rotation is 0 degrees (i.e. not rotated at all). -
7872*/ -
7873 -
7874/*! -
7875 \fn QGraphicsObject::rotationChanged() -
7876 -
7877 This signal gets emitted whenever the roation of the item changes. -
7878*/ -
7879 -
7880/*! -
7881 \property QGraphicsObject::scale -
7882 This property holds the scale of the item. -
7883 -
7884 A scale of less than 1 means the item will be displayed smaller than -
7885 normal, and a scale of greater than 1 means the item will be -
7886 displayed larger than normal. A negative scale means the item will -
7887 be mirrored. -
7888 -
7889 By default, items are displayed at a scale of 1 (i.e. at their -
7890 normal size). -
7891 -
7892 Scaling is from the item's transformOrigin. -
7893*/ -
7894 -
7895/*! -
7896 \fn void QGraphicsObject::scaleChanged() -
7897 -
7898 This signal is emitted when the scale of the item changes. -
7899*/ -
7900 -
7901 -
7902/*! -
7903 \property QGraphicsObject::enabled -
7904 \brief whether the item is enabled or not -
7905 -
7906 This property is declared in QGraphicsItem. -
7907 -
7908 By default, this property is true. -
7909 -
7910 \sa QGraphicsItem::isEnabled(), QGraphicsItem::setEnabled() -
7911*/ -
7912 -
7913/*! -
7914 \fn void QGraphicsObject::enabledChanged() -
7915 -
7916 This signal gets emitted whenever the item get's enabled or disabled. -
7917 -
7918 \sa isEnabled() -
7919*/ -
7920 -
7921/*! -
7922 \property QGraphicsObject::visible -
7923 \brief whether the item is visible or not -
7924 -
7925 This property is declared in QGraphicsItem. -
7926 -
7927 By default, this property is true. -
7928 -
7929 \sa QGraphicsItem::isVisible(), QGraphicsItem::setVisible() -
7930*/ -
7931 -
7932/*! -
7933 \fn QGraphicsObject::visibleChanged() -
7934 -
7935 This signal gets emitted whenever the visibility of the item changes -
7936 -
7937 \sa visible -
7938*/ -
7939 -
7940/*! -
7941 \fn const QObjectList &QGraphicsObject::children() const -
7942 \internal -
7943 -
7944 This function returns the same value as QObject::children(). It's -
7945 provided to differentiate between the obsolete member -
7946 QGraphicsItem::children() and QObject::children(). QGraphicsItem now -
7947 provides childItems() instead. -
7948*/ -
7949 -
7950/*! -
7951 \property QGraphicsObject::transformOriginPoint -
7952 \brief the transformation origin -
7953 -
7954 This property sets a specific point in the items coordiante system as the -
7955 origin for scale and rotation. -
7956 -
7957 \sa scale, rotation, QGraphicsItem::transformOriginPoint() -
7958*/ -
7959 -
7960/*! -
7961 \fn void QGraphicsObject::widthChanged() -
7962 \internal -
7963*/ -
7964 -
7965/*! -
7966 \fn void QGraphicsObject::heightChanged() -
7967 \internal -
7968*/ -
7969 -
7970/*! -
7971 -
7972 \fn QGraphicsObject::childrenChanged() -
7973 -
7974 This signal gets emitted whenever the children list changes -
7975 \internal -
7976*/ -
7977 -
7978/*! -
7979 \property QGraphicsObject::effect -
7980 \since 4.7 -
7981 \brief the effect attached to this item -
7982 -
7983 \sa QGraphicsItem::setGraphicsEffect(), QGraphicsItem::graphicsEffect() -
7984*/ -
7985 -
7986/*! -
7987 \class QAbstractGraphicsShapeItem -
7988 \brief The QAbstractGraphicsShapeItem class provides a common base for -
7989 all path items. -
7990 \since 4.2 -
7991 \ingroup graphicsview-api -
7992 \inmodule QtWidgets -
7993 -
7994 This class does not fully implement an item by itself; in particular, it -
7995 does not implement boundingRect() and paint(), which are inherited by -
7996 QGraphicsItem. -
7997 -
7998 You can subclass this item to provide a simple base implementation of -
7999 accessors for the item's pen and brush. -
8000 -
8001 \sa QGraphicsRectItem, QGraphicsEllipseItem, QGraphicsPathItem, -
8002 QGraphicsPolygonItem, QGraphicsTextItem, QGraphicsLineItem, -
8003 QGraphicsPixmapItem, {Graphics View Framework} -
8004*/ -
8005 -
8006class QAbstractGraphicsShapeItemPrivate : public QGraphicsItemPrivate -
8007{ -
8008 Q_DECLARE_PUBLIC(QAbstractGraphicsShapeItem) -
8009public: -
8010 -
8011 QBrush brush; -
8012 QPen pen; -
8013 -
8014 // Cached bounding rectangle -
8015 mutable QRectF boundingRect; -
8016}; -
8017 -
8018/*! -
8019 Constructs a QAbstractGraphicsShapeItem. \a parent is passed to -
8020 QGraphicsItem's constructor. -
8021*/ -
8022QAbstractGraphicsShapeItem::QAbstractGraphicsShapeItem(QGraphicsItem *parent) -
8023 : QGraphicsItem(*new QAbstractGraphicsShapeItemPrivate, parent) -
8024{ -
8025} -
8026 -
8027/*! -
8028 \internal -
8029*/ -
8030QAbstractGraphicsShapeItem::QAbstractGraphicsShapeItem(QAbstractGraphicsShapeItemPrivate &dd, QGraphicsItem *parent) -
8031 : QGraphicsItem(dd, parent) -
8032{ -
8033} -
8034 -
8035/*! -
8036 Destroys a QAbstractGraphicsShapeItem. -
8037*/ -
8038QAbstractGraphicsShapeItem::~QAbstractGraphicsShapeItem() -
8039{ -
8040} -
8041 -
8042/*! -
8043 Returns the item's pen. If no pen has been set, this function returns -
8044 QPen(), a default black solid line pen with 1 width. -
8045*/ -
8046QPen QAbstractGraphicsShapeItem::pen() const -
8047{ -
8048 Q_D(const QAbstractGraphicsShapeItem); -
8049 return d->pen; -
8050} -
8051 -
8052/*! -
8053 Sets the pen for this item to \a pen. -
8054 -
8055 The pen is used to draw the item's outline. -
8056 -
8057 \sa pen() -
8058*/ -
8059void QAbstractGraphicsShapeItem::setPen(const QPen &pen) -
8060{ -
8061 Q_D(QAbstractGraphicsShapeItem); -
8062 if (d->pen == pen) -
8063 return; -
8064 prepareGeometryChange(); -
8065 d->pen = pen; -
8066 d->boundingRect = QRectF(); -
8067 update(); -
8068} -
8069 -
8070/*! -
8071 Returns the item's brush, or an empty brush if no brush has been set. -
8072 -
8073 \sa setBrush() -
8074*/ -
8075QBrush QAbstractGraphicsShapeItem::brush() const -
8076{ -
8077 Q_D(const QAbstractGraphicsShapeItem); -
8078 return d->brush; -
8079} -
8080 -
8081/*! -
8082 Sets the item's brush to \a brush. -
8083 -
8084 The item's brush is used to fill the item. -
8085 -
8086 If you use a brush with a QGradient, the gradient -
8087 is relative to the item's coordinate system. -
8088 -
8089 \sa brush() -
8090*/ -
8091void QAbstractGraphicsShapeItem::setBrush(const QBrush &brush) -
8092{ -
8093 Q_D(QAbstractGraphicsShapeItem); -
8094 if (d->brush == brush) -
8095 return; -
8096 d->brush = brush; -
8097 update(); -
8098} -
8099 -
8100/*! -
8101 \reimp -
8102*/ -
8103bool QAbstractGraphicsShapeItem::isObscuredBy(const QGraphicsItem *item) const -
8104{ -
8105 return QGraphicsItem::isObscuredBy(item); -
8106} -
8107 -
8108/*! -
8109 \reimp -
8110*/ -
8111QPainterPath QAbstractGraphicsShapeItem::opaqueArea() const -
8112{ -
8113 Q_D(const QAbstractGraphicsShapeItem); -
8114 if (d->brush.isOpaque()) -
8115 return isClipped() ? clipPath() : shape(); -
8116 return QGraphicsItem::opaqueArea(); -
8117} -
8118 -
8119/*! -
8120 \class QGraphicsPathItem -
8121 \brief The QGraphicsPathItem class provides a path item that you -
8122 can add to a QGraphicsScene. -
8123 \since 4.2 -
8124 \ingroup graphicsview-api -
8125 \inmodule QtWidgets -
8126 -
8127 To set the item's path, pass a QPainterPath to QGraphicsPathItem's -
8128 constructor, or call the setPath() function. The path() function -
8129 returns the current path. -
8130 -
8131 \image graphicsview-pathitem.png -
8132 -
8133 QGraphicsPathItem uses the path to provide a reasonable -
8134 implementation of boundingRect(), shape(), and contains(). The -
8135 paint() function draws the path using the item's associated pen -
8136 and brush, which you can set by calling the setPen() and -
8137 setBrush() functions. -
8138 -
8139 \sa QGraphicsRectItem, QGraphicsEllipseItem, QGraphicsPolygonItem, -
8140 QGraphicsTextItem, QGraphicsLineItem, QGraphicsPixmapItem, {Graphics -
8141 View Framework} -
8142*/ -
8143 -
8144class QGraphicsPathItemPrivate : public QAbstractGraphicsShapeItemPrivate -
8145{ -
8146 Q_DECLARE_PUBLIC(QGraphicsPathItem) -
8147public: -
8148 QPainterPath path; -
8149}; -
8150 -
8151/*! -
8152 Constructs a QGraphicsPath item using \a path as the default path. \a -
8153 parent is passed to QAbstractGraphicsShapeItem's constructor. -
8154 -
8155 \sa QGraphicsScene::addItem() -
8156*/ -
8157QGraphicsPathItem::QGraphicsPathItem(const QPainterPath &path, -
8158 QGraphicsItem *parent) -
8159 : QAbstractGraphicsShapeItem(*new QGraphicsPathItemPrivate, parent) -
8160{ -
8161 if (!path.isEmpty()) -
8162 setPath(path); -
8163} -
8164 -
8165/*! -
8166 Constructs a QGraphicsPath. \a parent is passed to -
8167 QAbstractGraphicsShapeItem's constructor. -
8168 -
8169 \sa QGraphicsScene::addItem() -
8170*/ -
8171QGraphicsPathItem::QGraphicsPathItem(QGraphicsItem *parent) -
8172 : QAbstractGraphicsShapeItem(*new QGraphicsPathItemPrivate, parent) -
8173{ -
8174} -
8175 -
8176/*! -
8177 Destroys the QGraphicsPathItem. -
8178*/ -
8179QGraphicsPathItem::~QGraphicsPathItem() -
8180{ -
8181} -
8182 -
8183/*! -
8184 Returns the item's path as a QPainterPath. If no item has been set, an -
8185 empty QPainterPath is returned. -
8186 -
8187 \sa setPath() -
8188*/ -
8189QPainterPath QGraphicsPathItem::path() const -
8190{ -
8191 Q_D(const QGraphicsPathItem); -
8192 return d->path; -
8193} -
8194 -
8195/*! -
8196 Sets the item's path to be the given \a path. -
8197 -
8198 \sa path() -
8199*/ -
8200void QGraphicsPathItem::setPath(const QPainterPath &path) -
8201{ -
8202 Q_D(QGraphicsPathItem); -
8203 if (d->path == path) -
8204 return; -
8205 prepareGeometryChange(); -
8206 d->path = path; -
8207 d->boundingRect = QRectF(); -
8208 update(); -
8209} -
8210 -
8211/*! -
8212 \reimp -
8213*/ -
8214QRectF QGraphicsPathItem::boundingRect() const -
8215{ -
8216 Q_D(const QGraphicsPathItem); -
8217 if (d->boundingRect.isNull()) { -
8218 qreal pw = pen().style() == Qt::NoPen ? qreal(0) : pen().widthF(); -
8219 if (pw == 0.0) -
8220 d->boundingRect = d->path.controlPointRect(); -
8221 else { -
8222 d->boundingRect = shape().controlPointRect(); -
8223 } -
8224 } -
8225 return d->boundingRect; -
8226} -
8227 -
8228/*! -
8229 \reimp -
8230*/ -
8231QPainterPath QGraphicsPathItem::shape() const -
8232{ -
8233 Q_D(const QGraphicsPathItem); -
8234 return qt_graphicsItem_shapeFromPath(d->path, d->pen); -
8235} -
8236 -
8237/*! -
8238 \reimp -
8239*/ -
8240bool QGraphicsPathItem::contains(const QPointF &point) const -
8241{ -
8242 return QAbstractGraphicsShapeItem::contains(point); -
8243} -
8244 -
8245/*! -
8246 \reimp -
8247*/ -
8248void QGraphicsPathItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, -
8249 QWidget *widget) -
8250{ -
8251 Q_D(QGraphicsPathItem); -
8252 Q_UNUSED(widget); -
8253 painter->setPen(d->pen); -
8254 painter->setBrush(d->brush); -
8255 painter->drawPath(d->path); -
8256 -
8257 if (option->state & QStyle::State_Selected) -
8258 qt_graphicsItem_highlightSelected(this, painter, option); -
8259} -
8260 -
8261/*! -
8262 \reimp -
8263*/ -
8264bool QGraphicsPathItem::isObscuredBy(const QGraphicsItem *item) const -
8265{ -
8266 return QAbstractGraphicsShapeItem::isObscuredBy(item); -
8267} -
8268 -
8269/*! -
8270 \reimp -
8271*/ -
8272QPainterPath QGraphicsPathItem::opaqueArea() const -
8273{ -
8274 return QAbstractGraphicsShapeItem::opaqueArea(); -
8275} -
8276 -
8277/*! -
8278 \reimp -
8279*/ -
8280int QGraphicsPathItem::type() const -
8281{ -
8282 return Type; -
8283} -
8284 -
8285/*! -
8286 \internal -
8287*/ -
8288bool QGraphicsPathItem::supportsExtension(Extension extension) const -
8289{ -
8290 Q_UNUSED(extension); -
8291 return false; -
8292} -
8293 -
8294/*! -
8295 \internal -
8296*/ -
8297void QGraphicsPathItem::setExtension(Extension extension, const QVariant &variant) -
8298{ -
8299 Q_UNUSED(extension); -
8300 Q_UNUSED(variant); -
8301} -
8302 -
8303/*! -
8304 \internal -
8305*/ -
8306QVariant QGraphicsPathItem::extension(const QVariant &variant) const -
8307{ -
8308 Q_UNUSED(variant); -
8309 return QVariant(); -
8310} -
8311 -
8312/*! -
8313 \class QGraphicsRectItem -
8314 \brief The QGraphicsRectItem class provides a rectangle item that you -
8315 can add to a QGraphicsScene. -
8316 \since 4.2 -
8317 \ingroup graphicsview-api -
8318 \inmodule QtWidgets -
8319 -
8320 To set the item's rectangle, pass a QRectF to QGraphicsRectItem's -
8321 constructor, or call the setRect() function. The rect() function -
8322 returns the current rectangle. -
8323 -
8324 \image graphicsview-rectitem.png -
8325 -
8326 QGraphicsRectItem uses the rectangle and the pen width to provide -
8327 a reasonable implementation of boundingRect(), shape(), and -
8328 contains(). The paint() function draws the rectangle using the -
8329 item's associated pen and brush, which you can set by calling the -
8330 setPen() and setBrush() functions. -
8331 -
8332 \note The rendering of invalid rectangles, such as those with negative -
8333 widths or heights, is undefined. If you cannot be sure that you are -
8334 using valid rectangles (for example, if you are creating -
8335 rectangles using data from an unreliable source) then you should -
8336 use QRectF::normalized() to create normalized rectangles, and use -
8337 those instead. -
8338 -
8339 \sa QGraphicsPathItem, QGraphicsEllipseItem, QGraphicsPolygonItem, -
8340 QGraphicsTextItem, QGraphicsLineItem, QGraphicsPixmapItem, {Graphics -
8341 View Framework} -
8342*/ -
8343 -
8344class QGraphicsRectItemPrivate : public QAbstractGraphicsShapeItemPrivate -
8345{ -
8346 Q_DECLARE_PUBLIC(QGraphicsRectItem) -
8347public: -
8348 QRectF rect; -
8349}; -
8350 -
8351/*! -
8352 Constructs a QGraphicsRectItem, using \a rect as the default rectangle. -
8353 \a parent is passed to QAbstractGraphicsShapeItem's constructor. -
8354 -
8355 \sa QGraphicsScene::addItem() -
8356*/ -
8357QGraphicsRectItem::QGraphicsRectItem(const QRectF &rect, QGraphicsItem *parent) -
8358 : QAbstractGraphicsShapeItem(*new QGraphicsRectItemPrivate, parent) -
8359{ -
8360 setRect(rect); -
8361} -
8362 -
8363/*! -
8364 \fn QGraphicsRectItem::QGraphicsRectItem(qreal x, qreal y, qreal width, qreal height, -
8365 QGraphicsItem *parent) -
8366 -
8367 Constructs a QGraphicsRectItem with a default rectangle defined -
8368 by (\a x, \a y) and the given \a width and \a height. -
8369 -
8370 \a parent is passed to QAbstractGraphicsShapeItem's constructor. -
8371 -
8372 \sa QGraphicsScene::addItem() -
8373*/ -
8374QGraphicsRectItem::QGraphicsRectItem(qreal x, qreal y, qreal w, qreal h, -
8375 QGraphicsItem *parent) -
8376 : QAbstractGraphicsShapeItem(*new QGraphicsRectItemPrivate, parent) -
8377{ -
8378 setRect(QRectF(x, y, w, h)); -
8379} -
8380 -
8381/*! -
8382 Constructs a QGraphicsRectItem. \a parent is passed to -
8383 QAbstractGraphicsShapeItem's constructor. -
8384 -
8385 \sa QGraphicsScene::addItem() -
8386*/ -
8387QGraphicsRectItem::QGraphicsRectItem(QGraphicsItem *parent) -
8388 : QAbstractGraphicsShapeItem(*new QGraphicsRectItemPrivate, parent) -
8389{ -
8390} -
8391 -
8392/*! -
8393 Destroys the QGraphicsRectItem. -
8394*/ -
8395QGraphicsRectItem::~QGraphicsRectItem() -
8396{ -
8397} -
8398 -
8399/*! -
8400 Returns the item's rectangle. -
8401 -
8402 \sa setRect() -
8403*/ -
8404QRectF QGraphicsRectItem::rect() const -
8405{ -
8406 Q_D(const QGraphicsRectItem); -
8407 return d->rect; -
8408} -
8409 -
8410/*! -
8411 \fn void QGraphicsRectItem::setRect(const QRectF &rectangle) -
8412 -
8413 Sets the item's rectangle to be the given \a rectangle. -
8414 -
8415 \sa rect() -
8416*/ -
8417void QGraphicsRectItem::setRect(const QRectF &rect) -
8418{ -
8419 Q_D(QGraphicsRectItem); -
8420 if (d->rect == rect) -
8421 return; -
8422 prepareGeometryChange(); -
8423 d->rect = rect; -
8424 d->boundingRect = QRectF(); -
8425 update(); -
8426} -
8427 -
8428/*! -
8429 \fn void QGraphicsRectItem::setRect(qreal x, qreal y, qreal width, qreal height) -
8430 \fn void QGraphicsEllipseItem::setRect(qreal x, qreal y, qreal width, qreal height) -
8431 -
8432 Sets the item's rectangle to the rectangle defined by (\a x, \a y) -
8433 and the given \a width and \a height. -
8434 -
8435 This convenience function is equivalent to calling \c -
8436 {setRect(QRectF(x, y, width, height))} -
8437 -
8438 \sa rect() -
8439*/ -
8440 -
8441/*! -
8442 \reimp -
8443*/ -
8444QRectF QGraphicsRectItem::boundingRect() const -
8445{ -
8446 Q_D(const QGraphicsRectItem); -
8447 if (d->boundingRect.isNull()) { -
8448 qreal halfpw = pen().style() == Qt::NoPen ? qreal(0) : pen().widthF() / 2; -
8449 d->boundingRect = d->rect; -
8450 if (halfpw > 0.0) -
8451 d->boundingRect.adjust(-halfpw, -halfpw, halfpw, halfpw); -
8452 } -
8453 return d->boundingRect; -
8454} -
8455 -
8456/*! -
8457 \reimp -
8458*/ -
8459QPainterPath QGraphicsRectItem::shape() const -
8460{ -
8461 Q_D(const QGraphicsRectItem); -
8462 QPainterPath path; -
8463 path.addRect(d->rect); -
8464 return qt_graphicsItem_shapeFromPath(path, d->pen); -
8465} -
8466 -
8467/*! -
8468 \reimp -
8469*/ -
8470bool QGraphicsRectItem::contains(const QPointF &point) const -
8471{ -
8472 return QAbstractGraphicsShapeItem::contains(point); -
8473} -
8474 -
8475/*! -
8476 \reimp -
8477*/ -
8478void QGraphicsRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, -
8479 QWidget *widget) -
8480{ -
8481 Q_D(QGraphicsRectItem); -
8482 Q_UNUSED(widget); -
8483 painter->setPen(d->pen); -
8484 painter->setBrush(d->brush); -
8485 painter->drawRect(d->rect); -
8486 -
8487 if (option->state & QStyle::State_Selected) -
8488 qt_graphicsItem_highlightSelected(this, painter, option); -
8489} -
8490 -
8491/*! -
8492 \reimp -
8493*/ -
8494bool QGraphicsRectItem::isObscuredBy(const QGraphicsItem *item) const -
8495{ -
8496 return QAbstractGraphicsShapeItem::isObscuredBy(item); -
8497} -
8498 -
8499/*! -
8500 \reimp -
8501*/ -
8502QPainterPath QGraphicsRectItem::opaqueArea() const -
8503{ -
8504 return QAbstractGraphicsShapeItem::opaqueArea(); -
8505} -
8506 -
8507/*! -
8508 \reimp -
8509*/ -
8510int QGraphicsRectItem::type() const -
8511{ -
8512 return Type; -
8513} -
8514 -
8515/*! -
8516 \internal -
8517*/ -
8518bool QGraphicsRectItem::supportsExtension(Extension extension) const -
8519{ -
8520 Q_UNUSED(extension); -
8521 return false; -
8522} -
8523 -
8524/*! -
8525 \internal -
8526*/ -
8527void QGraphicsRectItem::setExtension(Extension extension, const QVariant &variant) -
8528{ -
8529 Q_UNUSED(extension); -
8530 Q_UNUSED(variant); -
8531} -
8532 -
8533/*! -
8534 \internal -
8535*/ -
8536QVariant QGraphicsRectItem::extension(const QVariant &variant) const -
8537{ -
8538 Q_UNUSED(variant); -
8539 return QVariant(); -
8540} -
8541 -
8542/*! -
8543 \class QGraphicsEllipseItem -
8544 \brief The QGraphicsEllipseItem class provides an ellipse item that you -
8545 can add to a QGraphicsScene. -
8546 \since 4.2 -
8547 \ingroup graphicsview-api -
8548 \inmodule QtWidgets -
8549 -
8550 QGraphicsEllipseItem respresents an ellipse with a fill and an outline, -
8551 and you can also use it for ellipse segments (see startAngle(), -
8552 spanAngle()). -
8553 -
8554 \table -
8555 \row -
8556 \li \inlineimage graphicsview-ellipseitem.png -
8557 \li \inlineimage graphicsview-ellipseitem-pie.png -
8558 \endtable -
8559 -
8560 To set the item's ellipse, pass a QRectF to QGraphicsEllipseItem's -
8561 constructor, or call setRect(). The rect() function returns the -
8562 current ellipse geometry. -
8563 -
8564 QGraphicsEllipseItem uses the rect and the pen width to provide a -
8565 reasonable implementation of boundingRect(), shape(), and contains(). The -
8566 paint() function draws the ellipse using the item's associated pen and -
8567 brush, which you can set by calling setPen() and setBrush(). -
8568 -
8569 \sa QGraphicsPathItem, QGraphicsRectItem, QGraphicsPolygonItem, -
8570 QGraphicsTextItem, QGraphicsLineItem, QGraphicsPixmapItem, {Graphics -
8571 View Framework} -
8572*/ -
8573 -
8574class QGraphicsEllipseItemPrivate : public QAbstractGraphicsShapeItemPrivate -
8575{ -
8576 Q_DECLARE_PUBLIC(QGraphicsEllipseItem) -
8577public: -
8578 inline QGraphicsEllipseItemPrivate() -
8579 : startAngle(0), spanAngle(360 * 16) -
8580 { } -
8581 -
8582 QRectF rect; -
8583 int startAngle; -
8584 int spanAngle; -
8585}; -
8586 -
8587/*! -
8588 Constructs a QGraphicsEllipseItem using \a rect as the default rectangle. -
8589 \a parent is passed to QAbstractGraphicsShapeItem's constructor. -
8590 -
8591 \sa QGraphicsScene::addItem() -
8592*/ -
8593QGraphicsEllipseItem::QGraphicsEllipseItem(const QRectF &rect, QGraphicsItem *parent) -
8594 : QAbstractGraphicsShapeItem(*new QGraphicsEllipseItemPrivate, parent) -
8595{ -
8596 setRect(rect); -
8597} -
8598 -
8599/*! -
8600 \fn QGraphicsEllipseItem::QGraphicsEllipseItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent) -
8601 \since 4.3 -
8602 -
8603 Constructs a QGraphicsEllipseItem using the rectangle defined by (\a x, \a -
8604 y) and the given \a width and \a height, as the default rectangle. \a -
8605 parent is passed to QAbstractGraphicsShapeItem's constructor. -
8606 -
8607 \sa QGraphicsScene::addItem() -
8608*/ -
8609QGraphicsEllipseItem::QGraphicsEllipseItem(qreal x, qreal y, qreal w, qreal h, -
8610 QGraphicsItem *parent) -
8611 : QAbstractGraphicsShapeItem(*new QGraphicsEllipseItemPrivate, parent) -
8612{ -
8613 setRect(x,y,w,h); -
8614} -
8615 -
8616 -
8617 -
8618/*! -
8619 Constructs a QGraphicsEllipseItem. \a parent is passed to -
8620 QAbstractGraphicsShapeItem's constructor. -
8621 -
8622 \sa QGraphicsScene::addItem() -
8623*/ -
8624QGraphicsEllipseItem::QGraphicsEllipseItem(QGraphicsItem *parent) -
8625 : QAbstractGraphicsShapeItem(*new QGraphicsEllipseItemPrivate, parent) -
8626{ -
8627} -
8628 -
8629/*! -
8630 Destroys the QGraphicsEllipseItem. -
8631*/ -
8632QGraphicsEllipseItem::~QGraphicsEllipseItem() -
8633{ -
8634} -
8635 -
8636/*! -
8637 Returns the item's ellipse geometry as a QRectF. -
8638 -
8639 \sa setRect(), QPainter::drawEllipse() -
8640*/ -
8641QRectF QGraphicsEllipseItem::rect() const -
8642{ -
8643 Q_D(const QGraphicsEllipseItem); -
8644 return d->rect; -
8645} -
8646 -
8647/*! -
8648 Sets the item's ellipse geometry to \a rect. The rectangle's left edge -
8649 defines the left edge of the ellipse, and the rectangle's top edge -
8650 describes the top of the ellipse. The height and width of the rectangle -
8651 describe the height and width of the ellipse. -
8652 -
8653 \sa rect(), QPainter::drawEllipse() -
8654*/ -
8655void QGraphicsEllipseItem::setRect(const QRectF &rect) -
8656{ -
8657 Q_D(QGraphicsEllipseItem); -
8658 if (d->rect == rect) -
8659 return; -
8660 prepareGeometryChange(); -
8661 d->rect = rect; -
8662 d->boundingRect = QRectF(); -
8663 update(); -
8664} -
8665 -
8666/*! -
8667 Returns the start angle for an ellipse segment in 16ths of a degree. This -
8668 angle is used together with spanAngle() for representing an ellipse -
8669 segment (a pie). By default, the start angle is 0. -
8670 -
8671 \sa setStartAngle(), spanAngle() -
8672*/ -
8673int QGraphicsEllipseItem::startAngle() const -
8674{ -
8675 Q_D(const QGraphicsEllipseItem); -
8676 return d->startAngle; -
8677} -
8678 -
8679/*! -
8680 Sets the start angle for an ellipse segment to \a angle, which is in 16ths -
8681 of a degree. This angle is used together with spanAngle() for representing -
8682 an ellipse segment (a pie). By default, the start angle is 0. -
8683 -
8684 \sa startAngle(), setSpanAngle(), QPainter::drawPie() -
8685*/ -
8686void QGraphicsEllipseItem::setStartAngle(int angle) -
8687{ -
8688 Q_D(QGraphicsEllipseItem); -
8689 if (angle != d->startAngle) { -
8690 prepareGeometryChange(); -
8691 d->boundingRect = QRectF(); -
8692 d->startAngle = angle; -
8693 update(); -
8694 } -
8695} -
8696 -
8697/*! -
8698 Returns the span angle of an ellipse segment in 16ths of a degree. This -
8699 angle is used together with startAngle() for representing an ellipse -
8700 segment (a pie). By default, this function returns 5760 (360 * 16, a full -
8701 ellipse). -
8702 -
8703 \sa setSpanAngle(), startAngle() -
8704*/ -
8705int QGraphicsEllipseItem::spanAngle() const -
8706{ -
8707 Q_D(const QGraphicsEllipseItem); -
8708 return d->spanAngle; -
8709} -
8710 -
8711/*! -
8712 Sets the span angle for an ellipse segment to \a angle, which is in 16ths -
8713 of a degree. This angle is used together with startAngle() to represent an -
8714 ellipse segment (a pie). By default, the span angle is 5760 (360 * 16, a -
8715 full ellipse). -
8716 -
8717 \sa spanAngle(), setStartAngle(), QPainter::drawPie() -
8718*/ -
8719void QGraphicsEllipseItem::setSpanAngle(int angle) -
8720{ -
8721 Q_D(QGraphicsEllipseItem); -
8722 if (angle != d->spanAngle) { -
8723 prepareGeometryChange(); -
8724 d->boundingRect = QRectF(); -
8725 d->spanAngle = angle; -
8726 update(); -
8727 } -
8728} -
8729 -
8730/*! -
8731 \reimp -
8732*/ -
8733QRectF QGraphicsEllipseItem::boundingRect() const -
8734{ -
8735 Q_D(const QGraphicsEllipseItem); -
8736 if (d->boundingRect.isNull()) { -
8737 qreal pw = pen().style() == Qt::NoPen ? qreal(0) : pen().widthF(); -
8738 if (pw == 0.0 && d->spanAngle == 360 * 16) -
8739 d->boundingRect = d->rect; -
8740 else -
8741 d->boundingRect = shape().controlPointRect(); -
8742 } -
8743 return d->boundingRect; -
8744} -
8745 -
8746/*! -
8747 \reimp -
8748*/ -
8749QPainterPath QGraphicsEllipseItem::shape() const -
8750{ -
8751 Q_D(const QGraphicsEllipseItem); -
8752 QPainterPath path; -
8753 if (d->rect.isNull()) -
8754 return path; -
8755 if (d->spanAngle != 360 * 16) { -
8756 path.moveTo(d->rect.center()); -
8757 path.arcTo(d->rect, d->startAngle / 16.0, d->spanAngle / 16.0); -
8758 } else { -
8759 path.addEllipse(d->rect); -
8760 } -
8761 -
8762 return qt_graphicsItem_shapeFromPath(path, d->pen); -
8763} -
8764 -
8765/*! -
8766 \reimp -
8767*/ -
8768bool QGraphicsEllipseItem::contains(const QPointF &point) const -
8769{ -
8770 return QAbstractGraphicsShapeItem::contains(point); -
8771} -
8772 -
8773/*! -
8774 \reimp -
8775*/ -
8776void QGraphicsEllipseItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, -
8777 QWidget *widget) -
8778{ -
8779 Q_D(QGraphicsEllipseItem); -
8780 Q_UNUSED(widget); -
8781 painter->setPen(d->pen); -
8782 painter->setBrush(d->brush); -
8783 if ((d->spanAngle != 0) && (qAbs(d->spanAngle) % (360 * 16) == 0)) -
8784 painter->drawEllipse(d->rect); -
8785 else -
8786 painter->drawPie(d->rect, d->startAngle, d->spanAngle); -
8787 -
8788 if (option->state & QStyle::State_Selected) -
8789 qt_graphicsItem_highlightSelected(this, painter, option); -
8790} -
8791 -
8792/*! -
8793 \reimp -
8794*/ -
8795bool QGraphicsEllipseItem::isObscuredBy(const QGraphicsItem *item) const -
8796{ -
8797 return QAbstractGraphicsShapeItem::isObscuredBy(item); -
8798} -
8799 -
8800/*! -
8801 \reimp -
8802*/ -
8803QPainterPath QGraphicsEllipseItem::opaqueArea() const -
8804{ -
8805 return QAbstractGraphicsShapeItem::opaqueArea(); -
8806} -
8807 -
8808/*! -
8809 \reimp -
8810*/ -
8811int QGraphicsEllipseItem::type() const -
8812{ -
8813 return Type; -
8814} -
8815 -
8816 -
8817/*! -
8818 \internal -
8819*/ -
8820bool QGraphicsEllipseItem::supportsExtension(Extension extension) const -
8821{ -
8822 Q_UNUSED(extension); -
8823 return false; -
8824} -
8825 -
8826/*! -
8827 \internal -
8828*/ -
8829void QGraphicsEllipseItem::setExtension(Extension extension, const QVariant &variant) -
8830{ -
8831 Q_UNUSED(extension); -
8832 Q_UNUSED(variant); -
8833} -
8834 -
8835/*! -
8836 \internal -
8837*/ -
8838QVariant QGraphicsEllipseItem::extension(const QVariant &variant) const -
8839{ -
8840 Q_UNUSED(variant); -
8841 return QVariant(); -
8842} -
8843 -
8844/*! -
8845 \class QGraphicsPolygonItem -
8846 \brief The QGraphicsPolygonItem class provides a polygon item that you -
8847 can add to a QGraphicsScene. -
8848 \since 4.2 -
8849 \ingroup graphicsview-api -
8850 \inmodule QtWidgets -
8851 -
8852 To set the item's polygon, pass a QPolygonF to -
8853 QGraphicsPolygonItem's constructor, or call the setPolygon() -
8854 function. The polygon() function returns the current polygon. -
8855 -
8856 \image graphicsview-polygonitem.png -
8857 -
8858 QGraphicsPolygonItem uses the polygon and the pen width to provide -
8859 a reasonable implementation of boundingRect(), shape(), and -
8860 contains(). The paint() function draws the polygon using the -
8861 item's associated pen and brush, which you can set by calling the -
8862 setPen() and setBrush() functions. -
8863 -
8864 \sa QGraphicsPathItem, QGraphicsRectItem, QGraphicsEllipseItem, -
8865 QGraphicsTextItem, QGraphicsLineItem, QGraphicsPixmapItem, {Graphics -
8866 View Framework} -
8867*/ -
8868 -
8869class QGraphicsPolygonItemPrivate : public QAbstractGraphicsShapeItemPrivate -
8870{ -
8871 Q_DECLARE_PUBLIC(QGraphicsPolygonItem) -
8872public: -
8873 inline QGraphicsPolygonItemPrivate() -
8874 : fillRule(Qt::OddEvenFill) -
8875 { } -
8876 -
8877 QPolygonF polygon; -
8878 Qt::FillRule fillRule; -
8879}; -
8880 -
8881/*! -
8882 Constructs a QGraphicsPolygonItem with \a polygon as the default -
8883 polygon. \a parent is passed to QAbstractGraphicsShapeItem's constructor. -
8884 -
8885 \sa QGraphicsScene::addItem() -
8886*/ -
8887QGraphicsPolygonItem::QGraphicsPolygonItem(const QPolygonF &polygon, QGraphicsItem *parent) -
8888 : QAbstractGraphicsShapeItem(*new QGraphicsPolygonItemPrivate, parent) -
8889{ -
8890 setPolygon(polygon); -
8891} -
8892 -
8893/*! -
8894 Constructs a QGraphicsPolygonItem. \a parent is passed to -
8895 QAbstractGraphicsShapeItem's constructor. -
8896 -
8897 \sa QGraphicsScene::addItem() -
8898*/ -
8899QGraphicsPolygonItem::QGraphicsPolygonItem(QGraphicsItem *parent) -
8900 : QAbstractGraphicsShapeItem(*new QGraphicsPolygonItemPrivate, parent) -
8901{ -
8902} -
8903 -
8904/*! -
8905 Destroys the QGraphicsPolygonItem. -
8906*/ -
8907QGraphicsPolygonItem::~QGraphicsPolygonItem() -
8908{ -
8909} -
8910 -
8911/*! -
8912 Returns the item's polygon, or an empty polygon if no polygon -
8913 has been set. -
8914 -
8915 \sa setPolygon() -
8916*/ -
8917QPolygonF QGraphicsPolygonItem::polygon() const -
8918{ -
8919 Q_D(const QGraphicsPolygonItem); -
8920 return d->polygon; -
8921} -
8922 -
8923/*! -
8924 Sets the item's polygon to be the given \a polygon. -
8925 -
8926 \sa polygon() -
8927*/ -
8928void QGraphicsPolygonItem::setPolygon(const QPolygonF &polygon) -
8929{ -
8930 Q_D(QGraphicsPolygonItem); -
8931 if (d->polygon == polygon) -
8932 return; -
8933 prepareGeometryChange(); -
8934 d->polygon = polygon; -
8935 d->boundingRect = QRectF(); -
8936 update(); -
8937} -
8938 -
8939/*! -
8940 Returns the fill rule of the polygon. The default fill rule is -
8941 Qt::OddEvenFill. -
8942 -
8943 \sa setFillRule(), QPainterPath::fillRule(), QPainter::drawPolygon() -
8944*/ -
8945Qt::FillRule QGraphicsPolygonItem::fillRule() const -
8946{ -
8947 Q_D(const QGraphicsPolygonItem); -
8948 return d->fillRule; -
8949} -
8950 -
8951/*! -
8952 Sets the fill rule of the polygon to \a rule. The default fill rule is -
8953 Qt::OddEvenFill. -
8954 -
8955 \sa fillRule(), QPainterPath::fillRule(), QPainter::drawPolygon() -
8956*/ -
8957void QGraphicsPolygonItem::setFillRule(Qt::FillRule rule) -
8958{ -
8959 Q_D(QGraphicsPolygonItem); -
8960 if (rule != d->fillRule) { -
8961 d->fillRule = rule; -
8962 update(); -
8963 } -
8964} -
8965 -
8966/*! -
8967 \reimp -
8968*/ -
8969QRectF QGraphicsPolygonItem::boundingRect() const -
8970{ -
8971 Q_D(const QGraphicsPolygonItem); -
8972 if (d->boundingRect.isNull()) { -
8973 qreal pw = pen().style() == Qt::NoPen ? qreal(0) : pen().widthF(); -
8974 if (pw == 0.0) -
8975 d->boundingRect = d->polygon.boundingRect(); -
8976 else -
8977 d->boundingRect = shape().controlPointRect(); -
8978 } -
8979 return d->boundingRect; -
8980} -
8981 -
8982/*! -
8983 \reimp -
8984*/ -
8985QPainterPath QGraphicsPolygonItem::shape() const -
8986{ -
8987 Q_D(const QGraphicsPolygonItem); -
8988 QPainterPath path; -
8989 path.addPolygon(d->polygon); -
8990 return qt_graphicsItem_shapeFromPath(path, d->pen); -
8991} -
8992 -
8993/*! -
8994 \reimp -
8995*/ -
8996bool QGraphicsPolygonItem::contains(const QPointF &point) const -
8997{ -
8998 return QAbstractGraphicsShapeItem::contains(point); -
8999} -
9000 -
9001/*! -
9002 \reimp -
9003*/ -
9004void QGraphicsPolygonItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) -
9005{ -
9006 Q_D(QGraphicsPolygonItem); -
9007 Q_UNUSED(widget); -
9008 painter->setPen(d->pen); -
9009 painter->setBrush(d->brush); -
9010 painter->drawPolygon(d->polygon, d->fillRule); -
9011 -
9012 if (option->state & QStyle::State_Selected) -
9013 qt_graphicsItem_highlightSelected(this, painter, option); -
9014} -
9015 -
9016/*! -
9017 \reimp -
9018*/ -
9019bool QGraphicsPolygonItem::isObscuredBy(const QGraphicsItem *item) const -
9020{ -
9021 return QAbstractGraphicsShapeItem::isObscuredBy(item); -
9022} -
9023 -
9024/*! -
9025 \reimp -
9026*/ -
9027QPainterPath QGraphicsPolygonItem::opaqueArea() const -
9028{ -
9029 return QAbstractGraphicsShapeItem::opaqueArea(); -
9030} -
9031 -
9032/*! -
9033 \reimp -
9034*/ -
9035int QGraphicsPolygonItem::type() const -
9036{ -
9037 return Type; -
9038} -
9039 -
9040/*! -
9041 \internal -
9042*/ -
9043bool QGraphicsPolygonItem::supportsExtension(Extension extension) const -
9044{ -
9045 Q_UNUSED(extension); -
9046 return false; -
9047} -
9048 -
9049/*! -
9050 \internal -
9051*/ -
9052void QGraphicsPolygonItem::setExtension(Extension extension, const QVariant &variant) -
9053{ -
9054 Q_UNUSED(extension); -
9055 Q_UNUSED(variant); -
9056} -
9057 -
9058/*! -
9059 \internal -
9060*/ -
9061QVariant QGraphicsPolygonItem::extension(const QVariant &variant) const -
9062{ -
9063 Q_UNUSED(variant); -
9064 return QVariant(); -
9065} -
9066 -
9067/*! -
9068 \class QGraphicsLineItem -
9069 \brief The QGraphicsLineItem class provides a line item that you can add to a -
9070 QGraphicsScene. -
9071 \since 4.2 -
9072 \ingroup graphicsview-api -
9073 \inmodule QtWidgets -
9074 -
9075 To set the item's line, pass a QLineF to QGraphicsLineItem's -
9076 constructor, or call the setLine() function. The line() function -
9077 returns the current line. By default the line is black with a -
9078 width of 0, but you can change this by calling setPen(). -
9079 -
9080 \image graphicsview-lineitem.png -
9081 -
9082 QGraphicsLineItem uses the line and the pen width to provide a reasonable -
9083 implementation of boundingRect(), shape(), and contains(). The paint() -
9084 function draws the line using the item's associated pen. -
9085 -
9086 \sa QGraphicsPathItem, QGraphicsRectItem, QGraphicsEllipseItem, -
9087 QGraphicsTextItem, QGraphicsPolygonItem, QGraphicsPixmapItem, -
9088 {Graphics View Framework} -
9089*/ -
9090 -
9091class QGraphicsLineItemPrivate : public QGraphicsItemPrivate -
9092{ -
9093 Q_DECLARE_PUBLIC(QGraphicsLineItem) -
9094public: -
9095 QLineF line; -
9096 QPen pen; -
9097}; -
9098 -
9099/*! -
9100 Constructs a QGraphicsLineItem, using \a line as the default line. \a -
9101 parent is passed to QGraphicsItem's constructor. -
9102 -
9103 \sa QGraphicsScene::addItem() -
9104*/ -
9105QGraphicsLineItem::QGraphicsLineItem(const QLineF &line, QGraphicsItem *parent) -
9106 : QGraphicsItem(*new QGraphicsLineItemPrivate, parent) -
9107{ -
9108 setLine(line); -
9109} -
9110 -
9111/*! -
9112 Constructs a QGraphicsLineItem, using the line between (\a x1, \a y1) and -
9113 (\a x2, \a y2) as the default line. \a parent is passed to -
9114 QGraphicsItem's constructor. -
9115 -
9116 \sa QGraphicsScene::addItem() -
9117*/ -
9118QGraphicsLineItem::QGraphicsLineItem(qreal x1, qreal y1, qreal x2, qreal y2, QGraphicsItem *parent) -
9119 : QGraphicsItem(*new QGraphicsLineItemPrivate, parent) -
9120{ -
9121 setLine(x1, y1, x2, y2); -
9122} -
9123 -
9124 -
9125 -
9126/*! -
9127 Constructs a QGraphicsLineItem. \a parent is passed to QGraphicsItem's -
9128 constructor. -
9129 -
9130 \sa QGraphicsScene::addItem() -
9131*/ -
9132QGraphicsLineItem::QGraphicsLineItem(QGraphicsItem *parent) -
9133 : QGraphicsItem(*new QGraphicsLineItemPrivate, parent) -
9134{ -
9135} -
9136 -
9137/*! -
9138 Destroys the QGraphicsLineItem. -
9139*/ -
9140QGraphicsLineItem::~QGraphicsLineItem() -
9141{ -
9142} -
9143 -
9144/*! -
9145 Returns the item's pen, or a black solid 0-width pen if no pen has -
9146 been set. -
9147 -
9148 \sa setPen() -
9149*/ -
9150QPen QGraphicsLineItem::pen() const -
9151{ -
9152 Q_D(const QGraphicsLineItem); -
9153 return d->pen; -
9154} -
9155 -
9156/*! -
9157 Sets the item's pen to \a pen. If no pen is set, the line will be painted -
9158 using a black solid 0-width pen. -
9159 -
9160 \sa pen() -
9161*/ -
9162void QGraphicsLineItem::setPen(const QPen &pen) -
9163{ -
9164 Q_D(QGraphicsLineItem); -
9165 if (d->pen == pen) -
9166 return; -
9167 prepareGeometryChange(); -
9168 d->pen = pen; -
9169 update(); -
9170} -
9171 -
9172/*! -
9173 Returns the item's line, or a null line if no line has been set. -
9174 -
9175 \sa setLine() -
9176*/ -
9177QLineF QGraphicsLineItem::line() const -
9178{ -
9179 Q_D(const QGraphicsLineItem); -
9180 return d->line; -
9181} -
9182 -
9183/*! -
9184 Sets the item's line to be the given \a line. -
9185 -
9186 \sa line() -
9187*/ -
9188void QGraphicsLineItem::setLine(const QLineF &line) -
9189{ -
9190 Q_D(QGraphicsLineItem); -
9191 if (d->line == line) -
9192 return; -
9193 prepareGeometryChange(); -
9194 d->line = line; -
9195 update(); -
9196} -
9197 -
9198/*! -
9199 \fn void QGraphicsLineItem::setLine(qreal x1, qreal y1, qreal x2, qreal y2) -
9200 \overload -
9201 -
9202 Sets the item's line to be the line between (\a x1, \a y1) and (\a -
9203 x2, \a y2). -
9204 -
9205 This is the same as calling \c {setLine(QLineF(x1, y1, x2, y2))}. -
9206*/ -
9207 -
9208/*! -
9209 \reimp -
9210*/ -
9211QRectF QGraphicsLineItem::boundingRect() const -
9212{ -
9213 Q_D(const QGraphicsLineItem); -
9214 if (d->pen.widthF() == 0.0) { -
9215 const qreal x1 = d->line.p1().x(); -
9216 const qreal x2 = d->line.p2().x(); -
9217 const qreal y1 = d->line.p1().y(); -
9218 const qreal y2 = d->line.p2().y(); -
9219 qreal lx = qMin(x1, x2); -
9220 qreal rx = qMax(x1, x2); -
9221 qreal ty = qMin(y1, y2); -
9222 qreal by = qMax(y1, y2); -
9223 return QRectF(lx, ty, rx - lx, by - ty); -
9224 } -
9225 return shape().controlPointRect(); -
9226} -
9227 -
9228/*! -
9229 \reimp -
9230*/ -
9231QPainterPath QGraphicsLineItem::shape() const -
9232{ -
9233 Q_D(const QGraphicsLineItem); -
9234 QPainterPath path; -
9235 if (d->line == QLineF()) -
9236 return path; -
9237 -
9238 path.moveTo(d->line.p1()); -
9239 path.lineTo(d->line.p2()); -
9240 return qt_graphicsItem_shapeFromPath(path, d->pen); -
9241} -
9242 -
9243/*! -
9244 \reimp -
9245*/ -
9246bool QGraphicsLineItem::contains(const QPointF &point) const -
9247{ -
9248 return QGraphicsItem::contains(point); -
9249} -
9250 -
9251/*! -
9252 \reimp -
9253*/ -
9254void QGraphicsLineItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) -
9255{ -
9256 Q_D(QGraphicsLineItem); -
9257 Q_UNUSED(widget); -
9258 painter->setPen(d->pen); -
9259 painter->drawLine(d->line); -
9260 -
9261 if (option->state & QStyle::State_Selected) -
9262 qt_graphicsItem_highlightSelected(this, painter, option); -
9263} -
9264 -
9265/*! -
9266 \reimp -
9267*/ -
9268bool QGraphicsLineItem::isObscuredBy(const QGraphicsItem *item) const -
9269{ -
9270 return QGraphicsItem::isObscuredBy(item); -
9271} -
9272 -
9273/*! -
9274 \reimp -
9275*/ -
9276QPainterPath QGraphicsLineItem::opaqueArea() const -
9277{ -
9278 return QGraphicsItem::opaqueArea(); -
9279} -
9280 -
9281/*! -
9282 \reimp -
9283*/ -
9284int QGraphicsLineItem::type() const -
9285{ -
9286 return Type; -
9287} -
9288 -
9289/*! -
9290 \internal -
9291*/ -
9292bool QGraphicsLineItem::supportsExtension(Extension extension) const -
9293{ -
9294 Q_UNUSED(extension); -
9295 return false; -
9296} -
9297 -
9298/*! -
9299 \internal -
9300*/ -
9301void QGraphicsLineItem::setExtension(Extension extension, const QVariant &variant) -
9302{ -
9303 Q_UNUSED(extension); -
9304 Q_UNUSED(variant); -
9305} -
9306 -
9307/*! -
9308 \internal -
9309*/ -
9310QVariant QGraphicsLineItem::extension(const QVariant &variant) const -
9311{ -
9312 Q_UNUSED(variant); -
9313 return QVariant(); -
9314} -
9315 -
9316/*! -
9317 \class QGraphicsPixmapItem -
9318 \brief The QGraphicsPixmapItem class provides a pixmap item that you can add to -
9319 a QGraphicsScene. -
9320 \since 4.2 -
9321 \ingroup graphicsview-api -
9322 \inmodule QtWidgets -
9323 -
9324 To set the item's pixmap, pass a QPixmap to QGraphicsPixmapItem's -
9325 constructor, or call the setPixmap() function. The pixmap() -
9326 function returns the current pixmap. -
9327 -
9328 QGraphicsPixmapItem uses pixmap's optional alpha mask to provide a -
9329 reasonable implementation of boundingRect(), shape(), and contains(). -
9330 -
9331 \image graphicsview-pixmapitem.png -
9332 -
9333 The pixmap is drawn at the item's (0, 0) coordinate, as returned by -
9334 offset(). You can change the drawing offset by calling setOffset(). -
9335 -
9336 You can set the pixmap's transformation mode by calling -
9337 setTransformationMode(). By default, Qt::FastTransformation is used, which -
9338 provides fast, non-smooth scaling. Qt::SmoothTransformation enables -
9339 QPainter::SmoothPixmapTransform on the painter, and the quality depends on -
9340 the platform and viewport. The result is usually not as good as calling -
9341 QPixmap::scale() directly. Call transformationMode() to get the current -
9342 transformation mode for the item. -
9343 -
9344 \sa QGraphicsPathItem, QGraphicsRectItem, QGraphicsEllipseItem, -
9345 QGraphicsTextItem, QGraphicsPolygonItem, QGraphicsLineItem, -
9346 {Graphics View Framework} -
9347*/ -
9348 -
9349/*! -
9350 \enum QGraphicsPixmapItem::ShapeMode -
9351 -
9352 This enum describes how QGraphicsPixmapItem calculates its shape and -
9353 opaque area. -
9354 -
9355 The default value is MaskShape. -
9356 -
9357 \value MaskShape The shape is determined by calling QPixmap::mask(). -
9358 This shape includes only the opaque pixels of the pixmap. -
9359 Because the shape is more complex, however, it can be slower than the other modes, -
9360 and uses more memory. -
9361 -
9362 \value BoundingRectShape The shape is determined by tracing the outline of -
9363 the pixmap. This is the fastest shape mode, but it does not take into account -
9364 any transparent areas on the pixmap. -
9365 -
9366 \value HeuristicMaskShape The shape is determine by calling -
9367 QPixmap::createHeuristicMask(). The performance and memory consumption -
9368 is similar to MaskShape. -
9369*/ -
9370extern QPainterPath qt_regionToPath(const QRegion &region); -
9371 -
9372class QGraphicsPixmapItemPrivate : public QGraphicsItemPrivate -
9373{ -
9374 Q_DECLARE_PUBLIC(QGraphicsPixmapItem) -
9375public: -
9376 QGraphicsPixmapItemPrivate() -
9377 : transformationMode(Qt::FastTransformation), -
9378 shapeMode(QGraphicsPixmapItem::MaskShape), -
9379 hasShape(false) -
9380 {} -
9381 -
9382 QPixmap pixmap; -
9383 Qt::TransformationMode transformationMode; -
9384 QPointF offset; -
9385 QGraphicsPixmapItem::ShapeMode shapeMode; -
9386 QPainterPath shape; -
9387 bool hasShape; -
9388 -
9389 void updateShape() -
9390 { -
9391 shape = QPainterPath(); -
9392 switch (shapeMode) { -
9393 case QGraphicsPixmapItem::MaskShape: { -
9394 QBitmap mask = pixmap.mask(); -
9395 if (!mask.isNull()) { -
9396 shape = qt_regionToPath(QRegion(mask).translated(offset.toPoint())); -
9397 break; -
9398 } -
9399 // FALL THROUGH -
9400 } -
9401 case QGraphicsPixmapItem::BoundingRectShape: -
9402 shape.addRect(QRectF(offset.x(), offset.y(), pixmap.width(), pixmap.height())); -
9403 break; -
9404 case QGraphicsPixmapItem::HeuristicMaskShape: -
9405#ifndef QT_NO_IMAGE_HEURISTIC_MASK -
9406 shape = qt_regionToPath(QRegion(pixmap.createHeuristicMask()).translated(offset.toPoint())); -
9407#else -
9408 shape.addRect(QRectF(offset.x(), offset.y(), pixmap.width(), pixmap.height())); -
9409#endif -
9410 break; -
9411 } -
9412 } -
9413}; -
9414 -
9415/*! -
9416 Constructs a QGraphicsPixmapItem, using \a pixmap as the default pixmap. -
9417 \a parent is passed to QGraphicsItem's constructor. -
9418 -
9419 \sa QGraphicsScene::addItem() -
9420*/ -
9421QGraphicsPixmapItem::QGraphicsPixmapItem(const QPixmap &pixmap, QGraphicsItem *parent) -
9422 : QGraphicsItem(*new QGraphicsPixmapItemPrivate, parent) -
9423{ -
9424 setPixmap(pixmap); -
9425} -
9426 -
9427/*! -
9428 Constructs a QGraphicsPixmapItem. \a parent is passed to QGraphicsItem's -
9429 constructor. -
9430 -
9431 \sa QGraphicsScene::addItem() -
9432*/ -
9433QGraphicsPixmapItem::QGraphicsPixmapItem(QGraphicsItem *parent) -
9434 : QGraphicsItem(*new QGraphicsPixmapItemPrivate, parent) -
9435{ -
9436} -
9437 -
9438/*! -
9439 Destroys the QGraphicsPixmapItem. -
9440*/ -
9441QGraphicsPixmapItem::~QGraphicsPixmapItem() -
9442{ -
9443} -
9444 -
9445/*! -
9446 Sets the item's pixmap to \a pixmap. -
9447 -
9448 \sa pixmap() -
9449*/ -
9450void QGraphicsPixmapItem::setPixmap(const QPixmap &pixmap) -
9451{ -
9452 Q_D(QGraphicsPixmapItem); -
9453 prepareGeometryChange(); -
9454 d->pixmap = pixmap; -
9455 d->hasShape = false; -
9456 update(); -
9457} -
9458 -
9459/*! -
9460 Returns the item's pixmap, or an invalid QPixmap if no pixmap has been -
9461 set. -
9462 -
9463 \sa setPixmap() -
9464*/ -
9465QPixmap QGraphicsPixmapItem::pixmap() const -
9466{ -
9467 Q_D(const QGraphicsPixmapItem); -
9468 return d->pixmap; -
9469} -
9470 -
9471/*! -
9472 Returns the transformation mode of the pixmap. The default mode is -
9473 Qt::FastTransformation, which provides quick transformation with no -
9474 smoothing. -
9475 -
9476 \sa setTransformationMode() -
9477*/ -
9478Qt::TransformationMode QGraphicsPixmapItem::transformationMode() const -
9479{ -
9480 Q_D(const QGraphicsPixmapItem); -
9481 return d->transformationMode; -
9482} -
9483 -
9484/*! -
9485 Sets the pixmap item's transformation mode to \a mode, and toggles an -
9486 update of the item. The default mode is Qt::FastTransformation, which -
9487 provides quick transformation with no smoothing. -
9488 -
9489 Qt::SmoothTransformation enables QPainter::SmoothPixmapTransform on the -
9490 painter, and the quality depends on the platform and viewport. The result -
9491 is usually not as good as calling QPixmap::scale() directly. -
9492 -
9493 \sa transformationMode() -
9494*/ -
9495void QGraphicsPixmapItem::setTransformationMode(Qt::TransformationMode mode) -
9496{ -
9497 Q_D(QGraphicsPixmapItem); -
9498 if (mode != d->transformationMode) { -
9499 d->transformationMode = mode; -
9500 update(); -
9501 } -
9502} -
9503 -
9504/*! -
9505 Returns the pixmap item's \e offset, which defines the point of the -
9506 top-left corner of the pixmap, in local coordinates. -
9507 -
9508 \sa setOffset() -
9509*/ -
9510QPointF QGraphicsPixmapItem::offset() const -
9511{ -
9512 Q_D(const QGraphicsPixmapItem); -
9513 return d->offset; -
9514} -
9515 -
9516/*! -
9517 Sets the pixmap item's offset to \a offset. QGraphicsPixmapItem will draw -
9518 its pixmap using \a offset for its top-left corner. -
9519 -
9520 \sa offset() -
9521*/ -
9522void QGraphicsPixmapItem::setOffset(const QPointF &offset) -
9523{ -
9524 Q_D(QGraphicsPixmapItem); -
9525 if (d->offset == offset) -
9526 return; -
9527 prepareGeometryChange(); -
9528 d->offset = offset; -
9529 d->hasShape = false; -
9530 update(); -
9531} -
9532 -
9533/*! -
9534 \fn void QGraphicsPixmapItem::setOffset(qreal x, qreal y) -
9535 \since 4.3 -
9536 -
9537 This convenience function is equivalent to calling setOffset(QPointF(\a x, \a y)). -
9538*/ -
9539 -
9540/*! -
9541 \reimp -
9542*/ -
9543QRectF QGraphicsPixmapItem::boundingRect() const -
9544{ -
9545 Q_D(const QGraphicsPixmapItem); -
9546 if (d->pixmap.isNull()) -
9547 return QRectF(); -
9548 if (d->flags & ItemIsSelectable) { -
9549 qreal pw = 1.0; -
9550 return QRectF(d->offset, d->pixmap.size()).adjusted(-pw/2, -pw/2, pw/2, pw/2); -
9551 } else { -
9552 return QRectF(d->offset, d->pixmap.size()); -
9553 } -
9554} -
9555 -
9556/*! -
9557 \reimp -
9558*/ -
9559QPainterPath QGraphicsPixmapItem::shape() const -
9560{ -
9561 Q_D(const QGraphicsPixmapItem); -
9562 if (!d->hasShape) { -
9563 QGraphicsPixmapItemPrivate *thatD = const_cast<QGraphicsPixmapItemPrivate *>(d); -
9564 thatD->updateShape(); -
9565 thatD->hasShape = true; -
9566 } -
9567 return d_func()->shape; -
9568} -
9569 -
9570/*! -
9571 \reimp -
9572*/ -
9573bool QGraphicsPixmapItem::contains(const QPointF &point) const -
9574{ -
9575 return QGraphicsItem::contains(point); -
9576} -
9577 -
9578/*! -
9579 \reimp -
9580*/ -
9581void QGraphicsPixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, -
9582 QWidget *widget) -
9583{ -
9584 Q_D(QGraphicsPixmapItem); -
9585 Q_UNUSED(widget); -
9586 -
9587 painter->setRenderHint(QPainter::SmoothPixmapTransform, -
9588 (d->transformationMode == Qt::SmoothTransformation)); -
9589 -
9590 painter->drawPixmap(d->offset, d->pixmap); -
9591 -
9592 if (option->state & QStyle::State_Selected) -
9593 qt_graphicsItem_highlightSelected(this, painter, option); -
9594} -
9595 -
9596/*! -
9597 \reimp -
9598*/ -
9599bool QGraphicsPixmapItem::isObscuredBy(const QGraphicsItem *item) const -
9600{ -
9601 return QGraphicsItem::isObscuredBy(item); -
9602} -
9603 -
9604/*! -
9605 \reimp -
9606*/ -
9607QPainterPath QGraphicsPixmapItem::opaqueArea() const -
9608{ -
9609 return shape(); -
9610} -
9611 -
9612/*! -
9613 \reimp -
9614*/ -
9615int QGraphicsPixmapItem::type() const -
9616{ -
9617 return Type; -
9618} -
9619 -
9620/*! -
9621 Returns the item's shape mode. The shape mode describes how -
9622 QGraphicsPixmapItem calculates its shape. The default mode is MaskShape. -
9623 -
9624 \sa setShapeMode(), ShapeMode -
9625*/ -
9626QGraphicsPixmapItem::ShapeMode QGraphicsPixmapItem::shapeMode() const -
9627{ -
9628 return d_func()->shapeMode; -
9629} -
9630 -
9631/*! -
9632 Sets the item's shape mode to \a mode. The shape mode describes how -
9633 QGraphicsPixmapItem calculates its shape. The default mode is MaskShape. -
9634 -
9635 \sa shapeMode(), ShapeMode -
9636*/ -
9637void QGraphicsPixmapItem::setShapeMode(ShapeMode mode) -
9638{ -
9639 Q_D(QGraphicsPixmapItem); -
9640 if (d->shapeMode == mode) -
9641 return; -
9642 d->shapeMode = mode; -
9643 d->hasShape = false; -
9644} -
9645 -
9646/*! -
9647 \internal -
9648*/ -
9649bool QGraphicsPixmapItem::supportsExtension(Extension extension) const -
9650{ -
9651 Q_UNUSED(extension); -
9652 return false; -
9653} -
9654 -
9655/*! -
9656 \internal -
9657*/ -
9658void QGraphicsPixmapItem::setExtension(Extension extension, const QVariant &variant) -
9659{ -
9660 Q_UNUSED(extension); -
9661 Q_UNUSED(variant); -
9662} -
9663 -
9664/*! -
9665 \internal -
9666*/ -
9667QVariant QGraphicsPixmapItem::extension(const QVariant &variant) const -
9668{ -
9669 Q_UNUSED(variant); -
9670 return QVariant(); -
9671} -
9672 -
9673/*! -
9674 \class QGraphicsTextItem -
9675 \brief The QGraphicsTextItem class provides a text item that you can add to -
9676 a QGraphicsScene to display formatted text. -
9677 \since 4.2 -
9678 \ingroup graphicsview-api -
9679 \inmodule QtWidgets -
9680 -
9681 If you only need to show plain text in an item, consider using QGraphicsSimpleTextItem -
9682 instead. -
9683 -
9684 To set the item's text, pass a QString to QGraphicsTextItem's -
9685 constructor, or call setHtml()/setPlainText(). -
9686 -
9687 QGraphicsTextItem uses the text's formatted size and the associated font -
9688 to provide a reasonable implementation of boundingRect(), shape(), -
9689 and contains(). You can set the font by calling setFont(). -
9690 -
9691 It is possible to make the item editable by setting the Qt::TextEditorInteraction flag -
9692 using setTextInteractionFlags(). -
9693 -
9694 The item's preferred text width can be set using setTextWidth() and obtained -
9695 using textWidth(). -
9696 -
9697 \note In order to align HTML text in the center, the item's text width must be set. -
9698 -
9699 \image graphicsview-textitem.png -
9700 -
9701 \note QGraphicsTextItem accepts \l{QGraphicsItem::acceptHoverEvents()}{hover events} -
9702 by default. You can change this with \l{QGraphicsItem::}{setAcceptHoverEvents()}. -
9703 -
9704 \sa QGraphicsSimpleTextItem, QGraphicsPathItem, QGraphicsRectItem, -
9705 QGraphicsEllipseItem, QGraphicsPixmapItem, QGraphicsPolygonItem, -
9706 QGraphicsLineItem, {Graphics View Framework} -
9707*/ -
9708 -
9709class QGraphicsTextItemPrivate -
9710{ -
9711public: -
9712 QGraphicsTextItemPrivate() -
9713 : control(0), pageNumber(0), useDefaultImpl(false), tabChangesFocus(false), clickCausedFocus(0) -
9714 { } -
9715 -
9716 mutable QWidgetTextControl *control; -
9717 QWidgetTextControl *textControl() const; -
9718 -
9719 inline QPointF controlOffset() const -
9720 { return QPointF(0., pageNumber * control->document()->pageSize().height()); } -
9721 inline void sendControlEvent(QEvent *e) -
9722 { if (control) control->processEvent(e, controlOffset()); } -
9723 -
9724 void _q_updateBoundingRect(const QSizeF &); -
9725 void _q_update(QRectF); -
9726 void _q_ensureVisible(QRectF); -
9727 bool _q_mouseOnEdge(QGraphicsSceneMouseEvent *); -
9728 -
9729 QRectF boundingRect; -
9730 int pageNumber; -
9731 bool useDefaultImpl; -
9732 bool tabChangesFocus; -
9733 -
9734 uint clickCausedFocus : 1; -
9735 -
9736 QGraphicsTextItem *qq; -
9737}; -
9738 -
9739 -
9740/*! -
9741 Constructs a QGraphicsTextItem, using \a text as the default plain -
9742 text. \a parent is passed to QGraphicsItem's constructor. -
9743 -
9744 \sa QGraphicsScene::addItem() -
9745*/ -
9746QGraphicsTextItem::QGraphicsTextItem(const QString &text, QGraphicsItem *parent) -
9747 : QGraphicsObject(*new QGraphicsItemPrivate, parent), -
9748 dd(new QGraphicsTextItemPrivate) -
9749{ -
9750 dd->qq = this; -
9751 if (!text.isEmpty()) -
9752 setPlainText(text); -
9753 setAcceptDrops(true); -
9754 setAcceptHoverEvents(true); -
9755 setFlags(ItemUsesExtendedStyleOption); -
9756} -
9757 -
9758/*! -
9759 Constructs a QGraphicsTextItem. \a parent is passed to QGraphicsItem's -
9760 constructor. -
9761 -
9762 \sa QGraphicsScene::addItem() -
9763*/ -
9764QGraphicsTextItem::QGraphicsTextItem(QGraphicsItem *parent) -
9765 : QGraphicsObject(*new QGraphicsItemPrivate, parent), -
9766 dd(new QGraphicsTextItemPrivate) -
9767{ -
9768 dd->qq = this; -
9769 setAcceptDrops(true); -
9770 setAcceptHoverEvents(true); -
9771 setFlag(ItemUsesExtendedStyleOption); -
9772} -
9773 -
9774/*! -
9775 Destroys the QGraphicsTextItem. -
9776*/ -
9777QGraphicsTextItem::~QGraphicsTextItem() -
9778{ -
9779 delete dd; -
9780} -
9781 -
9782/*! -
9783 Returns the item's text converted to HTML, or an empty QString if no text has been set. -
9784 -
9785 \sa setHtml() -
9786*/ -
9787QString QGraphicsTextItem::toHtml() const -
9788{ -
9789#ifndef QT_NO_TEXTHTMLPARSER -
9790 if (dd->control) -
9791 return dd->control->toHtml(); -
9792#endif -
9793 return QString(); -
9794} -
9795 -
9796/*! -
9797 Sets the item's text to \a text, assuming that text is HTML formatted. If -
9798 the item has keyboard input focus, this function will also call -
9799 ensureVisible() to ensure that the text is visible in all viewports. -
9800 -
9801 \sa toHtml(), hasFocus(), QGraphicsSimpleTextItem -
9802*/ -
9803void QGraphicsTextItem::setHtml(const QString &text) -
9804{ -
9805 dd->textControl()->setHtml(text); -
9806} -
9807 -
9808/*! -
9809 Returns the item's text converted to plain text, or an empty QString if no text has been set. -
9810 -
9811 \sa setPlainText() -
9812*/ -
9813QString QGraphicsTextItem::toPlainText() const -
9814{ -
9815 if (dd->control) -
9816 return dd->control->toPlainText(); -
9817 return QString(); -
9818} -
9819 -
9820/*! -
9821 Sets the item's text to \a text. If the item has keyboard input focus, -
9822 this function will also call ensureVisible() to ensure that the text is -
9823 visible in all viewports. -
9824 -
9825 \sa toHtml(), hasFocus() -
9826*/ -
9827void QGraphicsTextItem::setPlainText(const QString &text) -
9828{ -
9829 dd->textControl()->setPlainText(text); -
9830} -
9831 -
9832/*! -
9833 Returns the item's font, which is used to render the text. -
9834 -
9835 \sa setFont() -
9836*/ -
9837QFont QGraphicsTextItem::font() const -
9838{ -
9839 if (!dd->control) -
9840 return QFont(); -
9841 return dd->control->document()->defaultFont(); -
9842} -
9843 -
9844/*! -
9845 Sets the font used to render the text item to \a font. -
9846 -
9847 \sa font() -
9848*/ -
9849void QGraphicsTextItem::setFont(const QFont &font) -
9850{ -
9851 dd->textControl()->document()->setDefaultFont(font); -
9852} -
9853 -
9854/*! -
9855 Sets the color for unformatted text to \a col. -
9856*/ -
9857void QGraphicsTextItem::setDefaultTextColor(const QColor &col) -
9858{ -
9859 QWidgetTextControl *c = dd->textControl(); -
9860 QPalette pal = c->palette(); -
9861 QColor old = pal.color(QPalette::Text); -
9862 pal.setColor(QPalette::Text, col); -
9863 c->setPalette(pal); -
9864 if (old != col) -
9865 update(); -
9866} -
9867 -
9868/*! -
9869 Returns the default text color that is used to for unformatted text. -
9870*/ -
9871QColor QGraphicsTextItem::defaultTextColor() const -
9872{ -
9873 return dd->textControl()->palette().color(QPalette::Text); -
9874} -
9875 -
9876/*! -
9877 \reimp -
9878*/ -
9879QRectF QGraphicsTextItem::boundingRect() const -
9880{ -
9881 return dd->boundingRect; -
9882} -
9883 -
9884/*! -
9885 \reimp -
9886*/ -
9887QPainterPath QGraphicsTextItem::shape() const -
9888{ -
9889 if (!dd->control) -
9890 return QPainterPath(); -
9891 QPainterPath path; -
9892 path.addRect(dd->boundingRect); -
9893 return path; -
9894} -
9895 -
9896/*! -
9897 \reimp -
9898*/ -
9899bool QGraphicsTextItem::contains(const QPointF &point) const -
9900{ -
9901 return dd->boundingRect.contains(point); -
9902} -
9903 -
9904/*! -
9905 \reimp -
9906*/ -
9907void QGraphicsTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, -
9908 QWidget *widget) -
9909{ -
9910 Q_UNUSED(widget); -
9911 if (dd->control) { -
9912 painter->save(); -
9913 QRectF r = option->exposedRect; -
9914 painter->translate(-dd->controlOffset()); -
9915 r.translate(dd->controlOffset()); -
9916 -
9917 QTextDocument *doc = dd->control->document(); -
9918 QTextDocumentLayout *layout = qobject_cast<QTextDocumentLayout *>(doc->documentLayout()); -
9919 -
9920 // the layout might need to expand the root frame to -
9921 // the viewport if NoWrap is set -
9922 if (layout) -
9923 layout->setViewport(dd->boundingRect); -
9924 -
9925 dd->control->drawContents(painter, r); -
9926 -
9927 if (layout) -
9928 layout->setViewport(QRect()); -
9929 -
9930 painter->restore(); -
9931 } -
9932 -
9933 if (option->state & (QStyle::State_Selected | QStyle::State_HasFocus)) -
9934 qt_graphicsItem_highlightSelected(this, painter, option); -
9935} -
9936 -
9937/*! -
9938 \reimp -
9939*/ -
9940bool QGraphicsTextItem::isObscuredBy(const QGraphicsItem *item) const -
9941{ -
9942 return QGraphicsItem::isObscuredBy(item); -
9943} -
9944 -
9945/*! -
9946 \reimp -
9947*/ -
9948QPainterPath QGraphicsTextItem::opaqueArea() const -
9949{ -
9950 return QGraphicsItem::opaqueArea(); -
9951} -
9952 -
9953/*! -
9954 \reimp -
9955*/ -
9956int QGraphicsTextItem::type() const -
9957{ -
9958 return Type; -
9959} -
9960 -
9961/*! -
9962 Sets the preferred width for the item's text. If the actual text -
9963 is wider than the specified width then it will be broken into -
9964 multiple lines. -
9965 -
9966 If \a width is set to -1 then the text will not be broken into -
9967 multiple lines unless it is enforced through an explicit line -
9968 break or a new paragraph. -
9969 -
9970 The default value is -1. -
9971 -
9972 Note that QGraphicsTextItem keeps a QTextDocument internally, -
9973 which is used to calculate the text width. -
9974 -
9975 \sa textWidth(), QTextDocument::setTextWidth() -
9976*/ -
9977void QGraphicsTextItem::setTextWidth(qreal width) -
9978{ -
9979 dd->textControl()->setTextWidth(width); -
9980} -
9981 -
9982/*! -
9983 Returns the text width. -
9984 -
9985 The width is calculated with the QTextDocument that -
9986 QGraphicsTextItem keeps internally. -
9987 -
9988 \sa setTextWidth(), QTextDocument::textWidth() -
9989*/ -
9990qreal QGraphicsTextItem::textWidth() const -
9991{ -
9992 if (!dd->control) -
9993 return -1; -
9994 return dd->control->textWidth(); -
9995} -
9996 -
9997/*! -
9998 Adjusts the text item to a reasonable size. -
9999*/ -
10000void QGraphicsTextItem::adjustSize() -
10001{ -
10002 if (dd->control) -
10003 dd->control->adjustSize(); -
10004} -
10005 -
10006/*! -
10007 Sets the text document \a document on the item. -
10008*/ -
10009void QGraphicsTextItem::setDocument(QTextDocument *document) -
10010{ -
10011 dd->textControl()->setDocument(document); -
10012 dd->_q_updateBoundingRect(dd->control->size()); -
10013} -
10014 -
10015/*! -
10016 Returns the item's text document. -
10017*/ -
10018QTextDocument *QGraphicsTextItem::document() const -
10019{ -
10020 return dd->textControl()->document(); -
10021} -
10022 -
10023/*! -
10024 \reimp -
10025*/ -
10026bool QGraphicsTextItem::sceneEvent(QEvent *event) -
10027{ -
10028 QEvent::Type t = event->type(); -
10029 if (!dd->tabChangesFocus && (t == QEvent::KeyPress || t == QEvent::KeyRelease)) { -
10030 int k = ((QKeyEvent *)event)->key(); -
10031 if (k == Qt::Key_Tab || k == Qt::Key_Backtab) { -
10032 dd->sendControlEvent(event); -
10033 return true; -
10034 } -
10035 } -
10036 bool result = QGraphicsItem::sceneEvent(event); -
10037 -
10038 // Ensure input context is updated. -
10039 switch (event->type()) { -
10040 case QEvent::ContextMenu: -
10041 case QEvent::FocusIn: -
10042 case QEvent::FocusOut: -
10043 case QEvent::GraphicsSceneDragEnter: -
10044 case QEvent::GraphicsSceneDragLeave: -
10045 case QEvent::GraphicsSceneDragMove: -
10046 case QEvent::GraphicsSceneDrop: -
10047 case QEvent::GraphicsSceneHoverEnter: -
10048 case QEvent::GraphicsSceneHoverLeave: -
10049 case QEvent::GraphicsSceneHoverMove: -
10050 case QEvent::GraphicsSceneMouseDoubleClick: -
10051 case QEvent::GraphicsSceneMousePress: -
10052 case QEvent::GraphicsSceneMouseMove: -
10053 case QEvent::GraphicsSceneMouseRelease: -
10054 case QEvent::KeyPress: -
10055 case QEvent::KeyRelease: -
10056 // Reset the focus widget's input context, regardless -
10057 // of how this item gained or lost focus. -
10058 if (event->type() == QEvent::FocusIn || event->type() == QEvent::FocusOut) { -
10059 qApp->inputMethod()->reset(); -
10060 } else { -
10061 qApp->inputMethod()->update(Qt::ImQueryInput); -
10062 } -
10063 break; -
10064 case QEvent::ShortcutOverride: -
10065 dd->sendControlEvent(event); -
10066 return true; -
10067 default: -
10068 break; -
10069 } -
10070 -
10071 return result; -
10072} -
10073 -
10074/*! -
10075 \reimp -
10076*/ -
10077void QGraphicsTextItem::mousePressEvent(QGraphicsSceneMouseEvent *event) -
10078{ -
10079 if ((QGraphicsItem::d_ptr->flags & (ItemIsSelectable | ItemIsMovable)) -
10080 && (event->buttons() & Qt::LeftButton) && dd->_q_mouseOnEdge(event)) { -
10081 // User left-pressed on edge of selectable/movable item, use -
10082 // base impl. -
10083 dd->useDefaultImpl = true; -
10084 } else if (event->buttons() == event->button() -
10085 && dd->control->textInteractionFlags() == Qt::NoTextInteraction) { -
10086 // User pressed first button on non-interactive item. -
10087 dd->useDefaultImpl = true; -
10088 } -
10089 if (dd->useDefaultImpl) { -
10090 QGraphicsItem::mousePressEvent(event); -
10091 if (!event->isAccepted()) -
10092 dd->useDefaultImpl = false; -
10093 return; -
10094 } -
10095 -
10096 dd->sendControlEvent(event); -
10097} -
10098 -
10099/*! -
10100 \reimp -
10101*/ -
10102void QGraphicsTextItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) -
10103{ -
10104 if (dd->useDefaultImpl) { -
10105 QGraphicsItem::mouseMoveEvent(event); -
10106 return; -
10107 } -
10108 -
10109 dd->sendControlEvent(event); -
10110} -
10111 -
10112/*! -
10113 \reimp -
10114*/ -
10115void QGraphicsTextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) -
10116{ -
10117 if (dd->useDefaultImpl) { -
10118 QGraphicsItem::mouseReleaseEvent(event); -
10119 if (dd->control->textInteractionFlags() == Qt::NoTextInteraction -
10120 && !event->buttons()) { -
10121 // User released last button on non-interactive item. -
10122 dd->useDefaultImpl = false; -
10123 } else if ((event->buttons() & Qt::LeftButton) == 0) { -
10124 // User released the left button on an interactive item. -
10125 dd->useDefaultImpl = false; -
10126 } -
10127 return; -
10128 } -
10129 -
10130 QWidget *widget = event->widget(); -
10131 if (widget && (dd->control->textInteractionFlags() & Qt::TextEditable) && boundingRect().contains(event->pos())) { -
10132 qt_widget_private(widget)->handleSoftwareInputPanel(event->button(), dd->clickCausedFocus); -
10133 } -
10134 dd->clickCausedFocus = 0; -
10135 dd->sendControlEvent(event); -
10136} -
10137 -
10138/*! -
10139 \reimp -
10140*/ -
10141void QGraphicsTextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) -
10142{ -
10143 if (dd->useDefaultImpl) { -
10144 QGraphicsItem::mouseDoubleClickEvent(event); -
10145 return; -
10146 } -
10147 -
10148 if (!hasFocus()) { -
10149 QGraphicsItem::mouseDoubleClickEvent(event); -
10150 return; -
10151 } -
10152 -
10153 dd->sendControlEvent(event); -
10154} -
10155 -
10156/*! -
10157 \reimp -
10158*/ -
10159void QGraphicsTextItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) -
10160{ -
10161 dd->sendControlEvent(event); -
10162} -
10163 -
10164/*! -
10165 \reimp -
10166*/ -
10167void QGraphicsTextItem::keyPressEvent(QKeyEvent *event) -
10168{ -
10169 dd->sendControlEvent(event); -
10170} -
10171 -
10172/*! -
10173 \reimp -
10174*/ -
10175void QGraphicsTextItem::keyReleaseEvent(QKeyEvent *event) -
10176{ -
10177 dd->sendControlEvent(event); -
10178} -
10179 -
10180/*! -
10181 \reimp -
10182*/ -
10183void QGraphicsTextItem::focusInEvent(QFocusEvent *event) -
10184{ -
10185 dd->sendControlEvent(event); -
10186 if (event->reason() == Qt::MouseFocusReason) { -
10187 dd->clickCausedFocus = 1; -
10188 } -
10189 update(); -
10190} -
10191 -
10192/*! -
10193 \reimp -
10194*/ -
10195void QGraphicsTextItem::focusOutEvent(QFocusEvent *event) -
10196{ -
10197 dd->sendControlEvent(event); -
10198 update(); -
10199} -
10200 -
10201/*! -
10202 \reimp -
10203*/ -
10204void QGraphicsTextItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) -
10205{ -
10206 dd->sendControlEvent(event); -
10207} -
10208 -
10209/*! -
10210 \reimp -
10211*/ -
10212void QGraphicsTextItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) -
10213{ -
10214 dd->sendControlEvent(event); -
10215} -
10216 -
10217/*! -
10218 \reimp -
10219*/ -
10220void QGraphicsTextItem::dragMoveEvent(QGraphicsSceneDragDropEvent *event) -
10221{ -
10222 dd->sendControlEvent(event); -
10223} -
10224 -
10225/*! -
10226 \reimp -
10227*/ -
10228void QGraphicsTextItem::dropEvent(QGraphicsSceneDragDropEvent *event) -
10229{ -
10230 dd->sendControlEvent(event); -
10231} -
10232 -
10233/*! -
10234 \reimp -
10235*/ -
10236void QGraphicsTextItem::inputMethodEvent(QInputMethodEvent *event) -
10237{ -
10238 dd->sendControlEvent(event); -
10239} -
10240 -
10241/*! -
10242 \reimp -
10243*/ -
10244void QGraphicsTextItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) -
10245{ -
10246 dd->sendControlEvent(event); -
10247} -
10248 -
10249/*! -
10250 \reimp -
10251*/ -
10252void QGraphicsTextItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) -
10253{ -
10254 dd->sendControlEvent(event); -
10255} -
10256 -
10257/*! -
10258 \reimp -
10259*/ -
10260void QGraphicsTextItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) -
10261{ -
10262 dd->sendControlEvent(event); -
10263} -
10264 -
10265/*! -
10266 \reimp -
10267*/ -
10268QVariant QGraphicsTextItem::inputMethodQuery(Qt::InputMethodQuery query) const -
10269{ -
10270 QVariant v; -
10271 if (dd->control) -
10272 v = dd->control->inputMethodQuery(query); -
10273 if (v.type() == QVariant::RectF) -
10274 v = v.toRectF().translated(-dd->controlOffset()); -
10275 else if (v.type() == QVariant::PointF) -
10276 v = v.toPointF() - dd->controlOffset(); -
10277 else if (v.type() == QVariant::Rect) -
10278 v = v.toRect().translated(-dd->controlOffset().toPoint()); -
10279 else if (v.type() == QVariant::Point) -
10280 v = v.toPoint() - dd->controlOffset().toPoint(); -
10281 return v; -
10282} -
10283 -
10284/*! -
10285 \internal -
10286*/ -
10287bool QGraphicsTextItem::supportsExtension(Extension extension) const -
10288{ -
10289 Q_UNUSED(extension); -
10290 return false; -
10291} -
10292 -
10293/*! -
10294 \internal -
10295*/ -
10296void QGraphicsTextItem::setExtension(Extension extension, const QVariant &variant) -
10297{ -
10298 Q_UNUSED(extension); -
10299 Q_UNUSED(variant); -
10300} -
10301 -
10302/*! -
10303 \internal -
10304*/ -
10305QVariant QGraphicsTextItem::extension(const QVariant &variant) const -
10306{ -
10307 Q_UNUSED(variant); -
10308 return QVariant(); -
10309} -
10310 -
10311/*! -
10312 \internal -
10313*/ -
10314void QGraphicsTextItemPrivate::_q_update(QRectF rect) -
10315{ -
10316 if (rect.isValid()) { -
10317 rect.translate(-controlOffset()); -
10318 } else { -
10319 rect = boundingRect; -
10320 } -
10321 if (rect.intersects(boundingRect)) -
10322 qq->update(rect); -
10323} -
10324 -
10325/*! -
10326 \internal -
10327*/ -
10328void QGraphicsTextItemPrivate::_q_updateBoundingRect(const QSizeF &size) -
10329{ -
10330 if (!control) return; // can't happen -
10331 const QSizeF pageSize = control->document()->pageSize(); -
10332 // paged items have a constant (page) size -
10333 if (size == boundingRect.size() || pageSize.height() != -1) -
10334 return; -
10335 qq->prepareGeometryChange(); -
10336 boundingRect.setSize(size); -
10337 qq->update(); -
10338} -
10339 -
10340/*! -
10341 \internal -
10342*/ -
10343void QGraphicsTextItemPrivate::_q_ensureVisible(QRectF rect) -
10344{ -
10345 if (qq->hasFocus()) { -
10346 rect.translate(-controlOffset()); -
10347 qq->ensureVisible(rect, /*xmargin=*/0, /*ymargin=*/0); -
10348 } -
10349} -
10350 -
10351QWidgetTextControl *QGraphicsTextItemPrivate::textControl() const -
10352{ -
10353 if (!control) { -
10354 QGraphicsTextItem *that = const_cast<QGraphicsTextItem *>(qq); -
10355 control = new QWidgetTextControl(that); -
10356 control->setTextInteractionFlags(Qt::NoTextInteraction); -
10357 -
10358 QObject::connect(control, SIGNAL(updateRequest(QRectF)), -
10359 qq, SLOT(_q_update(QRectF))); -
10360 QObject::connect(control, SIGNAL(documentSizeChanged(QSizeF)), -
10361 qq, SLOT(_q_updateBoundingRect(QSizeF))); -
10362 QObject::connect(control, SIGNAL(visibilityRequest(QRectF)), -
10363 qq, SLOT(_q_ensureVisible(QRectF))); -
10364 QObject::connect(control, SIGNAL(linkActivated(QString)), -
10365 qq, SIGNAL(linkActivated(QString))); -
10366 QObject::connect(control, SIGNAL(linkHovered(QString)), -
10367 qq, SIGNAL(linkHovered(QString))); -
10368 -
10369 const QSizeF pgSize = control->document()->pageSize(); -
10370 if (pgSize.height() != -1) { -
10371 qq->prepareGeometryChange(); -
10372 that->dd->boundingRect.setSize(pgSize); -
10373 qq->update(); -
10374 } else { -
10375 that->dd->_q_updateBoundingRect(control->size()); -
10376 } -
10377 } -
10378 return control; -
10379} -
10380 -
10381/*! -
10382 \internal -
10383*/ -
10384bool QGraphicsTextItemPrivate::_q_mouseOnEdge(QGraphicsSceneMouseEvent *event) -
10385{ -
10386 QPainterPath path; -
10387 path.addRect(qq->boundingRect()); -
10388 -
10389 QPainterPath docPath; -
10390 const QTextFrameFormat format = control->document()->rootFrame()->frameFormat(); -
10391 docPath.addRect( -
10392 qq->boundingRect().adjusted( -
10393 format.leftMargin(), -
10394 format.topMargin(), -
10395 -format.rightMargin(), -
10396 -format.bottomMargin())); -
10397 -
10398 return path.subtracted(docPath).contains(event->pos()); -
10399} -
10400 -
10401/*! -
10402 \fn QGraphicsTextItem::linkActivated(const QString &link) -
10403 -
10404 This signal is emitted when the user clicks on a link on a text item -
10405 that enables Qt::LinksAccessibleByMouse or Qt::LinksAccessibleByKeyboard. -
10406 \a link is the link that was clicked. -
10407 -
10408 \sa setTextInteractionFlags() -
10409*/ -
10410 -
10411/*! -
10412 \fn QGraphicsTextItem::linkHovered(const QString &link) -
10413 -
10414 This signal is emitted when the user hovers over a link on a text item -
10415 that enables Qt::LinksAccessibleByMouse. \a link is -
10416 the link that was hovered over. -
10417 -
10418 \sa setTextInteractionFlags() -
10419*/ -
10420 -
10421/*! -
10422 Sets the flags \a flags to specify how the text item should react to user -
10423 input. -
10424 -
10425 The default for a QGraphicsTextItem is Qt::NoTextInteraction. This function -
10426 also affects the ItemIsFocusable QGraphicsItem flag by setting it if \a flags -
10427 is different from Qt::NoTextInteraction and clearing it otherwise. -
10428 -
10429 By default, the text is read-only. To transform the item into an editor, -
10430 set the Qt::TextEditable flag. -
10431*/ -
10432void QGraphicsTextItem::setTextInteractionFlags(Qt::TextInteractionFlags flags) -
10433{ -
10434 if (flags == Qt::NoTextInteraction) -
10435 setFlags(this->flags() & ~(QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemAcceptsInputMethod)); -
10436 else -
10437 setFlags(this->flags() | QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemAcceptsInputMethod); -
10438 -
10439 dd->textControl()->setTextInteractionFlags(flags); -
10440} -
10441 -
10442/*! -
10443 Returns the current text interaction flags. -
10444 -
10445 \sa setTextInteractionFlags() -
10446*/ -
10447Qt::TextInteractionFlags QGraphicsTextItem::textInteractionFlags() const -
10448{ -
10449 if (!dd->control) -
10450 return Qt::NoTextInteraction; -
10451 return dd->control->textInteractionFlags(); -
10452} -
10453 -
10454/*! -
10455 \since 4.5 -
10456 -
10457 If \a b is true, the \uicontrol Tab key will cause the widget to change focus; -
10458 otherwise, the tab key will insert a tab into the document. -
10459 -
10460 In some occasions text edits should not allow the user to input tabulators -
10461 or change indentation using the \uicontrol Tab key, as this breaks the focus -
10462 chain. The default is false. -
10463 -
10464 \sa tabChangesFocus(), ItemIsFocusable, textInteractionFlags() -
10465*/ -
10466void QGraphicsTextItem::setTabChangesFocus(bool b) -
10467{ -
10468 dd->tabChangesFocus = b; -
10469} -
10470 -
10471/*! -
10472 \since 4.5 -
10473 -
10474 Returns true if the \uicontrol Tab key will cause the widget to change focus; -
10475 otherwise, false is returned. -
10476 -
10477 By default, this behavior is disabled, and this function will return false. -
10478 -
10479 \sa setTabChangesFocus() -
10480*/ -
10481bool QGraphicsTextItem::tabChangesFocus() const -
10482{ -
10483 return dd->tabChangesFocus; -
10484} -
10485 -
10486/*! -
10487 \property QGraphicsTextItem::openExternalLinks -
10488 -
10489 Specifies whether QGraphicsTextItem should automatically open links using -
10490 QDesktopServices::openUrl() instead of emitting the -
10491 linkActivated signal. -
10492 -
10493 The default value is false. -
10494*/ -
10495void QGraphicsTextItem::setOpenExternalLinks(bool open) -
10496{ -
10497 dd->textControl()->setOpenExternalLinks(open); -
10498} -
10499 -
10500bool QGraphicsTextItem::openExternalLinks() const -
10501{ -
10502 if (!dd->control) -
10503 return false; -
10504 return dd->control->openExternalLinks(); -
10505} -
10506 -
10507/*! -
10508 \property QGraphicsTextItem::textCursor -
10509 -
10510 This property represents the visible text cursor in an editable -
10511 text item. -
10512 -
10513 By default, if the item's text has not been set, this property -
10514 contains a null text cursor; otherwise it contains a text cursor -
10515 placed at the start of the item's document. -
10516*/ -
10517void QGraphicsTextItem::setTextCursor(const QTextCursor &cursor) -
10518{ -
10519 dd->textControl()->setTextCursor(cursor); -
10520} -
10521 -
10522QTextCursor QGraphicsTextItem::textCursor() const -
10523{ -
10524 if (!dd->control) -
10525 return QTextCursor(); -
10526 return dd->control->textCursor(); -
10527} -
10528 -
10529class QGraphicsSimpleTextItemPrivate : public QAbstractGraphicsShapeItemPrivate -
10530{ -
10531 Q_DECLARE_PUBLIC(QGraphicsSimpleTextItem) -
10532public: -
10533 inline QGraphicsSimpleTextItemPrivate() { -
10534 pen.setStyle(Qt::NoPen); -
10535 brush.setStyle(Qt::SolidPattern); -
10536 } -
10537 QString text; -
10538 QFont font; -
10539 QRectF boundingRect; -
10540 -
10541 void updateBoundingRect(); -
10542}; -
10543 -
10544static QRectF setupTextLayout(QTextLayout *layout) -
10545{ -
10546 layout->setCacheEnabled(true); -
10547 layout->beginLayout(); -
10548 while (layout->createLine().isValid()) -
10549 ; -
10550 layout->endLayout(); -
10551 qreal maxWidth = 0; -
10552 qreal y = 0; -
10553 for (int i = 0; i < layout->lineCount(); ++i) { -
10554 QTextLine line = layout->lineAt(i); -
10555 maxWidth = qMax(maxWidth, line.naturalTextWidth()); -
10556 line.setPosition(QPointF(0, y)); -
10557 y += line.height(); -
10558 } -
10559 return QRectF(0, 0, maxWidth, y); -
10560} -
10561 -
10562void QGraphicsSimpleTextItemPrivate::updateBoundingRect() -
10563{ -
10564 Q_Q(QGraphicsSimpleTextItem); -
10565 QRectF br; -
10566 if (text.isEmpty()) { -
10567 br = QRectF(); -
10568 } else { -
10569 QString tmp = text; -
10570 tmp.replace(QLatin1Char('\n'), QChar::LineSeparator); -
10571 QStackTextEngine engine(tmp, font); -
10572 QTextLayout layout(&engine); -
10573 br = setupTextLayout(&layout); -
10574 } -
10575 if (br != boundingRect) { -
10576 q->prepareGeometryChange(); -
10577 boundingRect = br; -
10578 q->update(); -
10579 } -
10580} -
10581 -
10582/*! -
10583 \class QGraphicsSimpleTextItem -
10584 \brief The QGraphicsSimpleTextItem class provides a simple text path item -
10585 that you can add to a QGraphicsScene. -
10586 \since 4.2 -
10587 \ingroup graphicsview-api -
10588 \inmodule QtWidgets -
10589 -
10590 To set the item's text, you can either pass a QString to -
10591 QGraphicsSimpleTextItem's constructor, or call setText() to change the -
10592 text later. To set the text fill color, call setBrush(). -
10593 -
10594 The simple text item can have both a fill and an outline; setBrush() will -
10595 set the text fill (i.e., text color), and setPen() sets the pen that will -
10596 be used to draw the text outline. (The latter can be slow, especially for -
10597 complex pens, and items with long text content.) If all you want is to -
10598 draw a simple line of text, you should call setBrush() only, and leave the -
10599 pen unset; QGraphicsSimpleTextItem's pen is by default Qt::NoPen. -
10600 -
10601 QGraphicsSimpleTextItem uses the text's formatted size and the associated -
10602 font to provide a reasonable implementation of boundingRect(), shape(), -
10603 and contains(). You can set the font by calling setFont(). -
10604 -
10605 QGraphicsSimpleText does not display rich text; instead, you can use -
10606 QGraphicsTextItem, which provides full text control capabilities. -
10607 -
10608 \image graphicsview-simpletextitem.png -
10609 -
10610 \sa QGraphicsTextItem, QGraphicsPathItem, QGraphicsRectItem, -
10611 QGraphicsEllipseItem, QGraphicsPixmapItem, QGraphicsPolygonItem, -
10612 QGraphicsLineItem, {Graphics View Framework} -
10613*/ -
10614 -
10615/*! -
10616 Constructs a QGraphicsSimpleTextItem. -
10617 -
10618 \a parent is passed to QGraphicsItem's constructor. -
10619 -
10620 \sa QGraphicsScene::addItem() -
10621*/ -
10622QGraphicsSimpleTextItem::QGraphicsSimpleTextItem(QGraphicsItem *parent) -
10623 : QAbstractGraphicsShapeItem(*new QGraphicsSimpleTextItemPrivate, parent) -
10624{ -
10625} -
10626 -
10627/*! -
10628 Constructs a QGraphicsSimpleTextItem, using \a text as the default plain text. -
10629 -
10630 \a parent is passed to QGraphicsItem's constructor. -
10631 -
10632 \sa QGraphicsScene::addItem() -
10633*/ -
10634QGraphicsSimpleTextItem::QGraphicsSimpleTextItem(const QString &text, QGraphicsItem *parent) -
10635 : QAbstractGraphicsShapeItem(*new QGraphicsSimpleTextItemPrivate, parent) -
10636{ -
10637 setText(text); -
10638} -
10639 -
10640/*! -
10641 Destroys the QGraphicsSimpleTextItem. -
10642*/ -
10643QGraphicsSimpleTextItem::~QGraphicsSimpleTextItem() -
10644{ -
10645} -
10646 -
10647/*! -
10648 Sets the item's text to \a text. The text will be displayed as -
10649 plain text. Newline characters ('\\n') as well as characters of -
10650 type QChar::LineSeparator will cause item to break the text into -
10651 multiple lines. -
10652*/ -
10653void QGraphicsSimpleTextItem::setText(const QString &text) -
10654{ -
10655 Q_D(QGraphicsSimpleTextItem); -
10656 if (d->text == text) -
10657 return; -
10658 d->text = text; -
10659 d->updateBoundingRect(); -
10660 update(); -
10661} -
10662 -
10663/*! -
10664 Returns the item's text. -
10665*/ -
10666QString QGraphicsSimpleTextItem::text() const -
10667{ -
10668 Q_D(const QGraphicsSimpleTextItem); -
10669 return d->text; -
10670} -
10671 -
10672/*! -
10673 Sets the font that is used to draw the item's text to \a font. -
10674*/ -
10675void QGraphicsSimpleTextItem::setFont(const QFont &font) -
10676{ -
10677 Q_D(QGraphicsSimpleTextItem); -
10678 d->font = font; -
10679 d->updateBoundingRect(); -
10680} -
10681 -
10682/*! -
10683 Returns the font that is used to draw the item's text. -
10684*/ -
10685QFont QGraphicsSimpleTextItem::font() const -
10686{ -
10687 Q_D(const QGraphicsSimpleTextItem); -
10688 return d->font; -
10689} -
10690 -
10691/*! -
10692 \reimp -
10693*/ -
10694QRectF QGraphicsSimpleTextItem::boundingRect() const -
10695{ -
10696 Q_D(const QGraphicsSimpleTextItem); -
10697 return d->boundingRect; -
10698} -
10699 -
10700/*! -
10701 \reimp -
10702*/ -
10703QPainterPath QGraphicsSimpleTextItem::shape() const -
10704{ -
10705 Q_D(const QGraphicsSimpleTextItem); -
10706 QPainterPath path; -
10707 path.addRect(d->boundingRect); -
10708 return path; -
10709} -
10710 -
10711/*! -
10712 \reimp -
10713*/ -
10714bool QGraphicsSimpleTextItem::contains(const QPointF &point) const -
10715{ -
10716 Q_D(const QGraphicsSimpleTextItem); -
10717 return d->boundingRect.contains(point); -
10718} -
10719 -
10720/*! -
10721 \reimp -
10722*/ -
10723void QGraphicsSimpleTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) -
10724{ -
10725 Q_UNUSED(widget); -
10726 Q_D(QGraphicsSimpleTextItem); -
10727 -
10728 painter->setFont(d->font); -
10729 -
10730 QString tmp = d->text; -
10731 tmp.replace(QLatin1Char('\n'), QChar::LineSeparator); -
10732 QStackTextEngine engine(tmp, d->font); -
10733 QTextLayout layout(&engine); -
10734 setupTextLayout(&layout); -
10735 -
10736 QPen p; -
10737 p.setBrush(d->brush); -
10738 painter->setPen(p); -
10739 if (d->pen.style() == Qt::NoPen && d->brush.style() == Qt::SolidPattern) { -
10740 painter->setBrush(Qt::NoBrush); -
10741 } else { -
10742 QTextLayout::FormatRange range; -
10743 range.start = 0; -
10744 range.length = layout.text().length(); -
10745 range.format.setTextOutline(d->pen); -
10746 QList<QTextLayout::FormatRange> formats; -
10747 formats.append(range); -
10748 layout.setAdditionalFormats(formats); -
10749 } -
10750 -
10751 layout.draw(painter, QPointF(0, 0)); -
10752 -
10753 if (option->state & (QStyle::State_Selected | QStyle::State_HasFocus)) -
10754 qt_graphicsItem_highlightSelected(this, painter, option); -
10755} -
10756 -
10757/*! -
10758 \reimp -
10759*/ -
10760bool QGraphicsSimpleTextItem::isObscuredBy(const QGraphicsItem *item) const -
10761{ -
10762 return QAbstractGraphicsShapeItem::isObscuredBy(item); -
10763} -
10764 -
10765/*! -
10766 \reimp -
10767*/ -
10768QPainterPath QGraphicsSimpleTextItem::opaqueArea() const -
10769{ -
10770 return QAbstractGraphicsShapeItem::opaqueArea(); -
10771} -
10772 -
10773/*! -
10774 \reimp -
10775*/ -
10776int QGraphicsSimpleTextItem::type() const -
10777{ -
10778 return Type; -
10779} -
10780 -
10781/*! -
10782 \internal -
10783*/ -
10784bool QGraphicsSimpleTextItem::supportsExtension(Extension extension) const -
10785{ -
10786 Q_UNUSED(extension); -
10787 return false; -
10788} -
10789 -
10790/*! -
10791 \internal -
10792*/ -
10793void QGraphicsSimpleTextItem::setExtension(Extension extension, const QVariant &variant) -
10794{ -
10795 Q_UNUSED(extension); -
10796 Q_UNUSED(variant); -
10797} -
10798 -
10799/*! -
10800 \internal -
10801*/ -
10802QVariant QGraphicsSimpleTextItem::extension(const QVariant &variant) const -
10803{ -
10804 Q_UNUSED(variant); -
10805 return QVariant(); -
10806} -
10807 -
10808/*! -
10809 \class QGraphicsItemGroup -
10810 \brief The QGraphicsItemGroup class provides a container that treats -
10811 a group of items as a single item. -
10812 \since 4.2 -
10813 \ingroup graphicsview-api -
10814 \inmodule QtWidgets -
10815 -
10816 A QGraphicsItemGroup is a special type of compound item that -
10817 treats itself and all its children as one item (i.e., all events -
10818 and geometries for all children are merged together). It's common -
10819 to use item groups in presentation tools, when the user wants to -
10820 group several smaller items into one big item in order to simplify -
10821 moving and copying of items. -
10822 -
10823 If all you want is to store items inside other items, you can use -
10824 any QGraphicsItem directly by passing a suitable parent to -
10825 setParentItem(). -
10826 -
10827 The boundingRect() function of QGraphicsItemGroup returns the -
10828 bounding rectangle of all items in the item group. -
10829 QGraphicsItemGroup ignores the ItemIgnoresTransformations flag on -
10830 its children (i.e., with respect to the geometry of the group -
10831 item, the children are treated as if they were transformable). -
10832 -
10833 There are two ways to construct an item group. The easiest and -
10834 most common approach is to pass a list of items (e.g., all -
10835 selected items) to QGraphicsScene::createItemGroup(), which -
10836 returns a new QGraphicsItemGroup item. The other approach is to -
10837 manually construct a QGraphicsItemGroup item, add it to the scene -
10838 calling QGraphicsScene::addItem(), and then add items to the group -
10839 manually, one at a time by calling addToGroup(). To dismantle -
10840 ("ungroup") an item group, you can either call -
10841 QGraphicsScene::destroyItemGroup(), or you can manually remove all -
10842 items from the group by calling removeFromGroup(). -
10843 -
10844 \snippet code/src_gui_graphicsview_qgraphicsitem.cpp 17 -
10845 -
10846 The operation of adding and removing items preserves the items' -
10847 scene-relative position and transformation, as opposed to calling -
10848 setParentItem(), where only the child item's parent-relative -
10849 position and transformation are kept. -
10850 -
10851 The addtoGroup() function reparents the target item to this item -
10852 group, keeping the item's position and transformation intact -
10853 relative to the scene. Visually, this means that items added via -
10854 addToGroup() will remain completely unchanged as a result of this -
10855 operation, regardless of the item or the group's current position -
10856 or transformation; although the item's position and matrix are -
10857 likely to change. -
10858 -
10859 The removeFromGroup() function has similar semantics to -
10860 setParentItem(); it reparents the item to the parent item of the -
10861 item group. As with addToGroup(), the item's scene-relative -
10862 position and transformation remain intact. -
10863 -
10864 \sa QGraphicsItem, {Graphics View Framework} -
10865*/ -
10866 -
10867class QGraphicsItemGroupPrivate : public QGraphicsItemPrivate -
10868{ -
10869public: -
10870 QRectF itemsBoundingRect; -
10871}; -
10872 -
10873/*! -
10874 Constructs a QGraphicsItemGroup. \a parent is passed to QGraphicsItem's -
10875 constructor. -
10876 -
10877 \sa QGraphicsScene::addItem() -
10878*/ -
10879QGraphicsItemGroup::QGraphicsItemGroup(QGraphicsItem *parent) -
10880 : QGraphicsItem(*new QGraphicsItemGroupPrivate, parent) -
10881{ -
10882 setHandlesChildEvents(true); -
10883} -
10884 -
10885/*! -
10886 Destroys the QGraphicsItemGroup. -
10887*/ -
10888QGraphicsItemGroup::~QGraphicsItemGroup() -
10889{ -
10890} -
10891 -
10892/*! -
10893 Adds the given \a item and item's child items to this item group. -
10894 The item and child items will be reparented to this group, but its -
10895 position and transformation relative to the scene will stay intact. -
10896 -
10897 \sa removeFromGroup(), QGraphicsScene::createItemGroup() -
10898*/ -
10899void QGraphicsItemGroup::addToGroup(QGraphicsItem *item) -
10900{ -
10901 Q_D(QGraphicsItemGroup); -
10902 if (!item) { -
10903 qWarning("QGraphicsItemGroup::addToGroup: cannot add null item"); -
10904 return; -
10905 } -
10906 if (item == this) { -
10907 qWarning("QGraphicsItemGroup::addToGroup: cannot add a group to itself"); -
10908 return; -
10909 } -
10910 -
10911 // COMBINE -
10912 bool ok; -
10913 QTransform itemTransform = item->itemTransform(this, &ok); -
10914 -
10915 if (!ok) { -
10916 qWarning("QGraphicsItemGroup::addToGroup: could not find a valid transformation from item to group coordinates"); -
10917 return; -
10918 } -
10919 -
10920 QTransform newItemTransform(itemTransform); -
10921 item->setPos(mapFromItem(item, 0, 0)); -
10922 item->setParentItem(this); -
10923 -
10924 // removing position from translation component of the new transform -
10925 if (!item->pos().isNull()) -
10926 newItemTransform *= QTransform::fromTranslate(-item->x(), -item->y()); -
10927 -
10928 // removing additional transformations properties applied with itemTransform() -
10929 QPointF origin = item->transformOriginPoint(); -
10930 QMatrix4x4 m; -
10931 QList<QGraphicsTransform*> transformList = item->transformations(); -
10932 for (int i = 0; i < transformList.size(); ++i) -
10933 transformList.at(i)->applyTo(&m); -
10934 newItemTransform *= m.toTransform().inverted(); -
10935 newItemTransform.translate(origin.x(), origin.y()); -
10936 newItemTransform.rotate(-item->rotation()); -
10937 newItemTransform.scale(1/item->scale(), 1/item->scale()); -
10938 newItemTransform.translate(-origin.x(), -origin.y()); -
10939 -
10940 // ### Expensive, we could maybe use dirtySceneTransform bit for optimization -
10941 -
10942 item->setTransform(newItemTransform); -
10943 item->d_func()->setIsMemberOfGroup(true); -
10944 prepareGeometryChange(); -
10945 d->itemsBoundingRect |= itemTransform.mapRect(item->boundingRect() | item->childrenBoundingRect()); -
10946 update(); -
10947} -
10948 -
10949/*! -
10950 Removes the specified \a item from this group. The item will be -
10951 reparented to this group's parent item, or to 0 if this group has -
10952 no parent. Its position and transformation relative to the scene -
10953 will stay intact. -
10954 -
10955 \sa addToGroup(), QGraphicsScene::destroyItemGroup() -
10956*/ -
10957void QGraphicsItemGroup::removeFromGroup(QGraphicsItem *item) -
10958{ -
10959 Q_D(QGraphicsItemGroup); -
10960 if (!item) { -
10961 qWarning("QGraphicsItemGroup::removeFromGroup: cannot remove null item"); -
10962 return; -
10963 } -
10964 -
10965 QGraphicsItem *newParent = d_ptr->parent; -
10966 -
10967 // COMBINE -
10968 bool ok; -
10969 QTransform itemTransform; -
10970 if (newParent) -
10971 itemTransform = item->itemTransform(newParent, &ok); -
10972 else -
10973 itemTransform = item->sceneTransform(); -
10974 -
10975 QPointF oldPos = item->mapToItem(newParent, 0, 0); -
10976 item->setParentItem(newParent); -
10977 item->setPos(oldPos); -
10978 -
10979 // removing position from translation component of the new transform -
10980 if (!item->pos().isNull()) -
10981 itemTransform *= QTransform::fromTranslate(-item->x(), -item->y()); -
10982 -
10983 // removing additional transformations properties applied -
10984 // with itemTransform() or sceneTransform() -
10985 QPointF origin = item->transformOriginPoint(); -
10986 QMatrix4x4 m; -
10987 QList<QGraphicsTransform*> transformList = item->transformations(); -
10988 for (int i = 0; i < transformList.size(); ++i) -
10989 transformList.at(i)->applyTo(&m); -
10990 itemTransform *= m.toTransform().inverted(); -
10991 itemTransform.translate(origin.x(), origin.y()); -
10992 itemTransform.rotate(-item->rotation()); -
10993 itemTransform.scale(1 / item->scale(), 1 / item->scale()); -
10994 itemTransform.translate(-origin.x(), -origin.y()); -
10995 -
10996 // ### Expensive, we could maybe use dirtySceneTransform bit for optimization -
10997 -
10998 item->setTransform(itemTransform); -
10999 item->d_func()->setIsMemberOfGroup(item->group() != 0); -
11000 -
11001 // ### Quite expensive. But removeFromGroup() isn't called very often. -
11002 prepareGeometryChange(); -
11003 d->itemsBoundingRect = childrenBoundingRect(); -
11004} -
11005 -
11006/*! -
11007 \reimp -
11008 -
11009 Returns the bounding rect of this group item, and all its children. -
11010*/ -
11011QRectF QGraphicsItemGroup::boundingRect() const -
11012{ -
11013 Q_D(const QGraphicsItemGroup); -
11014 return d->itemsBoundingRect; -
11015} -
11016 -
11017/*! -
11018 \reimp -
11019*/ -
11020void QGraphicsItemGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, -
11021 QWidget *widget) -
11022{ -
11023 Q_UNUSED(widget); -
11024 if (option->state & QStyle::State_Selected) { -
11025 Q_D(QGraphicsItemGroup); -
11026 painter->setBrush(Qt::NoBrush); -
11027 painter->drawRect(d->itemsBoundingRect); -
11028 } -
11029} -
11030 -
11031/*! -
11032 \reimp -
11033*/ -
11034bool QGraphicsItemGroup::isObscuredBy(const QGraphicsItem *item) const -
11035{ -
11036 return QGraphicsItem::isObscuredBy(item); -
11037} -
11038 -
11039/*! -
11040 \reimp -
11041*/ -
11042QPainterPath QGraphicsItemGroup::opaqueArea() const -
11043{ -
11044 return QGraphicsItem::opaqueArea(); -
11045} -
11046 -
11047/*! -
11048 \reimp -
11049*/ -
11050int QGraphicsItemGroup::type() const -
11051{ -
11052 return Type; -
11053} -
11054 -
11055#ifndef QT_NO_GRAPHICSEFFECT -
11056QRectF QGraphicsItemEffectSourcePrivate::boundingRect(Qt::CoordinateSystem system) const -
11057{ -
11058 const bool deviceCoordinates = (system == Qt::DeviceCoordinates); -
11059 if (!info && deviceCoordinates) { -
11060 // Device coordinates without info not yet supported. -
11061 qWarning("QGraphicsEffectSource::boundingRect: Not yet implemented, lacking device context"); -
11062 return QRectF(); -
11063 } -
11064 -
11065 QRectF rect = item->boundingRect(); -
11066 if (!item->d_ptr->children.isEmpty()) -
11067 rect |= item->childrenBoundingRect(); -
11068 -
11069 if (deviceCoordinates) { -
11070 Q_ASSERT(info->painter); -
11071 rect = info->painter->worldTransform().mapRect(rect); -
11072 } -
11073 -
11074 return rect; -
11075} -
11076 -
11077void QGraphicsItemEffectSourcePrivate::draw(QPainter *painter) -
11078{ -
11079 if (!info) { -
11080 qWarning("QGraphicsEffectSource::draw: Can only begin as a result of QGraphicsEffect::draw"); -
11081 return; -
11082 } -
11083 -
11084 Q_ASSERT(item->d_ptr->scene); -
11085 QGraphicsScenePrivate *scened = item->d_ptr->scene->d_func(); -
11086 if (painter == info->painter) { -
11087 scened->draw(item, painter, info->viewTransform, info->transformPtr, info->exposedRegion, -
11088 info->widget, info->opacity, info->effectTransform, info->wasDirtySceneTransform, -
11089 info->drawItem); -
11090 } else { -
11091 QTransform effectTransform = info->painter->worldTransform().inverted(); -
11092 effectTransform *= painter->worldTransform(); -
11093 scened->draw(item, painter, info->viewTransform, info->transformPtr, info->exposedRegion, -
11094 info->widget, info->opacity, &effectTransform, info->wasDirtySceneTransform, -
11095 info->drawItem); -
11096 } -
11097} -
11098 -
11099// sourceRect must be in the given coordinate system -
11100QRect QGraphicsItemEffectSourcePrivate::paddedEffectRect(Qt::CoordinateSystem system, QGraphicsEffect::PixmapPadMode mode, const QRectF &sourceRect, bool *unpadded) const -
11101{ -
11102 QRectF effectRectF; -
11103 -
11104 if (unpadded) -
11105 *unpadded = false; -
11106 -
11107 if (mode == QGraphicsEffect::PadToEffectiveBoundingRect) { -
11108 if (info) { -
11109 QRectF deviceRect = system == Qt::DeviceCoordinates ? sourceRect : info->painter->worldTransform().mapRect(sourceRect); -
11110 effectRectF = item->graphicsEffect()->boundingRectFor(deviceRect); -
11111 if (unpadded) -
11112 *unpadded = (effectRectF.size() == sourceRect.size()); -
11113 if (info && system == Qt::LogicalCoordinates) -
11114 effectRectF = info->painter->worldTransform().inverted().mapRect(effectRectF); -
11115 } else { -
11116 // no choice but to send a logical coordinate bounding rect to boundingRectFor -
11117 effectRectF = item->graphicsEffect()->boundingRectFor(sourceRect); -
11118 } -
11119 } else if (mode == QGraphicsEffect::PadToTransparentBorder) { -
11120 // adjust by 1.5 to account for cosmetic pens -
11121 effectRectF = sourceRect.adjusted(-1.5, -1.5, 1.5, 1.5); -
11122 } else { -
11123 effectRectF = sourceRect; -
11124 if (unpadded) -
11125 *unpadded = true; -
11126 } -
11127 -
11128 return effectRectF.toAlignedRect(); -
11129} -
11130 -
11131QPixmap QGraphicsItemEffectSourcePrivate::pixmap(Qt::CoordinateSystem system, QPoint *offset, -
11132 QGraphicsEffect::PixmapPadMode mode) const -
11133{ -
11134 const bool deviceCoordinates = (system == Qt::DeviceCoordinates); -
11135 if (!info && deviceCoordinates) { -
11136 // Device coordinates without info not yet supported. -
11137 qWarning("QGraphicsEffectSource::pixmap: Not yet implemented, lacking device context"); -
11138 return QPixmap(); -
11139 } -
11140 if (!item->d_ptr->scene) -
11141 return QPixmap(); -
11142 QGraphicsScenePrivate *scened = item->d_ptr->scene->d_func(); -
11143 -
11144 bool unpadded; -
11145 const QRectF sourceRect = boundingRect(system); -
11146 QRect effectRect = paddedEffectRect(system, mode, sourceRect, &unpadded); -
11147 -
11148 if (offset) -
11149 *offset = effectRect.topLeft(); -
11150 -
11151 bool untransformed = !deviceCoordinates -
11152 || info->painter->worldTransform().type() <= QTransform::TxTranslate; -
11153 if (untransformed && unpadded && isPixmap()) { -
11154 if (offset) -
11155 *offset = boundingRect(system).topLeft().toPoint(); -
11156 return static_cast<QGraphicsPixmapItem *>(item)->pixmap(); -
11157 } -
11158 -
11159 if (effectRect.isEmpty()) -
11160 return QPixmap(); -
11161 -
11162 QPixmap pixmap(effectRect.size()); -
11163 pixmap.fill(Qt::transparent); -
11164 QPainter pixmapPainter(&pixmap); -
11165 pixmapPainter.setRenderHints(info ? info->painter->renderHints() : QPainter::TextAntialiasing); -
11166 -
11167 QTransform effectTransform = QTransform::fromTranslate(-effectRect.x(), -effectRect.y()); -
11168 if (deviceCoordinates && info->effectTransform) -
11169 effectTransform *= *info->effectTransform; -
11170 -
11171 if (!info) { -
11172 // Logical coordinates without info. -
11173 QTransform sceneTransform = item->sceneTransform(); -
11174 QTransform newEffectTransform = sceneTransform.inverted(); -
11175 newEffectTransform *= effectTransform; -
11176 scened->draw(item, &pixmapPainter, 0, &sceneTransform, 0, 0, qreal(1.0), -
11177 &newEffectTransform, false, true); -
11178 } else if (deviceCoordinates) { -
11179 // Device coordinates with info. -
11180 scened->draw(item, &pixmapPainter, info->viewTransform, info->transformPtr, 0, -
11181 info->widget, info->opacity, &effectTransform, info->wasDirtySceneTransform, -
11182 info->drawItem); -
11183 } else { -
11184 // Item coordinates with info. -
11185 QTransform newEffectTransform = info->transformPtr->inverted(); -
11186 newEffectTransform *= effectTransform; -
11187 scened->draw(item, &pixmapPainter, info->viewTransform, info->transformPtr, 0, -
11188 info->widget, info->opacity, &newEffectTransform, info->wasDirtySceneTransform, -
11189 info->drawItem); -
11190 } -
11191 -
11192 pixmapPainter.end(); -
11193 -
11194 return pixmap; -
11195} -
11196#endif //QT_NO_GRAPHICSEFFECT -
11197 -
11198#ifndef QT_NO_DEBUG_STREAM -
11199QDebug operator<<(QDebug debug, QGraphicsItem *item) -
11200{ -
11201 if (!item) { -
11202 debug << "QGraphicsItem(0)"; -
11203 return debug; -
11204 } -
11205 -
11206 if (QGraphicsObject *o = item->toGraphicsObject()) -
11207 debug << o->metaObject()->className(); -
11208 else -
11209 debug << "QGraphicsItem"; -
11210 debug << "(this =" << (void*)item -
11211 << ", parent =" << (void*)item->parentItem() -
11212 << ", pos =" << item->pos() -
11213 << ", z =" << item->zValue() << ", flags = " -
11214 << item->flags() << ")"; -
11215 return debug; -
11216} -
11217 -
11218QDebug operator<<(QDebug debug, QGraphicsObject *item) -
11219{ -
11220 if (!item) { -
11221 debug << "QGraphicsObject(0)"; -
11222 return debug; -
11223 } -
11224 -
11225 debug.nospace() << item->metaObject()->className() << '(' << (void*)item; -
11226 if (!item->objectName().isEmpty()) -
11227 debug << ", name = " << item->objectName(); -
11228 debug.nospace() << ", parent = " << ((void*)item->parentItem()) -
11229 << ", pos = " << item->pos() -
11230 << ", z = " << item->zValue() << ", flags = " -
11231 << item->flags() << ')'; -
11232 return debug.space(); -
11233} -
11234 -
11235QDebug operator<<(QDebug debug, QGraphicsItem::GraphicsItemChange change) -
11236{ -
11237 const char *str = "UnknownChange"; -
11238 switch (change) { -
11239 case QGraphicsItem::ItemChildAddedChange: -
11240 str = "ItemChildAddedChange"; -
11241 break; -
11242 case QGraphicsItem::ItemChildRemovedChange: -
11243 str = "ItemChildRemovedChange"; -
11244 break; -
11245 case QGraphicsItem::ItemCursorChange: -
11246 str = "ItemCursorChange"; -
11247 break; -
11248 case QGraphicsItem::ItemCursorHasChanged: -
11249 str = "ItemCursorHasChanged"; -
11250 break; -
11251 case QGraphicsItem::ItemEnabledChange: -
11252 str = "ItemEnabledChange"; -
11253 break; -
11254 case QGraphicsItem::ItemEnabledHasChanged: -
11255 str = "ItemEnabledHasChanged"; -
11256 break; -
11257 case QGraphicsItem::ItemFlagsChange: -
11258 str = "ItemFlagsChange"; -
11259 break; -
11260 case QGraphicsItem::ItemFlagsHaveChanged: -
11261 str = "ItemFlagsHaveChanged"; -
11262 break; -
11263 case QGraphicsItem::ItemMatrixChange: -
11264 str = "ItemMatrixChange"; -
11265 break; -
11266 case QGraphicsItem::ItemParentChange: -
11267 str = "ItemParentChange"; -
11268 break; -
11269 case QGraphicsItem::ItemParentHasChanged: -
11270 str = "ItemParentHasChanged"; -
11271 break; -
11272 case QGraphicsItem::ItemPositionChange: -
11273 str = "ItemPositionChange"; -
11274 break; -
11275 case QGraphicsItem::ItemPositionHasChanged: -
11276 str = "ItemPositionHasChanged"; -
11277 break; -
11278 case QGraphicsItem::ItemSceneChange: -
11279 str = "ItemSceneChange"; -
11280 break; -
11281 case QGraphicsItem::ItemSceneHasChanged: -
11282 str = "ItemSceneHasChanged"; -
11283 break; -
11284 case QGraphicsItem::ItemSelectedChange: -
11285 str = "ItemSelectedChange"; -
11286 break; -
11287 case QGraphicsItem::ItemSelectedHasChanged: -
11288 str = "ItemSelectedHasChanged"; -
11289 break; -
11290 case QGraphicsItem::ItemToolTipChange: -
11291 str = "ItemToolTipChange"; -
11292 break; -
11293 case QGraphicsItem::ItemToolTipHasChanged: -
11294 str = "ItemToolTipHasChanged"; -
11295 break; -
11296 case QGraphicsItem::ItemTransformChange: -
11297 str = "ItemTransformChange"; -
11298 break; -
11299 case QGraphicsItem::ItemTransformHasChanged: -
11300 str = "ItemTransformHasChanged"; -
11301 break; -
11302 case QGraphicsItem::ItemVisibleChange: -
11303 str = "ItemVisibleChange"; -
11304 break; -
11305 case QGraphicsItem::ItemVisibleHasChanged: -
11306 str = "ItemVisibleHasChanged"; -
11307 break; -
11308 case QGraphicsItem::ItemZValueChange: -
11309 str = "ItemZValueChange"; -
11310 break; -
11311 case QGraphicsItem::ItemZValueHasChanged: -
11312 str = "ItemZValueHasChanged"; -
11313 break; -
11314 case QGraphicsItem::ItemOpacityChange: -
11315 str = "ItemOpacityChange"; -
11316 break; -
11317 case QGraphicsItem::ItemOpacityHasChanged: -
11318 str = "ItemOpacityHasChanged"; -
11319 break; -
11320 case QGraphicsItem::ItemScenePositionHasChanged: -
11321 str = "ItemScenePositionHasChanged"; -
11322 break; -
11323 case QGraphicsItem::ItemRotationChange: -
11324 str = "ItemRotationChange"; -
11325 break; -
11326 case QGraphicsItem::ItemRotationHasChanged: -
11327 str = "ItemRotationHasChanged"; -
11328 break; -
11329 case QGraphicsItem::ItemScaleChange: -
11330 str = "ItemScaleChange"; -
11331 break; -
11332 case QGraphicsItem::ItemScaleHasChanged: -
11333 str = "ItemScaleHasChanged"; -
11334 break; -
11335 case QGraphicsItem::ItemTransformOriginPointChange: -
11336 str = "ItemTransformOriginPointChange"; -
11337 break; -
11338 case QGraphicsItem::ItemTransformOriginPointHasChanged: -
11339 str = "ItemTransformOriginPointHasChanged"; -
11340 break; -
11341 } -
11342 debug << str; -
11343 return debug; -
11344} -
11345 -
11346QDebug operator<<(QDebug debug, QGraphicsItem::GraphicsItemFlag flag) -
11347{ -
11348 const char *str = "UnknownFlag"; -
11349 switch (flag) { -
11350 case QGraphicsItem::ItemIsMovable: -
11351 str = "ItemIsMovable"; -
11352 break; -
11353 case QGraphicsItem::ItemIsSelectable: -
11354 str = "ItemIsSelectable"; -
11355 break; -
11356 case QGraphicsItem::ItemIsFocusable: -
11357 str = "ItemIsFocusable"; -
11358 break; -
11359 case QGraphicsItem::ItemClipsToShape: -
11360 str = "ItemClipsToShape"; -
11361 break; -
11362 case QGraphicsItem::ItemClipsChildrenToShape: -
11363 str = "ItemClipsChildrenToShape"; -
11364 break; -
11365 case QGraphicsItem::ItemIgnoresTransformations: -
11366 str = "ItemIgnoresTransformations"; -
11367 break; -
11368 case QGraphicsItem::ItemIgnoresParentOpacity: -
11369 str = "ItemIgnoresParentOpacity"; -
11370 break; -
11371 case QGraphicsItem::ItemDoesntPropagateOpacityToChildren: -
11372 str = "ItemDoesntPropagateOpacityToChildren"; -
11373 break; -
11374 case QGraphicsItem::ItemStacksBehindParent: -
11375 str = "ItemStacksBehindParent"; -
11376 break; -
11377 case QGraphicsItem::ItemUsesExtendedStyleOption: -
11378 str = "ItemUsesExtendedStyleOption"; -
11379 break; -
11380 case QGraphicsItem::ItemHasNoContents: -
11381 str = "ItemHasNoContents"; -
11382 break; -
11383 case QGraphicsItem::ItemSendsGeometryChanges: -
11384 str = "ItemSendsGeometryChanges"; -
11385 break; -
11386 case QGraphicsItem::ItemAcceptsInputMethod: -
11387 str = "ItemAcceptsInputMethod"; -
11388 break; -
11389 case QGraphicsItem::ItemNegativeZStacksBehindParent: -
11390 str = "ItemNegativeZStacksBehindParent"; -
11391 break; -
11392 case QGraphicsItem::ItemIsPanel: -
11393 str = "ItemIsPanel"; -
11394 break; -
11395 case QGraphicsItem::ItemIsFocusScope: -
11396 str = "ItemIsFocusScope"; -
11397 break; -
11398 case QGraphicsItem::ItemSendsScenePositionChanges: -
11399 str = "ItemSendsScenePositionChanges"; -
11400 break; -
11401 case QGraphicsItem::ItemStopsClickFocusPropagation: -
11402 str = "ItemStopsClickFocusPropagation"; -
11403 break; -
11404 case QGraphicsItem::ItemStopsFocusHandling: -
11405 str = "ItemStopsFocusHandling"; -
11406 break; -
11407 } -
11408 debug << str; -
11409 return debug; -
11410} -
11411 -
11412QDebug operator<<(QDebug debug, QGraphicsItem::GraphicsItemFlags flags) -
11413{ -
11414 debug << '('; -
11415 bool f = false; -
11416 for (int i = 0; i < 17; ++i) { -
11417 if (flags & (1 << i)) { -
11418 if (f) -
11419 debug << '|'; -
11420 f = true; -
11421 debug << QGraphicsItem::GraphicsItemFlag(int(flags & (1 << i))); -
11422 } -
11423 } -
11424 debug << ')'; -
11425 return debug; -
11426} -
11427 -
11428#endif -
11429 -
11430QT_END_NAMESPACE -
11431 -
11432#include "moc_qgraphicsitem.cpp" -
11433 -
11434#endif // QT_NO_GRAPHICSVIEW -
11435 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial