Commit cdb7b5a0 authored by Friedrich W. H. Kossebau's avatar Friedrich W. H. Kossebau
Browse files

Port away from Qt's foreach

parent ba75ef20
......@@ -41,6 +41,9 @@ include(ECMAddTests)
include(ECMSetupVersion)
include(ECMQtDeclareLoggingCategory)
add_definitions(
-DQT_NO_FOREACH
)
###### Switching to Subdirectories ######
add_subdirectory(icons)
......
......@@ -82,9 +82,9 @@ Examples:
void Killbots::Engine::refreshSpriteMap()
{
m_spriteMap.clear();
foreach( Sprite * bot, m_bots )
for (Sprite * bot : qAsConst(m_bots))
m_spriteMap.insert( bot->gridPos(), bot );
foreach( Sprite * junkheap, m_junkheaps )
for (Sprite * junkheap : qAsConst(m_junkheaps))
m_spriteMap.insert( junkheap->gridPos(), junkheap );
}
-------------------------------------------------------------------------------
......
......@@ -26,6 +26,8 @@
#include "killbots_debug.h"
#include <KRandom>
#include <array>
uint qHash(const QPoint &point)
{
return qHash(point.x() * 1000 + point.y());
......@@ -296,7 +298,7 @@ bool Killbots::Engine::useVaporizer()
if (!neighbors.isEmpty()) {
m_coordinator->beginNewAnimationStage();
foreach (Sprite *sprite, neighbors) {
for (Sprite *sprite : qAsConst(neighbors)) {
destroySprite(sprite);
}
updateEnergy(-m_rules->costOfVaporizer());
......@@ -318,7 +320,7 @@ void Killbots::Engine::moveRobots(bool justFastbots)
if (justFastbots) {
refreshSpriteMap();
foreach (Sprite *bot, m_bots) {
for (Sprite *bot : qAsConst(m_bots)) {
if (bot->spriteType() == Fastbot) {
const QPoint offset(sign(m_hero->gridPos().x() - bot->gridPos().x()), sign(m_hero->gridPos().y() - bot->gridPos().y()));
const QPoint target = bot->gridPos() + offset;
......@@ -328,7 +330,7 @@ void Killbots::Engine::moveRobots(bool justFastbots)
}
}
} else {
foreach (Sprite *bot, m_bots) {
for (Sprite *bot : qAsConst(m_bots)) {
const QPoint offset(sign(m_hero->gridPos().x() - bot->gridPos().x()), sign(m_hero->gridPos().y() - bot->gridPos().y()));
m_coordinator->slideSprite(bot, bot->gridPos() + offset);
}
......@@ -346,7 +348,8 @@ void Killbots::Engine::assessDamage()
}
// Check junkheaps for dead robots
foreach (Sprite *junkheap, m_junkheaps) {
const auto junkheaps = m_junkheaps;
for (Sprite *junkheap : junkheaps) {
destroyAllCollidingBots(junkheap, !m_heroIsDead);
}
......@@ -398,10 +401,10 @@ void Killbots::Engine::endGame()
void Killbots::Engine::refreshSpriteMap()
{
m_spriteMap.clear();
foreach (Sprite *bot, m_bots) {
for (Sprite *bot : qAsConst(m_bots)) {
m_spriteMap.insert(bot->gridPos(), bot);
}
foreach (Sprite *junkheap, m_junkheaps) {
for (Sprite *junkheap : qAsConst(m_junkheaps)) {
m_spriteMap.insert(junkheap->gridPos(), junkheap);
}
}
......@@ -640,19 +643,20 @@ bool Killbots::Engine::moveIsSafe(const QPoint &cell, HeroAction direction) cons
// If we're examining an vertical or horizontal neighbour, things are more complicated...
else {
// Assemble a list of the cells behind the neighbour.
QList<QPoint> cellsBehindNeighbor;
cellsBehindNeighbor << neighborsNeighbor;
const std::array<QPoint, 3> cellsBehindNeighbor {
neighborsNeighbor,
// Add neighboursNeighbour's anticlockwise neighbour.
// ( i + 2 ) % 8 is the direction a quarter turn anticlockwise from i.
cellsBehindNeighbor << neighborsNeighbor + offsetFromDirection((i + 2) % 8);
neighborsNeighbor + offsetFromDirection((i + 2) % 8),
// Add neighboursNeighbour's clockwise neighbour.
// ( i + 6 ) % 8 is the direction a quarter turn clockwise from i.
cellsBehindNeighbor << neighborsNeighbor + offsetFromDirection((i + 6) % 8);
neighborsNeighbor + offsetFromDirection((i + 6) % 8),
};
// Then we just count the number of fastbots and robots in the list of cells.
int fastbotsFound = 0;
int robotsFound = 0;
foreach (const QPoint &cell, cellsBehindNeighbor) {
for (const QPoint &cell : cellsBehindNeighbor) {
if (spriteTypeAt(cell) == Fastbot) {
++fastbotsFound;
} else if (spriteTypeAt(cell) == Robot) {
......@@ -716,14 +720,18 @@ void Killbots::Engine::cleanUpRound()
}
m_hero = nullptr;
foreach (Sprite *bot, m_bots) {
const auto bots = m_bots;
for (Sprite *bot : bots) {
destroySprite(bot, false);
}
Q_ASSERT(m_bots.isEmpty());
m_bots.clear();
foreach (Sprite *junkheap, m_junkheaps) {
const auto junkheaps = m_junkheaps;
for (Sprite *junkheap : junkheaps) {
destroySprite(junkheap);
}
Q_ASSERT(m_junkheaps.isEmpty());
m_junkheaps.clear();
m_spriteMap.clear();
......@@ -759,7 +767,8 @@ bool Killbots::Engine::destroyAllCollidingBots(const Sprite *sprite, bool calcul
{
bool result = false;
foreach (Sprite *robot, m_spriteMap.values(sprite->gridPos())) {
const auto robotsAtPos = m_spriteMap.values(sprite->gridPos());
for (Sprite *robot : robotsAtPos) {
if (robot != sprite && (robot->spriteType() == Robot || robot->spriteType() == Fastbot)) {
destroySprite(robot, calculatePoints);
result = true;
......
......@@ -166,13 +166,13 @@ void Killbots::MainWindow::createScoreDialog()
m_scoreDialog->setModal(false);
QStringList fileList;
const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("killbots/ruleset"), QStandardPaths::LocateDirectory);
Q_FOREACH (const QString &dir, dirs) {
for (const QString &dir : dirs) {
const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*.desktop"));
Q_FOREACH (const QString &file, fileNames) {
for (const QString &file : fileNames) {
fileList.append(dir + QLatin1Char('/') + file);
}
}
foreach (const QString &fileName, fileList) {
for (const QString &fileName : qAsConst(fileList)) {
const Ruleset *ruleset = Ruleset::load(fileName);
if (ruleset) {
m_scoreDialog->addLocalizedConfigGroupName(qMakePair(ruleset->scoreGroupKey(), ruleset->name()));
......
......@@ -54,7 +54,8 @@ void Killbots::RulesetDetailsDialog::loadRuleset(const Ruleset *ruleset)
if (m_labels.size() == 0) {
QFormLayout *layout = new QFormLayout(mMainWidget);
foreach (const KConfigSkeletonItem *item, ruleset->items()) {
const auto items = ruleset->items();
for (const KConfigSkeletonItem *item : items) {
if (!maskedItems.contains(item->name())) {
QString labelText = item->label().isEmpty() ? item->name() : item->label();
labelText = i18nc("%1 is a pretranslated string that we're turning into a label", "%1:", labelText);
......
......@@ -113,9 +113,9 @@ void Killbots::RulesetSelector::findRulesets()
m_listWidget->setSortingEnabled(true);
const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("killbots/rulesets/"), QStandardPaths::LocateDirectory);
Q_FOREACH (const QString &dir, dirs) {
for (const QString &dir : dirs) {
const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*.desktop"));
Q_FOREACH (const QString &file, fileNames) {
for (const QString &file : fileNames) {
const Ruleset *ruleset = Ruleset::load(file);
if (ruleset) {
QString name = ruleset->name();
......
......@@ -98,12 +98,12 @@ void Killbots::Scene::animateSprites(const QList<Sprite *> &newSprites,
if (value == 0.0) {
halfDone = false;
} else if (value < 1.0) {
foreach (Sprite *sprite, newSprites) {
for (Sprite *sprite : newSprites) {
sprite->resetTransform();
sprite->setTransform(QTransform::fromScale(value, value), true);
}
foreach (Sprite *sprite, slidingSprites) {
for (Sprite *sprite : slidingSprites) {
QPointF posInGridCoordinates = value * QPointF(sprite->nextGridPos() - sprite->currentGridPos()) + sprite->currentGridPos();
sprite->setPos(QPointF(posInGridCoordinates.x() * m_cellSize.width(), posInGridCoordinates.y() * m_cellSize.height()));
}
......@@ -114,26 +114,28 @@ void Killbots::Scene::animateSprites(const QList<Sprite *> &newSprites,
if (!halfDone && value >= 0.5) {
halfDone = true;
foreach (Sprite *sprite, teleportingSprites) {
for (Sprite *sprite : teleportingSprites) {
updateSpritePos(sprite, sprite->nextGridPos());
}
}
foreach (Sprite *sprite, teleportingSprites) {
for (Sprite *sprite : teleportingSprites) {
sprite->resetTransform();
sprite->setTransform(QTransform::fromScale(scaleFactor, scaleFactor), true);
}
foreach (Sprite *sprite, destroyedSprites) {
for (Sprite *sprite : destroyedSprites) {
sprite->resetTransform();
sprite->setTransform(QTransform::fromScale(1 - value, 1 - value), true);
sprite->setTransform(QTransform().rotate(value * 180), true);
}
} else {
foreach (Sprite *sprite, newSprites + slidingSprites + teleportingSprites) {
sprite->resetTransform();
sprite->advanceGridPosQueue();
updateSpritePos(sprite, sprite->currentGridPos());
for (auto& sprites : {newSprites, slidingSprites, teleportingSprites}) {
for (Sprite *sprite : sprites) {
sprite->resetTransform();
sprite->advanceGridPosQueue();
updateSpritePos(sprite, sprite->currentGridPos());
}
}
qDeleteAll(destroyedSprites);
......@@ -166,11 +168,11 @@ void Killbots::Scene::doLayout()
QFont font;
font.setPixelSize(newFontPixelSize);
foreach (NumericDisplayItem *display, m_numericDisplays) {
for (NumericDisplayItem *display : qAsConst(m_numericDisplays)) {
display->setFont(font);
displaySize = displaySize.expandedTo(display->preferredSize());
}
foreach (NumericDisplayItem *display, m_numericDisplays) {
for (NumericDisplayItem *display : qAsConst(m_numericDisplays)) {
display->setRenderSize(displaySize);
}
} else {
......@@ -179,7 +181,7 @@ void Killbots::Scene::doLayout()
// The rest of the function deals only with a list of visible displays.
QList<NumericDisplayItem *> visibleDisplays;
foreach (NumericDisplayItem *display, m_numericDisplays) {
for (NumericDisplayItem *display : qAsConst(m_numericDisplays)) {
if (display->isVisible()) {
visibleDisplays << display;
}
......@@ -210,7 +212,8 @@ void Killbots::Scene::doLayout()
const qreal newCellWidth = displaysOnTop ? cellWidthTop : cellWidthSide;
m_cellSize = QSize(qRound(newCellWidth), qRound(newCellWidth / aspectRatio));
foreach (QGraphicsItem *item, items()) {
const auto items = this->items();
for (QGraphicsItem *item : items) {
Sprite *sprite = qgraphicsitem_cast<Sprite *>(item);
if (sprite) {
sprite->setRenderSize(m_cellSize);
......@@ -229,7 +232,7 @@ void Killbots::Scene::doLayout()
const qreal displayYPos = (sceneRectYPos - (displaySize.height() + m_cellSize.height() / 2.0)) / 2;
int xPos = sceneRectXPos + (size.width() - widthOfDisplaysOnTop) / 2.0;
foreach (NumericDisplayItem *display, visibleDisplays) {
for (NumericDisplayItem *display : qAsConst(visibleDisplays)) {
display->setPos(xPos, displayYPos);
xPos += displaySize.width() + spacing;
}
......@@ -260,7 +263,7 @@ void Killbots::Scene::doLayout()
}
int yPos = -m_cellSize.height() / 2;
foreach (NumericDisplayItem *display, visibleDisplays) {
for (NumericDisplayItem *display : qAsConst(visibleDisplays)) {
display->setPos(displayXPos, yPos);
yPos += displaySize.height() + spacing;
}
......@@ -369,7 +372,8 @@ Killbots::HeroAction Killbots::Scene::getMouseDirection(QPointF cursorPosition)
bool Killbots::Scene::popupAtPosition(QPointF position) const
{
foreach (QGraphicsItem *item, items(position)) {
const auto itemsAtPos = items(position);
for (QGraphicsItem *item : itemsAtPos) {
if (dynamic_cast<KGamePopupItem *>(item) != nullptr) {
return true;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment