diff --git a/src/gui/progressbarpainter.cpp b/src/gui/progressbarpainter.cpp index 33154aec2..2932856d0 100644 --- a/src/gui/progressbarpainter.cpp +++ b/src/gui/progressbarpainter.cpp @@ -39,51 +39,8 @@ #endif #include "base/global.h" -#include "base/bittorrent/torrent.h" #include "gui/uithememanager.h" -namespace -{ - QHash 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 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) { @@ -93,12 +50,11 @@ ProgressBarPainter::ProgressBarPainter(QObject *parent) m_dummyProgressBar.setStyle(fusionStyle); #endif - loadUIThemeResources(); applyUITheme(); connect(UIThemeManager::instance(), &UIThemeManager::themeChanged, this, &ProgressBarPainter::applyUITheme); } -void ProgressBarPainter::paint(QPainter *painter, const QStyleOptionViewItem &option, const QString &text, const int progress, const BitTorrent::TorrentState torrentState) const +void ProgressBarPainter::paint(QPainter *painter, const QStyleOptionViewItem &option, const QString &text, const int progress, const QColor color) const { QStyleOptionProgressBar styleOption; styleOption.initFrom(&m_dummyProgressBar); @@ -116,9 +72,9 @@ 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 (torrentState != BitTorrent::TorrentState::Unknown) + if (color != nullptr) { - styleOption.palette.setColor(QPalette::Highlight, m_stateThemeColors.value(torrentState)); + styleOption.palette.setColor(QPalette::Highlight, color); } else if (m_chunkColor.isValid()) { @@ -136,9 +92,3 @@ void ProgressBarPainter::applyUITheme() { m_chunkColor = UIThemeManager::instance()->getColor(u"ProgressBar"_s); } - -void ProgressBarPainter::loadUIThemeResources() -{ - m_stateThemeColors = torrentStateColorsFromUITheme(); -} - diff --git a/src/gui/progressbarpainter.h b/src/gui/progressbarpainter.h index 9bc75888f..31ddba72f 100644 --- a/src/gui/progressbarpainter.h +++ b/src/gui/progressbarpainter.h @@ -33,8 +33,6 @@ #include #include -#include "base/bittorrent/torrent.h" - class QStyleOptionViewItem; class ProgressBarPainter : public QObject @@ -45,12 +43,10 @@ class ProgressBarPainter : public QObject public: explicit ProgressBarPainter(QObject *parent = nullptr); - void paint(QPainter *painter, const QStyleOptionViewItem &option, const QString &text, int progress, BitTorrent::TorrentState state = BitTorrent::TorrentState::Unknown) const; + void paint(QPainter *painter, const QStyleOptionViewItem &option, const QString &text, int progress, QColor color = nullptr) const; private: void applyUITheme(); - void loadUIThemeResources(); - QHash m_stateThemeColors; QColor m_chunkColor; // for painting progressbar with stylesheet option, a dummy progress bar is required diff --git a/src/gui/transferlistdelegate.cpp b/src/gui/transferlistdelegate.cpp index 37c326674..11ed7e416 100644 --- a/src/gui/transferlistdelegate.cpp +++ b/src/gui/transferlistdelegate.cpp @@ -31,10 +31,54 @@ #include #include "transferlistmodel.h" +#include "uithememanager.h" + +namespace +{ + QHash 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 colors; + for (const TorrentStateColorDescriptor &colorDescriptor : colorDescriptors) + { + const QColor themeColor = UIThemeManager::instance()->getColor(colorDescriptor.id); + colors.insert(colorDescriptor.state, themeColor); + } + return colors; + } +} TransferListDelegate::TransferListDelegate(QObject *parent) : QStyledItemDelegate {parent} { + m_stateThemeColors = torrentStateColorsFromUITheme(); } QWidget *TransferListDelegate::createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const @@ -90,7 +134,13 @@ 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, torrentState); + QColor color = nullptr; + if (torrentState != TorrentState::Unknown) + { + color = m_stateThemeColors.value(torrentState); + } + + m_progressBarPainter.paint(painter, customOption, index.data().toString(), progress, color); } break; default: diff --git a/src/gui/transferlistdelegate.h b/src/gui/transferlistdelegate.h index 01194d118..de4939897 100644 --- a/src/gui/transferlistdelegate.h +++ b/src/gui/transferlistdelegate.h @@ -31,6 +31,7 @@ #include #include "progressbarpainter.h" +#include "base/bittorrent/torrent.h" class TransferListDelegate final : public QStyledItemDelegate { @@ -47,4 +48,5 @@ public: private: ProgressBarPainter m_progressBarPainter; mutable int m_nameColHeight = -1; + QHash m_stateThemeColors; };