From 8d6b9b6181d2e589cb4743fe71f363736110d62c Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Thu, 8 Aug 2019 21:32:27 +0800 Subject: [PATCH] Revise sort model and delegate code --- src/gui/previewlistdelegate.cpp | 11 ++++-- src/gui/previewselectdialog.cpp | 10 ++--- src/gui/properties/peerlistdelegate.cpp | 11 +++--- src/gui/properties/proplistdelegate.cpp | 51 +++++++++++++------------ src/gui/search/searchlistdelegate.cpp | 1 - src/gui/search/searchsortmodel.cpp | 36 ++++++++--------- 6 files changed, 62 insertions(+), 58 deletions(-) diff --git a/src/gui/previewlistdelegate.cpp b/src/gui/previewlistdelegate.cpp index 9371da34d..afa0d14a9 100644 --- a/src/gui/previewlistdelegate.cpp +++ b/src/gui/previewlistdelegate.cpp @@ -50,23 +50,27 @@ PreviewListDelegate::PreviewListDelegate(QObject *parent) void PreviewListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { painter->save(); + QStyleOptionViewItem opt = QItemDelegate::setOptions(index, option); + drawBackground(painter, opt, index); switch (index.column()) { case PreviewSelectDialog::SIZE: - QItemDelegate::drawBackground(painter, opt, index); QItemDelegate::drawDisplay(painter, opt, option.rect, Utils::Misc::friendlyUnit(index.data().toLongLong())); break; + case PreviewSelectDialog::PROGRESS: { + const qreal progress = (index.data().toReal() * 100); + QStyleOptionProgressBar newopt; - qreal progress = index.data().toDouble() * 100.; 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(progress); newopt.maximum = 100; newopt.minimum = 0; newopt.state |= QStyle::State_Enabled; newopt.textVisible = true; + #if defined(Q_OS_WIN) || defined(Q_OS_MACOS) // XXX: To avoid having the progress text on the right of the bar QProxyStyle st("fusion"); @@ -76,6 +80,7 @@ void PreviewListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &o #endif } break; + default: QItemDelegate::paint(painter, option, index); } diff --git a/src/gui/previewselectdialog.cpp b/src/gui/previewselectdialog.cpp index d6f139006..821912a72 100644 --- a/src/gui/previewselectdialog.cpp +++ b/src/gui/previewselectdialog.cpp @@ -79,7 +79,7 @@ PreviewSelectDialog::PreviewSelectDialog(QWidget *parent, BitTorrent::TorrentHan m_ui->previewList->setItemDelegate(m_listDelegate); m_ui->previewList->setAlternatingRowColors(pref->useAlternatingRowColors()); // Fill list in - QVector fp = torrent->filesProgress(); + const QVector fp = torrent->filesProgress(); int nbFiles = torrent->filesCount(); for (int i = 0; i < nbFiles; ++i) { QString fileName = torrent->fileName(i); @@ -89,10 +89,10 @@ PreviewSelectDialog::PreviewSelectDialog(QWidget *parent, BitTorrent::TorrentHan if (Utils::Misc::isPreviewable(extension)) { int row = m_previewListModel->rowCount(); m_previewListModel->insertRow(row); - m_previewListModel->setData(m_previewListModel->index(row, NAME), QVariant(fileName)); - m_previewListModel->setData(m_previewListModel->index(row, SIZE), QVariant(torrent->fileSize(i))); - m_previewListModel->setData(m_previewListModel->index(row, PROGRESS), QVariant(fp[i])); - m_previewListModel->setData(m_previewListModel->index(row, FILE_INDEX), QVariant(i)); + m_previewListModel->setData(m_previewListModel->index(row, NAME), fileName); + m_previewListModel->setData(m_previewListModel->index(row, SIZE), torrent->fileSize(i)); + m_previewListModel->setData(m_previewListModel->index(row, PROGRESS), fp[i]); + m_previewListModel->setData(m_previewListModel->index(row, FILE_INDEX), i); } } diff --git a/src/gui/properties/peerlistdelegate.cpp b/src/gui/properties/peerlistdelegate.cpp index b1b3b9ef5..5d12e6805 100644 --- a/src/gui/properties/peerlistdelegate.cpp +++ b/src/gui/properties/peerlistdelegate.cpp @@ -44,6 +44,7 @@ void PeerListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti painter->save(); const bool hideValues = Preferences::instance()->getHideZeroValues(); + QStyleOptionViewItem opt = QItemDelegate::setOptions(index, option); QItemDelegate::drawBackground(painter, opt, index); @@ -54,7 +55,7 @@ void PeerListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti break; case TOT_DOWN: case TOT_UP: { - qlonglong size = index.data().toLongLong(); + const qlonglong size = index.data().toLongLong(); if (hideValues && (size <= 0)) break; opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter; @@ -63,8 +64,8 @@ void PeerListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti break; case DOWN_SPEED: case UP_SPEED: { - qreal speed = index.data().toDouble(); - if (speed <= 0.0) + 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)); @@ -72,9 +73,9 @@ void PeerListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti break; case PROGRESS: case RELEVANCE: { - qreal progress = index.data().toDouble(); + const qreal progress = index.data().toReal(); 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; default: diff --git a/src/gui/properties/proplistdelegate.cpp b/src/gui/properties/proplistdelegate.cpp index 6fb6a5f54..c49335afb 100644 --- a/src/gui/properties/proplistdelegate.cpp +++ b/src/gui/properties/proplistdelegate.cpp @@ -49,17 +49,16 @@ namespace { - QPalette progressBarDisabledPalette() { - auto getPalette = []() { - QProgressBar bar; - bar.setEnabled(false); - QStyleOptionProgressBar opt; - opt.initFrom(&bar); - return opt.palette; - }; - static QPalette palette = getPalette(); + static const QPalette palette = []() + { + QProgressBar bar; + bar.setEnabled(false); + QStyleOptionProgressBar opt; + opt.initFrom(&bar); + return opt.palette; + }(); return palette; } } @@ -73,6 +72,7 @@ PropListDelegate::PropListDelegate(PropertiesWidget *properties) void PropListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { painter->save(); + QStyleOptionViewItem opt = QItemDelegate::setOptions(index, option); QItemDelegate::drawBackground(painter, opt, index); @@ -81,15 +81,16 @@ void PropListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti case REMAINING: QItemDelegate::drawDisplay(painter, opt, option.rect, Utils::Misc::friendlyUnit(index.data().toLongLong())); break; + case PROGRESS: { - if (index.data().toDouble() < 0) + const qreal progress = (index.data().toReal() * 100); + if (progress < 0) break; QStyleOptionProgressBar newopt; - qreal progress = index.data().toDouble() * 100.; newopt.rect = opt.rect; - newopt.text = ((progress == 100.0) ? QString("100%") : Utils::String::fromDouble(progress, 1) + '%'); - newopt.progress = int(progress); + newopt.text = (progress == 100) ? QString("100%") : (Utils::String::fromDouble(progress, 1) + '%'); + newopt.progress = static_cast(progress); newopt.maximum = 100; newopt.minimum = 0; newopt.textVisible = true; @@ -101,14 +102,15 @@ void PropListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti newopt.state |= QStyle::State_Enabled; } -#if !defined(Q_OS_WIN) && !defined(Q_OS_MACOS) - QApplication::style()->drawControl(QStyle::CE_ProgressBar, &newopt, painter); -#else +#if defined(Q_OS_WIN) || defined(Q_OS_MACOS) // XXX: To avoid having the progress text on the right of the bar QProxyStyle("fusion").drawControl(QStyle::CE_ProgressBar, &newopt, painter, 0); +#else + QApplication::style()->drawControl(QStyle::CE_ProgressBar, &newopt, painter); #endif } break; + case PRIORITY: { QString text = ""; switch (static_cast(index.data().toInt())) { @@ -131,23 +133,26 @@ void PropListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti QItemDelegate::drawDisplay(painter, opt, option.rect, text); } break; + case AVAILABILITY: { - const qreal availability = index.data().toDouble(); + const qreal availability = index.data().toReal(); if (availability < 0) { QItemDelegate::drawDisplay(painter, opt, option.rect, tr("N/A")); } else { const QString value = (availability >= 1.0) ? QLatin1String("100") - : Utils::String::fromDouble(availability * 100., 1); - QItemDelegate::drawDisplay(painter, opt, option.rect, value + C_THIN_SPACE + QLatin1Char('%')); + : Utils::String::fromDouble(availability * 100, 1); + QItemDelegate::drawDisplay(painter, opt, option.rect, (value + C_THIN_SPACE + QLatin1Char('%'))); } } break; + default: QItemDelegate::paint(painter, option, index); break; } + painter->restore(); } @@ -176,7 +181,7 @@ QWidget *PropListDelegate::createEditor(QWidget *parent, const QStyleOptionViewI if (index.column() != PRIORITY) return nullptr; if (m_properties) { - BitTorrent::TorrentHandle *const torrent = m_properties->getCurrentTorrent(); + const BitTorrent::TorrentHandle *torrent = m_properties->getCurrentTorrent(); if (!torrent || !torrent->hasMetadata() || torrent->isSeed()) return nullptr; } @@ -195,9 +200,8 @@ QWidget *PropListDelegate::createEditor(QWidget *parent, const QStyleOptionViewI void PropListDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { - auto *combobox = static_cast(editor); - int value = combobox->currentIndex(); - qDebug("PropListDelegate: setModelData(%d)", value); + const auto *combobox = static_cast(editor); + const int value = combobox->currentIndex(); BitTorrent::DownloadPriority prio = BitTorrent::DownloadPriority::Normal; // NORMAL switch (value) { @@ -218,6 +222,5 @@ void PropListDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, void PropListDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const { - qDebug("UpdateEditor Geometry called"); editor->setGeometry(option.rect); } diff --git a/src/gui/search/searchlistdelegate.cpp b/src/gui/search/searchlistdelegate.cpp index dabe1961d..e5e9ced20 100644 --- a/src/gui/search/searchlistdelegate.cpp +++ b/src/gui/search/searchlistdelegate.cpp @@ -35,7 +35,6 @@ #include "base/utils/misc.h" #include "searchsortmodel.h" - SearchListDelegate::SearchListDelegate(QObject *parent) : QItemDelegate(parent) { diff --git a/src/gui/search/searchsortmodel.cpp b/src/gui/search/searchsortmodel.cpp index e83e9e753..a179708eb 100644 --- a/src/gui/search/searchsortmodel.cpp +++ b/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; } @@ -51,7 +51,7 @@ void SearchSortModel::enableNameFilter(bool enabled) void SearchSortModel::setNameFilter(const QString &searchTerm) { m_searchTerm = searchTerm; - if (searchTerm.length() > 2 + if ((searchTerm.length() > 2) && searchTerm.startsWith(QLatin1Char('"')) && searchTerm.endsWith(QLatin1Char('"'))) { 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(0), minSize); m_maxSize = std::max(static_cast(-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_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_maxLeeches = std::max(-1, maxLeeches); @@ -117,48 +117,44 @@ bool SearchSortModel::lessThan(const QModelIndex &left, const QModelIndex &right const QString strR = right.data().toString(); const int result = Utils::String::naturalCompare(strL, strR, Qt::CaseInsensitive); return (result < 0); - } + } break; default: return base::lessThan(left, 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(); + 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)) { - int i = name.indexOf(word, 0, Qt::CaseInsensitive); - if (i == -1) { + if (!name.contains(word, Qt::CaseInsensitive)) return false; - } } } 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)) - || ((m_maxSize > 0) && (size > m_maxSize))) { + || ((m_maxSize > 0) && (size > m_maxSize))) return false; - } } 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)) - || ((m_maxSeeds > 0) && (seeds > m_maxSeeds))) { + || ((m_maxSeeds > 0) && (seeds > m_maxSeeds))) return false; - } } 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)) - || ((m_maxLeeches > 0) && (leeches > m_maxLeeches))) { + || ((m_maxLeeches > 0) && (leeches > m_maxLeeches))) return false; - } } return base::filterAcceptsRow(sourceRow, sourceParent);