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);
}
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;

View File

@ -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;

View File

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

View File

@ -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<QByteArray> uniforms;
QList<QString> filter = QStringList("*.par");
QList<QFileInfo> files = QDir(":/res/boxes/").entryInfoList(filter, QDir::Files | QDir::Readable);
const QList<QFileInfo> 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<QFileInfo> 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();

View File

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

View File

@ -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;

View File

@ -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<QGraphicsItem *> 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]

View File

@ -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;

View File

@ -92,8 +92,8 @@ MainWindow::MainWindow()
//! [1]
void MainWindow::backgroundButtonGroupClicked(QAbstractButton *button)
{
QList<QAbstractButton *> buttons = backgroundButtonGroup->buttons();
foreach (QAbstractButton *myButton, buttons) {
const QList<QAbstractButton *> 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<QAbstractButton *> buttons = buttonGroup->buttons();
foreach (QAbstractButton *button, buttons) {
const QList<QAbstractButton *> 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<QGraphicsItem *> selectedItems = scene->selectedItems();
for (QGraphicsItem *item : qAsConst(selectedItems)) {
if (item->type() == Arrow::Type) {
scene->removeItem(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)
qgraphicsitem_cast<DiagramItem *>(item)->removeArrows();
scene->removeItem(item);
@ -165,10 +167,10 @@ void MainWindow::bringToFront()
return;
QGraphicsItem *selectedItem = scene->selectedItems().first();
QList<QGraphicsItem *> overlapItems = selectedItem->collidingItems();
const QList<QGraphicsItem *> 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<QGraphicsItem *> overlapItems = selectedItem->collidingItems();
const QList<QGraphicsItem *> 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;
}

View File

@ -164,16 +164,17 @@ void GraphWidget::timerEvent(QTimerEvent *event)
Q_UNUSED(event);
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))
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<QGraphicsItem *> items = scene()->items();
for (QGraphicsItem *item : items) {
if (qgraphicsitem_cast<Node *>(item))
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
qreal xvel = 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);
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;

View File

@ -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<QWidget *>(child))
setStyleHelper(childWidget, style);
}

View File

@ -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)