Skip to content

Commit

Permalink
refactor: prefer std::map over QMap in transmission-qt (#5641)
Browse files Browse the repository at this point in the history
* refactor: use std::map instead of QMap in PrefsDialog.cc

* refactor: use std::map instead of QMap in DetailsDialog.cc

* refactor: use std::map instead of QMap in OptionsDialog.cc

* refactor: use std::map instead of QMap in FileTreeModel.cc
  • Loading branch information
ckerr committed Jun 19, 2023
1 parent 040bc8a commit 4b9b992
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
25 changes: 13 additions & 12 deletions qt/DetailsDialog.cc
Expand Up @@ -21,7 +21,6 @@
#include <QItemSelectionModel>
#include <QLabel>
#include <QList>
#include <QMap>
#include <QMessageBox>
#include <QResizeEvent>
#include <QRegularExpression>
Expand Down Expand Up @@ -1098,20 +1097,23 @@ void DetailsDialog::refreshUI()
/// Peers tab
///

QMap<QString, QTreeWidgetItem*> peers2;
auto peers2 = decltype(peers_){};
QList<QTreeWidgetItem*> new_items;

for (Torrent const* const t : torrents)
{
QString const id_str(QString::number(t->id()));
PeerList const peers = t->peers();

for (Peer const& peer : peers)
for (Peer const& peer : t->peers())
{
QString const key = id_str + QLatin1Char(':') + peer.address;
auto* item = dynamic_cast<PeerItem*>(peers_.value(key, nullptr));

if (item == nullptr) // new peer has connected
PeerItem* item = nullptr;
if (auto iter = peers_.find(key); iter != std::end(peers_))
{
item = dynamic_cast<PeerItem*>(iter->second);
}
else // new peer has connected
{
item = new PeerItem(peer);
item->setTextAlignment(COL_UP, Qt::AlignRight | Qt::AlignVCenter);
Expand All @@ -1124,7 +1126,7 @@ void DetailsDialog::refreshUI()
new_items << item;
}

QString const code = peer.flags;
auto const& code = peer.flags;
item->setStatus(code);
item->refresh(peer);

Expand Down Expand Up @@ -1207,23 +1209,22 @@ void DetailsDialog::refreshUI()
item->setText(COL_STATUS, code);
item->setToolTip(COL_STATUS, code_tip);

peers2.insert(key, item);
peers2.try_emplace(key, item);
}
}

ui_.peersView->addTopLevelItems(new_items);

for (QString const& key : peers_.keys())
for (auto const& [key, item] : peers_)
{
if (!peers2.contains(key)) // old peer has disconnected
if (peers2.count(key) == 0U) // old peer has disconnected
{
QTreeWidgetItem* item = peers_.value(key, nullptr);
ui_.peersView->takeTopLevelItem(ui_.peersView->indexOfTopLevelItem(item));
delete item;
}
}

peers_ = peers2;
peers_ = std::move(peers2);

if (single)
{
Expand Down
4 changes: 2 additions & 2 deletions qt/DetailsDialog.h
Expand Up @@ -5,11 +5,11 @@

#pragma once

#include <map>
#include <memory>
#include <unordered_set>

#include <QString>
#include <QMap>
#include <QSet>
#include <QTimer>

Expand Down Expand Up @@ -137,7 +137,7 @@ private slots:
std::shared_ptr<TrackerModelFilter> tracker_filter_;
std::shared_ptr<TrackerDelegate> tracker_delegate_;

QMap<QString, QTreeWidgetItem*> peers_;
std::map<QString, QTreeWidgetItem*> peers_;

QIcon const icon_encrypted_ = QIcon(QStringLiteral(":/icons/encrypted.svg"));
QIcon const icon_unencrypted_ = {};
Expand Down
9 changes: 5 additions & 4 deletions qt/FileTreeModel.cc
Expand Up @@ -5,6 +5,7 @@

#include <algorithm>
#include <cassert>
#include <map>
#include <memory>

#include <libtransmission/transmission.h> // priorities
Expand Down Expand Up @@ -463,7 +464,7 @@ void FileTreeModel::emitSubtreeChanged(QModelIndex const& idx, int first_column,

void FileTreeModel::twiddleWanted(QModelIndexList const& indices)
{
QMap<bool, QModelIndexList> wanted_indices;
std::map<bool, QModelIndexList> wanted_indices;

for (QModelIndex const& i : getOrphanIndices(indices))
{
Expand All @@ -473,7 +474,7 @@ void FileTreeModel::twiddleWanted(QModelIndexList const& indices)

for (int i = 0; i <= 1; ++i)
{
if (wanted_indices.contains(i))
if (wanted_indices.count(i) != 0)
{
setWanted(wanted_indices[i], i != 0);
}
Expand All @@ -482,7 +483,7 @@ void FileTreeModel::twiddleWanted(QModelIndexList const& indices)

void FileTreeModel::twiddlePriority(QModelIndexList const& indices)
{
QMap<int, QModelIndexList> priority_indices;
std::map<int, QModelIndexList> priority_indices;

for (QModelIndex const& i : getOrphanIndices(indices))
{
Expand All @@ -508,7 +509,7 @@ void FileTreeModel::twiddlePriority(QModelIndexList const& indices)

for (int i = TR_PRI_LOW; i <= TR_PRI_HIGH; ++i)
{
if (priority_indices.contains(i))
if (priority_indices.count(i) != 0U)
{
setPriority(priority_indices[i], i);
}
Expand Down
4 changes: 2 additions & 2 deletions qt/OptionsDialog.h
Expand Up @@ -5,12 +5,12 @@

#pragma once

#include <map>
#include <optional>
#include <vector>

#include <QDir>
#include <QFile>
#include <QMap>
#include <QSet>
#include <QString>
#include <QTimer>
Expand Down Expand Up @@ -54,7 +54,7 @@ private slots:
void onSessionUpdated();

private:
using mybins_t = QMap<uint32_t, int32_t>;
using mybins_t = std::map<uint32_t, int32_t>;

void reload();
void updateWidgetsLocality();
Expand Down
8 changes: 3 additions & 5 deletions qt/PrefsDialog.cc
Expand Up @@ -203,7 +203,7 @@ void PrefsDialog::linkWidgetToPref(QWidget* widget, int pref_key)

pref_widget.setPrefKey(pref_key);
updateWidgetValue(widget, pref_key);
widgets_.insert(pref_key, widget);
widgets_.try_emplace(pref_key, widget);

if (auto const* check_box = qobject_cast<QCheckBox*>(widget); check_box != nullptr)
{
Expand Down Expand Up @@ -807,11 +807,9 @@ void PrefsDialog::refreshPref(int key)
break;
}

key2widget_t::iterator const it(widgets_.find(key));

if (it != widgets_.end())
if (auto iter = widgets_.find(key); iter != std::end(widgets_))
{
QWidget* w(it.value());
QWidget* const w = iter->second;

w->blockSignals(true);

Expand Down
4 changes: 2 additions & 2 deletions qt/PrefsDialog.h
Expand Up @@ -5,7 +5,7 @@

#pragma once

#include <QMap>
#include <map>

#include <libtransmission/tr-macros.h>

Expand Down Expand Up @@ -50,7 +50,7 @@ private slots:
void onBlocklistUpdated(int n);

private:
using key2widget_t = QMap<int, QWidget*>;
using key2widget_t = std::map<int, QWidget*>;

bool updateWidgetValue(QWidget* widget, int pref_key) const;
void linkWidgetToPref(QWidget* widget, int pref_key);
Expand Down

0 comments on commit 4b9b992

Please sign in to comment.