graphicsview/qgraphicsscene.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 QGraphicsScene -
44 \brief The QGraphicsScene class provides a surface for managing a large -
45 number of 2D graphical items. -
46 \since 4.2 -
47 \ingroup graphicsview-api -
48 \inmodule QtWidgets -
49 -
50 The class serves as a container for QGraphicsItems. It is used together -
51 with QGraphicsView for visualizing graphical items, such as lines, -
52 rectangles, text, or even custom items, on a 2D surface. QGraphicsScene is -
53 part of the \l{Graphics View Framework}. -
54 -
55 QGraphicsScene also provides functionality that lets you efficiently -
56 determine both the location of items, and for determining what items are -
57 visible within an arbitrary area on the scene. With the QGraphicsView -
58 widget, you can either visualize the whole scene, or zoom in and view only -
59 parts of the scene. -
60 -
61 Example: -
62 -
63 \snippet code/src_gui_graphicsview_qgraphicsscene.cpp 0 -
64 -
65 Note that QGraphicsScene has no visual appearance of its own; it only -
66 manages the items. You need to create a QGraphicsView widget to visualize -
67 the scene. -
68 -
69 To add items to a scene, you start off by constructing a QGraphicsScene -
70 object. Then, you have two options: either add your existing QGraphicsItem -
71 objects by calling addItem(), or you can call one of the convenience -
72 functions addEllipse(), addLine(), addPath(), addPixmap(), addPolygon(), -
73 addRect(), or addText(), which all return a pointer to the newly added item. -
74 The dimensions of the items added with these functions are relative to the -
75 item's coordinate system, and the items position is initialized to (0, -
76 0) in the scene. -
77 -
78 You can then visualize the scene using QGraphicsView. When the scene -
79 changes, (e.g., when an item moves or is transformed) QGraphicsScene -
80 emits the changed() signal. To remove an item, call removeItem(). -
81 -
82 QGraphicsScene uses an indexing algorithm to manage the location of items -
83 efficiently. By default, a BSP (Binary Space Partitioning) tree is used; an -
84 algorithm suitable for large scenes where most items remain static (i.e., -
85 do not move around). You can choose to disable this index by calling -
86 setItemIndexMethod(). For more information about the available indexing -
87 algorithms, see the itemIndexMethod property. -
88 -
89 The scene's bounding rect is set by calling setSceneRect(). Items can be -
90 placed at any position on the scene, and the size of the scene is by -
91 default unlimited. The scene rect is used only for internal bookkeeping, -
92 maintaining the scene's item index. If the scene rect is unset, -
93 QGraphicsScene will use the bounding area of all items, as returned by -
94 itemsBoundingRect(), as the scene rect. However, itemsBoundingRect() is a -
95 relatively time consuming function, as it operates by collecting -
96 positional information for every item on the scene. Because of this, you -
97 should always set the scene rect when operating on large scenes. -
98 -
99 One of QGraphicsScene's greatest strengths is its ability to efficiently -
100 determine the location of items. Even with millions of items on the scene, -
101 the items() functions can determine the location of an item within a few -
102 milliseconds. There are several overloads to items(): one that finds items -
103 at a certain position, one that finds items inside or intersecting with a -
104 polygon or a rectangle, and more. The list of returned items is sorted by -
105 stacking order, with the topmost item being the first item in the list. -
106 For convenience, there is also an itemAt() function that returns the -
107 topmost item at a given position. -
108 -
109 QGraphicsScene maintains selection information for the scene. To select -
110 items, call setSelectionArea(), and to clear the current selection, call -
111 clearSelection(). Call selectedItems() to get the list of all selected -
112 items. -
113 -
114 \section1 Event Handling and Propagation -
115 -
116 Another responsibility that QGraphicsScene has, is to propagate events -
117 from QGraphicsView. To send an event to a scene, you construct an event -
118 that inherits QEvent, and then send it using, for example, -
119 QApplication::sendEvent(). event() is responsible for dispatching -
120 the event to the individual items. Some common events are handled by -
121 convenience event handlers. For example, key press events are handled by -
122 keyPressEvent(), and mouse press events are handled by mousePressEvent(). -
123 -
124 Key events are delivered to the \e {focus item}. To set the focus item, -
125 you can either call setFocusItem(), passing an item that accepts focus, or -
126 the item itself can call QGraphicsItem::setFocus(). Call focusItem() to -
127 get the current focus item. For compatibility with widgets, the scene also -
128 maintains its own focus information. By default, the scene does not have -
129 focus, and all key events are discarded. If setFocus() is called, or if an -
130 item on the scene gains focus, the scene automatically gains focus. If the -
131 scene has focus, hasFocus() will return true, and key events will be -
132 forwarded to the focus item, if any. If the scene loses focus, (i.e., -
133 someone calls clearFocus()) while an item has focus, the scene will -
134 maintain its item focus information, and once the scene regains focus, it -
135 will make sure the last focus item regains focus. -
136 -
137 For mouse-over effects, QGraphicsScene dispatches \e {hover -
138 events}. If an item accepts hover events (see -
139 QGraphicsItem::acceptHoverEvents()), it will receive a \l -
140 {QEvent::}{GraphicsSceneHoverEnter} event when the mouse enters -
141 its area. As the mouse continues moving inside the item's area, -
142 QGraphicsScene will send it \l {QEvent::}{GraphicsSceneHoverMove} -
143 events. When the mouse leaves the item's area, the item will -
144 receive a \l {QEvent::}{GraphicsSceneHoverLeave} event. -
145 -
146 All mouse events are delivered to the current \e {mouse grabber} -
147 item. An item becomes the scene's mouse grabber if it accepts -
148 mouse events (see QGraphicsItem::acceptedMouseButtons()) and it -
149 receives a mouse press. It stays the mouse grabber until it -
150 receives a mouse release when no other mouse buttons are -
151 pressed. You can call mouseGrabberItem() to determine what item is -
152 currently grabbing the mouse. -
153 -
154 \sa QGraphicsItem, QGraphicsView -
155*/ -
156 -
157/*! -
158 \enum QGraphicsScene::SceneLayer -
159 \since 4.3 -
160 -
161 This enum describes the rendering layers in a QGraphicsScene. When -
162 QGraphicsScene draws the scene contents, it renders each of these layers -
163 separately, in order. -
164 -
165 Each layer represents a flag that can be OR'ed together when calling -
166 functions such as invalidate() or QGraphicsView::invalidateScene(). -
167 -
168 \value ItemLayer The item layer. QGraphicsScene renders all items are in -
169 this layer by calling the virtual function drawItems(). The item layer is -
170 drawn after the background layer, but before the foreground layer. -
171 -
172 \value BackgroundLayer The background layer. QGraphicsScene renders the -
173 scene's background in this layer by calling the virtual function -
174 drawBackground(). The background layer is drawn first of all layers. -
175 -
176 \value ForegroundLayer The foreground layer. QGraphicsScene renders the -
177 scene's foreground in this layer by calling the virtual function -
178 drawForeground(). The foreground layer is drawn last of all layers. -
179 -
180 \value AllLayers All layers; this value represents a combination of all -
181 three layers. -
182 -
183 \sa invalidate(), QGraphicsView::invalidateScene() -
184*/ -
185 -
186/*! -
187 \enum QGraphicsScene::ItemIndexMethod -
188 -
189 This enum describes the indexing algorithms QGraphicsScene provides for -
190 managing positional information about items on the scene. -
191 -
192 \value BspTreeIndex A Binary Space Partitioning tree is applied. All -
193 QGraphicsScene's item location algorithms are of an order close to -
194 logarithmic complexity, by making use of binary search. Adding, moving and -
195 removing items is logarithmic. This approach is best for static scenes -
196 (i.e., scenes where most items do not move). -
197 -
198 \value NoIndex No index is applied. Item location is of linear complexity, -
199 as all items on the scene are searched. Adding, moving and removing items, -
200 however, is done in constant time. This approach is ideal for dynamic -
201 scenes, where many items are added, moved or removed continuously. -
202 -
203 \sa setItemIndexMethod(), bspTreeDepth -
204*/ -
205 -
206#include "qgraphicsscene.h" -
207 -
208#ifndef QT_NO_GRAPHICSVIEW -
209 -
210#include "qgraphicsitem.h" -
211#include "qgraphicsitem_p.h" -
212#include "qgraphicslayout.h" -
213#include "qgraphicsscene_p.h" -
214#include "qgraphicssceneevent.h" -
215#include "qgraphicsview.h" -
216#include "qgraphicsview_p.h" -
217#include "qgraphicswidget.h" -
218#include "qgraphicswidget_p.h" -
219#include "qgraphicssceneindex_p.h" -
220#include "qgraphicsscenebsptreeindex_p.h" -
221#include "qgraphicsscenelinearindex_p.h" -
222 -
223#include <QtCore/qdebug.h> -
224#include <QtCore/qlist.h> -
225#include <QtCore/qmath.h> -
226#include <QtCore/qrect.h> -
227#include <QtCore/qset.h> -
228#include <QtCore/qstack.h> -
229#include <QtCore/qtimer.h> -
230#include <QtCore/qvarlengtharray.h> -
231#include <QtCore/QMetaMethod> -
232#include <QtWidgets/qapplication.h> -
233#include <QtWidgets/qdesktopwidget.h> -
234#include <QtGui/qevent.h> -
235#include <QtWidgets/qgraphicslayout.h> -
236#include <QtWidgets/qgraphicsproxywidget.h> -
237#include <QtWidgets/qgraphicswidget.h> -
238#include <QtGui/qmatrix.h> -
239#include <QtGui/qpaintengine.h> -
240#include <QtGui/qpainter.h> -
241#include <QtGui/qpixmapcache.h> -
242#include <QtGui/qpolygon.h> -
243#include <QtWidgets/qstyleoption.h> -
244#include <QtWidgets/qtooltip.h> -
245#include <QtGui/qtransform.h> -
246#include <QtGui/qinputmethod.h> -
247#include <QtWidgets/qgraphicseffect.h>-
#ifndef QT_NO_ACCESSIBILITY
# include <QtGui/qaccessible.h>
248#endif#include <private/qapplication_p.h> -
249#include <private/qobject_p.h> -
250#include <private/qgraphicseffect_p.h> -
251#include <private/qgesturemanager_p.h> -
252#include <private/qpathclipper_p.h> -
253 -
254// #define GESTURE_DEBUG -
255#ifndef GESTURE_DEBUG -
256# define DEBUG if (0) qDebug -
257#else -
258# define DEBUG qDebug -
259#endif -
260 -
261QT_BEGIN_NAMESPACE -
262 -
263bool qt_sendSpontaneousEvent(QObject *receiver, QEvent *event); -
264 -
265static void _q_hoverFromMouseEvent(QGraphicsSceneHoverEvent *hover, const QGraphicsSceneMouseEvent *mouseEvent) -
266{ -
267 hover->setWidget(mouseEvent->widget()); -
268 hover->setPos(mouseEvent->pos()); -
269 hover->setScenePos(mouseEvent->scenePos()); -
270 hover->setScreenPos(mouseEvent->screenPos()); -
271 hover->setLastPos(mouseEvent->lastPos()); -
272 hover->setLastScenePos(mouseEvent->lastScenePos()); -
273 hover->setLastScreenPos(mouseEvent->lastScreenPos()); -
274 hover->setModifiers(mouseEvent->modifiers()); -
275 hover->setAccepted(mouseEvent->isAccepted()); -
276} -
277 -
278/*! -
279 \internal -
280*/ -
281QGraphicsScenePrivate::QGraphicsScenePrivate() -
282 : indexMethod(QGraphicsScene::BspTreeIndex), -
283 index(0), -
284 lastItemCount(0), -
285 hasSceneRect(false), -
286 dirtyGrowingItemsBoundingRect(true), -
287 updateAll(false), -
288 calledEmitUpdated(false), -
289 processDirtyItemsEmitted(false), -
290 needSortTopLevelItems(true), -
291 holesInTopLevelSiblingIndex(false), -
292 topLevelSequentialOrdering(true), -
293 scenePosDescendantsUpdatePending(false), -
294 stickyFocus(false), -
295 hasFocus(false), -
296 lastMouseGrabberItemHasImplicitMouseGrab(false), -
297 allItemsIgnoreHoverEvents(true), -
298 allItemsUseDefaultCursor(true), -
299 painterStateProtection(true), -
300 sortCacheEnabled(false), -
301 allItemsIgnoreTouchEvents(true), -
302 selectionChanging(0), -
303 rectAdjust(2), -
304 focusItem(0), -
305 lastFocusItem(0), -
306 passiveFocusItem(0), -
307 tabFocusFirst(0), -
308 activePanel(0), -
309 lastActivePanel(0), -
310 activationRefCount(0), -
311 childExplicitActivation(0), -
312 lastMouseGrabberItem(0), -
313 dragDropItem(0), -
314 enterWidget(0), -
315 lastDropAction(Qt::IgnoreAction), -
316 style(0) -
317{ -
318} -
319 -
320/*! -
321 \internal -
322*/ -
323void QGraphicsScenePrivate::init() -
324{ -
325 Q_Q(QGraphicsScene); -
326 -
327 index = new QGraphicsSceneBspTreeIndex(q); -
328 -
329 // Keep this index so we can check for connected slots later on. -
330 changedSignalIndex = signalIndex("changed(QList<QRectF>)"); -
331 processDirtyItemsIndex = q->metaObject()->indexOfSlot("_q_processDirtyItems()"); -
332 polishItemsIndex = q->metaObject()->indexOfSlot("_q_polishItems()"); -
333 -
334 qApp->d_func()->scene_list.append(q); -
335 q->update(); -
336} -
337 -
338/*! -
339 \internal -
340*/ -
341QGraphicsScenePrivate *QGraphicsScenePrivate::get(QGraphicsScene *q) -
342{ -
343 return q->d_func(); -
344} -
345 -
346void QGraphicsScenePrivate::_q_emitUpdated() -
347{ -
348 Q_Q(QGraphicsScene); -
349 calledEmitUpdated = false; -
350 -
351 if (dirtyGrowingItemsBoundingRect) { -
352 if (!hasSceneRect) { -
353 const QRectF oldGrowingItemsBoundingRect = growingItemsBoundingRect; -
354 growingItemsBoundingRect |= q->itemsBoundingRect(); -
355 if (oldGrowingItemsBoundingRect != growingItemsBoundingRect) -
356 emit q->sceneRectChanged(growingItemsBoundingRect); -
357 } -
358 dirtyGrowingItemsBoundingRect = false; -
359 } -
360 -
361 // Ensure all views are connected if anything is connected. This disables -
362 // the optimization that items send updates directly to the views, but it -
363 // needs to happen in order to keep compatibility with the behavior from -
364 // Qt 4.4 and backward. -
365 if (isSignalConnected(changedSignalIndex)) { -
366 for (int i = 0; i < views.size(); ++i) { -
367 QGraphicsView *view = views.at(i); -
368 if (!view->d_func()->connectedToScene) { -
369 view->d_func()->connectedToScene = true; -
370 q->connect(q, SIGNAL(changed(QList<QRectF>)), -
371 views.at(i), SLOT(updateScene(QList<QRectF>))); -
372 } -
373 } -
374 } else { -
375 if (views.isEmpty()) { -
376 updateAll = false; -
377 return; -
378 } -
379 for (int i = 0; i < views.size(); ++i) -
380 views.at(i)->d_func()->processPendingUpdates(); -
381 // It's important that we update all views before we dispatch, hence two for-loops. -
382 for (int i = 0; i < views.size(); ++i) -
383 views.at(i)->d_func()->dispatchPendingUpdateRequests(); -
384 return; -
385 } -
386 -
387 // Notify the changes to anybody interested. -
388 QList<QRectF> oldUpdatedRects; -
389 oldUpdatedRects = updateAll ? (QList<QRectF>() << q->sceneRect()) : updatedRects; -
390 updateAll = false; -
391 updatedRects.clear(); -
392 emit q->changed(oldUpdatedRects); -
393} -
394 -
395/*! -
396 \internal -
397 -
398 ### This function is almost identical to QGraphicsItemPrivate::addChild(). -
399*/ -
400void QGraphicsScenePrivate::registerTopLevelItem(QGraphicsItem *item) -
401{ -
402 item->d_ptr->ensureSequentialSiblingIndex(); -
403 needSortTopLevelItems = true; // ### maybe false -
404 item->d_ptr->siblingIndex = topLevelItems.size(); -
405 topLevelItems.append(item); -
406} -
407 -
408/*! -
409 \internal -
410 -
411 ### This function is almost identical to QGraphicsItemPrivate::removeChild(). -
412*/ -
413void QGraphicsScenePrivate::unregisterTopLevelItem(QGraphicsItem *item) -
414{ -
415 if (!holesInTopLevelSiblingIndex) -
416 holesInTopLevelSiblingIndex = item->d_ptr->siblingIndex != topLevelItems.size() - 1; -
417 if (topLevelSequentialOrdering && !holesInTopLevelSiblingIndex) -
418 topLevelItems.removeAt(item->d_ptr->siblingIndex); -
419 else -
420 topLevelItems.removeOne(item); -
421 // NB! Do not use topLevelItems.removeAt(item->d_ptr->siblingIndex) because -
422 // the item is not guaranteed to be at the index after the list is sorted -
423 // (see ensureSortedTopLevelItems()). -
424 item->d_ptr->siblingIndex = -1; -
425 if (topLevelSequentialOrdering) -
426 topLevelSequentialOrdering = !holesInTopLevelSiblingIndex; -
427} -
428 -
429/*! -
430 \internal -
431*/ -
432void QGraphicsScenePrivate::_q_polishItems() -
433{ -
434 if (unpolishedItems.isEmpty()) -
435 return; -
436 -
437 const QVariant booleanTrueVariant(true); -
438 QGraphicsItem *item = 0; -
439 QGraphicsItemPrivate *itemd = 0; -
440 const int oldUnpolishedCount = unpolishedItems.count(); -
441 -
442 for (int i = 0; i < oldUnpolishedCount; ++i) { -
443 item = unpolishedItems.at(i); -
444 if (!item) -
445 continue; -
446 itemd = item->d_ptr.data(); -
447 itemd->pendingPolish = false; -
448 if (!itemd->explicitlyHidden) { -
449 item->itemChange(QGraphicsItem::ItemVisibleChange, booleanTrueVariant); -
450 item->itemChange(QGraphicsItem::ItemVisibleHasChanged, booleanTrueVariant); -
451 } -
452 if (itemd->isWidget) { -
453 QEvent event(QEvent::Polish); -
454 QApplication::sendEvent((QGraphicsWidget *)item, &event); -
455 } -
456 } -
457 -
458 if (unpolishedItems.count() == oldUnpolishedCount) { -
459 // No new items were added to the vector. -
460 unpolishedItems.clear(); -
461 } else { -
462 // New items were appended; keep them and remove the old ones. -
463 unpolishedItems.remove(0, oldUnpolishedCount); -
464 unpolishedItems.squeeze(); -
465 QMetaObject::invokeMethod(q_ptr, "_q_polishItems", Qt::QueuedConnection); -
466 } -
467} -
468 -
469void QGraphicsScenePrivate::_q_processDirtyItems() -
470{ -
471 processDirtyItemsEmitted = false; -
472 -
473 if (updateAll) { -
474 Q_ASSERT(calledEmitUpdated); -
475 // No need for further processing (except resetting the dirty states). -
476 // The growingItemsBoundingRect is updated in _q_emitUpdated. -
477 for (int i = 0; i < topLevelItems.size(); ++i) -
478 resetDirtyItem(topLevelItems.at(i), /*recursive=*/true); -
479 return; -
480 } -
481 -
482 const bool wasPendingSceneUpdate = calledEmitUpdated; -
483 const QRectF oldGrowingItemsBoundingRect = growingItemsBoundingRect; -
484 -
485 // Process items recursively. -
486 for (int i = 0; i < topLevelItems.size(); ++i) -
487 processDirtyItemsRecursive(topLevelItems.at(i)); -
488 -
489 dirtyGrowingItemsBoundingRect = false; -
490 if (!hasSceneRect && oldGrowingItemsBoundingRect != growingItemsBoundingRect) -
491 emit q_func()->sceneRectChanged(growingItemsBoundingRect); -
492 -
493 if (wasPendingSceneUpdate) -
494 return; -
495 -
496 for (int i = 0; i < views.size(); ++i) -
497 views.at(i)->d_func()->processPendingUpdates(); -
498 -
499 if (calledEmitUpdated) { -
500 // We did a compatibility QGraphicsScene::update in processDirtyItemsRecursive -
501 // and we cannot wait for the control to reach the eventloop before the -
502 // changed signal is emitted, so we emit it now. -
503 _q_emitUpdated(); -
504 } -
505 -
506 // Immediately dispatch all pending update requests on the views. -
507 for (int i = 0; i < views.size(); ++i) -
508 views.at(i)->d_func()->dispatchPendingUpdateRequests(); -
509} -
510 -
511/*! -
512 \internal -
513*/ -
514void QGraphicsScenePrivate::setScenePosItemEnabled(QGraphicsItem *item, bool enabled) -
515{ -
516 QGraphicsItem *p = item->d_ptr->parent; -
517 while (p) { -
518 p->d_ptr->scenePosDescendants = enabled; -
519 p = p->d_ptr->parent; -
520 } -
521 if (!enabled && !scenePosDescendantsUpdatePending) { -
522 scenePosDescendantsUpdatePending = true; -
523 QMetaObject::invokeMethod(q_func(), "_q_updateScenePosDescendants", Qt::QueuedConnection); -
524 } -
525} -
526 -
527/*! -
528 \internal -
529*/ -
530void QGraphicsScenePrivate::registerScenePosItem(QGraphicsItem *item) -
531{ -
532 scenePosItems.insert(item); -
533 setScenePosItemEnabled(item, true); -
534} -
535 -
536/*! -
537 \internal -
538*/ -
539void QGraphicsScenePrivate::unregisterScenePosItem(QGraphicsItem *item) -
540{ -
541 scenePosItems.remove(item); -
542 setScenePosItemEnabled(item, false); -
543} -
544 -
545/*! -
546 \internal -
547*/ -
548void QGraphicsScenePrivate::_q_updateScenePosDescendants() -
549{ -
550 foreach (QGraphicsItem *item, scenePosItems) { -
551 QGraphicsItem *p = item->d_ptr->parent; -
552 while (p) { -
553 p->d_ptr->scenePosDescendants = 1; -
554 p = p->d_ptr->parent; -
555 } -
556 } -
557 scenePosDescendantsUpdatePending = false; -
558} -
559 -
560/*! -
561 \internal -
562 -
563 Schedules an item for removal. This function leaves some stale indexes -
564 around in the BSP tree if called from the item's destructor; these will -
565 be cleaned up the next time someone triggers purgeRemovedItems(). -
566 -
567 Note: This function might get called from QGraphicsItem's destructor. \a item is -
568 being destroyed, so we cannot call any pure virtual functions on it (such -
569 as boundingRect()). Also, it is unnecessary to update the item's own state -
570 in any way. -
571*/ -
572void QGraphicsScenePrivate::removeItemHelper(QGraphicsItem *item) -
573{ -
574 Q_Q(QGraphicsScene); -
575 -
576 // Clear focus on the item to remove any reference in the focusWidget chain. -
577 item->clearFocus(); -
578 -
579 markDirty(item, QRectF(), /*invalidateChildren=*/false, /*force=*/false, -
580 /*ignoreOpacity=*/false, /*removingItemFromScene=*/true); -
581 -
582 if (item->d_ptr->inDestructor) { -
583 // The item is actually in its destructor, we call the special method in the index. -
584 index->deleteItem(item); -
585 } else { -
586 // Can potentially call item->boundingRect() (virtual function), that's why -
587 // we only can call this function if the item is not in its destructor. -
588 index->removeItem(item); -
589 } -
590 -
591 item->d_ptr->clearSubFocus(); -
592 -
593 if (item->flags() & QGraphicsItem::ItemSendsScenePositionChanges) -
594 unregisterScenePosItem(item); -
595 -
596 QGraphicsScene *oldScene = item->d_func()->scene; -
597 item->d_func()->scene = 0; -
598 -
599 //We need to remove all children first because they might use their parent -
600 //attributes (e.g. sceneTransform). -
601 if (!item->d_ptr->inDestructor) { -
602 // Remove all children recursively -
603 for (int i = 0; i < item->d_ptr->children.size(); ++i) -
604 q->removeItem(item->d_ptr->children.at(i)); -
605 } -
606 -
607 if (!item->d_ptr->inDestructor && item == tabFocusFirst) { -
608 QGraphicsWidget *widget = static_cast<QGraphicsWidget *>(item); -
609 widget->d_func()->fixFocusChainBeforeReparenting(0, oldScene, 0); -
610 } -
611 -
612 // Unregister focus proxy. -
613 item->d_ptr->resetFocusProxy(); -
614 -
615 // Remove from parent, or unregister from toplevels. -
616 if (QGraphicsItem *parentItem = item->parentItem()) { -
617 if (parentItem->scene()) { -
618 Q_ASSERT_X(parentItem->scene() == q, "QGraphicsScene::removeItem", -
619 "Parent item's scene is different from this item's scene"); -
620 item->setParentItem(0); -
621 } -
622 } else { -
623 unregisterTopLevelItem(item); -
624 } -
625 -
626 // Reset the mouse grabber and focus item data. -
627 if (item == focusItem) -
628 focusItem = 0; -
629 if (item == lastFocusItem) -
630 lastFocusItem = 0; -
631 if (item == passiveFocusItem) -
632 passiveFocusItem = 0; -
633 if (item == activePanel) { -
634 // ### deactivate... -
635 activePanel = 0; -
636 } -
637 if (item == lastActivePanel) -
638 lastActivePanel = 0; -
639 -
640 // Cancel active touches -
641 { -
642 QMap<int, QGraphicsItem *>::iterator it = itemForTouchPointId.begin(); -
643 while (it != itemForTouchPointId.end()) { -
644 if (it.value() == item) { -
645 sceneCurrentTouchPoints.remove(it.key()); -
646 it = itemForTouchPointId.erase(it); -
647 } else { -
648 ++it; -
649 } -
650 } -
651 } -
652 -
653 // Disable selectionChanged() for individual items -
654 ++selectionChanging; -
655 int oldSelectedItemsSize = selectedItems.size(); -
656 -
657 // Update selected & hovered item bookkeeping -
658 selectedItems.remove(item); -
659 hoverItems.removeAll(item); -
660 cachedItemsUnderMouse.removeAll(item); -
661 if (item->d_ptr->pendingPolish) { -
662 const int unpolishedIndex = unpolishedItems.indexOf(item); -
663 if (unpolishedIndex != -1) -
664 unpolishedItems[unpolishedIndex] = 0; -
665 item->d_ptr->pendingPolish = false; -
666 } -
667 resetDirtyItem(item); -
668 -
669 //We remove all references of item from the sceneEventFilter arrays -
670 QMultiMap<QGraphicsItem*, QGraphicsItem*>::iterator iterator = sceneEventFilters.begin(); -
671 while (iterator != sceneEventFilters.end()) { -
672 if (iterator.value() == item || iterator.key() == item) -
673 iterator = sceneEventFilters.erase(iterator); -
674 else -
675 ++iterator; -
676 } -
677 -
678 if (item->isPanel() && item->isVisible() && item->panelModality() != QGraphicsItem::NonModal) -
679 leaveModal(item); -
680 -
681 // Reset the mouse grabber and focus item data. -
682 if (mouseGrabberItems.contains(item)) -
683 ungrabMouse(item, /* item is dying */ item->d_ptr->inDestructor); -
684 -
685 // Reset the keyboard grabber -
686 if (keyboardGrabberItems.contains(item)) -
687 ungrabKeyboard(item, /* item is dying */ item->d_ptr->inDestructor); -
688 -
689 // Reset the last mouse grabber item -
690 if (item == lastMouseGrabberItem) -
691 lastMouseGrabberItem = 0; -
692 -
693 // Reset the current drop item -
694 if (item == dragDropItem) -
695 dragDropItem = 0; -
696 -
697 // Reenable selectionChanged() for individual items -
698 --selectionChanging; -
699 if (!selectionChanging && selectedItems.size() != oldSelectedItemsSize) -
700 emit q->selectionChanged(); -
701 -
702#ifndef QT_NO_GESTURES -
703 QHash<QGesture *, QGraphicsObject *>::iterator it; -
704 for (it = gestureTargets.begin(); it != gestureTargets.end();) { -
705 if (it.value() == item) -
706 it = gestureTargets.erase(it); -
707 else -
708 ++it; -
709 } -
710 -
711 QGraphicsObject *dummy = static_cast<QGraphicsObject *>(item); -
712 cachedTargetItems.removeOne(dummy); -
713 cachedItemGestures.remove(dummy); -
714 cachedAlreadyDeliveredGestures.remove(dummy); -
715 -
716 foreach (Qt::GestureType gesture, item->d_ptr->gestureContext.keys()) -
717 ungrabGesture(item, gesture); -
718#endif // QT_NO_GESTURES -
719} -
720 -
721/*! -
722 \internal -
723*/ -
724void QGraphicsScenePrivate::setActivePanelHelper(QGraphicsItem *item, bool duringActivationEvent) -
725{ -
726 Q_Q(QGraphicsScene); -
727 if (item && item->scene() != q) { -
728 qWarning("QGraphicsScene::setActivePanel: item %p must be part of this scene", -
729 item); -
730 return; -
731 } -
732 -
733 // Ensure the scene has focus when we change panel activation. -
734 q->setFocus(Qt::ActiveWindowFocusReason); -
735 -
736 // Find the item's panel. -
737 QGraphicsItem *panel = item ? item->panel() : 0; -
738 lastActivePanel = panel ? activePanel : 0; -
739 if (panel == activePanel || (!q->isActive() && !duringActivationEvent)) -
740 return; -
741 -
742 // Deactivate the last active panel. -
743 if (activePanel) { -
744 if (QGraphicsItem *fi = activePanel->focusItem()) { -
745 // Remove focus from the current focus item. -
746 if (fi == q->focusItem()) -
747 q->setFocusItem(0, Qt::ActiveWindowFocusReason); -
748 } -
749 -
750 QEvent event(QEvent::WindowDeactivate); -
751 q->sendEvent(activePanel, &event); -
752 } else if (panel && !duringActivationEvent) { -
753 // Deactivate the scene if changing activation to a panel. -
754 QEvent event(QEvent::WindowDeactivate); -
755 foreach (QGraphicsItem *item, q->items()) { -
756 if (item->isVisible() && !item->isPanel() && !item->parentItem()) -
757 q->sendEvent(item, &event); -
758 } -
759 } -
760 -
761 // Update activate state. -
762 activePanel = panel; -
763 QEvent event(QEvent::ActivationChange); -
764 QApplication::sendEvent(q, &event); -
765 -
766 // Activate -
767 if (panel) { -
768 QEvent event(QEvent::WindowActivate); -
769 q->sendEvent(panel, &event); -
770 -
771 // Set focus on the panel's focus item. -
772 if (QGraphicsItem *focusItem = panel->focusItem()) -
773 focusItem->setFocus(Qt::ActiveWindowFocusReason); -
774 } else if (q->isActive()) { -
775 // Activate the scene -
776 QEvent event(QEvent::WindowActivate); -
777 foreach (QGraphicsItem *item, q->items()) { -
778 if (item->isVisible() && !item->isPanel() && !item->parentItem()) -
779 q->sendEvent(item, &event); -
780 } -
781 } -
782} -
783 -
784/*! -
785 \internal -
786*/ -
787void QGraphicsScenePrivate::setFocusItemHelper(QGraphicsItem *item, -
788 Qt::FocusReason focusReason) -
789{ -
790 Q_Q(QGraphicsScene);
executed (the execution status of this line is deduced): QGraphicsScene * const q = q_func();
-
791 if (item == focusItem)
evaluated: item == focusItem
TRUEFALSE
yes
Evaluation Count:124
yes
Evaluation Count:21
21-124
792 return;
executed: return;
Execution Count:124
124
793 -
794 // Clear focus if asked to set focus on something that can't -
795 // accept input focus. -
796 if (item && (!(item->flags() & QGraphicsItem::ItemIsFocusable)
evaluated: item
TRUEFALSE
yes
Evaluation Count:13
yes
Evaluation Count:8
partially evaluated: !(item->flags() & QGraphicsItem::ItemIsFocusable)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:13
0-13
797 || !item->isVisible() || !item->isEnabled())) {
partially evaluated: !item->isVisible()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:13
partially evaluated: !item->isEnabled()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:13
0-13
798 item = 0;
never executed (the execution status of this line is deduced): item = 0;
-
799 }
never executed: }
0
800 -
801 // Set focus on the scene if an item requests focus. -
802 if (item) {
evaluated: item
TRUEFALSE
yes
Evaluation Count:13
yes
Evaluation Count:8
8-13
803 q->setFocus(focusReason);
executed (the execution status of this line is deduced): q->setFocus(focusReason);
-
804 if (item == focusItem)
partially evaluated: item == focusItem
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:13
0-13
805 return;
never executed: return;
0
806 }
executed: }
Execution Count:13
13
807 -
808 if (focusItem) {
evaluated: focusItem
TRUEFALSE
yes
Evaluation Count:13
yes
Evaluation Count:8
8-13
809 lastFocusItem = focusItem;
executed (the execution status of this line is deduced): lastFocusItem = focusItem;
-
810 -
811#ifndef QT_NO_IM -
812 if (lastFocusItem->flags() & QGraphicsItem::ItemAcceptsInputMethod) {
evaluated: lastFocusItem->flags() & QGraphicsItem::ItemAcceptsInputMethod
TRUEFALSE
yes
Evaluation Count:8
yes
Evaluation Count:5
5-8
813 // Close any external input method panel. This happens -
814 // automatically by removing WA_InputMethodEnabled on -
815 // the views, but if we are changing focus, we have to -
816 // do it ourselves. -
817 if (qApp)
partially evaluated: (static_cast<QApplication *>(QCoreApplication::instance()))
TRUEFALSE
yes
Evaluation Count:8
no
Evaluation Count:0
0-8
818 qApp->inputMethod()->commit();
executed: (static_cast<QApplication *>(QCoreApplication::instance()))->inputMethod()->commit();
Execution Count:8
8
819 }
executed: }
Execution Count:8
8
820#endif //QT_NO_IM -
821 -
822 focusItem = 0;
executed (the execution status of this line is deduced): focusItem = 0;
-
823 QFocusEvent event(QEvent::FocusOut, focusReason);
executed (the execution status of this line is deduced): QFocusEvent event(QEvent::FocusOut, focusReason);
-
824 sendEvent(lastFocusItem, &event);
executed (the execution status of this line is deduced): sendEvent(lastFocusItem, &event);
-
825 }
executed: }
Execution Count:13
13
826 -
827 // This handles the case that the item has been removed from the -
828 // scene in response to the FocusOut event. -
829 if (item && item->scene() != q)
evaluated: item
TRUEFALSE
yes
Evaluation Count:13
yes
Evaluation Count:8
partially evaluated: item->scene() != q
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:13
0-13
830 item = 0;
never executed: item = 0;
0
831 -
832 if (item)
evaluated: item
TRUEFALSE
yes
Evaluation Count:13
yes
Evaluation Count:8
8-13
833 focusItem = item;
executed: focusItem = item;
Execution Count:13
13
834 updateInputMethodSensitivityInViews();
executed (the execution status of this line is deduced): updateInputMethodSensitivityInViews();
-
835 -
836 #ifndef QT_NO_ACCESSIBILITY8-13
if (focusItem) {
if (QGraphicsObject *focusObj = focusItem->toGraphicsObject()) {
QAccessibleEvent event(focusObj, QAccessible::Focus);
QAccessible::updateAccessibility(&event);
}
}
#endifif (item) {
evaluated: item
TRUEFALSE
yes
Evaluation Count:13
yes
Evaluation Count:8
837 QFocusEvent event(QEvent::FocusIn, focusReason);
executed (the execution status of this line is deduced): QFocusEvent event(QEvent::FocusIn, focusReason);
-
838 sendEvent(item, &event);
executed (the execution status of this line is deduced): sendEvent(item, &event);
-
839 }
executed: }
Execution Count:13
13
840}
executed: }
Execution Count:21
21
841 -
842/*! -
843 \internal -
844*/ -
845void QGraphicsScenePrivate::addPopup(QGraphicsWidget *widget) -
846{ -
847 Q_ASSERT(widget); -
848 Q_ASSERT(!popupWidgets.contains(widget)); -
849 popupWidgets << widget; -
850 if (QGraphicsWidget *focusWidget = widget->focusWidget()) { -
851 focusWidget->setFocus(Qt::PopupFocusReason); -
852 } else { -
853 grabKeyboard((QGraphicsItem *)widget); -
854 if (focusItem && popupWidgets.size() == 1) { -
855 QFocusEvent event(QEvent::FocusOut, Qt::PopupFocusReason); -
856 sendEvent(focusItem, &event); -
857 } -
858 } -
859 grabMouse((QGraphicsItem *)widget); -
860} -
861 -
862/*! -
863 \internal -
864 -
865 Remove \a widget from the popup list. Important notes: -
866 -
867 \a widget is guaranteed to be in the list of popups, but it might not be -
868 the last entry; you can hide any item in the pop list before the others, -
869 and this must cause all later mouse grabbers to lose the grab. -
870*/ -
871void QGraphicsScenePrivate::removePopup(QGraphicsWidget *widget, bool itemIsDying) -
872{ -
873 Q_ASSERT(widget); -
874 int index = popupWidgets.indexOf(widget); -
875 Q_ASSERT(index != -1); -
876 -
877 for (int i = popupWidgets.size() - 1; i >= index; --i) { -
878 QGraphicsWidget *widget = popupWidgets.takeLast(); -
879 ungrabMouse(widget, itemIsDying); -
880 if (focusItem && popupWidgets.isEmpty()) { -
881 QFocusEvent event(QEvent::FocusIn, Qt::PopupFocusReason); -
882 sendEvent(focusItem, &event); -
883 } else if (keyboardGrabberItems.contains(static_cast<QGraphicsItem *>(widget))) { -
884 ungrabKeyboard(static_cast<QGraphicsItem *>(widget), itemIsDying); -
885 } -
886 if (!itemIsDying && widget->isVisible()) { -
887 widget->QGraphicsItem::d_ptr->setVisibleHelper(false, /* explicit = */ false); -
888 } -
889 } -
890} -
891 -
892/*! -
893 \internal -
894*/ -
895void QGraphicsScenePrivate::grabMouse(QGraphicsItem *item, bool implicit) -
896{ -
897 // Append to list of mouse grabber items, and send a mouse grab event. -
898 if (mouseGrabberItems.contains(item)) { -
899 if (mouseGrabberItems.last() == item) { -
900 Q_ASSERT(!implicit); -
901 if (!lastMouseGrabberItemHasImplicitMouseGrab) { -
902 qWarning("QGraphicsItem::grabMouse: already a mouse grabber"); -
903 } else { -
904 // Upgrade to an explicit mouse grab -
905 lastMouseGrabberItemHasImplicitMouseGrab = false; -
906 } -
907 } else { -
908 qWarning("QGraphicsItem::grabMouse: already blocked by mouse grabber: %p", -
909 mouseGrabberItems.last()); -
910 } -
911 return; -
912 } -
913 -
914 // Send ungrab event to the last grabber. -
915 if (!mouseGrabberItems.isEmpty()) { -
916 QGraphicsItem *last = mouseGrabberItems.last(); -
917 if (lastMouseGrabberItemHasImplicitMouseGrab) { -
918 // Implicit mouse grab is immediately lost. -
919 last->ungrabMouse(); -
920 } else { -
921 // Just send ungrab event to current grabber. -
922 QEvent ungrabEvent(QEvent::UngrabMouse); -
923 sendEvent(last, &ungrabEvent); -
924 } -
925 } -
926 -
927 mouseGrabberItems << item; -
928 lastMouseGrabberItemHasImplicitMouseGrab = implicit; -
929 -
930 // Send grab event to current grabber. -
931 QEvent grabEvent(QEvent::GrabMouse); -
932 sendEvent(item, &grabEvent); -
933} -
934 -
935/*! -
936 \internal -
937*/ -
938void QGraphicsScenePrivate::ungrabMouse(QGraphicsItem *item, bool itemIsDying) -
939{ -
940 int index = mouseGrabberItems.indexOf(item); -
941 if (index == -1) { -
942 qWarning("QGraphicsItem::ungrabMouse: not a mouse grabber"); -
943 return; -
944 } -
945 -
946 if (item != mouseGrabberItems.last()) { -
947 // Recursively ungrab the next mouse grabber until we reach this item -
948 // to ensure state consistency. -
949 ungrabMouse(mouseGrabberItems.at(index + 1), itemIsDying); -
950 } -
951 if (!popupWidgets.isEmpty() && item == popupWidgets.last()) { -
952 // If the item is a popup, go via removePopup to ensure state -
953 // consistency and that it gets hidden correctly - beware that -
954 // removePopup() reenters this function to continue removing the grab. -
955 removePopup((QGraphicsWidget *)item, itemIsDying); -
956 return; -
957 } -
958 -
959 // Send notification about mouse ungrab. -
960 if (!itemIsDying) { -
961 QEvent event(QEvent::UngrabMouse); -
962 sendEvent(item, &event); -
963 } -
964 -
965 // Remove the item from the list of grabbers. Whenever this happens, we -
966 // reset the implicitGrab (there can be only ever be one implicit grabber -
967 // in a scene, and it is always the latest grabber; if the implicit grab -
968 // is lost, it is not automatically regained. -
969 mouseGrabberItems.takeLast(); -
970 lastMouseGrabberItemHasImplicitMouseGrab = false; -
971 -
972 // Send notification about mouse regrab. ### It's unfortunate that all the -
973 // items get a GrabMouse event, but this is a rare case with a simple -
974 // implementation and it does ensure a consistent state. -
975 if (!itemIsDying && !mouseGrabberItems.isEmpty()) { -
976 QGraphicsItem *last = mouseGrabberItems.last(); -
977 QEvent event(QEvent::GrabMouse); -
978 sendEvent(last, &event); -
979 } -
980} -
981 -
982/*! -
983 \internal -
984*/ -
985void QGraphicsScenePrivate::clearMouseGrabber() -
986{ -
987 if (!mouseGrabberItems.isEmpty()) -
988 mouseGrabberItems.first()->ungrabMouse(); -
989 lastMouseGrabberItem = 0; -
990} -
991 -
992/*! -
993 \internal -
994*/ -
995void QGraphicsScenePrivate::grabKeyboard(QGraphicsItem *item) -
996{ -
997 if (keyboardGrabberItems.contains(item)) { -
998 if (keyboardGrabberItems.last() == item) -
999 qWarning("QGraphicsItem::grabKeyboard: already a keyboard grabber"); -
1000 else -
1001 qWarning("QGraphicsItem::grabKeyboard: already blocked by keyboard grabber: %p", -
1002 keyboardGrabberItems.last()); -
1003 return; -
1004 } -
1005 -
1006 // Send ungrab event to the last grabber. -
1007 if (!keyboardGrabberItems.isEmpty()) { -
1008 // Just send ungrab event to current grabber. -
1009 QEvent ungrabEvent(QEvent::UngrabKeyboard); -
1010 sendEvent(keyboardGrabberItems.last(), &ungrabEvent); -
1011 } -
1012 -
1013 keyboardGrabberItems << item; -
1014 -
1015 // Send grab event to current grabber. -
1016 QEvent grabEvent(QEvent::GrabKeyboard); -
1017 sendEvent(item, &grabEvent); -
1018} -
1019 -
1020/*! -
1021 \internal -
1022*/ -
1023void QGraphicsScenePrivate::ungrabKeyboard(QGraphicsItem *item, bool itemIsDying) -
1024{ -
1025 int index = keyboardGrabberItems.lastIndexOf(item); -
1026 if (index == -1) { -
1027 qWarning("QGraphicsItem::ungrabKeyboard: not a keyboard grabber"); -
1028 return; -
1029 } -
1030 if (item != keyboardGrabberItems.last()) { -
1031 // Recursively ungrab the topmost keyboard grabber until we reach this -
1032 // item to ensure state consistency. -
1033 ungrabKeyboard(keyboardGrabberItems.at(index + 1), itemIsDying); -
1034 } -
1035 -
1036 // Send notification about keyboard ungrab. -
1037 if (!itemIsDying) { -
1038 QEvent event(QEvent::UngrabKeyboard); -
1039 sendEvent(item, &event); -
1040 } -
1041 -
1042 // Remove the item from the list of grabbers. -
1043 keyboardGrabberItems.takeLast(); -
1044 -
1045 // Send notification about mouse regrab. -
1046 if (!itemIsDying && !keyboardGrabberItems.isEmpty()) { -
1047 QGraphicsItem *last = keyboardGrabberItems.last(); -
1048 QEvent event(QEvent::GrabKeyboard); -
1049 sendEvent(last, &event); -
1050 } -
1051} -
1052 -
1053/*! -
1054 \internal -
1055*/ -
1056void QGraphicsScenePrivate::clearKeyboardGrabber() -
1057{ -
1058 if (!keyboardGrabberItems.isEmpty()) -
1059 ungrabKeyboard(keyboardGrabberItems.first()); -
1060} -
1061 -
1062void QGraphicsScenePrivate::enableMouseTrackingOnViews() -
1063{ -
1064 foreach (QGraphicsView *view, views) -
1065 view->viewport()->setMouseTracking(true); -
1066} -
1067 -
1068/*! -
1069 Returns all items for the screen position in \a event. -
1070*/ -
1071QList<QGraphicsItem *> QGraphicsScenePrivate::itemsAtPosition(const QPoint &screenPos, -
1072 const QPointF &scenePos, -
1073 QWidget *widget) const -
1074{ -
1075 Q_Q(const QGraphicsScene); -
1076 QGraphicsView *view = widget ? qobject_cast<QGraphicsView *>(widget->parentWidget()) : 0; -
1077 if (!view) -
1078 return q->items(scenePos, Qt::IntersectsItemShape, Qt::DescendingOrder, QTransform()); -
1079 -
1080 const QRectF pointRect(QPointF(widget->mapFromGlobal(screenPos)), QSizeF(1, 1)); -
1081 if (!view->isTransformed()) -
1082 return q->items(pointRect, Qt::IntersectsItemShape, Qt::DescendingOrder); -
1083 -
1084 const QTransform viewTransform = view->viewportTransform(); -
1085 if (viewTransform.type() <= QTransform::TxScale) { -
1086 return q->items(viewTransform.inverted().mapRect(pointRect), Qt::IntersectsItemShape, -
1087 Qt::DescendingOrder, viewTransform); -
1088 } -
1089 return q->items(viewTransform.inverted().map(pointRect), Qt::IntersectsItemShape, -
1090 Qt::DescendingOrder, viewTransform); -
1091} -
1092 -
1093/*! -
1094 \internal -
1095*/ -
1096void QGraphicsScenePrivate::storeMouseButtonsForMouseGrabber(QGraphicsSceneMouseEvent *event) -
1097{ -
1098 for (int i = 0x1; i <= 0x10; i <<= 1) { -
1099 if (event->buttons() & i) { -
1100 mouseGrabberButtonDownPos.insert(Qt::MouseButton(i), -
1101 mouseGrabberItems.last()->d_ptr->genericMapFromScene(event->scenePos(), -
1102 event->widget())); -
1103 mouseGrabberButtonDownScenePos.insert(Qt::MouseButton(i), event->scenePos()); -
1104 mouseGrabberButtonDownScreenPos.insert(Qt::MouseButton(i), event->screenPos()); -
1105 } -
1106 } -
1107} -
1108 -
1109/*! -
1110 \internal -
1111*/ -
1112void QGraphicsScenePrivate::installSceneEventFilter(QGraphicsItem *watched, QGraphicsItem *filter) -
1113{ -
1114 sceneEventFilters.insert(watched, filter); -
1115} -
1116 -
1117/*! -
1118 \internal -
1119*/ -
1120void QGraphicsScenePrivate::removeSceneEventFilter(QGraphicsItem *watched, QGraphicsItem *filter) -
1121{ -
1122 if (!sceneEventFilters.contains(watched)) -
1123 return; -
1124 -
1125 QMultiMap<QGraphicsItem *, QGraphicsItem *>::Iterator it = sceneEventFilters.lowerBound(watched); -
1126 QMultiMap<QGraphicsItem *, QGraphicsItem *>::Iterator end = sceneEventFilters.upperBound(watched); -
1127 do { -
1128 if (it.value() == filter) -
1129 it = sceneEventFilters.erase(it); -
1130 else -
1131 ++it; -
1132 } while (it != end); -
1133} -
1134 -
1135/*! -
1136 \internal -
1137*/ -
1138bool QGraphicsScenePrivate::filterDescendantEvent(QGraphicsItem *item, QEvent *event) -
1139{ -
1140 if (item && (item->d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorFiltersChildEvents)) { -
1141 QGraphicsItem *parent = item->parentItem(); -
1142 while (parent) { -
1143 if (parent->d_ptr->filtersDescendantEvents && parent->sceneEventFilter(item, event)) -
1144 return true; -
1145 if (!(parent->d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorFiltersChildEvents)) -
1146 return false; -
1147 parent = parent->parentItem(); -
1148 } -
1149 } -
1150 return false; -
1151} -
1152 -
1153/*! -
1154 \internal -
1155*/ -
1156bool QGraphicsScenePrivate::filterEvent(QGraphicsItem *item, QEvent *event) -
1157{ -
1158 if (item && !sceneEventFilters.contains(item)) -
1159 return false; -
1160 -
1161 QMultiMap<QGraphicsItem *, QGraphicsItem *>::Iterator it = sceneEventFilters.lowerBound(item); -
1162 QMultiMap<QGraphicsItem *, QGraphicsItem *>::Iterator end = sceneEventFilters.upperBound(item); -
1163 while (it != end) { -
1164 // ### The filterer and filteree might both be deleted. -
1165 if (it.value()->sceneEventFilter(it.key(), event)) -
1166 return true; -
1167 ++it; -
1168 } -
1169 return false; -
1170} -
1171 -
1172/*! -
1173 \internal -
1174 -
1175 This is the final dispatch point for any events from the scene to the -
1176 item. It filters the event first - if the filter returns true, the event -
1177 is considered to have been eaten by the filter, and is therefore stopped -
1178 (the default filter returns false). Then/otherwise, if the item is -
1179 enabled, the event is sent; otherwise it is stopped. -
1180*/ -
1181bool QGraphicsScenePrivate::sendEvent(QGraphicsItem *item, QEvent *event) -
1182{ -
1183 if (QGraphicsObject *object = item->toGraphicsObject()) { -
1184#ifndef QT_NO_GESTURES -
1185 QGestureManager *gestureManager = QApplicationPrivate::instance()->gestureManager; -
1186 if (gestureManager) { -
1187 if (gestureManager->filterEvent(object, event)) -
1188 return true; -
1189 } -
1190#endif // QT_NO_GESTURES -
1191 } -
1192 -
1193 if (filterEvent(item, event)) -
1194 return false; -
1195 if (filterDescendantEvent(item, event)) -
1196 return false; -
1197 if (!item || !item->isEnabled()) -
1198 return false; -
1199 if (QGraphicsObject *o = item->toGraphicsObject()) { -
1200 bool spont = event->spontaneous(); -
1201 if (spont ? qt_sendSpontaneousEvent(o, event) : QApplication::sendEvent(o, event)) -
1202 return true; -
1203 event->spont = spont; -
1204 } -
1205 return item->sceneEvent(event); -
1206} -
1207 -
1208/*! -
1209 \internal -
1210*/ -
1211void QGraphicsScenePrivate::cloneDragDropEvent(QGraphicsSceneDragDropEvent *dest, -
1212 QGraphicsSceneDragDropEvent *source) -
1213{ -
1214 dest->setWidget(source->widget()); -
1215 dest->setPos(source->pos()); -
1216 dest->setScenePos(source->scenePos()); -
1217 dest->setScreenPos(source->screenPos()); -
1218 dest->setButtons(source->buttons()); -
1219 dest->setModifiers(source->modifiers()); -
1220 dest->setPossibleActions(source->possibleActions()); -
1221 dest->setProposedAction(source->proposedAction()); -
1222 dest->setDropAction(source->dropAction()); -
1223 dest->setSource(source->source()); -
1224 dest->setMimeData(source->mimeData()); -
1225} -
1226 -
1227/*! -
1228 \internal -
1229*/ -
1230void QGraphicsScenePrivate::sendDragDropEvent(QGraphicsItem *item, -
1231 QGraphicsSceneDragDropEvent *dragDropEvent) -
1232{ -
1233 dragDropEvent->setPos(item->d_ptr->genericMapFromScene(dragDropEvent->scenePos(), dragDropEvent->widget())); -
1234 sendEvent(item, dragDropEvent); -
1235} -
1236 -
1237/*! -
1238 \internal -
1239*/ -
1240void QGraphicsScenePrivate::sendHoverEvent(QEvent::Type type, QGraphicsItem *item, -
1241 QGraphicsSceneHoverEvent *hoverEvent) -
1242{ -
1243 QGraphicsSceneHoverEvent event(type); -
1244 event.setWidget(hoverEvent->widget()); -
1245 event.setPos(item->d_ptr->genericMapFromScene(hoverEvent->scenePos(), hoverEvent->widget())); -
1246 event.setScenePos(hoverEvent->scenePos()); -
1247 event.setScreenPos(hoverEvent->screenPos()); -
1248 event.setLastPos(item->d_ptr->genericMapFromScene(hoverEvent->lastScenePos(), hoverEvent->widget())); -
1249 event.setLastScenePos(hoverEvent->lastScenePos()); -
1250 event.setLastScreenPos(hoverEvent->lastScreenPos()); -
1251 event.setModifiers(hoverEvent->modifiers()); -
1252 sendEvent(item, &event); -
1253} -
1254 -
1255/*! -
1256 \internal -
1257*/ -
1258void QGraphicsScenePrivate::sendMouseEvent(QGraphicsSceneMouseEvent *mouseEvent) -
1259{ -
1260 if (mouseEvent->button() == 0 && mouseEvent->buttons() == 0 && lastMouseGrabberItemHasImplicitMouseGrab) { -
1261 // ### This is a temporary fix for until we get proper mouse -
1262 // grab events. -
1263 clearMouseGrabber(); -
1264 return; -
1265 } -
1266 -
1267 QGraphicsItem *item = mouseGrabberItems.last(); -
1268 if (item->isBlockedByModalPanel()) -
1269 return; -
1270 -
1271 for (int i = 0x1; i <= 0x10; i <<= 1) { -
1272 Qt::MouseButton button = Qt::MouseButton(i); -
1273 mouseEvent->setButtonDownPos(button, mouseGrabberButtonDownPos.value(button, item->d_ptr->genericMapFromScene(mouseEvent->scenePos(), mouseEvent->widget()))); -
1274 mouseEvent->setButtonDownScenePos(button, mouseGrabberButtonDownScenePos.value(button, mouseEvent->scenePos())); -
1275 mouseEvent->setButtonDownScreenPos(button, mouseGrabberButtonDownScreenPos.value(button, mouseEvent->screenPos())); -
1276 } -
1277 mouseEvent->setPos(item->d_ptr->genericMapFromScene(mouseEvent->scenePos(), mouseEvent->widget())); -
1278 mouseEvent->setLastPos(item->d_ptr->genericMapFromScene(mouseEvent->lastScenePos(), mouseEvent->widget())); -
1279 sendEvent(item, mouseEvent); -
1280} -
1281 -
1282/*! -
1283 \internal -
1284*/ -
1285void QGraphicsScenePrivate::mousePressEventHandler(QGraphicsSceneMouseEvent *mouseEvent) -
1286{ -
1287 Q_Q(QGraphicsScene); -
1288 -
1289 // Ignore by default, unless we find a mouse grabber that accepts it. -
1290 mouseEvent->ignore(); -
1291 -
1292 // Deliver to any existing mouse grabber. -
1293 if (!mouseGrabberItems.isEmpty()) { -
1294 if (mouseGrabberItems.last()->isBlockedByModalPanel()) -
1295 return; -
1296 // The event is ignored by default, but we disregard the event's -
1297 // accepted state after delivery; the mouse is grabbed, after all. -
1298 sendMouseEvent(mouseEvent); -
1299 return; -
1300 } -
1301 -
1302 // Start by determining the number of items at the current position. -
1303 // Reuse value from earlier calculations if possible. -
1304 if (cachedItemsUnderMouse.isEmpty()) { -
1305 cachedItemsUnderMouse = itemsAtPosition(mouseEvent->screenPos(), -
1306 mouseEvent->scenePos(), -
1307 mouseEvent->widget()); -
1308 } -
1309 -
1310 // Update window activation. -
1311 QGraphicsItem *topItem = cachedItemsUnderMouse.value(0); -
1312 QGraphicsWidget *newActiveWindow = topItem ? topItem->window() : 0; -
1313 if (newActiveWindow && newActiveWindow->isBlockedByModalPanel(&topItem)) { -
1314 // pass activation to the blocking modal window -
1315 newActiveWindow = topItem ? topItem->window() : 0; -
1316 } -
1317 -
1318 if (newActiveWindow != q->activeWindow()) -
1319 q->setActiveWindow(newActiveWindow); -
1320 -
1321 // Set focus on the topmost enabled item that can take focus. -
1322 bool setFocus = false; -
1323 -
1324 foreach (QGraphicsItem *item, cachedItemsUnderMouse) { -
1325 if (item->isBlockedByModalPanel() -
1326 || (item->d_ptr->flags & QGraphicsItem::ItemStopsFocusHandling)) { -
1327 // Make sure we don't clear focus. -
1328 setFocus = true; -
1329 break; -
1330 } -
1331 if (item->isEnabled() && ((item->flags() & QGraphicsItem::ItemIsFocusable))) { -
1332 if (!item->isWidget() || ((QGraphicsWidget *)item)->focusPolicy() & Qt::ClickFocus) { -
1333 setFocus = true; -
1334 if (item != q->focusItem() && item->d_ptr->mouseSetsFocus) -
1335 q->setFocusItem(item, Qt::MouseFocusReason); -
1336 break; -
1337 } -
1338 } -
1339 if (item->isPanel()) -
1340 break; -
1341 if (item->d_ptr->flags & QGraphicsItem::ItemStopsClickFocusPropagation) -
1342 break; -
1343 } -
1344 -
1345 // Check for scene modality. -
1346 bool sceneModality = false; -
1347 for (int i = 0; i < modalPanels.size(); ++i) { -
1348 if (modalPanels.at(i)->panelModality() == QGraphicsItem::SceneModal) { -
1349 sceneModality = true; -
1350 break; -
1351 } -
1352 } -
1353 -
1354 // If nobody could take focus, clear it. -
1355 if (!stickyFocus && !setFocus && !sceneModality) -
1356 q->setFocusItem(0, Qt::MouseFocusReason); -
1357 -
1358 // Any item will do. -
1359 if (sceneModality && cachedItemsUnderMouse.isEmpty()) -
1360 cachedItemsUnderMouse << modalPanels.first(); -
1361 -
1362 // Find a mouse grabber by sending mouse press events to all mouse grabber -
1363 // candidates one at a time, until the event is accepted. It's accepted by -
1364 // default, so the receiver has to explicitly ignore it for it to pass -
1365 // through. -
1366 foreach (QGraphicsItem *item, cachedItemsUnderMouse) { -
1367 if (!(item->acceptedMouseButtons() & mouseEvent->button())) { -
1368 // Skip items that don't accept the event's mouse button. -
1369 continue; -
1370 } -
1371 -
1372 // Check if this item is blocked by a modal panel and deliver the mouse event to the -
1373 // blocking panel instead of this item if blocked. -
1374 (void) item->isBlockedByModalPanel(&item); -
1375 -
1376 grabMouse(item, /* implicit = */ true); -
1377 mouseEvent->accept(); -
1378 -
1379 // check if the item we are sending to are disabled (before we send the event) -
1380 bool disabled = !item->isEnabled(); -
1381 bool isPanel = item->isPanel(); -
1382 if (mouseEvent->type() == QEvent::GraphicsSceneMouseDoubleClick -
1383 && item != lastMouseGrabberItem && lastMouseGrabberItem) { -
1384 // If this item is different from the item that received the last -
1385 // mouse event, and mouseEvent is a doubleclick event, then the -
1386 // event is converted to a press. Known limitation: -
1387 // Triple-clicking will not generate a doubleclick, though. -
1388 QGraphicsSceneMouseEvent mousePress(QEvent::GraphicsSceneMousePress); -
1389 mousePress.spont = mouseEvent->spont; -
1390 mousePress.accept(); -
1391 mousePress.setButton(mouseEvent->button()); -
1392 mousePress.setButtons(mouseEvent->buttons()); -
1393 mousePress.setScreenPos(mouseEvent->screenPos()); -
1394 mousePress.setScenePos(mouseEvent->scenePos()); -
1395 mousePress.setModifiers(mouseEvent->modifiers()); -
1396 mousePress.setWidget(mouseEvent->widget()); -
1397 mousePress.setButtonDownPos(mouseEvent->button(), -
1398 mouseEvent->buttonDownPos(mouseEvent->button())); -
1399 mousePress.setButtonDownScenePos(mouseEvent->button(), -
1400 mouseEvent->buttonDownScenePos(mouseEvent->button())); -
1401 mousePress.setButtonDownScreenPos(mouseEvent->button(), -
1402 mouseEvent->buttonDownScreenPos(mouseEvent->button())); -
1403 sendMouseEvent(&mousePress); -
1404 mouseEvent->setAccepted(mousePress.isAccepted()); -
1405 } else { -
1406 sendMouseEvent(mouseEvent); -
1407 } -
1408 -
1409 bool dontSendUngrabEvents = mouseGrabberItems.isEmpty() || mouseGrabberItems.last() != item; -
1410 if (disabled) { -
1411 ungrabMouse(item, /* itemIsDying = */ dontSendUngrabEvents); -
1412 break; -
1413 } -
1414 if (mouseEvent->isAccepted()) { -
1415 if (!mouseGrabberItems.isEmpty()) -
1416 storeMouseButtonsForMouseGrabber(mouseEvent); -
1417 lastMouseGrabberItem = item; -
1418 return; -
1419 } -
1420 ungrabMouse(item, /* itemIsDying = */ dontSendUngrabEvents); -
1421 -
1422 // Don't propagate through panels. -
1423 if (isPanel) -
1424 break; -
1425 } -
1426 -
1427 // Is the event still ignored? Then the mouse press goes to the scene. -
1428 // Reset the mouse grabber, clear the selection, clear focus, and leave -
1429 // the event ignored so that it can propagate through the originating -
1430 // view. -
1431 if (!mouseEvent->isAccepted()) { -
1432 clearMouseGrabber(); -
1433 -
1434 QGraphicsView *view = mouseEvent->widget() ? qobject_cast<QGraphicsView *>(mouseEvent->widget()->parentWidget()) : 0; -
1435 bool dontClearSelection = view && view->dragMode() == QGraphicsView::ScrollHandDrag; -
1436 if (!dontClearSelection) { -
1437 // Clear the selection if the originating view isn't in scroll -
1438 // hand drag mode. The view will clear the selection if no drag -
1439 // happened. -
1440 q->clearSelection(); -
1441 } -
1442 } -
1443} -
1444 -
1445/*! -
1446 \internal -
1447 -
1448 Ensures that the list of toplevels is sorted by insertion order, and that -
1449 the siblingIndexes are packed (no gaps), and start at 0. -
1450 -
1451 ### This function is almost identical to -
1452 QGraphicsItemPrivate::ensureSequentialSiblingIndex(). -
1453*/ -
1454void QGraphicsScenePrivate::ensureSequentialTopLevelSiblingIndexes() -
1455{ -
1456 if (!topLevelSequentialOrdering) { -
1457 std::sort(topLevelItems.begin(), topLevelItems.end(), QGraphicsItemPrivate::insertionOrder); -
1458 topLevelSequentialOrdering = true; -
1459 needSortTopLevelItems = 1; -
1460 } -
1461 if (holesInTopLevelSiblingIndex) { -
1462 holesInTopLevelSiblingIndex = 0; -
1463 for (int i = 0; i < topLevelItems.size(); ++i) -
1464 topLevelItems[i]->d_ptr->siblingIndex = i; -
1465 } -
1466} -
1467 -
1468/*! -
1469 \internal -
1470 -
1471 Set the font and propagate the changes if the font is different from the -
1472 current font. -
1473*/ -
1474void QGraphicsScenePrivate::setFont_helper(const QFont &font) -
1475{ -
1476 if (this->font == font && this->font.resolve() == font.resolve()) -
1477 return; -
1478 updateFont(font); -
1479} -
1480 -
1481/*! -
1482 \internal -
1483 -
1484 Resolve the scene's font against the application font, and propagate the -
1485 changes too all items in the scene. -
1486*/ -
1487void QGraphicsScenePrivate::resolveFont() -
1488{ -
1489 QFont naturalFont = QApplication::font(); -
1490 naturalFont.resolve(0); -
1491 QFont resolvedFont = font.resolve(naturalFont); -
1492 updateFont(resolvedFont); -
1493} -
1494 -
1495/*! -
1496 \internal -
1497 -
1498 Update the font, and whether or not it has changed, reresolve all fonts in -
1499 the scene. -
1500*/ -
1501void QGraphicsScenePrivate::updateFont(const QFont &font) -
1502{ -
1503 Q_Q(QGraphicsScene); -
1504 -
1505 // Update local font setting. -
1506 this->font = font; -
1507 -
1508 // Resolve the fonts of all top-level widget items, or widget items -
1509 // whose parent is not a widget. -
1510 foreach (QGraphicsItem *item, q->items()) { -
1511 if (!item->parentItem()) { -
1512 // Resolvefont for an item is a noop operation, but -
1513 // every item can be a widget, or can have a widget -
1514 // childre. -
1515 item->d_ptr->resolveFont(font.resolve()); -
1516 } -
1517 } -
1518 -
1519 // Send the scene a FontChange event. -
1520 QEvent event(QEvent::FontChange); -
1521 QApplication::sendEvent(q, &event); -
1522} -
1523 -
1524/*! -
1525 \internal -
1526 -
1527 Set the palette and propagate the changes if the palette is different from -
1528 the current palette. -
1529*/ -
1530void QGraphicsScenePrivate::setPalette_helper(const QPalette &palette) -
1531{ -
1532 if (this->palette == palette && this->palette.resolve() == palette.resolve()) -
1533 return; -
1534 updatePalette(palette); -
1535} -
1536 -
1537/*! -
1538 \internal -
1539 -
1540 Resolve the scene's palette against the application palette, and propagate -
1541 the changes too all items in the scene. -
1542*/ -
1543void QGraphicsScenePrivate::resolvePalette() -
1544{ -
1545 QPalette naturalPalette = QApplication::palette(); -
1546 naturalPalette.resolve(0); -
1547 QPalette resolvedPalette = palette.resolve(naturalPalette); -
1548 updatePalette(resolvedPalette); -
1549} -
1550 -
1551/*! -
1552 \internal -
1553 -
1554 Update the palette, and whether or not it has changed, reresolve all -
1555 palettes in the scene. -
1556*/ -
1557void QGraphicsScenePrivate::updatePalette(const QPalette &palette) -
1558{ -
1559 Q_Q(QGraphicsScene); -
1560 -
1561 // Update local palette setting. -
1562 this->palette = palette; -
1563 -
1564 // Resolve the palettes of all top-level widget items, or widget items -
1565 // whose parent is not a widget. -
1566 foreach (QGraphicsItem *item, q->items()) { -
1567 if (!item->parentItem()) { -
1568 // Resolvefont for an item is a noop operation, but -
1569 // every item can be a widget, or can have a widget -
1570 // childre. -
1571 item->d_ptr->resolvePalette(palette.resolve()); -
1572 } -
1573 } -
1574 -
1575 // Send the scene a PaletteChange event. -
1576 QEvent event(QEvent::PaletteChange); -
1577 QApplication::sendEvent(q, &event); -
1578} -
1579 -
1580/*! -
1581 Constructs a QGraphicsScene object. The \a parent parameter is -
1582 passed to QObject's constructor. -
1583*/ -
1584QGraphicsScene::QGraphicsScene(QObject *parent) -
1585 : QObject(*new QGraphicsScenePrivate, parent) -
1586{ -
1587 d_func()->init(); -
1588} -
1589 -
1590/*! -
1591 Constructs a QGraphicsScene object, using \a sceneRect for its -
1592 scene rectangle. The \a parent parameter is passed to QObject's -
1593 constructor. -
1594 -
1595 \sa sceneRect -
1596*/ -
1597QGraphicsScene::QGraphicsScene(const QRectF &sceneRect, QObject *parent) -
1598 : QObject(*new QGraphicsScenePrivate, parent) -
1599{ -
1600 d_func()->init(); -
1601 setSceneRect(sceneRect); -
1602} -
1603 -
1604/*! -
1605 Constructs a QGraphicsScene object, using the rectangle specified -
1606 by (\a x, \a y), and the given \a width and \a height for its -
1607 scene rectangle. The \a parent parameter is passed to QObject's -
1608 constructor. -
1609 -
1610 \sa sceneRect -
1611*/ -
1612QGraphicsScene::QGraphicsScene(qreal x, qreal y, qreal width, qreal height, QObject *parent) -
1613 : QObject(*new QGraphicsScenePrivate, parent) -
1614{ -
1615 d_func()->init(); -
1616 setSceneRect(x, y, width, height); -
1617} -
1618 -
1619/*! -
1620 Removes and deletes all items from the scene object -
1621 before destroying the scene object. The scene object -
1622 is removed from the application's global scene list, -
1623 and it is removed from all associated views. -
1624*/ -
1625QGraphicsScene::~QGraphicsScene() -
1626{ -
1627 Q_D(QGraphicsScene); -
1628 -
1629 // Remove this scene from qApp's global scene list. -
1630 if (!QApplicationPrivate::is_app_closing) -
1631 qApp->d_func()->scene_list.removeAll(this); -
1632 -
1633 clear(); -
1634 -
1635 // Remove this scene from all associated views. -
1636 for (int j = 0; j < d->views.size(); ++j) -
1637 d->views.at(j)->setScene(0); -
1638} -
1639 -
1640/*! -
1641 \property QGraphicsScene::sceneRect -
1642 \brief the scene rectangle; the bounding rectangle of the scene -
1643 -
1644 The scene rectangle defines the extent of the scene. It is -
1645 primarily used by QGraphicsView to determine the view's default -
1646 scrollable area, and by QGraphicsScene to manage item indexing. -
1647 -
1648 If unset, or if set to a null QRectF, sceneRect() will return the largest -
1649 bounding rect of all items on the scene since the scene was created (i.e., -
1650 a rectangle that grows when items are added to or moved in the scene, but -
1651 never shrinks). -
1652 -
1653 \sa width(), height(), QGraphicsView::sceneRect -
1654*/ -
1655QRectF QGraphicsScene::sceneRect() const -
1656{ -
1657 Q_D(const QGraphicsScene); -
1658 if (d->hasSceneRect) -
1659 return d->sceneRect; -
1660 -
1661 if (d->dirtyGrowingItemsBoundingRect) { -
1662 // Lazily update the growing items bounding rect -
1663 QGraphicsScenePrivate *thatd = const_cast<QGraphicsScenePrivate *>(d); -
1664 QRectF oldGrowingBoundingRect = thatd->growingItemsBoundingRect; -
1665 thatd->growingItemsBoundingRect |= itemsBoundingRect(); -
1666 thatd->dirtyGrowingItemsBoundingRect = false; -
1667 if (oldGrowingBoundingRect != thatd->growingItemsBoundingRect) -
1668 emit const_cast<QGraphicsScene *>(this)->sceneRectChanged(thatd->growingItemsBoundingRect); -
1669 } -
1670 return d->growingItemsBoundingRect; -
1671} -
1672void QGraphicsScene::setSceneRect(const QRectF &rect) -
1673{ -
1674 Q_D(QGraphicsScene); -
1675 if (rect != d->sceneRect) { -
1676 d->hasSceneRect = !rect.isNull(); -
1677 d->sceneRect = rect; -
1678 emit sceneRectChanged(d->hasSceneRect ? rect : d->growingItemsBoundingRect); -
1679 } -
1680} -
1681 -
1682/*! -
1683 \fn qreal QGraphicsScene::width() const -
1684 -
1685 This convenience function is equivalent to calling sceneRect().width(). -
1686 -
1687 \sa height() -
1688*/ -
1689 -
1690/*! -
1691 \fn qreal QGraphicsScene::height() const -
1692 -
1693 This convenience function is equivalent to calling \c sceneRect().height(). -
1694 -
1695 \sa width() -
1696*/ -
1697 -
1698/*! -
1699 Renders the \a source rect from scene into \a target, using \a painter. This -
1700 function is useful for capturing the contents of the scene onto a paint -
1701 device, such as a QImage (e.g., to take a screenshot), or for printing -
1702 with QPrinter. For example: -
1703 -
1704 \snippet code/src_gui_graphicsview_qgraphicsscene.cpp 1 -
1705 -
1706 If \a source is a null rect, this function will use sceneRect() to -
1707 determine what to render. If \a target is a null rect, the dimensions of \a -
1708 painter's paint device will be used. -
1709 -
1710 The source rect contents will be transformed according to \a -
1711 aspectRatioMode to fit into the target rect. By default, the aspect ratio -
1712 is kept, and \a source is scaled to fit in \a target. -
1713 -
1714 \sa QGraphicsView::render() -
1715*/ -
1716void QGraphicsScene::render(QPainter *painter, const QRectF &target, const QRectF &source, -
1717 Qt::AspectRatioMode aspectRatioMode) -
1718{ -
1719 // ### Switch to using the recursive rendering algorithm instead. -
1720 -
1721 // Default source rect = scene rect -
1722 QRectF sourceRect = source; -
1723 if (sourceRect.isNull()) -
1724 sourceRect = sceneRect(); -
1725 -
1726 // Default target rect = device rect -
1727 QRectF targetRect = target; -
1728 if (targetRect.isNull()) { -
1729 if (painter->device()->devType() == QInternal::Picture) -
1730 targetRect = sourceRect; -
1731 else -
1732 targetRect.setRect(0, 0, painter->device()->width(), painter->device()->height()); -
1733 } -
1734 -
1735 // Find the ideal x / y scaling ratio to fit \a source into \a target. -
1736 qreal xratio = targetRect.width() / sourceRect.width(); -
1737 qreal yratio = targetRect.height() / sourceRect.height(); -
1738 -
1739 // Scale according to the aspect ratio mode. -
1740 switch (aspectRatioMode) { -
1741 case Qt::KeepAspectRatio: -
1742 xratio = yratio = qMin(xratio, yratio); -
1743 break; -
1744 case Qt::KeepAspectRatioByExpanding: -
1745 xratio = yratio = qMax(xratio, yratio); -
1746 break; -
1747 case Qt::IgnoreAspectRatio: -
1748 break; -
1749 } -
1750 -
1751 // Find all items to draw, and reverse the list (we want to draw -
1752 // in reverse order). -
1753 QList<QGraphicsItem *> itemList = items(sourceRect, Qt::IntersectsItemBoundingRect); -
1754 QGraphicsItem **itemArray = new QGraphicsItem *[itemList.size()]; -
1755 int numItems = itemList.size(); -
1756 for (int i = 0; i < numItems; ++i) -
1757 itemArray[numItems - i - 1] = itemList.at(i); -
1758 itemList.clear(); -
1759 -
1760 painter->save(); -
1761 -
1762 // Transform the painter. -
1763 painter->setClipRect(targetRect, Qt::IntersectClip); -
1764 QTransform painterTransform; -
1765 painterTransform *= QTransform() -
1766 .translate(targetRect.left(), targetRect.top()) -
1767 .scale(xratio, yratio) -
1768 .translate(-sourceRect.left(), -sourceRect.top()); -
1769 painter->setWorldTransform(painterTransform, true); -
1770 -
1771 // Two unit vectors. -
1772 QLineF v1(0, 0, 1, 0); -
1773 QLineF v2(0, 0, 0, 1); -
1774 -
1775 // Generate the style options -
1776 QStyleOptionGraphicsItem *styleOptionArray = new QStyleOptionGraphicsItem[numItems]; -
1777 for (int i = 0; i < numItems; ++i) -
1778 itemArray[i]->d_ptr->initStyleOption(&styleOptionArray[i], painterTransform, targetRect.toRect()); -
1779 -
1780 // Render the scene. -
1781 drawBackground(painter, sourceRect); -
1782 drawItems(painter, numItems, itemArray, styleOptionArray); -
1783 drawForeground(painter, sourceRect); -
1784 -
1785 delete [] itemArray; -
1786 delete [] styleOptionArray; -
1787 -
1788 painter->restore(); -
1789} -
1790 -
1791/*! -
1792 \property QGraphicsScene::itemIndexMethod -
1793 \brief the item indexing method. -
1794 -
1795 QGraphicsScene applies an indexing algorithm to the scene, to speed up -
1796 item discovery functions like items() and itemAt(). Indexing is most -
1797 efficient for static scenes (i.e., where items don't move around). For -
1798 dynamic scenes, or scenes with many animated items, the index bookkeeping -
1799 can outweight the fast lookup speeds. -
1800 -
1801 For the common case, the default index method BspTreeIndex works fine. If -
1802 your scene uses many animations and you are experiencing slowness, you can -
1803 disable indexing by calling \c setItemIndexMethod(NoIndex). -
1804 -
1805 \sa bspTreeDepth -
1806*/ -
1807QGraphicsScene::ItemIndexMethod QGraphicsScene::itemIndexMethod() const -
1808{ -
1809 Q_D(const QGraphicsScene); -
1810 return d->indexMethod; -
1811} -
1812void QGraphicsScene::setItemIndexMethod(ItemIndexMethod method) -
1813{ -
1814 Q_D(QGraphicsScene); -
1815 if (d->indexMethod == method) -
1816 return; -
1817 -
1818 d->indexMethod = method; -
1819 -
1820 QList<QGraphicsItem *> oldItems = d->index->items(Qt::DescendingOrder); -
1821 delete d->index; -
1822 if (method == BspTreeIndex) -
1823 d->index = new QGraphicsSceneBspTreeIndex(this); -
1824 else -
1825 d->index = new QGraphicsSceneLinearIndex(this); -
1826 for (int i = oldItems.size() - 1; i >= 0; --i) -
1827 d->index->addItem(oldItems.at(i)); -
1828} -
1829 -
1830/*! -
1831 \property QGraphicsScene::bspTreeDepth -
1832 \brief the depth of QGraphicsScene's BSP index tree -
1833 \since 4.3 -
1834 -
1835 This property has no effect when NoIndex is used. -
1836 -
1837 This value determines the depth of QGraphicsScene's BSP tree. The depth -
1838 directly affects QGraphicsScene's performance and memory usage; the latter -
1839 growing exponentially with the depth of the tree. With an optimal tree -
1840 depth, QGraphicsScene can instantly determine the locality of items, even -
1841 for scenes with thousands or millions of items. This also greatly improves -
1842 rendering performance. -
1843 -
1844 By default, the value is 0, in which case Qt will guess a reasonable -
1845 default depth based on the size, location and number of items in the -
1846 scene. If these parameters change frequently, however, you may experience -
1847 slowdowns as QGraphicsScene retunes the depth internally. You can avoid -
1848 potential slowdowns by fixating the tree depth through setting this -
1849 property. -
1850 -
1851 The depth of the tree and the size of the scene rectangle decide the -
1852 granularity of the scene's partitioning. The size of each scene segment is -
1853 determined by the following algorithm: -
1854 -
1855 \snippet code/src_gui_graphicsview_qgraphicsscene.cpp 2 -
1856 -
1857 The BSP tree has an optimal size when each segment contains between 0 and -
1858 10 items. -
1859 -
1860 \sa itemIndexMethod -
1861*/ -
1862int QGraphicsScene::bspTreeDepth() const -
1863{ -
1864 Q_D(const QGraphicsScene); -
1865 QGraphicsSceneBspTreeIndex *bspTree = qobject_cast<QGraphicsSceneBspTreeIndex *>(d->index); -
1866 return bspTree ? bspTree->bspTreeDepth() : 0; -
1867} -
1868void QGraphicsScene::setBspTreeDepth(int depth) -
1869{ -
1870 Q_D(QGraphicsScene); -
1871 if (depth < 0) { -
1872 qWarning("QGraphicsScene::setBspTreeDepth: invalid depth %d ignored; must be >= 0", depth); -
1873 return; -
1874 } -
1875 -
1876 QGraphicsSceneBspTreeIndex *bspTree = qobject_cast<QGraphicsSceneBspTreeIndex *>(d->index); -
1877 if (!bspTree) { -
1878 qWarning("QGraphicsScene::setBspTreeDepth: can not apply if indexing method is not BSP"); -
1879 return; -
1880 } -
1881 bspTree->setBspTreeDepth(depth); -
1882} -
1883 -
1884/*! -
1885 \property QGraphicsScene::sortCacheEnabled -
1886 \brief whether sort caching is enabled -
1887 \since 4.5 -
1888 \obsolete -
1889 -
1890 Since Qt 4.6, this property has no effect. -
1891*/ -
1892bool QGraphicsScene::isSortCacheEnabled() const -
1893{ -
1894 Q_D(const QGraphicsScene); -
1895 return d->sortCacheEnabled; -
1896} -
1897void QGraphicsScene::setSortCacheEnabled(bool enabled) -
1898{ -
1899 Q_D(QGraphicsScene); -
1900 if (d->sortCacheEnabled == enabled) -
1901 return; -
1902 d->sortCacheEnabled = enabled; -
1903} -
1904 -
1905/*! -
1906 Calculates and returns the bounding rect of all items on the scene. This -
1907 function works by iterating over all items, and because of this, it can -
1908 be slow for large scenes. -
1909 -
1910 \sa sceneRect() -
1911*/ -
1912QRectF QGraphicsScene::itemsBoundingRect() const -
1913{ -
1914 // Does not take untransformable items into account. -
1915 QRectF boundingRect; -
1916 foreach (QGraphicsItem *item, items()) -
1917 boundingRect |= item->sceneBoundingRect(); -
1918 return boundingRect; -
1919} -
1920 -
1921/*! -
1922 Returns an ordered list of all items on the scene. \a order decides the -
1923 stacking order. -
1924 -
1925 \sa addItem(), removeItem(), {QGraphicsItem#Sorting}{Sorting} -
1926*/ -
1927QList<QGraphicsItem *> QGraphicsScene::items(Qt::SortOrder order) const -
1928{ -
1929 Q_D(const QGraphicsScene); -
1930 return d->index->items(order); -
1931} -
1932 -
1933/*! -
1934 \fn QList<QGraphicsItem *> QGraphicsScene::items(qreal x, qreal y, qreal w, qreal h, Qt::ItemSelectionMode mode) const -
1935 \obsolete -
1936 \since 4.3 -
1937 -
1938 This convenience function is equivalent to calling items(QRectF(\a x, \a y, \a w, \a h), \a mode). -
1939 -
1940 This function is deprecated and returns incorrect results if the scene -
1941 contains items that ignore transformations. Use the overload that takes -
1942 a QTransform instead. -
1943*/ -
1944 -
1945/*! -
1946 \fn QList<QGraphicsItem *> QGraphicsScene::items(qreal x, qreal y, qreal w, qreal h, Qt::ItemSelectionMode mode, Qt::SortOrder order, const QTransform &deviceTransform) const -
1947 \overload -
1948 \since 4.6 -
1949 -
1950 \brief Returns all visible items that, depending on \a mode, are -
1951 either inside or intersect with the rectangle defined by \a x, \a y, -
1952 \a w and \a h, in a list sorted using \a order. In this case, "visible" defines items for which: -
1953 isVisible() returns true, effectiveOpacity() returns a value greater than 0.0 -
1954 (which is fully transparent) and the parent item does not clip it. -
1955 -
1956 \a deviceTransform is the transformation that applies to the view, and needs to -
1957 be provided if the scene contains items that ignore transformations. -
1958*/ -
1959 -
1960/*! -
1961 \fn QList<QGraphicsItem *> QGraphicsScene::items(const QPointF &pos, Qt::ItemSelectionMode mode, Qt::SortOrder order, const QTransform &deviceTransform) const -
1962 \since 4.6 -
1963 -
1964 \brief Returns all visible items that, depending on \a mode, are at -
1965 the specified \a pos in a list sorted using \a order. In this case, "visible" defines items for which: -
1966 isVisible() returns true, effectiveOpacity() returns a value greater than 0.0 -
1967 (which is fully transparent) and the parent item does not clip it. -
1968 -
1969 The default value for \a mode is Qt::IntersectsItemShape; all items whose -
1970 exact shape intersects with \a pos are returned. -
1971 -
1972 \a deviceTransform is the transformation that applies to the view, and needs to -
1973 be provided if the scene contains items that ignore transformations. -
1974 -
1975 \sa itemAt(), {QGraphicsItem#Sorting}{Sorting} -
1976*/ -
1977QList<QGraphicsItem *> QGraphicsScene::items(const QPointF &pos, Qt::ItemSelectionMode mode, -
1978 Qt::SortOrder order, const QTransform &deviceTransform) const -
1979{ -
1980 Q_D(const QGraphicsScene); -
1981 return d->index->items(pos, mode, order, deviceTransform); -
1982} -
1983 -
1984/*! -
1985 \fn QList<QGraphicsItem *> QGraphicsScene::items(const QRectF &rect, Qt::ItemSelectionMode mode, Qt::SortOrder order, const QTransform &deviceTransform) const -
1986 \overload -
1987 \since 4.6 -
1988 -
1989 \brief Returns all visible items that, depending on \a mode, are -
1990 either inside or intersect with the specified \a rect, in a -
1991 list sorted using \a order. In this case, "visible" defines items for which: -
1992 isVisible() returns true, effectiveOpacity() returns a value greater than 0.0 -
1993 (which is fully transparent) and the parent item does not clip it. -
1994 -
1995 The default value for \a mode is Qt::IntersectsItemShape; all items whose -
1996 exact shape intersects with or is contained by \a rect are returned. -
1997 -
1998 \a deviceTransform is the transformation that applies to the view, and needs to -
1999 be provided if the scene contains items that ignore transformations. -
2000 -
2001 \sa itemAt(), {QGraphicsItem#Sorting}{Sorting} -
2002*/ -
2003QList<QGraphicsItem *> QGraphicsScene::items(const QRectF &rect, Qt::ItemSelectionMode mode, -
2004 Qt::SortOrder order, const QTransform &deviceTransform) const -
2005{ -
2006 Q_D(const QGraphicsScene); -
2007 return d->index->items(rect, mode, order, deviceTransform); -
2008} -
2009 -
2010/*! -
2011 \fn QList<QGraphicsItem *> QGraphicsScene::items(const QPolygonF &polygon, Qt::ItemSelectionMode mode, Qt::SortOrder order, const QTransform &deviceTransform) const -
2012 \overload -
2013 \since 4.6 -
2014 -
2015 \brief Returns all visible items that, depending on \a mode, are -
2016 either inside or intersect with the specified \a polygon, in -
2017 a list sorted using \a order. In this case, "visible" defines items for which: -
2018 isVisible() returns true, effectiveOpacity() returns a value greater than 0.0 -
2019 (which is fully transparent) and the parent item does not clip it. -
2020 -
2021 The default value for \a mode is Qt::IntersectsItemShape; all items whose -
2022 exact shape intersects with or is contained by \a polygon are returned. -
2023 -
2024 \a deviceTransform is the transformation that applies to the view, and needs to -
2025 be provided if the scene contains items that ignore transformations. -
2026 -
2027 \sa itemAt(), {QGraphicsItem#Sorting}{Sorting} -
2028*/ -
2029QList<QGraphicsItem *> QGraphicsScene::items(const QPolygonF &polygon, Qt::ItemSelectionMode mode, -
2030 Qt::SortOrder order, const QTransform &deviceTransform) const -
2031{ -
2032 Q_D(const QGraphicsScene); -
2033 return d->index->items(polygon, mode, order, deviceTransform); -
2034} -
2035 -
2036/*! -
2037 \fn QList<QGraphicsItem *> QGraphicsScene::items(const QPainterPath &path, Qt::ItemSelectionMode mode, Qt::SortOrder order, const QTransform &deviceTransform) const -
2038 \overload -
2039 \since 4.6 -
2040 -
2041 \brief Returns all visible items that, depending on \a mode, are -
2042 either inside or intersect with the specified \a path, in a -
2043 list sorted using \a order. In this case, "visible" defines items for which: -
2044 isVisible() returns true, effectiveOpacity() returns a value greater than 0.0 -
2045 (which is fully transparent) and the parent item does not clip it. -
2046 -
2047 The default value for \a mode is Qt::IntersectsItemShape; all items whose -
2048 exact shape intersects with or is contained by \a path are returned. -
2049 -
2050 \a deviceTransform is the transformation that applies to the view, and needs to -
2051 be provided if the scene contains items that ignore transformations. -
2052 -
2053 \sa itemAt(), {QGraphicsItem#Sorting}{Sorting} -
2054*/ -
2055QList<QGraphicsItem *> QGraphicsScene::items(const QPainterPath &path, Qt::ItemSelectionMode mode, -
2056 Qt::SortOrder order, const QTransform &deviceTransform) const -
2057{ -
2058 Q_D(const QGraphicsScene); -
2059 return d->index->items(path, mode, order, deviceTransform); -
2060} -
2061 -
2062/*! -
2063 Returns a list of all items that collide with \a item. Collisions are -
2064 determined by calling QGraphicsItem::collidesWithItem(); the collision -
2065 detection is determined by \a mode. By default, all items whose shape -
2066 intersects \a item or is contained inside \a item's shape are returned. -
2067 -
2068 The items are returned in descending stacking order (i.e., the first item -
2069 in the list is the uppermost item, and the last item is the lowermost -
2070 item). -
2071 -
2072 \sa items(), itemAt(), QGraphicsItem::collidesWithItem(), {QGraphicsItem#Sorting}{Sorting} -
2073*/ -
2074QList<QGraphicsItem *> QGraphicsScene::collidingItems(const QGraphicsItem *item, -
2075 Qt::ItemSelectionMode mode) const -
2076{ -
2077 Q_D(const QGraphicsScene); -
2078 if (!item) { -
2079 qWarning("QGraphicsScene::collidingItems: cannot find collisions for null item"); -
2080 return QList<QGraphicsItem *>(); -
2081 } -
2082 -
2083 // Does not support ItemIgnoresTransformations. -
2084 QList<QGraphicsItem *> tmp; -
2085 foreach (QGraphicsItem *itemInVicinity, d->index->estimateItems(item->sceneBoundingRect(), Qt::DescendingOrder)) { -
2086 if (item != itemInVicinity && item->collidesWithItem(itemInVicinity, mode)) -
2087 tmp << itemInVicinity; -
2088 } -
2089 return tmp; -
2090} -
2091 -
2092/*! -
2093 \fn QGraphicsItem *QGraphicsScene::itemAt(const QPointF &position) const -
2094 \overload -
2095 \obsolete -
2096 -
2097 Returns the topmost visible item at the specified \a position, or 0 if -
2098 there are no items at this position. -
2099 -
2100 This function is deprecated and returns incorrect results if the scene -
2101 contains items that ignore transformations. Use the overload that takes -
2102 a QTransform instead. -
2103 -
2104 Note: See items() for a definition of which items are considered visible by this function. -
2105 -
2106 \sa items(), collidingItems(), {QGraphicsItem#Sorting}{Sorting} -
2107*/ -
2108 -
2109/*! -
2110 \since 4.6 -
2111 -
2112 Returns the topmost visible item at the specified \a position, or 0 -
2113 if there are no items at this position. -
2114 -
2115 \a deviceTransform is the transformation that applies to the view, and needs to -
2116 be provided if the scene contains items that ignore transformations. -
2117 -
2118 Note: See items() for a definition of which items are considered visible by this function. -
2119 -
2120 \sa items(), collidingItems(), {QGraphicsItem#Sorting}{Sorting} -
2121*/ -
2122QGraphicsItem *QGraphicsScene::itemAt(const QPointF &position, const QTransform &deviceTransform) const -
2123{ -
2124 QList<QGraphicsItem *> itemsAtPoint = items(position, Qt::IntersectsItemShape, -
2125 Qt::DescendingOrder, deviceTransform); -
2126 return itemsAtPoint.isEmpty() ? 0 : itemsAtPoint.first(); -
2127} -
2128 -
2129/*! -
2130 \fn QGraphicsScene::itemAt(qreal x, qreal y, const QTransform &deviceTransform) const -
2131 \overload -
2132 \since 4.6 -
2133 -
2134 Returns the topmost visible item at the position specified by (\a x, \a -
2135 y), or 0 if there are no items at this position. -
2136 -
2137 \a deviceTransform is the transformation that applies to the view, and needs to -
2138 be provided if the scene contains items that ignore transformations. -
2139 -
2140 This convenience function is equivalent to calling \c -
2141 {itemAt(QPointF(x, y), deviceTransform)}. -
2142 -
2143 Note: See items() for a definition of which items are considered visible by this function. -
2144*/ -
2145 -
2146/*! -
2147 \fn QGraphicsScene::itemAt(qreal x, qreal y) const -
2148 \overload -
2149 \obsolete -
2150 -
2151 Returns the topmost visible item at the position specified by (\a x, \a -
2152 y), or 0 if there are no items at this position. -
2153 -
2154 This convenience function is equivalent to calling \c -
2155 {itemAt(QPointF(x, y))}. -
2156 -
2157 This function is deprecated and returns incorrect results if the scene -
2158 contains items that ignore transformations. Use the overload that takes -
2159 a QTransform instead. -
2160 -
2161 Note: See items() for a definition of which items are considered visible by this function. -
2162*/ -
2163 -
2164/*! -
2165 Returns a list of all currently selected items. The items are -
2166 returned in no particular order. -
2167 -
2168 \sa setSelectionArea() -
2169*/ -
2170QList<QGraphicsItem *> QGraphicsScene::selectedItems() const -
2171{ -
2172 Q_D(const QGraphicsScene); -
2173 -
2174 // Optimization: Lazily removes items that are not selected. -
2175 QGraphicsScene *that = const_cast<QGraphicsScene *>(this); -
2176 QSet<QGraphicsItem *> actuallySelectedSet; -
2177 foreach (QGraphicsItem *item, that->d_func()->selectedItems) { -
2178 if (item->isSelected()) -
2179 actuallySelectedSet << item; -
2180 } -
2181 -
2182 that->d_func()->selectedItems = actuallySelectedSet; -
2183 -
2184 return d->selectedItems.values(); -
2185} -
2186 -
2187/*! -
2188 Returns the selection area that was previously set with -
2189 setSelectionArea(), or an empty QPainterPath if no selection area has been -
2190 set. -
2191 -
2192 \sa setSelectionArea() -
2193*/ -
2194QPainterPath QGraphicsScene::selectionArea() const -
2195{ -
2196 Q_D(const QGraphicsScene); -
2197 return d->selectionArea; -
2198} -
2199 -
2200/*! -
2201 \since 4.6 -
2202 -
2203 Sets the selection area to \a path. All items within this area are -
2204 immediately selected, and all items outside are unselected. You can get -
2205 the list of all selected items by calling selectedItems(). -
2206 -
2207 \a deviceTransform is the transformation that applies to the view, and needs to -
2208 be provided if the scene contains items that ignore transformations. -
2209 -
2210 For an item to be selected, it must be marked as \e selectable -
2211 (QGraphicsItem::ItemIsSelectable). -
2212 -
2213 \sa clearSelection(), selectionArea() -
2214*/ -
2215void QGraphicsScene::setSelectionArea(const QPainterPath &path, const QTransform &deviceTransform) -
2216{ -
2217 setSelectionArea(path, Qt::IntersectsItemShape, deviceTransform); -
2218} -
2219 -
2220/*! -
2221 \overload -
2222 \since 4.6 -
2223 -
2224 Sets the selection area to \a path using \a mode to determine if items are -
2225 included in the selection area. -
2226 -
2227 \a deviceTransform is the transformation that applies to the view, and needs to -
2228 be provided if the scene contains items that ignore transformations. -
2229 -
2230 \sa clearSelection(), selectionArea() -
2231*/ -
2232void QGraphicsScene::setSelectionArea(const QPainterPath &path, Qt::ItemSelectionMode mode, -
2233 const QTransform &deviceTransform) -
2234{ -
2235 Q_D(QGraphicsScene); -
2236 -
2237 // Note: with boolean path operations, we can improve performance here -
2238 // quite a lot by "growing" the old path instead of replacing it. That -
2239 // allows us to only check the intersect area for changes, instead of -
2240 // reevaluating the whole path over again. -
2241 d->selectionArea = path; -
2242 -
2243 QSet<QGraphicsItem *> unselectItems = d->selectedItems; -
2244 -
2245 // Disable emitting selectionChanged() for individual items. -
2246 ++d->selectionChanging; -
2247 bool changed = false; -
2248 -
2249 // Set all items in path to selected. -
2250 foreach (QGraphicsItem *item, items(path, mode, Qt::DescendingOrder, deviceTransform)) { -
2251 if (item->flags() & QGraphicsItem::ItemIsSelectable) { -
2252 if (!item->isSelected()) -
2253 changed = true; -
2254 unselectItems.remove(item); -
2255 item->setSelected(true); -
2256 } -
2257 } -
2258 -
2259 // Unselect all items outside path. -
2260 foreach (QGraphicsItem *item, unselectItems) { -
2261 item->setSelected(false); -
2262 changed = true; -
2263 } -
2264 -
2265 // Reenable emitting selectionChanged() for individual items. -
2266 --d->selectionChanging; -
2267 -
2268 if (!d->selectionChanging && changed) -
2269 emit selectionChanged(); -
2270} -
2271 -
2272/*! -
2273 Clears the current selection. -
2274 -
2275 \sa setSelectionArea(), selectedItems() -
2276*/ -
2277void QGraphicsScene::clearSelection() -
2278{ -
2279 Q_D(QGraphicsScene); -
2280 -
2281 // Disable emitting selectionChanged -
2282 ++d->selectionChanging; -
2283 bool changed = !d->selectedItems.isEmpty(); -
2284 -
2285 foreach (QGraphicsItem *item, d->selectedItems) -
2286 item->setSelected(false); -
2287 d->selectedItems.clear(); -
2288 -
2289 // Reenable emitting selectionChanged() for individual items. -
2290 --d->selectionChanging; -
2291 -
2292 if (!d->selectionChanging && changed) -
2293 emit selectionChanged(); -
2294} -
2295 -
2296/*! -
2297 \since 4.4 -
2298 -
2299 Removes and deletes all items from the scene, but otherwise leaves the -
2300 state of the scene unchanged. -
2301 -
2302 \sa addItem() -
2303*/ -
2304void QGraphicsScene::clear() -
2305{ -
2306 Q_D(QGraphicsScene); -
2307 // NB! We have to clear the index before deleting items; otherwise the -
2308 // index might try to access dangling item pointers. -
2309 d->index->clear(); -
2310 // NB! QGraphicsScenePrivate::unregisterTopLevelItem() removes items -
2311 while (!d->topLevelItems.isEmpty()) -
2312 delete d->topLevelItems.first(); -
2313 Q_ASSERT(d->topLevelItems.isEmpty()); -
2314 d->lastItemCount = 0; -
2315 d->allItemsIgnoreHoverEvents = true; -
2316 d->allItemsUseDefaultCursor = true; -
2317 d->allItemsIgnoreTouchEvents = true; -
2318} -
2319 -
2320/*! -
2321 Groups all items in \a items into a new QGraphicsItemGroup, and returns a -
2322 pointer to the group. The group is created with the common ancestor of \a -
2323 items as its parent, and with position (0, 0). The items are all -
2324 reparented to the group, and their positions and transformations are -
2325 mapped to the group. If \a items is empty, this function will return an -
2326 empty top-level QGraphicsItemGroup. -
2327 -
2328 QGraphicsScene has ownership of the group item; you do not need to delete -
2329 it. To dismantle (ungroup) a group, call destroyItemGroup(). -
2330 -
2331 \sa destroyItemGroup(), QGraphicsItemGroup::addToGroup() -
2332*/ -
2333QGraphicsItemGroup *QGraphicsScene::createItemGroup(const QList<QGraphicsItem *> &items) -
2334{ -
2335 // Build a list of the first item's ancestors -
2336 QList<QGraphicsItem *> ancestors; -
2337 int n = 0; -
2338 if (!items.isEmpty()) { -
2339 QGraphicsItem *parent = items.at(n++); -
2340 while ((parent = parent->parentItem())) -
2341 ancestors.append(parent); -
2342 } -
2343 -
2344 // Find the common ancestor for all items -
2345 QGraphicsItem *commonAncestor = 0; -
2346 if (!ancestors.isEmpty()) { -
2347 while (n < items.size()) { -
2348 int commonIndex = -1; -
2349 QGraphicsItem *parent = items.at(n++); -
2350 do { -
2351 int index = ancestors.indexOf(parent, qMax(0, commonIndex)); -
2352 if (index != -1) { -
2353 commonIndex = index; -
2354 break; -
2355 } -
2356 } while ((parent = parent->parentItem())); -
2357 -
2358 if (commonIndex == -1) { -
2359 commonAncestor = 0; -
2360 break; -
2361 } -
2362 -
2363 commonAncestor = ancestors.at(commonIndex); -
2364 } -
2365 } -
2366 -
2367 // Create a new group at that level -
2368 QGraphicsItemGroup *group = new QGraphicsItemGroup(commonAncestor); -
2369 if (!commonAncestor) -
2370 addItem(group); -
2371 foreach (QGraphicsItem *item, items) -
2372 group->addToGroup(item); -
2373 return group; -
2374} -
2375 -
2376/*! -
2377 Reparents all items in \a group to \a group's parent item, then removes \a -
2378 group from the scene, and finally deletes it. The items' positions and -
2379 transformations are mapped from the group to the group's parent. -
2380 -
2381 \sa createItemGroup(), QGraphicsItemGroup::removeFromGroup() -
2382*/ -
2383void QGraphicsScene::destroyItemGroup(QGraphicsItemGroup *group) -
2384{ -
2385 foreach (QGraphicsItem *item, group->childItems()) -
2386 group->removeFromGroup(item); -
2387 removeItem(group); -
2388 delete group; -
2389} -
2390 -
2391/*! -
2392 Adds or moves the \a item and all its childen to this scene. -
2393 This scene takes ownership of the \a item. -
2394 -
2395 If the item is visible (i.e., QGraphicsItem::isVisible() returns -
2396 true), QGraphicsScene will emit changed() once control goes back -
2397 to the event loop. -
2398 -
2399 If the item is already in a different scene, it will first be -
2400 removed from its old scene, and then added to this scene as a -
2401 top-level. -
2402 -
2403 QGraphicsScene will send ItemSceneChange notifications to \a item -
2404 while it is added to the scene. If item does not currently belong -
2405 to a scene, only one notification is sent. If it does belong to -
2406 scene already (i.e., it is moved to this scene), QGraphicsScene -
2407 will send an addition notification as the item is removed from its -
2408 previous scene. -
2409 -
2410 If the item is a panel, the scene is active, and there is no -
2411 active panel in the scene, then the item will be activated. -
2412 -
2413 \sa removeItem(), addEllipse(), addLine(), addPath(), addPixmap(), -
2414 addRect(), addText(), addWidget(), {QGraphicsItem#Sorting}{Sorting} -
2415*/ -
2416void QGraphicsScene::addItem(QGraphicsItem *item) -
2417{ -
2418 Q_D(QGraphicsScene); -
2419 if (!item) { -
2420 qWarning("QGraphicsScene::addItem: cannot add null item"); -
2421 return; -
2422 } -
2423 if (item->d_ptr->scene == this) { -
2424 qWarning("QGraphicsScene::addItem: item has already been added to this scene"); -
2425 return; -
2426 } -
2427 // Remove this item from its existing scene -
2428 if (QGraphicsScene *oldScene = item->d_ptr->scene) -
2429 oldScene->removeItem(item); -
2430 -
2431 // Notify the item that its scene is changing, and allow the item to -
2432 // react. -
2433 const QVariant newSceneVariant(item->itemChange(QGraphicsItem::ItemSceneChange, -
2434 QVariant::fromValue<QGraphicsScene *>(this))); -
2435 QGraphicsScene *targetScene = qvariant_cast<QGraphicsScene *>(newSceneVariant); -
2436 if (targetScene != this) { -
2437 if (targetScene && item->d_ptr->scene != targetScene) -
2438 targetScene->addItem(item); -
2439 return; -
2440 } -
2441 -
2442 // QDeclarativeItems do not rely on initial itemChanged message, as the componentComplete -
2443 // function allows far more opportunity for delayed-construction optimization. -
2444 if (!item->d_ptr->isDeclarativeItem) { -
2445 if (d->unpolishedItems.isEmpty()) { -
2446 QMetaMethod method = metaObject()->method(d->polishItemsIndex); -
2447 method.invoke(this, Qt::QueuedConnection); -
2448 } -
2449 d->unpolishedItems.append(item); -
2450 item->d_ptr->pendingPolish = true; -
2451 } -
2452 -
2453 // Detach this item from its parent if the parent's scene is different -
2454 // from this scene. -
2455 if (QGraphicsItem *itemParent = item->d_ptr->parent) { -
2456 if (itemParent->d_ptr->scene != this) -
2457 item->setParentItem(0); -
2458 } -
2459 -
2460 // Add the item to this scene -
2461 item->d_func()->scene = targetScene; -
2462 -
2463 // Add the item in the index -
2464 d->index->addItem(item); -
2465 -
2466 // Add to list of toplevels if this item is a toplevel. -
2467 if (!item->d_ptr->parent) -
2468 d->registerTopLevelItem(item); -
2469 -
2470 // Add to list of items that require an update. We cannot assume that the -
2471 // item is fully constructed, so calling item->update() can lead to a pure -
2472 // virtual function call to boundingRect(). -
2473 d->markDirty(item); -
2474 d->dirtyGrowingItemsBoundingRect = true; -
2475 -
2476 // Disable selectionChanged() for individual items -
2477 ++d->selectionChanging; -
2478 int oldSelectedItemSize = d->selectedItems.size(); -
2479 -
2480 // Enable mouse tracking if the item accepts hover events or has a cursor set. -
2481 if (d->allItemsIgnoreHoverEvents && d->itemAcceptsHoverEvents_helper(item)) { -
2482 d->allItemsIgnoreHoverEvents = false; -
2483 d->enableMouseTrackingOnViews(); -
2484 } -
2485#ifndef QT_NO_CURSOR -
2486 if (d->allItemsUseDefaultCursor && item->d_ptr->hasCursor) { -
2487 d->allItemsUseDefaultCursor = false; -
2488 if (d->allItemsIgnoreHoverEvents) // already enabled otherwise -
2489 d->enableMouseTrackingOnViews(); -
2490 } -
2491#endif //QT_NO_CURSOR -
2492 -
2493 // Enable touch events if the item accepts touch events. -
2494 if (d->allItemsIgnoreTouchEvents && item->d_ptr->acceptTouchEvents) { -
2495 d->allItemsIgnoreTouchEvents = false; -
2496 d->enableTouchEventsOnViews(); -
2497 } -
2498 -
2499#ifndef QT_NO_GESTURES -
2500 foreach (Qt::GestureType gesture, item->d_ptr->gestureContext.keys()) -
2501 d->grabGesture(item, gesture); -
2502#endif -
2503 -
2504 // Update selection lists -
2505 if (item->isSelected()) -
2506 d->selectedItems << item; -
2507 if (item->isWidget() && item->isVisible() && static_cast<QGraphicsWidget *>(item)->windowType() == Qt::Popup) -
2508 d->addPopup(static_cast<QGraphicsWidget *>(item)); -
2509 if (item->isPanel() && item->isVisible() && item->panelModality() != QGraphicsItem::NonModal) -
2510 d->enterModal(item); -
2511 -
2512 // Update creation order focus chain. Make sure to leave the widget's -
2513 // internal tab order intact. -
2514 if (item->isWidget()) { -
2515 QGraphicsWidget *widget = static_cast<QGraphicsWidget *>(item); -
2516 if (!d->tabFocusFirst) { -
2517 // No first tab focus widget - make this the first tab focus -
2518 // widget. -
2519 d->tabFocusFirst = widget; -
2520 } else if (!widget->parentWidget()) { -
2521 // Adding a widget that is not part of a tab focus chain. -
2522 QGraphicsWidget *last = d->tabFocusFirst->d_func()->focusPrev; -
2523 QGraphicsWidget *lastNew = widget->d_func()->focusPrev; -
2524 last->d_func()->focusNext = widget; -
2525 widget->d_func()->focusPrev = last; -
2526 d->tabFocusFirst->d_func()->focusPrev = lastNew; -
2527 lastNew->d_func()->focusNext = d->tabFocusFirst; -
2528 } -
2529 } -
2530 -
2531 // Add all children recursively -
2532 item->d_ptr->ensureSortedChildren(); -
2533 for (int i = 0; i < item->d_ptr->children.size(); ++i) -
2534 addItem(item->d_ptr->children.at(i)); -
2535 -
2536 // Resolve font and palette. -
2537 item->d_ptr->resolveFont(d->font.resolve()); -
2538 item->d_ptr->resolvePalette(d->palette.resolve()); -
2539 -
2540 -
2541 // Reenable selectionChanged() for individual items -
2542 --d->selectionChanging; -
2543 if (!d->selectionChanging && d->selectedItems.size() != oldSelectedItemSize) -
2544 emit selectionChanged(); -
2545 -
2546 // Deliver post-change notification -
2547 item->itemChange(QGraphicsItem::ItemSceneHasChanged, newSceneVariant); -
2548 -
2549 // Update explicit activation -
2550 bool autoActivate = true; -
2551 if (!d->childExplicitActivation && item->d_ptr->explicitActivate) -
2552 d->childExplicitActivation = item->d_ptr->wantsActive ? 1 : 2; -
2553 if (d->childExplicitActivation && item->isPanel()) { -
2554 if (d->childExplicitActivation == 1) -
2555 setActivePanel(item); -
2556 else -
2557 autoActivate = false; -
2558 d->childExplicitActivation = 0; -
2559 } else if (!item->d_ptr->parent) { -
2560 d->childExplicitActivation = 0; -
2561 } -
2562 -
2563 // Auto-activate this item's panel if nothing else has been activated -
2564 if (autoActivate) { -
2565 if (!d->lastActivePanel && !d->activePanel && item->isPanel()) { -
2566 if (isActive()) -
2567 setActivePanel(item); -
2568 else -
2569 d->lastActivePanel = item; -
2570 } -
2571 } -
2572 -
2573 if (item->d_ptr->flags & QGraphicsItem::ItemSendsScenePositionChanges) -
2574 d->registerScenePosItem(item); -
2575 -
2576 // Ensure that newly added items that have subfocus set, gain -
2577 // focus automatically if there isn't a focus item already. -
2578 if (!d->focusItem && item != d->lastFocusItem && item->focusItem() == item) -
2579 item->focusItem()->setFocus(); -
2580 -
2581 d->updateInputMethodSensitivityInViews(); -
2582} -
2583 -
2584/*! -
2585 Creates and adds an ellipse item to the scene, and returns the item -
2586 pointer. The geometry of the ellipse is defined by \a rect, and its pen -
2587 and brush are initialized to \a pen and \a brush. -
2588 -
2589 Note that the item's geometry is provided in item coordinates, and its -
2590 position is initialized to (0, 0). -
2591 -
2592 If the item is visible (i.e., QGraphicsItem::isVisible() returns true), -
2593 QGraphicsScene will emit changed() once control goes back to the event -
2594 loop. -
2595 -
2596 \sa addLine(), addPath(), addPixmap(), addRect(), addText(), addItem(), -
2597 addWidget() -
2598*/ -
2599QGraphicsEllipseItem *QGraphicsScene::addEllipse(const QRectF &rect, const QPen &pen, const QBrush &brush) -
2600{ -
2601 QGraphicsEllipseItem *item = new QGraphicsEllipseItem(rect); -
2602 item->setPen(pen); -
2603 item->setBrush(brush); -
2604 addItem(item); -
2605 return item; -
2606} -
2607 -
2608/*! -
2609 \fn QGraphicsEllipseItem *QGraphicsScene::addEllipse(qreal x, qreal y, qreal w, qreal h, const QPen &pen, const QBrush &brush) -
2610 \since 4.3 -
2611 -
2612 This convenience function is equivalent to calling addEllipse(QRectF(\a x, -
2613 \a y, \a w, \a h), \a pen, \a brush). -
2614*/ -
2615 -
2616/*! -
2617 Creates and adds a line item to the scene, and returns the item -
2618 pointer. The geometry of the line is defined by \a line, and its pen -
2619 is initialized to \a pen. -
2620 -
2621 Note that the item's geometry is provided in item coordinates, and its -
2622 position is initialized to (0, 0). -
2623 -
2624 If the item is visible (i.e., QGraphicsItem::isVisible() returns true), -
2625 QGraphicsScene will emit changed() once control goes back to the event -
2626 loop. -
2627 -
2628 \sa addEllipse(), addPath(), addPixmap(), addRect(), addText(), addItem(), -
2629 addWidget() -
2630*/ -
2631QGraphicsLineItem *QGraphicsScene::addLine(const QLineF &line, const QPen &pen) -
2632{ -
2633 QGraphicsLineItem *item = new QGraphicsLineItem(line); -
2634 item->setPen(pen); -
2635 addItem(item); -
2636 return item; -
2637} -
2638 -
2639/*! -
2640 \fn QGraphicsLineItem *QGraphicsScene::addLine(qreal x1, qreal y1, qreal x2, qreal y2, const QPen &pen) -
2641 \since 4.3 -
2642 -
2643 This convenience function is equivalent to calling addLine(QLineF(\a x1, -
2644 \a y1, \a x2, \a y2), \a pen). -
2645*/ -
2646 -
2647/*! -
2648 Creates and adds a path item to the scene, and returns the item -
2649 pointer. The geometry of the path is defined by \a path, and its pen and -
2650 brush are initialized to \a pen and \a brush. -
2651 -
2652 Note that the item's geometry is provided in item coordinates, and its -
2653 position is initialized to (0, 0). -
2654 -
2655 If the item is visible (i.e., QGraphicsItem::isVisible() returns true), -
2656 QGraphicsScene will emit changed() once control goes back to the event -
2657 loop. -
2658 -
2659 \sa addEllipse(), addLine(), addPixmap(), addRect(), addText(), addItem(), -
2660 addWidget() -
2661*/ -
2662QGraphicsPathItem *QGraphicsScene::addPath(const QPainterPath &path, const QPen &pen, const QBrush &brush) -
2663{ -
2664 QGraphicsPathItem *item = new QGraphicsPathItem(path); -
2665 item->setPen(pen); -
2666 item->setBrush(brush); -
2667 addItem(item); -
2668 return item; -
2669} -
2670 -
2671/*! -
2672 Creates and adds a pixmap item to the scene, and returns the item -
2673 pointer. The pixmap is defined by \a pixmap. -
2674 -
2675 Note that the item's geometry is provided in item coordinates, and its -
2676 position is initialized to (0, 0). -
2677 -
2678 If the item is visible (i.e., QGraphicsItem::isVisible() returns true), -
2679 QGraphicsScene will emit changed() once control goes back to the event -
2680 loop. -
2681 -
2682 \sa addEllipse(), addLine(), addPath(), addRect(), addText(), addItem(), -
2683 addWidget() -
2684*/ -
2685QGraphicsPixmapItem *QGraphicsScene::addPixmap(const QPixmap &pixmap) -
2686{ -
2687 QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap); -
2688 addItem(item); -
2689 return item; -
2690} -
2691 -
2692/*! -
2693 Creates and adds a polygon item to the scene, and returns the item -
2694 pointer. The polygon is defined by \a polygon, and its pen and -
2695 brush are initialized to \a pen and \a brush. -
2696 -
2697 Note that the item's geometry is provided in item coordinates, and its -
2698 position is initialized to (0, 0). -
2699 -
2700 If the item is visible (i.e., QGraphicsItem::isVisible() returns true), -
2701 QGraphicsScene will emit changed() once control goes back to the event -
2702 loop. -
2703 -
2704 \sa addEllipse(), addLine(), addPath(), addRect(), addText(), addItem(), -
2705 addWidget() -
2706*/ -
2707QGraphicsPolygonItem *QGraphicsScene::addPolygon(const QPolygonF &polygon, -
2708 const QPen &pen, const QBrush &brush) -
2709{ -
2710 QGraphicsPolygonItem *item = new QGraphicsPolygonItem(polygon); -
2711 item->setPen(pen); -
2712 item->setBrush(brush); -
2713 addItem(item); -
2714 return item; -
2715} -
2716 -
2717/*! -
2718 Creates and adds a rectangle item to the scene, and returns the item -
2719 pointer. The geometry of the rectangle is defined by \a rect, and its pen -
2720 and brush are initialized to \a pen and \a brush. -
2721 -
2722 Note that the item's geometry is provided in item coordinates, and its -
2723 position is initialized to (0, 0). For example, if a QRect(50, 50, 100, -
2724 100) is added, its top-left corner will be at (50, 50) relative to the -
2725 origin in the items coordinate system. -
2726 -
2727 If the item is visible (i.e., QGraphicsItem::isVisible() returns true), -
2728 QGraphicsScene will emit changed() once control goes back to the event -
2729 loop. -
2730 -
2731 \sa addEllipse(), addLine(), addPixmap(), addPixmap(), addText(), -
2732 addItem(), addWidget() -
2733*/ -
2734QGraphicsRectItem *QGraphicsScene::addRect(const QRectF &rect, const QPen &pen, const QBrush &brush) -
2735{ -
2736 QGraphicsRectItem *item = new QGraphicsRectItem(rect); -
2737 item->setPen(pen); -
2738 item->setBrush(brush); -
2739 addItem(item); -
2740 return item; -
2741} -
2742 -
2743/*! -
2744 \fn QGraphicsRectItem *QGraphicsScene::addRect(qreal x, qreal y, qreal w, qreal h, const QPen &pen, const QBrush &brush) -
2745 \since 4.3 -
2746 -
2747 This convenience function is equivalent to calling addRect(QRectF(\a x, -
2748 \a y, \a w, \a h), \a pen, \a brush). -
2749*/ -
2750 -
2751/*! -
2752 Creates and adds a text item to the scene, and returns the item -
2753 pointer. The text string is initialized to \a text, and its font -
2754 is initialized to \a font. -
2755 -
2756 The item's position is initialized to (0, 0). -
2757 -
2758 If the item is visible (i.e., QGraphicsItem::isVisible() returns true), -
2759 QGraphicsScene will emit changed() once control goes back to the event -
2760 loop. -
2761 -
2762 \sa addEllipse(), addLine(), addPixmap(), addPixmap(), addRect(), -
2763 addItem(), addWidget() -
2764*/ -
2765QGraphicsTextItem *QGraphicsScene::addText(const QString &text, const QFont &font) -
2766{ -
2767 QGraphicsTextItem *item = new QGraphicsTextItem(text); -
2768 item->setFont(font); -
2769 addItem(item); -
2770 return item; -
2771} -
2772 -
2773/*! -
2774 Creates and adds a QGraphicsSimpleTextItem to the scene, and returns the -
2775 item pointer. The text string is initialized to \a text, and its font is -
2776 initialized to \a font. -
2777 -
2778 The item's position is initialized to (0, 0). -
2779 -
2780 If the item is visible (i.e., QGraphicsItem::isVisible() returns true), -
2781 QGraphicsScene will emit changed() once control goes back to the event -
2782 loop. -
2783 -
2784 \sa addEllipse(), addLine(), addPixmap(), addPixmap(), addRect(), -
2785 addItem(), addWidget() -
2786*/ -
2787QGraphicsSimpleTextItem *QGraphicsScene::addSimpleText(const QString &text, const QFont &font) -
2788{ -
2789 QGraphicsSimpleTextItem *item = new QGraphicsSimpleTextItem(text); -
2790 item->setFont(font); -
2791 addItem(item); -
2792 return item; -
2793} -
2794 -
2795/*! -
2796 Creates a new QGraphicsProxyWidget for \a widget, adds it to the scene, -
2797 and returns a pointer to the proxy. \a wFlags set the default window flags -
2798 for the embedding proxy widget. -
2799 -
2800 The item's position is initialized to (0, 0). -
2801 -
2802 If the item is visible (i.e., QGraphicsItem::isVisible() returns true), -
2803 QGraphicsScene will emit changed() once control goes back to the event -
2804 loop. -
2805 -
2806 Note that widgets with the Qt::WA_PaintOnScreen widget attribute -
2807 set and widgets that wrap an external application or controller -
2808 are not supported. Examples are QGLWidget and QAxWidget. -
2809 -
2810 \sa addEllipse(), addLine(), addPixmap(), addPixmap(), addRect(), -
2811 addText(), addSimpleText(), addItem() -
2812*/ -
2813QGraphicsProxyWidget *QGraphicsScene::addWidget(QWidget *widget, Qt::WindowFlags wFlags) -
2814{ -
2815 QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget(0, wFlags); -
2816 proxy->setWidget(widget); -
2817 addItem(proxy); -
2818 return proxy; -
2819} -
2820 -
2821/*! -
2822 Removes the item \a item and all its children from the scene. The -
2823 ownership of \a item is passed on to the caller (i.e., -
2824 QGraphicsScene will no longer delete \a item when destroyed). -
2825 -
2826 \sa addItem() -
2827*/ -
2828void QGraphicsScene::removeItem(QGraphicsItem *item) -
2829{ -
2830 // ### Refactoring: This function shares much functionality with _q_removeItemLater() -
2831 Q_D(QGraphicsScene); -
2832 if (!item) { -
2833 qWarning("QGraphicsScene::removeItem: cannot remove 0-item"); -
2834 return; -
2835 } -
2836 if (item->scene() != this) { -
2837 qWarning("QGraphicsScene::removeItem: item %p's scene (%p)" -
2838 " is different from this scene (%p)", -
2839 item, item->scene(), this); -
2840 return; -
2841 } -
2842 -
2843 // Notify the item that it's scene is changing to 0, allowing the item to -
2844 // react. -
2845 const QVariant newSceneVariant(item->itemChange(QGraphicsItem::ItemSceneChange, -
2846 QVariant::fromValue<QGraphicsScene *>(0))); -
2847 QGraphicsScene *targetScene = qvariant_cast<QGraphicsScene *>(newSceneVariant); -
2848 if (targetScene != 0 && targetScene != this) { -
2849 targetScene->addItem(item); -
2850 return; -
2851 } -
2852 -
2853 d->removeItemHelper(item); -
2854 -
2855 // Deliver post-change notification -
2856 item->itemChange(QGraphicsItem::ItemSceneHasChanged, newSceneVariant); -
2857 -
2858 d->updateInputMethodSensitivityInViews(); -
2859} -
2860 -
2861/*! -
2862 When the scene is active, this functions returns the scene's current focus -
2863 item, or 0 if no item currently has focus. When the scene is inactive, this -
2864 functions returns the item that will gain input focus when the scene becomes -
2865 active. -
2866 -
2867 The focus item receives keyboard input when the scene receives a -
2868 key event. -
2869 -
2870 \sa setFocusItem(), QGraphicsItem::hasFocus(), isActive() -
2871*/ -
2872QGraphicsItem *QGraphicsScene::focusItem() const -
2873{ -
2874 Q_D(const QGraphicsScene); -
2875 return isActive() ? d->focusItem : d->passiveFocusItem; -
2876} -
2877 -
2878/*! -
2879 Sets the scene's focus item to \a item, with the focus reason \a -
2880 focusReason, after removing focus from any previous item that may have had -
2881 focus. -
2882 -
2883 If \a item is 0, or if it either does not accept focus (i.e., it does not -
2884 have the QGraphicsItem::ItemIsFocusable flag enabled), or is not visible -
2885 or not enabled, this function only removes focus from any previous -
2886 focusitem. -
2887 -
2888 If item is not 0, and the scene does not currently have focus (i.e., -
2889 hasFocus() returns false), this function will call setFocus() -
2890 automatically. -
2891 -
2892 \sa focusItem(), hasFocus(), setFocus() -
2893*/ -
2894void QGraphicsScene::setFocusItem(QGraphicsItem *item, Qt::FocusReason focusReason) -
2895{ -
2896 Q_D(QGraphicsScene); -
2897 if (item) -
2898 item->setFocus(focusReason); -
2899 else -
2900 d->setFocusItemHelper(item, focusReason); -
2901} -
2902 -
2903/*! -
2904 Returns true if the scene has focus; otherwise returns false. If the scene -
2905 has focus, it will will forward key events from QKeyEvent to any item that -
2906 has focus. -
2907 -
2908 \sa setFocus(), setFocusItem() -
2909*/ -
2910bool QGraphicsScene::hasFocus() const -
2911{ -
2912 Q_D(const QGraphicsScene); -
2913 return d->hasFocus; -
2914} -
2915 -
2916/*! -
2917 Sets focus on the scene by sending a QFocusEvent to the scene, passing \a -
2918 focusReason as the reason. If the scene regains focus after having -
2919 previously lost it while an item had focus, the last focus item will -
2920 receive focus with \a focusReason as the reason. -
2921 -
2922 If the scene already has focus, this function does nothing. -
2923 -
2924 \sa hasFocus(), clearFocus(), setFocusItem() -
2925*/ -
2926void QGraphicsScene::setFocus(Qt::FocusReason focusReason) -
2927{ -
2928 Q_D(QGraphicsScene); -
2929 if (d->hasFocus || !isActive()) -
2930 return; -
2931 QFocusEvent event(QEvent::FocusIn, focusReason); -
2932 QCoreApplication::sendEvent(this, &event); -
2933} -
2934 -
2935/*! -
2936 Clears focus from the scene. If any item has focus when this function is -
2937 called, it will lose focus, and regain focus again once the scene regains -
2938 focus. -
2939 -
2940 A scene that does not have focus ignores key events. -
2941 -
2942 \sa hasFocus(), setFocus(), setFocusItem() -
2943*/ -
2944void QGraphicsScene::clearFocus() -
2945{ -
2946 Q_D(QGraphicsScene); -
2947 if (d->hasFocus) { -
2948 d->hasFocus = false; -
2949 d->passiveFocusItem = d->focusItem; -
2950 setFocusItem(0, Qt::OtherFocusReason); -
2951 } -
2952} -
2953 -
2954/*! -
2955 \property QGraphicsScene::stickyFocus -
2956 \brief whether clicking into the scene background will clear focus -
2957 -
2958 \since 4.6 -
2959 -
2960 In a QGraphicsScene with stickyFocus set to true, focus will remain -
2961 unchanged when the user clicks into the scene background or on an item -
2962 that does not accept focus. Otherwise, focus will be cleared. -
2963 -
2964 By default, this property is false. -
2965 -
2966 Focus changes in response to a mouse press. You can reimplement -
2967 mousePressEvent() in a subclass of QGraphicsScene to toggle this property -
2968 based on where the user has clicked. -
2969 -
2970 \sa clearFocus(), setFocusItem() -
2971*/ -
2972void QGraphicsScene::setStickyFocus(bool enabled) -
2973{ -
2974 Q_D(QGraphicsScene); -
2975 d->stickyFocus = enabled; -
2976} -
2977bool QGraphicsScene::stickyFocus() const -
2978{ -
2979 Q_D(const QGraphicsScene); -
2980 return d->stickyFocus; -
2981} -
2982 -
2983/*! -
2984 Returns the current mouse grabber item, or 0 if no item is currently -
2985 grabbing the mouse. The mouse grabber item is the item that receives all -
2986 mouse events sent to the scene. -
2987 -
2988 An item becomes a mouse grabber when it receives and accepts a -
2989 mouse press event, and it stays the mouse grabber until either of -
2990 the following events occur: -
2991 -
2992 \list -
2993 \li If the item receives a mouse release event when there are no other -
2994 buttons pressed, it loses the mouse grab. -
2995 \li If the item becomes invisible (i.e., someone calls \c {item->setVisible(false)}), -
2996 or if it becomes disabled (i.e., someone calls \c {item->setEnabled(false)}), -
2997 it loses the mouse grab. -
2998 \li If the item is removed from the scene, it loses the mouse grab. -
2999 \endlist -
3000 -
3001 If the item loses its mouse grab, the scene will ignore all mouse events -
3002 until a new item grabs the mouse (i.e., until a new item receives a mouse -
3003 press event). -
3004*/ -
3005QGraphicsItem *QGraphicsScene::mouseGrabberItem() const -
3006{ -
3007 Q_D(const QGraphicsScene); -
3008 return !d->mouseGrabberItems.isEmpty() ? d->mouseGrabberItems.last() : 0; -
3009} -
3010 -
3011/*! -
3012 \property QGraphicsScene::backgroundBrush -
3013 \brief the background brush of the scene. -
3014 -
3015 Set this property to changes the scene's background to a different color, -
3016 gradient or texture. The default background brush is Qt::NoBrush. The -
3017 background is drawn before (behind) the items. -
3018 -
3019 Example: -
3020 -
3021 \snippet code/src_gui_graphicsview_qgraphicsscene.cpp 3 -
3022 -
3023 QGraphicsScene::render() calls drawBackground() to draw the scene -
3024 background. For more detailed control over how the background is drawn, -
3025 you can reimplement drawBackground() in a subclass of QGraphicsScene. -
3026*/ -
3027QBrush QGraphicsScene::backgroundBrush() const -
3028{ -
3029 Q_D(const QGraphicsScene); -
3030 return d->backgroundBrush; -
3031} -
3032void QGraphicsScene::setBackgroundBrush(const QBrush &brush) -
3033{ -
3034 Q_D(QGraphicsScene); -
3035 d->backgroundBrush = brush; -
3036 foreach (QGraphicsView *view, d->views) { -
3037 view->resetCachedContent(); -
3038 view->viewport()->update(); -
3039 } -
3040 update(); -
3041} -
3042 -
3043/*! -
3044 \property QGraphicsScene::foregroundBrush -
3045 \brief the foreground brush of the scene. -
3046 -
3047 Change this property to set the scene's foreground to a different -
3048 color, gradient or texture. -
3049 -
3050 The foreground is drawn after (on top of) the items. The default -
3051 foreground brush is Qt::NoBrush ( i.e. the foreground is not -
3052 drawn). -
3053 -
3054 Example: -
3055 -
3056 \snippet code/src_gui_graphicsview_qgraphicsscene.cpp 4 -
3057 -
3058 QGraphicsScene::render() calls drawForeground() to draw the scene -
3059 foreground. For more detailed control over how the foreground is -
3060 drawn, you can reimplement the drawForeground() function in a -
3061 QGraphicsScene subclass. -
3062*/ -
3063QBrush QGraphicsScene::foregroundBrush() const -
3064{ -
3065 Q_D(const QGraphicsScene); -
3066 return d->foregroundBrush; -
3067} -
3068void QGraphicsScene::setForegroundBrush(const QBrush &brush) -
3069{ -
3070 Q_D(QGraphicsScene); -
3071 d->foregroundBrush = brush; -
3072 foreach (QGraphicsView *view, views()) -
3073 view->viewport()->update(); -
3074 update(); -
3075} -
3076 -
3077/*! -
3078 This method is used by input methods to query a set of properties of -
3079 the scene to be able to support complex input method operations as support -
3080 for surrounding text and reconversions. -
3081 -
3082 The \a query parameter specifies which property is queried. -
3083 -
3084 \sa QWidget::inputMethodQuery() -
3085*/ -
3086QVariant QGraphicsScene::inputMethodQuery(Qt::InputMethodQuery query) const -
3087{ -
3088 Q_D(const QGraphicsScene); -
3089 if (!d->focusItem || !(d->focusItem->flags() & QGraphicsItem::ItemAcceptsInputMethod)) -
3090 return QVariant(); -
3091 const QTransform matrix = d->focusItem->sceneTransform(); -
3092 QVariant value = d->focusItem->inputMethodQuery(query); -
3093 if (value.type() == QVariant::RectF) -
3094 value = matrix.mapRect(value.toRectF()); -
3095 else if (value.type() == QVariant::PointF) -
3096 value = matrix.map(value.toPointF()); -
3097 else if (value.type() == QVariant::Rect) -
3098 value = matrix.mapRect(value.toRect()); -
3099 else if (value.type() == QVariant::Point) -
3100 value = matrix.map(value.toPoint()); -
3101 return value; -
3102} -
3103 -
3104/*! -
3105 \fn void QGraphicsScene::update(const QRectF &rect) -
3106 Schedules a redraw of the area \a rect on the scene. -
3107 -
3108 \sa sceneRect(), changed() -
3109*/ -
3110void QGraphicsScene::update(const QRectF &rect) -
3111{ -
3112 Q_D(QGraphicsScene); -
3113 if (d->updateAll || (rect.isEmpty() && !rect.isNull())) -
3114 return; -
3115 -
3116 // Check if anyone's connected; if not, we can send updates directly to -
3117 // the views. Otherwise or if there are no views, use old behavior. -
3118 bool directUpdates = !(d->isSignalConnected(d->changedSignalIndex)) && !d->views.isEmpty(); -
3119 if (rect.isNull()) { -
3120 d->updateAll = true; -
3121 d->updatedRects.clear(); -
3122 if (directUpdates) { -
3123 // Update all views. -
3124 for (int i = 0; i < d->views.size(); ++i) -
3125 d->views.at(i)->d_func()->fullUpdatePending = true; -
3126 } -
3127 } else { -
3128 if (directUpdates) { -
3129 // Update all views. -
3130 for (int i = 0; i < d->views.size(); ++i) { -
3131 QGraphicsView *view = d->views.at(i); -
3132 if (view->isTransformed()) -
3133 view->d_func()->updateRectF(view->viewportTransform().mapRect(rect)); -
3134 else -
3135 view->d_func()->updateRectF(rect); -
3136 } -
3137 } else { -
3138 d->updatedRects << rect; -
3139 } -
3140 } -
3141 -
3142 if (!d->calledEmitUpdated) { -
3143 d->calledEmitUpdated = true; -
3144 QMetaObject::invokeMethod(this, "_q_emitUpdated", Qt::QueuedConnection); -
3145 } -
3146} -
3147 -
3148/*! -
3149 \fn void QGraphicsScene::update(qreal x, qreal y, qreal w, qreal h) -
3150 \overload -
3151 \since 4.3 -
3152 -
3153 This function is equivalent to calling update(QRectF(\a x, \a y, \a w, -
3154 \a h)); -
3155*/ -
3156 -
3157/*! -
3158 Invalidates and schedules a redraw of the \a layers in \a rect on the -
3159 scene. Any cached content in \a layers is unconditionally invalidated and -
3160 redrawn. -
3161 -
3162 You can use this function overload to notify QGraphicsScene of changes to -
3163 the background or the foreground of the scene. This function is commonly -
3164 used for scenes with tile-based backgrounds to notify changes when -
3165 QGraphicsView has enabled -
3166 \l{QGraphicsView::CacheBackground}{CacheBackground}. -
3167 -
3168 Example: -
3169 -
3170 \snippet code/src_gui_graphicsview_qgraphicsscene.cpp 5 -
3171 -
3172 Note that QGraphicsView currently supports background caching only (see -
3173 QGraphicsView::CacheBackground). This function is equivalent to calling -
3174 update() if any layer but BackgroundLayer is passed. -
3175 -
3176 \sa QGraphicsView::resetCachedContent() -
3177*/ -
3178void QGraphicsScene::invalidate(const QRectF &rect, SceneLayers layers) -
3179{ -
3180 foreach (QGraphicsView *view, views()) -
3181 view->invalidateScene(rect, layers); -
3182 update(rect); -
3183} -
3184 -
3185/*! -
3186 \fn void QGraphicsScene::invalidate(qreal x, qreal y, qreal w, qreal h, SceneLayers layers) -
3187 \overload -
3188 \since 4.3 -
3189 -
3190 This convenience function is equivalent to calling invalidate(QRectF(\a x, \a -
3191 y, \a w, \a h), \a layers); -
3192*/ -
3193 -
3194/*! -
3195 Returns a list of all the views that display this scene. -
3196 -
3197 \sa QGraphicsView::scene() -
3198*/ -
3199QList <QGraphicsView *> QGraphicsScene::views() const -
3200{ -
3201 Q_D(const QGraphicsScene); -
3202 return d->views; -
3203} -
3204 -
3205/*! -
3206 This slot \e advances the scene by one step, by calling -
3207 QGraphicsItem::advance() for all items on the scene. This is done in two -
3208 phases: in the first phase, all items are notified that the scene is about -
3209 to change, and in the second phase all items are notified that they can -
3210 move. In the first phase, QGraphicsItem::advance() is called passing a -
3211 value of 0 as an argument, and 1 is passed in the second phase. -
3212 -
3213 Note that you can also use the \l{The Animation Framework}{Animation -
3214 Framework} for animations. -
3215 -
3216 \sa QGraphicsItem::advance(), QTimeLine -
3217*/ -
3218void QGraphicsScene::advance() -
3219{ -
3220 for (int i = 0; i < 2; ++i) { -
3221 foreach (QGraphicsItem *item, items()) -
3222 item->advance(i); -
3223 } -
3224} -
3225 -
3226/*! -
3227 Processes the event \a event, and dispatches it to the respective -
3228 event handlers. -
3229 -
3230 In addition to calling the convenience event handlers, this -
3231 function is responsible for converting mouse move events to hover -
3232 events for when there is no mouse grabber item. Hover events are -
3233 delivered directly to items; there is no convenience function for -
3234 them. -
3235 -
3236 Unlike QWidget, QGraphicsScene does not have the convenience functions -
3237 \l{QWidget::}{enterEvent()} and \l{QWidget::}{leaveEvent()}. Use this -
3238 function to obtain those events instead. -
3239 -
3240 \sa contextMenuEvent(), keyPressEvent(), keyReleaseEvent(), -
3241 mousePressEvent(), mouseMoveEvent(), mouseReleaseEvent(), -
3242 mouseDoubleClickEvent(), focusInEvent(), focusOutEvent() -
3243*/ -
3244bool QGraphicsScene::event(QEvent *event) -
3245{ -
3246 Q_D(QGraphicsScene); -
3247 -
3248 switch (event->type()) { -
3249 case QEvent::GraphicsSceneMousePress: -
3250 case QEvent::GraphicsSceneMouseMove: -
3251 case QEvent::GraphicsSceneMouseRelease: -
3252 case QEvent::GraphicsSceneMouseDoubleClick: -
3253 case QEvent::GraphicsSceneHoverEnter: -
3254 case QEvent::GraphicsSceneHoverLeave: -
3255 case QEvent::GraphicsSceneHoverMove: -
3256 case QEvent::TouchBegin: -
3257 case QEvent::TouchUpdate: -
3258 case QEvent::TouchEnd: -
3259 // Reset the under-mouse list to ensure that this event gets fresh -
3260 // item-under-mouse data. Be careful about this list; if people delete -
3261 // items from inside event handlers, this list can quickly end up -
3262 // having stale pointers in it. We need to clear it before dispatching -
3263 // events that use it. -
3264 // ### this should only be cleared if we received a new mouse move event, -
3265 // which relies on us fixing the replay mechanism in QGraphicsView. -
3266 d->cachedItemsUnderMouse.clear(); -
3267 default: -
3268 break; -
3269 } -
3270 -
3271 switch (event->type()) { -
3272 case QEvent::GraphicsSceneDragEnter: -
3273 dragEnterEvent(static_cast<QGraphicsSceneDragDropEvent *>(event)); -
3274 break; -
3275 case QEvent::GraphicsSceneDragMove: -
3276 dragMoveEvent(static_cast<QGraphicsSceneDragDropEvent *>(event)); -
3277 break; -
3278 case QEvent::GraphicsSceneDragLeave: -
3279 dragLeaveEvent(static_cast<QGraphicsSceneDragDropEvent *>(event)); -
3280 break; -
3281 case QEvent::GraphicsSceneDrop: -
3282 dropEvent(static_cast<QGraphicsSceneDragDropEvent *>(event)); -
3283 break; -
3284 case QEvent::GraphicsSceneContextMenu: -
3285 contextMenuEvent(static_cast<QGraphicsSceneContextMenuEvent *>(event)); -
3286 break; -
3287 case QEvent::KeyPress: -
3288 if (!d->focusItem) { -
3289 QKeyEvent *k = static_cast<QKeyEvent *>(event); -
3290 if (k->key() == Qt::Key_Tab || k->key() == Qt::Key_Backtab) { -
3291 if (!(k->modifiers() & (Qt::ControlModifier | Qt::AltModifier))) { //### Add MetaModifier? -
3292 bool res = false; -
3293 if (k->key() == Qt::Key_Backtab -
3294 || (k->key() == Qt::Key_Tab && (k->modifiers() & Qt::ShiftModifier))) { -
3295 res = focusNextPrevChild(false); -
3296 } else if (k->key() == Qt::Key_Tab) { -
3297 res = focusNextPrevChild(true); -
3298 } -
3299 if (!res) -
3300 event->ignore(); -
3301 return true; -
3302 } -
3303 } -
3304 } -
3305 keyPressEvent(static_cast<QKeyEvent *>(event)); -
3306 break; -
3307 case QEvent::KeyRelease: -
3308 keyReleaseEvent(static_cast<QKeyEvent *>(event)); -
3309 break; -
3310 case QEvent::ShortcutOverride: { -
3311 QGraphicsItem *parent = focusItem(); -
3312 while (parent) { -
3313 d->sendEvent(parent, event); -
3314 if (event->isAccepted()) -
3315 return true; -
3316 parent = parent->parentItem(); -
3317 } -
3318 } -
3319 return false; -
3320 case QEvent::GraphicsSceneMouseMove: -
3321 { -
3322 QGraphicsSceneMouseEvent *mouseEvent = static_cast<QGraphicsSceneMouseEvent *>(event); -
3323 d->lastSceneMousePos = mouseEvent->scenePos(); -
3324 mouseMoveEvent(mouseEvent); -
3325 break; -
3326 } -
3327 case QEvent::GraphicsSceneMousePress: -
3328 mousePressEvent(static_cast<QGraphicsSceneMouseEvent *>(event)); -
3329 break; -
3330 case QEvent::GraphicsSceneMouseRelease: -
3331 mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent *>(event)); -
3332 break; -
3333 case QEvent::GraphicsSceneMouseDoubleClick: -
3334 mouseDoubleClickEvent(static_cast<QGraphicsSceneMouseEvent *>(event)); -
3335 break; -
3336 case QEvent::GraphicsSceneWheel: -
3337 wheelEvent(static_cast<QGraphicsSceneWheelEvent *>(event)); -
3338 break; -
3339 case QEvent::FocusIn: -
3340 focusInEvent(static_cast<QFocusEvent *>(event)); -
3341 break; -
3342 case QEvent::FocusOut: -
3343 focusOutEvent(static_cast<QFocusEvent *>(event)); -
3344 break; -
3345 case QEvent::GraphicsSceneHoverEnter: -
3346 case QEvent::GraphicsSceneHoverLeave: -
3347 case QEvent::GraphicsSceneHoverMove: -
3348 { -
3349 QGraphicsSceneHoverEvent *hoverEvent = static_cast<QGraphicsSceneHoverEvent *>(event); -
3350 d->lastSceneMousePos = hoverEvent->scenePos(); -
3351 d->dispatchHoverEvent(hoverEvent); -
3352 break; -
3353 } -
3354 case QEvent::Leave: -
3355 // hackieshly unpacking the viewport pointer from the leave event. -
3356 d->leaveScene(reinterpret_cast<QWidget *>(event->d)); -
3357 break; -
3358 case QEvent::GraphicsSceneHelp: -
3359 helpEvent(static_cast<QGraphicsSceneHelpEvent *>(event)); -
3360 break; -
3361 case QEvent::InputMethod: -
3362 inputMethodEvent(static_cast<QInputMethodEvent *>(event)); -
3363 break; -
3364 case QEvent::WindowActivate: -
3365 if (!d->activationRefCount++) { -
3366 if (d->lastActivePanel) { -
3367 // Activate the last panel. -
3368 d->setActivePanelHelper(d->lastActivePanel, true); -
3369 } else if (d->tabFocusFirst && d->tabFocusFirst->isPanel()) { -
3370 // Activate the panel of the first item in the tab focus -
3371 // chain. -
3372 d->setActivePanelHelper(d->tabFocusFirst, true); -
3373 } else { -
3374 // Activate all toplevel items. -
3375 QEvent event(QEvent::WindowActivate); -
3376 foreach (QGraphicsItem *item, items()) { -
3377 if (item->isVisible() && !item->isPanel() && !item->parentItem()) -
3378 sendEvent(item, &event); -
3379 } -
3380 } -
3381 } -
3382 break; -
3383 case QEvent::WindowDeactivate: -
3384 if (!--d->activationRefCount) { -
3385 if (d->activePanel) { -
3386 // Deactivate the active panel (but keep it so we can -
3387 // reactivate it later). -
3388 QGraphicsItem *lastActivePanel = d->activePanel; -
3389 d->setActivePanelHelper(0, true); -
3390 d->lastActivePanel = lastActivePanel; -
3391 } else { -
3392 // Activate all toplevel items. -
3393 QEvent event(QEvent::WindowDeactivate); -
3394 foreach (QGraphicsItem *item, items()) { -
3395 if (item->isVisible() && !item->isPanel() && !item->parentItem()) -
3396 sendEvent(item, &event); -
3397 } -
3398 } -
3399 } -
3400 break; -
3401 case QEvent::ApplicationFontChange: { -
3402 // Resolve the existing scene font. -
3403 d->resolveFont(); -
3404 break; -
3405 } -
3406 case QEvent::FontChange: -
3407 // Update the entire scene when the font changes. -
3408 update(); -
3409 break; -
3410 case QEvent::ApplicationPaletteChange: { -
3411 // Resolve the existing scene palette. -
3412 d->resolvePalette(); -
3413 break; -
3414 } -
3415 case QEvent::PaletteChange: -
3416 // Update the entire scene when the palette changes. -
3417 update(); -
3418 break; -
3419 case QEvent::StyleChange: -
3420 // Reresolve all widgets' styles. Update all top-level widgets' -
3421 // geometries that do not have an explicit style set. -
3422 update(); -
3423 break; -
3424 case QEvent::StyleAnimationUpdate: -
3425 // Because QGraphicsItem is not a QObject, QStyle driven -
3426 // animations are forced to update the whole scene -
3427 update(); -
3428 break; -
3429 case QEvent::TouchBegin: -
3430 case QEvent::TouchUpdate: -
3431 case QEvent::TouchEnd: -
3432 d->touchEventHandler(static_cast<QTouchEvent *>(event)); -
3433 break; -
3434#ifndef QT_NO_GESTURES -
3435 case QEvent::Gesture: -
3436 case QEvent::GestureOverride: -
3437 d->gestureEventHandler(static_cast<QGestureEvent *>(event)); -
3438 break; -
3439#endif // QT_NO_GESTURES -
3440 default: -
3441 return QObject::event(event); -
3442 } -
3443 return true; -
3444} -
3445 -
3446/*! -
3447 \reimp -
3448 -
3449 QGraphicsScene filters QApplication's events to detect palette and font -
3450 changes. -
3451*/ -
3452bool QGraphicsScene::eventFilter(QObject *watched, QEvent *event) -
3453{ -
3454 if (watched != qApp) -
3455 return false; -
3456 -
3457 switch (event->type()) { -
3458 case QEvent::ApplicationPaletteChange: -
3459 QApplication::postEvent(this, new QEvent(QEvent::ApplicationPaletteChange)); -
3460 break; -
3461 case QEvent::ApplicationFontChange: -
3462 QApplication::postEvent(this, new QEvent(QEvent::ApplicationFontChange)); -
3463 break; -
3464 default: -
3465 break; -
3466 } -
3467 return false; -
3468} -
3469 -
3470/*! -
3471 This event handler, for event \a contextMenuEvent, can be reimplemented in -
3472 a subclass to receive context menu events. The default implementation -
3473 forwards the event to the topmost visible item that accepts context menu events at -
3474 the position of the event. If no items accept context menu events at this -
3475 position, the event is ignored. -
3476 -
3477 Note: See items() for a definition of which items are considered visible by this function. -
3478 -
3479 \sa QGraphicsItem::contextMenuEvent() -
3480*/ -
3481void QGraphicsScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMenuEvent) -
3482{ -
3483 Q_D(QGraphicsScene); -
3484 // Ignore by default. -
3485 contextMenuEvent->ignore(); -
3486 -
3487 // Send the event to all items at this position until one item accepts the -
3488 // event. -
3489 foreach (QGraphicsItem *item, d->itemsAtPosition(contextMenuEvent->screenPos(), -
3490 contextMenuEvent->scenePos(), -
3491 contextMenuEvent->widget())) { -
3492 contextMenuEvent->setPos(item->d_ptr->genericMapFromScene(contextMenuEvent->scenePos(), -
3493 contextMenuEvent->widget())); -
3494 contextMenuEvent->accept(); -
3495 if (!d->sendEvent(item, contextMenuEvent)) -
3496 break; -
3497 -
3498 if (contextMenuEvent->isAccepted()) -
3499 break; -
3500 } -
3501} -
3502 -
3503/*! -
3504 This event handler, for event \a event, can be reimplemented in a subclass -
3505 to receive drag enter events for the scene. -
3506 -
3507 The default implementation accepts the event and prepares the scene to -
3508 accept drag move events. -
3509 -
3510 \sa QGraphicsItem::dragEnterEvent(), dragMoveEvent(), dragLeaveEvent(), -
3511 dropEvent() -
3512*/ -
3513void QGraphicsScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event) -
3514{ -
3515 Q_D(QGraphicsScene); -
3516 d->dragDropItem = 0; -
3517 d->lastDropAction = Qt::IgnoreAction; -
3518 event->accept(); -
3519} -
3520 -
3521/*! -
3522 This event handler, for event \a event, can be reimplemented in a subclass -
3523 to receive drag move events for the scene. -
3524 -
3525 Note: See items() for a definition of which items are considered visible by this function. -
3526 -
3527 \sa QGraphicsItem::dragMoveEvent(), dragEnterEvent(), dragLeaveEvent(), -
3528 dropEvent() -
3529*/ -
3530void QGraphicsScene::dragMoveEvent(QGraphicsSceneDragDropEvent *event) -
3531{ -
3532 Q_D(QGraphicsScene); -
3533 event->ignore(); -
3534 -
3535 if (!d->mouseGrabberItems.isEmpty()) { -
3536 // Mouse grabbers that start drag events lose the mouse grab. -
3537 d->clearMouseGrabber(); -
3538 d->mouseGrabberButtonDownPos.clear(); -
3539 d->mouseGrabberButtonDownScenePos.clear(); -
3540 d->mouseGrabberButtonDownScreenPos.clear(); -
3541 } -
3542 -
3543 bool eventDelivered = false; -
3544 -
3545 // Find the topmost enabled items under the cursor. They are all -
3546 // candidates for accepting drag & drop events. -
3547 foreach (QGraphicsItem *item, d->itemsAtPosition(event->screenPos(), -
3548 event->scenePos(), -
3549 event->widget())) { -
3550 if (!item->isEnabled() || !item->acceptDrops()) -
3551 continue; -
3552 -
3553 if (item != d->dragDropItem) { -
3554 // Enter the new drag drop item. If it accepts the event, we send -
3555 // the leave to the parent item. -
3556 QGraphicsSceneDragDropEvent dragEnter(QEvent::GraphicsSceneDragEnter); -
3557 d->cloneDragDropEvent(&dragEnter, event); -
3558 dragEnter.setDropAction(event->proposedAction()); -
3559 d->sendDragDropEvent(item, &dragEnter); -
3560 event->setAccepted(dragEnter.isAccepted()); -
3561 event->setDropAction(dragEnter.dropAction()); -
3562 if (!event->isAccepted()) { -
3563 // Propagate to the item under -
3564 continue; -
3565 } -
3566 -
3567 d->lastDropAction = event->dropAction(); -
3568 -
3569 if (d->dragDropItem) { -
3570 // Leave the last drag drop item. A perfect implementation -
3571 // would set the position of this event to the point where -
3572 // this event and the last event intersect with the item's -
3573 // shape, but that's not easy to do. :-) -
3574 QGraphicsSceneDragDropEvent dragLeave(QEvent::GraphicsSceneDragLeave); -
3575 d->cloneDragDropEvent(&dragLeave, event); -
3576 d->sendDragDropEvent(d->dragDropItem, &dragLeave); -
3577 } -
3578 -
3579 // We've got a new drag & drop item -
3580 d->dragDropItem = item; -
3581 } -
3582 -
3583 // Send the move event. -
3584 event->setDropAction(d->lastDropAction); -
3585 event->accept(); -
3586 d->sendDragDropEvent(item, event); -
3587 if (event->isAccepted()) -
3588 d->lastDropAction = event->dropAction(); -
3589 eventDelivered = true; -
3590 break; -
3591 } -
3592 -
3593 if (!eventDelivered) { -
3594 if (d->dragDropItem) { -
3595 // Leave the last drag drop item -
3596 QGraphicsSceneDragDropEvent dragLeave(QEvent::GraphicsSceneDragLeave); -
3597 d->cloneDragDropEvent(&dragLeave, event); -
3598 d->sendDragDropEvent(d->dragDropItem, &dragLeave); -
3599 d->dragDropItem = 0; -
3600 } -
3601 // Propagate -
3602 event->setDropAction(Qt::IgnoreAction); -
3603 } -
3604} -
3605 -
3606/*! -
3607 This event handler, for event \a event, can be reimplemented in a subclass -
3608 to receive drag leave events for the scene. -
3609 -
3610 \sa QGraphicsItem::dragLeaveEvent(), dragEnterEvent(), dragMoveEvent(), -
3611 dropEvent() -
3612*/ -
3613void QGraphicsScene::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) -
3614{ -
3615 Q_D(QGraphicsScene); -
3616 if (d->dragDropItem) { -
3617 // Leave the last drag drop item -
3618 d->sendDragDropEvent(d->dragDropItem, event); -
3619 d->dragDropItem = 0; -
3620 } -
3621} -
3622 -
3623/*! -
3624 This event handler, for event \a event, can be reimplemented in a subclass -
3625 to receive drop events for the scene. -
3626 -
3627 \sa QGraphicsItem::dropEvent(), dragEnterEvent(), dragMoveEvent(), -
3628 dragLeaveEvent() -
3629*/ -
3630void QGraphicsScene::dropEvent(QGraphicsSceneDragDropEvent *event) -
3631{ -
3632 Q_UNUSED(event); -
3633 Q_D(QGraphicsScene); -
3634 if (d->dragDropItem) { -
3635 // Drop on the last drag drop item -
3636 d->sendDragDropEvent(d->dragDropItem, event); -
3637 d->dragDropItem = 0; -
3638 } -
3639} -
3640 -
3641/*! -
3642 This event handler, for event \a focusEvent, can be reimplemented in a -
3643 subclass to receive focus in events. -
3644 -
3645 The default implementation sets focus on the scene, and then on the last -
3646 focus item. -
3647 -
3648 \sa QGraphicsItem::focusOutEvent() -
3649*/ -
3650void QGraphicsScene::focusInEvent(QFocusEvent *focusEvent) -
3651{ -
3652 Q_D(QGraphicsScene); -
3653 -
3654 d->hasFocus = true; -
3655 switch (focusEvent->reason()) { -
3656 case Qt::TabFocusReason: -
3657 if (!focusNextPrevChild(true)) -
3658 focusEvent->ignore(); -
3659 break; -
3660 case Qt::BacktabFocusReason: -
3661 if (!focusNextPrevChild(false)) -
3662 focusEvent->ignore(); -
3663 break; -
3664 default: -
3665 if (d->passiveFocusItem) { -
3666 // Set focus on the last focus item -
3667 setFocusItem(d->passiveFocusItem, focusEvent->reason()); -
3668 } -
3669 break; -
3670 } -
3671} -
3672 -
3673/*! -
3674 This event handler, for event \a focusEvent, can be reimplemented in a -
3675 subclass to receive focus out events. -
3676 -
3677 The default implementation removes focus from any focus item, then removes -
3678 focus from the scene. -
3679 -
3680 \sa QGraphicsItem::focusInEvent() -
3681*/ -
3682void QGraphicsScene::focusOutEvent(QFocusEvent *focusEvent) -
3683{ -
3684 Q_D(QGraphicsScene); -
3685 d->hasFocus = false; -
3686 d->passiveFocusItem = d->focusItem; -
3687 setFocusItem(0, focusEvent->reason()); -
3688 -
3689 // Remove all popups when the scene loses focus. -
3690 if (!d->popupWidgets.isEmpty()) -
3691 d->removePopup(d->popupWidgets.first()); -
3692} -
3693 -
3694/*! -
3695 This event handler, for event \a helpEvent, can be -
3696 reimplemented in a subclass to receive help events. The events -
3697 are of type QEvent::ToolTip, which are created when a tooltip is -
3698 requested. -
3699 -
3700 The default implementation shows the tooltip of the topmost -
3701 visible item, i.e., the item with the highest z-value, at the mouse -
3702 cursor position. If no item has a tooltip set, this function -
3703 does nothing. -
3704 -
3705 Note: See items() for a definition of which items are considered visible by this function. -
3706 -
3707 \sa QGraphicsItem::toolTip(), QGraphicsSceneHelpEvent -
3708*/ -
3709void QGraphicsScene::helpEvent(QGraphicsSceneHelpEvent *helpEvent) -
3710{ -
3711#ifdef QT_NO_TOOLTIP -
3712 Q_UNUSED(helpEvent); -
3713#else -
3714 // Find the first item that does tooltips -
3715 Q_D(QGraphicsScene); -
3716 QList<QGraphicsItem *> itemsAtPos = d->itemsAtPosition(helpEvent->screenPos(), -
3717 helpEvent->scenePos(), -
3718 helpEvent->widget()); -
3719 QGraphicsItem *toolTipItem = 0; -
3720 for (int i = 0; i < itemsAtPos.size(); ++i) { -
3721 QGraphicsItem *tmp = itemsAtPos.at(i); -
3722 if (tmp->d_func()->isProxyWidget()) { -
3723 // if the item is a proxy widget, the event is forwarded to it -
3724 sendEvent(tmp, helpEvent); -
3725 if (helpEvent->isAccepted()) -
3726 return; -
3727 } -
3728 if (!tmp->toolTip().isEmpty()) { -
3729 toolTipItem = tmp; -
3730 break; -
3731 } -
3732 } -
3733 -
3734 // Show or hide the tooltip -
3735 QString text; -
3736 QPoint point; -
3737 if (toolTipItem && !toolTipItem->toolTip().isEmpty()) { -
3738 text = toolTipItem->toolTip(); -
3739 point = helpEvent->screenPos(); -
3740 } -
3741 QToolTip::showText(point, text, helpEvent->widget()); -
3742 helpEvent->setAccepted(!text.isEmpty()); -
3743#endif -
3744} -
3745 -
3746bool QGraphicsScenePrivate::itemAcceptsHoverEvents_helper(const QGraphicsItem *item) const -
3747{ -
3748 return (item->d_ptr->acceptsHover -
3749 || (item->d_ptr->isWidget -
3750 && static_cast<const QGraphicsWidget *>(item)->d_func()->hasDecoration())) -
3751 && !item->isBlockedByModalPanel(); -
3752} -
3753 -
3754/*! -
3755 This event handler, for event \a hoverEvent, can be reimplemented in a -
3756 subclass to receive hover enter events. The default implementation -
3757 forwards the event to the topmost visible item that accepts hover events at the -
3758 scene position from the event. -
3759 -
3760 Note: See items() for a definition of which items are considered visible by this function. -
3761 -
3762 \sa QGraphicsItem::hoverEvent(), QGraphicsItem::setAcceptHoverEvents() -
3763*/ -
3764bool QGraphicsScenePrivate::dispatchHoverEvent(QGraphicsSceneHoverEvent *hoverEvent) -
3765{ -
3766 if (allItemsIgnoreHoverEvents) -
3767 return false; -
3768 -
3769 // Find the first item that accepts hover events, reusing earlier -
3770 // calculated data is possible. -
3771 if (cachedItemsUnderMouse.isEmpty()) { -
3772 cachedItemsUnderMouse = itemsAtPosition(hoverEvent->screenPos(), -
3773 hoverEvent->scenePos(), -
3774 hoverEvent->widget()); -
3775 } -
3776 -
3777 QGraphicsItem *item = 0; -
3778 for (int i = 0; i < cachedItemsUnderMouse.size(); ++i) { -
3779 QGraphicsItem *tmp = cachedItemsUnderMouse.at(i); -
3780 if (itemAcceptsHoverEvents_helper(tmp)) { -
3781 item = tmp; -
3782 break; -
3783 } -
3784 } -
3785 -
3786 // Find the common ancestor item for the new topmost hoverItem and the -
3787 // last item in the hoverItem list. -
3788 QGraphicsItem *commonAncestorItem = (item && !hoverItems.isEmpty()) ? item->commonAncestorItem(hoverItems.last()) : 0; -
3789 while (commonAncestorItem && !itemAcceptsHoverEvents_helper(commonAncestorItem)) -
3790 commonAncestorItem = commonAncestorItem->parentItem(); -
3791 if (commonAncestorItem && commonAncestorItem->panel() != item->panel()) { -
3792 // The common ancestor isn't in the same panel as the two hovered -
3793 // items. -
3794 commonAncestorItem = 0; -
3795 } -
3796 -
3797 // Check if the common ancestor item is known. -
3798 int index = commonAncestorItem ? hoverItems.indexOf(commonAncestorItem) : -1; -
3799 // Send hover leaves to any existing hovered children of the common -
3800 // ancestor item. -
3801 for (int i = hoverItems.size() - 1; i > index; --i) { -
3802 QGraphicsItem *lastItem = hoverItems.takeLast(); -
3803 if (itemAcceptsHoverEvents_helper(lastItem)) -
3804 sendHoverEvent(QEvent::GraphicsSceneHoverLeave, lastItem, hoverEvent); -
3805 } -
3806 -
3807 // Item is a child of a known item. Generate enter events for the -
3808 // missing links. -
3809 QList<QGraphicsItem *> parents; -
3810 QGraphicsItem *parent = item; -
3811 while (parent && parent != commonAncestorItem) { -
3812 parents.prepend(parent); -
3813 if (parent->isPanel()) { -
3814 // Stop at the panel - we don't deliver beyond this point. -
3815 break; -
3816 } -
3817 parent = parent->parentItem(); -
3818 } -
3819 for (int i = 0; i < parents.size(); ++i) { -
3820 parent = parents.at(i); -
3821 hoverItems << parent; -
3822 if (itemAcceptsHoverEvents_helper(parent)) -
3823 sendHoverEvent(QEvent::GraphicsSceneHoverEnter, parent, hoverEvent); -
3824 } -
3825 -
3826 // Generate a move event for the item itself -
3827 if (item -
3828 && !hoverItems.isEmpty() -
3829 && item == hoverItems.last()) { -
3830 sendHoverEvent(QEvent::GraphicsSceneHoverMove, item, hoverEvent); -
3831 return true; -
3832 } -
3833 return false; -
3834} -
3835 -
3836/*! -
3837 \internal -
3838 -
3839 Handles all actions necessary to clean up the scene when the mouse leaves -
3840 the view. -
3841*/ -
3842void QGraphicsScenePrivate::leaveScene(QWidget *viewport) -
3843{ -
3844#ifndef QT_NO_TOOLTIP -
3845 QToolTip::hideText(); -
3846#endif -
3847 QGraphicsView *view = qobject_cast<QGraphicsView *>(viewport->parent()); -
3848 // Send HoverLeave events to all existing hover items, topmost first. -
3849 QGraphicsSceneHoverEvent hoverEvent; -
3850 hoverEvent.setWidget(viewport); -
3851 -
3852 if (view) { -
3853 QPoint cursorPos = QCursor::pos(); -
3854 hoverEvent.setScenePos(view->mapToScene(viewport->mapFromGlobal(cursorPos))); -
3855 hoverEvent.setLastScenePos(hoverEvent.scenePos()); -
3856 hoverEvent.setScreenPos(cursorPos); -
3857 hoverEvent.setLastScreenPos(hoverEvent.screenPos()); -
3858 } -
3859 -
3860 while (!hoverItems.isEmpty()) { -
3861 QGraphicsItem *lastItem = hoverItems.takeLast(); -
3862 if (itemAcceptsHoverEvents_helper(lastItem)) -
3863 sendHoverEvent(QEvent::GraphicsSceneHoverLeave, lastItem, &hoverEvent); -
3864 } -
3865} -
3866 -
3867/*! -
3868 This event handler, for event \a keyEvent, can be reimplemented in a -
3869 subclass to receive keypress events. The default implementation forwards -
3870 the event to current focus item. -
3871 -
3872 \sa QGraphicsItem::keyPressEvent(), focusItem() -
3873*/ -
3874void QGraphicsScene::keyPressEvent(QKeyEvent *keyEvent) -
3875{ -
3876 // ### Merge this function with keyReleaseEvent; they are identical -
3877 // ### (except this comment). -
3878 Q_D(QGraphicsScene); -
3879 QGraphicsItem *item = !d->keyboardGrabberItems.isEmpty() ? d->keyboardGrabberItems.last() : 0; -
3880 if (!item) -
3881 item = focusItem(); -
3882 if (item) { -
3883 QGraphicsItem *p = item; -
3884 do { -
3885 // Accept the event by default -
3886 keyEvent->accept(); -
3887 // Send it; QGraphicsItem::keyPressEvent ignores it. If the event -
3888 // is filtered out, stop propagating it. -
3889 if (p->isBlockedByModalPanel()) -
3890 break; -
3891 if (!d->sendEvent(p, keyEvent)) -
3892 break; -
3893 } while (!keyEvent->isAccepted() && !p->isPanel() && (p = p->parentItem())); -
3894 } else { -
3895 keyEvent->ignore(); -
3896 } -
3897} -
3898 -
3899/*! -
3900 This event handler, for event \a keyEvent, can be reimplemented in a -
3901 subclass to receive key release events. The default implementation -
3902 forwards the event to current focus item. -
3903 -
3904 \sa QGraphicsItem::keyReleaseEvent(), focusItem() -
3905*/ -
3906void QGraphicsScene::keyReleaseEvent(QKeyEvent *keyEvent) -
3907{ -
3908 // ### Merge this function with keyPressEvent; they are identical (except -
3909 // ### this comment). -
3910 Q_D(QGraphicsScene); -
3911 QGraphicsItem *item = !d->keyboardGrabberItems.isEmpty() ? d->keyboardGrabberItems.last() : 0; -
3912 if (!item) -
3913 item = focusItem(); -
3914 if (item) { -
3915 QGraphicsItem *p = item; -
3916 do { -
3917 // Accept the event by default -
3918 keyEvent->accept(); -
3919 // Send it; QGraphicsItem::keyPressEvent ignores it. If the event -
3920 // is filtered out, stop propagating it. -
3921 if (p->isBlockedByModalPanel()) -
3922 break; -
3923 if (!d->sendEvent(p, keyEvent)) -
3924 break; -
3925 } while (!keyEvent->isAccepted() && !p->isPanel() && (p = p->parentItem())); -
3926 } else { -
3927 keyEvent->ignore(); -
3928 } -
3929} -
3930 -
3931/*! -
3932 This event handler, for event \a mouseEvent, can be reimplemented -
3933 in a subclass to receive mouse press events for the scene. -
3934 -
3935 The default implementation depends on the state of the scene. If -
3936 there is a mouse grabber item, then the event is sent to the mouse -
3937 grabber. Otherwise, it is forwarded to the topmost visible item that -
3938 accepts mouse events at the scene position from the event, and -
3939 that item promptly becomes the mouse grabber item. -
3940 -
3941 If there is no item at the given position on the scene, the -
3942 selection area is reset, any focus item loses its input focus, and -
3943 the event is then ignored. -
3944 -
3945 Note: See items() for a definition of which items are considered visible by this function. -
3946 -
3947 \sa QGraphicsItem::mousePressEvent(), -
3948 QGraphicsItem::setAcceptedMouseButtons() -
3949*/ -
3950void QGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) -
3951{ -
3952 Q_D(QGraphicsScene); -
3953 if (d->mouseGrabberItems.isEmpty()) { -
3954 // Dispatch hover events -
3955 QGraphicsSceneHoverEvent hover; -
3956 _q_hoverFromMouseEvent(&hover, mouseEvent); -
3957 d->dispatchHoverEvent(&hover); -
3958 } -
3959 -
3960 d->mousePressEventHandler(mouseEvent); -
3961} -
3962 -
3963/*! -
3964 This event handler, for event \a mouseEvent, can be reimplemented -
3965 in a subclass to receive mouse move events for the scene. -
3966 -
3967 The default implementation depends on the mouse grabber state. If there is -
3968 a mouse grabber item, the event is sent to the mouse grabber. If there -
3969 are any items that accept hover events at the current position, the event -
3970 is translated into a hover event and accepted; otherwise it's ignored. -
3971 -
3972 \sa QGraphicsItem::mousePressEvent(), QGraphicsItem::mouseReleaseEvent(), -
3973 QGraphicsItem::mouseDoubleClickEvent(), QGraphicsItem::setAcceptedMouseButtons() -
3974*/ -
3975void QGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent) -
3976{ -
3977 Q_D(QGraphicsScene); -
3978 if (d->mouseGrabberItems.isEmpty()) { -
3979 if (mouseEvent->buttons()) -
3980 return; -
3981 QGraphicsSceneHoverEvent hover; -
3982 _q_hoverFromMouseEvent(&hover, mouseEvent); -
3983 mouseEvent->setAccepted(d->dispatchHoverEvent(&hover)); -
3984 return; -
3985 } -
3986 -
3987 // Forward the event to the mouse grabber -
3988 d->sendMouseEvent(mouseEvent); -
3989 mouseEvent->accept(); -
3990} -
3991 -
3992/*! -
3993 This event handler, for event \a mouseEvent, can be reimplemented -
3994 in a subclass to receive mouse release events for the scene. -
3995 -
3996 The default implementation depends on the mouse grabber state. If -
3997 there is no mouse grabber, the event is ignored. Otherwise, if -
3998 there is a mouse grabber item, the event is sent to the mouse -
3999 grabber. If this mouse release represents the last pressed button -
4000 on the mouse, the mouse grabber item then loses the mouse grab. -
4001 -
4002 \sa QGraphicsItem::mousePressEvent(), QGraphicsItem::mouseMoveEvent(), -
4003 QGraphicsItem::mouseDoubleClickEvent(), QGraphicsItem::setAcceptedMouseButtons() -
4004*/ -
4005void QGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) -
4006{ -
4007 Q_D(QGraphicsScene); -
4008 if (d->mouseGrabberItems.isEmpty()) { -
4009 mouseEvent->ignore(); -
4010 return; -
4011 } -
4012 -
4013 // Forward the event to the mouse grabber -
4014 d->sendMouseEvent(mouseEvent); -
4015 mouseEvent->accept(); -
4016 -
4017 // Reset the mouse grabber when the last mouse button has been released. -
4018 if (!mouseEvent->buttons()) { -
4019 if (!d->mouseGrabberItems.isEmpty()) { -
4020 d->lastMouseGrabberItem = d->mouseGrabberItems.last(); -
4021 if (d->lastMouseGrabberItemHasImplicitMouseGrab) -
4022 d->mouseGrabberItems.last()->ungrabMouse(); -
4023 } else { -
4024 d->lastMouseGrabberItem = 0; -
4025 } -
4026 -
4027 // Generate a hoverevent -
4028 QGraphicsSceneHoverEvent hoverEvent; -
4029 _q_hoverFromMouseEvent(&hoverEvent, mouseEvent); -
4030 d->dispatchHoverEvent(&hoverEvent); -
4031 } -
4032} -
4033 -
4034/*! -
4035 This event handler, for event \a mouseEvent, can be reimplemented -
4036 in a subclass to receive mouse doubleclick events for the scene. -
4037 -
4038 If someone doubleclicks on the scene, the scene will first receive -
4039 a mouse press event, followed by a release event (i.e., a click), -
4040 then a doubleclick event, and finally a release event. If the -
4041 doubleclick event is delivered to a different item than the one -
4042 that received the first press and release, it will be delivered as -
4043 a press event. However, tripleclick events are not delivered as -
4044 doubleclick events in this case. -
4045 -
4046 The default implementation is similar to mousePressEvent(). -
4047 -
4048 Note: See items() for a definition of which items are considered visible by this function. -
4049 -
4050 \sa QGraphicsItem::mousePressEvent(), QGraphicsItem::mouseMoveEvent(), -
4051 QGraphicsItem::mouseReleaseEvent(), QGraphicsItem::setAcceptedMouseButtons() -
4052*/ -
4053void QGraphicsScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent) -
4054{ -
4055 Q_D(QGraphicsScene); -
4056 d->mousePressEventHandler(mouseEvent); -
4057} -
4058 -
4059/*! -
4060 This event handler, for event \a wheelEvent, can be reimplemented in a -
4061 subclass to receive mouse wheel events for the scene. -
4062 -
4063 By default, the event is delivered to the topmost visible item under the -
4064 cursor. If ignored, the event propagates to the item beneath, and again -
4065 until the event is accepted, or it reaches the scene. If no items accept -
4066 the event, it is ignored. -
4067 -
4068 Note: See items() for a definition of which items are considered visible by this function. -
4069 -
4070 \sa QGraphicsItem::wheelEvent() -
4071*/ -
4072void QGraphicsScene::wheelEvent(QGraphicsSceneWheelEvent *wheelEvent) -
4073{ -
4074 Q_D(QGraphicsScene); -
4075 QList<QGraphicsItem *> wheelCandidates = d->itemsAtPosition(wheelEvent->screenPos(), -
4076 wheelEvent->scenePos(), -
4077 wheelEvent->widget()); -
4078 -
4079#ifdef Q_WS_MAC -
4080 // On Mac, ignore the event if the first item under the mouse is not the last opened -
4081 // popup (or one of its descendant) -
4082 if (!d->popupWidgets.isEmpty() && !wheelCandidates.isEmpty() && wheelCandidates.first() != d->popupWidgets.back() && !d->popupWidgets.back()->isAncestorOf(wheelCandidates.first())) { -
4083 wheelEvent->accept(); -
4084 return; -
4085 } -
4086#else -
4087 // Find the first popup under the mouse (including the popup's descendants) starting from the last. -
4088 // Remove all popups after the one found, or all or them if no popup is under the mouse. -
4089 // Then continue with the event. -
4090 QList<QGraphicsWidget *>::const_iterator iter = d->popupWidgets.constEnd(); -
4091 while (--iter >= d->popupWidgets.constBegin() && !wheelCandidates.isEmpty()) { -
4092 if (wheelCandidates.first() == *iter || (*iter)->isAncestorOf(wheelCandidates.first())) -
4093 break; -
4094 d->removePopup(*iter); -
4095 } -
4096#endif -
4097 -
4098 bool hasSetFocus = false; -
4099 foreach (QGraphicsItem *item, wheelCandidates) { -
4100 if (!hasSetFocus && item->isEnabled() -
4101 && ((item->flags() & QGraphicsItem::ItemIsFocusable) && item->d_ptr->mouseSetsFocus)) { -
4102 if (item->isWidget() && static_cast<QGraphicsWidget *>(item)->focusPolicy() == Qt::WheelFocus) { -
4103 hasSetFocus = true; -
4104 if (item != focusItem()) -
4105 setFocusItem(item, Qt::MouseFocusReason); -
4106 } -
4107 } -
4108 -
4109 wheelEvent->setPos(item->d_ptr->genericMapFromScene(wheelEvent->scenePos(), -
4110 wheelEvent->widget())); -
4111 wheelEvent->accept(); -
4112 bool isPanel = item->isPanel(); -
4113 d->sendEvent(item, wheelEvent); -
4114 if (isPanel || wheelEvent->isAccepted()) -
4115 break; -
4116 } -
4117} -
4118 -
4119/*! -
4120 This event handler, for event \a event, can be reimplemented in a -
4121 subclass to receive input method events for the scene. -
4122 -
4123 The default implementation forwards the event to the focusItem(). -
4124 If no item currently has focus or the current focus item does not -
4125 accept input methods, this function does nothing. -
4126 -
4127 \sa QGraphicsItem::inputMethodEvent() -
4128*/ -
4129void QGraphicsScene::inputMethodEvent(QInputMethodEvent *event) -
4130{ -
4131 Q_D(QGraphicsScene); -
4132 if (d->focusItem && (d->focusItem->flags() & QGraphicsItem::ItemAcceptsInputMethod)) -
4133 d->sendEvent(d->focusItem, event); -
4134} -
4135 -
4136/*! -
4137 Draws the background of the scene using \a painter, before any items and -
4138 the foreground are drawn. Reimplement this function to provide a custom -
4139 background for the scene. -
4140 -
4141 All painting is done in \e scene coordinates. The \a rect -
4142 parameter is the exposed rectangle. -
4143 -
4144 If all you want is to define a color, texture, or gradient for the -
4145 background, you can call setBackgroundBrush() instead. -
4146 -
4147 \sa drawForeground(), drawItems() -
4148*/ -
4149void QGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect) -
4150{ -
4151 Q_D(QGraphicsScene); -
4152 -
4153 if (d->backgroundBrush.style() != Qt::NoBrush) { -
4154 if (d->painterStateProtection) -
4155 painter->save(); -
4156 painter->setBrushOrigin(0, 0); -
4157 painter->fillRect(rect, backgroundBrush()); -
4158 if (d->painterStateProtection) -
4159 painter->restore(); -
4160 } -
4161} -
4162 -
4163/*! -
4164 Draws the foreground of the scene using \a painter, after the background -
4165 and all items have been drawn. Reimplement this function to provide a -
4166 custom foreground for the scene. -
4167 -
4168 All painting is done in \e scene coordinates. The \a rect -
4169 parameter is the exposed rectangle. -
4170 -
4171 If all you want is to define a color, texture or gradient for the -
4172 foreground, you can call setForegroundBrush() instead. -
4173 -
4174 \sa drawBackground(), drawItems() -
4175*/ -
4176void QGraphicsScene::drawForeground(QPainter *painter, const QRectF &rect) -
4177{ -
4178 Q_D(QGraphicsScene); -
4179 -
4180 if (d->foregroundBrush.style() != Qt::NoBrush) { -
4181 if (d->painterStateProtection) -
4182 painter->save(); -
4183 painter->setBrushOrigin(0, 0); -
4184 painter->fillRect(rect, foregroundBrush()); -
4185 if (d->painterStateProtection) -
4186 painter->restore(); -
4187 } -
4188} -
4189 -
4190static void _q_paintItem(QGraphicsItem *item, QPainter *painter, -
4191 const QStyleOptionGraphicsItem *option, QWidget *widget, -
4192 bool useWindowOpacity, bool painterStateProtection) -
4193{ -
4194 if (!item->isWidget()) { -
4195 item->paint(painter, option, widget); -
4196 return; -
4197 } -
4198 QGraphicsWidget *widgetItem = static_cast<QGraphicsWidget *>(item); -
4199 QGraphicsProxyWidget *proxy = qobject_cast<QGraphicsProxyWidget *>(widgetItem); -
4200 const qreal windowOpacity = (proxy && proxy->widget() && useWindowOpacity) -
4201 ? proxy->widget()->windowOpacity() : 1.0; -
4202 const qreal oldPainterOpacity = painter->opacity(); -
4203 -
4204 if (qFuzzyIsNull(windowOpacity)) -
4205 return; -
4206 // Set new painter opacity. -
4207 if (windowOpacity < 1.0) -
4208 painter->setOpacity(oldPainterOpacity * windowOpacity); -
4209 -
4210 // set layoutdirection on the painter -
4211 Qt::LayoutDirection oldLayoutDirection = painter->layoutDirection(); -
4212 painter->setLayoutDirection(widgetItem->layoutDirection()); -
4213 -
4214 if (widgetItem->isWindow() && widgetItem->windowType() != Qt::Popup && widgetItem->windowType() != Qt::ToolTip -
4215 && !(widgetItem->windowFlags() & Qt::FramelessWindowHint)) { -
4216 if (painterStateProtection) -
4217 painter->save(); -
4218 widgetItem->paintWindowFrame(painter, option, widget); -
4219 if (painterStateProtection) -
4220 painter->restore(); -
4221 } else if (widgetItem->autoFillBackground()) { -
4222 painter->fillRect(option->exposedRect, widgetItem->palette().window()); -
4223 } -
4224 -
4225 widgetItem->paint(painter, option, widget); -
4226 -
4227 // Restore layoutdirection on the painter. -
4228 painter->setLayoutDirection(oldLayoutDirection); -
4229 // Restore painter opacity. -
4230 if (windowOpacity < 1.0) -
4231 painter->setOpacity(oldPainterOpacity); -
4232} -
4233 -
4234static void _q_paintIntoCache(QPixmap *pix, QGraphicsItem *item, const QRegion &pixmapExposed, -
4235 const QTransform &itemToPixmap, QPainter::RenderHints renderHints, -
4236 const QStyleOptionGraphicsItem *option, bool painterStateProtection) -
4237{ -
4238 QPixmap subPix; -
4239 QPainter pixmapPainter; -
4240 QRect br = pixmapExposed.boundingRect(); -
4241 -
4242 // Don't use subpixmap if we get a full update. -
4243 if (pixmapExposed.isEmpty() || (pixmapExposed.rectCount() == 1 && br.contains(pix->rect()))) { -
4244 pix->fill(Qt::transparent); -
4245 pixmapPainter.begin(pix); -
4246 } else { -
4247 subPix = QPixmap(br.size()); -
4248 subPix.fill(Qt::transparent); -
4249 pixmapPainter.begin(&subPix); -
4250 pixmapPainter.translate(-br.topLeft()); -
4251 if (!pixmapExposed.isEmpty()) { -
4252 // Applied to subPix; paint is adjusted to the coordinate space is -
4253 // correct. -
4254 pixmapPainter.setClipRegion(pixmapExposed); -
4255 } -
4256 } -
4257 -
4258 pixmapPainter.setRenderHints(pixmapPainter.renderHints(), false); -
4259 pixmapPainter.setRenderHints(renderHints, true); -
4260 pixmapPainter.setWorldTransform(itemToPixmap, true); -
4261 -
4262 // Render. -
4263 _q_paintItem(item, &pixmapPainter, option, 0, false, painterStateProtection); -
4264 pixmapPainter.end(); -
4265 -
4266 if (!subPix.isNull()) { -
4267 // Blit the subpixmap into the main pixmap. -
4268 pixmapPainter.begin(pix); -
4269 pixmapPainter.setCompositionMode(QPainter::CompositionMode_Source); -
4270 pixmapPainter.setClipRegion(pixmapExposed); -
4271 pixmapPainter.drawPixmap(br.topLeft(), subPix); -
4272 pixmapPainter.end(); -
4273 } -
4274} -
4275 -
4276// Copied from qpaintengine_vg.cpp -
4277// Returns true for 90, 180, and 270 degree rotations. -
4278static inline bool transformIsSimple(const QTransform& transform) -
4279{ -
4280 QTransform::TransformationType type = transform.type(); -
4281 if (type <= QTransform::TxScale) { -
4282 return true; -
4283 } else if (type == QTransform::TxRotate) { -
4284 // Check for 90, and 270 degree rotations. -
4285 qreal m11 = transform.m11(); -
4286 qreal m12 = transform.m12(); -
4287 qreal m21 = transform.m21(); -
4288 qreal m22 = transform.m22(); -
4289 if (m11 == 0.0f && m22 == 0.0f) { -
4290 if (m12 == 1.0f && m21 == -1.0f) -
4291 return true; // 90 degrees. -
4292 else if (m12 == -1.0f && m21 == 1.0f) -
4293 return true; // 270 degrees. -
4294 else if (m12 == -1.0f && m21 == -1.0f) -
4295 return true; // 90 degrees inverted y. -
4296 else if (m12 == 1.0f && m21 == 1.0f) -
4297 return true; // 270 degrees inverted y. -
4298 } -
4299 } -
4300 return false; -
4301} -
4302 -
4303/*! -
4304 \internal -
4305 -
4306 Draws items directly, or using cache. -
4307*/ -
4308void QGraphicsScenePrivate::drawItemHelper(QGraphicsItem *item, QPainter *painter, -
4309 const QStyleOptionGraphicsItem *option, QWidget *widget, -
4310 bool painterStateProtection) -
4311{ -
4312 QGraphicsItemPrivate *itemd = item->d_ptr.data(); -
4313 QGraphicsItem::CacheMode cacheMode = QGraphicsItem::CacheMode(itemd->cacheMode); -
4314 -
4315 // Render directly, using no cache. -
4316 if (cacheMode == QGraphicsItem::NoCache -
4317#ifdef Q_WS_X11 -
4318 || !X11->use_xrender -
4319#endif -
4320 ) { -
4321 _q_paintItem(static_cast<QGraphicsWidget *>(item), painter, option, widget, true, painterStateProtection); -
4322 return; -
4323 } -
4324 -
4325 const qreal oldPainterOpacity = painter->opacity(); -
4326 qreal newPainterOpacity = oldPainterOpacity; -
4327 QGraphicsProxyWidget *proxy = item->isWidget() ? qobject_cast<QGraphicsProxyWidget *>(static_cast<QGraphicsWidget *>(item)) : 0; -
4328 if (proxy && proxy->widget()) { -
4329 const qreal windowOpacity = proxy->widget()->windowOpacity(); -
4330 if (windowOpacity < 1.0) -
4331 newPainterOpacity *= windowOpacity; -
4332 } -
4333 -
4334 // Item's (local) bounding rect -
4335 QRectF brect = item->boundingRect(); -
4336 QRectF adjustedBrect(brect); -
4337 _q_adjustRect(&adjustedBrect); -
4338 if (adjustedBrect.isEmpty()) -
4339 return; -
4340 -
4341 // Fetch the off-screen transparent buffer and exposed area info. -
4342 QPixmapCache::Key pixmapKey; -
4343 QPixmap pix; -
4344 bool pixmapFound; -
4345 QGraphicsItemCache *itemCache = itemd->extraItemCache(); -
4346 if (cacheMode == QGraphicsItem::ItemCoordinateCache) { -
4347 pixmapKey = itemCache->key; -
4348 } else { -
4349 pixmapKey = itemCache->deviceData.value(widget).key; -
4350 } -
4351 -
4352 // Find pixmap in cache. -
4353 pixmapFound = QPixmapCache::find(pixmapKey, &pix); -
4354 -
4355 // Render using item coordinate cache mode. -
4356 if (cacheMode == QGraphicsItem::ItemCoordinateCache) { -
4357 QSize pixmapSize; -
4358 bool fixedCacheSize = false; -
4359 QRect br = brect.toAlignedRect(); -
4360 if ((fixedCacheSize = itemCache->fixedSize.isValid())) { -
4361 pixmapSize = itemCache->fixedSize; -
4362 } else { -
4363 pixmapSize = br.size(); -
4364 } -
4365 -
4366 // Create or recreate the pixmap. -
4367 int adjust = itemCache->fixedSize.isValid() ? 0 : 2; -
4368 QSize adjustSize(adjust*2, adjust*2); -
4369 br.adjust(-adjust, -adjust, adjust, adjust); -
4370 if (pix.isNull() || (!fixedCacheSize && (pixmapSize + adjustSize) != pix.size())) { -
4371 pix = QPixmap(pixmapSize + adjustSize); -
4372 itemCache->boundingRect = br; -
4373 itemCache->exposed.clear(); -
4374 itemCache->allExposed = true; -
4375 } else if (itemCache->boundingRect != br) { -
4376 itemCache->boundingRect = br; -
4377 itemCache->exposed.clear(); -
4378 itemCache->allExposed = true; -
4379 } -
4380 -
4381 // Redraw any newly exposed areas. -
4382 if (itemCache->allExposed || !itemCache->exposed.isEmpty()) { -
4383 -
4384 //We know that we will modify the pixmap, removing it from the cache -
4385 //will detach the one we have and avoid a deep copy -
4386 if (pixmapFound) -
4387 QPixmapCache::remove(pixmapKey); -
4388 -
4389 // Fit the item's bounding rect into the pixmap's coordinates. -
4390 QTransform itemToPixmap; -
4391 if (fixedCacheSize) { -
4392 const QPointF scale(pixmapSize.width() / brect.width(), pixmapSize.height() / brect.height()); -
4393 itemToPixmap.scale(scale.x(), scale.y()); -
4394 } -
4395 itemToPixmap.translate(-br.x(), -br.y()); -
4396 -
4397 // Generate the item's exposedRect and map its list of expose -
4398 // rects to device coordinates. -
4399 styleOptionTmp = *option; -
4400 QRegion pixmapExposed; -
4401 QRectF exposedRect; -
4402 if (!itemCache->allExposed) { -
4403 for (int i = 0; i < itemCache->exposed.size(); ++i) { -
4404 QRectF r = itemCache->exposed.at(i); -
4405 exposedRect |= r; -
4406 pixmapExposed += itemToPixmap.mapRect(r).toAlignedRect(); -
4407 } -
4408 } else { -
4409 exposedRect = brect; -
4410 } -
4411 styleOptionTmp.exposedRect = exposedRect; -
4412 -
4413 // Render. -
4414 _q_paintIntoCache(&pix, item, pixmapExposed, itemToPixmap, painter->renderHints(), -
4415 &styleOptionTmp, painterStateProtection); -
4416 -
4417 // insert this pixmap into the cache. -
4418 itemCache->key = QPixmapCache::insert(pix); -
4419 -
4420 // Reset expose data. -
4421 itemCache->allExposed = false; -
4422 itemCache->exposed.clear(); -
4423 } -
4424 -
4425 // Redraw the exposed area using the transformed painter. Depending on -
4426 // the hardware, this may be a server-side operation, or an expensive -
4427 // qpixmap-image-transform-pixmap roundtrip. -
4428 if (newPainterOpacity != oldPainterOpacity) { -
4429 painter->setOpacity(newPainterOpacity); -
4430 painter->drawPixmap(br.topLeft(), pix); -
4431 painter->setOpacity(oldPainterOpacity); -
4432 } else { -
4433 painter->drawPixmap(br.topLeft(), pix); -
4434 } -
4435 return; -
4436 } -
4437 -
4438 // Render using device coordinate cache mode. -
4439 if (cacheMode == QGraphicsItem::DeviceCoordinateCache) { -
4440 // Find the item's bounds in device coordinates. -
4441 QRectF deviceBounds = painter->worldTransform().mapRect(brect); -
4442 QRect deviceRect = deviceBounds.toRect().adjusted(-1, -1, 1, 1); -
4443 if (deviceRect.isEmpty()) -
4444 return; -
4445 QRect viewRect = widget ? widget->rect() : QRect(); -
4446 if (widget && !viewRect.intersects(deviceRect)) -
4447 return; -
4448 -
4449 // Resort to direct rendering if the device rect exceeds the -
4450 // (optional) maximum bounds. (QGraphicsSvgItem uses this). -
4451 QSize maximumCacheSize = -
4452 itemd->extra(QGraphicsItemPrivate::ExtraMaxDeviceCoordCacheSize).toSize(); -
4453 if (!maximumCacheSize.isEmpty() -
4454 && (deviceRect.width() > maximumCacheSize.width() -
4455 || deviceRect.height() > maximumCacheSize.height())) { -
4456 _q_paintItem(static_cast<QGraphicsWidget *>(item), painter, option, widget, -
4457 oldPainterOpacity != newPainterOpacity, painterStateProtection); -
4458 return; -
4459 } -
4460 -
4461 // Create or reuse offscreen pixmap, possibly scroll/blit from the old one. -
4462 // If the world transform is rotated we always recreate the cache to avoid -
4463 // wrong blending. -
4464 bool pixModified = false; -
4465 QGraphicsItemCache::DeviceData *deviceData = &itemCache->deviceData[widget]; -
4466 bool invertable = true; -
4467 QTransform diff = deviceData->lastTransform.inverted(&invertable); -
4468 if (invertable) -
4469 diff *= painter->worldTransform(); -
4470 deviceData->lastTransform = painter->worldTransform(); -
4471 bool allowPartialCacheExposure = false; -
4472 bool simpleTransform = invertable && diff.type() <= QTransform::TxTranslate -
4473 && transformIsSimple(painter->worldTransform()); -
4474 if (!simpleTransform) { -
4475 pixModified = true; -
4476 itemCache->allExposed = true; -
4477 itemCache->exposed.clear(); -
4478 deviceData->cacheIndent = QPoint(); -
4479 pix = QPixmap(); -
4480 } else if (!viewRect.isNull()) { -
4481 allowPartialCacheExposure = deviceData->cacheIndent != QPoint(); -
4482 } -
4483 -
4484 // Allow partial cache exposure if the device rect isn't fully contained and -
4485 // deviceRect is 20% taller or wider than the viewRect. -
4486 if (!allowPartialCacheExposure && !viewRect.isNull() && !viewRect.contains(deviceRect)) { -
4487 allowPartialCacheExposure = (viewRect.width() * 1.2 < deviceRect.width()) -
4488 || (viewRect.height() * 1.2 < deviceRect.height()); -
4489 } -
4490 -
4491 QRegion scrollExposure; -
4492 if (allowPartialCacheExposure) { -
4493 // Part of pixmap is drawn. Either device contains viewrect (big -
4494 // item covers whole screen) or parts of device are outside the -
4495 // viewport. In either case the device rect must be the intersect -
4496 // between the two. -
4497 int dx = deviceRect.left() < viewRect.left() ? viewRect.left() - deviceRect.left() : 0; -
4498 int dy = deviceRect.top() < viewRect.top() ? viewRect.top() - deviceRect.top() : 0; -
4499 QPoint newCacheIndent(dx, dy); -
4500 deviceRect &= viewRect; -
4501 -
4502 if (pix.isNull()) { -
4503 deviceData->cacheIndent = QPoint(); -
4504 itemCache->allExposed = true; -
4505 itemCache->exposed.clear(); -
4506 pixModified = true; -
4507 } -
4508 -
4509 // Copy / "scroll" the old pixmap onto the new ole and calculate -
4510 // scrolled exposure. -
4511 if (newCacheIndent != deviceData->cacheIndent || deviceRect.size() != pix.size()) { -
4512 QPoint diff = newCacheIndent - deviceData->cacheIndent; -
4513 QPixmap newPix(deviceRect.size()); -
4514 // ### Investigate removing this fill (test with Plasma and -
4515 // graphicssystem raster). -
4516 newPix.fill(Qt::transparent); -
4517 if (!pix.isNull()) { -
4518 QPainter newPixPainter(&newPix); -
4519 newPixPainter.drawPixmap(-diff, pix); -
4520 newPixPainter.end(); -
4521 } -
4522 QRegion exposed; -
4523 exposed += newPix.rect(); -
4524 if (!pix.isNull()) -
4525 exposed -= QRect(-diff, pix.size()); -
4526 scrollExposure = exposed; -
4527 -
4528 pix = newPix; -
4529 pixModified = true; -
4530 } -
4531 deviceData->cacheIndent = newCacheIndent; -
4532 } else { -
4533 // Full pixmap is drawn. -
4534 deviceData->cacheIndent = QPoint(); -
4535 -
4536 // Auto-adjust the pixmap size. -
4537 if (deviceRect.size() != pix.size()) { -
4538 // exposed needs to cover the whole pixmap -
4539 pix = QPixmap(deviceRect.size()); -
4540 pixModified = true; -
4541 itemCache->allExposed = true; -
4542 itemCache->exposed.clear(); -
4543 } -
4544 } -
4545 -
4546 // Check for newly invalidated areas. -
4547 if (itemCache->allExposed || !itemCache->exposed.isEmpty() || !scrollExposure.isEmpty()) { -
4548 //We know that we will modify the pixmap, removing it from the cache -
4549 //will detach the one we have and avoid a deep copy -
4550 if (pixmapFound) -
4551 QPixmapCache::remove(pixmapKey); -
4552 -
4553 // Construct an item-to-pixmap transform. -
4554 QPointF p = deviceRect.topLeft(); -
4555 QTransform itemToPixmap = painter->worldTransform(); -
4556 if (!p.isNull()) -
4557 itemToPixmap *= QTransform::fromTranslate(-p.x(), -p.y()); -
4558 -
4559 // Map the item's logical expose to pixmap coordinates. -
4560 QRegion pixmapExposed = scrollExposure; -
4561 if (!itemCache->allExposed) { -
4562 const QVector<QRectF> &exposed = itemCache->exposed; -
4563 for (int i = 0; i < exposed.size(); ++i) -
4564 pixmapExposed += itemToPixmap.mapRect(exposed.at(i)).toRect().adjusted(-1, -1, 1, 1); -
4565 } -
4566 -
4567 // Calculate the style option's exposedRect. -
4568 QRectF br; -
4569 if (itemCache->allExposed) { -
4570 br = item->boundingRect(); -
4571 } else { -
4572 const QVector<QRectF> &exposed = itemCache->exposed; -
4573 for (int i = 0; i < exposed.size(); ++i) -
4574 br |= exposed.at(i); -
4575 QTransform pixmapToItem = itemToPixmap.inverted(); -
4576 foreach (const QRect &r, scrollExposure.rects()) -
4577 br |= pixmapToItem.mapRect(r); -
4578 } -
4579 styleOptionTmp = *option; -
4580 styleOptionTmp.exposedRect = br.adjusted(-1, -1, 1, 1); -
4581 -
4582 // Render the exposed areas. -
4583 _q_paintIntoCache(&pix, item, pixmapExposed, itemToPixmap, painter->renderHints(), -
4584 &styleOptionTmp, painterStateProtection); -
4585 -
4586 // Reset expose data. -
4587 pixModified = true; -
4588 itemCache->allExposed = false; -
4589 itemCache->exposed.clear(); -
4590 } -
4591 -
4592 if (pixModified) { -
4593 // Insert this pixmap into the cache. -
4594 deviceData->key = QPixmapCache::insert(pix); -
4595 } -
4596 -
4597 // Redraw the exposed area using an untransformed painter. This -
4598 // effectively becomes a bitblit that does not transform the cache. -
4599 QTransform restoreTransform = painter->worldTransform(); -
4600 painter->setWorldTransform(QTransform()); -
4601 if (newPainterOpacity != oldPainterOpacity) { -
4602 painter->setOpacity(newPainterOpacity); -
4603 painter->drawPixmap(deviceRect.topLeft(), pix); -
4604 painter->setOpacity(oldPainterOpacity); -
4605 } else { -
4606 painter->drawPixmap(deviceRect.topLeft(), pix); -
4607 } -
4608 painter->setWorldTransform(restoreTransform); -
4609 return; -
4610 } -
4611} -
4612 -
4613void QGraphicsScenePrivate::drawItems(QPainter *painter, const QTransform *const viewTransform, -
4614 QRegion *exposedRegion, QWidget *widget) -
4615{ -
4616 // Make sure we don't have unpolished items before we draw. -
4617 if (!unpolishedItems.isEmpty()) -
4618 _q_polishItems(); -
4619 -
4620 updateAll = false; -
4621 QRectF exposedSceneRect; -
4622 if (exposedRegion && indexMethod != QGraphicsScene::NoIndex) { -
4623 exposedSceneRect = exposedRegion->boundingRect().adjusted(-1, -1, 1, 1); -
4624 if (viewTransform) -
4625 exposedSceneRect = viewTransform->inverted().mapRect(exposedSceneRect); -
4626 } -
4627 const QList<QGraphicsItem *> tli = index->estimateTopLevelItems(exposedSceneRect, Qt::AscendingOrder); -
4628 for (int i = 0; i < tli.size(); ++i) -
4629 drawSubtreeRecursive(tli.at(i), painter, viewTransform, exposedRegion, widget); -
4630} -
4631 -
4632void QGraphicsScenePrivate::drawSubtreeRecursive(QGraphicsItem *item, QPainter *painter, -
4633 const QTransform *const viewTransform, -
4634 QRegion *exposedRegion, QWidget *widget, -
4635 qreal parentOpacity, const QTransform *const effectTransform) -
4636{ -
4637 Q_ASSERT(item); -
4638 -
4639 if (!item->d_ptr->visible) -
4640 return; -
4641 -
4642 const bool itemHasContents = !(item->d_ptr->flags & QGraphicsItem::ItemHasNoContents); -
4643 const bool itemHasChildren = !item->d_ptr->children.isEmpty(); -
4644 if (!itemHasContents && !itemHasChildren) -
4645 return; // Item has neither contents nor children!(?) -
4646 -
4647 const qreal opacity = item->d_ptr->combineOpacityFromParent(parentOpacity); -
4648 const bool itemIsFullyTransparent = QGraphicsItemPrivate::isOpacityNull(opacity); -
4649 if (itemIsFullyTransparent && (!itemHasChildren || item->d_ptr->childrenCombineOpacity())) -
4650 return; -
4651 -
4652 QTransform transform(Qt::Uninitialized); -
4653 QTransform *transformPtr = 0; -
4654 bool translateOnlyTransform = false; -
4655#define ENSURE_TRANSFORM_PTR \ -
4656 if (!transformPtr) { \ -
4657 Q_ASSERT(!itemIsUntransformable); \ -
4658 if (viewTransform) { \ -
4659 transform = item->d_ptr->sceneTransform; \ -
4660 transform *= *viewTransform; \ -
4661 transformPtr = &transform; \ -
4662 } else { \ -
4663 transformPtr = &item->d_ptr->sceneTransform; \ -
4664 translateOnlyTransform = item->d_ptr->sceneTransformTranslateOnly; \ -
4665 } \ -
4666 } -
4667 -
4668 // Update the item's scene transform if the item is transformable; -
4669 // otherwise calculate the full transform, -
4670 bool wasDirtyParentSceneTransform = false; -
4671 const bool itemIsUntransformable = item->d_ptr->itemIsUntransformable(); -
4672 if (itemIsUntransformable) { -
4673 transform = item->deviceTransform(viewTransform ? *viewTransform : QTransform()); -
4674 transformPtr = &transform; -
4675 } else if (item->d_ptr->dirtySceneTransform) { -
4676 item->d_ptr->updateSceneTransformFromParent(); -
4677 Q_ASSERT(!item->d_ptr->dirtySceneTransform); -
4678 wasDirtyParentSceneTransform = true; -
4679 } -
4680 -
4681 const bool itemClipsChildrenToShape = (item->d_ptr->flags & QGraphicsItem::ItemClipsChildrenToShape); -
4682 bool drawItem = itemHasContents && !itemIsFullyTransparent; -
4683 if (drawItem) { -
4684 const QRectF brect = adjustedItemEffectiveBoundingRect(item); -
4685 ENSURE_TRANSFORM_PTR -
4686 QRect viewBoundingRect = translateOnlyTransform ? brect.translated(transformPtr->dx(), transformPtr->dy()).toAlignedRect() -
4687 : transformPtr->mapRect(brect).toAlignedRect(); -
4688 viewBoundingRect.adjust(-int(rectAdjust), -int(rectAdjust), rectAdjust, rectAdjust); -
4689 if (widget) -
4690 item->d_ptr->paintedViewBoundingRects.insert(widget, viewBoundingRect); -
4691 drawItem = exposedRegion ? exposedRegion->intersects(viewBoundingRect) -
4692 : !viewBoundingRect.normalized().isEmpty(); -
4693 if (!drawItem) { -
4694 if (!itemHasChildren) -
4695 return; -
4696 if (itemClipsChildrenToShape) { -
4697 if (wasDirtyParentSceneTransform) -
4698 item->d_ptr->invalidateChildrenSceneTransform(); -
4699 return; -
4700 } -
4701 } -
4702 } // else we know for sure this item has children we must process. -
4703 -
4704 if (itemHasChildren && itemClipsChildrenToShape) -
4705 ENSURE_TRANSFORM_PTR; -
4706 -
4707#ifndef QT_NO_GRAPHICSEFFECT -
4708 if (item->d_ptr->graphicsEffect && item->d_ptr->graphicsEffect->isEnabled()) { -
4709 ENSURE_TRANSFORM_PTR; -
4710 QGraphicsItemPaintInfo info(viewTransform, transformPtr, effectTransform, exposedRegion, widget, &styleOptionTmp, -
4711 painter, opacity, wasDirtyParentSceneTransform, itemHasContents && !itemIsFullyTransparent); -
4712 QGraphicsEffectSource *source = item->d_ptr->graphicsEffect->d_func()->source; -
4713 QGraphicsItemEffectSourcePrivate *sourced = static_cast<QGraphicsItemEffectSourcePrivate *> -
4714 (source->d_func()); -
4715 sourced->info = &info; -
4716 const QTransform restoreTransform = painter->worldTransform(); -
4717 if (effectTransform) -
4718 painter->setWorldTransform(*transformPtr * *effectTransform); -
4719 else -
4720 painter->setWorldTransform(*transformPtr); -
4721 painter->setOpacity(opacity); -
4722 -
4723 if (sourced->currentCachedSystem() != Qt::LogicalCoordinates -
4724 && sourced->lastEffectTransform != painter->worldTransform()) -
4725 { -
4726 if (sourced->lastEffectTransform.type() <= QTransform::TxTranslate -
4727 && painter->worldTransform().type() <= QTransform::TxTranslate) -
4728 { -
4729 QRectF sourceRect = sourced->boundingRect(Qt::DeviceCoordinates); -
4730 QRect effectRect = sourced->paddedEffectRect(Qt::DeviceCoordinates, sourced->currentCachedMode(), sourceRect); -
4731 -
4732 sourced->setCachedOffset(effectRect.topLeft()); -
4733 } else { -
4734 sourced->invalidateCache(QGraphicsEffectSourcePrivate::TransformChanged); -
4735 } -
4736 -
4737 sourced->lastEffectTransform = painter->worldTransform(); -
4738 } -
4739 -
4740 item->d_ptr->graphicsEffect->draw(painter); -
4741 painter->setWorldTransform(restoreTransform); -
4742 sourced->info = 0; -
4743 } else -
4744#endif //QT_NO_GRAPHICSEFFECT -
4745 { -
4746 draw(item, painter, viewTransform, transformPtr, exposedRegion, widget, opacity, -
4747 effectTransform, wasDirtyParentSceneTransform, drawItem); -
4748 } -
4749} -
4750 -
4751static inline void setClip(QPainter *painter, QGraphicsItem *item) -
4752{ -
4753 painter->save(); -
4754 QRectF clipRect; -
4755 const QPainterPath clipPath(item->shape()); -
4756 if (QPathClipper::pathToRect(clipPath, &clipRect)) -
4757 painter->setClipRect(clipRect, Qt::IntersectClip); -
4758 else -
4759 painter->setClipPath(clipPath, Qt::IntersectClip); -
4760} -
4761 -
4762static inline void setWorldTransform(QPainter *painter, const QTransform *const transformPtr, -
4763 const QTransform *effectTransform) -
4764{ -
4765 Q_ASSERT(transformPtr); -
4766 if (effectTransform) -
4767 painter->setWorldTransform(*transformPtr * *effectTransform); -
4768 else -
4769 painter->setWorldTransform(*transformPtr); -
4770} -
4771 -
4772void QGraphicsScenePrivate::draw(QGraphicsItem *item, QPainter *painter, const QTransform *const viewTransform, -
4773 const QTransform *const transformPtr, QRegion *exposedRegion, QWidget *widget, -
4774 qreal opacity, const QTransform *effectTransform, -
4775 bool wasDirtyParentSceneTransform, bool drawItem) -
4776{ -
4777 const bool itemIsFullyTransparent = QGraphicsItemPrivate::isOpacityNull(opacity); -
4778 const bool itemClipsChildrenToShape = (item->d_ptr->flags & QGraphicsItem::ItemClipsChildrenToShape); -
4779 const bool itemHasChildren = !item->d_ptr->children.isEmpty(); -
4780 bool setChildClip = itemClipsChildrenToShape; -
4781 bool itemHasChildrenStackedBehind = false; -
4782 -
4783 int i = 0; -
4784 if (itemHasChildren) { -
4785 if (itemClipsChildrenToShape) -
4786 setWorldTransform(painter, transformPtr, effectTransform); -
4787 -
4788 item->d_ptr->ensureSortedChildren(); -
4789 // Items with the 'ItemStacksBehindParent' flag are put in front of the list -
4790 // so all we have to do is to check the first item. -
4791 itemHasChildrenStackedBehind = (item->d_ptr->children.at(0)->d_ptr->flags -
4792 & QGraphicsItem::ItemStacksBehindParent); -
4793 -
4794 if (itemHasChildrenStackedBehind) { -
4795 if (itemClipsChildrenToShape) { -
4796 setClip(painter, item); -
4797 setChildClip = false; -
4798 } -
4799 -
4800 // Draw children behind -
4801 for (i = 0; i < item->d_ptr->children.size(); ++i) { -
4802 QGraphicsItem *child = item->d_ptr->children.at(i); -
4803 if (wasDirtyParentSceneTransform) -
4804 child->d_ptr->dirtySceneTransform = 1; -
4805 if (!(child->d_ptr->flags & QGraphicsItem::ItemStacksBehindParent)) -
4806 break; -
4807 if (itemIsFullyTransparent && !(child->d_ptr->flags & QGraphicsItem::ItemIgnoresParentOpacity)) -
4808 continue; -
4809 drawSubtreeRecursive(child, painter, viewTransform, exposedRegion, widget, opacity, effectTransform); -
4810 } -
4811 } -
4812 } -
4813 -
4814 // Draw item -
4815 if (drawItem) { -
4816 Q_ASSERT(!itemIsFullyTransparent); -
4817 Q_ASSERT(!(item->d_ptr->flags & QGraphicsItem::ItemHasNoContents)); -
4818 Q_ASSERT(transformPtr); -
4819 item->d_ptr->initStyleOption(&styleOptionTmp, *transformPtr, exposedRegion -
4820 ? *exposedRegion : QRegion(), exposedRegion == 0); -
4821 -
4822 const bool itemClipsToShape = item->d_ptr->flags & QGraphicsItem::ItemClipsToShape; -
4823 bool restorePainterClip = false; -
4824 -
4825 if (!itemHasChildren || !itemClipsChildrenToShape) { -
4826 // Item does not have children or clip children to shape. -
4827 setWorldTransform(painter, transformPtr, effectTransform); -
4828 if ((restorePainterClip = itemClipsToShape)) -
4829 setClip(painter, item); -
4830 } else if (itemHasChildrenStackedBehind){ -
4831 // Item clips children to shape and has children stacked behind, which means -
4832 // the painter is already clipped to the item's shape. -
4833 if (itemClipsToShape) { -
4834 // The clip is already correct. Ensure correct world transform. -
4835 setWorldTransform(painter, transformPtr, effectTransform); -
4836 } else { -
4837 // Remove clip (this also ensures correct world transform). -
4838 painter->restore(); -
4839 setChildClip = true; -
4840 } -
4841 } else if (itemClipsToShape) { -
4842 // Item clips children and itself to shape. It does not have hildren stacked -
4843 // behind, which means the clip has not yet been set. We set it now and re-use it -
4844 // for the children. -
4845 setClip(painter, item); -
4846 setChildClip = false; -
4847 } -
4848 -
4849 if (painterStateProtection && !restorePainterClip) -
4850 painter->save(); -
4851 -
4852 painter->setOpacity(opacity); -
4853 if (!item->d_ptr->cacheMode && !item->d_ptr->isWidget) -
4854 item->paint(painter, &styleOptionTmp, widget); -
4855 else -
4856 drawItemHelper(item, painter, &styleOptionTmp, widget, painterStateProtection); -
4857 -
4858 if (painterStateProtection || restorePainterClip) -
4859 painter->restore(); -
4860 -
4861 static int drawRect = qgetenv("QT_DRAW_SCENE_ITEM_RECTS").toInt(); -
4862 if (drawRect) { -
4863 QPen oldPen = painter->pen(); -
4864 QBrush oldBrush = painter->brush(); -
4865 quintptr ptr = reinterpret_cast<quintptr>(item); -
4866 const QColor color = QColor::fromHsv(ptr % 255, 255, 255); -
4867 painter->setPen(color); -
4868 painter->setBrush(Qt::NoBrush); -
4869 painter->drawRect(adjustedItemBoundingRect(item)); -
4870 painter->setPen(oldPen); -
4871 painter->setBrush(oldBrush); -
4872 } -
4873 } -
4874 -
4875 // Draw children in front -
4876 if (itemHasChildren) { -
4877 if (setChildClip) -
4878 setClip(painter, item); -
4879 -
4880 for (; i < item->d_ptr->children.size(); ++i) { -
4881 QGraphicsItem *child = item->d_ptr->children.at(i); -
4882 if (wasDirtyParentSceneTransform) -
4883 child->d_ptr->dirtySceneTransform = 1; -
4884 if (itemIsFullyTransparent && !(child->d_ptr->flags & QGraphicsItem::ItemIgnoresParentOpacity)) -
4885 continue; -
4886 drawSubtreeRecursive(child, painter, viewTransform, exposedRegion, widget, opacity, effectTransform); -
4887 } -
4888 -
4889 // Restore child clip -
4890 if (itemClipsChildrenToShape) -
4891 painter->restore(); -
4892 } -
4893} -
4894 -
4895void QGraphicsScenePrivate::markDirty(QGraphicsItem *item, const QRectF &rect, bool invalidateChildren, -
4896 bool force, bool ignoreOpacity, bool removingItemFromScene, -
4897 bool updateBoundingRect) -
4898{ -
4899 Q_ASSERT(item); -
4900 if (updateAll) -
4901 return; -
4902 -
4903 if (removingItemFromScene && !ignoreOpacity && !item->d_ptr->ignoreOpacity) { -
4904 // If any of the item's ancestors ignore opacity, it means that the opacity -
4905 // was set to 0 (and the update request has not yet been processed). That -
4906 // also means that we have to ignore the opacity for the item itself; otherwise -
4907 // things like: parent->setOpacity(0); scene->removeItem(child) won't work. -
4908 // Note that we only do this when removing items from the scene. In all other -
4909 // cases the ignoreOpacity bit propagates properly in processDirtyItems, but -
4910 // since the item is removed immediately it won't be processed there. -
4911 QGraphicsItem *p = item->d_ptr->parent; -
4912 while (p) { -
4913 if (p->d_ptr->ignoreOpacity) { -
4914 item->d_ptr->ignoreOpacity = true; -
4915 break; -
4916 } -
4917 p = p->d_ptr->parent; -
4918 } -
4919 } -
4920 -
4921 if (item->d_ptr->discardUpdateRequest(/*ignoreVisibleBit=*/force, -
4922 /*ignoreDirtyBit=*/removingItemFromScene || invalidateChildren, -
4923 /*ignoreOpacity=*/ignoreOpacity)) { -
4924 if (item->d_ptr->dirty) { -
4925 // The item is already marked as dirty and will be processed later. However, -
4926 // we have to make sure ignoreVisible and ignoreOpacity are set properly; -
4927 // otherwise things like: item->update(); item->hide() (force is now true) -
4928 // won't work as expected. -
4929 if (force) -
4930 item->d_ptr->ignoreVisible = 1; -
4931 if (ignoreOpacity) -
4932 item->d_ptr->ignoreOpacity = 1; -
4933 } -
4934 return; -
4935 } -
4936 -
4937 const bool fullItemUpdate = rect.isNull(); -
4938 if (!fullItemUpdate && rect.isEmpty()) -
4939 return; -
4940 -
4941 if (!processDirtyItemsEmitted) { -
4942 QMetaMethod method = q_ptr->metaObject()->method(processDirtyItemsIndex); -
4943 method.invoke(q_ptr, Qt::QueuedConnection); -
4944// QMetaObject::invokeMethod(q_ptr, "_q_processDirtyItems", Qt::QueuedConnection); -
4945 processDirtyItemsEmitted = true; -
4946 } -
4947 -
4948 if (removingItemFromScene) { -
4949 // Note that this function can be called from the item's destructor, so -
4950 // do NOT call any virtual functions on it within this block. -
4951 if (isSignalConnected(changedSignalIndex) || views.isEmpty()) { -
4952 // This block of code is kept for compatibility. Since 4.5, by default -
4953 // QGraphicsView does not connect the signal and we use the below -
4954 // method of delivering updates. -
4955 q_func()->update(); -
4956 return; -
4957 } -
4958 -
4959 for (int i = 0; i < views.size(); ++i) { -
4960 QGraphicsViewPrivate *viewPrivate = views.at(i)->d_func(); -
4961 QRect rect = item->d_ptr->paintedViewBoundingRects.value(viewPrivate->viewport); -
4962 rect.translate(viewPrivate->dirtyScrollOffset); -
4963 viewPrivate->updateRect(rect); -
4964 } -
4965 return; -
4966 } -
4967 -
4968 bool hasNoContents = item->d_ptr->flags & QGraphicsItem::ItemHasNoContents; -
4969 if (!hasNoContents) { -
4970 item->d_ptr->dirty = 1; -
4971 if (fullItemUpdate) -
4972 item->d_ptr->fullUpdatePending = 1; -
4973 else if (!item->d_ptr->fullUpdatePending) -
4974 item->d_ptr->needsRepaint |= rect; -
4975 } else if (item->d_ptr->graphicsEffect) { -
4976 invalidateChildren = true; -
4977 } -
4978 -
4979 if (invalidateChildren) { -
4980 item->d_ptr->allChildrenDirty = 1; -
4981 item->d_ptr->dirtyChildren = 1; -
4982 } -
4983 -
4984 if (force) -
4985 item->d_ptr->ignoreVisible = 1; -
4986 if (ignoreOpacity) -
4987 item->d_ptr->ignoreOpacity = 1; -
4988 -
4989 if (!updateBoundingRect) -
4990 item->d_ptr->markParentDirty(); -
4991} -
4992 -
4993static inline bool updateHelper(QGraphicsViewPrivate *view, QGraphicsItemPrivate *item, -
4994 const QRectF &rect, bool itemIsUntransformable) -
4995{ -
4996 Q_ASSERT(view); -
4997 Q_ASSERT(item); -
4998 -
4999 QGraphicsItem *itemq = static_cast<QGraphicsItem *>(item->q_ptr); -
5000 QGraphicsView *viewq = static_cast<QGraphicsView *>(view->q_ptr); -
5001 -
5002 if (itemIsUntransformable) { -
5003 const QTransform xform = itemq->deviceTransform(viewq->viewportTransform()); -
5004 if (!item->hasBoundingRegionGranularity) -
5005 return view->updateRectF(xform.mapRect(rect)); -
5006 return view->updateRegion(rect, xform); -
5007 } -
5008 -
5009 if (item->sceneTransformTranslateOnly && view->identityMatrix) { -
5010 const qreal dx = item->sceneTransform.dx(); -
5011 const qreal dy = item->sceneTransform.dy(); -
5012 QRectF r(rect); -
5013 r.translate(dx - view->horizontalScroll(), dy - view->verticalScroll()); -
5014 return view->updateRectF(r); -
5015 } -
5016 -
5017 if (!viewq->isTransformed()) { -
5018 if (!item->hasBoundingRegionGranularity) -
5019 return view->updateRectF(item->sceneTransform.mapRect(rect)); -
5020 return view->updateRegion(rect, item->sceneTransform); -
5021 } -
5022 -
5023 QTransform xform = item->sceneTransform; -
5024 xform *= viewq->viewportTransform(); -
5025 if (!item->hasBoundingRegionGranularity) -
5026 return view->updateRectF(xform.mapRect(rect)); -
5027 return view->updateRegion(rect, xform); -
5028} -
5029 -
5030void QGraphicsScenePrivate::processDirtyItemsRecursive(QGraphicsItem *item, bool dirtyAncestorContainsChildren, -
5031 qreal parentOpacity) -
5032{ -
5033 Q_Q(QGraphicsScene); -
5034 Q_ASSERT(item); -
5035 Q_ASSERT(!updateAll); -
5036 -
5037 if (!item->d_ptr->dirty && !item->d_ptr->dirtyChildren) { -
5038 resetDirtyItem(item); -
5039 return; -
5040 } -
5041 -
5042 const bool itemIsHidden = !item->d_ptr->ignoreVisible && !item->d_ptr->visible; -
5043 if (itemIsHidden) { -
5044 resetDirtyItem(item, /*recursive=*/true); -
5045 return; -
5046 } -
5047 -
5048 bool itemHasContents = !(item->d_ptr->flags & QGraphicsItem::ItemHasNoContents); -
5049 const bool itemHasChildren = !item->d_ptr->children.isEmpty(); -
5050 if (!itemHasContents) { -
5051 if (!itemHasChildren) { -
5052 resetDirtyItem(item); -
5053 return; // Item has neither contents nor children!(?) -
5054 } -
5055 if (item->d_ptr->graphicsEffect) -
5056 itemHasContents = true; -
5057 } -
5058 -
5059 const qreal opacity = item->d_ptr->combineOpacityFromParent(parentOpacity); -
5060 const bool itemIsFullyTransparent = !item->d_ptr->ignoreOpacity -
5061 && QGraphicsItemPrivate::isOpacityNull(opacity); -
5062 if (itemIsFullyTransparent && (!itemHasChildren || item->d_ptr->childrenCombineOpacity())) { -
5063 resetDirtyItem(item, /*recursive=*/itemHasChildren); -
5064 return; -
5065 } -
5066 -
5067 bool wasDirtyParentSceneTransform = item->d_ptr->dirtySceneTransform; -
5068 const bool itemIsUntransformable = item->d_ptr->itemIsUntransformable(); -
5069 if (wasDirtyParentSceneTransform && !itemIsUntransformable) { -
5070 item->d_ptr->updateSceneTransformFromParent(); -
5071 Q_ASSERT(!item->d_ptr->dirtySceneTransform); -
5072 } -
5073 -
5074 const bool wasDirtyParentViewBoundingRects = item->d_ptr->paintedViewBoundingRectsNeedRepaint; -
5075 if (itemIsFullyTransparent || !itemHasContents || dirtyAncestorContainsChildren) { -
5076 // Make sure we don't process invisible items or items with no content. -
5077 item->d_ptr->dirty = 0; -
5078 item->d_ptr->fullUpdatePending = 0; -
5079 // Might have a dirty view bounding rect otherwise. -
5080 if (itemIsFullyTransparent || !itemHasContents) -
5081 item->d_ptr->paintedViewBoundingRectsNeedRepaint = 0; -
5082 } -
5083 -
5084 if (!hasSceneRect && item->d_ptr->geometryChanged && item->d_ptr->visible) { -
5085 // Update growingItemsBoundingRect. -
5086 if (item->d_ptr->sceneTransformTranslateOnly) { -
5087 growingItemsBoundingRect |= item->boundingRect().translated(item->d_ptr->sceneTransform.dx(), -
5088 item->d_ptr->sceneTransform.dy()); -
5089 } else { -
5090 growingItemsBoundingRect |= item->d_ptr->sceneTransform.mapRect(item->boundingRect()); -
5091 } -
5092 } -
5093 -
5094 // Process item. -
5095 if (item->d_ptr->dirty || item->d_ptr->paintedViewBoundingRectsNeedRepaint) { -
5096 const bool useCompatUpdate = views.isEmpty() || isSignalConnected(changedSignalIndex); -
5097 const QRectF itemBoundingRect = adjustedItemEffectiveBoundingRect(item); -
5098 -
5099 if (useCompatUpdate && !itemIsUntransformable && qFuzzyIsNull(item->boundingRegionGranularity())) { -
5100 // This block of code is kept for compatibility. Since 4.5, by default -
5101 // QGraphicsView does not connect the signal and we use the below -
5102 // method of delivering updates. -
5103 if (item->d_ptr->sceneTransformTranslateOnly) { -
5104 q->update(itemBoundingRect.translated(item->d_ptr->sceneTransform.dx(), -
5105 item->d_ptr->sceneTransform.dy())); -
5106 } else { -
5107 QRectF rect = item->d_ptr->sceneTransform.mapRect(itemBoundingRect); -
5108 if (!rect.isEmpty()) -
5109 q->update(rect); -
5110 } -
5111 } else { -
5112 QRectF dirtyRect; -
5113 bool uninitializedDirtyRect = true; -
5114 -
5115 for (int j = 0; j < views.size(); ++j) { -
5116 QGraphicsView *view = views.at(j); -
5117 QGraphicsViewPrivate *viewPrivate = view->d_func(); -
5118 QRect &paintedViewBoundingRect = item->d_ptr->paintedViewBoundingRects[viewPrivate->viewport]; -
5119 if (viewPrivate->fullUpdatePending -
5120 || viewPrivate->viewportUpdateMode == QGraphicsView::NoViewportUpdate) { -
5121 // Okay, if we have a full update pending or no viewport update, this item's -
5122 // paintedViewBoundingRect will be updated correctly in the next paintEvent if -
5123 // it is inside the viewport, but for now we can pretend that it is outside. -
5124 paintedViewBoundingRect = QRect(-1, -1, -1, -1); -
5125 continue; -
5126 } -
5127 -
5128 if (item->d_ptr->paintedViewBoundingRectsNeedRepaint) { -
5129 paintedViewBoundingRect.translate(viewPrivate->dirtyScrollOffset); -
5130 if (!viewPrivate->updateRect(paintedViewBoundingRect)) -
5131 paintedViewBoundingRect = QRect(-1, -1, -1, -1); // Outside viewport. -
5132 } -
5133 -
5134 if (!item->d_ptr->dirty) -
5135 continue; -
5136 -
5137 if (!item->d_ptr->paintedViewBoundingRectsNeedRepaint -
5138 && paintedViewBoundingRect.x() == -1 && paintedViewBoundingRect.y() == -1 -
5139 && paintedViewBoundingRect.width() == -1 && paintedViewBoundingRect.height() == -1) { -
5140 continue; // Outside viewport. -
5141 } -
5142 -
5143 if (uninitializedDirtyRect) { -
5144 dirtyRect = itemBoundingRect; -
5145 if (!item->d_ptr->fullUpdatePending) { -
5146 _q_adjustRect(&item->d_ptr->needsRepaint); -
5147 dirtyRect &= item->d_ptr->needsRepaint; -
5148 } -
5149 uninitializedDirtyRect = false; -
5150 } -
5151 -
5152 if (dirtyRect.isEmpty()) -
5153 continue; // Discard updates outside the bounding rect. -
5154 -
5155 if (!updateHelper(viewPrivate, item->d_ptr.data(), dirtyRect, itemIsUntransformable) -
5156 && item->d_ptr->paintedViewBoundingRectsNeedRepaint) { -
5157 paintedViewBoundingRect = QRect(-1, -1, -1, -1); // Outside viewport. -
5158 } -
5159 } -
5160 } -
5161 } -
5162 -
5163 // Process children. -
5164 if (itemHasChildren && item->d_ptr->dirtyChildren) { -
5165 const bool itemClipsChildrenToShape = item->d_ptr->flags & QGraphicsItem::ItemClipsChildrenToShape; -
5166 // Items with no content are threated as 'dummy' items which means they are never drawn and -
5167 // 'processed', so the painted view bounding rect is never up-to-date. This means that whenever -
5168 // such an item changes geometry, its children have to take care of the update regardless -
5169 // of whether the item clips children to shape or not. -
5170 const bool bypassUpdateClip = !itemHasContents && wasDirtyParentViewBoundingRects; -
5171 if (itemClipsChildrenToShape && !bypassUpdateClip) { -
5172 // Make sure child updates are clipped to the item's bounding rect. -
5173 for (int i = 0; i < views.size(); ++i) -
5174 views.at(i)->d_func()->setUpdateClip(item); -
5175 } -
5176 if (!dirtyAncestorContainsChildren) { -
5177 dirtyAncestorContainsChildren = item->d_ptr->fullUpdatePending -
5178 && itemClipsChildrenToShape; -
5179 } -
5180 const bool allChildrenDirty = item->d_ptr->allChildrenDirty; -
5181 const bool parentIgnoresVisible = item->d_ptr->ignoreVisible; -
5182 const bool parentIgnoresOpacity = item->d_ptr->ignoreOpacity; -
5183 for (int i = 0; i < item->d_ptr->children.size(); ++i) { -
5184 QGraphicsItem *child = item->d_ptr->children.at(i); -
5185 if (wasDirtyParentSceneTransform) -
5186 child->d_ptr->dirtySceneTransform = 1; -
5187 if (wasDirtyParentViewBoundingRects) -
5188 child->d_ptr->paintedViewBoundingRectsNeedRepaint = 1; -
5189 if (parentIgnoresVisible) -
5190 child->d_ptr->ignoreVisible = 1; -
5191 if (parentIgnoresOpacity) -
5192 child->d_ptr->ignoreOpacity = 1; -
5193 if (allChildrenDirty) { -
5194 child->d_ptr->dirty = 1; -
5195 child->d_ptr->fullUpdatePending = 1; -
5196 child->d_ptr->dirtyChildren = 1; -
5197 child->d_ptr->allChildrenDirty = 1; -
5198 } -
5199 processDirtyItemsRecursive(child, dirtyAncestorContainsChildren, opacity); -
5200 } -
5201 -
5202 if (itemClipsChildrenToShape) { -
5203 // Reset updateClip. -
5204 for (int i = 0; i < views.size(); ++i) -
5205 views.at(i)->d_func()->setUpdateClip(0); -
5206 } -
5207 } else if (wasDirtyParentSceneTransform) { -
5208 item->d_ptr->invalidateChildrenSceneTransform(); -
5209 } -
5210 -
5211 resetDirtyItem(item); -
5212} -
5213 -
5214/*! -
5215 \obsolete -
5216 -
5217 Paints the given \a items using the provided \a painter, after the -
5218 background has been drawn, and before the foreground has been -
5219 drawn. All painting is done in \e scene coordinates. Before -
5220 drawing each item, the painter must be transformed using -
5221 QGraphicsItem::sceneTransform(). -
5222 -
5223 The \a options parameter is the list of style option objects for -
5224 each item in \a items. The \a numItems parameter is the number of -
5225 items in \a items and options in \a options. The \a widget -
5226 parameter is optional; if specified, it should point to the widget -
5227 that is being painted on. -
5228 -
5229 The default implementation prepares the painter matrix, and calls -
5230 QGraphicsItem::paint() on all items. Reimplement this function to -
5231 provide custom painting of all items for the scene; gaining -
5232 complete control over how each item is drawn. In some cases this -
5233 can increase drawing performance significantly. -
5234 -
5235 Example: -
5236 -
5237 \snippet graphicssceneadditemsnippet.cpp 0 -
5238 -
5239 Since Qt 4.6, this function is not called anymore unless -
5240 the QGraphicsView::IndirectPainting flag is given as an Optimization -
5241 flag. -
5242 -
5243 \sa drawBackground(), drawForeground() -
5244*/ -
5245void QGraphicsScene::drawItems(QPainter *painter, -
5246 int numItems, -
5247 QGraphicsItem *items[], -
5248 const QStyleOptionGraphicsItem options[], QWidget *widget) -
5249{ -
5250 Q_D(QGraphicsScene); -
5251 // Make sure we don't have unpolished items before we draw. -
5252 if (!d->unpolishedItems.isEmpty()) -
5253 d->_q_polishItems(); -
5254 -
5255 const qreal opacity = painter->opacity(); -
5256 QTransform viewTransform = painter->worldTransform(); -
5257 Q_UNUSED(options); -
5258 -
5259 // Determine view, expose and flags. -
5260 QGraphicsView *view = widget ? qobject_cast<QGraphicsView *>(widget->parentWidget()) : 0; -
5261 QRegion *expose = 0; -
5262 const quint32 oldRectAdjust = d->rectAdjust; -
5263 if (view) { -
5264 d->updateAll = false; -
5265 expose = &view->d_func()->exposedRegion; -
5266 if (view->d_func()->optimizationFlags & QGraphicsView::DontAdjustForAntialiasing) -
5267 d->rectAdjust = 1; -
5268 else -
5269 d->rectAdjust = 2; -
5270 } -
5271 -
5272 // Find all toplevels, they are already sorted. -
5273 QList<QGraphicsItem *> topLevelItems; -
5274 for (int i = 0; i < numItems; ++i) { -
5275 QGraphicsItem *item = items[i]->topLevelItem(); -
5276 if (!item->d_ptr->itemDiscovered) { -
5277 topLevelItems << item; -
5278 item->d_ptr->itemDiscovered = 1; -
5279 d->drawSubtreeRecursive(item, painter, &viewTransform, expose, widget); -
5280 } -
5281 } -
5282 -
5283 d->rectAdjust = oldRectAdjust; -
5284 // Reset discovery bits. -
5285 for (int i = 0; i < topLevelItems.size(); ++i) -
5286 topLevelItems.at(i)->d_ptr->itemDiscovered = 0; -
5287 -
5288 painter->setWorldTransform(viewTransform); -
5289 painter->setOpacity(opacity); -
5290} -
5291 -
5292/*! -
5293 \since 4.4 -
5294 -
5295 Finds a new widget to give the keyboard focus to, as appropriate for Tab -
5296 and Shift+Tab, and returns true if it can find a new widget, or false if -
5297 it cannot. If \a next is true, this function searches forward; if \a next -
5298 is false, it searches backward. -
5299 -
5300 You can reimplement this function in a subclass of QGraphicsScene to -
5301 provide fine-grained control over how tab focus passes inside your -
5302 scene. The default implementation is based on the tab focus chain defined -
5303 by QGraphicsWidget::setTabOrder(). -
5304*/ -
5305bool QGraphicsScene::focusNextPrevChild(bool next) -
5306{ -
5307 Q_D(QGraphicsScene); -
5308 -
5309 QGraphicsItem *item = focusItem(); -
5310 if (item && !item->isWidget()) { -
5311 // Tab out of the scene. -
5312 return false; -
5313 } -
5314 if (!item) { -
5315 if (d->lastFocusItem && !d->lastFocusItem->isWidget()) { -
5316 // Restore focus to the last focusable non-widget item that had -
5317 // focus. -
5318 setFocusItem(d->lastFocusItem, next ? Qt::TabFocusReason : Qt::BacktabFocusReason); -
5319 return true; -
5320 } -
5321 } -
5322 if (!d->tabFocusFirst) { -
5323 // No widgets... -
5324 return false; -
5325 } -
5326 -
5327 // The item must be a widget. -
5328 QGraphicsWidget *widget = 0; -
5329 if (!item) { -
5330 widget = next ? d->tabFocusFirst : d->tabFocusFirst->d_func()->focusPrev; -
5331 } else { -
5332 QGraphicsWidget *test = static_cast<QGraphicsWidget *>(item); -
5333 widget = next ? test->d_func()->focusNext : test->d_func()->focusPrev; -
5334 if ((next && widget == d->tabFocusFirst) || (!next && widget == d->tabFocusFirst->d_func()->focusPrev)) -
5335 return false; -
5336 } -
5337 QGraphicsWidget *widgetThatHadFocus = widget; -
5338 -
5339 // Run around the focus chain until we find a widget that can take tab focus. -
5340 do { -
5341 if (widget->flags() & QGraphicsItem::ItemIsFocusable -
5342 && widget->isEnabled() && widget->isVisibleTo(0) -
5343 && (widget->focusPolicy() & Qt::TabFocus) -
5344 && (!item || !item->isPanel() || item->isAncestorOf(widget)) -
5345 ) { -
5346 setFocusItem(widget, next ? Qt::TabFocusReason : Qt::BacktabFocusReason); -
5347 return true; -
5348 } -
5349 widget = next ? widget->d_func()->focusNext : widget->d_func()->focusPrev; -
5350 if ((next && widget == d->tabFocusFirst) || (!next && widget == d->tabFocusFirst->d_func()->focusPrev)) -
5351 return false; -
5352 } while (widget != widgetThatHadFocus); -
5353 -
5354 return false; -
5355} -
5356 -
5357/*! -
5358 \fn QGraphicsScene::changed(const QList<QRectF> &region) -
5359 -
5360 This signal is emitted by QGraphicsScene when control reaches the -
5361 event loop, if the scene content changes. The \a region parameter -
5362 contains a list of scene rectangles that indicate the area that -
5363 has been changed. -
5364 -
5365 \sa QGraphicsView::updateScene() -
5366*/ -
5367 -
5368/*! -
5369 \fn QGraphicsScene::sceneRectChanged(const QRectF &rect) -
5370 -
5371 This signal is emitted by QGraphicsScene whenever the scene rect changes. -
5372 The \a rect parameter is the new scene rectangle. -
5373 -
5374 \sa QGraphicsView::updateSceneRect() -
5375*/ -
5376 -
5377/*! -
5378 \fn QGraphicsScene::selectionChanged() -
5379 \since 4.3 -
5380 -
5381 This signal is emitted by QGraphicsScene whenever the selection -
5382 changes. You can call selectedItems() to get the new list of selected -
5383 items. -
5384 -
5385 The selection changes whenever an item is selected or unselected, a -
5386 selection area is set, cleared or otherwise changed, if a preselected item -
5387 is added to the scene, or if a selected item is removed from the scene. -
5388 -
5389 QGraphicsScene emits this signal only once for group selection operations. -
5390 For example, if you set a selection area, select or unselect a -
5391 QGraphicsItemGroup, or if you add or remove from the scene a parent item -
5392 that contains several selected items, selectionChanged() is emitted only -
5393 once after the operation has completed (instead of once for each item). -
5394 -
5395 \sa setSelectionArea(), selectedItems(), QGraphicsItem::setSelected() -
5396*/ -
5397 -
5398/*! -
5399 \since 4.4 -
5400 -
5401 Returns the scene's style, or the same as QApplication::style() if the -
5402 scene has not been explicitly assigned a style. -
5403 -
5404 \sa setStyle() -
5405*/ -
5406QStyle *QGraphicsScene::style() const -
5407{ -
5408 Q_D(const QGraphicsScene); -
5409 // ### This function, and the use of styles in general, is non-reentrant. -
5410 return d->style ? d->style : QApplication::style(); -
5411} -
5412 -
5413/*! -
5414 \since 4.4 -
5415 -
5416 Sets or replaces the style of the scene to \a style, and reparents the -
5417 style to this scene. Any previously assigned style is deleted. The scene's -
5418 style defaults to QApplication::style(), and serves as the default for all -
5419 QGraphicsWidget items in the scene. -
5420 -
5421 Changing the style, either directly by calling this function, or -
5422 indirectly by calling QApplication::setStyle(), will automatically update -
5423 the style for all widgets in the scene that do not have a style explicitly -
5424 assigned to them. -
5425 -
5426 If \a style is 0, QGraphicsScene will revert to QApplication::style(). -
5427 -
5428 \sa style() -
5429*/ -
5430void QGraphicsScene::setStyle(QStyle *style) -
5431{ -
5432 Q_D(QGraphicsScene); -
5433 // ### This function, and the use of styles in general, is non-reentrant. -
5434 if (style == d->style) -
5435 return; -
5436 -
5437 // Delete the old style, -
5438 delete d->style; -
5439 if ((d->style = style)) -
5440 d->style->setParent(this); -
5441 -
5442 // Notify the scene. -
5443 QEvent event(QEvent::StyleChange); -
5444 QApplication::sendEvent(this, &event); -
5445 -
5446 // Notify all widgets that don't have a style explicitly set. -
5447 foreach (QGraphicsItem *item, items()) { -
5448 if (item->isWidget()) { -
5449 QGraphicsWidget *widget = static_cast<QGraphicsWidget *>(item); -
5450 if (!widget->testAttribute(Qt::WA_SetStyle)) -
5451 QApplication::sendEvent(widget, &event); -
5452 } -
5453 } -
5454} -
5455 -
5456/*! -
5457 \property QGraphicsScene::font -
5458 \since 4.4 -
5459 \brief the scene's default font -
5460 -
5461 This property provides the scene's font. The scene font defaults to, -
5462 and resolves all its entries from, QApplication::font. -
5463 -
5464 If the scene's font changes, either directly through setFont() or -
5465 indirectly when the application font changes, QGraphicsScene first -
5466 sends itself a \l{QEvent::FontChange}{FontChange} event, and it then -
5467 sends \l{QEvent::FontChange}{FontChange} events to all top-level -
5468 widget items in the scene. These items respond by resolving their own -
5469 fonts to the scene, and they then notify their children, who again -
5470 notify their children, and so on, until all widget items have updated -
5471 their fonts. -
5472 -
5473 Changing the scene font, (directly or indirectly through -
5474 QApplication::setFont(),) automatically schedules a redraw the entire -
5475 scene. -
5476 -
5477 \sa QWidget::font, QApplication::setFont(), palette, style() -
5478*/ -
5479QFont QGraphicsScene::font() const -
5480{ -
5481 Q_D(const QGraphicsScene); -
5482 return d->font; -
5483} -
5484void QGraphicsScene::setFont(const QFont &font) -
5485{ -
5486 Q_D(QGraphicsScene); -
5487 QFont naturalFont = QApplication::font(); -
5488 naturalFont.resolve(0); -
5489 QFont resolvedFont = font.resolve(naturalFont); -
5490 d->setFont_helper(resolvedFont); -
5491} -
5492 -
5493/*! -
5494 \property QGraphicsScene::palette -
5495 \since 4.4 -
5496 \brief the scene's default palette -
5497 -
5498 This property provides the scene's palette. The scene palette defaults to, -
5499 and resolves all its entries from, QApplication::palette. -
5500 -
5501 If the scene's palette changes, either directly through setPalette() or -
5502 indirectly when the application palette changes, QGraphicsScene first -
5503 sends itself a \l{QEvent::PaletteChange}{PaletteChange} event, and it then -
5504 sends \l{QEvent::PaletteChange}{PaletteChange} events to all top-level -
5505 widget items in the scene. These items respond by resolving their own -
5506 palettes to the scene, and they then notify their children, who again -
5507 notify their children, and so on, until all widget items have updated -
5508 their palettes. -
5509 -
5510 Changing the scene palette, (directly or indirectly through -
5511 QApplication::setPalette(),) automatically schedules a redraw the entire -
5512 scene. -
5513 -
5514 \sa QWidget::palette, QApplication::setPalette(), font, style() -
5515*/ -
5516QPalette QGraphicsScene::palette() const -
5517{ -
5518 Q_D(const QGraphicsScene); -
5519 return d->palette; -
5520} -
5521void QGraphicsScene::setPalette(const QPalette &palette) -
5522{ -
5523 Q_D(QGraphicsScene); -
5524 QPalette naturalPalette = QApplication::palette(); -
5525 naturalPalette.resolve(0); -
5526 QPalette resolvedPalette = palette.resolve(naturalPalette); -
5527 d->setPalette_helper(resolvedPalette); -
5528} -
5529 -
5530/*! -
5531 \since 4.6 -
5532 -
5533 Returns true if the scene is active (e.g., it's viewed by -
5534 at least one QGraphicsView that is active); otherwise returns false. -
5535 -
5536 \sa QGraphicsItem::isActive(), QWidget::isActiveWindow() -
5537*/ -
5538bool QGraphicsScene::isActive() const -
5539{ -
5540 Q_D(const QGraphicsScene); -
5541 return d->activationRefCount > 0; -
5542} -
5543 -
5544/*! -
5545 \since 4.6 -
5546 Returns the current active panel, or 0 if no panel is currently active. -
5547 -
5548 \sa QGraphicsScene::setActivePanel() -
5549*/ -
5550QGraphicsItem *QGraphicsScene::activePanel() const -
5551{ -
5552 Q_D(const QGraphicsScene); -
5553 return d->activePanel; -
5554} -
5555 -
5556/*! -
5557 \since 4.6 -
5558 Activates \a item, which must be an item in this scene. You -
5559 can also pass 0 for \a item, in which case QGraphicsScene will -
5560 deactivate any currently active panel. -
5561 -
5562 If the scene is currently inactive, \a item remains inactive until the -
5563 scene becomes active (or, ir \a item is 0, no item will be activated). -
5564 -
5565 \sa activePanel(), isActive(), QGraphicsItem::isActive() -
5566*/ -
5567void QGraphicsScene::setActivePanel(QGraphicsItem *item) -
5568{ -
5569 Q_D(QGraphicsScene); -
5570 d->setActivePanelHelper(item, false); -
5571} -
5572 -
5573/*! -
5574 \since 4.4 -
5575 -
5576 Returns the current active window, or 0 if no window is currently -
5577 active. -
5578 -
5579 \sa QGraphicsScene::setActiveWindow() -
5580*/ -
5581QGraphicsWidget *QGraphicsScene::activeWindow() const -
5582{ -
5583 Q_D(const QGraphicsScene); -
5584 if (d->activePanel && d->activePanel->isWindow()) -
5585 return static_cast<QGraphicsWidget *>(d->activePanel); -
5586 return 0; -
5587} -
5588 -
5589/*! -
5590 \since 4.4 -
5591 Activates \a widget, which must be a widget in this scene. You can also -
5592 pass 0 for \a widget, in which case QGraphicsScene will deactivate any -
5593 currently active window. -
5594 -
5595 \sa activeWindow(), QGraphicsWidget::isActiveWindow() -
5596*/ -
5597void QGraphicsScene::setActiveWindow(QGraphicsWidget *widget) -
5598{ -
5599 if (widget && widget->scene() != this) { -
5600 qWarning("QGraphicsScene::setActiveWindow: widget %p must be part of this scene", -
5601 widget); -
5602 return; -
5603 } -
5604 -
5605 // Activate the widget's panel (all windows are panels). -
5606 QGraphicsItem *panel = widget ? widget->panel() : 0; -
5607 setActivePanel(panel); -
5608 -
5609 // Raise -
5610 if (panel) { -
5611 QList<QGraphicsItem *> siblingWindows; -
5612 QGraphicsItem *parent = panel->parentItem(); -
5613 // Raise ### inefficient for toplevels -
5614 foreach (QGraphicsItem *sibling, parent ? parent->childItems() : items()) { -
5615 if (sibling != panel && sibling->isWindow()) -
5616 siblingWindows << sibling; -
5617 } -
5618 -
5619 // Find the highest z value. -
5620 qreal z = panel->zValue(); -
5621 for (int i = 0; i < siblingWindows.size(); ++i) -
5622 z = qMax(z, siblingWindows.at(i)->zValue()); -
5623 -
5624 // This will probably never overflow. -
5625 const qreal litt = qreal(0.001); -
5626 panel->setZValue(z + litt); -
5627 } -
5628} -
5629 -
5630/*! -
5631 \since 4.6 -
5632 -
5633 Sends event \a event to item \a item through possible event filters. -
5634 -
5635 The event is sent only if the item is enabled. -
5636 -
5637 Returns \c false if the event was filtered or if the item is disabled. -
5638 Otherwise returns the value that was returned from the event handler. -
5639 -
5640 \sa QGraphicsItem::sceneEvent(), QGraphicsItem::sceneEventFilter() -
5641*/ -
5642bool QGraphicsScene::sendEvent(QGraphicsItem *item, QEvent *event) -
5643{ -
5644 Q_D(QGraphicsScene); -
5645 if (!item) { -
5646 qWarning("QGraphicsScene::sendEvent: cannot send event to a null item"); -
5647 return false; -
5648 } -
5649 if (item->scene() != this) { -
5650 qWarning("QGraphicsScene::sendEvent: item %p's scene (%p)" -
5651 " is different from this scene (%p)", -
5652 item, item->scene(), this); -
5653 return false; -
5654 } -
5655 return d->sendEvent(item, event); -
5656} -
5657 -
5658void QGraphicsScenePrivate::addView(QGraphicsView *view) -
5659{ -
5660 views << view; -
5661#ifndef QT_NO_GESTURES -
5662 foreach (Qt::GestureType gesture, grabbedGestures.keys()) -
5663 view->viewport()->grabGesture(gesture); -
5664#endif -
5665} -
5666 -
5667void QGraphicsScenePrivate::removeView(QGraphicsView *view) -
5668{ -
5669 views.removeAll(view); -
5670} -
5671 -
5672void QGraphicsScenePrivate::updateTouchPointsForItem(QGraphicsItem *item, QTouchEvent *touchEvent) -
5673{ -
5674 QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints(); -
5675 for (int i = 0; i < touchPoints.count(); ++i) { -
5676 QTouchEvent::TouchPoint &touchPoint = touchPoints[i]; -
5677 touchPoint.setRect(item->mapFromScene(touchPoint.sceneRect()).boundingRect()); -
5678 touchPoint.setStartPos(item->d_ptr->genericMapFromScene(touchPoint.startScenePos(), static_cast<QWidget *>(touchEvent->target()))); -
5679 touchPoint.setLastPos(item->d_ptr->genericMapFromScene(touchPoint.lastScenePos(), static_cast<QWidget *>(touchEvent->target()))); -
5680 } -
5681 touchEvent->setTouchPoints(touchPoints); -
5682} -
5683 -
5684int QGraphicsScenePrivate::findClosestTouchPointId(const QPointF &scenePos) -
5685{ -
5686 int closestTouchPointId = -1; -
5687 qreal closestDistance = qreal(0.); -
5688 foreach (const QTouchEvent::TouchPoint &touchPoint, sceneCurrentTouchPoints) { -
5689 qreal distance = QLineF(scenePos, touchPoint.scenePos()).length(); -
5690 if (closestTouchPointId == -1|| distance < closestDistance) { -
5691 closestTouchPointId = touchPoint.id(); -
5692 closestDistance = distance; -
5693 } -
5694 } -
5695 return closestTouchPointId; -
5696} -
5697 -
5698void QGraphicsScenePrivate::touchEventHandler(QTouchEvent *sceneTouchEvent) -
5699{ -
5700 typedef QPair<Qt::TouchPointStates, QList<QTouchEvent::TouchPoint> > StatesAndTouchPoints; -
5701 QHash<QGraphicsItem *, StatesAndTouchPoints> itemsNeedingEvents; -
5702 -
5703 for (int i = 0; i < sceneTouchEvent->touchPoints().count(); ++i) { -
5704 const QTouchEvent::TouchPoint &touchPoint = sceneTouchEvent->touchPoints().at(i); -
5705 -
5706 // update state -
5707 QGraphicsItem *item = 0; -
5708 if (touchPoint.state() == Qt::TouchPointPressed) { -
5709 if (sceneTouchEvent->device()->type() == QTouchDevice::TouchPad) { -
5710 // on touch-pad devices, send all touch points to the same item -
5711 item = itemForTouchPointId.isEmpty() -
5712 ? 0 -
5713 : itemForTouchPointId.constBegin().value(); -
5714 } -
5715 -
5716 if (!item) { -
5717 // determine which item this touch point will go to -
5718 cachedItemsUnderMouse = itemsAtPosition(touchPoint.screenPos().toPoint(), -
5719 touchPoint.scenePos(), -
5720 static_cast<QWidget *>(sceneTouchEvent->target())); -
5721 item = cachedItemsUnderMouse.isEmpty() ? 0 : cachedItemsUnderMouse.first(); -
5722 } -
5723 -
5724 if (sceneTouchEvent->device()->type() == QTouchDevice::TouchScreen) { -
5725 // on touch-screens, combine this touch point with the closest one we find -
5726 int closestTouchPointId = findClosestTouchPointId(touchPoint.scenePos()); -
5727 QGraphicsItem *closestItem = itemForTouchPointId.value(closestTouchPointId); -
5728 if (!item || (closestItem && cachedItemsUnderMouse.contains(closestItem))) -
5729 item = closestItem; -
5730 } -
5731 if (!item) -
5732 continue; -
5733 -
5734 itemForTouchPointId.insert(touchPoint.id(), item); -
5735 sceneCurrentTouchPoints.insert(touchPoint.id(), touchPoint); -
5736 } else if (touchPoint.state() == Qt::TouchPointReleased) { -
5737 item = itemForTouchPointId.take(touchPoint.id()); -
5738 if (!item) -
5739 continue; -
5740 -
5741 sceneCurrentTouchPoints.remove(touchPoint.id()); -
5742 } else { -
5743 item = itemForTouchPointId.value(touchPoint.id()); -
5744 if (!item) -
5745 continue; -
5746 Q_ASSERT(sceneCurrentTouchPoints.contains(touchPoint.id())); -
5747 sceneCurrentTouchPoints[touchPoint.id()] = touchPoint; -
5748 } -
5749 -
5750 StatesAndTouchPoints &statesAndTouchPoints = itemsNeedingEvents[item]; -
5751 statesAndTouchPoints.first |= touchPoint.state(); -
5752 statesAndTouchPoints.second.append(touchPoint); -
5753 } -
5754 -
5755 if (itemsNeedingEvents.isEmpty()) { -
5756 sceneTouchEvent->accept(); -
5757 return; -
5758 } -
5759 -
5760 bool ignoreSceneTouchEvent = true; -
5761 QHash<QGraphicsItem *, StatesAndTouchPoints>::ConstIterator it = itemsNeedingEvents.constBegin(); -
5762 const QHash<QGraphicsItem *, StatesAndTouchPoints>::ConstIterator end = itemsNeedingEvents.constEnd(); -
5763 for (; it != end; ++it) { -
5764 QGraphicsItem *item = it.key(); -
5765 -
5766 (void) item->isBlockedByModalPanel(&item); -
5767 -
5768 // determine event type from the state mask -
5769 QEvent::Type eventType; -
5770 switch (it.value().first) { -
5771 case Qt::TouchPointPressed: -
5772 // all touch points have pressed state -
5773 eventType = QEvent::TouchBegin; -
5774 break; -
5775 case Qt::TouchPointReleased: -
5776 // all touch points have released state -
5777 eventType = QEvent::TouchEnd; -
5778 break; -
5779 case Qt::TouchPointStationary: -
5780 // don't send the event if nothing changed -
5781 continue; -
5782 default: -
5783 // all other combinations -
5784 eventType = QEvent::TouchUpdate; -
5785 break; -
5786 } -
5787 -
5788 QTouchEvent touchEvent(eventType); -
5789 touchEvent.setWindow(sceneTouchEvent->window()); -
5790 touchEvent.setTarget(sceneTouchEvent->target()); -
5791 touchEvent.setDevice(sceneTouchEvent->device()); -
5792 touchEvent.setModifiers(sceneTouchEvent->modifiers()); -
5793 touchEvent.setTouchPointStates(it.value().first); -
5794 touchEvent.setTouchPoints(it.value().second); -
5795 touchEvent.setTimestamp(sceneTouchEvent->timestamp()); -
5796 -
5797 switch (touchEvent.type()) { -
5798 case QEvent::TouchBegin: -
5799 { -
5800 // if the TouchBegin handler recurses, we assume that means the event -
5801 // has been implicitly accepted and continue to send touch events -
5802 item->d_ptr->acceptedTouchBeginEvent = true; -
5803 bool res = sendTouchBeginEvent(item, &touchEvent) -
5804 && touchEvent.isAccepted(); -
5805 if (!res) { -
5806 // forget about these touch points, we didn't handle them -
5807 for (int i = 0; i < touchEvent.touchPoints().count(); ++i) { -
5808 const QTouchEvent::TouchPoint &touchPoint = touchEvent.touchPoints().at(i); -
5809 itemForTouchPointId.remove(touchPoint.id()); -
5810 sceneCurrentTouchPoints.remove(touchPoint.id()); -
5811 } -
5812 ignoreSceneTouchEvent = false; -
5813 } -
5814 break; -
5815 } -
5816 default: -
5817 if (item->d_ptr->acceptedTouchBeginEvent) { -
5818 updateTouchPointsForItem(item, &touchEvent); -
5819 (void) sendEvent(item, &touchEvent); -
5820 ignoreSceneTouchEvent = false; -
5821 } -
5822 break; -
5823 } -
5824 } -
5825 sceneTouchEvent->setAccepted(ignoreSceneTouchEvent); -
5826} -
5827 -
5828bool QGraphicsScenePrivate::sendTouchBeginEvent(QGraphicsItem *origin, QTouchEvent *touchEvent) -
5829{ -
5830 Q_Q(QGraphicsScene); -
5831 -
5832 if (cachedItemsUnderMouse.isEmpty() || cachedItemsUnderMouse.first() != origin) { -
5833 const QTouchEvent::TouchPoint &firstTouchPoint = touchEvent->touchPoints().first(); -
5834 cachedItemsUnderMouse = itemsAtPosition(firstTouchPoint.screenPos().toPoint(), -
5835 firstTouchPoint.scenePos(), -
5836 static_cast<QWidget *>(touchEvent->target())); -
5837 } -
5838 -
5839 // Set focus on the topmost enabled item that can take focus. -
5840 bool setFocus = false; -
5841 -
5842 foreach (QGraphicsItem *item, cachedItemsUnderMouse) { -
5843 if (item->isEnabled() && ((item->flags() & QGraphicsItem::ItemIsFocusable) && item->d_ptr->mouseSetsFocus)) { -
5844 if (!item->isWidget() || ((QGraphicsWidget *)item)->focusPolicy() & Qt::ClickFocus) { -
5845 setFocus = true; -
5846 if (item != q->focusItem()) -
5847 q->setFocusItem(item, Qt::MouseFocusReason); -
5848 break; -
5849 } -
5850 } -
5851 if (item->isPanel()) -
5852 break; -
5853 if (item->d_ptr->flags & QGraphicsItem::ItemStopsClickFocusPropagation) -
5854 break; -
5855 if (item->d_ptr->flags & QGraphicsItem::ItemStopsFocusHandling) { -
5856 // Make sure we don't clear focus. -
5857 setFocus = true; -
5858 break; -
5859 } -
5860 } -
5861 -
5862 // If nobody could take focus, clear it. -
5863 if (!stickyFocus && !setFocus) -
5864 q->setFocusItem(0, Qt::MouseFocusReason); -
5865 -
5866 bool res = false; -
5867 bool eventAccepted = touchEvent->isAccepted(); -
5868 foreach (QGraphicsItem *item, cachedItemsUnderMouse) { -
5869 // first, try to deliver the touch event -
5870 updateTouchPointsForItem(item, touchEvent); -
5871 bool acceptTouchEvents = item->acceptTouchEvents(); -
5872 touchEvent->setAccepted(acceptTouchEvents); -
5873 res = acceptTouchEvents && sendEvent(item, touchEvent); -
5874 eventAccepted = touchEvent->isAccepted(); -
5875 if (itemForTouchPointId.value(touchEvent->touchPoints().first().id()) == 0) { -
5876 // item was deleted -
5877 item = 0; -
5878 } else { -
5879 item->d_ptr->acceptedTouchBeginEvent = (res && eventAccepted); -
5880 } -
5881 touchEvent->spont = false; -
5882 if (res && eventAccepted) { -
5883 // the first item to accept the TouchBegin gets an implicit grab. -
5884 for (int i = 0; i < touchEvent->touchPoints().count(); ++i) { -
5885 const QTouchEvent::TouchPoint &touchPoint = touchEvent->touchPoints().at(i); -
5886 itemForTouchPointId[touchPoint.id()] = item; // can be zero -
5887 } -
5888 break; -
5889 } -
5890 if (item && item->isPanel()) -
5891 break; -
5892 } -
5893 -
5894 touchEvent->setAccepted(eventAccepted); -
5895 return res; -
5896} -
5897 -
5898void QGraphicsScenePrivate::enableTouchEventsOnViews() -
5899{ -
5900 foreach (QGraphicsView *view, views) -
5901 view->viewport()->setAttribute(Qt::WA_AcceptTouchEvents, true); -
5902} -
5903 -
5904void QGraphicsScenePrivate::updateInputMethodSensitivityInViews() -
5905{ -
5906 for (int i = 0; i < views.size(); ++i) -
5907 views.at(i)->d_func()->updateInputMethodSensitivity(); -
5908} -
5909 -
5910void QGraphicsScenePrivate::enterModal(QGraphicsItem *panel, QGraphicsItem::PanelModality previousModality) -
5911{ -
5912 Q_Q(QGraphicsScene); -
5913 Q_ASSERT(panel && panel->isPanel()); -
5914 -
5915 QGraphicsItem::PanelModality panelModality = panel->d_ptr->panelModality; -
5916 if (previousModality != QGraphicsItem::NonModal) { -
5917 // the panel is changing from one modality type to another... temporarily set it back so -
5918 // that blockedPanels is populated correctly -
5919 panel->d_ptr->panelModality = previousModality; -
5920 } -
5921 -
5922 QSet<QGraphicsItem *> blockedPanels; -
5923 QList<QGraphicsItem *> items = q->items(); // ### store panels separately -
5924 for (int i = 0; i < items.count(); ++i) { -
5925 QGraphicsItem *item = items.at(i); -
5926 if (item->isPanel() && item->isBlockedByModalPanel()) -
5927 blockedPanels.insert(item); -
5928 } -
5929 // blockedPanels contains all currently blocked panels -
5930 -
5931 if (previousModality != QGraphicsItem::NonModal) { -
5932 // reset the modality to the proper value, since we changed it above -
5933 panel->d_ptr->panelModality = panelModality; -
5934 // remove this panel so that it will be reinserted at the front of the stack -
5935 modalPanels.removeAll(panel); -
5936 } -
5937 -
5938 modalPanels.prepend(panel); -
5939 -
5940 if (!hoverItems.isEmpty()) { -
5941 // send GraphicsSceneHoverLeave events to newly blocked hoverItems -
5942 QGraphicsSceneHoverEvent hoverEvent; -
5943 hoverEvent.setScenePos(lastSceneMousePos); -
5944 dispatchHoverEvent(&hoverEvent); -
5945 } -
5946 -
5947 if (!mouseGrabberItems.isEmpty() && lastMouseGrabberItemHasImplicitMouseGrab) { -
5948 QGraphicsItem *item = mouseGrabberItems.last(); -
5949 if (item->isBlockedByModalPanel()) -
5950 ungrabMouse(item, /*itemIsDying =*/ false); -
5951 } -
5952 -
5953 QEvent windowBlockedEvent(QEvent::WindowBlocked); -
5954 QEvent windowUnblockedEvent(QEvent::WindowUnblocked); -
5955 for (int i = 0; i < items.count(); ++i) { -
5956 QGraphicsItem *item = items.at(i); -
5957 if (item->isPanel()) { -
5958 if (!blockedPanels.contains(item) && item->isBlockedByModalPanel()) { -
5959 // send QEvent::WindowBlocked to newly blocked panels -
5960 sendEvent(item, &windowBlockedEvent); -
5961 } else if (blockedPanels.contains(item) && !item->isBlockedByModalPanel()) { -
5962 // send QEvent::WindowUnblocked to unblocked panels when downgrading -
5963 // a panel from SceneModal to PanelModal -
5964 sendEvent(item, &windowUnblockedEvent); -
5965 } -
5966 } -
5967 } -
5968} -
5969 -
5970void QGraphicsScenePrivate::leaveModal(QGraphicsItem *panel) -
5971{ -
5972 Q_Q(QGraphicsScene); -
5973 Q_ASSERT(panel && panel->isPanel()); -
5974 -
5975 QSet<QGraphicsItem *> blockedPanels; -
5976 QList<QGraphicsItem *> items = q->items(); // ### same as above -
5977 for (int i = 0; i < items.count(); ++i) { -
5978 QGraphicsItem *item = items.at(i); -
5979 if (item->isPanel() && item->isBlockedByModalPanel()) -
5980 blockedPanels.insert(item); -
5981 } -
5982 -
5983 modalPanels.removeAll(panel); -
5984 -
5985 QEvent e(QEvent::WindowUnblocked); -
5986 for (int i = 0; i < items.count(); ++i) { -
5987 QGraphicsItem *item = items.at(i); -
5988 if (item->isPanel() && blockedPanels.contains(item) && !item->isBlockedByModalPanel()) -
5989 sendEvent(item, &e); -
5990 } -
5991 -
5992 // send GraphicsSceneHoverEnter events to newly unblocked items -
5993 QGraphicsSceneHoverEvent hoverEvent; -
5994 hoverEvent.setScenePos(lastSceneMousePos); -
5995 dispatchHoverEvent(&hoverEvent); -
5996} -
5997 -
5998#ifndef QT_NO_GESTURES -
5999void QGraphicsScenePrivate::gestureTargetsAtHotSpots(const QSet<QGesture *> &gestures, -
6000 Qt::GestureFlag flag, -
6001 QHash<QGraphicsObject *, QSet<QGesture *> > *targets, -
6002 QSet<QGraphicsObject *> *itemsSet, -
6003 QSet<QGesture *> *normal, -
6004 QSet<QGesture *> *conflicts) -
6005{ -
6006 QSet<QGesture *> normalGestures; // that are not in conflicted state. -
6007 foreach (QGesture *gesture, gestures) { -
6008 if (!gesture->hasHotSpot()) -
6009 continue; -
6010 const Qt::GestureType gestureType = gesture->gestureType(); -
6011 QList<QGraphicsItem *> items = itemsAtPosition(QPoint(), gesture->d_func()->sceneHotSpot, 0); -
6012 for (int j = 0; j < items.size(); ++j) { -
6013 QGraphicsItem *item = items.at(j); -
6014 -
6015 // Check if the item is blocked by a modal panel and use it as -
6016 // a target instead of this item. -
6017 (void) item->isBlockedByModalPanel(&item); -
6018 -
6019 if (QGraphicsObject *itemobj = item->toGraphicsObject()) { -
6020 QGraphicsItemPrivate *d = item->QGraphicsItem::d_func(); -
6021 QMap<Qt::GestureType, Qt::GestureFlags>::const_iterator it = -
6022 d->gestureContext.constFind(gestureType); -
6023 if (it != d->gestureContext.constEnd() && (!flag || (it.value() & flag))) { -
6024 if (normalGestures.contains(gesture)) { -
6025 normalGestures.remove(gesture); -
6026 if (conflicts) -
6027 conflicts->insert(gesture); -
6028 } else { -
6029 normalGestures.insert(gesture); -
6030 } -
6031 if (targets) -
6032 (*targets)[itemobj].insert(gesture); -
6033 if (itemsSet) -
6034 (*itemsSet).insert(itemobj); -
6035 } -
6036 } -
6037 // Don't propagate through panels. -
6038 if (item->isPanel()) -
6039 break; -
6040 } -
6041 } -
6042 if (normal) -
6043 *normal = normalGestures; -
6044} -
6045 -
6046void QGraphicsScenePrivate::gestureEventHandler(QGestureEvent *event) -
6047{ -
6048 QWidget *viewport = event->widget(); -
6049 if (!viewport) -
6050 return; -
6051 QGraphicsView *graphicsView = qobject_cast<QGraphicsView *>(viewport->parent()); -
6052 if (!graphicsView) -
6053 return; -
6054 -
6055 QList<QGesture *> allGestures = event->gestures(); -
6056 DEBUG() << "QGraphicsScenePrivate::gestureEventHandler:" -
6057 << "Gestures:" << allGestures; -
6058 -
6059 QSet<QGesture *> startedGestures; -
6060 QPoint delta = viewport->mapFromGlobal(QPoint()); -
6061 QTransform toScene = QTransform::fromTranslate(delta.x(), delta.y()) -
6062 * graphicsView->viewportTransform().inverted(); -
6063 foreach (QGesture *gesture, allGestures) { -
6064 // cache scene coordinates of the hot spot -
6065 if (gesture->hasHotSpot()) { -
6066 gesture->d_func()->sceneHotSpot = toScene.map(gesture->hotSpot()); -
6067 } else { -
6068 gesture->d_func()->sceneHotSpot = QPointF(); -
6069 } -
6070 -
6071 QGraphicsObject *target = gestureTargets.value(gesture, 0); -
6072 if (!target) { -
6073 // when we are not in started mode but don't have a target -
6074 // then the only one interested in gesture is the view/scene -
6075 if (gesture->state() == Qt::GestureStarted) -
6076 startedGestures.insert(gesture); -
6077 } -
6078 } -
6079 -
6080 if (!startedGestures.isEmpty()) { -
6081 QSet<QGesture *> normalGestures; // that have just one target -
6082 QSet<QGesture *> conflictedGestures; // that have multiple possible targets -
6083 gestureTargetsAtHotSpots(startedGestures, Qt::GestureFlag(0), &cachedItemGestures, 0, -
6084 &normalGestures, &conflictedGestures); -
6085 cachedTargetItems = cachedItemGestures.keys(); -
6086 std::sort(cachedTargetItems.begin(), cachedTargetItems.end(), qt_closestItemFirst); -
6087 DEBUG() << "QGraphicsScenePrivate::gestureEventHandler:" -
6088 << "Normal gestures:" << normalGestures -
6089 << "Conflicting gestures:" << conflictedGestures; -
6090 -
6091 // deliver conflicted gestures as override events AND remember -
6092 // initial gesture targets -
6093 if (!conflictedGestures.isEmpty()) { -
6094 for (int i = 0; i < cachedTargetItems.size(); ++i) { -
6095 QPointer<QGraphicsObject> item = cachedTargetItems.at(i); -
6096 -
6097 // get gestures to deliver to the current item -
6098 QSet<QGesture *> gestures = conflictedGestures & cachedItemGestures.value(item.data()); -
6099 if (gestures.isEmpty()) -
6100 continue; -
6101 -
6102 DEBUG() << "QGraphicsScenePrivate::gestureEventHandler:" -
6103 << "delivering override to" -
6104 << item.data() << gestures; -
6105 // send gesture override -
6106 QGestureEvent ev(gestures.toList()); -
6107 ev.t = QEvent::GestureOverride; -
6108 ev.setWidget(event->widget()); -
6109 // mark event and individual gestures as ignored -
6110 ev.ignore(); -
6111 foreach(QGesture *g, gestures) -
6112 ev.setAccepted(g, false); -
6113 sendEvent(item.data(), &ev); -
6114 // mark all accepted gestures to deliver them as normal gesture events -
6115 foreach (QGesture *g, gestures) { -
6116 if (ev.isAccepted() || ev.isAccepted(g)) { -
6117 conflictedGestures.remove(g); -
6118 // mark the item as a gesture target -
6119 if (item) { -
6120 gestureTargets.insert(g, item.data()); -
6121 QHash<QGraphicsObject *, QSet<QGesture *> >::iterator it, e; -
6122 it = cachedItemGestures.begin(); -
6123 e = cachedItemGestures.end(); -
6124 for(; it != e; ++it) -
6125 it.value().remove(g); -
6126 cachedItemGestures[item.data()].insert(g); -
6127 } -
6128 DEBUG() << "QGraphicsScenePrivate::gestureEventHandler:" -
6129 << "override was accepted:" -
6130 << g << item.data(); -
6131 } -
6132 // remember the first item that received the override event -
6133 // as it most likely become a target if no one else accepts -
6134 // the override event -
6135 if (!gestureTargets.contains(g) && item) -
6136 gestureTargets.insert(g, item.data()); -
6137 -
6138 } -
6139 if (conflictedGestures.isEmpty()) -
6140 break; -
6141 } -
6142 } -
6143 // remember the initial target item for each gesture that was not in -
6144 // the conflicted state. -
6145 if (!normalGestures.isEmpty()) { -
6146 for (int i = 0; i < cachedTargetItems.size() && !normalGestures.isEmpty(); ++i) { -
6147 QGraphicsObject *item = cachedTargetItems.at(i); -
6148 -
6149 // get gestures to deliver to the current item -
6150 foreach (QGesture *g, cachedItemGestures.value(item)) { -
6151 if (!gestureTargets.contains(g)) { -
6152 gestureTargets.insert(g, item); -
6153 normalGestures.remove(g); -
6154 } -
6155 } -
6156 } -
6157 } -
6158 } -
6159 -
6160 -
6161 // deliver all gesture events -
6162 QSet<QGesture *> undeliveredGestures; -
6163 QSet<QGesture *> parentPropagatedGestures; -
6164 foreach (QGesture *gesture, allGestures) { -
6165 if (QGraphicsObject *target = gestureTargets.value(gesture, 0)) { -
6166 cachedItemGestures[target].insert(gesture); -
6167 cachedTargetItems.append(target); -
6168 undeliveredGestures.insert(gesture); -
6169 QGraphicsItemPrivate *d = target->QGraphicsItem::d_func(); -
6170 const Qt::GestureFlags flags = d->gestureContext.value(gesture->gestureType()); -
6171 if (flags & Qt::IgnoredGesturesPropagateToParent) -
6172 parentPropagatedGestures.insert(gesture); -
6173 } else { -
6174 DEBUG() << "QGraphicsScenePrivate::gestureEventHandler:" -
6175 << "no target for" << gesture << "at" -
6176 << gesture->hotSpot() << gesture->d_func()->sceneHotSpot; -
6177 } -
6178 } -
6179 std::sort(cachedTargetItems.begin(), cachedTargetItems.end(), qt_closestItemFirst); -
6180 for (int i = 0; i < cachedTargetItems.size(); ++i) { -
6181 QPointer<QGraphicsObject> receiver = cachedTargetItems.at(i); -
6182 QSet<QGesture *> gestures = -
6183 undeliveredGestures & cachedItemGestures.value(receiver.data()); -
6184 gestures -= cachedAlreadyDeliveredGestures.value(receiver.data()); -
6185 -
6186 if (gestures.isEmpty()) -
6187 continue; -
6188 -
6189 cachedAlreadyDeliveredGestures[receiver.data()] += gestures; -
6190 const bool isPanel = receiver.data()->isPanel(); -
6191 -
6192 DEBUG() << "QGraphicsScenePrivate::gestureEventHandler:" -
6193 << "delivering to" -
6194 << receiver.data() << gestures; -
6195 QGestureEvent ev(gestures.toList()); -
6196 ev.setWidget(event->widget()); -
6197 sendEvent(receiver.data(), &ev); -
6198 QSet<QGesture *> ignoredGestures; -
6199 foreach (QGesture *g, gestures) { -
6200 if (!ev.isAccepted() && !ev.isAccepted(g)) { -
6201 // if the gesture was ignored by its target, we will update the -
6202 // targetItems list with a possible target items (items that -
6203 // want to receive partial gestures). -
6204 // ### wont' work if the target was destroyed in the event-
/ ### won't work if the target was destroyed in the event
6205 // we will just stop delivering it. -
6206 if (receiver && receiver.data() == gestureTargets.value(g, 0)) -
6207 ignoredGestures.insert(g); -
6208 } else { -
6209 if (receiver && g->state() == Qt::GestureStarted) { -
6210 // someone accepted the propagated initial GestureStarted -
6211 // event, let it be the new target for all following events. -
6212 gestureTargets[g] = receiver.data(); -
6213 } -
6214 undeliveredGestures.remove(g); -
6215 } -
6216 } -
6217 if (undeliveredGestures.isEmpty()) -
6218 break; -
6219 -
6220 // ignoredGestures list is only filled when delivering to the gesture -
6221 // target item, so it is safe to assume item == target. -
6222 if (!ignoredGestures.isEmpty() && !isPanel) { -
6223 // look for new potential targets for gestures that were ignored -
6224 // and should be propagated. -
6225 -
6226 QSet<QGraphicsObject *> targetsSet = cachedTargetItems.toSet(); -
6227 -
6228 if (receiver) { -
6229 // first if the gesture should be propagated to parents only -
6230 for (QSet<QGesture *>::iterator it = ignoredGestures.begin(); -
6231 it != ignoredGestures.end();) { -
6232 if (parentPropagatedGestures.contains(*it)) { -
6233 QGesture *gesture = *it; -
6234 const Qt::GestureType gestureType = gesture->gestureType(); -
6235 QGraphicsItem *item = receiver.data(); -
6236 while (item) { -
6237 if (QGraphicsObject *obj = item->toGraphicsObject()) { -
6238 if (item->d_func()->gestureContext.contains(gestureType)) { -
6239 targetsSet.insert(obj); -
6240 cachedItemGestures[obj].insert(gesture); -
6241 } -
6242 } -
6243 if (item->isPanel()) -
6244 break; -
6245 item = item->parentItem(); -
6246 } -
6247 -
6248 it = ignoredGestures.erase(it); -
6249 continue; -
6250 } -
6251 ++it; -
6252 } -
6253 } -
6254 -
6255 gestureTargetsAtHotSpots(ignoredGestures, Qt::ReceivePartialGestures, -
6256 &cachedItemGestures, &targetsSet, 0, 0); -
6257 -
6258 cachedTargetItems = targetsSet.toList(); -
6259 std::sort(cachedTargetItems.begin(), cachedTargetItems.end(), qt_closestItemFirst); -
6260 DEBUG() << "QGraphicsScenePrivate::gestureEventHandler:" -
6261 << "new targets:" << cachedTargetItems; -
6262 i = -1; // start delivery again -
6263 continue; -
6264 } -
6265 } -
6266 -
6267 foreach (QGesture *g, startedGestures) { -
6268 if (g->gestureCancelPolicy() == QGesture::CancelAllInContext) { -
6269 DEBUG() << "lets try to cancel some"; -
6270 // find gestures in context in Qt::GestureStarted or Qt::GestureUpdated state and cancel them -
6271 cancelGesturesForChildren(g); -
6272 } -
6273 } -
6274 -
6275 // forget about targets for gestures that have ended -
6276 foreach (QGesture *g, allGestures) { -
6277 switch (g->state()) { -
6278 case Qt::GestureFinished: -
6279 case Qt::GestureCanceled: -
6280 gestureTargets.remove(g); -
6281 break; -
6282 default: -
6283 break; -
6284 } -
6285 } -
6286 -
6287 cachedTargetItems.clear(); -
6288 cachedItemGestures.clear(); -
6289 cachedAlreadyDeliveredGestures.clear(); -
6290} -
6291 -
6292void QGraphicsScenePrivate::cancelGesturesForChildren(QGesture *original) -
6293{ -
6294 Q_ASSERT(original); -
6295 QGraphicsItem *originalItem = gestureTargets.value(original); -
6296 if (originalItem == 0) // we only act on accepted gestures, which implies it has a target. -
6297 return; -
6298 -
6299 // iterate over all active gestures and for each find the owner -
6300 // if the owner is part of our sub-hierarchy, cancel it. -
6301 -
6302 QSet<QGesture *> canceledGestures; -
6303 QHash<QGesture *, QGraphicsObject *>::Iterator iter = gestureTargets.begin(); -
6304 while (iter != gestureTargets.end()) { -
6305 QGraphicsObject *item = iter.value(); -
6306 // note that we don't touch the gestures for our originalItem -
6307 if (item != originalItem && originalItem->isAncestorOf(item)) { -
6308 DEBUG() << " found a gesture to cancel" << iter.key(); -
6309 iter.key()->d_func()->state = Qt::GestureCanceled; -
6310 canceledGestures << iter.key(); -
6311 } -
6312 ++iter; -
6313 } -
6314 -
6315 // sort them per target item by cherry picking from almostCanceledGestures and delivering -
6316 QSet<QGesture *> almostCanceledGestures = canceledGestures; -
6317 QSet<QGesture *>::Iterator setIter; -
6318 while (!almostCanceledGestures.isEmpty()) { -
6319 QGraphicsObject *target = 0; -
6320 QSet<QGesture*> gestures; -
6321 setIter = almostCanceledGestures.begin(); -
6322 // sort per target item -
6323 while (setIter != almostCanceledGestures.end()) { -
6324 QGraphicsObject *item = gestureTargets.value(*setIter); -
6325 if (target == 0) -
6326 target = item; -
6327 if (target == item) { -
6328 gestures << *setIter; -
6329 setIter = almostCanceledGestures.erase(setIter); -
6330 } else { -
6331 ++setIter; -
6332 } -
6333 } -
6334 Q_ASSERT(target); -
6335 -
6336 QList<QGesture *> list = gestures.toList(); -
6337 QGestureEvent ev(list); -
6338 sendEvent(target, &ev); -
6339 -
6340 foreach (QGesture *g, list) { -
6341 if (ev.isAccepted() || ev.isAccepted(g)) -
6342 gestures.remove(g); -
6343 } -
6344 -
6345 foreach (QGesture *g, gestures) { -
6346 if (!g->hasHotSpot()) -
6347 continue; -
6348 -
6349 QList<QGraphicsItem *> items = itemsAtPosition(QPoint(), g->d_func()->sceneHotSpot, 0); -
6350 for (int j = 0; j < items.size(); ++j) { -
6351 QGraphicsObject *item = items.at(j)->toGraphicsObject(); -
6352 if (!item) -
6353 continue; -
6354 QGraphicsItemPrivate *d = item->QGraphicsItem::d_func(); -
6355 if (d->gestureContext.contains(g->gestureType())) { -
6356 QList<QGesture *> list; -
6357 list << g; -
6358 QGestureEvent ev(list); -
6359 sendEvent(item, &ev); -
6360 if (ev.isAccepted() || ev.isAccepted(g)) -
6361 break; // successfully delivered -
6362 } -
6363 } -
6364 } -
6365 } -
6366 -
6367 QGestureManager *gestureManager = QApplicationPrivate::instance()->gestureManager; -
6368 Q_ASSERT(gestureManager); // it would be very odd if we got called without a manager. -
6369 for (setIter = canceledGestures.begin(); setIter != canceledGestures.end(); ++setIter) { -
6370 gestureManager->recycle(*setIter); -
6371 gestureTargets.remove(*setIter); -
6372 } -
6373} -
6374 -
6375void QGraphicsScenePrivate::grabGesture(QGraphicsItem *, Qt::GestureType gesture) -
6376{ -
6377 (void)QGestureManager::instance(); // create a gesture manager -
6378 if (!grabbedGestures[gesture]++) { -
6379 foreach (QGraphicsView *view, views) -
6380 view->viewport()->grabGesture(gesture); -
6381 } -
6382} -
6383 -
6384void QGraphicsScenePrivate::ungrabGesture(QGraphicsItem *item, Qt::GestureType gesture) -
6385{ -
6386 // we know this can only be an object -
6387 Q_ASSERT(item->d_ptr->isObject); -
6388 QGraphicsObject *obj = static_cast<QGraphicsObject *>(item); -
6389 QGestureManager::instance()->cleanupCachedGestures(obj, gesture); -
6390 if (!--grabbedGestures[gesture]) { -
6391 foreach (QGraphicsView *view, views) -
6392 view->viewport()->ungrabGesture(gesture); -
6393 } -
6394} -
6395#endif // QT_NO_GESTURES -
6396 -
6397QT_END_NAMESPACE -
6398 -
6399#include "moc_qgraphicsscene.cpp" -
6400 -
6401#endif // QT_NO_GRAPHICSVIEW -
6402 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial