From 2d4295f16761a0e47b5218bbb1db5c14d16ce4fe Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 30 Nov 2015 12:19:31 +0100 Subject: QGraphicsView: replace some Q_FOREACH loops over const locals with C++11 range-for This needs to be handled a bit carefully, because Qt containers will detach upon being iterated over using range-for. In the cases of this patch, that cannot happen, because all containers are local and marked as const (either by this patch or before). Separate patches will deal with other situations. Range-for loops are much more efficient than foreach loops. This patch shaves ~1.8KiB of text size off an optimized Linux AMD64 GCC 4.9 build. Change-Id: I5c58658937ac4323594161bf94a2fce3c5667914 Reviewed-by: Olivier Goffart (Woboq GmbH) --- .../graphicsview/qgraphicsanchorlayout_p.cpp | 2 +- src/widgets/graphicsview/qgraphicsscene.cpp | 30 +++++++++++----------- src/widgets/graphicsview/qgraphicsview.cpp | 6 ++--- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/widgets/graphicsview/qgraphicsanchorlayout_p.cpp b/src/widgets/graphicsview/qgraphicsanchorlayout_p.cpp index adc77ded47..e8501474a5 100644 --- a/src/widgets/graphicsview/qgraphicsanchorlayout_p.cpp +++ b/src/widgets/graphicsview/qgraphicsanchorlayout_p.cpp @@ -2588,7 +2588,7 @@ void QGraphicsAnchorLayoutPrivate::identifyFloatItems(const QSet & { QSet nonFloating; - foreach (const AnchorData *ad, visited) + for (const AnchorData *ad : visited) identifyNonFloatItems_helper(ad, &nonFloating); QSet allItems; diff --git a/src/widgets/graphicsview/qgraphicsscene.cpp b/src/widgets/graphicsview/qgraphicsscene.cpp index 3993138f3f..80c0aa1a30 100644 --- a/src/widgets/graphicsview/qgraphicsscene.cpp +++ b/src/widgets/graphicsview/qgraphicsscene.cpp @@ -2437,7 +2437,7 @@ QGraphicsItemGroup *QGraphicsScene::createItemGroup(const QList QGraphicsItemGroup *group = new QGraphicsItemGroup(commonAncestor); if (!commonAncestor) addItem(group); - foreach (QGraphicsItem *item, items) + for (QGraphicsItem *item : items) group->addToGroup(item); return group; } @@ -4149,9 +4149,9 @@ void QGraphicsScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent) void QGraphicsScene::wheelEvent(QGraphicsSceneWheelEvent *wheelEvent) { Q_D(QGraphicsScene); - QList wheelCandidates = d->itemsAtPosition(wheelEvent->screenPos(), - wheelEvent->scenePos(), - wheelEvent->widget()); + const QList 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 @@ -4173,7 +4173,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(item)->focusPolicy() == Qt::WheelFocus) { @@ -6181,7 +6181,7 @@ void QGraphicsScenePrivate::gestureTargetsAtHotSpots(const QSet &ges QSet *conflicts) { QSet 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(); @@ -6229,7 +6229,7 @@ void QGraphicsScenePrivate::gestureEventHandler(QGestureEvent *event) if (!graphicsView) return; - QList allGestures = event->gestures(); + const QList allGestures = event->gestures(); DEBUG() << "QGraphicsScenePrivate::gestureEventHandler:" << "Gestures:" << allGestures; @@ -6237,7 +6237,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()); @@ -6272,7 +6272,7 @@ void QGraphicsScenePrivate::gestureEventHandler(QGestureEvent *event) QPointer item = cachedTargetItems.at(i); // get gestures to deliver to the current item - QSet gestures = conflictedGestures & cachedItemGestures.value(item.data()); + const QSet gestures = conflictedGestures & cachedItemGestures.value(item.data()); if (gestures.isEmpty()) continue; @@ -6285,11 +6285,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 @@ -6339,7 +6339,7 @@ void QGraphicsScenePrivate::gestureEventHandler(QGestureEvent *event) // deliver all gesture events QSet undeliveredGestures; QSet parentPropagatedGestures; - foreach (QGesture *gesture, allGestures) { + for (QGesture *gesture : allGestures) { if (QGraphicsObject *target = gestureTargets.value(gesture, 0)) { cachedItemGestures[target].insert(gesture); cachedTargetItems.append(target); @@ -6451,7 +6451,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: @@ -6511,12 +6511,12 @@ void QGraphicsScenePrivate::cancelGesturesForChildren(QGesture *original) } Q_ASSERT(target); - QList list = gestures.toList(); + const QList list = gestures.toList(); QGestureEvent ev(list); sendEvent(target, &ev); if (!ev.isAccepted()) { - foreach (QGesture *g, list) { + for (QGesture *g : list) { if (ev.isAccepted(g)) continue; diff --git a/src/widgets/graphicsview/qgraphicsview.cpp b/src/widgets/graphicsview/qgraphicsview.cpp index 928d826323..ad4733145a 100644 --- a/src/widgets/graphicsview/qgraphicsview.cpp +++ b/src/widgets/graphicsview/qgraphicsview.cpp @@ -2467,7 +2467,7 @@ QPolygonF QGraphicsView::mapToScene(const QPolygon &polygon) const { QPolygonF poly; poly.reserve(polygon.count()); - foreach (const QPoint &point, polygon) + for (const QPoint &point : polygon) poly << mapToScene(point); return poly; } @@ -2563,7 +2563,7 @@ QPolygon QGraphicsView::mapFromScene(const QPolygonF &polygon) const { QPolygon poly; poly.reserve(polygon.count()); - foreach (const QPointF &point, polygon) + for (const QPointF &point : polygon) poly << mapFromScene(point); return poly; } @@ -2696,7 +2696,7 @@ void QGraphicsView::updateScene(const QList &rects) QTransform transform = viewportTransform(); // Convert scene rects to viewport rects. - foreach (const QRectF &rect, rects) { + for (const QRectF &rect : rects) { QRect xrect = transform.mapRect(rect).toAlignedRect(); if (!(d->optimizationFlags & DontAdjustForAntialiasing)) xrect.adjust(-2, -2, 2, 2); -- cgit v1.2.3