From 4757f51fa48c88658be8ca67a4b9894a883ac111 Mon Sep 17 00:00:00 2001 From: iphydf Date: Mon, 18 Nov 2024 01:39:21 +0000 Subject: [PATCH] cleanup: Use range-for in more places. clang-tidy: modernize-loop-convert --- src/chatlog/chatline.cpp | 8 ++-- src/ipc.cpp | 10 ++-- src/model/friendlist/friendlistmanager.cpp | 4 +- src/net/bootstrapnodeupdater.cpp | 4 +- src/widget/form/settings/avform.cpp | 15 +++--- .../form/settings/userinterfaceform.cpp | 4 +- src/widget/friendlistwidget.cpp | 46 +++++++++---------- test/model/friendlistmanager_test.cpp | 3 +- 8 files changed, 45 insertions(+), 49 deletions(-) diff --git a/src/chatlog/chatline.cpp b/src/chatlog/chatline.cpp index 736bbd9508..4f020f9ab9 100644 --- a/src/chatlog/chatline.cpp +++ b/src/chatlog/chatline.cpp @@ -156,11 +156,11 @@ void ChatLine::layout(qreal w, QPointF scenePos) qreal fixedWidth = (content.size() - 1) * columnSpacing; qreal varWidth = 0.0; // used for normalisation - for (int i = 0; i < format.size(); ++i) { - if (format[i].policy == ColumnFormat::FixedSize) - fixedWidth += format[i].size; + for (auto& i : format) { + if (i.policy == ColumnFormat::FixedSize) + fixedWidth += i.size; else - varWidth += format[i].size; + varWidth += i.size; } if (varWidth == 0.0) diff --git a/src/ipc.cpp b/src/ipc.cpp index ce30f14318..ab56dabc67 100644 --- a/src/ipc.cpp +++ b/src/ipc.cpp @@ -217,9 +217,9 @@ bool IPC::isEventAccepted(time_t time) if (difftime(global()->lastProcessed, time) > 0) { IPCMemory* mem = global(); - for (uint32_t i = 0; i < EVENT_QUEUE_SIZE; ++i) { - if (mem->events[i].posted == time && mem->events[i].processed) { - result = mem->events[i].accepted; + for (auto& event : mem->events) { + if (event.posted == time && event.processed) { + result = event.accepted; break; } } @@ -268,8 +268,8 @@ void IPC::setProfileId(uint32_t profileId_) IPC::IPCEvent* IPC::fetchEvent() { IPCMemory* mem = global(); - for (uint32_t i = 0; i < EVENT_QUEUE_SIZE; ++i) { - IPCEvent* evt = &mem->events[i]; + for (auto& event : mem->events) { + IPCEvent* evt = &event; // Garbage-collect events that were not processed in EVENT_GC_TIMEOUT // and events that were processed and EVENT_GC_TIMEOUT passed after diff --git a/src/model/friendlist/friendlistmanager.cpp b/src/model/friendlist/friendlistmanager.cpp index 4b021d24bc..025c0607d7 100644 --- a/src/model/friendlist/friendlistmanager.cpp +++ b/src/model/friendlist/friendlistmanager.cpp @@ -68,8 +68,8 @@ void FriendListManager::sortByActivity() void FriendListManager::resetParents() { - for (int i = 0; i < items.size(); ++i) { - IFriendListItem* itemTmp = items[i].get(); + for (auto& item : items) { + IFriendListItem* itemTmp = item.get(); itemTmp->getWidget()->setParent(nullptr); } } diff --git a/src/net/bootstrapnodeupdater.cpp b/src/net/bootstrapnodeupdater.cpp index 1ec7ec241a..ed450cb879 100644 --- a/src/net/bootstrapnodeupdater.cpp +++ b/src/net/bootstrapnodeupdater.cpp @@ -175,8 +175,8 @@ QByteArray serialize(QList nodes) nodeJson.insert(NodeFields::maintainer, node.maintainer); QJsonArray tcp_ports; - for (size_t i = 0; i < node.tcpPorts.size(); ++i) { - tcp_ports.push_back(node.tcpPorts.at(i)); + for (unsigned short tcpPort : node.tcpPorts) { + tcp_ports.push_back(tcpPort); } nodeJson.insert(NodeFields::tcp_ports, tcp_ports); jsonNodes.append(nodeJson); diff --git a/src/widget/form/settings/avform.cpp b/src/widget/form/settings/avform.cpp index 34995e090a..546f6e22f3 100644 --- a/src/widget/form/settings/avform.cpp +++ b/src/widget/form/settings/avform.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include "audio/audio.h" #include "audio/iaudiosettings.h" @@ -230,9 +231,9 @@ void AVForm::selectBestModes(QVector& allVideoModes) if (mode.FPS > 60) continue; - for (auto iter = idealModes.begin(); iter != idealModes.end(); ++iter) { - int res = iter->first; - VideoMode idealMode = iter->second; + for (auto& iter : idealModes) { + int res = iter.first; + VideoMode idealMode = iter.second; // don't take approximately correct resolutions unless they really // are close if (mode.norm(idealMode) > idealMode.tolerance()) @@ -265,8 +266,8 @@ void AVForm::selectBestModes(QVector& allVideoModes) } QVector newVideoModes; - for (auto it = bestModeInds.rbegin(); it != bestModeInds.rend(); ++it) { - VideoMode mode_ = allVideoModes[it->second]; + for (auto& bestModeInd : std::ranges::reverse_view(bestModeInds)) { + VideoMode mode_ = allVideoModes[bestModeInd.second]; if (newVideoModes.empty()) { newVideoModes.push_back(mode_); @@ -288,9 +289,7 @@ void AVForm::fillCameraModesComboBox() bool previouslyBlocked = videoModescomboBox->blockSignals(true); videoModescomboBox->clear(); - for (int i = 0; i < videoModes.size(); ++i) { - VideoMode mode = videoModes[i]; - + for (auto mode : videoModes) { QString str; std::string pixelFormat = CameraDevice::getPixelFormatString(mode.pixel_format).toStdString(); qDebug("width: %d, height: %d, FPS: %f, pixel format: %s", mode.width, mode.height, diff --git a/src/widget/form/settings/userinterfaceform.cpp b/src/widget/form/settings/userinterfaceform.cpp index 65f9b3b269..d06f6b6bfb 100644 --- a/src/widget/form/settings/userinterfaceform.cpp +++ b/src/widget/form/settings/userinterfaceform.cpp @@ -235,8 +235,8 @@ void UserInterfaceForm::reloadSmileys() } QStringList smileys; - for (int i = 0; i < emoticons.size(); ++i) - smileys.push_front(emoticons.at(i).first()); + for (const auto& emoticon : emoticons) + smileys.push_front(emoticon.first()); emoticonsIcons.clear(); const QSize size(18, 18); diff --git a/src/widget/friendlistwidget.cpp b/src/widget/friendlistwidget.cpp index 08fccf73f3..494b93a6d6 100644 --- a/src/widget/friendlistwidget.cpp +++ b/src/widget/friendlistwidget.cpp @@ -157,25 +157,24 @@ void FriendListWidget::sortByMode() QVector friendItems; // Items that are not included in the circle int posByName = 0; // Needed for scroll contacts // Linking a friend with a circle and setting scroll position - for (int i = 0; i < itemsTmp.size(); ++i) { - if (itemsTmp[i]->isFriend() && itemsTmp[i]->getCircleId() >= 0) { - CircleWidget* circleWgt = CircleWidget::getFromID(itemsTmp[i]->getCircleId()); + for (auto& i : itemsTmp) { + if (i->isFriend() && i->getCircleId() >= 0) { + CircleWidget* circleWgt = CircleWidget::getFromID(i->getCircleId()); if (circleWgt != nullptr) { // Place a friend in the circle and continue - FriendWidget* frndTmp = - qobject_cast((itemsTmp[i].get())->getWidget()); + FriendWidget* frndTmp = qobject_cast((i.get())->getWidget()); circleWgt->addFriendWidget(frndTmp, frndTmp->getFriend()->getStatus()); continue; } } // Place the item without the circle in the vector and set the position - itemsTmp[i]->setNameSortedPos(posByName++); - friendItems.push_back(itemsTmp[i].get()); + i->setNameSortedPos(posByName++); + friendItems.push_back(i.get()); } // Add groups and friends without circles - for (int i = 0; i < friendItems.size(); ++i) { - listLayout->addWidget(friendItems[i]->getWidget()); + for (auto& friendItem : friendItems) { + listLayout->addWidget(friendItem->getWidget()); } // TODO: Try to remove @@ -192,15 +191,14 @@ void FriendListWidget::sortByMode() return a->getName().toUpper() < b->getName().toUpper(); }); - for (int i = 0; i < circles.size(); ++i) { + for (auto circle : circles) { - QVector> itemsInCircle = - getItemsFromCircle(circles.at(i)); - for (int j = 0; j < itemsInCircle.size(); ++j) { - itemsInCircle.at(j)->setNameSortedPos(posByName++); + QVector> itemsInCircle = getItemsFromCircle(circle); + for (const auto& j : itemsInCircle) { + j->setNameSortedPos(posByName++); } - listLayout->addWidget(circles.at(i)); + listLayout->addWidget(circle); } } } else if (mode == SortingMode::Activity) { @@ -233,8 +231,8 @@ void FriendListWidget::sortByMode() QVector> itemsTmp = manager->getItems(); - for (int i = 0; i < itemsTmp.size(); ++i) { - listLayout->addWidget(itemsTmp[i]->getWidget()); + for (auto& i : itemsTmp) { + listLayout->addWidget(i->getWidget()); } activityLayout = new QVBoxLayout(); @@ -249,12 +247,12 @@ void FriendListWidget::sortByMode() manager->applyFilter(); // Insert widgets to CategoryWidget - for (int i = 0; i < itemsTmp.size(); ++i) { - if (itemsTmp[i]->isFriend()) { - int timeIndex = static_cast(getTimeBucket(itemsTmp[i]->getLastActivity())); + for (auto& i : itemsTmp) { + if (i->isFriend()) { + int timeIndex = static_cast(getTimeBucket(i->getLastActivity())); QWidget* widget = activityLayout->itemAt(timeIndex)->widget(); CategoryWidget* categoryWidget = qobject_cast(widget); - FriendWidget* frnd = qobject_cast((itemsTmp[i].get())->getWidget()); + FriendWidget* frnd = qobject_cast((i.get())->getWidget()); if (!isVisible() || (isVisible() && frnd->isVisible())) { categoryWidget->addFriendWidget(frnd, frnd->getFriend()->getStatus()); } @@ -320,10 +318,10 @@ QVector> FriendListWidget::getItemsFromCircle(C { QVector> itemsTmp = manager->getItems(); QVector> itemsInCircle; - for (int i = 0; i < itemsTmp.size(); ++i) { - int circleId = itemsTmp.at(i)->getCircleId(); + for (const auto& i : itemsTmp) { + int circleId = i->getCircleId(); if (CircleWidget::getFromID(circleId) == circle) { - itemsInCircle.push_back(itemsTmp.at(i)); + itemsInCircle.push_back(i); } } return itemsInCircle; diff --git a/test/model/friendlistmanager_test.cpp b/test/model/friendlistmanager_test.cpp index d8314c2757..7b95ed00f0 100644 --- a/test/model/friendlistmanager_test.cpp +++ b/test/model/friendlistmanager_test.cpp @@ -279,8 +279,7 @@ class FriendItemsBuilder // Add friends and set the date of the last activity by index QDateTime dateTime = QDateTime::currentDateTime(); - for (int i = 0; i < sortedByActivityFriends.size(); ++i) { - QString name = sortedByActivityFriends.at(i); + for (auto name : sortedByActivityFriends) { vec.push_back(std::shared_ptr( new MockFriend(name, isOnline(name), getDateTime(name)))); }