Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Qt6CT::{resolvePath,loadColorScheme} refactor #58

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions src/qt6ct-common/qt6ct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ QStringList Qt6CT::sharedColorSchemePaths()

QString Qt6CT::resolvePath(const QString &path)
{
if(path.isEmpty())
return path;

QString tmp = path;
tmp.replace("~", QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
if(!tmp.contains("$"))
Expand All @@ -143,17 +146,18 @@ QString Qt6CT::resolvePath(const QString &path)
return tmp;
}

QPalette Qt6CT::loadColorScheme(const QString &filePath, const QPalette &fallback)
std::optional<QPalette> Qt6CT::loadColorScheme(const QString &filePath)
{
QPalette customPalette;
if(filePath.isEmpty())
return std::nullopt;

QSettings settings(filePath, QSettings::IniFormat);
settings.beginGroup("ColorScheme");
QStringList activeColors = settings.value("active_colors").toStringList();
QStringList inactiveColors = settings.value("inactive_colors").toStringList();
QStringList disabledColors = settings.value("disabled_colors").toStringList();
settings.endGroup();


#if (QT_VERSION >= QT_VERSION_CHECK(6,6,0))
if(activeColors.count() == QPalette::Accent)
activeColors << activeColors.at(QPalette::Highlight);
Expand All @@ -163,24 +167,19 @@ QPalette Qt6CT::loadColorScheme(const QString &filePath, const QPalette &fallbac
disabledColors << disabledColors.at(QPalette::Highlight);
#endif

if(activeColors.count() < QPalette::NColorRoles ||
inactiveColors.count() < QPalette::NColorRoles ||
disabledColors.count() < QPalette::NColorRoles)
return std::nullopt;

if(activeColors.count() >= QPalette::NColorRoles &&
inactiveColors.count() >= QPalette::NColorRoles &&
disabledColors.count() >= QPalette::NColorRoles)
{
for (int i = 0; i < QPalette::NColorRoles; i++)
{
QPalette::ColorRole role = QPalette::ColorRole(i);
customPalette.setColor(QPalette::Active, role, QColor(activeColors.at(i)));
customPalette.setColor(QPalette::Inactive, role, QColor(inactiveColors.at(i)));
customPalette.setColor(QPalette::Disabled, role, QColor(disabledColors.at(i)));
}
}
else
QPalette customPalette;
for (int i = 0; i < QPalette::NColorRoles; i++)
{
customPalette = fallback; //load fallback palette
QPalette::ColorRole role = QPalette::ColorRole(i);
customPalette.setColor(QPalette::Active, role, QColor(activeColors.at(i)));
customPalette.setColor(QPalette::Inactive, role, QColor(inactiveColors.at(i)));
customPalette.setColor(QPalette::Disabled, role, QColor(disabledColors.at(i)));
}

return customPalette;
}

Expand Down
2 changes: 1 addition & 1 deletion src/qt6ct-common/qt6ct.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class QT6CT_EXPORT Qt6CT
static QString userColorSchemePath();
static QStringList sharedColorSchemePaths();
static QString resolvePath(const QString &path);
static QPalette loadColorScheme(const QString &filePath, const QPalette &fallback);
static std::optional<QPalette> loadColorScheme(const QString &filePath);

static void registerStyleInstance(StyleInstance *instance);
static void unregisterStyleInstance(StyleInstance *instance);
Expand Down
16 changes: 6 additions & 10 deletions src/qt6ct-qtplugin/qt6ctplatformtheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ QPlatformDialogHelper *Qt6CTPlatformTheme::createPlatformDialogHelper(DialogType
const QPalette *Qt6CTPlatformTheme::palette(QPlatformTheme::Palette type) const
{
qDebug() << Q_FUNC_INFO << type;
return (m_usePalette && m_palette) ? m_palette.get() : QGenericUnixTheme::palette(type);
return (m_usePalette && m_palette) ? &*m_palette : QGenericUnixTheme::palette(type);
}

const QFont *Qt6CTPlatformTheme::font(QPlatformTheme::Font type) const
Expand Down Expand Up @@ -183,7 +183,7 @@ void Qt6CTPlatformTheme::applySettings()
}

if(!m_palette)
m_palette = std::make_unique<QPalette>(qApp->style()->standardPalette());
m_palette = qApp->style()->standardPalette();

if(m_update && m_usePalette)
qApp->setPalette(*m_palette);
Expand Down Expand Up @@ -250,18 +250,14 @@ void Qt6CTPlatformTheme::updateSettings()

void Qt6CTPlatformTheme::readSettings()
{
m_palette.reset();

QSettings settings(Qt6CT::configFile(), QSettings::IniFormat);

settings.beginGroup("Appearance");
m_style = settings.value("style", "Fusion").toString();
QString schemePath = settings.value("color_scheme_path").toString();
if(!schemePath.isEmpty() && settings.value("custom_palette", false).toBool())
{
schemePath = Qt6CT::resolvePath(schemePath); //replace environment variables
m_palette = std::make_unique<QPalette>(Qt6CT::loadColorScheme(schemePath, *QPlatformTheme::palette(SystemPalette)));
}
QString schemePath = settings.value("custom_palette", false).toBool()
? Qt6CT::resolvePath(settings.value("color_scheme_path").toString()) //replace environment variables
: QString();
m_palette = Qt6CT::loadColorScheme(schemePath);
m_iconTheme = settings.value("icon_theme").toString();
//load dialogs
if(!m_update)
Expand Down
2 changes: 1 addition & 1 deletion src/qt6ct-qtplugin/qt6ctplatformtheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private slots:
#endif
QString loadStyleSheets(const QStringList &paths);
QString m_style, m_iconTheme, m_userStyleSheet, m_prevStyleSheet;
std::unique_ptr<QPalette> m_palette;
std::optional<QPalette> m_palette;
QFont m_generalFont, m_fixedFont;
int m_doubleClickInterval;
int m_cursorFlashTime;
Expand Down
7 changes: 3 additions & 4 deletions src/qt6ct/appearancepage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ void AppearancePage::on_styleComboBox_textActivated(const QString &text)

void AppearancePage::on_colorSchemeComboBox_activated(int)
{
m_customPalette = Qt6CT::loadColorScheme(m_ui->colorSchemeComboBox->currentData().toString(), palette());
m_customPalette = Qt6CT::loadColorScheme(m_ui->colorSchemeComboBox->currentData().toString()).value_or(palette());
updatePalette();
}

Expand Down Expand Up @@ -326,8 +326,7 @@ void AppearancePage::readSettings()
m_ui->styleComboBox->setCurrentText(style);

m_ui->customPaletteButton->setChecked(settings.value("custom_palette", false).toBool());
QString colorSchemePath = settings.value("color_scheme_path").toString();
colorSchemePath = Qt6CT::resolvePath(colorSchemePath); //replace environment variables
QString colorSchemePath = Qt6CT::resolvePath(settings.value("color_scheme_path").toString()); //replace environment variables

QDir("/").mkpath(Qt6CT::userColorSchemePath());
findColorSchemes(Qt6CT::userColorSchemePath());
Expand All @@ -342,7 +341,7 @@ void AppearancePage::readSettings()
int index = m_ui->colorSchemeComboBox->findData(colorSchemePath);
if(index >= 0)
m_ui->colorSchemeComboBox->setCurrentIndex(index);
m_customPalette = Qt6CT::loadColorScheme(m_ui->colorSchemeComboBox->currentData().toString(), palette());
m_customPalette = Qt6CT::loadColorScheme(m_ui->colorSchemeComboBox->currentData().toString()).value_or(palette());
}

on_styleComboBox_textActivated(m_ui->styleComboBox->currentText());
Expand Down