Move implementation to its own file

This commit is contained in:
Chocobo1
2019-07-17 21:57:52 +08:00
parent 1e70c52e7a
commit 07263d2d70
18 changed files with 535 additions and 290 deletions

View File

@@ -17,6 +17,8 @@ trackersadditiondialog.h
# sources
downloadedpiecesbar.cpp
peerlistdelegate.cpp
peerlistsortmodel.cpp
peerlistwidget.cpp
peersadditiondialog.cpp
pieceavailabilitybar.cpp

View File

@@ -0,0 +1,91 @@
/*
* 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: {
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: {
qreal speed = index.data().toDouble();
if (speed <= 0.0)
break;
opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
QItemDelegate::drawDisplay(painter, opt, opt.rect, Utils::Misc::friendlyUnit(speed, true));
}
break;
case PROGRESS:
case RELEVANCE: {
qreal progress = index.data().toDouble();
opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
QItemDelegate::drawDisplay(painter, opt, opt.rect, Utils::String::fromDouble(progress * 100.0, 1) + '%');
}
break;
default:
QItemDelegate::paint(painter, option, index);
}
painter->restore();
}
QWidget *PeerListDelegate::createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const
{
// No editor here
return nullptr;
}

View File

@@ -30,15 +30,11 @@
#define PEERLISTDELEGATE_H
#include <QItemDelegate>
#include <QPainter>
#include "base/preferences.h"
#include "base/utils/misc.h"
#include "base/utils/string.h"
class PeerListDelegate : public QItemDelegate
{
Q_OBJECT
Q_DISABLE_COPY(PeerListDelegate)
public:
enum PeerListColumns
@@ -61,60 +57,11 @@ public:
COL_COUNT
};
PeerListDelegate(QObject *parent) : QItemDelegate(parent) {}
explicit PeerListDelegate(QObject *parent);
~PeerListDelegate() override {}
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
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: {
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: {
qreal speed = index.data().toDouble();
if (speed <= 0.0)
break;
opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
QItemDelegate::drawDisplay(painter, opt, opt.rect, Utils::Misc::friendlyUnit(speed, true));
}
break;
case PROGRESS:
case RELEVANCE: {
qreal progress = index.data().toDouble();
opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
QItemDelegate::drawDisplay(painter, opt, opt.rect, Utils::String::fromDouble(progress * 100.0, 1) + '%');
}
break;
default:
QItemDelegate::paint(painter, option, index);
}
painter->restore();
}
QWidget *createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const override
{
// No editor here
return nullptr;
}
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

View File

@@ -0,0 +1,53 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2013 Nick Tiskov <daymansmail@gmail.com>
*
* 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 "peerlistsortmodel.h"
#include "base/utils/string.h"
#include "peerlistdelegate.h"
PeerListSortModel::PeerListSortModel(QObject *parent)
: QSortFilterProxyModel(parent)
{
}
bool PeerListSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
switch (sortColumn()) {
case PeerListDelegate::IP:
case PeerListDelegate::CLIENT: {
const QString strL = left.data().toString();
const QString strR = right.data().toString();
const int result = Utils::String::naturalCompare(strL, strR, Qt::CaseInsensitive);
return (result < 0);
}
break;
default:
return QSortFilterProxyModel::lessThan(left, right);
};
}

View File

@@ -31,34 +31,16 @@
#include <QSortFilterProxyModel>
#include "peerlistdelegate.h"
class PeerListSortModel : public QSortFilterProxyModel
{
Q_OBJECT
Q_DISABLE_COPY(PeerListSortModel)
public:
PeerListSortModel(QObject *parent = nullptr)
: QSortFilterProxyModel(parent)
{
}
explicit PeerListSortModel(QObject *parent = nullptr);
protected:
bool lessThan(const QModelIndex &left, const QModelIndex &right) const
{
switch (sortColumn()) {
case PeerListDelegate::IP:
case PeerListDelegate::CLIENT: {
const QString strL = left.data().toString();
const QString strR = right.data().toString();
const int result = Utils::String::naturalCompare(strL, strR, Qt::CaseInsensitive);
return (result < 0);
}
break;
default:
return QSortFilterProxyModel::lessThan(left, right);
};
}
private:
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
};
#endif // PEERLISTSORTMODEL_H

View File

@@ -23,6 +23,8 @@ HEADERS += \
SOURCES += \
$$PWD/downloadedpiecesbar.cpp \
$$PWD/peerlistdelegate.cpp \
$$PWD/peerlistsortmodel.cpp \
$$PWD/peerlistwidget.cpp \
$$PWD/peersadditiondialog.cpp \
$$PWD/pieceavailabilitybar.cpp \