Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup: Use range-for in more places. #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/chatlog/chatline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions src/ipc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/model/friendlist/friendlistmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/net/bootstrapnodeupdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ QByteArray serialize(QList<DhtServer> 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);
Expand Down
15 changes: 7 additions & 8 deletions src/widget/form/settings/avform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <QDebug>
#include <QScreen>
#include <QShowEvent>
#include <ranges>
robinlinden marked this conversation as resolved.
Show resolved Hide resolved

#include "audio/audio.h"
#include "audio/iaudiosettings.h"
Expand Down Expand Up @@ -230,9 +231,9 @@ void AVForm::selectBestModes(QVector<VideoMode>& 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())
Expand Down Expand Up @@ -265,8 +266,8 @@ void AVForm::selectBestModes(QVector<VideoMode>& allVideoModes)
}

QVector<VideoMode> 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_);
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/widget/form/settings/userinterfaceform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
46 changes: 22 additions & 24 deletions src/widget/friendlistwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,25 +157,24 @@ void FriendListWidget::sortByMode()
QVector<IFriendListItem*> 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<FriendWidget*>((itemsTmp[i].get())->getWidget());
FriendWidget* frndTmp = qobject_cast<FriendWidget*>((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
Expand All @@ -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<std::shared_ptr<IFriendListItem>> itemsInCircle =
getItemsFromCircle(circles.at(i));
for (int j = 0; j < itemsInCircle.size(); ++j) {
itemsInCircle.at(j)->setNameSortedPos(posByName++);
QVector<std::shared_ptr<IFriendListItem>> itemsInCircle = getItemsFromCircle(circle);
for (const auto& j : itemsInCircle) {
j->setNameSortedPos(posByName++);
}

listLayout->addWidget(circles.at(i));
listLayout->addWidget(circle);
}
}
} else if (mode == SortingMode::Activity) {
Expand Down Expand Up @@ -233,8 +231,8 @@ void FriendListWidget::sortByMode()

QVector<std::shared_ptr<IFriendListItem>> 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();
Expand All @@ -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<int>(getTimeBucket(itemsTmp[i]->getLastActivity()));
for (auto& i : itemsTmp) {
if (i->isFriend()) {
int timeIndex = static_cast<int>(getTimeBucket(i->getLastActivity()));
QWidget* widget = activityLayout->itemAt(timeIndex)->widget();
CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(widget);
FriendWidget* frnd = qobject_cast<FriendWidget*>((itemsTmp[i].get())->getWidget());
FriendWidget* frnd = qobject_cast<FriendWidget*>((i.get())->getWidget());
if (!isVisible() || (isVisible() && frnd->isVisible())) {
categoryWidget->addFriendWidget(frnd, frnd->getFriend()->getStatus());
}
Expand Down Expand Up @@ -320,10 +318,10 @@ QVector<std::shared_ptr<IFriendListItem>> FriendListWidget::getItemsFromCircle(C
{
QVector<std::shared_ptr<IFriendListItem>> itemsTmp = manager->getItems();
QVector<std::shared_ptr<IFriendListItem>> 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;
Expand Down
3 changes: 1 addition & 2 deletions test/model/friendlistmanager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<IFriendListItem>(
new MockFriend(name, isOnline(name), getDateTime(name))));
}
Expand Down
Loading