From dcf632b39c7dfd287bcc227a9642a63f1a32810a Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Tue, 6 Aug 2019 23:02:45 +0800 Subject: [PATCH 1/4] Remove redundant margin properties Those are the same with the default values. --- src/gui/aboutdialog.ui | 1 - src/gui/optionsdialog.ui | 12 ------------ 2 files changed, 13 deletions(-) diff --git a/src/gui/aboutdialog.ui b/src/gui/aboutdialog.ui index f0ed552d6..fa27a9818 100644 --- a/src/gui/aboutdialog.ui +++ b/src/gui/aboutdialog.ui @@ -489,7 +489,6 @@ - diff --git a/src/gui/optionsdialog.ui b/src/gui/optionsdialog.ui index 7a3bf911c..3c7d51030 100644 --- a/src/gui/optionsdialog.ui +++ b/src/gui/optionsdialog.ui @@ -561,9 +561,6 @@ - - 0 - @@ -604,9 +601,6 @@ - - 0 - @@ -730,9 +724,6 @@ true - - 0 - @@ -2274,9 +2265,6 @@ false - - 9 - From c61116882b60bf0e9aa867aafb528e965ffbcebd Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Wed, 7 Aug 2019 14:04:03 +0800 Subject: [PATCH 2/4] Clean up TransferListModel class --- src/gui/transferlistmodel.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/gui/transferlistmodel.cpp b/src/gui/transferlistmodel.cpp index 7c79e8528..66ea53667 100644 --- a/src/gui/transferlistmodel.cpp +++ b/src/gui/transferlistmodel.cpp @@ -160,11 +160,11 @@ QVariant TransferListModel::headerData(int section, Qt::Orientation orientation, return {}; } -QVariant TransferListModel::data(const QModelIndex &index, int role) const +QVariant TransferListModel::data(const QModelIndex &index, const int role) const { if (!index.isValid()) return {}; - BitTorrent::TorrentHandle *const torrent = m_torrents.value(index.row()); + const BitTorrent::TorrentHandle *torrent = m_torrents.value(index.row()); if (!torrent) return {}; if ((role == Qt::DecorationRole) && (index.column() == TR_NAME)) @@ -258,7 +258,7 @@ bool TransferListModel::setData(const QModelIndex &index, const QVariant &value, BitTorrent::TorrentHandle *const torrent = m_torrents.value(index.row()); if (!torrent) return false; - // Category, seed date and Name columns can be edited + // Category and Name columns can be edited switch (index.column()) { case TR_NAME: torrent->setName(value.toString()); @@ -275,7 +275,7 @@ bool TransferListModel::setData(const QModelIndex &index, const QVariant &value, void TransferListModel::addTorrent(BitTorrent::TorrentHandle *const torrent) { - if (m_torrents.indexOf(torrent) == -1) { + if (!m_torrents.contains(torrent)) { const int row = m_torrents.size(); beginInsertRows(QModelIndex(), row, row); m_torrents << torrent; @@ -322,7 +322,7 @@ void TransferListModel::handleTorrentsUpdated() // Static functions -QIcon getIconByState(BitTorrent::TorrentState state) +QIcon getIconByState(const BitTorrent::TorrentState state) { switch (state) { case BitTorrent::TorrentState::Downloading: @@ -359,7 +359,7 @@ QIcon getIconByState(BitTorrent::TorrentState state) } } -QColor getColorByState(BitTorrent::TorrentState state) +QColor getColorByState(const BitTorrent::TorrentState state) { // Color names taken from http://cloford.com/resources/colours/500col.htm bool dark = isDarkTheme(); From 061219d0a2155a77f98a4a06d0db697e5a5e4d30 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Wed, 7 Aug 2019 14:26:36 +0800 Subject: [PATCH 3/4] Simplify code --- src/gui/properties/peerlistwidget.cpp | 2 +- src/gui/transferlistdelegate.cpp | 90 +++++++++++---------------- src/gui/transferlistsortmodel.cpp | 49 +++++++-------- 3 files changed, 63 insertions(+), 78 deletions(-) diff --git a/src/gui/properties/peerlistwidget.cpp b/src/gui/properties/peerlistwidget.cpp index feda1608e..28aa4fc09 100644 --- a/src/gui/properties/peerlistwidget.cpp +++ b/src/gui/properties/peerlistwidget.cpp @@ -301,7 +301,7 @@ void PeerListWidget::copySelectedPeers() int row = m_proxyModel->mapToSource(index).row(); QString ip = m_listModel->data(m_listModel->index(row, PeerListDelegate::IP_HIDDEN)).toString(); QString myport = m_listModel->data(m_listModel->index(row, PeerListDelegate::PORT)).toString(); - if (ip.indexOf('.') == -1) // IPv6 + if (!ip.contains('.')) // IPv6 selectedPeers << '[' + ip + "]:" + myport; else // IPv4 selectedPeers << ip + ':' + myport; diff --git a/src/gui/transferlistdelegate.cpp b/src/gui/transferlistdelegate.cpp index 7cfbef768..ffc2edbf0 100644 --- a/src/gui/transferlistdelegate.cpp +++ b/src/gui/transferlistdelegate.cpp @@ -54,16 +54,18 @@ TransferListDelegate::TransferListDelegate(QObject *parent) void TransferListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { painter->save(); + bool isHideState = true; if (Preferences::instance()->getHideZeroComboValues() == 1) { // paused torrents only - QModelIndex stateIndex = index.sibling(index.row(), TransferListModel::TR_STATUS); + const QModelIndex stateIndex = index.sibling(index.row(), TransferListModel::TR_STATUS); if (stateIndex.data().value() != BitTorrent::TorrentState::PausedDownloading) isHideState = false; } - const bool hideValues = Preferences::instance()->getHideZeroValues() & isHideState; + const bool hideValues = Preferences::instance()->getHideZeroValues() && isHideState; QStyleOptionViewItem opt = QItemDelegate::setOptions(index, option); QItemDelegate::drawBackground(painter, opt, index); + switch (index.column()) { case TransferListModel::TR_AMOUNT_DOWNLOADED: case TransferListModel::TR_AMOUNT_UPLOADED: @@ -81,10 +83,10 @@ void TransferListDelegate::paint(QPainter *painter, const QStyleOptionViewItem & } break; case TransferListModel::TR_ETA: { - opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter; - QItemDelegate::drawDisplay(painter, opt, option.rect, Utils::Misc::userFriendlyDuration(index.data().toLongLong(), MAX_ETA)); + opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter; + QItemDelegate::drawDisplay(painter, opt, option.rect, Utils::Misc::userFriendlyDuration(index.data().toLongLong(), MAX_ETA)); + } break; - } case TransferListModel::TR_SEEDS: case TransferListModel::TR_PEERS: { qlonglong value = index.data().toLongLong(); @@ -148,30 +150,32 @@ void TransferListDelegate::paint(QPainter *painter, const QStyleOptionViewItem & case TransferListModel::TR_QUEUE_POSITION: { const int queuePos = index.data().toInt(); opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter; - if (queuePos > 0) { + if (queuePos > 0) QItemDelegate::paint(painter, opt, index); - } - else { + else QItemDelegate::drawDisplay(painter, opt, opt.rect, "*"); - } } break; case TransferListModel::TR_PROGRESS: { + const qreal progress = index.data().toDouble() * 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) - QApplication::style()->drawControl(QStyle::CE_ProgressBar, &newopt, painter); -#else - // XXX: To avoid having the progress text on the right of the bar + +#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"); - st.drawControl(QStyle::CE_ProgressBar, &newopt, painter, 0); + st.drawControl(QStyle::CE_ProgressBar, &newopt, painter); +#else + QApplication::style()->drawControl(QStyle::CE_ProgressBar, &newopt, painter); #endif } break; @@ -207,6 +211,7 @@ void TransferListDelegate::paint(QPainter *painter, const QStyleOptionViewItem & default: QItemDelegate::paint(painter, option, index); } + painter->restore(); } @@ -225,7 +230,7 @@ QSize TransferListDelegate::sizeHint(const QStyleOptionViewItem &option, const Q static int nameColHeight = -1; if (nameColHeight == -1) { - QModelIndex nameColumn = index.sibling(index.row(), TransferListModel::TR_NAME); + const QModelIndex nameColumn = index.sibling(index.row(), TransferListModel::TR_NAME); nameColHeight = QItemDelegate::sizeHint(option, nameColumn).height(); } @@ -236,60 +241,41 @@ QSize TransferListDelegate::sizeHint(const QStyleOptionViewItem &option, const Q QString TransferListDelegate::getStatusString(const BitTorrent::TorrentState state) const { - QString str; - switch (state) { case BitTorrent::TorrentState::Downloading: - str = tr("Downloading"); - break; + return tr("Downloading"); case BitTorrent::TorrentState::StalledDownloading: - str = tr("Stalled", "Torrent is waiting for download to begin"); - break; + return tr("Stalled", "Torrent is waiting for download to begin"); case BitTorrent::TorrentState::DownloadingMetadata: - str = tr("Downloading metadata", "used when loading a magnet link"); - break; + return tr("Downloading metadata", "Used when loading a magnet link"); case BitTorrent::TorrentState::ForcedDownloading: - str = tr("[F] Downloading", "used when the torrent is forced started. You probably shouldn't translate the F."); - break; + return tr("[F] Downloading", "Used when the torrent is forced started. You probably shouldn't translate the F."); case BitTorrent::TorrentState::Allocating: - str = tr("Allocating", "qBittorrent is allocating the files on disk"); - break; + return tr("Allocating", "qBittorrent is allocating the files on disk"); case BitTorrent::TorrentState::Uploading: case BitTorrent::TorrentState::StalledUploading: - str = tr("Seeding", "Torrent is complete and in upload-only mode"); - break; + return tr("Seeding", "Torrent is complete and in upload-only mode"); case BitTorrent::TorrentState::ForcedUploading: - str = tr("[F] Seeding", "used when the torrent is forced started. You probably shouldn't translate the F."); - break; + return tr("[F] Seeding", "Used when the torrent is forced started. You probably shouldn't translate the F."); case BitTorrent::TorrentState::QueuedDownloading: case BitTorrent::TorrentState::QueuedUploading: - str = tr("Queued", "i.e. torrent is queued"); - break; + return tr("Queued", "Torrent is queued"); case BitTorrent::TorrentState::CheckingDownloading: case BitTorrent::TorrentState::CheckingUploading: - str = tr("Checking", "Torrent local data is being checked"); - break; + return tr("Checking", "Torrent local data is being checked"); case BitTorrent::TorrentState::CheckingResumeData: - str = tr("Checking resume data", "used when loading the torrents from disk after qbt is launched. It checks the correctness of the .fastresume file. Normally it is completed in a fraction of a second, unless loading many many torrents."); - break; + return tr("Checking resume data", "Used when loading the torrents from disk after qbt is launched. It checks the correctness of the .fastresume file. Normally it is completed in a fraction of a second, unless loading many many torrents."); case BitTorrent::TorrentState::PausedDownloading: - str = tr("Paused"); - break; + return tr("Paused"); case BitTorrent::TorrentState::PausedUploading: - str = tr("Completed"); - break; + return tr("Completed"); case BitTorrent::TorrentState::Moving: - str = tr("Moving", "Torrent local data are being moved/relocated"); - break; + return tr("Moving", "Torrent local data are being moved/relocated"); case BitTorrent::TorrentState::MissingFiles: - str = tr("Missing Files"); - break; + return tr("Missing Files"); case BitTorrent::TorrentState::Error: - str = tr("Errored", "torrent status, the torrent has an error"); - break; + return tr("Errored", "Torrent status, the torrent has an error"); default: - str = ""; + return {}; } - - return str; } diff --git a/src/gui/transferlistsortmodel.cpp b/src/gui/transferlistsortmodel.cpp index 78a136e22..045610141 100644 --- a/src/gui/transferlistsortmodel.cpp +++ b/src/gui/transferlistsortmodel.cpp @@ -114,13 +114,11 @@ bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex case TransferListModel::TR_ADD_DATE: case TransferListModel::TR_SEED_DATE: - case TransferListModel::TR_SEEN_COMPLETE_DATE: { + case TransferListModel::TR_SEEN_COMPLETE_DATE: return dateLessThan(sortColumn(), left, right, true); - } - case TransferListModel::TR_QUEUE_POSITION: { + case TransferListModel::TR_QUEUE_POSITION: return lowerPositionThan(left, right); - } case TransferListModel::TR_SEEDS: case TransferListModel::TR_PEERS: { @@ -140,20 +138,22 @@ bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex } case TransferListModel::TR_ETA: { - const TransferListModel *model = qobject_cast(sourceModel()); - // Sorting rules prioritized. // 1. Active torrents at the top // 2. Seeding torrents at the bottom // 3. Torrents with invalid ETAs at the bottom - const bool isActiveL = TorrentFilter::ActiveTorrent.match(model->torrentHandle(model->index(left.row()))); - const bool isActiveR = TorrentFilter::ActiveTorrent.match(model->torrentHandle(model->index(right.row()))); + const TransferListModel *model = qobject_cast(sourceModel()); + + // From QSortFilterProxyModel::lessThan() documentation: + // "Note: The indices passed in correspond to the source model" + const bool isActiveL = TorrentFilter::ActiveTorrent.match(model->torrentHandle(left)); + const bool isActiveR = TorrentFilter::ActiveTorrent.match(model->torrentHandle(right)); if (isActiveL != isActiveR) return isActiveL; - const int queuePosL = model->data(model->index(left.row(), TransferListModel::TR_QUEUE_POSITION)).toInt(); - const int queuePosR = model->data(model->index(right.row(), TransferListModel::TR_QUEUE_POSITION)).toInt(); + const int queuePosL = left.sibling(left.row(), TransferListModel::TR_QUEUE_POSITION).data().toInt(); + const int queuePosR = right.sibling(right.row(), TransferListModel::TR_QUEUE_POSITION).data().toInt(); const bool isSeedingL = (queuePosL < 0); const bool isSeedingR = (queuePosR < 0); if (isSeedingL != isSeedingR) { @@ -201,22 +201,20 @@ bool TransferListSortModel::lessThan(const QModelIndex &left, const QModelIndex return (vL < vR); } - default: { + default: if (left.data() != right.data()) return QSortFilterProxyModel::lessThan(left, right); return lowerPositionThan(left, right); - } } } bool TransferListSortModel::lowerPositionThan(const QModelIndex &left, const QModelIndex &right) const { - const TransferListModel *model = qobject_cast(sourceModel()); - // Sort according to TR_QUEUE_POSITION - const int queueL = model->data(model->index(left.row(), TransferListModel::TR_QUEUE_POSITION)).toInt(); - const int queueR = model->data(model->index(right.row(), TransferListModel::TR_QUEUE_POSITION)).toInt(); + const int queueL = left.sibling(left.row(), TransferListModel::TR_QUEUE_POSITION).data().toInt(); + const int queueR = right.sibling(right.row(), TransferListModel::TR_QUEUE_POSITION).data().toInt(); + if ((queueL > 0) || (queueR > 0)) { if ((queueL > 0) && (queueR > 0)) return queueL < queueR; @@ -233,9 +231,9 @@ bool TransferListSortModel::lowerPositionThan(const QModelIndex &left, const QMo // (detailed discussion in #2526 and #2158). bool TransferListSortModel::dateLessThan(const int dateColumn, const QModelIndex &left, const QModelIndex &right, bool sortInvalidInBottom) const { - const TransferListModel *model = qobject_cast(sourceModel()); - const QDateTime dateL = model->data(model->index(left.row(), dateColumn)).toDateTime(); - const QDateTime dateR = model->data(model->index(right.row(), dateColumn)).toDateTime(); + const QDateTime dateL = left.sibling(left.row(), dateColumn).data().toDateTime(); + const QDateTime dateR = right.sibling(right.row(), dateColumn).data().toDateTime(); + if (dateL.isValid() && dateR.isValid()) { if (dateL != dateR) return dateL < dateR; @@ -248,23 +246,24 @@ bool TransferListSortModel::dateLessThan(const int dateColumn, const QModelIndex } // Finally, sort by hash - const QString hashL(model->torrentHandle(model->index(left.row()))->hash()); - const QString hashR(model->torrentHandle(model->index(right.row()))->hash()); + const TransferListModel *model = qobject_cast(sourceModel()); + const QString hashL = model->torrentHandle(left)->hash(); + const QString hashR = model->torrentHandle(right)->hash(); return hashL < hashR; } -bool TransferListSortModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const +bool TransferListSortModel::filterAcceptsRow(const int sourceRow, const QModelIndex &sourceParent) const { return matchFilter(sourceRow, sourceParent) && QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent); } -bool TransferListSortModel::matchFilter(int sourceRow, const QModelIndex &sourceParent) const +bool TransferListSortModel::matchFilter(const int sourceRow, const QModelIndex &sourceParent) const { - auto *model = qobject_cast(sourceModel()); + const auto *model = qobject_cast(sourceModel()); if (!model) return false; - BitTorrent::TorrentHandle *const torrent = model->torrentHandle(model->index(sourceRow, 0, sourceParent)); + const BitTorrent::TorrentHandle *torrent = model->torrentHandle(model->index(sourceRow, 0, sourceParent)); if (!torrent) return false; return m_filter.match(torrent); From 8d6b9b6181d2e589cb4743fe71f363736110d62c Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Thu, 8 Aug 2019 21:32:27 +0800 Subject: [PATCH 4/4] 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);