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. 28
      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 @@ -26,7 +26,7 @@ add_library(qbt_gui STATIC
powermanagement/powermanagement.h
previewlistdelegate.h
previewselectdialog.h
progressbardelegate.h
progressbarpainter.h
properties/downloadedpiecesbar.h
properties/peerlistsortmodel.h
properties/peerlistwidget.h
@ -106,7 +106,7 @@ add_library(qbt_gui STATIC @@ -106,7 +106,7 @@ add_library(qbt_gui STATIC
powermanagement/powermanagement.cpp
previewlistdelegate.cpp
previewselectdialog.cpp
progressbardelegate.cpp
progressbarpainter.cpp
properties/downloadedpiecesbar.cpp
properties/peerlistsortmodel.cpp
properties/peerlistwidget.cpp

4
src/gui/gui.pri

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

28
src/gui/previewlistdelegate.cpp

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

6
src/gui/previewlistdelegate.h

@ -30,6 +30,8 @@ @@ -30,6 +30,8 @@
#include <QItemDelegate>
#include "progressbarpainter.h"
class PreviewListDelegate final : public QItemDelegate
{
Q_OBJECT
@ -38,7 +40,9 @@ class PreviewListDelegate final : public QItemDelegate @@ -38,7 +40,9 @@ class PreviewListDelegate final : public QItemDelegate
public:
explicit PreviewListDelegate(QObject *parent = nullptr);
private:
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) 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 @@ @@ -26,49 +26,46 @@
* exception statement from your version.
*/
#include "progressbardelegate.h"
#include "progressbarpainter.h"
#include <QApplication>
#include <QModelIndex>
#include <QPainter>
#include <QPalette>
#include <QStyleOptionProgressBar>
#include <QStyleOptionViewItem>
#if (defined(Q_OS_WIN) || defined(Q_OS_MACOS))
#include <QProxyStyle>
#endif
ProgressBarDelegate::ProgressBarDelegate(const int progressColumn, const int dataRole, QObject *parent)
: QStyledItemDelegate {parent}
, m_progressColumn {progressColumn}
, m_dataRole {dataRole}
ProgressBarPainter::ProgressBarPainter()
{
#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
}
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();
option.progress = static_cast<int>(index.data(m_dataRole).toReal());
option.maximum = 100;
option.minimum = 0;
option.textVisible = true;
}
void ProgressBarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (index.column() != m_progressColumn)
return QStyledItemDelegate::paint(painter, option, index);
QStyleOptionProgressBar styleOption;
styleOption.initFrom(&m_dummyProgressBar);
// QStyleOptionProgressBar fields
styleOption.maximum = 100;
styleOption.minimum = 0;
styleOption.progress = progress;
styleOption.text = text;
styleOption.textVisible = true;
// QStyleOption fields
styleOption.rect = option.rect;
styleOption.state = option.state;
QStyleOptionProgressBar newopt;
newopt.initFrom(&m_dummyProgressBar);
newopt.rect = option.rect;
newopt.state = option.state;
initProgressStyleOption(newopt, index);
const bool isEnabled = option.state.testFlag(QStyle::State_Enabled);
styleOption.palette.setCurrentColorGroup(isEnabled ? QPalette::Active : QPalette::Disabled);
painter->save();
m_dummyProgressBar.style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter);
m_dummyProgressBar.style()->drawControl(QStyle::CE_ProgressBar, &newopt, painter, &m_dummyProgressBar);
const QStyle *style = m_dummyProgressBar.style();
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter);
style->drawControl(QStyle::CE_ProgressBar, &styleOption, painter, &m_dummyProgressBar);
painter->restore();
}

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

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

65
src/gui/properties/proplistdelegate.cpp

@ -28,61 +28,22 @@ @@ -28,61 +28,22 @@
#include "proplistdelegate.h"
#include <QApplication>
#include <QComboBox>
#include <QModelIndex>
#include <QPainter>
#include <QPalette>
#include <QProgressBar>
#include <QStyleOptionProgressBar>
#if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
#include <QProxyStyle>
#endif
#include "base/bittorrent/downloadpriority.h"
#include "base/bittorrent/torrent.h"
#include "gui/torrentcontentmodel.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)
: ProgressBarDelegate {PROGRESS, TorrentContentModel::UnderlyingDataRole, properties}
, m_properties(properties)
: QStyledItemDelegate {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
{
auto *combobox = static_cast<QComboBox *>(editor);
@ -156,3 +117,25 @@ void PropListDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionV @@ -156,3 +117,25 @@ void PropListDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionV
{
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 @@ @@ -28,11 +28,12 @@
#pragma once
#include "gui/progressbardelegate.h"
#include <QStyledItemDelegate>
#include "gui/progressbarpainter.h"
class QAbstractItemModel;
class QModelIndex;
class QPainter;
class QStyleOptionViewItem;
class PropertiesWidget;
@ -48,25 +49,26 @@ enum PropColumn @@ -48,25 +49,26 @@ enum PropColumn
AVAILABILITY
};
class PropListDelegate final : public ProgressBarDelegate
class PropListDelegate final : public QStyledItemDelegate
{
Q_OBJECT
Q_DISABLE_COPY(PropListDelegate)
public:
explicit PropListDelegate(PropertiesWidget *properties);
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:
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:
void filteredFilesChanged() const;
private:
void initProgressStyleOption(QStyleOptionProgressBar &option, const QModelIndex &index) const override;
PropertiesWidget *m_properties;
ProgressBarPainter m_progressBarPainter;
};

19
src/gui/transferlistdelegate.cpp

@ -33,7 +33,7 @@ @@ -33,7 +33,7 @@
#include "transferlistmodel.h"
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 @@ -61,3 +61,20 @@ QSize TransferListDelegate::sizeHint(const QStyleOptionViewItem &option, const Q
size.setHeight(std::max(nameColHeight, size.height()));
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 @@ @@ -28,9 +28,11 @@
#pragma once
#include "progressbardelegate.h"
#include <QStyledItemDelegate>
class TransferListDelegate final : public ProgressBarDelegate
#include "progressbarpainter.h"
class TransferListDelegate final : public QStyledItemDelegate
{
Q_OBJECT
Q_DISABLE_COPY(TransferListDelegate)
@ -40,4 +42,8 @@ public: @@ -40,4 +42,8 @@ public:
QWidget *createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) 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 @@ -126,32 +126,27 @@ namespace
}
TransferListWidget::TransferListWidget(QWidget *parent, MainWindow *mainWindow)
: QTreeView(parent)
, m_mainWindow(mainWindow)
: QTreeView {parent}
, m_listModel {new TransferListModel {this}}
, m_sortFilterModel {new TransferListSortModel {this}}
, m_mainWindow {mainWindow}
{
setUniformRowHeights(true);
// Load settings
bool columnLoaded = loadSettings();
const bool columnLoaded = loadSettings();
// Create and apply delegate
m_listDelegate = new TransferListDelegate(this);
setItemDelegate(m_listDelegate);
// Create transfer list model
m_listModel = new TransferListModel(this);
setItemDelegate(new TransferListDelegate {this});
m_sortFilterModel = new TransferListSortModel(this);
m_sortFilterModel->setDynamicSortFilter(true);
m_sortFilterModel->setSourceModel(m_listModel);
m_sortFilterModel->setFilterKeyColumn(TransferListModel::TR_NAME);
m_sortFilterModel->setFilterRole(Qt::DisplayRole);
m_sortFilterModel->setSortCaseSensitivity(Qt::CaseInsensitive);
m_sortFilterModel->setSortRole(TransferListModel::UnderlyingDataRole);
setModel(m_sortFilterModel);
// Visual settings
setUniformRowHeights(true);
setRootIsDecorated(false);
setAllColumnsShowFocus(true);
setSortingEnabled(true);

2
src/gui/transferlistwidget.h

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

Loading…
Cancel
Save