Skip to content

Commit

Permalink
Add KDE color schemes support
Browse files Browse the repository at this point in the history
  • Loading branch information
ilya-fedin committed Oct 21, 2024
1 parent 33e0cf8 commit 7aaa013
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 7 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ add_definitions(-DQT_DISABLE_DEPRECATED_BEFORE=0x060000 -DUSE_WIDGETS)

find_package(Qt6 REQUIRED COMPONENTS BuildInternals Core Widgets OPTIONAL_COMPONENTS QuickControls2 LinguistTools)
find_package(KF6Config)
find_package(KF6ColorScheme)

get_target_property(QT_QTPATHS_EXECUTABLE Qt6::qtpaths IMPORTED_LOCATION)

Expand Down
3 changes: 2 additions & 1 deletion src/qt6ct-common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../)

add_library(qt6ct-common SHARED ${app_SRCS})
set_target_properties(qt6ct-common PROPERTIES VERSION ${QT6CT_VERSION})
target_link_libraries(qt6ct-common PRIVATE Qt6::Gui)
target_link_libraries(qt6ct-common PRIVATE Qt6::Gui $<TARGET_NAME_IF_EXISTS:KF6::ConfigCore> $<TARGET_NAME_IF_EXISTS:KF6::ColorScheme>)
target_compile_definitions(qt6ct-common PRIVATE $<$<TARGET_EXISTS:KF6::ConfigCore>:KF_CONFIGCORE_LIB> $<$<TARGET_EXISTS:KF6::ColorScheme>:KF_COLORSCHEME_LIB>)
install(TARGETS qt6ct-common DESTINATION ${CMAKE_INSTALL_LIBDIR})
17 changes: 17 additions & 0 deletions src/qt6ct-common/qt6ct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
#include <QFile>
#include <QSettings>
#include <QtDebug>
#if defined KF_CONFIGCORE_LIB && defined KF_COLORSCHEME_LIB
#include <KSharedConfig>
#include <KColorScheme>
#endif
#include "qt6ct.h"

#ifndef QT6CT_DATADIR
Expand Down Expand Up @@ -116,6 +120,9 @@ QStringList Qt6CT::sharedColorSchemePaths()
for(const QString &p : QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation))
{
paths << (p + QLatin1String("/qt6ct/colors"));
#if defined KF_CONFIGCORE_LIB && defined KF_COLORSCHEME_LIB
paths << (p + QLatin1String("/color-schemes"));
#endif
}
paths << QLatin1String(QT6CT_DATADIR"/qt6ct/colors");
paths.removeDuplicates();
Expand Down Expand Up @@ -143,8 +150,18 @@ QString Qt6CT::resolvePath(const QString &path)
return tmp;
}

bool Qt6CT::isKColorScheme(const QString &filePath)
{
return filePath.toLower().endsWith(".colors");
}

QPalette Qt6CT::loadColorScheme(const QString &filePath, const QPalette &fallback)
{
#if defined KF_CONFIGCORE_LIB && defined KF_COLORSCHEME_LIB
if(isKColorScheme(filePath))
return KColorScheme::createApplicationPalette(KSharedConfig::openConfig(filePath));
#endif

QPalette customPalette;
QSettings settings(filePath, QSettings::IniFormat);
settings.beginGroup("ColorScheme");
Expand Down
1 change: 1 addition & 0 deletions src/qt6ct-common/qt6ct.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class QT6CT_EXPORT Qt6CT
static QString userColorSchemePath();
static QStringList sharedColorSchemePaths();
static QString resolvePath(const QString &path);
static bool isKColorScheme(const QString &filePath);
static QPalette loadColorScheme(const QString &filePath, const QPalette &fallback);

static void registerStyleInstance(StyleInstance *instance);
Expand Down
23 changes: 23 additions & 0 deletions src/qt6ct-qtplugin/qt6ctplatformtheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ Qt6CTPlatformTheme::Qt6CTPlatformTheme()
QMetaObject::invokeMethod(this, "createFSWatcher", Qt::QueuedConnection);
#endif
QGuiApplication::setFont(m_generalFont);
//must be applied before Q_COREAPP_STARTUP_FUNCTION execution
if(Qt6CT::isKColorScheme(m_schemePath))
qApp->setProperty("KDE_COLOR_SCHEME_PATH", m_schemePath);
#if defined QT_WIDGETS_LIB && defined QT_QUICKCONTROLS2_LIB
if(hasWidgets())
//don't override the value explicitly set by the user
Expand All @@ -82,6 +85,7 @@ Qt6CTPlatformTheme::Qt6CTPlatformTheme()
if(!QStyleFactory::keys().contains("qt6ct-style"))
qCCritical(lqt6ct) << "unable to find qt6ct proxy style";
#endif
QCoreApplication::instance()->installEventFilter(this);
}

Qt6CTPlatformTheme::~Qt6CTPlatformTheme()
Expand Down Expand Up @@ -178,6 +182,9 @@ void Qt6CTPlatformTheme::applySettings()

QGuiApplication::setFont(m_generalFont); //apply font

if(Qt6CT::isKColorScheme(m_schemePath))
qApp->setProperty("KDE_COLOR_SCHEME_PATH", m_schemePath);

#ifdef QT_WIDGETS_LIB
if(hasWidgets())
{
Expand Down Expand Up @@ -259,6 +266,7 @@ void Qt6CTPlatformTheme::updateSettings()

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

QSettings settings(Qt6CT::configFile(), QSettings::IniFormat);
Expand All @@ -269,6 +277,7 @@ void Qt6CTPlatformTheme::readSettings()
if(!schemePath.isEmpty() && settings.value("custom_palette", false).toBool())
{
schemePath = Qt6CT::resolvePath(schemePath); //replace environment variables
m_schemePath = schemePath;
m_palette = std::make_unique<QPalette>(Qt6CT::loadColorScheme(schemePath, *QPlatformTheme::palette(SystemPalette)));
}
m_iconTheme = settings.value("icon_theme").toString();
Expand Down Expand Up @@ -378,3 +387,17 @@ QString Qt6CTPlatformTheme::loadStyleSheets(const QStringList &paths)
content.replace(regExp, "\n");
return content;
}

//There's such a thing as KColorSchemeManager that lets the user to change the color scheme
//application-wide and we should re-apply the color scheme if KCSM resets it to the default
//which leads KColorScheme to get the color scheme from kdeglobals which won't help us.
bool Qt6CTPlatformTheme::eventFilter(QObject *obj, QEvent *e)
{
if(obj == qApp &&
e->type() == QEvent::DynamicPropertyChange &&
static_cast<QDynamicPropertyChangeEvent*>(e)->propertyName() == "KDE_COLOR_SCHEME_PATH" &&
qApp->property("KDE_COLOR_SCHEME_PATH").toString().isEmpty() &&
Qt6CT::isKColorScheme(m_schemePath))
applySettings();
return QObject::eventFilter(obj, e);
}
5 changes: 4 additions & 1 deletion src/qt6ct-qtplugin/qt6ctplatformtheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class Qt6CTPlatformTheme : public QObject, public QGenericUnixTheme
//virtual QList<QKeySequence> keyBindings(QKeySequence::StandardKey key) const;
//virtual QString standardButtonText(int button) const;

protected:
bool eventFilter(QObject *obj, QEvent *e) override;

private slots:
void applySettings();
#ifdef QT_WIDGETS_LIB
Expand All @@ -79,7 +82,7 @@ private slots:
bool hasWidgets();
#endif
QString loadStyleSheets(const QStringList &paths);
QString m_style, m_iconTheme, m_userStyleSheet, m_prevStyleSheet;
QString m_style, m_schemePath, m_iconTheme, m_userStyleSheet, m_prevStyleSheet;
std::unique_ptr<QPalette> m_palette;
QFont m_generalFont, m_fixedFont;
int m_doubleClickInterval;
Expand Down
2 changes: 1 addition & 1 deletion src/qt6ct/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ endif()

add_executable(qt6ct ${app_SRCS})
target_link_libraries(qt6ct PRIVATE Qt6::Widgets Qt6::WidgetsPrivate $<TARGET_NAME_IF_EXISTS:KF6::ConfigCore> qt6ct-common)
target_compile_definitions(qt6ct PRIVATE $<$<TARGET_EXISTS:KF6::ConfigCore>:KF_CONFIGCORE_LIB>)
target_compile_definitions(qt6ct PRIVATE $<$<TARGET_EXISTS:KF6::ConfigCore>:KF_CONFIGCORE_LIB> $<$<TARGET_EXISTS:KF6::ColorScheme>:KF_COLORSCHEME_LIB>)
install(TARGETS qt6ct DESTINATION ${CMAKE_INSTALL_BINDIR})
install(FILES qt6ct.desktop DESTINATION ${CMAKE_INSTALL_DATADIR}/applications)
22 changes: 19 additions & 3 deletions src/qt6ct/appearancepage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ AppearancePage::AppearancePage(QWidget *parent) :
QMenu *menu = new QMenu(this);
menu->addAction(QIcon::fromTheme("document-new"), tr("Create"), this, SLOT(createColorScheme()));
m_changeColorSchemeAction = menu->addAction(QIcon::fromTheme("accessories-text-editor"), tr("Edit"), this, SLOT(changeColorScheme()));
menu->addAction(QIcon::fromTheme("edit-copy"), tr("Create a Copy"), this, SLOT(copyColorScheme()));
m_copyColorSchemeAction = menu->addAction(QIcon::fromTheme("edit-copy"), tr("Create a Copy"), this, SLOT(copyColorScheme()));
m_renameColorSchemeAction = menu->addAction(tr("Rename"), this, SLOT(renameColorScheme()));
menu->addSeparator();
m_removeColorSchemeAction = menu->addAction(QIcon::fromTheme("edit-delete"), tr("Remove"), this, SLOT(removeColorScheme()));
Expand Down Expand Up @@ -315,6 +315,7 @@ void AppearancePage::setPreviewPalette(const QPalette &p)

void AppearancePage::updateActions()
{
m_copyColorSchemeAction->setVisible(!Qt6CT::isKColorScheme(m_ui->colorSchemeComboBox->currentData().toString()));
if(m_ui->colorSchemeComboBox->count() == 0 ||
!QFileInfo(m_ui->colorSchemeComboBox->currentData().toString()).isWritable())
{
Expand Down Expand Up @@ -393,11 +394,26 @@ void AppearancePage::findColorSchemes(const QString &path)
{
QDir dir(path);
dir.setFilter(QDir::Files);
dir.setNameFilters(QStringList() << "*.conf");
QStringList nameFilters;
nameFilters << "*.conf";
#if defined KF_CONFIGCORE_LIB && defined KF_COLORSCHEME_LIB
nameFilters << "*.colors";
#endif
dir.setNameFilters(nameFilters);

for(const QFileInfo &info : dir.entryInfoList())
{
m_ui->colorSchemeComboBox->addItem(info.baseName(), info.filePath());
QString name = info.baseName();
QString path = info.filePath();
#if defined KF_CONFIGCORE_LIB && defined KF_COLORSCHEME_LIB
if(Qt6CT::isKColorScheme(path))
{
KSharedConfigPtr config = KSharedConfig::openConfig(path, KConfig::SimpleConfig);
KConfigGroup group(config, "General");
name = group.readEntry("Name", name) + " (KColorScheme)";
}
#endif
m_ui->colorSchemeComboBox->addItem(name, path);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/qt6ct/appearancepage.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private slots:
QStyle *m_selectedStyle = nullptr;
QPalette m_customPalette;
QWidget *m_previewWidget;
QAction *m_changeColorSchemeAction, *m_renameColorSchemeAction, *m_removeColorSchemeAction;
QAction *m_changeColorSchemeAction, *m_copyColorSchemeAction, *m_renameColorSchemeAction, *m_removeColorSchemeAction;
Ui::PreviewForm *m_previewUi;
};

Expand Down

0 comments on commit 7aaa013

Please sign in to comment.