revert progressbarpainter.

add QColor param to progressbarpainter.paint

transferlistdelegate.cpp determines progress bar color
This commit is contained in:
Mark 2025-07-06 18:11:19 +04:00
commit ed2090261b
4 changed files with 57 additions and 59 deletions

View file

@ -39,51 +39,8 @@
#endif #endif
#include "base/global.h" #include "base/global.h"
#include "base/bittorrent/torrent.h"
#include "gui/uithememanager.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) ProgressBarPainter::ProgressBarPainter(QObject *parent)
: QObject(parent) : QObject(parent)
{ {
@ -93,12 +50,11 @@ ProgressBarPainter::ProgressBarPainter(QObject *parent)
m_dummyProgressBar.setStyle(fusionStyle); m_dummyProgressBar.setStyle(fusionStyle);
#endif #endif
loadUIThemeResources();
applyUITheme(); applyUITheme();
connect(UIThemeManager::instance(), &UIThemeManager::themeChanged, this, &ProgressBarPainter::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; QStyleOptionProgressBar styleOption;
styleOption.initFrom(&m_dummyProgressBar); 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); const bool isEnabled = option.state.testFlag(QStyle::State_Enabled);
styleOption.palette.setCurrentColorGroup(isEnabled ? QPalette::Active : QPalette::Disabled); 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()) else if (m_chunkColor.isValid())
{ {
@ -136,9 +92,3 @@ void ProgressBarPainter::applyUITheme()
{ {
m_chunkColor = UIThemeManager::instance()->getColor(u"ProgressBar"_s); m_chunkColor = UIThemeManager::instance()->getColor(u"ProgressBar"_s);
} }
void ProgressBarPainter::loadUIThemeResources()
{
m_stateThemeColors = torrentStateColorsFromUITheme();
}

View file

@ -33,8 +33,6 @@
#include <QObject> #include <QObject>
#include <QProgressBar> #include <QProgressBar>
#include "base/bittorrent/torrent.h"
class QStyleOptionViewItem; class QStyleOptionViewItem;
class ProgressBarPainter : public QObject class ProgressBarPainter : public QObject
@ -45,12 +43,10 @@ class ProgressBarPainter : public QObject
public: public:
explicit ProgressBarPainter(QObject *parent = nullptr); 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: private:
void applyUITheme(); void applyUITheme();
void loadUIThemeResources();
QHash<BitTorrent::TorrentState, QColor> m_stateThemeColors;
QColor m_chunkColor; QColor m_chunkColor;
// for painting progressbar with stylesheet option, a dummy progress bar is required // for painting progressbar with stylesheet option, a dummy progress bar is required

View file

@ -31,10 +31,54 @@
#include <QModelIndex> #include <QModelIndex>
#include "transferlistmodel.h" #include "transferlistmodel.h"
#include "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;
}
}
TransferListDelegate::TransferListDelegate(QObject *parent) TransferListDelegate::TransferListDelegate(QObject *parent)
: QStyledItemDelegate {parent} : QStyledItemDelegate {parent}
{ {
m_stateThemeColors = torrentStateColorsFromUITheme();
} }
QWidget *TransferListDelegate::createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const QWidget *TransferListDelegate::createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const
@ -90,7 +134,13 @@ void TransferListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
QStyleOptionViewItem customOption {option}; QStyleOptionViewItem customOption {option};
customOption.state.setFlag(QStyle::State_Enabled, isEnableState(torrentState)); 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; break;
default: default:

View file

@ -31,6 +31,7 @@
#include <QStyledItemDelegate> #include <QStyledItemDelegate>
#include "progressbarpainter.h" #include "progressbarpainter.h"
#include "base/bittorrent/torrent.h"
class TransferListDelegate final : public QStyledItemDelegate class TransferListDelegate final : public QStyledItemDelegate
{ {
@ -47,4 +48,5 @@ public:
private: private:
ProgressBarPainter m_progressBarPainter; ProgressBarPainter m_progressBarPainter;
mutable int m_nameColHeight = -1; mutable int m_nameColHeight = -1;
QHash<BitTorrent::TorrentState, QColor> m_stateThemeColors;
}; };