diff options
Diffstat (limited to 'src/widgets/graphicsview/qgraphicsscene.cpp')
-rw-r--r-- | src/widgets/graphicsview/qgraphicsscene.cpp | 253 |
1 files changed, 138 insertions, 115 deletions
diff --git a/src/widgets/graphicsview/qgraphicsscene.cpp b/src/widgets/graphicsview/qgraphicsscene.cpp index 5e3b426d49..15edb15619 100644 --- a/src/widgets/graphicsview/qgraphicsscene.cpp +++ b/src/widgets/graphicsview/qgraphicsscene.cpp @@ -1,31 +1,37 @@ /**************************************************************************** ** -** Copyright (C) 2015 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtWidgets module of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $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 The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/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 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company 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 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** @@ -232,6 +238,7 @@ #include <QtGui/qpainter.h> #include <QtGui/qpixmapcache.h> #include <QtGui/qpolygon.h> +#include <QtGui/qtouchdevice.h> #include <QtWidgets/qstyleoption.h> #include <QtWidgets/qtooltip.h> #include <QtGui/qtransform.h> @@ -756,8 +763,9 @@ void QGraphicsScenePrivate::setActivePanelHelper(QGraphicsItem *item, bool durin q->sendEvent(activePanel, &event); } else if (panel && !duringActivationEvent) { // Deactivate the scene if changing activation to a panel. + const auto items = q->items(); QEvent event(QEvent::WindowDeactivate); - foreach (QGraphicsItem *item, q->items()) { + for (QGraphicsItem *item : items) { if (item->isVisible() && !item->isPanel() && !item->parentItem()) q->sendEvent(item, &event); } @@ -791,9 +799,10 @@ void QGraphicsScenePrivate::setActivePanelHelper(QGraphicsItem *item, bool durin } while (fw != panel); } } else if (q->isActive()) { + const auto items = q->items(); // Activate the scene QEvent event(QEvent::WindowActivate); - foreach (QGraphicsItem *item, q->items()) { + for (QGraphicsItem *item : items) { if (item->isVisible() && !item->isPanel() && !item->parentItem()) q->sendEvent(item, &event); } @@ -932,7 +941,7 @@ void QGraphicsScenePrivate::grabMouse(QGraphicsItem *item, bool implicit) { // Append to list of mouse grabber items, and send a mouse grab event. if (mouseGrabberItems.contains(item)) { - if (mouseGrabberItems.last() == item) { + if (mouseGrabberItems.constLast() == item) { Q_ASSERT(!implicit); if (!lastMouseGrabberItemHasImplicitMouseGrab) { qWarning("QGraphicsItem::grabMouse: already a mouse grabber"); @@ -942,14 +951,14 @@ void QGraphicsScenePrivate::grabMouse(QGraphicsItem *item, bool implicit) } } else { qWarning("QGraphicsItem::grabMouse: already blocked by mouse grabber: %p", - mouseGrabberItems.last()); + mouseGrabberItems.constLast()); } return; } // Send ungrab event to the last grabber. if (!mouseGrabberItems.isEmpty()) { - QGraphicsItem *last = mouseGrabberItems.last(); + QGraphicsItem *last = mouseGrabberItems.constLast(); if (lastMouseGrabberItemHasImplicitMouseGrab) { // Implicit mouse grab is immediately lost. last->ungrabMouse(); @@ -979,12 +988,12 @@ void QGraphicsScenePrivate::ungrabMouse(QGraphicsItem *item, bool itemIsDying) return; } - if (item != mouseGrabberItems.last()) { + if (item != mouseGrabberItems.constLast()) { // Recursively ungrab the next mouse grabber until we reach this item // to ensure state consistency. ungrabMouse(mouseGrabberItems.at(index + 1), itemIsDying); } - if (!popupWidgets.isEmpty() && item == popupWidgets.last()) { + if (!popupWidgets.isEmpty() && item == popupWidgets.constLast()) { // If the item is a popup, go via removePopup to ensure state // consistency and that it gets hidden correctly - beware that // removePopup() reenters this function to continue removing the grab. @@ -1009,7 +1018,7 @@ void QGraphicsScenePrivate::ungrabMouse(QGraphicsItem *item, bool itemIsDying) // items get a GrabMouse event, but this is a rare case with a simple // implementation and it does ensure a consistent state. if (!itemIsDying && !mouseGrabberItems.isEmpty()) { - QGraphicsItem *last = mouseGrabberItems.last(); + QGraphicsItem *last = mouseGrabberItems.constLast(); QEvent event(QEvent::GrabMouse); sendEvent(last, &event); } @@ -1031,11 +1040,11 @@ void QGraphicsScenePrivate::clearMouseGrabber() void QGraphicsScenePrivate::grabKeyboard(QGraphicsItem *item) { if (keyboardGrabberItems.contains(item)) { - if (keyboardGrabberItems.last() == item) + if (keyboardGrabberItems.constLast() == item) qWarning("QGraphicsItem::grabKeyboard: already a keyboard grabber"); else qWarning("QGraphicsItem::grabKeyboard: already blocked by keyboard grabber: %p", - keyboardGrabberItems.last()); + keyboardGrabberItems.constLast()); return; } @@ -1043,7 +1052,7 @@ void QGraphicsScenePrivate::grabKeyboard(QGraphicsItem *item) if (!keyboardGrabberItems.isEmpty()) { // Just send ungrab event to current grabber. QEvent ungrabEvent(QEvent::UngrabKeyboard); - sendEvent(keyboardGrabberItems.last(), &ungrabEvent); + sendEvent(keyboardGrabberItems.constLast(), &ungrabEvent); } keyboardGrabberItems << item; @@ -1063,7 +1072,7 @@ void QGraphicsScenePrivate::ungrabKeyboard(QGraphicsItem *item, bool itemIsDying qWarning("QGraphicsItem::ungrabKeyboard: not a keyboard grabber"); return; } - if (item != keyboardGrabberItems.last()) { + if (item != keyboardGrabberItems.constLast()) { // Recursively ungrab the topmost keyboard grabber until we reach this // item to ensure state consistency. ungrabKeyboard(keyboardGrabberItems.at(index + 1), itemIsDying); @@ -1080,7 +1089,7 @@ void QGraphicsScenePrivate::ungrabKeyboard(QGraphicsItem *item, bool itemIsDying // Send notification about mouse regrab. if (!itemIsDying && !keyboardGrabberItems.isEmpty()) { - QGraphicsItem *last = keyboardGrabberItems.last(); + QGraphicsItem *last = keyboardGrabberItems.constLast(); QEvent event(QEvent::GrabKeyboard); sendEvent(last, &event); } @@ -1092,7 +1101,7 @@ void QGraphicsScenePrivate::ungrabKeyboard(QGraphicsItem *item, bool itemIsDying void QGraphicsScenePrivate::clearKeyboardGrabber() { if (!keyboardGrabberItems.isEmpty()) - ungrabKeyboard(keyboardGrabberItems.first()); + ungrabKeyboard(keyboardGrabberItems.constFirst()); } void QGraphicsScenePrivate::enableMouseTrackingOnViews() @@ -1134,8 +1143,8 @@ void QGraphicsScenePrivate::storeMouseButtonsForMouseGrabber(QGraphicsSceneMouse for (int i = 0x1; i <= 0x10; i <<= 1) { if (event->buttons() & i) { mouseGrabberButtonDownPos.insert(Qt::MouseButton(i), - mouseGrabberItems.last()->d_ptr->genericMapFromScene(event->scenePos(), - event->widget())); + mouseGrabberItems.constLast()->d_ptr->genericMapFromScene(event->scenePos(), + event->widget())); mouseGrabberButtonDownScenePos.insert(Qt::MouseButton(i), event->scenePos()); mouseGrabberButtonDownScreenPos.insert(Qt::MouseButton(i), event->screenPos()); } @@ -1300,7 +1309,7 @@ void QGraphicsScenePrivate::sendMouseEvent(QGraphicsSceneMouseEvent *mouseEvent) return; } - QGraphicsItem *item = mouseGrabberItems.last(); + QGraphicsItem *item = mouseGrabberItems.constLast(); if (item->isBlockedByModalPanel()) return; @@ -1327,7 +1336,7 @@ void QGraphicsScenePrivate::mousePressEventHandler(QGraphicsSceneMouseEvent *mou // Deliver to any existing mouse grabber. if (!mouseGrabberItems.isEmpty()) { - if (mouseGrabberItems.last()->isBlockedByModalPanel()) + if (mouseGrabberItems.constLast()->isBlockedByModalPanel()) return; // The event is ignored by default, but we disregard the event's // accepted state after delivery; the mouse is grabbed, after all. @@ -1393,7 +1402,7 @@ void QGraphicsScenePrivate::mousePressEventHandler(QGraphicsSceneMouseEvent *mou // Any item will do. if (sceneModality && cachedItemsUnderMouse.isEmpty()) - cachedItemsUnderMouse << modalPanels.first(); + cachedItemsUnderMouse << modalPanels.constFirst(); // Find a mouse grabber by sending mouse press events to all mouse grabber // candidates one at a time, until the event is accepted. It's accepted by @@ -1442,7 +1451,7 @@ void QGraphicsScenePrivate::mousePressEventHandler(QGraphicsSceneMouseEvent *mou sendMouseEvent(mouseEvent); } - bool dontSendUngrabEvents = mouseGrabberItems.isEmpty() || mouseGrabberItems.last() != item; + bool dontSendUngrabEvents = mouseGrabberItems.isEmpty() || mouseGrabberItems.constLast() != item; if (disabled) { ungrabMouse(item, /* itemIsDying = */ dontSendUngrabEvents); break; @@ -1545,7 +1554,8 @@ void QGraphicsScenePrivate::updateFont(const QFont &font) // Resolve the fonts of all top-level widget items, or widget items // whose parent is not a widget. - foreach (QGraphicsItem *item, q->items()) { + const auto items = q->items(); + for (QGraphicsItem *item : items) { if (!item->parentItem()) { // Resolvefont for an item is a noop operation, but // every item can be a widget, or can have a widget @@ -1601,7 +1611,8 @@ void QGraphicsScenePrivate::updatePalette(const QPalette &palette) // Resolve the palettes of all top-level widget items, or widget items // whose parent is not a widget. - foreach (QGraphicsItem *item, q->items()) { + const auto items = q->items(); + for (QGraphicsItem *item : items) { if (!item->parentItem()) { // ResolvePalette for an item is a noop operation, but // every item can be a widget, or can have a widget @@ -1947,7 +1958,8 @@ QRectF QGraphicsScene::itemsBoundingRect() const { // Does not take untransformable items into account. QRectF boundingRect; - foreach (QGraphicsItem *item, items()) + const auto items_ = items(); + for (QGraphicsItem *item : items_) boundingRect |= item->sceneBoundingRect(); return boundingRect; } @@ -2116,7 +2128,8 @@ QList<QGraphicsItem *> QGraphicsScene::collidingItems(const QGraphicsItem *item, // Does not support ItemIgnoresTransformations. QList<QGraphicsItem *> tmp; - foreach (QGraphicsItem *itemInVicinity, d->index->estimateItems(item->sceneBoundingRect(), Qt::DescendingOrder)) { + const auto itemsInVicinity = d->index->estimateItems(item->sceneBoundingRect(), Qt::DescendingOrder); + for (QGraphicsItem *itemInVicinity : itemsInVicinity) { if (item != itemInVicinity && item->collidesWithItem(itemInVicinity, mode)) tmp << itemInVicinity; } @@ -2155,8 +2168,8 @@ QList<QGraphicsItem *> QGraphicsScene::collidingItems(const QGraphicsItem *item, */ QGraphicsItem *QGraphicsScene::itemAt(const QPointF &position, const QTransform &deviceTransform) const { - QList<QGraphicsItem *> itemsAtPoint = items(position, Qt::IntersectsItemShape, - Qt::DescendingOrder, deviceTransform); + const QList<QGraphicsItem *> itemsAtPoint = items(position, Qt::IntersectsItemShape, + Qt::DescendingOrder, deviceTransform); return itemsAtPoint.isEmpty() ? 0 : itemsAtPoint.first(); } @@ -2303,7 +2316,8 @@ void QGraphicsScene::setSelectionArea(const QPainterPath &path, bool changed = false; // Set all items in path to selected. - foreach (QGraphicsItem *item, items(path, mode, Qt::DescendingOrder, deviceTransform)) { + const auto items = this->items(path, mode, Qt::DescendingOrder, deviceTransform); + for (QGraphicsItem *item : items) { if (item->flags() & QGraphicsItem::ItemIsSelectable) { if (!item->isSelected()) changed = true; @@ -2430,7 +2444,7 @@ QGraphicsItemGroup *QGraphicsScene::createItemGroup(const QList<QGraphicsItem *> QGraphicsItemGroup *group = new QGraphicsItemGroup(commonAncestor); if (!commonAncestor) addItem(group); - foreach (QGraphicsItem *item, items) + for (QGraphicsItem *item : items) group->addToGroup(item); return group; } @@ -2444,7 +2458,8 @@ QGraphicsItemGroup *QGraphicsScene::createItemGroup(const QList<QGraphicsItem *> */ void QGraphicsScene::destroyItemGroup(QGraphicsItemGroup *group) { - foreach (QGraphicsItem *item, group->childItems()) + const auto items = group->childItems(); + for (QGraphicsItem *item : items) group->removeFromGroup(item); removeItem(group); delete group; @@ -2559,7 +2574,8 @@ void QGraphicsScene::addItem(QGraphicsItem *item) } #ifndef QT_NO_GESTURES - foreach (Qt::GestureType gesture, item->d_ptr->gestureContext.keys()) + const auto gestures = item->d_ptr->gestureContext.keys(); // FIXME: iterate over hash directly? + for (Qt::GestureType gesture : gestures) d->grabGesture(item, gesture); #endif @@ -3130,7 +3146,8 @@ void QGraphicsScene::setForegroundBrush(const QBrush &brush) { Q_D(QGraphicsScene); d->foregroundBrush = brush; - foreach (QGraphicsView *view, views()) + const auto views_ = views(); + for (QGraphicsView *view : views_) view->viewport()->update(); update(); } @@ -3238,7 +3255,8 @@ void QGraphicsScene::update(const QRectF &rect) */ void QGraphicsScene::invalidate(const QRectF &rect, SceneLayers layers) { - foreach (QGraphicsView *view, views()) + const auto views_ = views(); + for (QGraphicsView *view : views_) view->invalidateScene(rect, layers); update(rect); } @@ -3279,7 +3297,8 @@ QList <QGraphicsView *> QGraphicsScene::views() const void QGraphicsScene::advance() { for (int i = 0; i < 2; ++i) { - foreach (QGraphicsItem *item, items()) + const auto items_ = items(); + for (QGraphicsItem *item : items_) item->advance(i); } } @@ -3434,7 +3453,8 @@ bool QGraphicsScene::event(QEvent *event) } else { // Activate all toplevel items. QEvent event(QEvent::WindowActivate); - foreach (QGraphicsItem *item, items()) { + const auto items_ = items(); + for (QGraphicsItem *item : items_) { if (item->isVisible() && !item->isPanel() && !item->parentItem()) sendEvent(item, &event); } @@ -3452,7 +3472,8 @@ bool QGraphicsScene::event(QEvent *event) } else { // Activate all toplevel items. QEvent event(QEvent::WindowDeactivate); - foreach (QGraphicsItem *item, items()) { + const auto items_ = items(); + for (QGraphicsItem *item : items_) { if (item->isVisible() && !item->isPanel() && !item->parentItem()) sendEvent(item, &event); } @@ -3547,9 +3568,10 @@ void QGraphicsScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMen // Send the event to all items at this position until one item accepts the // event. - foreach (QGraphicsItem *item, d->itemsAtPosition(contextMenuEvent->screenPos(), - contextMenuEvent->scenePos(), - contextMenuEvent->widget())) { + const auto items = d->itemsAtPosition(contextMenuEvent->screenPos(), + contextMenuEvent->scenePos(), + contextMenuEvent->widget()); + for (QGraphicsItem *item : items) { contextMenuEvent->setPos(item->d_ptr->genericMapFromScene(contextMenuEvent->scenePos(), contextMenuEvent->widget())); contextMenuEvent->accept(); @@ -3605,9 +3627,10 @@ void QGraphicsScene::dragMoveEvent(QGraphicsSceneDragDropEvent *event) // Find the topmost enabled items under the cursor. They are all // candidates for accepting drag & drop events. - foreach (QGraphicsItem *item, d->itemsAtPosition(event->screenPos(), - event->scenePos(), - event->widget())) { + const auto items = d->itemsAtPosition(event->screenPos(), + event->scenePos(), + event->widget()); + for (QGraphicsItem *item : items) { if (!item->isEnabled() || !item->acceptDrops()) continue; @@ -3749,7 +3772,7 @@ void QGraphicsScene::focusOutEvent(QFocusEvent *focusEvent) // Remove all popups when the scene loses focus. if (!d->popupWidgets.isEmpty()) - d->removePopup(d->popupWidgets.first()); + d->removePopup(d->popupWidgets.constFirst()); } /*! @@ -3846,7 +3869,7 @@ bool QGraphicsScenePrivate::dispatchHoverEvent(QGraphicsSceneHoverEvent *hoverEv // Find the common ancestor item for the new topmost hoverItem and the // last item in the hoverItem list. - QGraphicsItem *commonAncestorItem = (item && !hoverItems.isEmpty()) ? item->commonAncestorItem(hoverItems.last()) : 0; + QGraphicsItem *commonAncestorItem = (item && !hoverItems.isEmpty()) ? item->commonAncestorItem(hoverItems.constLast()) : 0; while (commonAncestorItem && !itemAcceptsHoverEvents_helper(commonAncestorItem)) commonAncestorItem = commonAncestorItem->parentItem(); if (commonAncestorItem && commonAncestorItem->panel() != item->panel()) { @@ -3870,15 +3893,15 @@ bool QGraphicsScenePrivate::dispatchHoverEvent(QGraphicsSceneHoverEvent *hoverEv QList<QGraphicsItem *> parents; QGraphicsItem *parent = item; while (parent && parent != commonAncestorItem) { - parents.prepend(parent); + parents.append(parent); if (parent->isPanel()) { // Stop at the panel - we don't deliver beyond this point. break; } parent = parent->parentItem(); } - for (int i = 0; i < parents.size(); ++i) { - parent = parents.at(i); + for (auto it = parents.crbegin(), end = parents.crend(); it != end; ++it) { + QGraphicsItem *parent = *it; hoverItems << parent; if (itemAcceptsHoverEvents_helper(parent)) sendHoverEvent(QEvent::GraphicsSceneHoverEnter, parent, hoverEvent); @@ -3887,7 +3910,7 @@ bool QGraphicsScenePrivate::dispatchHoverEvent(QGraphicsSceneHoverEvent *hoverEv // Generate a move event for the item itself if (item && !hoverItems.isEmpty() - && item == hoverItems.last()) { + && item == hoverItems.constLast()) { sendHoverEvent(QEvent::GraphicsSceneHoverMove, item, hoverEvent); return true; } @@ -3937,7 +3960,7 @@ void QGraphicsScene::keyPressEvent(QKeyEvent *keyEvent) // ### Merge this function with keyReleaseEvent; they are identical // ### (except this comment). Q_D(QGraphicsScene); - QGraphicsItem *item = !d->keyboardGrabberItems.isEmpty() ? d->keyboardGrabberItems.last() : 0; + QGraphicsItem *item = !d->keyboardGrabberItems.isEmpty() ? d->keyboardGrabberItems.constLast() : 0; if (!item) item = focusItem(); if (item) { @@ -3969,7 +3992,7 @@ void QGraphicsScene::keyReleaseEvent(QKeyEvent *keyEvent) // ### Merge this function with keyPressEvent; they are identical (except // ### this comment). Q_D(QGraphicsScene); - QGraphicsItem *item = !d->keyboardGrabberItems.isEmpty() ? d->keyboardGrabberItems.last() : 0; + QGraphicsItem *item = !d->keyboardGrabberItems.isEmpty() ? d->keyboardGrabberItems.constLast() : 0; if (!item) item = focusItem(); if (item) { @@ -4078,9 +4101,9 @@ void QGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) // Reset the mouse grabber when the last mouse button has been released. if (!mouseEvent->buttons()) { if (!d->mouseGrabberItems.isEmpty()) { - d->lastMouseGrabberItem = d->mouseGrabberItems.last(); + d->lastMouseGrabberItem = d->mouseGrabberItems.constLast(); if (d->lastMouseGrabberItemHasImplicitMouseGrab) - d->mouseGrabberItems.last()->ungrabMouse(); + d->mouseGrabberItems.constLast()->ungrabMouse(); } else { d->lastMouseGrabberItem = 0; } @@ -4133,9 +4156,9 @@ void QGraphicsScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent) void QGraphicsScene::wheelEvent(QGraphicsSceneWheelEvent *wheelEvent) { Q_D(QGraphicsScene); - QList<QGraphicsItem *> wheelCandidates = d->itemsAtPosition(wheelEvent->screenPos(), - wheelEvent->scenePos(), - wheelEvent->widget()); + const QList<QGraphicsItem *> wheelCandidates = d->itemsAtPosition(wheelEvent->screenPos(), + wheelEvent->scenePos(), + wheelEvent->widget()); #ifdef Q_DEAD_CODE_FROM_QT4_MAC // On Mac, ignore the event if the first item under the mouse is not the last opened @@ -4157,7 +4180,7 @@ void QGraphicsScene::wheelEvent(QGraphicsSceneWheelEvent *wheelEvent) #endif bool hasSetFocus = false; - foreach (QGraphicsItem *item, wheelCandidates) { + for (QGraphicsItem *item : wheelCandidates) { if (!hasSetFocus && item->isEnabled() && ((item->flags() & QGraphicsItem::ItemIsFocusable) && item->d_ptr->mouseSetsFocus)) { if (item->isWidget() && static_cast<QGraphicsWidget *>(item)->focusPolicy() == Qt::WheelFocus) { @@ -4635,7 +4658,8 @@ void QGraphicsScenePrivate::drawItemHelper(QGraphicsItem *item, QPainter *painte for (int i = 0; i < exposed.size(); ++i) br |= exposed.at(i); QTransform pixmapToItem = itemToPixmap.inverted(); - foreach (const QRect &r, scrollExposure.rects()) + const auto rects = scrollExposure.rects(); + for (const QRect &r : rects) br |= pixmapToItem.mapRect(r); } styleOptionTmp = *option; @@ -5564,7 +5588,8 @@ void QGraphicsScene::setStyle(QStyle *style) QApplication::sendEvent(this, &event); // Notify all widgets that don't have a style explicitly set. - foreach (QGraphicsItem *item, items()) { + const auto items_ = items(); + for (QGraphicsItem *item : items_) { if (item->isWidget()) { QGraphicsWidget *widget = static_cast<QGraphicsWidget *>(item); if (!widget->testAttribute(Qt::WA_SetStyle)) @@ -5728,18 +5753,16 @@ void QGraphicsScene::setActiveWindow(QGraphicsWidget *widget) // Raise if (panel) { - QList<QGraphicsItem *> siblingWindows; QGraphicsItem *parent = panel->parentItem(); // Raise ### inefficient for toplevels - foreach (QGraphicsItem *sibling, parent ? parent->childItems() : items()) { - if (sibling != panel && sibling->isWindow()) - siblingWindows << sibling; - } // Find the highest z value. qreal z = panel->zValue(); - for (int i = 0; i < siblingWindows.size(); ++i) - z = qMax(z, siblingWindows.at(i)->zValue()); + const auto siblings = parent ? parent->childItems() : items(); + for (QGraphicsItem *sibling : siblings) { + if (sibling != panel && sibling->isWindow()) + z = qMax(z, sibling->zValue()); + } // This will probably never overflow. const qreal litt = qreal(0.001); @@ -5822,7 +5845,8 @@ void QGraphicsScenePrivate::addView(QGraphicsView *view) { views << view; #ifndef QT_NO_GESTURES - foreach (Qt::GestureType gesture, grabbedGestures.keys()) + const auto gestures = grabbedGestures.keys(); + for (Qt::GestureType gesture : gestures) view->viewport()->grabGesture(gesture); #endif } @@ -5834,14 +5858,11 @@ void QGraphicsScenePrivate::removeView(QGraphicsView *view) void QGraphicsScenePrivate::updateTouchPointsForItem(QGraphicsItem *item, QTouchEvent *touchEvent) { - QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints(); - for (int i = 0; i < touchPoints.count(); ++i) { - QTouchEvent::TouchPoint &touchPoint = touchPoints[i]; + for (auto &touchPoint : touchEvent->_touchPoints) { touchPoint.setRect(item->mapFromScene(touchPoint.sceneRect()).boundingRect()); touchPoint.setStartPos(item->d_ptr->genericMapFromScene(touchPoint.startScenePos(), static_cast<QWidget *>(touchEvent->target()))); touchPoint.setLastPos(item->d_ptr->genericMapFromScene(touchPoint.lastScenePos(), static_cast<QWidget *>(touchEvent->target()))); } - touchEvent->setTouchPoints(touchPoints); } int QGraphicsScenePrivate::findClosestTouchPointId(const QPointF &scenePos) @@ -5881,7 +5902,7 @@ void QGraphicsScenePrivate::touchEventHandler(QTouchEvent *sceneTouchEvent) cachedItemsUnderMouse = itemsAtPosition(touchPoint.screenPos().toPoint(), touchPoint.scenePos(), static_cast<QWidget *>(sceneTouchEvent->target())); - item = cachedItemsUnderMouse.isEmpty() ? 0 : cachedItemsUnderMouse.first(); + item = cachedItemsUnderMouse.isEmpty() ? 0 : cachedItemsUnderMouse.constFirst(); } if (sceneTouchEvent->device()->type() == QTouchDevice::TouchScreen) { @@ -5992,7 +6013,7 @@ bool QGraphicsScenePrivate::sendTouchBeginEvent(QGraphicsItem *origin, QTouchEve { Q_Q(QGraphicsScene); - if (cachedItemsUnderMouse.isEmpty() || cachedItemsUnderMouse.first() != origin) { + if (cachedItemsUnderMouse.isEmpty() || cachedItemsUnderMouse.constFirst() != origin) { const QTouchEvent::TouchPoint &firstTouchPoint = touchEvent->touchPoints().first(); cachedItemsUnderMouse = itemsAtPosition(firstTouchPoint.screenPos().toPoint(), firstTouchPoint.scenePos(), @@ -6108,7 +6129,7 @@ void QGraphicsScenePrivate::enterModal(QGraphicsItem *panel, QGraphicsItem::Pane } if (!mouseGrabberItems.isEmpty() && lastMouseGrabberItemHasImplicitMouseGrab) { - QGraphicsItem *item = mouseGrabberItems.last(); + QGraphicsItem *item = mouseGrabberItems.constLast(); if (item->isBlockedByModalPanel()) ungrabMouse(item, /*itemIsDying =*/ false); } @@ -6167,7 +6188,7 @@ void QGraphicsScenePrivate::gestureTargetsAtHotSpots(const QSet<QGesture *> &ges QSet<QGesture *> *conflicts) { QSet<QGesture *> normalGestures; // that are not in conflicted state. - foreach (QGesture *gesture, gestures) { + for (QGesture *gesture : gestures) { if (!gesture->hasHotSpot()) continue; const Qt::GestureType gestureType = gesture->gestureType(); @@ -6215,7 +6236,7 @@ void QGraphicsScenePrivate::gestureEventHandler(QGestureEvent *event) if (!graphicsView) return; - QList<QGesture *> allGestures = event->gestures(); + const QList<QGesture *> allGestures = event->gestures(); DEBUG() << "QGraphicsScenePrivate::gestureEventHandler:" << "Gestures:" << allGestures; @@ -6223,7 +6244,7 @@ void QGraphicsScenePrivate::gestureEventHandler(QGestureEvent *event) QPoint delta = viewport->mapFromGlobal(QPoint()); QTransform toScene = QTransform::fromTranslate(delta.x(), delta.y()) * graphicsView->viewportTransform().inverted(); - foreach (QGesture *gesture, allGestures) { + for (QGesture *gesture : allGestures) { // cache scene coordinates of the hot spot if (gesture->hasHotSpot()) { gesture->d_func()->sceneHotSpot = toScene.map(gesture->hotSpot()); @@ -6258,7 +6279,7 @@ void QGraphicsScenePrivate::gestureEventHandler(QGestureEvent *event) QPointer<QGraphicsObject> item = cachedTargetItems.at(i); // get gestures to deliver to the current item - QSet<QGesture *> gestures = conflictedGestures & cachedItemGestures.value(item.data()); + const QSet<QGesture *> gestures = conflictedGestures & cachedItemGestures.value(item.data()); if (gestures.isEmpty()) continue; @@ -6271,11 +6292,11 @@ void QGraphicsScenePrivate::gestureEventHandler(QGestureEvent *event) ev.setWidget(event->widget()); // mark event and individual gestures as ignored ev.ignore(); - foreach(QGesture *g, gestures) + for (QGesture *g : gestures) ev.setAccepted(g, false); sendEvent(item.data(), &ev); // mark all accepted gestures to deliver them as normal gesture events - foreach (QGesture *g, gestures) { + for (QGesture *g : gestures) { if (ev.isAccepted() || ev.isAccepted(g)) { conflictedGestures.remove(g); // mark the item as a gesture target @@ -6310,7 +6331,8 @@ void QGraphicsScenePrivate::gestureEventHandler(QGestureEvent *event) QGraphicsObject *item = cachedTargetItems.at(i); // get gestures to deliver to the current item - foreach (QGesture *g, cachedItemGestures.value(item)) { + const auto gestures = cachedItemGestures.value(item); + for (QGesture *g : gestures) { if (!gestureTargets.contains(g)) { gestureTargets.insert(g, item); normalGestures.remove(g); @@ -6324,7 +6346,7 @@ void QGraphicsScenePrivate::gestureEventHandler(QGestureEvent *event) // deliver all gesture events QSet<QGesture *> undeliveredGestures; QSet<QGesture *> parentPropagatedGestures; - foreach (QGesture *gesture, allGestures) { + for (QGesture *gesture : allGestures) { if (QGraphicsObject *target = gestureTargets.value(gesture, 0)) { cachedItemGestures[target].insert(gesture); cachedTargetItems.append(target); @@ -6436,7 +6458,7 @@ void QGraphicsScenePrivate::gestureEventHandler(QGestureEvent *event) } // forget about targets for gestures that have ended - foreach (QGesture *g, allGestures) { + for (QGesture *g : allGestures) { switch (g->state()) { case Qt::GestureFinished: case Qt::GestureCanceled: @@ -6496,32 +6518,33 @@ void QGraphicsScenePrivate::cancelGesturesForChildren(QGesture *original) } Q_ASSERT(target); - QList<QGesture *> list = gestures.toList(); + const QList<QGesture *> list = gestures.toList(); QGestureEvent ev(list); sendEvent(target, &ev); - foreach (QGesture *g, list) { - if (ev.isAccepted() || ev.isAccepted(g)) - gestures.remove(g); - } + if (!ev.isAccepted()) { + for (QGesture *g : list) { - foreach (QGesture *g, gestures) { - if (!g->hasHotSpot()) - continue; + if (ev.isAccepted(g)) + continue; - QList<QGraphicsItem *> items = itemsAtPosition(QPoint(), g->d_func()->sceneHotSpot, 0); - for (int j = 0; j < items.size(); ++j) { - QGraphicsObject *item = items.at(j)->toGraphicsObject(); - if (!item) + if (!g->hasHotSpot()) continue; - QGraphicsItemPrivate *d = item->QGraphicsItem::d_func(); - if (d->gestureContext.contains(g->gestureType())) { - QList<QGesture *> list; - list << g; - QGestureEvent ev(list); - sendEvent(item, &ev); - if (ev.isAccepted() || ev.isAccepted(g)) - break; // successfully delivered + + QList<QGraphicsItem *> items = itemsAtPosition(QPoint(), g->d_func()->sceneHotSpot, 0); + for (int j = 0; j < items.size(); ++j) { + QGraphicsObject *item = items.at(j)->toGraphicsObject(); + if (!item) + continue; + QGraphicsItemPrivate *d = item->QGraphicsItem::d_func(); + if (d->gestureContext.contains(g->gestureType())) { + QList<QGesture *> list; + list << g; + QGestureEvent ev(list); + sendEvent(item, &ev); + if (ev.isAccepted() || ev.isAccepted(g)) + break; // successfully delivered + } } } } |