Browse Source

Clean up code in TrackerListWidget class

adaptive-webui-19844
Chocobo1 5 years ago
parent
commit
662e1e6134
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 121
      src/gui/properties/trackerlistwidget.cpp
  2. 19
      src/gui/properties/trackerlistwidget.h

121
src/gui/properties/trackerlistwidget.cpp

@ -54,6 +54,8 @@
#include "propertieswidget.h" #include "propertieswidget.h"
#include "trackersadditiondialog.h" #include "trackersadditiondialog.h"
#define NB_STICKY_ITEM 3
TrackerListWidget::TrackerListWidget(PropertiesWidget *properties) TrackerListWidget::TrackerListWidget(PropertiesWidget *properties)
: QTreeWidget() : QTreeWidget()
, m_properties(properties) , m_properties(properties)
@ -87,6 +89,7 @@ TrackerListWidget::TrackerListWidget(PropertiesWidget *properties)
connect(header(), &QHeaderView::sectionMoved, this, &TrackerListWidget::saveSettings); connect(header(), &QHeaderView::sectionMoved, this, &TrackerListWidget::saveSettings);
connect(header(), &QHeaderView::sectionResized, this, &TrackerListWidget::saveSettings); connect(header(), &QHeaderView::sectionResized, this, &TrackerListWidget::saveSettings);
connect(header(), &QHeaderView::sortIndicatorChanged, this, &TrackerListWidget::saveSettings); connect(header(), &QHeaderView::sortIndicatorChanged, this, &TrackerListWidget::saveSettings);
// Set DHT, PeX, LSD items // Set DHT, PeX, LSD items
m_DHTItem = new QTreeWidgetItem({ "", "** [DHT] **", "", "0", "", "", "0" }); m_DHTItem = new QTreeWidgetItem({ "", "** [DHT] **", "", "0", "", "", "0" });
insertTopLevelItem(0, m_DHTItem); insertTopLevelItem(0, m_DHTItem);
@ -97,33 +100,38 @@ TrackerListWidget::TrackerListWidget(PropertiesWidget *properties)
m_LSDItem = new QTreeWidgetItem({ "", "** [LSD] **", "", "0", "", "", "0" }); m_LSDItem = new QTreeWidgetItem({ "", "** [LSD] **", "", "0", "", "", "0" });
insertTopLevelItem(2, m_LSDItem); insertTopLevelItem(2, m_LSDItem);
setRowColor(2, QColor("grey")); setRowColor(2, QColor("grey"));
// Set static items alignment // Set static items alignment
m_DHTItem->setTextAlignment(COL_PEERS, (Qt::AlignRight | Qt::AlignVCenter)); const Qt::Alignment alignment = (Qt::AlignRight | Qt::AlignVCenter);
m_PEXItem->setTextAlignment(COL_PEERS, (Qt::AlignRight | Qt::AlignVCenter)); m_DHTItem->setTextAlignment(COL_PEERS, alignment);
m_LSDItem->setTextAlignment(COL_PEERS, (Qt::AlignRight | Qt::AlignVCenter)); m_PEXItem->setTextAlignment(COL_PEERS, alignment);
m_DHTItem->setTextAlignment(COL_SEEDS, (Qt::AlignRight | Qt::AlignVCenter)); m_LSDItem->setTextAlignment(COL_PEERS, alignment);
m_PEXItem->setTextAlignment(COL_SEEDS, (Qt::AlignRight | Qt::AlignVCenter)); m_DHTItem->setTextAlignment(COL_SEEDS, alignment);
m_LSDItem->setTextAlignment(COL_SEEDS, (Qt::AlignRight | Qt::AlignVCenter)); m_PEXItem->setTextAlignment(COL_SEEDS, alignment);
m_DHTItem->setTextAlignment(COL_LEECHES, (Qt::AlignRight | Qt::AlignVCenter)); m_LSDItem->setTextAlignment(COL_SEEDS, alignment);
m_PEXItem->setTextAlignment(COL_LEECHES, (Qt::AlignRight | Qt::AlignVCenter)); m_DHTItem->setTextAlignment(COL_LEECHES, alignment);
m_LSDItem->setTextAlignment(COL_LEECHES, (Qt::AlignRight | Qt::AlignVCenter)); m_PEXItem->setTextAlignment(COL_LEECHES, alignment);
m_DHTItem->setTextAlignment(COL_DOWNLOADED, (Qt::AlignRight | Qt::AlignVCenter)); m_LSDItem->setTextAlignment(COL_LEECHES, alignment);
m_PEXItem->setTextAlignment(COL_DOWNLOADED, (Qt::AlignRight | Qt::AlignVCenter)); m_DHTItem->setTextAlignment(COL_DOWNLOADED, alignment);
m_LSDItem->setTextAlignment(COL_DOWNLOADED, (Qt::AlignRight | Qt::AlignVCenter)); m_PEXItem->setTextAlignment(COL_DOWNLOADED, alignment);
m_LSDItem->setTextAlignment(COL_DOWNLOADED, alignment);
// Set header alignment // Set header alignment
headerItem()->setTextAlignment(COL_TIER, (Qt::AlignRight | Qt::AlignVCenter)); headerItem()->setTextAlignment(COL_TIER, alignment);
headerItem()->setTextAlignment(COL_PEERS, (Qt::AlignRight | Qt::AlignVCenter)); headerItem()->setTextAlignment(COL_PEERS, alignment);
headerItem()->setTextAlignment(COL_SEEDS, (Qt::AlignRight | Qt::AlignVCenter)); headerItem()->setTextAlignment(COL_SEEDS, alignment);
headerItem()->setTextAlignment(COL_LEECHES, (Qt::AlignRight | Qt::AlignVCenter)); headerItem()->setTextAlignment(COL_LEECHES, alignment);
headerItem()->setTextAlignment(COL_DOWNLOADED, (Qt::AlignRight | Qt::AlignVCenter)); headerItem()->setTextAlignment(COL_DOWNLOADED, alignment);
// Set hotkeys // Set hotkeys
m_editHotkey = new QShortcut(Qt::Key_F2, this, nullptr, nullptr, Qt::WidgetShortcut); const auto *editHotkey = new QShortcut(Qt::Key_F2, this, nullptr, nullptr, Qt::WidgetShortcut);
connect(m_editHotkey, &QShortcut::activated, this, &TrackerListWidget::editSelectedTracker); connect(editHotkey, &QShortcut::activated, this, &TrackerListWidget::editSelectedTracker);
const auto *deleteHotkey = new QShortcut(QKeySequence::Delete, this, nullptr, nullptr, Qt::WidgetShortcut);
connect(deleteHotkey, &QShortcut::activated, this, &TrackerListWidget::deleteSelectedTrackers);
const auto *copyHotkey = new QShortcut(QKeySequence::Copy, this, nullptr, nullptr, Qt::WidgetShortcut);
connect(copyHotkey, &QShortcut::activated, this, &TrackerListWidget::copyTrackerUrl);
connect(this, &QAbstractItemView::doubleClicked, this, &TrackerListWidget::editSelectedTracker); connect(this, &QAbstractItemView::doubleClicked, this, &TrackerListWidget::editSelectedTracker);
m_deleteHotkey = new QShortcut(QKeySequence::Delete, this, nullptr, nullptr, Qt::WidgetShortcut);
connect(m_deleteHotkey, &QShortcut::activated, this, &TrackerListWidget::deleteSelectedTrackers);
m_copyHotkey = new QShortcut(QKeySequence::Copy, this, nullptr, nullptr, Qt::WidgetShortcut);
connect(m_copyHotkey, &QShortcut::activated, this, &TrackerListWidget::copyTrackerUrl);
// This hack fixes reordering of first column with Qt5. // This hack fixes reordering of first column with Qt5.
// https://github.com/qtproject/qtbase/commit/e0fc088c0c8bc61dbcaf5928b24986cd61a22777 // https://github.com/qtproject/qtbase/commit/e0fc088c0c8bc61dbcaf5928b24986cd61a22777
@ -138,10 +146,12 @@ TrackerListWidget::~TrackerListWidget()
saveSettings(); saveSettings();
} }
QList<QTreeWidgetItem*> TrackerListWidget::getSelectedTrackerItems() const QVector<QTreeWidgetItem *> TrackerListWidget::getSelectedTrackerItems() const
{ {
const QList<QTreeWidgetItem *> selectedTrackerItems = selectedItems(); const QList<QTreeWidgetItem *> selectedTrackerItems = selectedItems();
QList<QTreeWidgetItem *> selectedTrackers; QVector<QTreeWidgetItem *> selectedTrackers;
selectedTrackers.reserve(selectedTrackerItems.size());
for (QTreeWidgetItem *item : selectedTrackerItems) { for (QTreeWidgetItem *item : selectedTrackerItems) {
if (indexOfTopLevelItem(item) >= NB_STICKY_ITEM) // Ignore STICKY ITEMS if (indexOfTopLevelItem(item) >= NB_STICKY_ITEM) // Ignore STICKY ITEMS
selectedTrackers << item; selectedTrackers << item;
@ -165,7 +175,7 @@ void TrackerListWidget::moveSelectionUp()
clear(); clear();
return; return;
} }
const QList<QTreeWidgetItem *> selectedTrackerItems = getSelectedTrackerItems(); const QVector<QTreeWidgetItem *> selectedTrackerItems = getSelectedTrackerItems();
if (selectedTrackerItems.isEmpty()) return; if (selectedTrackerItems.isEmpty()) return;
bool change = false; bool change = false;
@ -207,7 +217,7 @@ void TrackerListWidget::moveSelectionDown()
clear(); clear();
return; return;
} }
const QList<QTreeWidgetItem *> selectedTrackerItems = getSelectedTrackerItems(); const QVector<QTreeWidgetItem *> selectedTrackerItems = getSelectedTrackerItems();
if (selectedTrackerItems.isEmpty()) return; if (selectedTrackerItems.isEmpty()) return;
bool change = false; bool change = false;
@ -246,6 +256,7 @@ void TrackerListWidget::clear()
{ {
qDeleteAll(m_trackerItems); qDeleteAll(m_trackerItems);
m_trackerItems.clear(); m_trackerItems.clear();
m_DHTItem->setText(COL_STATUS, ""); m_DHTItem->setText(COL_STATUS, "");
m_DHTItem->setText(COL_SEEDS, ""); m_DHTItem->setText(COL_SEEDS, "");
m_DHTItem->setText(COL_LEECHES, ""); m_DHTItem->setText(COL_LEECHES, "");
@ -260,7 +271,7 @@ void TrackerListWidget::clear()
m_LSDItem->setText(COL_MSG, ""); m_LSDItem->setText(COL_MSG, "");
} }
void TrackerListWidget::loadStickyItems(BitTorrent::TorrentHandle *const torrent) void TrackerListWidget::loadStickyItems(const BitTorrent::TorrentHandle *torrent)
{ {
QString working = tr("Working"); QString working = tr("Working");
QString disabled = tr("Disabled"); QString disabled = tr("Disabled");
@ -327,16 +338,18 @@ void TrackerListWidget::loadStickyItems(BitTorrent::TorrentHandle *const torrent
void TrackerListWidget::loadTrackers() void TrackerListWidget::loadTrackers()
{ {
// Load trackers from torrent handle // Load trackers from torrent handle
BitTorrent::TorrentHandle *const torrent = m_properties->getCurrentTorrent(); const BitTorrent::TorrentHandle *torrent = m_properties->getCurrentTorrent();
if (!torrent) return; if (!torrent) return;
loadStickyItems(torrent); loadStickyItems(torrent);
// Load actual trackers information // Load actual trackers information
QHash<QString, BitTorrent::TrackerInfo> trackerData = torrent->trackerInfos(); const QHash<QString, BitTorrent::TrackerInfo> trackerData = torrent->trackerInfos();
QStringList oldTrackerURLs = m_trackerItems.keys(); QStringList oldTrackerURLs = m_trackerItems.keys();
for (const BitTorrent::TrackerEntry &entry : asConst(torrent->trackers())) { for (const BitTorrent::TrackerEntry &entry : asConst(torrent->trackers())) {
QString trackerURL = entry.url(); const QString trackerURL = entry.url();
QTreeWidgetItem *item = m_trackerItems.value(trackerURL, nullptr); QTreeWidgetItem *item = m_trackerItems.value(trackerURL, nullptr);
if (!item) { if (!item) {
item = new QTreeWidgetItem(); item = new QTreeWidgetItem();
@ -347,9 +360,11 @@ void TrackerListWidget::loadTrackers()
else { else {
oldTrackerURLs.removeOne(trackerURL); oldTrackerURLs.removeOne(trackerURL);
} }
item->setText(COL_TIER, QString::number(entry.tier())); item->setText(COL_TIER, QString::number(entry.tier()));
BitTorrent::TrackerInfo data = trackerData.value(trackerURL);
QString errorMessage = data.lastMessage.trimmed(); const BitTorrent::TrackerInfo data = trackerData.value(trackerURL);
switch (entry.status()) { switch (entry.status()) {
case BitTorrent::TrackerEntry::Working: case BitTorrent::TrackerEntry::Working:
item->setText(COL_STATUS, tr("Working")); item->setText(COL_STATUS, tr("Working"));
@ -361,7 +376,7 @@ void TrackerListWidget::loadTrackers()
break; break;
case BitTorrent::TrackerEntry::NotWorking: case BitTorrent::TrackerEntry::NotWorking:
item->setText(COL_STATUS, tr("Not working")); item->setText(COL_STATUS, tr("Not working"));
item->setText(COL_MSG, errorMessage); item->setText(COL_MSG, data.lastMessage.trimmed());
break; break;
case BitTorrent::TrackerEntry::NotContacted: case BitTorrent::TrackerEntry::NotContacted:
item->setText(COL_STATUS, tr("Not contacted yet")); item->setText(COL_STATUS, tr("Not contacted yet"));
@ -370,16 +385,24 @@ void TrackerListWidget::loadTrackers()
} }
item->setText(COL_PEERS, QString::number(data.numPeers)); item->setText(COL_PEERS, QString::number(data.numPeers));
item->setText(COL_SEEDS, (entry.numSeeds() > -1) ? QString::number(entry.numSeeds()) : tr("N/A")); item->setText(COL_SEEDS, ((entry.numSeeds() > -1)
item->setText(COL_LEECHES, (entry.numLeeches() > -1) ? QString::number(entry.numLeeches()) : tr("N/A")); ? QString::number(entry.numSeeds())
item->setText(COL_DOWNLOADED, (entry.numDownloaded() > -1) ? QString::number(entry.numDownloaded()) : tr("N/A")); : tr("N/A")));
item->setText(COL_LEECHES, ((entry.numLeeches() > -1)
item->setTextAlignment(COL_TIER, (Qt::AlignRight | Qt::AlignVCenter)); ? QString::number(entry.numLeeches())
item->setTextAlignment(COL_PEERS, (Qt::AlignRight | Qt::AlignVCenter)); : tr("N/A")));
item->setTextAlignment(COL_SEEDS, (Qt::AlignRight | Qt::AlignVCenter)); item->setText(COL_DOWNLOADED, ((entry.numDownloaded() > -1)
item->setTextAlignment(COL_LEECHES, (Qt::AlignRight | Qt::AlignVCenter)); ? QString::number(entry.numDownloaded())
item->setTextAlignment(COL_DOWNLOADED, (Qt::AlignRight | Qt::AlignVCenter)); : tr("N/A")));
const Qt::Alignment alignment = (Qt::AlignRight | Qt::AlignVCenter);
item->setTextAlignment(COL_TIER, alignment);
item->setTextAlignment(COL_PEERS, alignment);
item->setTextAlignment(COL_SEEDS, alignment);
item->setTextAlignment(COL_LEECHES, alignment);
item->setTextAlignment(COL_DOWNLOADED, alignment);
} }
// Remove old trackers // Remove old trackers
for (const QString &tracker : asConst(oldTrackerURLs)) for (const QString &tracker : asConst(oldTrackerURLs))
delete m_trackerItems.take(tracker); delete m_trackerItems.take(tracker);
@ -400,7 +423,7 @@ void TrackerListWidget::askForTrackers()
void TrackerListWidget::copyTrackerUrl() void TrackerListWidget::copyTrackerUrl()
{ {
const QList<QTreeWidgetItem *> selectedTrackerItems = getSelectedTrackerItems(); const QVector<QTreeWidgetItem *> selectedTrackerItems = getSelectedTrackerItems();
if (selectedTrackerItems.isEmpty()) return; if (selectedTrackerItems.isEmpty()) return;
QStringList urlsToCopy; QStringList urlsToCopy;
@ -421,7 +444,7 @@ void TrackerListWidget::deleteSelectedTrackers()
return; return;
} }
const QList<QTreeWidgetItem *> selectedTrackerItems = getSelectedTrackerItems(); const QVector<QTreeWidgetItem *> selectedTrackerItems = getSelectedTrackerItems();
if (selectedTrackerItems.isEmpty()) return; if (selectedTrackerItems.isEmpty()) return;
QStringList urlsToRemove; QStringList urlsToRemove;
@ -453,7 +476,7 @@ void TrackerListWidget::editSelectedTracker()
BitTorrent::TorrentHandle *const torrent = m_properties->getCurrentTorrent(); BitTorrent::TorrentHandle *const torrent = m_properties->getCurrentTorrent();
if (!torrent) return; if (!torrent) return;
QList<QTreeWidgetItem *> selectedTrackerItems = getSelectedTrackerItems(); const QVector<QTreeWidgetItem *> selectedTrackerItems = getSelectedTrackerItems();
if (selectedTrackerItems.isEmpty()) return; if (selectedTrackerItems.isEmpty()) return;
// During multi-select only process item selected last // During multi-select only process item selected last
@ -574,8 +597,8 @@ void TrackerListWidget::saveSettings() const
QStringList TrackerListWidget::headerLabels() QStringList TrackerListWidget::headerLabels()
{ {
static const QStringList header { return {
tr("Tier") tr("Tier")
, tr("URL") , tr("URL")
, tr("Status") , tr("Status")
, tr("Peers") , tr("Peers")
@ -584,8 +607,6 @@ QStringList TrackerListWidget::headerLabels()
, tr("Downloaded") , tr("Downloaded")
, tr("Message") , tr("Message")
}; };
return header;
} }
int TrackerListWidget::visibleColumnsCount() const int TrackerListWidget::visibleColumnsCount() const

19
src/gui/properties/trackerlistwidget.h

@ -29,14 +29,10 @@
#ifndef TRACKERLIST_H #ifndef TRACKERLIST_H
#define TRACKERLIST_H #define TRACKERLIST_H
#include <QList>
#include <QTreeWidget> #include <QTreeWidget>
#include <QVector>
#include "propertieswidget.h" class PropertiesWidget;
class QShortcut;
#define NB_STICKY_ITEM 3
namespace BitTorrent namespace BitTorrent
{ {
@ -75,7 +71,7 @@ public slots:
void moveSelectionDown(); void moveSelectionDown();
void clear(); void clear();
void loadStickyItems(BitTorrent::TorrentHandle *const torrent); void loadStickyItems(const BitTorrent::TorrentHandle *torrent);
void loadTrackers(); void loadTrackers();
void askForTrackers(); void askForTrackers();
void copyTrackerUrl(); void copyTrackerUrl();
@ -88,19 +84,16 @@ public slots:
void saveSettings() const; void saveSettings() const;
protected: protected:
QList<QTreeWidgetItem *> getSelectedTrackerItems() const; QVector<QTreeWidgetItem *> getSelectedTrackerItems() const;
private: private:
static QStringList headerLabels();
PropertiesWidget *m_properties; PropertiesWidget *m_properties;
QHash<QString, QTreeWidgetItem *> m_trackerItems; QHash<QString, QTreeWidgetItem *> m_trackerItems;
QTreeWidgetItem *m_DHTItem; QTreeWidgetItem *m_DHTItem;
QTreeWidgetItem *m_PEXItem; QTreeWidgetItem *m_PEXItem;
QTreeWidgetItem *m_LSDItem; QTreeWidgetItem *m_LSDItem;
QShortcut *m_editHotkey;
QShortcut *m_deleteHotkey;
QShortcut *m_copyHotkey;
static QStringList headerLabels();
}; };
#endif // TRACKERLIST_H #endif // TRACKERLIST_H

Loading…
Cancel
Save