Browse Source

Merge pull request #14543 from Chocobo1/cleanup

Simplify progress bar painting
adaptive-webui-19844
Mike Tzou 4 years ago committed by GitHub
parent
commit
6ed2e2694f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      src/gui/CMakeLists.txt
  2. 4
      src/gui/gui.pri
  3. 30
      src/gui/previewlistdelegate.cpp
  4. 6
      src/gui/previewlistdelegate.h
  5. 51
      src/gui/progressbarpainter.cpp
  6. 14
      src/gui/progressbarpainter.h
  7. 65
      src/gui/properties/proplistdelegate.cpp
  8. 16
      src/gui/properties/proplistdelegate.h
  9. 19
      src/gui/transferlistdelegate.cpp
  10. 10
      src/gui/transferlistdelegate.h
  11. 19
      src/gui/transferlistwidget.cpp
  12. 2
      src/gui/transferlistwidget.h

4
src/gui/CMakeLists.txt

@ -26,7 +26,7 @@ add_library(qbt_gui STATIC
powermanagement/powermanagement.h powermanagement/powermanagement.h
previewlistdelegate.h previewlistdelegate.h
previewselectdialog.h previewselectdialog.h
progressbardelegate.h progressbarpainter.h
properties/downloadedpiecesbar.h properties/downloadedpiecesbar.h
properties/peerlistsortmodel.h properties/peerlistsortmodel.h
properties/peerlistwidget.h properties/peerlistwidget.h
@ -106,7 +106,7 @@ add_library(qbt_gui STATIC
powermanagement/powermanagement.cpp powermanagement/powermanagement.cpp
previewlistdelegate.cpp previewlistdelegate.cpp
previewselectdialog.cpp previewselectdialog.cpp
progressbardelegate.cpp progressbarpainter.cpp
properties/downloadedpiecesbar.cpp properties/downloadedpiecesbar.cpp
properties/peerlistsortmodel.cpp properties/peerlistsortmodel.cpp
properties/peerlistwidget.cpp properties/peerlistwidget.cpp

4
src/gui/gui.pri

@ -27,7 +27,7 @@ HEADERS += \
$$PWD/powermanagement/powermanagement.h \ $$PWD/powermanagement/powermanagement.h \
$$PWD/previewlistdelegate.h \ $$PWD/previewlistdelegate.h \
$$PWD/previewselectdialog.h \ $$PWD/previewselectdialog.h \
$$PWD/progressbardelegate.h \ $$PWD/progressbarpainter.h \
$$PWD/properties/downloadedpiecesbar.h \ $$PWD/properties/downloadedpiecesbar.h \
$$PWD/properties/peerlistsortmodel.h \ $$PWD/properties/peerlistsortmodel.h \
$$PWD/properties/peerlistwidget.h \ $$PWD/properties/peerlistwidget.h \
@ -107,7 +107,7 @@ SOURCES += \
$$PWD/powermanagement/powermanagement.cpp \ $$PWD/powermanagement/powermanagement.cpp \
$$PWD/previewlistdelegate.cpp \ $$PWD/previewlistdelegate.cpp \
$$PWD/previewselectdialog.cpp \ $$PWD/previewselectdialog.cpp \
$$PWD/progressbardelegate.cpp \ $$PWD/progressbarpainter.cpp \
$$PWD/properties/downloadedpiecesbar.cpp \ $$PWD/properties/downloadedpiecesbar.cpp \
$$PWD/properties/peerlistsortmodel.cpp \ $$PWD/properties/peerlistsortmodel.cpp \
$$PWD/properties/peerlistwidget.cpp \ $$PWD/properties/peerlistwidget.cpp \

30
src/gui/previewlistdelegate.cpp

@ -28,18 +28,11 @@
#include "previewlistdelegate.h" #include "previewlistdelegate.h"
#include <QApplication>
#include <QModelIndex> #include <QModelIndex>
#include <QPainter> #include <QPainter>
#include <QStyleOptionProgressBar>
#include <QStyleOptionViewItem> #include <QStyleOptionViewItem>
#if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
#include <QProxyStyle>
#endif
#include "base/utils/misc.h" #include "base/utils/misc.h"
#include "base/utils/string.h"
#include "previewselectdialog.h" #include "previewselectdialog.h"
PreviewListDelegate::PreviewListDelegate(QObject *parent) PreviewListDelegate::PreviewListDelegate(QObject *parent)
@ -61,30 +54,19 @@ void PreviewListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &o
break; break;
case PreviewSelectDialog::PROGRESS: case PreviewSelectDialog::PROGRESS:
{ {
const qreal progress = (index.data().toReal() * 100); const qreal progress = (index.data().toReal() * 100);
const QString text = (progress >= 100)
? QString::fromLatin1("100%")
: (Utils::String::fromDouble(progress, 1) + '%');
QStyleOptionProgressBar newopt; m_progressBarPainter.paint(painter, option, text, static_cast<int>(progress));
newopt.rect = opt.rect;
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");
st.drawControl(QStyle::CE_ProgressBar, &newopt, painter, 0);
#else
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &newopt, painter);
#endif
} }
break; break;
default: default:
QItemDelegate::paint(painter, option, index); QItemDelegate::paint(painter, option, index);
break;
} }
painter->restore(); painter->restore();

6
src/gui/previewlistdelegate.h

@ -30,6 +30,8 @@
#include <QItemDelegate> #include <QItemDelegate>
#include "progressbarpainter.h"
class PreviewListDelegate final : public QItemDelegate class PreviewListDelegate final : public QItemDelegate
{ {
Q_OBJECT Q_OBJECT
@ -38,7 +40,9 @@ class PreviewListDelegate final : public QItemDelegate
public: public:
explicit PreviewListDelegate(QObject *parent = nullptr); explicit PreviewListDelegate(QObject *parent = nullptr);
private:
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
QWidget *createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const override; QWidget *createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const override;
private:
ProgressBarPainter m_progressBarPainter;
}; };

51
src/gui/progressbardelegate.cpp → src/gui/progressbarpainter.cpp

@ -26,49 +26,46 @@
* exception statement from your version. * exception statement from your version.
*/ */
#include "progressbardelegate.h" #include "progressbarpainter.h"
#include <QApplication>
#include <QModelIndex>
#include <QPainter> #include <QPainter>
#include <QPalette>
#include <QStyleOptionProgressBar>
#include <QStyleOptionViewItem> #include <QStyleOptionViewItem>
#if (defined(Q_OS_WIN) || defined(Q_OS_MACOS)) #if (defined(Q_OS_WIN) || defined(Q_OS_MACOS))
#include <QProxyStyle> #include <QProxyStyle>
#endif #endif
ProgressBarDelegate::ProgressBarDelegate(const int progressColumn, const int dataRole, QObject *parent) ProgressBarPainter::ProgressBarPainter()
: QStyledItemDelegate {parent}
, m_progressColumn {progressColumn}
, m_dataRole {dataRole}
{ {
#if (defined(Q_OS_WIN) || defined(Q_OS_MACOS)) #if (defined(Q_OS_WIN) || defined(Q_OS_MACOS))
m_dummyProgressBar.setStyle(new QProxyStyle {"fusion"}); auto *fusionStyle = new QProxyStyle {"fusion"};
fusionStyle->setParent(&m_dummyProgressBar);
m_dummyProgressBar.setStyle(fusionStyle);
#endif #endif
} }
void ProgressBarDelegate::initProgressStyleOption(QStyleOptionProgressBar &option, const QModelIndex &index) const void ProgressBarPainter::paint(QPainter *painter, const QStyleOptionViewItem &option, const QString &text, const int progress) const
{ {
option.text = index.data().toString(); QStyleOptionProgressBar styleOption;
option.progress = static_cast<int>(index.data(m_dataRole).toReal()); styleOption.initFrom(&m_dummyProgressBar);
option.maximum = 100; // QStyleOptionProgressBar fields
option.minimum = 0; styleOption.maximum = 100;
option.textVisible = true; styleOption.minimum = 0;
} styleOption.progress = progress;
styleOption.text = text;
void ProgressBarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const styleOption.textVisible = true;
{ // QStyleOption fields
if (index.column() != m_progressColumn) styleOption.rect = option.rect;
return QStyledItemDelegate::paint(painter, option, index); styleOption.state = option.state;
QStyleOptionProgressBar newopt; const bool isEnabled = option.state.testFlag(QStyle::State_Enabled);
newopt.initFrom(&m_dummyProgressBar); styleOption.palette.setCurrentColorGroup(isEnabled ? QPalette::Active : QPalette::Disabled);
newopt.rect = option.rect;
newopt.state = option.state;
initProgressStyleOption(newopt, index);
painter->save(); painter->save();
m_dummyProgressBar.style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter); const QStyle *style = m_dummyProgressBar.style();
m_dummyProgressBar.style()->drawControl(QStyle::CE_ProgressBar, &newopt, painter, &m_dummyProgressBar); style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter);
style->drawControl(QStyle::CE_ProgressBar, &styleOption, painter, &m_dummyProgressBar);
painter->restore(); painter->restore();
} }

14
src/gui/progressbardelegate.h → src/gui/progressbarpainter.h

@ -29,23 +29,17 @@
#pragma once #pragma once
#include <QProgressBar> #include <QProgressBar>
#include <QStyledItemDelegate>
class QStyleOptionProgressBar; class QStyleOptionViewItem;
class ProgressBarDelegate : public QStyledItemDelegate class ProgressBarPainter
{ {
public: public:
ProgressBarDelegate(int progressColumn, int dataRole, QObject *parent = nullptr); ProgressBarPainter();
protected: void paint(QPainter *painter, const QStyleOptionViewItem &option, const QString &text, int progress) const;
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
virtual void initProgressStyleOption(QStyleOptionProgressBar &option, const QModelIndex &index) const;
private: private:
const int m_progressColumn;
const int m_dataRole;
// for painting progressbar with stylesheet option, a dummy progress bar is required // for painting progressbar with stylesheet option, a dummy progress bar is required
QProgressBar m_dummyProgressBar; QProgressBar m_dummyProgressBar;
}; };

65
src/gui/properties/proplistdelegate.cpp

@ -28,61 +28,22 @@
#include "proplistdelegate.h" #include "proplistdelegate.h"
#include <QApplication>
#include <QComboBox> #include <QComboBox>
#include <QModelIndex> #include <QModelIndex>
#include <QPainter> #include <QPainter>
#include <QPalette>
#include <QProgressBar> #include <QProgressBar>
#include <QStyleOptionProgressBar>
#if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
#include <QProxyStyle>
#endif
#include "base/bittorrent/downloadpriority.h" #include "base/bittorrent/downloadpriority.h"
#include "base/bittorrent/torrent.h" #include "base/bittorrent/torrent.h"
#include "gui/torrentcontentmodel.h" #include "gui/torrentcontentmodel.h"
#include "propertieswidget.h" #include "propertieswidget.h"
namespace
{
QPalette progressBarDisabledPalette()
{
static const QPalette palette = []()
{
QProgressBar bar;
bar.setEnabled(false);
QStyleOptionProgressBar opt;
opt.initFrom(&bar);
return opt.palette;
}();
return palette;
}
}
PropListDelegate::PropListDelegate(PropertiesWidget *properties) PropListDelegate::PropListDelegate(PropertiesWidget *properties)
: ProgressBarDelegate {PROGRESS, TorrentContentModel::UnderlyingDataRole, properties} : QStyledItemDelegate {properties}
, m_properties(properties) , m_properties {properties}
{ {
} }
void PropListDelegate::initProgressStyleOption(QStyleOptionProgressBar &option, const QModelIndex &index) const
{
ProgressBarDelegate::initProgressStyleOption(option, index);
const int priority
= index.sibling(index.row(), PRIORITY).data(TorrentContentModel::UnderlyingDataRole).toInt();
if (static_cast<BitTorrent::DownloadPriority>(priority) == BitTorrent::DownloadPriority::Ignored)
{
option.state &= ~QStyle::State_Enabled;
option.palette = progressBarDisabledPalette();
}
else
{
option.state |= QStyle::State_Enabled;
}
}
void PropListDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const void PropListDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{ {
auto *combobox = static_cast<QComboBox *>(editor); auto *combobox = static_cast<QComboBox *>(editor);
@ -156,3 +117,25 @@ void PropListDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionV
{ {
editor->setGeometry(option.rect); editor->setGeometry(option.rect);
} }
void PropListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
switch (index.column())
{
case PropColumn::PROGRESS:
{
const int progress = static_cast<int>(index.data(TorrentContentModel::UnderlyingDataRole).toReal());
const int priority = index.sibling(index.row(), PropColumn::PRIORITY).data(TorrentContentModel::UnderlyingDataRole).toInt();
const bool isEnabled = static_cast<BitTorrent::DownloadPriority>(priority) != BitTorrent::DownloadPriority::Ignored;
QStyleOptionViewItem customOption {option};
customOption.state.setFlag(QStyle::State_Enabled, isEnabled);
m_progressBarPainter.paint(painter, customOption, index.data().toString(), progress);
}
break;
default:
QStyledItemDelegate::paint(painter, option, index);
break;
}
}

16
src/gui/properties/proplistdelegate.h

@ -28,11 +28,12 @@
#pragma once #pragma once
#include "gui/progressbardelegate.h" #include <QStyledItemDelegate>
#include "gui/progressbarpainter.h"
class QAbstractItemModel; class QAbstractItemModel;
class QModelIndex; class QModelIndex;
class QPainter;
class QStyleOptionViewItem; class QStyleOptionViewItem;
class PropertiesWidget; class PropertiesWidget;
@ -48,25 +49,26 @@ enum PropColumn
AVAILABILITY AVAILABILITY
}; };
class PropListDelegate final : public ProgressBarDelegate class PropListDelegate final : public QStyledItemDelegate
{ {
Q_OBJECT Q_OBJECT
Q_DISABLE_COPY(PropListDelegate)
public: public:
explicit PropListDelegate(PropertiesWidget *properties); explicit PropListDelegate(PropertiesWidget *properties);
void setEditorData(QWidget *editor, const QModelIndex &index) const override; void setEditorData(QWidget *editor, const QModelIndex &index) const override;
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem & /* option */, const QModelIndex &index) const override; QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
public slots: public slots:
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex & /* index */) const override; void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
signals: signals:
void filteredFilesChanged() const; void filteredFilesChanged() const;
private: private:
void initProgressStyleOption(QStyleOptionProgressBar &option, const QModelIndex &index) const override;
PropertiesWidget *m_properties; PropertiesWidget *m_properties;
ProgressBarPainter m_progressBarPainter;
}; };

19
src/gui/transferlistdelegate.cpp

@ -33,7 +33,7 @@
#include "transferlistmodel.h" #include "transferlistmodel.h"
TransferListDelegate::TransferListDelegate(QObject *parent) TransferListDelegate::TransferListDelegate(QObject *parent)
: ProgressBarDelegate {TransferListModel::TR_PROGRESS, TransferListModel::UnderlyingDataRole, parent} : QStyledItemDelegate {parent}
{ {
} }
@ -61,3 +61,20 @@ QSize TransferListDelegate::sizeHint(const QStyleOptionViewItem &option, const Q
size.setHeight(std::max(nameColHeight, size.height())); size.setHeight(std::max(nameColHeight, size.height()));
return size; return size;
} }
void TransferListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
switch (index.column())
{
case TransferListModel::TR_PROGRESS:
{
const int progress = static_cast<int>(index.data(TransferListModel::UnderlyingDataRole).toReal());
m_progressBarPainter.paint(painter, option, index.data().toString(), progress);
}
break;
default:
QStyledItemDelegate::paint(painter, option, index);
break;
}
}

10
src/gui/transferlistdelegate.h

@ -28,9 +28,11 @@
#pragma once #pragma once
#include "progressbardelegate.h" #include <QStyledItemDelegate>
class TransferListDelegate final : public ProgressBarDelegate #include "progressbarpainter.h"
class TransferListDelegate final : public QStyledItemDelegate
{ {
Q_OBJECT Q_OBJECT
Q_DISABLE_COPY(TransferListDelegate) Q_DISABLE_COPY(TransferListDelegate)
@ -40,4 +42,8 @@ public:
QWidget *createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const override; QWidget *createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const override;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override; QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
private:
ProgressBarPainter m_progressBarPainter;
}; };

19
src/gui/transferlistwidget.cpp

@ -126,32 +126,27 @@ namespace
} }
TransferListWidget::TransferListWidget(QWidget *parent, MainWindow *mainWindow) TransferListWidget::TransferListWidget(QWidget *parent, MainWindow *mainWindow)
: QTreeView(parent) : QTreeView {parent}
, m_mainWindow(mainWindow) , m_listModel {new TransferListModel {this}}
, m_sortFilterModel {new TransferListSortModel {this}}
, m_mainWindow {mainWindow}
{ {
setUniformRowHeights(true);
// Load settings // Load settings
bool columnLoaded = loadSettings(); const bool columnLoaded = loadSettings();
// Create and apply delegate // Create and apply delegate
m_listDelegate = new TransferListDelegate(this); setItemDelegate(new TransferListDelegate {this});
setItemDelegate(m_listDelegate);
// Create transfer list model
m_listModel = new TransferListModel(this);
m_sortFilterModel = new TransferListSortModel(this);
m_sortFilterModel->setDynamicSortFilter(true); m_sortFilterModel->setDynamicSortFilter(true);
m_sortFilterModel->setSourceModel(m_listModel); m_sortFilterModel->setSourceModel(m_listModel);
m_sortFilterModel->setFilterKeyColumn(TransferListModel::TR_NAME); m_sortFilterModel->setFilterKeyColumn(TransferListModel::TR_NAME);
m_sortFilterModel->setFilterRole(Qt::DisplayRole); m_sortFilterModel->setFilterRole(Qt::DisplayRole);
m_sortFilterModel->setSortCaseSensitivity(Qt::CaseInsensitive); m_sortFilterModel->setSortCaseSensitivity(Qt::CaseInsensitive);
m_sortFilterModel->setSortRole(TransferListModel::UnderlyingDataRole); m_sortFilterModel->setSortRole(TransferListModel::UnderlyingDataRole);
setModel(m_sortFilterModel); setModel(m_sortFilterModel);
// Visual settings // Visual settings
setUniformRowHeights(true);
setRootIsDecorated(false); setRootIsDecorated(false);
setAllColumnsShowFocus(true); setAllColumnsShowFocus(true);
setSortingEnabled(true); setSortingEnabled(true);

2
src/gui/transferlistwidget.h

@ -33,7 +33,6 @@
#include <QTreeView> #include <QTreeView>
class MainWindow; class MainWindow;
class TransferListDelegate;
class TransferListModel; class TransferListModel;
class TransferListSortModel; class TransferListSortModel;
@ -121,7 +120,6 @@ private:
void applyToSelectedTorrents(const std::function<void (BitTorrent::Torrent *const)> &fn); void applyToSelectedTorrents(const std::function<void (BitTorrent::Torrent *const)> &fn);
QVector<BitTorrent::Torrent *> getVisibleTorrents() const; QVector<BitTorrent::Torrent *> getVisibleTorrents() const;
TransferListDelegate *m_listDelegate;
TransferListModel *m_listModel; TransferListModel *m_listModel;
TransferListSortModel *m_sortFilterModel; TransferListSortModel *m_sortFilterModel;
MainWindow *m_mainWindow; MainWindow *m_mainWindow;

Loading…
Cancel
Save