From 0ff9f12157a8223de3d324ed1e4e82c5166df624 Mon Sep 17 00:00:00 2001 From: blue Date: Wed, 26 Jan 2022 23:53:44 +0300 Subject: [PATCH] new optional KDE Frameworks plugin to support system color schemes --- CMakeLists.txt | 19 +++++++++++++ core/main.cpp | 5 +++- plugins/CMakeLists.txt | 6 ++++ plugins/colorschemetools.cpp | 39 ++++++++++++++++++++++++++ shared/global.cpp | 37 +++++++++++++++++++++++- shared/global.h | 11 ++++++++ ui/widgets/settings/pageappearance.cpp | 21 ++++++++------ ui/widgets/settings/pageappearance.h | 2 ++ 8 files changed, 130 insertions(+), 10 deletions(-) create mode 100644 plugins/colorschemetools.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index cd9d793..717cf91 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,7 @@ target_include_directories(squawk PRIVATE ${CMAKE_SOURCE_DIR}) option(SYSTEM_QXMPP "Use system qxmpp lib" ON) option(WITH_KWALLET "Build KWallet support module" ON) option(WITH_KIO "Build KIO support module" ON) +option(WITH_KCONFIG "Build KConfig support module" ON) # Dependencies ## Qt @@ -90,6 +91,24 @@ if (WITH_KWALLET) endif () endif () +if (WITH_KCONFIG) + find_package(KF5Config CONFIG) + if (NOT KF5Config_FOUND) + set(WITH_KCONFIG OFF) + message("KConfig package wasn't found, KConfig support modules wouldn't be built") + else() + find_package(KF5ConfigWidgets CONFIG) + if (NOT KF5ConfigWidgets_FOUND) + set(WITH_KCONFIG OFF) + message("KConfigWidgets package wasn't found, KConfigWidgets support modules wouldn't be built") + else() + target_compile_definitions(squawk PRIVATE WITH_KCONFIG) + message("Building with support of KConfig") + message("Building with support of KConfigWidgets") + endif() + endif() +endif() + ## Signal (TODO) # find_package(Signal REQUIRED) diff --git a/core/main.cpp b/core/main.cpp index c7bb820..03de307 100644 --- a/core/main.cpp +++ b/core/main.cpp @@ -176,7 +176,10 @@ int main(int argc, char *argv[]) int result = app.exec(); if (coreThread->isRunning()) { - coreThread->wait(); + //coreThread->wait(); + //todo if I uncomment that, the app will no quit if it has reconnected at least once + //it feels like a symptom of something badly desinged in the core coreThread + //need to investigate; } return result; diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 84fc09b..5a5a41d 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -2,3 +2,9 @@ if (WITH_KIO) add_library(openFileManagerWindowJob SHARED openfilemanagerwindowjob.cpp) target_link_libraries(openFileManagerWindowJob PRIVATE KF5::KIOWidgets) endif () + +if (WITH_KCONFIG) + add_library(colorSchemeTools SHARED colorschemetools.cpp) + target_link_libraries(colorSchemeTools PRIVATE KF5::ConfigCore) + target_link_libraries(colorSchemeTools PRIVATE KF5::ConfigWidgets) +endif() diff --git a/plugins/colorschemetools.cpp b/plugins/colorschemetools.cpp new file mode 100644 index 0000000..0ad8e24 --- /dev/null +++ b/plugins/colorschemetools.cpp @@ -0,0 +1,39 @@ +#include +#include + +#include +#include + +QPixmap createPixmap(int size, const QBrush& window, const QBrush& button, const QBrush& view, const QBrush& selection); + +extern "C" QIcon* createPreview(const QString& path) { + KSharedConfigPtr schemeConfig = KSharedConfig::openConfig(path); + QIcon* result = new QIcon(); + KColorScheme activeWindow(QPalette::Active, KColorScheme::Window, schemeConfig); + KColorScheme activeButton(QPalette::Active, KColorScheme::Button, schemeConfig); + KColorScheme activeView(QPalette::Active, KColorScheme::View, schemeConfig); + KColorScheme activeSelection(QPalette::Active, KColorScheme::Selection, schemeConfig); + + result->addPixmap(createPixmap(16, activeWindow.background(), activeButton.background(), activeView.background(), activeSelection.background())); + result->addPixmap(createPixmap(24, activeWindow.background(), activeButton.background(), activeView.background(), activeSelection.background())); + + return result; +} + +extern "C" void deletePreview(QIcon* icon) { + delete icon; +} + +QPixmap createPixmap(int size, const QBrush& window, const QBrush& button, const QBrush& view, const QBrush& selection) { + QPixmap pix(size, size); + pix.fill(Qt::black); + QPainter p; + p.begin(&pix); + const int itemSize = size / 2 - 1; + p.fillRect(1, 1, itemSize, itemSize, window); + p.fillRect(1 + itemSize, 1, itemSize, itemSize, button); + p.fillRect(1, 1 + itemSize, itemSize, itemSize, view); + p.fillRect(1 + itemSize, 1 + itemSize, itemSize, itemSize, selection); + p.end(); + return pix; +} diff --git a/shared/global.cpp b/shared/global.cpp index 7ee9599..7dafed6 100644 --- a/shared/global.cpp +++ b/shared/global.cpp @@ -28,6 +28,12 @@ QLibrary Shared::Global::openFileManagerWindowJob("openFileManagerWindowJob"); Shared::Global::HighlightInFileManager Shared::Global::hfm = 0; #endif +#ifdef WITH_KCONFIG +QLibrary Shared::Global::colorSchemeTools("colorSchemeTools"); +Shared::Global::CreatePreview Shared::Global::createPreview = 0; +Shared::Global::DeletePreview Shared::Global::deletePreview = 0; +#endif + Shared::Global::Global(): availability({ tr("Online", "Availability"), @@ -87,7 +93,8 @@ Shared::Global::Global(): defaultSystemStyle(QApplication::style()->objectName()), pluginSupport({ {"KWallet", false}, - {"openFileManagerWindowJob", false} + {"openFileManagerWindowJob", false}, + {"colorSchemeTools", false} }), fileCache() { @@ -111,6 +118,22 @@ Shared::Global::Global(): qDebug() << "KIO::OpenFileManagerWindow support disabled: couldn't load the library" << openFileManagerWindowJob.errorString(); } #endif + +#ifdef WITH_KCONFIG + colorSchemeTools.load(); + if (colorSchemeTools.isLoaded()) { + createPreview = (CreatePreview) colorSchemeTools.resolve("createPreview"); + deletePreview = (DeletePreview) colorSchemeTools.resolve("deletePreview"); + if (createPreview && deletePreview) { + setSupported("colorSchemeTools", true); + qDebug() << "Color Schemes support enabled"; + } else { + qDebug() << "Color Schemes support disabled: couldn't resolve required methods in the library"; + } + } else { + qDebug() << "Color Schemes support disabled: couldn't load the library" << colorSchemeTools.errorString(); + } +#endif } @@ -276,6 +299,18 @@ void Shared::Global::highlightInFileManager(const QString& path) } } +QIcon Shared::Global::createThemePreview(const QString& path) +{ + if (supported("colorSchemeTools")) { + QIcon* icon = createPreview(path); + QIcon localIcon = *icon; + deletePreview(icon); + return localIcon; + } else { + return QIcon(); + } +} + #define FROM_INT_INPL(Enum) \ template<> \ diff --git a/shared/global.h b/shared/global.h index a87b9bc..bd3c9a6 100644 --- a/shared/global.h +++ b/shared/global.h @@ -95,6 +95,7 @@ namespace Shared { static FileInfo getFileInfo(const QString& path); static void highlightInFileManager(const QString& path); + static QIcon createThemePreview(const QString& path); template static T fromInt(int src); @@ -128,6 +129,16 @@ namespace Shared { static HighlightInFileManager hfm; #endif + +#ifdef WITH_KCONFIG + static QLibrary colorSchemeTools; + + typedef QIcon* (*CreatePreview)(const QString&); + typedef void (*DeletePreview)(QIcon*); + + static CreatePreview createPreview; + static DeletePreview deletePreview; +#endif }; } diff --git a/ui/widgets/settings/pageappearance.cpp b/ui/widgets/settings/pageappearance.cpp index 1c558f2..57da7aa 100644 --- a/ui/widgets/settings/pageappearance.cpp +++ b/ui/widgets/settings/pageappearance.cpp @@ -33,16 +33,19 @@ PageAppearance::PageAppearance(QWidget* parent): connect(m_ui->themeInput, qOverload(&QComboBox::currentIndexChanged), this, &PageAppearance::onThemeChanged); - m_ui->colorInput->addItem(tr("System")); - const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, "color-schemes", QStandardPaths::LocateDirectory); - QStringList schemeFiles; - for (const QString &dir : dirs) { - const QStringList fileNames = QDir(dir).entryList(filters); - for (const QString &file : fileNames) { - m_ui->colorInput->addItem(dir + QDir::separator() + file); + if (Shared::Global::supported("colorSchemeTools")) { + m_ui->colorInput->addItem(tr("System")); + const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, "color-schemes", QStandardPaths::LocateDirectory); + QStringList schemeFiles; + for (const QString &dir : dirs) { + const QStringList fileNames = QDir(dir).entryList(filters); + for (const QString &file : fileNames) { + m_ui->colorInput->addItem(Shared::Global::createThemePreview(dir + QDir::separator() + file), file); + } } + } else { + m_ui->colorInput->setEnabled(false); } - } PageAppearance::~PageAppearance() @@ -55,3 +58,5 @@ void PageAppearance::onThemeChanged(int index) emit variableModified("theme", styles[index]); } } + + diff --git a/ui/widgets/settings/pageappearance.h b/ui/widgets/settings/pageappearance.h index 9cb1830..b31f3c1 100644 --- a/ui/widgets/settings/pageappearance.h +++ b/ui/widgets/settings/pageappearance.h @@ -8,6 +8,8 @@ #include #include +#include "shared/global.h" + namespace Ui { class PageAppearance;