Allow to customize ProgressBar color

PR #22928.
This commit is contained in:
Vladimir Golovnev 2025-07-01 10:28:26 +03:00 committed by GitHub
parent 55de9b07d2
commit efedbcb407
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 50 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, {{}, {}}}
}; };
} }

View file

@ -28,6 +28,7 @@
#include "uithemedialog.h" #include "uithemedialog.h"
#include <QtSystemDetection>
#include <QColor> #include <QColor>
#include <QColorDialog> #include <QColorDialog>
#include <QFileDialog> #include <QFileDialog>
@ -233,6 +234,10 @@ UIThemeDialog::UIThemeDialog(QWidget *parent)
{ {
m_ui->setupUi(this); m_ui->setupUi(this);
#if (defined(Q_OS_WIN) || defined(Q_OS_MACOS))
m_ui->colorsWarningLabel->hide();
#endif
connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(m_ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); connect(m_ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);

View file

@ -72,6 +72,21 @@
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_3"> <layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QLabel" name="colorsWarningLabel">
<property name="text">
<string>Note that some custom colors may not be applied in some desktop environments.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
<property name="font">
<font>
<italic>true</italic>
</font>
</property>
</widget>
</item>
<item> <item>
<layout class="QGridLayout" name="colorsLayout" columnstretch="1,0,0,0,0" columnminimumwidth="0,15,0,15,0"> <layout class="QGridLayout" name="colorsLayout" columnstretch="1,0,0,0,0" columnminimumwidth="0,15,0,15,0">
<item row="0" column="0"> <item row="0" column="0">