apply torrent state color theme to progress bar

This commit is contained in:
Mark 2025-07-06 13:29:33 +04:00
commit abe13bf838
5 changed files with 64 additions and 13 deletions

View file

@ -34,6 +34,7 @@
#include "base/utils/string.h"
#include "previewselectdialog.h"
#include "transferlistmodel.h"
PreviewListDelegate::PreviewListDelegate(QObject *parent)
: QStyledItemDelegate(parent)
@ -53,7 +54,10 @@ void PreviewListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &o
? u"100%"_s
: (Utils::String::fromDouble(progress, 1) + u'%');
m_progressBarPainter.paint(painter, option, text, static_cast<int>(progress));
const QModelIndex statusIndex = index.siblingAtColumn(TransferListModel::TR_STATUS);
const auto torrentState = statusIndex.data(TransferListModel::UnderlyingDataRole).value<BitTorrent::TorrentState>();
m_progressBarPainter.paint(painter, option, text, static_cast<int>(progress), torrentState);
}
break;

View file

@ -39,8 +39,51 @@
#endif
#include "base/global.h"
#include "base/bittorrent/torrent.h"
#include "gui/uithememanager.h"
namespace
{
QHash<BitTorrent::TorrentState, QColor> torrentStateColorsFromUITheme()
{
struct TorrentStateColorDescriptor
{
const BitTorrent::TorrentState state;
const QString id;
};
const TorrentStateColorDescriptor colorDescriptors[] =
{
{BitTorrent::TorrentState::Downloading, u"TransferList.Downloading"_s},
{BitTorrent::TorrentState::StalledDownloading, u"TransferList.StalledDownloading"_s},
{BitTorrent::TorrentState::DownloadingMetadata, u"TransferList.DownloadingMetadata"_s},
{BitTorrent::TorrentState::ForcedDownloadingMetadata, u"TransferList.ForcedDownloadingMetadata"_s},
{BitTorrent::TorrentState::ForcedDownloading, u"TransferList.ForcedDownloading"_s},
{BitTorrent::TorrentState::Uploading, u"TransferList.Uploading"_s},
{BitTorrent::TorrentState::StalledUploading, u"TransferList.StalledUploading"_s},
{BitTorrent::TorrentState::ForcedUploading, u"TransferList.ForcedUploading"_s},
{BitTorrent::TorrentState::QueuedDownloading, u"TransferList.QueuedDownloading"_s},
{BitTorrent::TorrentState::QueuedUploading, u"TransferList.QueuedUploading"_s},
{BitTorrent::TorrentState::CheckingDownloading, u"TransferList.CheckingDownloading"_s},
{BitTorrent::TorrentState::CheckingUploading, u"TransferList.CheckingUploading"_s},
{BitTorrent::TorrentState::CheckingResumeData, u"TransferList.CheckingResumeData"_s},
{BitTorrent::TorrentState::StoppedDownloading, u"TransferList.StoppedDownloading"_s},
{BitTorrent::TorrentState::StoppedUploading, u"TransferList.StoppedUploading"_s},
{BitTorrent::TorrentState::Moving, u"TransferList.Moving"_s},
{BitTorrent::TorrentState::MissingFiles, u"TransferList.MissingFiles"_s},
{BitTorrent::TorrentState::Error, u"TransferList.Error"_s}
};
QHash<BitTorrent::TorrentState, QColor> colors;
for (const TorrentStateColorDescriptor &colorDescriptor : colorDescriptors)
{
const QColor themeColor = UIThemeManager::instance()->getColor(colorDescriptor.id);
colors.insert(colorDescriptor.state, themeColor);
}
return colors;
}
}
ProgressBarPainter::ProgressBarPainter(QObject *parent)
: QObject(parent)
{
@ -50,11 +93,10 @@ ProgressBarPainter::ProgressBarPainter(QObject *parent)
m_dummyProgressBar.setStyle(fusionStyle);
#endif
applyUITheme();
connect(UIThemeManager::instance(), &UIThemeManager::themeChanged, this, &ProgressBarPainter::applyUITheme);
loadUIThemeResources();
}
void ProgressBarPainter::paint(QPainter *painter, const QStyleOptionViewItem &option, const QString &text, const int progress) const
void ProgressBarPainter::paint(QPainter *painter, const QStyleOptionViewItem &option, const QString &text, const int progress, const BitTorrent::TorrentState torrentState) const
{
QStyleOptionProgressBar styleOption;
styleOption.initFrom(&m_dummyProgressBar);
@ -72,8 +114,7 @@ void ProgressBarPainter::paint(QPainter *painter, const QStyleOptionViewItem &op
const bool isEnabled = option.state.testFlag(QStyle::State_Enabled);
styleOption.palette.setCurrentColorGroup(isEnabled ? QPalette::Active : QPalette::Disabled);
if (m_chunkColor.isValid())
styleOption.palette.setColor(QPalette::Highlight, m_chunkColor);
styleOption.palette.setColor(QPalette::Highlight, m_stateThemeColors.value(torrentState));
painter->save();
const QStyle *style = m_dummyProgressBar.style();
@ -82,7 +123,7 @@ void ProgressBarPainter::paint(QPainter *painter, const QStyleOptionViewItem &op
painter->restore();
}
void ProgressBarPainter::applyUITheme()
void ProgressBarPainter::loadUIThemeResources()
{
m_chunkColor = UIThemeManager::instance()->getColor(u"ProgressBar"_s);
m_stateThemeColors = torrentStateColorsFromUITheme();
}

View file

@ -33,6 +33,8 @@
#include <QObject>
#include <QProgressBar>
#include "base/bittorrent/torrent.h"
class QStyleOptionViewItem;
class ProgressBarPainter : public QObject
@ -43,12 +45,12 @@ class ProgressBarPainter : public QObject
public:
explicit ProgressBarPainter(QObject *parent = nullptr);
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QString &text, int progress) const;
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QString &text, int progress, BitTorrent::TorrentState torrentState) const;
private:
void applyUITheme();
void loadUIThemeResources();
QHash<BitTorrent::TorrentState, QColor> m_stateThemeColors;
QColor m_chunkColor;
// for painting progressbar with stylesheet option, a dummy progress bar is required
QProgressBar m_dummyProgressBar;
};

View file

@ -34,6 +34,7 @@
#include <QPainter>
#include <QProgressBar>
#include "transferlistmodel.h"
#include "base/bittorrent/downloadpriority.h"
#include "base/bittorrent/torrent.h"
#include "gui/torrentcontentmodel.h"
@ -140,10 +141,13 @@ void TorrentContentItemDelegate::paint(QPainter *painter, const QStyleOptionView
const int priority = index.sibling(index.row(), TorrentContentModelItem::COL_PRIO).data(TorrentContentModel::UnderlyingDataRole).toInt();
const bool isEnabled = static_cast<BitTorrent::DownloadPriority>(priority) != BitTorrent::DownloadPriority::Ignored;
const QModelIndex statusIndex = index.siblingAtColumn(TransferListModel::TR_STATUS);
const auto torrentState = statusIndex.data(TransferListModel::UnderlyingDataRole).value<BitTorrent::TorrentState>();
QStyleOptionViewItem customOption {option};
customOption.state.setFlag(QStyle::State_Enabled, isEnabled);
m_progressBarPainter.paint(painter, customOption, index.data().toString(), progress);
m_progressBarPainter.paint(painter, customOption, index.data().toString(), progress, torrentState);
}
break;
default:

View file

@ -90,7 +90,7 @@ void TransferListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
QStyleOptionViewItem customOption {option};
customOption.state.setFlag(QStyle::State_Enabled, isEnableState(torrentState));
m_progressBarPainter.paint(painter, customOption, index.data().toString(), progress);
m_progressBarPainter.paint(painter, customOption, index.data().toString(), progress, torrentState);
}
break;
default: