From 26f7edb09eaf66590de9a3603489464fd868eedc Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Fri, 7 Dec 2018 11:59:02 +0100 Subject: [PATCH] Cleanup Widgets examples - foreach Cleanup the Widgets examples - replace foreach with range-based for loop in graphicsview subdirectory Change-Id: I9093b3ae89d73d0b860d29929943881c90074bce Reviewed-by: Luca Beldi Reviewed-by: Paul Wicking --- .../widgets/graphicsview/boxes/glbuffers.cpp | 4 +- .../widgets/graphicsview/boxes/glbuffers.h | 2 +- examples/widgets/graphicsview/boxes/qtbox.cpp | 24 +++----- examples/widgets/graphicsview/boxes/scene.cpp | 56 +++++++------------ .../graphicsview/collidingmice/mouse.cpp | 11 ++-- .../graphicsview/diagramscene/diagramitem.cpp | 5 +- .../diagramscene/diagramscene.cpp | 10 ++-- .../graphicsview/diagramscene/diagramscene.h | 2 +- .../graphicsview/diagramscene/mainwindow.cpp | 22 ++++---- .../graphicsview/elasticnodes/graphwidget.cpp | 10 ++-- .../graphicsview/elasticnodes/node.cpp | 7 ++- .../embeddeddialogs/embeddeddialog.cpp | 6 +- .../graphicsview/flowlayout/flowlayout.cpp | 9 +-- 13 files changed, 74 insertions(+), 94 deletions(-) diff --git a/examples/widgets/graphicsview/boxes/glbuffers.cpp b/examples/widgets/graphicsview/boxes/glbuffers.cpp index 851cd17796..b52b26c4ef 100644 --- a/examples/widgets/graphicsview/boxes/glbuffers.cpp +++ b/examples/widgets/graphicsview/boxes/glbuffers.cpp @@ -218,14 +218,14 @@ GLTextureCube::GLTextureCube(int size) glBindTexture(GL_TEXTURE_CUBE_MAP, 0); } -GLTextureCube::GLTextureCube(const QStringList& fileNames, int size) +GLTextureCube::GLTextureCube(const QStringList &fileNames, int size) { // TODO: Add error handling. glBindTexture(GL_TEXTURE_CUBE_MAP, m_texture); int index = 0; - foreach (QString file, fileNames) { + for (const QString &file : fileNames) { QImage image(file); if (image.isNull()) { m_failed = true; diff --git a/examples/widgets/graphicsview/boxes/glbuffers.h b/examples/widgets/graphicsview/boxes/glbuffers.h index 03c24a91d3..e2363d561e 100644 --- a/examples/widgets/graphicsview/boxes/glbuffers.h +++ b/examples/widgets/graphicsview/boxes/glbuffers.h @@ -129,7 +129,7 @@ class GLTextureCube : public GLTexture { public: GLTextureCube(int size); - explicit GLTextureCube(const QStringList& fileNames, int size = 0); + explicit GLTextureCube(const QStringList &fileNames, int size = 0); void load(int size, int face, QRgb *data); void bind() override; void unbind() override; diff --git a/examples/widgets/graphicsview/boxes/qtbox.cpp b/examples/widgets/graphicsview/boxes/qtbox.cpp index 3a184dd0b6..68d5c251f4 100644 --- a/examples/widgets/graphicsview/boxes/qtbox.cpp +++ b/examples/widgets/graphicsview/boxes/qtbox.cpp @@ -127,10 +127,8 @@ void ItemBase::duplicateSelectedItems(QGraphicsScene *scene) if (!scene) return; - QList selected; - selected = scene->selectedItems(); - - foreach (QGraphicsItem *item, selected) { + const QList selected = scene->selectedItems(); + for (QGraphicsItem *item : selected) { ItemBase *itemBase = qgraphicsitem_cast(item); if (itemBase) scene->addItem(itemBase->createNew(itemBase->m_size, itemBase->pos().x() + itemBase->m_size, itemBase->pos().y())); @@ -142,10 +140,8 @@ void ItemBase::deleteSelectedItems(QGraphicsScene *scene) if (!scene) return; - QList selected; - selected = scene->selectedItems(); - - foreach (QGraphicsItem *item, selected) { + const QList selected = scene->selectedItems(); + for (QGraphicsItem *item : selected) { ItemBase *itemBase = qgraphicsitem_cast(item); if (itemBase) delete itemBase; @@ -157,10 +153,8 @@ void ItemBase::growSelectedItems(QGraphicsScene *scene) if (!scene) return; - QList selected; - selected = scene->selectedItems(); - - foreach (QGraphicsItem *item, selected) { + const QList selected = scene->selectedItems(); + for (QGraphicsItem *item : selected) { ItemBase *itemBase = qgraphicsitem_cast(item); if (itemBase) { itemBase->prepareGeometryChange(); @@ -176,10 +170,8 @@ void ItemBase::shrinkSelectedItems(QGraphicsScene *scene) if (!scene) return; - QList selected; - selected = scene->selectedItems(); - - foreach (QGraphicsItem *item, selected) { + const QList selected = scene->selectedItems(); + for (QGraphicsItem *item : selected) { ItemBase *itemBase = qgraphicsitem_cast(item); if (itemBase) { itemBase->prepareGeometryChange(); diff --git a/examples/widgets/graphicsview/boxes/scene.cpp b/examples/widgets/graphicsview/boxes/scene.cpp index 3c65206c02..7f62ac894b 100644 --- a/examples/widgets/graphicsview/boxes/scene.cpp +++ b/examples/widgets/graphicsview/boxes/scene.cpp @@ -311,15 +311,13 @@ RenderOptionsDialog::RenderOptionsDialog() layout->addWidget(check, 0, 0, 1, 2); ++row; - QPalette palette; - // Load all .par files // .par files have a simple syntax for specifying user adjustable uniform variables. - QSet uniforms; - QList filter = QStringList("*.par"); - QList files = QDir(":/res/boxes/").entryInfoList(filter, QDir::Files | QDir::Readable); + const QList files = QDir(QStringLiteral(":/res/boxes/")) + .entryInfoList({ QStringLiteral("*.par") }, + QDir::Files | QDir::Readable); - foreach (QFileInfo fileInfo, files) { + for (const QFileInfo &fileInfo : files) { QFile file(fileInfo.absoluteFilePath()); if (file.open(QIODevice::ReadOnly)) { while (!file.atEnd()) { @@ -404,7 +402,7 @@ int RenderOptionsDialog::addShader(const QString &name) void RenderOptionsDialog::emitParameterChanged() { - foreach (ParameterEdit *edit, m_parameterEdits) + for (ParameterEdit *edit : qAsConst(m_parameterEdits)) edit->emitChange(); } @@ -541,24 +539,15 @@ Scene::Scene(int width, int height, int maxTextureSize) Scene::~Scene() { - if (m_box) - delete m_box; - foreach (GLTexture *texture, m_textures) - if (texture) delete texture; - if (m_mainCubemap) - delete m_mainCubemap; - foreach (QGLShaderProgram *program, m_programs) - if (program) delete program; - if (m_vertexShader) - delete m_vertexShader; - foreach (QGLShader *shader, m_fragmentShaders) - if (shader) delete shader; - foreach (GLRenderTargetCube *rt, m_cubemaps) - if (rt) delete rt; - if (m_environmentShader) - delete m_environmentShader; - if (m_environmentProgram) - delete m_environmentProgram; + delete m_box; + qDeleteAll(m_textures); + delete m_mainCubemap; + qDeleteAll(m_programs); + delete m_vertexShader; + qDeleteAll(m_fragmentShaders); + qDeleteAll(m_cubemaps); + delete m_environmentShader; + delete m_environmentProgram; } void Scene::initGL() @@ -603,15 +592,13 @@ void Scene::initGL() m_mainCubemap = new GLRenderTargetCube(512); - QStringList filter; QList files; // Load all .png files as textures m_currentTexture = 0; - filter = QStringList("*.png"); - files = QDir(":/res/boxes/").entryInfoList(filter, QDir::Files | QDir::Readable); + files = QDir(":/res/boxes/").entryInfoList({ QStringLiteral("*.png") }, QDir::Files | QDir::Readable); - foreach (QFileInfo file, files) { + for (const QFileInfo &file : qAsConst(files)) { GLTexture *texture = new GLTexture2D(file.absoluteFilePath(), qMin(256, m_maxTextureSize), qMin(256, m_maxTextureSize)); if (texture->failed()) { delete texture; @@ -626,9 +613,8 @@ void Scene::initGL() // Load all .fsh files as fragment shaders m_currentShader = 0; - filter = QStringList("*.fsh"); - files = QDir(":/res/boxes/").entryInfoList(filter, QDir::Files | QDir::Readable); - foreach (QFileInfo file, files) { + files = QDir(":/res/boxes/").entryInfoList({ QStringLiteral("*.fsh") }, QDir::Files | QDir::Readable); + for (const QFileInfo &file : qAsConst(files)) { QGLShaderProgram *program = new QGLShaderProgram; QGLShader* shader = new QGLShader(QGLShader::Fragment); shader->compileSourceFile(file.absoluteFilePath()); @@ -664,7 +650,7 @@ void Scene::initGL() m_renderOptions->emitParameterChanged(); } -static void loadMatrix(const QMatrix4x4& m) +static void loadMatrix(const QMatrix4x4 &m) { // static to prevent glLoadMatrixf to fail on certain drivers static GLfloat mat[16]; @@ -1053,7 +1039,7 @@ void Scene::toggleDynamicCubemap(int state) void Scene::setColorParameter(const QString &name, QRgb color) { // set the color in all programs - foreach (QGLShaderProgram *program, m_programs) { + for (QGLShaderProgram *program : qAsConst(m_programs)) { program->bind(); program->setUniformValue(program->uniformLocation(name), QColor(color)); program->release(); @@ -1063,7 +1049,7 @@ void Scene::setColorParameter(const QString &name, QRgb color) void Scene::setFloatParameter(const QString &name, float value) { // set the color in all programs - foreach (QGLShaderProgram *program, m_programs) { + for (QGLShaderProgram *program : qAsConst(m_programs)) { program->bind(); program->setUniformValue(program->uniformLocation(name), value); program->release(); diff --git a/examples/widgets/graphicsview/collidingmice/mouse.cpp b/examples/widgets/graphicsview/collidingmice/mouse.cpp index 14f887f6e3..a8fcc05d76 100644 --- a/examples/widgets/graphicsview/collidingmice/mouse.cpp +++ b/examples/widgets/graphicsview/collidingmice/mouse.cpp @@ -160,11 +160,12 @@ void Mouse::advance(int step) // Try not to crash with any other mice //! [7] - QList dangerMice = scene()->items(QPolygonF() - << mapToScene(0, 0) - << mapToScene(-30, -50) - << mapToScene(30, -50)); - foreach (QGraphicsItem *item, dangerMice) { + const QList dangerMice = scene()->items(QPolygonF() + << mapToScene(0, 0) + << mapToScene(-30, -50) + << mapToScene(30, -50)); + + for (const QGraphicsItem *item : dangerMice) { if (item == this) continue; diff --git a/examples/widgets/graphicsview/diagramscene/diagramitem.cpp b/examples/widgets/graphicsview/diagramscene/diagramitem.cpp index db6dabee56..3aa685635e 100644 --- a/examples/widgets/graphicsview/diagramscene/diagramitem.cpp +++ b/examples/widgets/graphicsview/diagramscene/diagramitem.cpp @@ -111,7 +111,7 @@ void DiagramItem::removeArrow(Arrow *arrow) //! [2] void DiagramItem::removeArrows() { - foreach (Arrow *arrow, arrows) { + for (Arrow *arrow : qAsConst(arrows)) { arrow->startItem()->removeArrow(arrow); arrow->endItem()->removeArrow(arrow); scene()->removeItem(arrow); @@ -154,9 +154,8 @@ void DiagramItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) QVariant DiagramItem::itemChange(GraphicsItemChange change, const QVariant &value) { if (change == QGraphicsItem::ItemPositionChange) { - foreach (Arrow *arrow, arrows) { + for (Arrow *arrow : qAsConst(arrows)) arrow->updatePosition(); - } } return value; diff --git a/examples/widgets/graphicsview/diagramscene/diagramscene.cpp b/examples/widgets/graphicsview/diagramscene/diagramscene.cpp index 51d855cc75..40272834c3 100644 --- a/examples/widgets/graphicsview/diagramscene/diagramscene.cpp +++ b/examples/widgets/graphicsview/diagramscene/diagramscene.cpp @@ -234,12 +234,10 @@ void DiagramScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) //! [13] //! [14] -bool DiagramScene::isItemChange(int type) +bool DiagramScene::isItemChange(int type) const { - foreach (QGraphicsItem *item, selectedItems()) { - if (item->type() == type) - return true; - } - return false; + const QList items = selectedItems(); + const auto cb = [type](const QGraphicsItem *item) { return item->type() == type; }; + return std::find_if(items.begin(), items.end(), cb) != items.end(); } //! [14] diff --git a/examples/widgets/graphicsview/diagramscene/diagramscene.h b/examples/widgets/graphicsview/diagramscene/diagramscene.h index 95f7f3c621..15063d58b7 100644 --- a/examples/widgets/graphicsview/diagramscene/diagramscene.h +++ b/examples/widgets/graphicsview/diagramscene/diagramscene.h @@ -100,7 +100,7 @@ protected: void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) override; private: - bool isItemChange(int type); + bool isItemChange(int type) const; DiagramItem::DiagramType myItemType; QMenu *myItemMenu; diff --git a/examples/widgets/graphicsview/diagramscene/mainwindow.cpp b/examples/widgets/graphicsview/diagramscene/mainwindow.cpp index 875c41b284..07f92f61c1 100644 --- a/examples/widgets/graphicsview/diagramscene/mainwindow.cpp +++ b/examples/widgets/graphicsview/diagramscene/mainwindow.cpp @@ -92,8 +92,8 @@ MainWindow::MainWindow() //! [1] void MainWindow::backgroundButtonGroupClicked(QAbstractButton *button) { - QList buttons = backgroundButtonGroup->buttons(); - foreach (QAbstractButton *myButton, buttons) { + const QList buttons = backgroundButtonGroup->buttons(); + for (QAbstractButton *myButton : buttons) { if (myButton != button) button->setChecked(false); } @@ -115,8 +115,8 @@ void MainWindow::backgroundButtonGroupClicked(QAbstractButton *button) //! [2] void MainWindow::buttonGroupClicked(int id) { - QList buttons = buttonGroup->buttons(); - foreach (QAbstractButton *button, buttons) { + const QList buttons = buttonGroup->buttons(); + for (QAbstractButton *button : buttons) { if (buttonGroup->button(id) != button) button->setChecked(false); } @@ -132,7 +132,8 @@ void MainWindow::buttonGroupClicked(int id) //! [3] void MainWindow::deleteItem() { - foreach (QGraphicsItem *item, scene->selectedItems()) { + QList selectedItems = scene->selectedItems(); + for (QGraphicsItem *item : qAsConst(selectedItems)) { if (item->type() == Arrow::Type) { scene->removeItem(item); Arrow *arrow = qgraphicsitem_cast(item); @@ -142,7 +143,8 @@ void MainWindow::deleteItem() } } - foreach (QGraphicsItem *item, scene->selectedItems()) { + selectedItems = scene->selectedItems(); + for (QGraphicsItem *item : qAsConst(selectedItems)) { if (item->type() == DiagramItem::Type) qgraphicsitem_cast(item)->removeArrows(); scene->removeItem(item); @@ -165,10 +167,10 @@ void MainWindow::bringToFront() return; QGraphicsItem *selectedItem = scene->selectedItems().first(); - QList overlapItems = selectedItem->collidingItems(); + const QList overlapItems = selectedItem->collidingItems(); qreal zValue = 0; - foreach (QGraphicsItem *item, overlapItems) { + for (const QGraphicsItem *item : overlapItems) { if (item->zValue() >= zValue && item->type() == DiagramItem::Type) zValue = item->zValue() + 0.1; } @@ -183,10 +185,10 @@ void MainWindow::sendToBack() return; QGraphicsItem *selectedItem = scene->selectedItems().first(); - QList overlapItems = selectedItem->collidingItems(); + const QList overlapItems = selectedItem->collidingItems(); qreal zValue = 0; - foreach (QGraphicsItem *item, overlapItems) { + for (const QGraphicsItem *item : overlapItems) { if (item->zValue() <= zValue && item->type() == DiagramItem::Type) zValue = item->zValue() - 0.1; } diff --git a/examples/widgets/graphicsview/elasticnodes/graphwidget.cpp b/examples/widgets/graphicsview/elasticnodes/graphwidget.cpp index 81928a4ee1..6b817b2a21 100644 --- a/examples/widgets/graphicsview/elasticnodes/graphwidget.cpp +++ b/examples/widgets/graphicsview/elasticnodes/graphwidget.cpp @@ -164,16 +164,17 @@ void GraphWidget::timerEvent(QTimerEvent *event) Q_UNUSED(event); QList nodes; - foreach (QGraphicsItem *item, scene()->items()) { + const QList items = scene()->items(); + for (QGraphicsItem *item : items) { if (Node *node = qgraphicsitem_cast(item)) nodes << node; } - foreach (Node *node, nodes) + for (Node *node : qAsConst(nodes)) node->calculateForces(); bool itemsMoved = false; - foreach (Node *node, nodes) { + for (Node *node : qAsConst(nodes)) { if (node->advancePosition()) itemsMoved = true; } @@ -246,7 +247,8 @@ void GraphWidget::scaleView(qreal scaleFactor) void GraphWidget::shuffle() { - foreach (QGraphicsItem *item, scene()->items()) { + const QList items = scene()->items(); + for (QGraphicsItem *item : items) { if (qgraphicsitem_cast(item)) item->setPos(-150 + QRandomGenerator::global()->bounded(300), -150 + QRandomGenerator::global()->bounded(300)); } diff --git a/examples/widgets/graphicsview/elasticnodes/node.cpp b/examples/widgets/graphicsview/elasticnodes/node.cpp index 71737c984b..d4fa3d2b4f 100644 --- a/examples/widgets/graphicsview/elasticnodes/node.cpp +++ b/examples/widgets/graphicsview/elasticnodes/node.cpp @@ -94,7 +94,8 @@ void Node::calculateForces() // Sum up all forces pushing this item away qreal xvel = 0; qreal yvel = 0; - foreach (QGraphicsItem *item, scene()->items()) { + const QList items = scene()->items(); + for (QGraphicsItem *item : items) { Node *node = qgraphicsitem_cast(item); if (!node) continue; @@ -113,7 +114,7 @@ void Node::calculateForces() //! [4] // Now subtract all forces pulling items together double weight = (edgeList.size() + 1) * 10; - foreach (Edge *edge, edgeList) { + for (const Edge *edge : qAsConst(edgeList)) { QPointF vec; if (edge->sourceNode() == this) vec = mapToItem(edge->destNode(), 0, 0); @@ -194,7 +195,7 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) { switch (change) { case ItemPositionHasChanged: - foreach (Edge *edge, edgeList) + for (Edge *edge : qAsConst(edgeList)) edge->adjust(); graph->itemMoved(); break; diff --git a/examples/widgets/graphicsview/embeddeddialogs/embeddeddialog.cpp b/examples/widgets/graphicsview/embeddeddialogs/embeddeddialog.cpp index 0c3ed86565..0c831360cf 100644 --- a/examples/widgets/graphicsview/embeddeddialogs/embeddeddialog.cpp +++ b/examples/widgets/graphicsview/embeddeddialogs/embeddeddialog.cpp @@ -60,7 +60,8 @@ EmbeddedDialog::EmbeddedDialog(QWidget *parent) ui->setupUi(this); ui->layoutDirection->setCurrentIndex(layoutDirection() != Qt::LeftToRight); - foreach (QString styleName, QStyleFactory::keys()) { + const QStringList styleKeys = QStyleFactory::keys(); + for (const QString &styleName : styleKeys) { ui->style->addItem(styleName); if (style()->objectName().toLower() == styleName.toLower()) ui->style->setCurrentIndex(ui->style->count() - 1); @@ -101,7 +102,8 @@ static void setStyleHelper(QWidget *widget, QStyle *style) { widget->setStyle(style); widget->setPalette(style->standardPalette()); - foreach (QObject *child, widget->children()) { + const QObjectList children = widget->children(); + for (QObject *child : children) { if (QWidget *childWidget = qobject_cast(child)) setStyleHelper(childWidget, style); } diff --git a/examples/widgets/graphicsview/flowlayout/flowlayout.cpp b/examples/widgets/graphicsview/flowlayout/flowlayout.cpp index 065c3e00ea..ab6bbb7aa4 100644 --- a/examples/widgets/graphicsview/flowlayout/flowlayout.cpp +++ b/examples/widgets/graphicsview/flowlayout/flowlayout.cpp @@ -154,8 +154,7 @@ QSizeF FlowLayout::minSize(const QSizeF &constraint) const } else if (constraint.height() >= 0) { // width for height? // not supported } else { - QGraphicsLayoutItem *item; - foreach (item, m_items) + for (const QGraphicsLayoutItem *item : qAsConst(m_items)) size = size.expandedTo(item->effectiveSizeHint(Qt::MinimumSize)); size += QSize(left + right, top + bottom); } @@ -167,10 +166,9 @@ QSizeF FlowLayout::prefSize() const qreal left, right; getContentsMargins(&left, 0, &right, 0); - QGraphicsLayoutItem *item; qreal maxh = 0; qreal totalWidth = 0; - foreach (item, m_items) { + for (const QGraphicsLayoutItem *item : qAsConst(m_items)) { if (totalWidth > 0) totalWidth += spacing(Qt::Horizontal); QSizeF pref = item->effectiveSizeHint(Qt::PreferredSize); @@ -187,10 +185,9 @@ QSizeF FlowLayout::prefSize() const QSizeF FlowLayout::maxSize() const { - QGraphicsLayoutItem *item; qreal totalWidth = 0; qreal totalHeight = 0; - foreach (item, m_items) { + for (const QGraphicsLayoutItem *item : qAsConst(m_items)) { if (totalWidth > 0) totalWidth += spacing(Qt::Horizontal); if (totalHeight > 0)