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 <v.ronin@yahoo.it>
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
This commit is contained in:
Christian Ehrlicher 2018-12-07 11:59:02 +01:00
parent bce32c8ab8
commit 26f7edb09e
13 changed files with 74 additions and 94 deletions

View File

@ -218,14 +218,14 @@ GLTextureCube::GLTextureCube(int size)
glBindTexture(GL_TEXTURE_CUBE_MAP, 0); glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
} }
GLTextureCube::GLTextureCube(const QStringList& fileNames, int size) GLTextureCube::GLTextureCube(const QStringList &fileNames, int size)
{ {
// TODO: Add error handling. // TODO: Add error handling.
glBindTexture(GL_TEXTURE_CUBE_MAP, m_texture); glBindTexture(GL_TEXTURE_CUBE_MAP, m_texture);
int index = 0; int index = 0;
foreach (QString file, fileNames) { for (const QString &file : fileNames) {
QImage image(file); QImage image(file);
if (image.isNull()) { if (image.isNull()) {
m_failed = true; m_failed = true;

View File

@ -129,7 +129,7 @@ class GLTextureCube : public GLTexture
{ {
public: public:
GLTextureCube(int size); 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 load(int size, int face, QRgb *data);
void bind() override; void bind() override;
void unbind() override; void unbind() override;

View File

@ -127,10 +127,8 @@ void ItemBase::duplicateSelectedItems(QGraphicsScene *scene)
if (!scene) if (!scene)
return; return;
QList<QGraphicsItem *> selected; const QList<QGraphicsItem *> selected = scene->selectedItems();
selected = scene->selectedItems(); for (QGraphicsItem *item : selected) {
foreach (QGraphicsItem *item, selected) {
ItemBase *itemBase = qgraphicsitem_cast<ItemBase *>(item); ItemBase *itemBase = qgraphicsitem_cast<ItemBase *>(item);
if (itemBase) if (itemBase)
scene->addItem(itemBase->createNew(itemBase->m_size, itemBase->pos().x() + itemBase->m_size, itemBase->pos().y())); 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) if (!scene)
return; return;
QList<QGraphicsItem *> selected; const QList<QGraphicsItem *> selected = scene->selectedItems();
selected = scene->selectedItems(); for (QGraphicsItem *item : selected) {
foreach (QGraphicsItem *item, selected) {
ItemBase *itemBase = qgraphicsitem_cast<ItemBase *>(item); ItemBase *itemBase = qgraphicsitem_cast<ItemBase *>(item);
if (itemBase) if (itemBase)
delete itemBase; delete itemBase;
@ -157,10 +153,8 @@ void ItemBase::growSelectedItems(QGraphicsScene *scene)
if (!scene) if (!scene)
return; return;
QList<QGraphicsItem *> selected; const QList<QGraphicsItem *> selected = scene->selectedItems();
selected = scene->selectedItems(); for (QGraphicsItem *item : selected) {
foreach (QGraphicsItem *item, selected) {
ItemBase *itemBase = qgraphicsitem_cast<ItemBase *>(item); ItemBase *itemBase = qgraphicsitem_cast<ItemBase *>(item);
if (itemBase) { if (itemBase) {
itemBase->prepareGeometryChange(); itemBase->prepareGeometryChange();
@ -176,10 +170,8 @@ void ItemBase::shrinkSelectedItems(QGraphicsScene *scene)
if (!scene) if (!scene)
return; return;
QList<QGraphicsItem *> selected; const QList<QGraphicsItem *> selected = scene->selectedItems();
selected = scene->selectedItems(); for (QGraphicsItem *item : selected) {
foreach (QGraphicsItem *item, selected) {
ItemBase *itemBase = qgraphicsitem_cast<ItemBase *>(item); ItemBase *itemBase = qgraphicsitem_cast<ItemBase *>(item);
if (itemBase) { if (itemBase) {
itemBase->prepareGeometryChange(); itemBase->prepareGeometryChange();

View File

@ -311,15 +311,13 @@ RenderOptionsDialog::RenderOptionsDialog()
layout->addWidget(check, 0, 0, 1, 2); layout->addWidget(check, 0, 0, 1, 2);
++row; ++row;
QPalette palette;
// Load all .par files // Load all .par files
// .par files have a simple syntax for specifying user adjustable uniform variables. // .par files have a simple syntax for specifying user adjustable uniform variables.
QSet<QByteArray> uniforms; const QList<QFileInfo> files = QDir(QStringLiteral(":/res/boxes/"))
QList<QString> filter = QStringList("*.par"); .entryInfoList({ QStringLiteral("*.par") },
QList<QFileInfo> files = QDir(":/res/boxes/").entryInfoList(filter, QDir::Files | QDir::Readable); QDir::Files | QDir::Readable);
foreach (QFileInfo fileInfo, files) { for (const QFileInfo &fileInfo : files) {
QFile file(fileInfo.absoluteFilePath()); QFile file(fileInfo.absoluteFilePath());
if (file.open(QIODevice::ReadOnly)) { if (file.open(QIODevice::ReadOnly)) {
while (!file.atEnd()) { while (!file.atEnd()) {
@ -404,7 +402,7 @@ int RenderOptionsDialog::addShader(const QString &name)
void RenderOptionsDialog::emitParameterChanged() void RenderOptionsDialog::emitParameterChanged()
{ {
foreach (ParameterEdit *edit, m_parameterEdits) for (ParameterEdit *edit : qAsConst(m_parameterEdits))
edit->emitChange(); edit->emitChange();
} }
@ -541,24 +539,15 @@ Scene::Scene(int width, int height, int maxTextureSize)
Scene::~Scene() Scene::~Scene()
{ {
if (m_box) delete m_box;
delete m_box; qDeleteAll(m_textures);
foreach (GLTexture *texture, m_textures) delete m_mainCubemap;
if (texture) delete texture; qDeleteAll(m_programs);
if (m_mainCubemap) delete m_vertexShader;
delete m_mainCubemap; qDeleteAll(m_fragmentShaders);
foreach (QGLShaderProgram *program, m_programs) qDeleteAll(m_cubemaps);
if (program) delete program; delete m_environmentShader;
if (m_vertexShader) delete m_environmentProgram;
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;
} }
void Scene::initGL() void Scene::initGL()
@ -603,15 +592,13 @@ void Scene::initGL()
m_mainCubemap = new GLRenderTargetCube(512); m_mainCubemap = new GLRenderTargetCube(512);
QStringList filter;
QList<QFileInfo> files; QList<QFileInfo> files;
// Load all .png files as textures // Load all .png files as textures
m_currentTexture = 0; m_currentTexture = 0;
filter = QStringList("*.png"); files = QDir(":/res/boxes/").entryInfoList({ QStringLiteral("*.png") }, QDir::Files | QDir::Readable);
files = QDir(":/res/boxes/").entryInfoList(filter, 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)); GLTexture *texture = new GLTexture2D(file.absoluteFilePath(), qMin(256, m_maxTextureSize), qMin(256, m_maxTextureSize));
if (texture->failed()) { if (texture->failed()) {
delete texture; delete texture;
@ -626,9 +613,8 @@ void Scene::initGL()
// Load all .fsh files as fragment shaders // Load all .fsh files as fragment shaders
m_currentShader = 0; m_currentShader = 0;
filter = QStringList("*.fsh"); files = QDir(":/res/boxes/").entryInfoList({ QStringLiteral("*.fsh") }, QDir::Files | QDir::Readable);
files = QDir(":/res/boxes/").entryInfoList(filter, QDir::Files | QDir::Readable); for (const QFileInfo &file : qAsConst(files)) {
foreach (QFileInfo file, files) {
QGLShaderProgram *program = new QGLShaderProgram; QGLShaderProgram *program = new QGLShaderProgram;
QGLShader* shader = new QGLShader(QGLShader::Fragment); QGLShader* shader = new QGLShader(QGLShader::Fragment);
shader->compileSourceFile(file.absoluteFilePath()); shader->compileSourceFile(file.absoluteFilePath());
@ -664,7 +650,7 @@ void Scene::initGL()
m_renderOptions->emitParameterChanged(); 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 to prevent glLoadMatrixf to fail on certain drivers
static GLfloat mat[16]; static GLfloat mat[16];
@ -1053,7 +1039,7 @@ void Scene::toggleDynamicCubemap(int state)
void Scene::setColorParameter(const QString &name, QRgb color) void Scene::setColorParameter(const QString &name, QRgb color)
{ {
// set the color in all programs // set the color in all programs
foreach (QGLShaderProgram *program, m_programs) { for (QGLShaderProgram *program : qAsConst(m_programs)) {
program->bind(); program->bind();
program->setUniformValue(program->uniformLocation(name), QColor(color)); program->setUniformValue(program->uniformLocation(name), QColor(color));
program->release(); program->release();
@ -1063,7 +1049,7 @@ void Scene::setColorParameter(const QString &name, QRgb color)
void Scene::setFloatParameter(const QString &name, float value) void Scene::setFloatParameter(const QString &name, float value)
{ {
// set the color in all programs // set the color in all programs
foreach (QGLShaderProgram *program, m_programs) { for (QGLShaderProgram *program : qAsConst(m_programs)) {
program->bind(); program->bind();
program->setUniformValue(program->uniformLocation(name), value); program->setUniformValue(program->uniformLocation(name), value);
program->release(); program->release();

View File

@ -160,11 +160,12 @@ void Mouse::advance(int step)
// Try not to crash with any other mice // Try not to crash with any other mice
//! [7] //! [7]
QList<QGraphicsItem *> dangerMice = scene()->items(QPolygonF() const QList<QGraphicsItem *> dangerMice = scene()->items(QPolygonF()
<< mapToScene(0, 0) << mapToScene(0, 0)
<< mapToScene(-30, -50) << mapToScene(-30, -50)
<< mapToScene(30, -50)); << mapToScene(30, -50));
foreach (QGraphicsItem *item, dangerMice) {
for (const QGraphicsItem *item : dangerMice) {
if (item == this) if (item == this)
continue; continue;

View File

@ -111,7 +111,7 @@ void DiagramItem::removeArrow(Arrow *arrow)
//! [2] //! [2]
void DiagramItem::removeArrows() void DiagramItem::removeArrows()
{ {
foreach (Arrow *arrow, arrows) { for (Arrow *arrow : qAsConst(arrows)) {
arrow->startItem()->removeArrow(arrow); arrow->startItem()->removeArrow(arrow);
arrow->endItem()->removeArrow(arrow); arrow->endItem()->removeArrow(arrow);
scene()->removeItem(arrow); scene()->removeItem(arrow);
@ -154,9 +154,8 @@ void DiagramItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
QVariant DiagramItem::itemChange(GraphicsItemChange change, const QVariant &value) QVariant DiagramItem::itemChange(GraphicsItemChange change, const QVariant &value)
{ {
if (change == QGraphicsItem::ItemPositionChange) { if (change == QGraphicsItem::ItemPositionChange) {
foreach (Arrow *arrow, arrows) { for (Arrow *arrow : qAsConst(arrows))
arrow->updatePosition(); arrow->updatePosition();
}
} }
return value; return value;

View File

@ -234,12 +234,10 @@ void DiagramScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
//! [13] //! [13]
//! [14] //! [14]
bool DiagramScene::isItemChange(int type) bool DiagramScene::isItemChange(int type) const
{ {
foreach (QGraphicsItem *item, selectedItems()) { const QList<QGraphicsItem *> items = selectedItems();
if (item->type() == type) const auto cb = [type](const QGraphicsItem *item) { return item->type() == type; };
return true; return std::find_if(items.begin(), items.end(), cb) != items.end();
}
return false;
} }
//! [14] //! [14]

View File

@ -100,7 +100,7 @@ protected:
void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) override; void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) override;
private: private:
bool isItemChange(int type); bool isItemChange(int type) const;
DiagramItem::DiagramType myItemType; DiagramItem::DiagramType myItemType;
QMenu *myItemMenu; QMenu *myItemMenu;

View File

@ -92,8 +92,8 @@ MainWindow::MainWindow()
//! [1] //! [1]
void MainWindow::backgroundButtonGroupClicked(QAbstractButton *button) void MainWindow::backgroundButtonGroupClicked(QAbstractButton *button)
{ {
QList<QAbstractButton *> buttons = backgroundButtonGroup->buttons(); const QList<QAbstractButton *> buttons = backgroundButtonGroup->buttons();
foreach (QAbstractButton *myButton, buttons) { for (QAbstractButton *myButton : buttons) {
if (myButton != button) if (myButton != button)
button->setChecked(false); button->setChecked(false);
} }
@ -115,8 +115,8 @@ void MainWindow::backgroundButtonGroupClicked(QAbstractButton *button)
//! [2] //! [2]
void MainWindow::buttonGroupClicked(int id) void MainWindow::buttonGroupClicked(int id)
{ {
QList<QAbstractButton *> buttons = buttonGroup->buttons(); const QList<QAbstractButton *> buttons = buttonGroup->buttons();
foreach (QAbstractButton *button, buttons) { for (QAbstractButton *button : buttons) {
if (buttonGroup->button(id) != button) if (buttonGroup->button(id) != button)
button->setChecked(false); button->setChecked(false);
} }
@ -132,7 +132,8 @@ void MainWindow::buttonGroupClicked(int id)
//! [3] //! [3]
void MainWindow::deleteItem() void MainWindow::deleteItem()
{ {
foreach (QGraphicsItem *item, scene->selectedItems()) { QList<QGraphicsItem *> selectedItems = scene->selectedItems();
for (QGraphicsItem *item : qAsConst(selectedItems)) {
if (item->type() == Arrow::Type) { if (item->type() == Arrow::Type) {
scene->removeItem(item); scene->removeItem(item);
Arrow *arrow = qgraphicsitem_cast<Arrow *>(item); Arrow *arrow = qgraphicsitem_cast<Arrow *>(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) if (item->type() == DiagramItem::Type)
qgraphicsitem_cast<DiagramItem *>(item)->removeArrows(); qgraphicsitem_cast<DiagramItem *>(item)->removeArrows();
scene->removeItem(item); scene->removeItem(item);
@ -165,10 +167,10 @@ void MainWindow::bringToFront()
return; return;
QGraphicsItem *selectedItem = scene->selectedItems().first(); QGraphicsItem *selectedItem = scene->selectedItems().first();
QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems(); const QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems();
qreal zValue = 0; qreal zValue = 0;
foreach (QGraphicsItem *item, overlapItems) { for (const QGraphicsItem *item : overlapItems) {
if (item->zValue() >= zValue && item->type() == DiagramItem::Type) if (item->zValue() >= zValue && item->type() == DiagramItem::Type)
zValue = item->zValue() + 0.1; zValue = item->zValue() + 0.1;
} }
@ -183,10 +185,10 @@ void MainWindow::sendToBack()
return; return;
QGraphicsItem *selectedItem = scene->selectedItems().first(); QGraphicsItem *selectedItem = scene->selectedItems().first();
QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems(); const QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems();
qreal zValue = 0; qreal zValue = 0;
foreach (QGraphicsItem *item, overlapItems) { for (const QGraphicsItem *item : overlapItems) {
if (item->zValue() <= zValue && item->type() == DiagramItem::Type) if (item->zValue() <= zValue && item->type() == DiagramItem::Type)
zValue = item->zValue() - 0.1; zValue = item->zValue() - 0.1;
} }

View File

@ -164,16 +164,17 @@ void GraphWidget::timerEvent(QTimerEvent *event)
Q_UNUSED(event); Q_UNUSED(event);
QList<Node *> nodes; QList<Node *> nodes;
foreach (QGraphicsItem *item, scene()->items()) { const QList<QGraphicsItem *> items = scene()->items();
for (QGraphicsItem *item : items) {
if (Node *node = qgraphicsitem_cast<Node *>(item)) if (Node *node = qgraphicsitem_cast<Node *>(item))
nodes << node; nodes << node;
} }
foreach (Node *node, nodes) for (Node *node : qAsConst(nodes))
node->calculateForces(); node->calculateForces();
bool itemsMoved = false; bool itemsMoved = false;
foreach (Node *node, nodes) { for (Node *node : qAsConst(nodes)) {
if (node->advancePosition()) if (node->advancePosition())
itemsMoved = true; itemsMoved = true;
} }
@ -246,7 +247,8 @@ void GraphWidget::scaleView(qreal scaleFactor)
void GraphWidget::shuffle() void GraphWidget::shuffle()
{ {
foreach (QGraphicsItem *item, scene()->items()) { const QList<QGraphicsItem *> items = scene()->items();
for (QGraphicsItem *item : items) {
if (qgraphicsitem_cast<Node *>(item)) if (qgraphicsitem_cast<Node *>(item))
item->setPos(-150 + QRandomGenerator::global()->bounded(300), -150 + QRandomGenerator::global()->bounded(300)); item->setPos(-150 + QRandomGenerator::global()->bounded(300), -150 + QRandomGenerator::global()->bounded(300));
} }

View File

@ -94,7 +94,8 @@ void Node::calculateForces()
// Sum up all forces pushing this item away // Sum up all forces pushing this item away
qreal xvel = 0; qreal xvel = 0;
qreal yvel = 0; qreal yvel = 0;
foreach (QGraphicsItem *item, scene()->items()) { const QList<QGraphicsItem *> items = scene()->items();
for (QGraphicsItem *item : items) {
Node *node = qgraphicsitem_cast<Node *>(item); Node *node = qgraphicsitem_cast<Node *>(item);
if (!node) if (!node)
continue; continue;
@ -113,7 +114,7 @@ void Node::calculateForces()
//! [4] //! [4]
// Now subtract all forces pulling items together // Now subtract all forces pulling items together
double weight = (edgeList.size() + 1) * 10; double weight = (edgeList.size() + 1) * 10;
foreach (Edge *edge, edgeList) { for (const Edge *edge : qAsConst(edgeList)) {
QPointF vec; QPointF vec;
if (edge->sourceNode() == this) if (edge->sourceNode() == this)
vec = mapToItem(edge->destNode(), 0, 0); vec = mapToItem(edge->destNode(), 0, 0);
@ -194,7 +195,7 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
{ {
switch (change) { switch (change) {
case ItemPositionHasChanged: case ItemPositionHasChanged:
foreach (Edge *edge, edgeList) for (Edge *edge : qAsConst(edgeList))
edge->adjust(); edge->adjust();
graph->itemMoved(); graph->itemMoved();
break; break;

View File

@ -60,7 +60,8 @@ EmbeddedDialog::EmbeddedDialog(QWidget *parent)
ui->setupUi(this); ui->setupUi(this);
ui->layoutDirection->setCurrentIndex(layoutDirection() != Qt::LeftToRight); 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); ui->style->addItem(styleName);
if (style()->objectName().toLower() == styleName.toLower()) if (style()->objectName().toLower() == styleName.toLower())
ui->style->setCurrentIndex(ui->style->count() - 1); ui->style->setCurrentIndex(ui->style->count() - 1);
@ -101,7 +102,8 @@ static void setStyleHelper(QWidget *widget, QStyle *style)
{ {
widget->setStyle(style); widget->setStyle(style);
widget->setPalette(style->standardPalette()); widget->setPalette(style->standardPalette());
foreach (QObject *child, widget->children()) { const QObjectList children = widget->children();
for (QObject *child : children) {
if (QWidget *childWidget = qobject_cast<QWidget *>(child)) if (QWidget *childWidget = qobject_cast<QWidget *>(child))
setStyleHelper(childWidget, style); setStyleHelper(childWidget, style);
} }

View File

@ -154,8 +154,7 @@ QSizeF FlowLayout::minSize(const QSizeF &constraint) const
} else if (constraint.height() >= 0) { // width for height? } else if (constraint.height() >= 0) { // width for height?
// not supported // not supported
} else { } else {
QGraphicsLayoutItem *item; for (const QGraphicsLayoutItem *item : qAsConst(m_items))
foreach (item, m_items)
size = size.expandedTo(item->effectiveSizeHint(Qt::MinimumSize)); size = size.expandedTo(item->effectiveSizeHint(Qt::MinimumSize));
size += QSize(left + right, top + bottom); size += QSize(left + right, top + bottom);
} }
@ -167,10 +166,9 @@ QSizeF FlowLayout::prefSize() const
qreal left, right; qreal left, right;
getContentsMargins(&left, 0, &right, 0); getContentsMargins(&left, 0, &right, 0);
QGraphicsLayoutItem *item;
qreal maxh = 0; qreal maxh = 0;
qreal totalWidth = 0; qreal totalWidth = 0;
foreach (item, m_items) { for (const QGraphicsLayoutItem *item : qAsConst(m_items)) {
if (totalWidth > 0) if (totalWidth > 0)
totalWidth += spacing(Qt::Horizontal); totalWidth += spacing(Qt::Horizontal);
QSizeF pref = item->effectiveSizeHint(Qt::PreferredSize); QSizeF pref = item->effectiveSizeHint(Qt::PreferredSize);
@ -187,10 +185,9 @@ QSizeF FlowLayout::prefSize() const
QSizeF FlowLayout::maxSize() const QSizeF FlowLayout::maxSize() const
{ {
QGraphicsLayoutItem *item;
qreal totalWidth = 0; qreal totalWidth = 0;
qreal totalHeight = 0; qreal totalHeight = 0;
foreach (item, m_items) { for (const QGraphicsLayoutItem *item : qAsConst(m_items)) {
if (totalWidth > 0) if (totalWidth > 0)
totalWidth += spacing(Qt::Horizontal); totalWidth += spacing(Qt::Horizontal);
if (totalHeight > 0) if (totalHeight > 0)