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) @@ -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<int>(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 @@ -76,6 +80,7 @@ void PreviewListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &o
#endif
}
break;
default:
QItemDelegate::paint(painter, option, index);
}

10
src/gui/previewselectdialog.cpp

@ -79,7 +79,7 @@ PreviewSelectDialog::PreviewSelectDialog(QWidget *parent, BitTorrent::TorrentHan @@ -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<qreal> fp = torrent->filesProgress();
const QVector<qreal> 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 @@ -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);
}
}

11
src/gui/properties/peerlistdelegate.cpp

@ -44,6 +44,7 @@ void PeerListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti @@ -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 @@ -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 @@ -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 @@ -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:

41
src/gui/properties/proplistdelegate.cpp

@ -49,17 +49,16 @@ @@ -49,17 +49,16 @@
namespace
{
QPalette progressBarDisabledPalette()
{
auto getPalette = []() {
static const QPalette palette = []()
{
QProgressBar bar;
bar.setEnabled(false);
QStyleOptionProgressBar opt;
opt.initFrom(&bar);
return opt.palette;
};
static QPalette palette = getPalette();
}();
return palette;
}
}
@ -73,6 +72,7 @@ PropListDelegate::PropListDelegate(PropertiesWidget *properties) @@ -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 @@ -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<int>(progress);
newopt.maximum = 100;
newopt.minimum = 0;
newopt.textVisible = true;
@ -101,14 +102,15 @@ void PropListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti @@ -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<BitTorrent::DownloadPriority>(index.data().toInt())) {
@ -131,23 +133,26 @@ void PropListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti @@ -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 @@ -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 @@ -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<QComboBox *>(editor);
int value = combobox->currentIndex();
qDebug("PropListDelegate: setModelData(%d)", value);
const auto *combobox = static_cast<QComboBox *>(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, @@ -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);
}

1
src/gui/search/searchlistdelegate.cpp

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

34
src/gui/search/searchsortmodel.cpp

@ -43,7 +43,7 @@ SearchSortModel::SearchSortModel(QObject *parent) @@ -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) @@ -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) @@ -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_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_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);
@ -124,42 +124,38 @@ bool SearchSortModel::lessThan(const QModelIndex &left, const QModelIndex &right @@ -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();
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);
}

Loading…
Cancel
Save