From ba2221bd7314c42353cd7ab2895c043d06d837ac Mon Sep 17 00:00:00 2001 From: Anton Kudryavtsev Date: Thu, 12 Apr 2018 18:31:41 +0300 Subject: [PATCH] Widgets: use range-based for instead of foreach Change-Id: Id9ec2db6cfa661ff9b3b6ace9ffaa071a2d57f94 Reviewed-by: Edward Welbourne Reviewed-by: Lars Knoll --- src/widgets/kernel/qapplication.cpp | 2 +- src/widgets/kernel/qgesture.cpp | 4 +- src/widgets/util/qscroller.cpp | 4 +- src/widgets/widgets/qdockarealayout.cpp | 12 ++--- src/widgets/widgets/qmainwindowlayout.cpp | 55 +++++++++++++---------- src/widgets/widgets/qmdiarea.cpp | 4 +- src/widgets/widgets/qmenu.cpp | 2 +- src/widgets/widgets/qtextbrowser.cpp | 2 +- 8 files changed, 47 insertions(+), 38 deletions(-) diff --git a/src/widgets/kernel/qapplication.cpp b/src/widgets/kernel/qapplication.cpp index 39540cf898..c9933aff8a 100644 --- a/src/widgets/kernel/qapplication.cpp +++ b/src/widgets/kernel/qapplication.cpp @@ -3637,7 +3637,7 @@ bool QApplication::notify(QObject *receiver, QEvent *e) break; w = w->parentWidget(); } - foreach (QGesture *g, allGestures) + for (QGesture *g : qAsConst(allGestures)) gestureEvent->setAccepted(g, false); gestureEvent->m_accept = false; // to make sure we check individual gestures } else { diff --git a/src/widgets/kernel/qgesture.cpp b/src/widgets/kernel/qgesture.cpp index 7f8bf18e90..fc715687c6 100644 --- a/src/widgets/kernel/qgesture.cpp +++ b/src/widgets/kernel/qgesture.cpp @@ -916,7 +916,7 @@ QGesture *QGestureEvent::gesture(Qt::GestureType type) const QList QGestureEvent::activeGestures() const { QList gestures; - foreach (QGesture *gesture, m_gestures) { + for (QGesture *gesture : m_gestures) { if (gesture->state() != Qt::GestureCanceled) gestures.append(gesture); } @@ -929,7 +929,7 @@ QList QGestureEvent::activeGestures() const QList QGestureEvent::canceledGestures() const { QList gestures; - foreach (QGesture *gesture, m_gestures) { + for (QGesture *gesture : m_gestures) { if (gesture->state() == Qt::GestureCanceled) gestures.append(gesture); } diff --git a/src/widgets/util/qscroller.cpp b/src/widgets/util/qscroller.cpp index 903a141e49..28504f5631 100644 --- a/src/widgets/util/qscroller.cpp +++ b/src/widgets/util/qscroller.cpp @@ -1894,7 +1894,7 @@ qreal QScrollerPrivate::nextSnapPos(qreal p, int dir, Qt::Orientation orientatio if (orientation == Qt::Horizontal) { // the snap points in the list - foreach (qreal snapPos, snapPositionsX) { + for (qreal snapPos : snapPositionsX) { qreal snapPosDist = snapPos - p; if ((dir > 0 && snapPosDist < 0) || (dir < 0 && snapPosDist > 0)) @@ -1941,7 +1941,7 @@ qreal QScrollerPrivate::nextSnapPos(qreal p, int dir, Qt::Orientation orientatio } else { // (orientation == Qt::Vertical) // the snap points in the list - foreach (qreal snapPos, snapPositionsY) { + for (qreal snapPos : snapPositionsY) { qreal snapPosDist = snapPos - p; if ((dir > 0 && snapPosDist < 0) || (dir < 0 && snapPosDist > 0)) diff --git a/src/widgets/widgets/qdockarealayout.cpp b/src/widgets/widgets/qdockarealayout.cpp index 71726eaeee..9c216738c0 100644 --- a/src/widgets/widgets/qdockarealayout.cpp +++ b/src/widgets/widgets/qdockarealayout.cpp @@ -2628,8 +2628,9 @@ void QDockAreaLayout::removePlaceHolder(const QString &name) QList index = indexOfPlaceHolder(name); if (!index.isEmpty()) remove(index); - foreach (QDockWidgetGroupWindow *dwgw, mainWindow->findChildren( - QString(), Qt::FindDirectChildrenOnly)) { + const auto groups = + mainWindow->findChildren(QString(), Qt::FindDirectChildrenOnly); + for (QDockWidgetGroupWindow *dwgw : groups) { index = dwgw->layoutInfo()->indexOfPlaceHolder(name); if (!index.isEmpty()) { dwgw->layoutInfo()->remove(index); @@ -3064,8 +3065,9 @@ QRect QDockAreaLayout::constrainedRect(QRect rect, QWidget* widget) bool QDockAreaLayout::restoreDockWidget(QDockWidget *dockWidget) { QDockAreaLayoutItem *item = 0; - foreach (QDockWidgetGroupWindow *dwgw, mainWindow->findChildren( - QString(), Qt::FindDirectChildrenOnly)) { + const auto groups = + mainWindow->findChildren(QString(), Qt::FindDirectChildrenOnly); + for (QDockWidgetGroupWindow *dwgw : groups) { QList index = dwgw->layoutInfo()->indexOfPlaceHolder(dockWidget->objectName()); if (!index.isEmpty()) { dockWidget->setParent(dwgw); @@ -3174,7 +3176,7 @@ void QDockAreaLayout::resizeDocks(const QList &docks, if (!info->tabbed && info->o == o) { info->item_list[path.constLast()].size = size; int totalSize = 0; - foreach (const QDockAreaLayoutItem &item, info->item_list) { + for (const QDockAreaLayoutItem &item : qAsConst(info->item_list)) { if (!item.skip()) { if (totalSize != 0) totalSize += sep; diff --git a/src/widgets/widgets/qmainwindowlayout.cpp b/src/widgets/widgets/qmainwindowlayout.cpp index d4ce0ffa4c..0297172100 100644 --- a/src/widgets/widgets/qmainwindowlayout.cpp +++ b/src/widgets/widgets/qmainwindowlayout.cpp @@ -423,7 +423,8 @@ void QDockWidgetGroupWindow::destroyOrHideIfEmpty() } // Make sure to reparent the possibly floating or hidden QDockWidgets to the parent - foreach (QDockWidget *dw, findChildren(QString(), Qt::FindDirectChildrenOnly)) { + const auto dockWidgets = findChildren(QString(), Qt::FindDirectChildrenOnly); + for (QDockWidget *dw : dockWidgets) { bool wasFloating = dw->isFloating(); bool wasHidden = dw->isHidden(); dw->setParent(parentWidget()); @@ -442,7 +443,8 @@ void QDockWidgetGroupWindow::destroyOrHideIfEmpty() dw->show(); } #if QT_CONFIG(tabbar) - foreach (QTabBar *tb, findChildren(QString(), Qt::FindDirectChildrenOnly)) + const auto tabBars = findChildren(QString(), Qt::FindDirectChildrenOnly); + for (QTabBar *tb : tabBars) tb->setParent(parentWidget()); #endif deleteLater(); @@ -1034,10 +1036,10 @@ void QMainWindowLayoutState::saveState(QDataStream &stream) const #if QT_CONFIG(dockwidget) dockAreaLayout.saveState(stream); #if QT_CONFIG(tabbar) - QList floatingTabs = + const QList floatingTabs = mainWindow->findChildren(QString(), Qt::FindDirectChildrenOnly); - foreach (QDockWidgetGroupWindow *floating, floatingTabs) { + for (QDockWidgetGroupWindow *floating : floatingTabs) { if (floating->layoutInfo()->isEmpty()) continue; stream << uchar(QDockAreaLayout::FloatingDockWidgetTabMarker) << floating->geometry(); @@ -1525,9 +1527,9 @@ void QMainWindowLayout::setDocumentMode(bool enabled) _documentMode = enabled; // Update the document mode for all tab bars - foreach (QTabBar *bar, usedTabBars) + for (QTabBar *bar : qAsConst(usedTabBars)) bar->setDocumentMode(_documentMode); - foreach (QTabBar *bar, unusedTabBars) + for (QTabBar *bar : qAsConst(unusedTabBars)) bar->setDocumentMode(_documentMode); } #endif // QT_CONFIG(tabbar) @@ -1806,8 +1808,9 @@ QDockAreaLayoutInfo *QMainWindowLayout::dockInfo(QWidget *widget) QDockAreaLayoutInfo *info = layoutState.dockAreaLayout.info(widget); if (info) return info; - foreach (QDockWidgetGroupWindow *dwgw, - parent()->findChildren(QString(), Qt::FindDirectChildrenOnly)) { + const auto groups = + parent()->findChildren(QString(), Qt::FindDirectChildrenOnly); + for (QDockWidgetGroupWindow *dwgw : groups) { info = dwgw->layoutInfo()->info(widget); if (info) return info; @@ -2074,8 +2077,9 @@ bool QMainWindowLayout::plug(QLayoutItem *widgetItem) layoutState.remove(previousPath); previousPath = currentHoveredFloat->layoutInfo()->indexOf(widget); // Let's remove the widget from any possible group window - foreach (QDockWidgetGroupWindow *dwgw, - parent()->findChildren(QString(), Qt::FindDirectChildrenOnly)) { + const auto groups = + parent()->findChildren(QString(), Qt::FindDirectChildrenOnly); + for (QDockWidgetGroupWindow *dwgw : groups) { if (dwgw == currentHoveredFloat) continue; QList path = dwgw->layoutInfo()->indexOf(widget); @@ -2103,8 +2107,9 @@ bool QMainWindowLayout::plug(QLayoutItem *widgetItem) #if QT_CONFIG(dockwidget) // Let's remove the widget from any possible group window - foreach (QDockWidgetGroupWindow *dwgw, - parent()->findChildren(QString(), Qt::FindDirectChildrenOnly)) { + const auto groups = + parent()->findChildren(QString(), Qt::FindDirectChildrenOnly); + for (QDockWidgetGroupWindow *dwgw : groups) { QList path = dwgw->layoutInfo()->indexOf(widget); if (!path.isEmpty()) dwgw->layoutInfo()->remove(path); @@ -2246,7 +2251,7 @@ void QMainWindowLayout::animationFinished(QWidget *widget) #if QT_CONFIG(dockwidget) parentWidget()->update(layoutState.dockAreaLayout.separatorRegion()); #if QT_CONFIG(tabbar) - foreach (QTabBar *tab_bar, usedTabBars) + for (QTabBar *tab_bar : qAsConst(usedTabBars)) tab_bar->show(); #endif // QT_CONFIG(tabbar) #endif // QT_CONFIG(dockwidget) @@ -2530,7 +2535,8 @@ void QMainWindowLayout::hover(QLayoutItem *widgetItem, const QPoint &mousePos) // Check if we are over another floating dock widget QVarLengthArray candidates; - foreach (QObject *c, parentWidget()->children()) { + const auto siblings = parentWidget()->children(); + for (QObject *c : siblings) { QWidget *w = qobject_cast(c); if (!w) continue; @@ -2540,7 +2546,8 @@ void QMainWindowLayout::hover(QLayoutItem *widgetItem, const QPoint &mousePos) candidates << w; if (QDockWidgetGroupWindow *group = qobject_cast(w)) { // Sometimes, there are floating QDockWidget that have a QDockWidgetGroupWindow as a parent. - foreach (QObject *c, group->children()) { + const auto groupChildren = group->children(); + for (QObject *c : groupChildren) { if (QDockWidget *dw = qobject_cast(c)) { if (dw != widget && dw->isFloating() && dw->isVisible() && !dw->isMinimized()) candidates << dw; @@ -2668,14 +2675,14 @@ void QMainWindowLayout::applyState(QMainWindowLayoutState &newState, bool animat { #if QT_CONFIG(dockwidget) && QT_CONFIG(tabwidget) QSet used = newState.dockAreaLayout.usedTabBars(); - foreach (QDockWidgetGroupWindow *dwgw, - parent()->findChildren(QString(), Qt::FindDirectChildrenOnly)) { + const auto groups = + parent()->findChildren(QString(), Qt::FindDirectChildrenOnly); + for (QDockWidgetGroupWindow *dwgw : groups) used += dwgw->layoutInfo()->usedTabBars(); - } - QSet retired = usedTabBars - used; + const QSet retired = usedTabBars - used; usedTabBars = used; - foreach (QTabBar *tab_bar, retired) { + for (QTabBar *tab_bar : retired) { tab_bar->hide(); while (tab_bar->count() > 0) tab_bar->removeTab(0); @@ -2683,10 +2690,10 @@ void QMainWindowLayout::applyState(QMainWindowLayoutState &newState, bool animat } if (sep == 1) { - QSet usedSeps = newState.dockAreaLayout.usedSeparatorWidgets(); - QSet retiredSeps = usedSeparatorWidgets - usedSeps; + const QSet usedSeps = newState.dockAreaLayout.usedSeparatorWidgets(); + const QSet retiredSeps = usedSeparatorWidgets - usedSeps; usedSeparatorWidgets = usedSeps; - foreach (QWidget *sepWidget, retiredSeps) { + for (QWidget *sepWidget : retiredSeps) { unusedSeparatorWidgets.append(sepWidget); } } @@ -2728,7 +2735,7 @@ bool QMainWindowLayout::restoreState(QDataStream &stream) #if QT_CONFIG(dockwidget) if (parentWidget()->isVisible()) { #if QT_CONFIG(tabbar) - foreach (QTabBar *tab_bar, usedTabBars) + for (QTabBar *tab_bar : qAsConst(usedTabBars)) tab_bar->show(); #endif diff --git a/src/widgets/widgets/qmdiarea.cpp b/src/widgets/widgets/qmdiarea.cpp index 45c01dec80..8639f57ea1 100644 --- a/src/widgets/widgets/qmdiarea.cpp +++ b/src/widgets/widgets/qmdiarea.cpp @@ -471,8 +471,8 @@ QVector MinOverlapPlacer::getCandidatePlacements(const QSize &size, const ylist.erase(std::unique(ylist.begin(), ylist.end()), ylist.end()); result.reserve(ylist.size() * xlist.size()); - foreach (int y, ylist) - foreach (int x, xlist) + for (int y : qAsConst(ylist)) + for (int x : qAsConst(xlist)) result << QRect(QPoint(x, y), size); return result; } diff --git a/src/widgets/widgets/qmenu.cpp b/src/widgets/widgets/qmenu.cpp index 363647aee0..96635ae505 100644 --- a/src/widgets/widgets/qmenu.cpp +++ b/src/widgets/widgets/qmenu.cpp @@ -2433,7 +2433,7 @@ void QMenu::popup(const QPoint &p, QAction *atAction) atAction = d->defaultAction; // TODO: This works for first level menus, not yet sub menus } else { - foreach (QAction *action, d->actions) + for (QAction *action : qAsConst(d->actions)) if (action->isEnabled()) { atAction = action; break; diff --git a/src/widgets/widgets/qtextbrowser.cpp b/src/widgets/widgets/qtextbrowser.cpp index b1cb556505..46b973bae7 100644 --- a/src/widgets/widgets/qtextbrowser.cpp +++ b/src/widgets/widgets/qtextbrowser.cpp @@ -169,7 +169,7 @@ QString QTextBrowserPrivate::findFile(const QUrl &name) const if (QFileInfo(fileName).isAbsolute()) return fileName; - foreach (QString path, searchPaths) { + for (QString path : qAsConst(searchPaths)) { if (!path.endsWith(QLatin1Char('/'))) path.append(QLatin1Char('/')); path.append(fileName);