Browse Source

Use default delegate for PeerListWidget

Removes the usage of QItemDelegate
adaptive-webui-19844
jagannatharjun 4 years ago
parent
commit
13aab6e04d
  1. 2
      src/gui/CMakeLists.txt
  2. 2
      src/gui/gui.pri
  3. 92
      src/gui/properties/peerlistdelegate.cpp
  4. 67
      src/gui/properties/peerlistdelegate.h
  5. 11
      src/gui/properties/peerlistsortmodel.cpp
  6. 5
      src/gui/properties/peerlistsortmodel.h
  7. 138
      src/gui/properties/peerlistwidget.cpp
  8. 20
      src/gui/properties/peerlistwidget.h

2
src/gui/CMakeLists.txt

@ -30,7 +30,6 @@ add_library(qbt_gui STATIC
previewlistdelegate.h previewlistdelegate.h
previewselectdialog.h previewselectdialog.h
properties/downloadedpiecesbar.h properties/downloadedpiecesbar.h
properties/peerlistdelegate.h
properties/peerlistsortmodel.h properties/peerlistsortmodel.h
properties/peerlistwidget.h properties/peerlistwidget.h
properties/peersadditiondialog.h properties/peersadditiondialog.h
@ -111,7 +110,6 @@ add_library(qbt_gui STATIC
previewlistdelegate.cpp previewlistdelegate.cpp
previewselectdialog.cpp previewselectdialog.cpp
properties/downloadedpiecesbar.cpp properties/downloadedpiecesbar.cpp
properties/peerlistdelegate.cpp
properties/peerlistsortmodel.cpp properties/peerlistsortmodel.cpp
properties/peerlistwidget.cpp properties/peerlistwidget.cpp
properties/peersadditiondialog.cpp properties/peersadditiondialog.cpp

2
src/gui/gui.pri

@ -28,7 +28,6 @@ HEADERS += \
$$PWD/previewlistdelegate.h \ $$PWD/previewlistdelegate.h \
$$PWD/previewselectdialog.h \ $$PWD/previewselectdialog.h \
$$PWD/properties/downloadedpiecesbar.h \ $$PWD/properties/downloadedpiecesbar.h \
$$PWD/properties/peerlistdelegate.h \
$$PWD/properties/peerlistsortmodel.h \ $$PWD/properties/peerlistsortmodel.h \
$$PWD/properties/peerlistwidget.h \ $$PWD/properties/peerlistwidget.h \
$$PWD/properties/peersadditiondialog.h \ $$PWD/properties/peersadditiondialog.h \
@ -109,7 +108,6 @@ SOURCES += \
$$PWD/previewlistdelegate.cpp \ $$PWD/previewlistdelegate.cpp \
$$PWD/previewselectdialog.cpp \ $$PWD/previewselectdialog.cpp \
$$PWD/properties/downloadedpiecesbar.cpp \ $$PWD/properties/downloadedpiecesbar.cpp \
$$PWD/properties/peerlistdelegate.cpp \
$$PWD/properties/peerlistsortmodel.cpp \ $$PWD/properties/peerlistsortmodel.cpp \
$$PWD/properties/peerlistwidget.cpp \ $$PWD/properties/peerlistwidget.cpp \
$$PWD/properties/peersadditiondialog.cpp \ $$PWD/properties/peersadditiondialog.cpp \

92
src/gui/properties/peerlistdelegate.cpp

@ -1,92 +0,0 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include "peerlistdelegate.h"
#include <QPainter>
#include "base/preferences.h"
#include "base/utils/misc.h"
#include "base/utils/string.h"
PeerListDelegate::PeerListDelegate(QObject *parent)
: QItemDelegate(parent)
{
}
void PeerListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
painter->save();
const bool hideValues = Preferences::instance()->getHideZeroValues();
QStyleOptionViewItem opt = QItemDelegate::setOptions(index, option);
QItemDelegate::drawBackground(painter, opt, index);
switch (index.column()) {
case PORT:
opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
QItemDelegate::drawDisplay(painter, opt, option.rect, index.data().toString());
break;
case TOT_DOWN:
case TOT_UP: {
const qlonglong size = index.data().toLongLong();
if (hideValues && (size <= 0))
break;
opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
QItemDelegate::drawDisplay(painter, opt, option.rect, Utils::Misc::friendlyUnit(size));
}
break;
case DOWN_SPEED:
case UP_SPEED: {
const int speed = index.data().toInt();
if (speed <= 0)
break;
opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
QItemDelegate::drawDisplay(painter, opt, opt.rect, Utils::Misc::friendlyUnit(speed, true));
}
break;
case PROGRESS:
case RELEVANCE: {
const qreal progress = index.data().toReal();
opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
QItemDelegate::drawDisplay(painter, opt, opt.rect, (Utils::String::fromDouble(progress * 100, 1) + '%'));
}
break;
default:
QItemDelegate::paint(painter, option, index);
}
painter->restore();
}
QWidget *PeerListDelegate::createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const
{
// No editor here
return nullptr;
}

67
src/gui/properties/peerlistdelegate.h

@ -1,67 +0,0 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#ifndef PEERLISTDELEGATE_H
#define PEERLISTDELEGATE_H
#include <QItemDelegate>
class PeerListDelegate : public QItemDelegate
{
Q_OBJECT
Q_DISABLE_COPY(PeerListDelegate)
public:
enum PeerListColumns
{
COUNTRY,
IP,
PORT,
CONNECTION,
FLAGS,
CLIENT,
PROGRESS,
DOWN_SPEED,
UP_SPEED,
TOT_DOWN,
TOT_UP,
RELEVANCE,
DOWNLOADING_PIECE,
IP_HIDDEN,
COL_COUNT
};
explicit PeerListDelegate(QObject *parent);
private:
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
QWidget *createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const override;
};
#endif // PEERLISTDELEGATE_H

11
src/gui/properties/peerlistsortmodel.cpp

@ -29,20 +29,21 @@
#include "peerlistsortmodel.h" #include "peerlistsortmodel.h"
#include "base/utils/string.h" #include "base/utils/string.h"
#include "peerlistdelegate.h" #include "peerlistwidget.h"
PeerListSortModel::PeerListSortModel(QObject *parent) PeerListSortModel::PeerListSortModel(QObject *parent)
: QSortFilterProxyModel(parent) : QSortFilterProxyModel(parent)
{ {
setSortRole(UnderlyingDataRole);
} }
bool PeerListSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const bool PeerListSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{ {
switch (sortColumn()) { switch (sortColumn()) {
case PeerListDelegate::IP: case PeerListWidget::IP:
case PeerListDelegate::CLIENT: { case PeerListWidget::CLIENT: {
const QString strL = left.data().toString(); const QString strL = left.data(UnderlyingDataRole).toString();
const QString strR = right.data().toString(); const QString strR = right.data(UnderlyingDataRole).toString();
const int result = Utils::String::naturalCompare(strL, strR, Qt::CaseInsensitive); const int result = Utils::String::naturalCompare(strL, strR, Qt::CaseInsensitive);
return (result < 0); return (result < 0);
} }

5
src/gui/properties/peerlistsortmodel.h

@ -37,6 +37,11 @@ class PeerListSortModel final : public QSortFilterProxyModel
Q_DISABLE_COPY(PeerListSortModel) Q_DISABLE_COPY(PeerListSortModel)
public: public:
enum
{
UnderlyingDataRole = Qt::UserRole
};
explicit PeerListSortModel(QObject *parent = nullptr); explicit PeerListSortModel(QObject *parent = nullptr);
private: private:

138
src/gui/properties/peerlistwidget.cpp

@ -52,8 +52,9 @@
#include "base/net/geoipmanager.h" #include "base/net/geoipmanager.h"
#include "base/net/reverseresolution.h" #include "base/net/reverseresolution.h"
#include "base/preferences.h" #include "base/preferences.h"
#include "base/utils/misc.h"
#include "base/utils/string.h"
#include "gui/uithememanager.h" #include "gui/uithememanager.h"
#include "peerlistdelegate.h"
#include "peerlistsortmodel.h" #include "peerlistsortmodel.h"
#include "peersadditiondialog.h" #include "peersadditiondialog.h"
#include "propertieswidget.h" #include "propertieswidget.h"
@ -88,61 +89,59 @@ PeerListWidget::PeerListWidget(PropertiesWidget *parent)
setSelectionMode(QAbstractItemView::ExtendedSelection); setSelectionMode(QAbstractItemView::ExtendedSelection);
header()->setStretchLastSection(false); header()->setStretchLastSection(false);
// List Model // List Model
m_listModel = new QStandardItemModel(0, PeerListDelegate::COL_COUNT, this); m_listModel = new QStandardItemModel(0, PeerListColumns::COL_COUNT, this);
m_listModel->setHeaderData(PeerListDelegate::COUNTRY, Qt::Horizontal, tr("Country/Region")); // Country flag column m_listModel->setHeaderData(PeerListColumns::COUNTRY, Qt::Horizontal, tr("Country/Region")); // Country flag column
m_listModel->setHeaderData(PeerListDelegate::IP, Qt::Horizontal, tr("IP")); m_listModel->setHeaderData(PeerListColumns::IP, Qt::Horizontal, tr("IP"));
m_listModel->setHeaderData(PeerListDelegate::PORT, Qt::Horizontal, tr("Port")); m_listModel->setHeaderData(PeerListColumns::PORT, Qt::Horizontal, tr("Port"));
m_listModel->setHeaderData(PeerListDelegate::FLAGS, Qt::Horizontal, tr("Flags")); m_listModel->setHeaderData(PeerListColumns::FLAGS, Qt::Horizontal, tr("Flags"));
m_listModel->setHeaderData(PeerListDelegate::CONNECTION, Qt::Horizontal, tr("Connection")); m_listModel->setHeaderData(PeerListColumns::CONNECTION, Qt::Horizontal, tr("Connection"));
m_listModel->setHeaderData(PeerListDelegate::CLIENT, Qt::Horizontal, tr("Client", "i.e.: Client application")); m_listModel->setHeaderData(PeerListColumns::CLIENT, Qt::Horizontal, tr("Client", "i.e.: Client application"));
m_listModel->setHeaderData(PeerListDelegate::PROGRESS, Qt::Horizontal, tr("Progress", "i.e: % downloaded")); m_listModel->setHeaderData(PeerListColumns::PROGRESS, Qt::Horizontal, tr("Progress", "i.e: % downloaded"));
m_listModel->setHeaderData(PeerListDelegate::DOWN_SPEED, Qt::Horizontal, tr("Down Speed", "i.e: Download speed")); m_listModel->setHeaderData(PeerListColumns::DOWN_SPEED, Qt::Horizontal, tr("Down Speed", "i.e: Download speed"));
m_listModel->setHeaderData(PeerListDelegate::UP_SPEED, Qt::Horizontal, tr("Up Speed", "i.e: Upload speed")); m_listModel->setHeaderData(PeerListColumns::UP_SPEED, Qt::Horizontal, tr("Up Speed", "i.e: Upload speed"));
m_listModel->setHeaderData(PeerListDelegate::TOT_DOWN, Qt::Horizontal, tr("Downloaded", "i.e: total data downloaded")); m_listModel->setHeaderData(PeerListColumns::TOT_DOWN, Qt::Horizontal, tr("Downloaded", "i.e: total data downloaded"));
m_listModel->setHeaderData(PeerListDelegate::TOT_UP, Qt::Horizontal, tr("Uploaded", "i.e: total data uploaded")); m_listModel->setHeaderData(PeerListColumns::TOT_UP, Qt::Horizontal, tr("Uploaded", "i.e: total data uploaded"));
m_listModel->setHeaderData(PeerListDelegate::RELEVANCE, Qt::Horizontal, tr("Relevance", "i.e: How relevant this peer is to us. How many pieces it has that we don't.")); m_listModel->setHeaderData(PeerListColumns::RELEVANCE, Qt::Horizontal, tr("Relevance", "i.e: How relevant this peer is to us. How many pieces it has that we don't."));
m_listModel->setHeaderData(PeerListDelegate::DOWNLOADING_PIECE, Qt::Horizontal, tr("Files", "i.e. files that are being downloaded right now")); m_listModel->setHeaderData(PeerListColumns::DOWNLOADING_PIECE, Qt::Horizontal, tr("Files", "i.e. files that are being downloaded right now"));
// Set header text alignment // Set header text alignment
m_listModel->setHeaderData(PeerListDelegate::PORT, Qt::Horizontal, QVariant(Qt::AlignRight | Qt::AlignVCenter), Qt::TextAlignmentRole); m_listModel->setHeaderData(PeerListColumns::PORT, Qt::Horizontal, QVariant(Qt::AlignRight | Qt::AlignVCenter), Qt::TextAlignmentRole);
m_listModel->setHeaderData(PeerListDelegate::PROGRESS, Qt::Horizontal, QVariant(Qt::AlignRight | Qt::AlignVCenter), Qt::TextAlignmentRole); m_listModel->setHeaderData(PeerListColumns::PROGRESS, Qt::Horizontal, QVariant(Qt::AlignRight | Qt::AlignVCenter), Qt::TextAlignmentRole);
m_listModel->setHeaderData(PeerListDelegate::DOWN_SPEED, Qt::Horizontal, QVariant(Qt::AlignRight | Qt::AlignVCenter), Qt::TextAlignmentRole); m_listModel->setHeaderData(PeerListColumns::DOWN_SPEED, Qt::Horizontal, QVariant(Qt::AlignRight | Qt::AlignVCenter), Qt::TextAlignmentRole);
m_listModel->setHeaderData(PeerListDelegate::UP_SPEED, Qt::Horizontal, QVariant(Qt::AlignRight | Qt::AlignVCenter), Qt::TextAlignmentRole); m_listModel->setHeaderData(PeerListColumns::UP_SPEED, Qt::Horizontal, QVariant(Qt::AlignRight | Qt::AlignVCenter), Qt::TextAlignmentRole);
m_listModel->setHeaderData(PeerListDelegate::TOT_DOWN, Qt::Horizontal, QVariant(Qt::AlignRight | Qt::AlignVCenter), Qt::TextAlignmentRole); m_listModel->setHeaderData(PeerListColumns::TOT_DOWN, Qt::Horizontal, QVariant(Qt::AlignRight | Qt::AlignVCenter), Qt::TextAlignmentRole);
m_listModel->setHeaderData(PeerListDelegate::TOT_UP, Qt::Horizontal, QVariant(Qt::AlignRight | Qt::AlignVCenter), Qt::TextAlignmentRole); m_listModel->setHeaderData(PeerListColumns::TOT_UP, Qt::Horizontal, QVariant(Qt::AlignRight | Qt::AlignVCenter), Qt::TextAlignmentRole);
m_listModel->setHeaderData(PeerListDelegate::RELEVANCE, Qt::Horizontal, QVariant(Qt::AlignRight | Qt::AlignVCenter), Qt::TextAlignmentRole); m_listModel->setHeaderData(PeerListColumns::RELEVANCE, Qt::Horizontal, QVariant(Qt::AlignRight | Qt::AlignVCenter), Qt::TextAlignmentRole);
// Proxy model to support sorting without actually altering the underlying model // Proxy model to support sorting without actually altering the underlying model
m_proxyModel = new PeerListSortModel(this); m_proxyModel = new PeerListSortModel(this);
m_proxyModel->setDynamicSortFilter(true); m_proxyModel->setDynamicSortFilter(true);
m_proxyModel->setSourceModel(m_listModel); m_proxyModel->setSourceModel(m_listModel);
m_proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive); m_proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
setModel(m_proxyModel); setModel(m_proxyModel);
hideColumn(PeerListDelegate::IP_HIDDEN); hideColumn(PeerListColumns::IP_HIDDEN);
hideColumn(PeerListDelegate::COL_COUNT); hideColumn(PeerListColumns::COL_COUNT);
m_resolveCountries = Preferences::instance()->resolvePeerCountries(); m_resolveCountries = Preferences::instance()->resolvePeerCountries();
if (!m_resolveCountries) if (!m_resolveCountries)
hideColumn(PeerListDelegate::COUNTRY); hideColumn(PeerListColumns::COUNTRY);
// Ensure that at least one column is visible at all times // Ensure that at least one column is visible at all times
bool atLeastOne = false; bool atLeastOne = false;
for (int i = 0; i < PeerListDelegate::IP_HIDDEN; ++i) { for (int i = 0; i < PeerListColumns::IP_HIDDEN; ++i) {
if (!isColumnHidden(i)) { if (!isColumnHidden(i)) {
atLeastOne = true; atLeastOne = true;
break; break;
} }
} }
if (!atLeastOne) if (!atLeastOne)
setColumnHidden(PeerListDelegate::IP, false); setColumnHidden(PeerListColumns::IP, false);
// To also mitigate the above issue, we have to resize each column when // To also mitigate the above issue, we have to resize each column when
// its size is 0, because explicitly 'showing' the column isn't enough // its size is 0, because explicitly 'showing' the column isn't enough
// in the above scenario. // in the above scenario.
for (int i = 0; i < PeerListDelegate::IP_HIDDEN; ++i) { for (int i = 0; i < PeerListColumns::IP_HIDDEN; ++i) {
if ((columnWidth(i) <= 0) && !isColumnHidden(i)) if ((columnWidth(i) <= 0) && !isColumnHidden(i))
resizeColumnToContents(i); resizeColumnToContents(i);
} }
// Context menu // Context menu
setContextMenuPolicy(Qt::CustomContextMenu); setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, &QWidget::customContextMenuRequested, this, &PeerListWidget::showPeerListMenu); connect(this, &QWidget::customContextMenuRequested, this, &PeerListWidget::showPeerListMenu);
// List delegate
setItemDelegate(new PeerListDelegate(this));
// Enable sorting // Enable sorting
setSortingEnabled(true); setSortingEnabled(true);
// IP to Hostname resolver // IP to Hostname resolver
@ -177,8 +176,8 @@ void PeerListWidget::displayToggleColumnsMenu(const QPoint &)
menu->setAttribute(Qt::WA_DeleteOnClose); menu->setAttribute(Qt::WA_DeleteOnClose);
menu->setTitle(tr("Column visibility")); menu->setTitle(tr("Column visibility"));
for (int i = 0; i < PeerListDelegate::IP_HIDDEN; ++i) { for (int i = 0; i < PeerListColumns::IP_HIDDEN; ++i) {
if ((i == PeerListDelegate::COUNTRY) && !Preferences::instance()->resolvePeerCountries()) if ((i == PeerListColumns::COUNTRY) && !Preferences::instance()->resolvePeerCountries())
continue; continue;
QAction *myAct = menu->addAction(m_listModel->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString()); QAction *myAct = menu->addAction(m_listModel->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
@ -190,7 +189,7 @@ void PeerListWidget::displayToggleColumnsMenu(const QPoint &)
connect(menu, &QMenu::triggered, this, [this](const QAction *action) connect(menu, &QMenu::triggered, this, [this](const QAction *action)
{ {
int visibleCols = 0; int visibleCols = 0;
for (int i = 0; i < PeerListDelegate::IP_HIDDEN; ++i) { for (int i = 0; i < PeerListColumns::IP_HIDDEN; ++i) {
if (!isColumnHidden(i)) if (!isColumnHidden(i))
++visibleCols; ++visibleCols;
@ -238,12 +237,12 @@ void PeerListWidget::updatePeerCountryResolutionState()
m_resolveCountries = resolveCountries; m_resolveCountries = resolveCountries;
if (m_resolveCountries) { if (m_resolveCountries) {
loadPeers(m_properties->getCurrentTorrent()); loadPeers(m_properties->getCurrentTorrent());
showColumn(PeerListDelegate::COUNTRY); showColumn(PeerListColumns::COUNTRY);
if (columnWidth(PeerListDelegate::COUNTRY) <= 0) if (columnWidth(PeerListColumns::COUNTRY) <= 0)
resizeColumnToContents(PeerListDelegate::COUNTRY); resizeColumnToContents(PeerListColumns::COUNTRY);
} }
else { else {
hideColumn(PeerListDelegate::COUNTRY); hideColumn(PeerListColumns::COUNTRY);
} }
} }
@ -299,7 +298,7 @@ void PeerListWidget::banSelectedPeers()
const QModelIndexList selectedIndexes = selectionModel()->selectedRows(); const QModelIndexList selectedIndexes = selectionModel()->selectedRows();
for (const QModelIndex &index : selectedIndexes) { for (const QModelIndex &index : selectedIndexes) {
const int row = m_proxyModel->mapToSource(index).row(); const int row = m_proxyModel->mapToSource(index).row();
const QString ip = m_listModel->item(row, PeerListDelegate::IP_HIDDEN)->text(); const QString ip = m_listModel->item(row, PeerListColumns::IP_HIDDEN)->text();
BitTorrent::Session::instance()->banIP(ip); BitTorrent::Session::instance()->banIP(ip);
LogMsg(tr("Peer \"%1\" is manually banned").arg(ip)); LogMsg(tr("Peer \"%1\" is manually banned").arg(ip));
} }
@ -314,8 +313,8 @@ void PeerListWidget::copySelectedPeers()
for (const QModelIndex &index : selectedIndexes) { for (const QModelIndex &index : selectedIndexes) {
const int row = m_proxyModel->mapToSource(index).row(); const int row = m_proxyModel->mapToSource(index).row();
const QString ip = m_listModel->item(row, PeerListDelegate::IP_HIDDEN)->text(); const QString ip = m_listModel->item(row, PeerListColumns::IP_HIDDEN)->text();
const QString port = m_listModel->item(row, PeerListDelegate::PORT)->text(); const QString port = m_listModel->item(row, PeerListColumns::PORT)->text();
if (!ip.contains('.')) // IPv6 if (!ip.contains('.')) // IPv6
selectedPeers << ('[' + ip + "]:" + port); selectedPeers << ('[' + ip + "]:" + port);
@ -382,6 +381,22 @@ void PeerListWidget::updatePeer(const BitTorrent::TorrentHandle *torrent, const
{ {
const PeerEndpoint peerEndpoint {peer.address(), peer.connectionType()}; const PeerEndpoint peerEndpoint {peer.address(), peer.connectionType()};
const QString peerIp = peerEndpoint.address.ip.toString(); const QString peerIp = peerEndpoint.address.ip.toString();
const Qt::Alignment intDataTextAlignment = Qt::AlignRight | Qt::AlignVCenter;
const auto setModelData =
[this] (const int row, const int column, const QString &displayData
, const QVariant &underlyingData, const Qt::Alignment textAlignmentData = {}
, const QString &toolTip = {})
{
const QMap<int, QVariant> data =
{
{Qt::DisplayRole, displayData},
{PeerListSortModel::UnderlyingDataRole, underlyingData},
{Qt::TextAlignmentRole, QVariant {textAlignmentData}},
{Qt::ToolTipRole, toolTip}
};
m_listModel->setItemData(m_listModel->index(row, column), data);
};
auto itemIter = m_peerItems.find(peerEndpoint); auto itemIter = m_peerItems.find(peerEndpoint);
isNewPeer = (itemIter == m_peerItems.end()); isNewPeer = (itemIter == m_peerItems.end());
@ -389,30 +404,31 @@ void PeerListWidget::updatePeer(const BitTorrent::TorrentHandle *torrent, const
// new item // new item
const int row = m_listModel->rowCount(); const int row = m_listModel->rowCount();
m_listModel->insertRow(row); m_listModel->insertRow(row);
m_listModel->setData(m_listModel->index(row, PeerListDelegate::IP), peerIp);
m_listModel->setData(m_listModel->index(row, PeerListDelegate::IP), peerIp, Qt::ToolTipRole);
m_listModel->setData(m_listModel->index(row, PeerListDelegate::PORT), peer.address().port);
m_listModel->setData(m_listModel->index(row, PeerListDelegate::IP_HIDDEN), peerIp);
itemIter = m_peerItems.insert(peerEndpoint, m_listModel->item(row, PeerListDelegate::IP)); setModelData(row, PeerListColumns::IP, peerIp, peerIp, {}, peerIp);
setModelData(row, PeerListColumns::PORT, QString::number(peer.address().port), peer.address().port, intDataTextAlignment);
setModelData(row, PeerListColumns::IP_HIDDEN, peerIp, peerIp);
itemIter = m_peerItems.insert(peerEndpoint, m_listModel->item(row, PeerListColumns::IP));
m_itemsByIP[peerEndpoint.address.ip].insert(itemIter.value()); m_itemsByIP[peerEndpoint.address.ip].insert(itemIter.value());
} }
const int row = (*itemIter)->row(); const int row = (*itemIter)->row();
m_listModel->setData(m_listModel->index(row, PeerListDelegate::CONNECTION), peer.connectionType());
m_listModel->setData(m_listModel->index(row, PeerListDelegate::FLAGS), peer.flags()); setModelData(row, PeerListColumns::CONNECTION, peer.connectionType(), peer.connectionType());
m_listModel->setData(m_listModel->index(row, PeerListDelegate::FLAGS), peer.flagsDescription(), Qt::ToolTipRole); setModelData(row, PeerListColumns::FLAGS, peer.flags(), peer.flags(), {}, peer.flagsDescription());
m_listModel->setData(m_listModel->index(row, PeerListDelegate::CLIENT), peer.client().toHtmlEscaped()); const QString client = peer.client().toHtmlEscaped();
m_listModel->setData(m_listModel->index(row, PeerListDelegate::PROGRESS), peer.progress()); setModelData(row, PeerListColumns::CLIENT, client, client);
m_listModel->setData(m_listModel->index(row, PeerListDelegate::DOWN_SPEED), peer.payloadDownSpeed()); setModelData(row, PeerListColumns::PROGRESS, (Utils::String::fromDouble(peer.progress() * 100, 1) + '%'), peer.progress(), intDataTextAlignment);
m_listModel->setData(m_listModel->index(row, PeerListDelegate::UP_SPEED), peer.payloadUpSpeed()); setModelData(row, PeerListColumns::DOWN_SPEED, Utils::Misc::friendlyUnit(peer.payloadDownSpeed(), 1), peer.payloadDownSpeed(), intDataTextAlignment);
m_listModel->setData(m_listModel->index(row, PeerListDelegate::TOT_DOWN), peer.totalDownload()); setModelData(row, PeerListColumns::UP_SPEED, Utils::Misc::friendlyUnit(peer.payloadUpSpeed(), true), peer.payloadUpSpeed(), intDataTextAlignment);
m_listModel->setData(m_listModel->index(row, PeerListDelegate::TOT_UP), peer.totalUpload()); setModelData(row, PeerListColumns::TOT_DOWN, Utils::Misc::friendlyUnit(peer.totalDownload()), peer.totalDownload(), intDataTextAlignment);
m_listModel->setData(m_listModel->index(row, PeerListDelegate::RELEVANCE), peer.relevance()); setModelData(row, PeerListColumns::TOT_UP, Utils::Misc::friendlyUnit(peer.totalUpload()), peer.totalUpload(), intDataTextAlignment);
setModelData(row, PeerListColumns::RELEVANCE, (Utils::String::fromDouble(peer.relevance() * 100, 1) + '%'), peer.relevance(), intDataTextAlignment);
const QStringList downloadingFiles {torrent->info().filesForPiece(peer.downloadingPieceIndex())}; const QStringList downloadingFiles {torrent->info().filesForPiece(peer.downloadingPieceIndex())};
m_listModel->setData(m_listModel->index(row, PeerListDelegate::DOWNLOADING_PIECE), downloadingFiles.join(';')); const QString downloadingFilesDisplayValue = downloadingFiles.join(';');
m_listModel->setData(m_listModel->index(row, PeerListDelegate::DOWNLOADING_PIECE), downloadingFiles.join('\n'), Qt::ToolTipRole); setModelData(row, PeerListColumns::DOWNLOADING_PIECE, downloadingFilesDisplayValue, downloadingFilesDisplayValue, {}, downloadingFiles.join('\n'));
if (m_resolver) if (m_resolver)
m_resolver->resolve(peerEndpoint.address.ip); m_resolver->resolve(peerEndpoint.address.ip);
@ -420,9 +436,9 @@ void PeerListWidget::updatePeer(const BitTorrent::TorrentHandle *torrent, const
if (m_resolveCountries) { if (m_resolveCountries) {
const QIcon icon = UIThemeManager::instance()->getFlagIcon(peer.country()); const QIcon icon = UIThemeManager::instance()->getFlagIcon(peer.country());
if (!icon.isNull()) { if (!icon.isNull()) {
m_listModel->setData(m_listModel->index(row, PeerListDelegate::COUNTRY), icon, Qt::DecorationRole); m_listModel->setData(m_listModel->index(row, PeerListColumns::COUNTRY), icon, Qt::DecorationRole);
const QString countryName = Net::GeoIPManager::CountryName(peer.country()); const QString countryName = Net::GeoIPManager::CountryName(peer.country());
m_listModel->setData(m_listModel->index(row, PeerListDelegate::COUNTRY), countryName, Qt::ToolTipRole); m_listModel->setData(m_listModel->index(row, PeerListColumns::COUNTRY), countryName, Qt::ToolTipRole);
} }
} }
} }
@ -439,10 +455,10 @@ void PeerListWidget::handleResolved(const QHostAddress &ip, const QString &hostn
void PeerListWidget::handleSortColumnChanged(const int col) void PeerListWidget::handleSortColumnChanged(const int col)
{ {
if (col == PeerListDelegate::COUNTRY) if (col == PeerListColumns::COUNTRY)
m_proxyModel->setSortRole(Qt::ToolTipRole); m_proxyModel->setSortRole(Qt::ToolTipRole);
else else
m_proxyModel->setSortRole(Qt::DisplayRole); m_proxyModel->setSortRole(PeerListSortModel::UnderlyingDataRole);
} }
void PeerListWidget::wheelEvent(QWheelEvent *event) void PeerListWidget::wheelEvent(QWheelEvent *event)

20
src/gui/properties/peerlistwidget.h

@ -58,6 +58,26 @@ class PeerListWidget final : public QTreeView
Q_OBJECT Q_OBJECT
public: public:
enum PeerListColumns
{
COUNTRY,
IP,
PORT,
CONNECTION,
FLAGS,
CLIENT,
PROGRESS,
DOWN_SPEED,
UP_SPEED,
TOT_DOWN,
TOT_UP,
RELEVANCE,
DOWNLOADING_PIECE,
IP_HIDDEN,
COL_COUNT
};
explicit PeerListWidget(PropertiesWidget *parent); explicit PeerListWidget(PropertiesWidget *parent);
~PeerListWidget() override; ~PeerListWidget() override;

Loading…
Cancel
Save