Allow to customize ProgressBar colors

This commit is contained in:
Vladimir Golovnev (Glassez) 2025-06-26 15:18:32 +03:00
commit 6e335a3278
No known key found for this signature in database
GPG key ID: 52A2C7DEE2DFA6F7
3 changed files with 30 additions and 5 deletions

View file

@ -1,5 +1,6 @@
/* /*
* Bittorrent Client using Qt and libtorrent. * Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2025 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2020 Prince Gupta <jagannatharjun11@gmail.com> * Copyright (C) 2020 Prince Gupta <jagannatharjun11@gmail.com>
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
@ -38,14 +39,19 @@
#endif #endif
#include "base/global.h" #include "base/global.h"
#include "gui/uithememanager.h"
ProgressBarPainter::ProgressBarPainter() ProgressBarPainter::ProgressBarPainter(QObject *parent)
: QObject(parent)
{ {
#if (defined(Q_OS_WIN) || defined(Q_OS_MACOS)) #if (defined(Q_OS_WIN) || defined(Q_OS_MACOS))
auto *fusionStyle = new QProxyStyle {u"fusion"_s}; auto *fusionStyle = new QProxyStyle(u"fusion"_s);
fusionStyle->setParent(&m_dummyProgressBar); fusionStyle->setParent(&m_dummyProgressBar);
m_dummyProgressBar.setStyle(fusionStyle); m_dummyProgressBar.setStyle(fusionStyle);
#endif #endif
applyUITheme();
connect(UIThemeManager::instance(), &UIThemeManager::themeChanged, this, &ProgressBarPainter::applyUITheme);
} }
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
@ -66,9 +72,17 @@ 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 (m_chunkColor.isValid())
styleOption.palette.setColor(QPalette::Highlight, m_chunkColor);
painter->save(); painter->save();
const QStyle *style = m_dummyProgressBar.style(); const QStyle *style = m_dummyProgressBar.style();
style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, option.widget); style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, option.widget);
style->drawControl(QStyle::CE_ProgressBar, &styleOption, painter, &m_dummyProgressBar); style->drawControl(QStyle::CE_ProgressBar, &styleOption, painter, &m_dummyProgressBar);
painter->restore(); painter->restore();
} }
void ProgressBarPainter::applyUITheme()
{
m_chunkColor = UIThemeManager::instance()->getColor(u"ProgressBar"_s);
}

View file

@ -1,5 +1,6 @@
/* /*
* Bittorrent Client using Qt and libtorrent. * Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2025 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2020 Prince Gupta <jagannatharjun11@gmail.com> * Copyright (C) 2020 Prince Gupta <jagannatharjun11@gmail.com>
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
@ -28,18 +29,26 @@
#pragma once #pragma once
#include <QColor>
#include <QObject>
#include <QProgressBar> #include <QProgressBar>
class QStyleOptionViewItem; class QStyleOptionViewItem;
class ProgressBarPainter class ProgressBarPainter : public QObject
{ {
Q_OBJECT
Q_DISABLE_COPY_MOVE(ProgressBarPainter)
public: public:
ProgressBarPainter(); 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) const;
private: private:
void applyUITheme();
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
QProgressBar m_dummyProgressBar; QProgressBar m_dummyProgressBar;
}; };

View file

@ -85,7 +85,9 @@ inline QHash<QString, UIThemeColor> defaultUIThemeColors()
{u"PiecesBar.Border"_s, {{}, {}}}, {u"PiecesBar.Border"_s, {{}, {}}},
{u"PiecesBar.Piece"_s, {{}, {}}}, {u"PiecesBar.Piece"_s, {{}, {}}},
{u"PiecesBar.PartialPiece"_s, {{}, {}}}, {u"PiecesBar.PartialPiece"_s, {{}, {}}},
{u"PiecesBar.MissingPiece"_s, {{}, {}}} {u"PiecesBar.MissingPiece"_s, {{}, {}}},
{u"ProgressBar"_s, {{}, {}}}
}; };
} }