Browse Source

Revise sort model and delegate code

adaptive-webui-19844
Chocobo1 5 years ago
parent
commit
8d6b9b6181
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 11
      src/gui/previewlistdelegate.cpp
  2. 10
      src/gui/previewselectdialog.cpp
  3. 11
      src/gui/properties/peerlistdelegate.cpp
  4. 41
      src/gui/properties/proplistdelegate.cpp
  5. 1
      src/gui/search/searchlistdelegate.cpp
  6. 34
      src/gui/search/searchsortmodel.cpp

11
src/gui/previewlistdelegate.cpp

@ -50,23 +50,27 @@ PreviewListDelegate::PreviewListDelegate(QObject *parent)
void PreviewListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const void PreviewListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{ {
painter->save(); painter->save();
QStyleOptionViewItem opt = QItemDelegate::setOptions(index, option); QStyleOptionViewItem opt = QItemDelegate::setOptions(index, option);
drawBackground(painter, opt, index);
switch (index.column()) { switch (index.column()) {
case PreviewSelectDialog::SIZE: case PreviewSelectDialog::SIZE:
QItemDelegate::drawBackground(painter, opt, index);
QItemDelegate::drawDisplay(painter, opt, option.rect, Utils::Misc::friendlyUnit(index.data().toLongLong())); QItemDelegate::drawDisplay(painter, opt, option.rect, Utils::Misc::friendlyUnit(index.data().toLongLong()));
break; break;
case PreviewSelectDialog::PROGRESS: { case PreviewSelectDialog::PROGRESS: {
const qreal progress = (index.data().toReal() * 100);
QStyleOptionProgressBar newopt; QStyleOptionProgressBar newopt;
qreal progress = index.data().toDouble() * 100.;
newopt.rect = opt.rect; newopt.rect = opt.rect;
newopt.text = ((progress == 100.0) ? QString("100%") : Utils::String::fromDouble(progress, 1) + '%'); newopt.text = ((progress == 100) ? QString("100%") : (Utils::String::fromDouble(progress, 1) + '%'));
newopt.progress = static_cast<int>(progress); newopt.progress = static_cast<int>(progress);
newopt.maximum = 100; newopt.maximum = 100;
newopt.minimum = 0; newopt.minimum = 0;
newopt.state |= QStyle::State_Enabled; newopt.state |= QStyle::State_Enabled;
newopt.textVisible = true; newopt.textVisible = true;
#if defined(Q_OS_WIN) || defined(Q_OS_MACOS) #if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
// XXX: To avoid having the progress text on the right of the bar // XXX: To avoid having the progress text on the right of the bar
QProxyStyle st("fusion"); QProxyStyle st("fusion");
@ -76,6 +80,7 @@ void PreviewListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &o
#endif #endif
} }
break; break;
default: default:
QItemDelegate::paint(painter, option, index); QItemDelegate::paint(painter, option, index);
} }

10
src/gui/previewselectdialog.cpp

@ -79,7 +79,7 @@ PreviewSelectDialog::PreviewSelectDialog(QWidget *parent, BitTorrent::TorrentHan
m_ui->previewList->setItemDelegate(m_listDelegate); m_ui->previewList->setItemDelegate(m_listDelegate);
m_ui->previewList->setAlternatingRowColors(pref->useAlternatingRowColors()); m_ui->previewList->setAlternatingRowColors(pref->useAlternatingRowColors());
// Fill list in // Fill list in
QVector<qreal> fp = torrent->filesProgress(); const QVector<qreal> fp = torrent->filesProgress();
int nbFiles = torrent->filesCount(); int nbFiles = torrent->filesCount();
for (int i = 0; i < nbFiles; ++i) { for (int i = 0; i < nbFiles; ++i) {
QString fileName = torrent->fileName(i); QString fileName = torrent->fileName(i);
@ -89,10 +89,10 @@ PreviewSelectDialog::PreviewSelectDialog(QWidget *parent, BitTorrent::TorrentHan
if (Utils::Misc::isPreviewable(extension)) { if (Utils::Misc::isPreviewable(extension)) {
int row = m_previewListModel->rowCount(); int row = m_previewListModel->rowCount();
m_previewListModel->insertRow(row); m_previewListModel->insertRow(row);
m_previewListModel->setData(m_previewListModel->index(row, NAME), QVariant(fileName)); m_previewListModel->setData(m_previewListModel->index(row, NAME), fileName);
m_previewListModel->setData(m_previewListModel->index(row, SIZE), QVariant(torrent->fileSize(i))); m_previewListModel->setData(m_previewListModel->index(row, SIZE), torrent->fileSize(i));
m_previewListModel->setData(m_previewListModel->index(row, PROGRESS), QVariant(fp[i])); m_previewListModel->setData(m_previewListModel->index(row, PROGRESS), fp[i]);
m_previewListModel->setData(m_previewListModel->index(row, FILE_INDEX), QVariant(i)); m_previewListModel->setData(m_previewListModel->index(row, FILE_INDEX), i);
} }
} }

11
src/gui/properties/peerlistdelegate.cpp

@ -44,6 +44,7 @@ void PeerListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
painter->save(); painter->save();
const bool hideValues = Preferences::instance()->getHideZeroValues(); const bool hideValues = Preferences::instance()->getHideZeroValues();
QStyleOptionViewItem opt = QItemDelegate::setOptions(index, option); QStyleOptionViewItem opt = QItemDelegate::setOptions(index, option);
QItemDelegate::drawBackground(painter, opt, index); QItemDelegate::drawBackground(painter, opt, index);
@ -54,7 +55,7 @@ void PeerListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
break; break;
case TOT_DOWN: case TOT_DOWN:
case TOT_UP: { case TOT_UP: {
qlonglong size = index.data().toLongLong(); const qlonglong size = index.data().toLongLong();
if (hideValues && (size <= 0)) if (hideValues && (size <= 0))
break; break;
opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter; opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
@ -63,8 +64,8 @@ void PeerListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
break; break;
case DOWN_SPEED: case DOWN_SPEED:
case UP_SPEED: { case UP_SPEED: {
qreal speed = index.data().toDouble(); const int speed = index.data().toInt();
if (speed <= 0.0) if (speed <= 0)
break; break;
opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter; opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
QItemDelegate::drawDisplay(painter, opt, opt.rect, Utils::Misc::friendlyUnit(speed, true)); QItemDelegate::drawDisplay(painter, opt, opt.rect, Utils::Misc::friendlyUnit(speed, true));
@ -72,9 +73,9 @@ void PeerListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
break; break;
case PROGRESS: case PROGRESS:
case RELEVANCE: { case RELEVANCE: {
qreal progress = index.data().toDouble(); const qreal progress = index.data().toReal();
opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter; opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
QItemDelegate::drawDisplay(painter, opt, opt.rect, Utils::String::fromDouble(progress * 100.0, 1) + '%'); QItemDelegate::drawDisplay(painter, opt, opt.rect, (Utils::String::fromDouble(progress * 100, 1) + '%'));
} }
break; break;
default: default:

41
src/gui/properties/proplistdelegate.cpp

@ -49,17 +49,16 @@
namespace namespace
{ {
QPalette progressBarDisabledPalette() QPalette progressBarDisabledPalette()
{ {
auto getPalette = []() { static const QPalette palette = []()
{
QProgressBar bar; QProgressBar bar;
bar.setEnabled(false); bar.setEnabled(false);
QStyleOptionProgressBar opt; QStyleOptionProgressBar opt;
opt.initFrom(&bar); opt.initFrom(&bar);
return opt.palette; return opt.palette;
}; }();
static QPalette palette = getPalette();
return palette; return palette;
} }
} }
@ -73,6 +72,7 @@ PropListDelegate::PropListDelegate(PropertiesWidget *properties)
void PropListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const void PropListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{ {
painter->save(); painter->save();
QStyleOptionViewItem opt = QItemDelegate::setOptions(index, option); QStyleOptionViewItem opt = QItemDelegate::setOptions(index, option);
QItemDelegate::drawBackground(painter, opt, index); QItemDelegate::drawBackground(painter, opt, index);
@ -81,15 +81,16 @@ void PropListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
case REMAINING: case REMAINING:
QItemDelegate::drawDisplay(painter, opt, option.rect, Utils::Misc::friendlyUnit(index.data().toLongLong())); QItemDelegate::drawDisplay(painter, opt, option.rect, Utils::Misc::friendlyUnit(index.data().toLongLong()));
break; break;
case PROGRESS: { case PROGRESS: {
if (index.data().toDouble() < 0) const qreal progress = (index.data().toReal() * 100);
if (progress < 0)
break; break;
QStyleOptionProgressBar newopt; QStyleOptionProgressBar newopt;
qreal progress = index.data().toDouble() * 100.;
newopt.rect = opt.rect; newopt.rect = opt.rect;
newopt.text = ((progress == 100.0) ? QString("100%") : Utils::String::fromDouble(progress, 1) + '%'); newopt.text = (progress == 100) ? QString("100%") : (Utils::String::fromDouble(progress, 1) + '%');
newopt.progress = int(progress); newopt.progress = static_cast<int>(progress);
newopt.maximum = 100; newopt.maximum = 100;
newopt.minimum = 0; newopt.minimum = 0;
newopt.textVisible = true; newopt.textVisible = true;
@ -101,14 +102,15 @@ void PropListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
newopt.state |= QStyle::State_Enabled; newopt.state |= QStyle::State_Enabled;
} }
#if !defined(Q_OS_WIN) && !defined(Q_OS_MACOS) #if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &newopt, painter);
#else
// XXX: To avoid having the progress text on the right of the bar // XXX: To avoid having the progress text on the right of the bar
QProxyStyle("fusion").drawControl(QStyle::CE_ProgressBar, &newopt, painter, 0); QProxyStyle("fusion").drawControl(QStyle::CE_ProgressBar, &newopt, painter, 0);
#else
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &newopt, painter);
#endif #endif
} }
break; break;
case PRIORITY: { case PRIORITY: {
QString text = ""; QString text = "";
switch (static_cast<BitTorrent::DownloadPriority>(index.data().toInt())) { switch (static_cast<BitTorrent::DownloadPriority>(index.data().toInt())) {
@ -131,23 +133,26 @@ void PropListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
QItemDelegate::drawDisplay(painter, opt, option.rect, text); QItemDelegate::drawDisplay(painter, opt, option.rect, text);
} }
break; break;
case AVAILABILITY: { case AVAILABILITY: {
const qreal availability = index.data().toDouble(); const qreal availability = index.data().toReal();
if (availability < 0) { if (availability < 0) {
QItemDelegate::drawDisplay(painter, opt, option.rect, tr("N/A")); QItemDelegate::drawDisplay(painter, opt, option.rect, tr("N/A"));
} }
else { else {
const QString value = (availability >= 1.0) const QString value = (availability >= 1.0)
? QLatin1String("100") ? QLatin1String("100")
: Utils::String::fromDouble(availability * 100., 1); : Utils::String::fromDouble(availability * 100, 1);
QItemDelegate::drawDisplay(painter, opt, option.rect, value + C_THIN_SPACE + QLatin1Char('%')); QItemDelegate::drawDisplay(painter, opt, option.rect, (value + C_THIN_SPACE + QLatin1Char('%')));
} }
} }
break; break;
default: default:
QItemDelegate::paint(painter, option, index); QItemDelegate::paint(painter, option, index);
break; break;
} }
painter->restore(); painter->restore();
} }
@ -176,7 +181,7 @@ QWidget *PropListDelegate::createEditor(QWidget *parent, const QStyleOptionViewI
if (index.column() != PRIORITY) return nullptr; if (index.column() != PRIORITY) return nullptr;
if (m_properties) { if (m_properties) {
BitTorrent::TorrentHandle *const torrent = m_properties->getCurrentTorrent(); const BitTorrent::TorrentHandle *torrent = m_properties->getCurrentTorrent();
if (!torrent || !torrent->hasMetadata() || torrent->isSeed()) if (!torrent || !torrent->hasMetadata() || torrent->isSeed())
return nullptr; return nullptr;
} }
@ -195,9 +200,8 @@ QWidget *PropListDelegate::createEditor(QWidget *parent, const QStyleOptionViewI
void PropListDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const void PropListDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{ {
auto *combobox = static_cast<QComboBox *>(editor); const auto *combobox = static_cast<QComboBox *>(editor);
int value = combobox->currentIndex(); const int value = combobox->currentIndex();
qDebug("PropListDelegate: setModelData(%d)", value);
BitTorrent::DownloadPriority prio = BitTorrent::DownloadPriority::Normal; // NORMAL BitTorrent::DownloadPriority prio = BitTorrent::DownloadPriority::Normal; // NORMAL
switch (value) { switch (value) {
@ -218,6 +222,5 @@ void PropListDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
void PropListDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const void PropListDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const
{ {
qDebug("UpdateEditor Geometry called");
editor->setGeometry(option.rect); editor->setGeometry(option.rect);
} }

1
src/gui/search/searchlistdelegate.cpp

@ -35,7 +35,6 @@
#include "base/utils/misc.h" #include "base/utils/misc.h"
#include "searchsortmodel.h" #include "searchsortmodel.h"
SearchListDelegate::SearchListDelegate(QObject *parent) SearchListDelegate::SearchListDelegate(QObject *parent)
: QItemDelegate(parent) : QItemDelegate(parent)
{ {

34
src/gui/search/searchsortmodel.cpp

@ -43,7 +43,7 @@ SearchSortModel::SearchSortModel(QObject *parent)
{ {
} }
void SearchSortModel::enableNameFilter(bool enabled) void SearchSortModel::enableNameFilter(const bool enabled)
{ {
m_isNameFilterEnabled = enabled; m_isNameFilterEnabled = enabled;
} }
@ -51,7 +51,7 @@ void SearchSortModel::enableNameFilter(bool enabled)
void SearchSortModel::setNameFilter(const QString &searchTerm) void SearchSortModel::setNameFilter(const QString &searchTerm)
{ {
m_searchTerm = searchTerm; m_searchTerm = searchTerm;
if (searchTerm.length() > 2 if ((searchTerm.length() > 2)
&& searchTerm.startsWith(QLatin1Char('"')) && searchTerm.endsWith(QLatin1Char('"'))) { && searchTerm.startsWith(QLatin1Char('"')) && searchTerm.endsWith(QLatin1Char('"'))) {
m_searchTermWords = QStringList(m_searchTerm.mid(1, m_searchTerm.length() - 2)); m_searchTermWords = QStringList(m_searchTerm.mid(1, m_searchTerm.length() - 2));
} }
@ -60,19 +60,19 @@ void SearchSortModel::setNameFilter(const QString &searchTerm)
} }
} }
void SearchSortModel::setSizeFilter(qint64 minSize, qint64 maxSize) void SearchSortModel::setSizeFilter(const qint64 minSize, const qint64 maxSize)
{ {
m_minSize = std::max(static_cast<qint64>(0), minSize); m_minSize = std::max(static_cast<qint64>(0), minSize);
m_maxSize = std::max(static_cast<qint64>(-1), maxSize); m_maxSize = std::max(static_cast<qint64>(-1), maxSize);
} }
void SearchSortModel::setSeedsFilter(int minSeeds, int maxSeeds) void SearchSortModel::setSeedsFilter(const int minSeeds, const int maxSeeds)
{ {
m_minSeeds = std::max(0, minSeeds); m_minSeeds = std::max(0, minSeeds);
m_maxSeeds = std::max(-1, maxSeeds); m_maxSeeds = std::max(-1, maxSeeds);
} }
void SearchSortModel::setLeechesFilter(int minLeeches, int maxLeeches) void SearchSortModel::setLeechesFilter(const int minLeeches, const int maxLeeches)
{ {
m_minLeeches = std::max(0, minLeeches); m_minLeeches = std::max(0, minLeeches);
m_maxLeeches = std::max(-1, maxLeeches); m_maxLeeches = std::max(-1, maxLeeches);
@ -124,42 +124,38 @@ bool SearchSortModel::lessThan(const QModelIndex &left, const QModelIndex &right
}; };
} }
bool SearchSortModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const bool SearchSortModel::filterAcceptsRow(const int sourceRow, const QModelIndex &sourceParent) const
{ {
const QAbstractItemModel *const sourceModel = this->sourceModel(); const QAbstractItemModel *const sourceModel = this->sourceModel();
if (m_isNameFilterEnabled && !m_searchTerm.isEmpty()) { if (m_isNameFilterEnabled && !m_searchTerm.isEmpty()) {
QString name = sourceModel->data(sourceModel->index(sourceRow, NAME, sourceParent)).toString(); const QString name = sourceModel->data(sourceModel->index(sourceRow, NAME, sourceParent)).toString();
for (const QString &word : asConst(m_searchTermWords)) { for (const QString &word : asConst(m_searchTermWords)) {
int i = name.indexOf(word, 0, Qt::CaseInsensitive); if (!name.contains(word, Qt::CaseInsensitive))
if (i == -1) {
return false; return false;
} }
} }
}
if ((m_minSize > 0) || (m_maxSize >= 0)) { if ((m_minSize > 0) || (m_maxSize >= 0)) {
qlonglong size = sourceModel->data(sourceModel->index(sourceRow, SIZE, sourceParent)).toLongLong(); const qlonglong size = sourceModel->data(sourceModel->index(sourceRow, SIZE, sourceParent)).toLongLong();
if (((m_minSize > 0) && (size < m_minSize)) if (((m_minSize > 0) && (size < m_minSize))
|| ((m_maxSize > 0) && (size > m_maxSize))) { || ((m_maxSize > 0) && (size > m_maxSize)))
return false; return false;
} }
}
if ((m_minSeeds > 0) || (m_maxSeeds >= 0)) { if ((m_minSeeds > 0) || (m_maxSeeds >= 0)) {
int seeds = sourceModel->data(sourceModel->index(sourceRow, SEEDS, sourceParent)).toInt(); const int seeds = sourceModel->data(sourceModel->index(sourceRow, SEEDS, sourceParent)).toInt();
if (((m_minSeeds > 0) && (seeds < m_minSeeds)) if (((m_minSeeds > 0) && (seeds < m_minSeeds))
|| ((m_maxSeeds > 0) && (seeds > m_maxSeeds))) { || ((m_maxSeeds > 0) && (seeds > m_maxSeeds)))
return false; return false;
} }
}
if ((m_minLeeches > 0) || (m_maxLeeches >= 0)) { if ((m_minLeeches > 0) || (m_maxLeeches >= 0)) {
int leeches = sourceModel->data(sourceModel->index(sourceRow, LEECHES, sourceParent)).toInt(); const int leeches = sourceModel->data(sourceModel->index(sourceRow, LEECHES, sourceParent)).toInt();
if (((m_minLeeches > 0) && (leeches < m_minLeeches)) if (((m_minLeeches > 0) && (leeches < m_minLeeches))
|| ((m_maxLeeches > 0) && (leeches > m_maxLeeches))) { || ((m_maxLeeches > 0) && (leeches > m_maxLeeches)))
return false; return false;
} }
}
return base::filterAcceptsRow(sourceRow, sourceParent); return base::filterAcceptsRow(sourceRow, sourceParent);
} }

Loading…
Cancel
Save