[education/rkward] rkward: Add a shared instance of KColorScheme to RKStyle
Thomas Friedrichsmeier
null at kde.org
Thu Apr 14 22:14:06 BST 2022
Git commit 45d4a043492c889848420b11a88e808fc35e649b by Thomas Friedrichsmeier.
Committed on 14/04/2022 at 07:11.
Pushed by tfry into branch 'master'.
Add a shared instance of KColorScheme to RKStyle
M +4 -4 rkward/misc/rkprogresscontrol.cpp
M +21 -0 rkward/misc/rkstyle.cpp
M +9 -2 rkward/misc/rkstyle.h
M +4 -4 rkward/rkward.cpp
M +3 -2 rkward/windows/rkhtmlwindow.cpp
https://invent.kde.org/education/rkward/commit/45d4a043492c889848420b11a88e808fc35e649b
diff --git a/rkward/misc/rkprogresscontrol.cpp b/rkward/misc/rkprogresscontrol.cpp
index f12d33d2..f3eba6cd 100644
--- a/rkward/misc/rkprogresscontrol.cpp
+++ b/rkward/misc/rkprogresscontrol.cpp
@@ -21,6 +21,7 @@ SPDX-License-Identifier: GPL-2.0-or-later
#include "../rbackend/rkrinterface.h"
#include "../settings/rksettingsmoduler.h"
#include "../misc/rkcommonfunctions.h"
+#include "../misc/rkstyle.h"
#include "../debug.h"
@@ -52,7 +53,6 @@ private:
ROutput::ROutputType last_output_type;
bool prevent_close;
bool is_done;
- KColorScheme color_scheme;
};
@@ -227,7 +227,7 @@ QString RKProgressControl::fullCommandOutput() {
#include <kstandardguiitem.h>
-RKProgressControlDialog::RKProgressControlDialog(const QString &text, const QString &caption, int mode_flags, bool modal) : QDialog(nullptr), color_scheme(QPalette::Normal) {
+RKProgressControlDialog::RKProgressControlDialog(const QString &text, const QString &caption, int mode_flags, bool modal) : QDialog(nullptr) {
RK_TRACE (MISC);
setAttribute (Qt::WA_DeleteOnClose, true);
@@ -314,9 +314,9 @@ void RKProgressControlDialog::addOutput (const ROutput *output) {
output_text->insertPlainText ("\n");
if (output->type == ROutput::Output) {
- output_text->setTextColor(color_scheme.foreground(KColorScheme::NormalText).color());
+ output_text->setTextColor(RKStyle::viewScheme()->foreground(KColorScheme::NormalText).color());
} else {
- output_text->setTextColor(color_scheme.foreground(KColorScheme::NegativeText).color());
+ output_text->setTextColor(RKStyle::viewScheme()->foreground(KColorScheme::NegativeText).color());
if (!detailsbox->isVisible ()) toggleDetails ();
error_indicator->show ();
}
diff --git a/rkward/misc/rkstyle.cpp b/rkward/misc/rkstyle.cpp
index ea66405d..a9b4a073 100644
--- a/rkward/misc/rkstyle.cpp
+++ b/rkward/misc/rkstyle.cpp
@@ -9,6 +9,12 @@ SPDX-License-Identifier: GPL-2.0-or-later
#include <QApplication>
#include <QStyle>
+#include <KColorScheme>
+
+#include "../debug.h"
+
+KColorScheme* RKStyle::_view_scheme = nullptr;
+
int RKStyle::marginHint() {
return QApplication::style()->pixelMetric(QStyle::PM_DefaultChildMargin);
}
@@ -16,3 +22,18 @@ int RKStyle::marginHint() {
int RKStyle::spacingHint() {
return QApplication::style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing);
}
+
+KColorScheme* RKStyle::viewScheme() {
+ if (!_view_scheme) {
+ RK_TRACE(MISC);
+ _view_scheme = new KColorScheme(QPalette::Normal);
+ QObject::connect(qApp, &QGuiApplication::paletteChanged, cleanResources); // will be re-initialized when needed, again; NOTE: Not emitted before Qt 5.13.0, so quirky, before
+ }
+ return _view_scheme;
+}
+
+void RKStyle::cleanResources() {
+ RK_TRACE(MISC);
+ delete _view_scheme;
+ _view_scheme = nullptr;
+}
diff --git a/rkward/misc/rkstyle.h b/rkward/misc/rkstyle.h
index 63c8a95c..f2ac381e 100644
--- a/rkward/misc/rkstyle.h
+++ b/rkward/misc/rkstyle.h
@@ -7,6 +7,8 @@ SPDX-License-Identifier: GPL-2.0-or-later
#ifndef RKSTYLE_H
#define RKSTYLE_H
+class KColorScheme;
+
/**
Namespace to hold common styling options: Spacing and color scheme.
@@ -14,10 +16,15 @@ Namespace to hold common styling options: Spacing and color scheme.
*/
class RKStyle{
public:
-/// returns KDialog::marginHint (), without the need to include kdialog.h in all the sources
+/// @returns KDialog::marginHint (), without the need to include kdialog.h in all the sources
static int marginHint ();
-/// returns KDialog::spacingHint (), without the need to include kdialog.h in all the sources
+/// @returns KDialog::spacingHint (), without the need to include kdialog.h in all the sources
static int spacingHint ();
+/// @returns a cached instance of the color scheme for normal views. Particularly useful for setting specials colors in item models
+ static KColorScheme* viewScheme();
+ static void cleanResources();
+private:
+ static KColorScheme* _view_scheme;
};
#endif
diff --git a/rkward/rkward.cpp b/rkward/rkward.cpp
index f50d2c12..e42d9b22 100644
--- a/rkward/rkward.cpp
+++ b/rkward/rkward.cpp
@@ -55,7 +55,7 @@ SPDX-License-Identifier: GPL-2.0-or-later
#include "misc/rkxmlguisyncer.h"
#include "misc/rkdbusapi.h"
#include "misc/rkdialogbuttonbox.h"
-
+#include "misc/rkstyle.h"
#include "dialogs/startupdialog.h"
#include "dialogs/rkloadlibsdialog.h"
#include "dialogs/rkimportdialog.h"
@@ -187,6 +187,7 @@ RKWardMainWindow::~RKWardMainWindow() {
delete RControlWindow::getControl ();
factory ()->removeClient (RKComponentMap::getMap ());
delete RKComponentMap::getMap ();
+ RKStyle::cleanResources();
}
KatePluginIntegrationApp* RKWardMainWindow::katePluginIntegration () {
@@ -912,10 +913,9 @@ void RKWardMainWindow::slotDetachWindow () {
}
void setIndictatorColor(QWidget *widget, KColorScheme::ForegroundRole fg, KColorScheme::BackgroundRole bg) {
- KColorScheme color_scheme(QPalette::Normal); // default parameter in more recent versions of KColorScheme (->KF6: remove)
QPalette palette = widget->palette();
- palette.setBrush(widget->backgroundRole(), color_scheme.background(bg));
- palette.setBrush(widget->foregroundRole(), color_scheme.foreground(fg));
+ palette.setBrush(widget->backgroundRole(), RKStyle::viewScheme()->background(bg));
+ palette.setBrush(widget->foregroundRole(), RKStyle::viewScheme()->foreground(fg));
widget->setAutoFillBackground(true);
widget->setPalette(palette);
}
diff --git a/rkward/windows/rkhtmlwindow.cpp b/rkward/windows/rkhtmlwindow.cpp
index 988bf4bc..57cd4ba4 100644
--- a/rkward/windows/rkhtmlwindow.cpp
+++ b/rkward/windows/rkhtmlwindow.cpp
@@ -52,6 +52,7 @@ SPDX-License-Identifier: GPL-2.0-or-later
#include "../misc/rkmessagecatalog.h"
#include "../misc/rkfindbar.h"
#include "../misc/rkoutputdirectory.h"
+#include "../misc/rkstyle.h"
#include "../plugin/rkcomponentmap.h"
#include "../windows/rkworkplace.h"
#include "../windows/rkworkplaceview.h"
@@ -307,12 +308,12 @@ RKHTMLWindow::RKHTMLWindow (QWidget *parent, WindowMode mode) : RKMDIWindow (par
// Apply current color scheme to page. This needs support in the CSS of the page, so will only work for RKWard help and output pages.
// Note that the CSS in those pages also has "automatic" support for dark mode ("prefers-color-scheme: dark"; but see https://bugreports.qt.io/browse/QTBUG-89753), however,
// for a seamless appearance, the only option is to set the theme colors dynamically, via javascript.
- auto scheme = KColorScheme(QPalette::Normal);
+ auto scheme = RKStyle::viewScheme();
QString color_scheme_js = QStringLiteral("function rksetcolor(name, value) { document.querySelector(':root').style.setProperty(name, value); }\n"
"rksetcolor('--regular-text-color', '%1');\n"
"rksetcolor('--background-color', '%2');\n"
"rksetcolor('--header-color', '%3');\n"
- "rksetcolor('--anchor-color', '%4');").arg(scheme.foreground().color().name(), scheme.background().color().name(), scheme.foreground(KColorScheme::VisitedText).color().name(), scheme.foreground(KColorScheme::LinkText).color().name());
+ "rksetcolor('--anchor-color', '%4');").arg(scheme->foreground().color().name(), scheme->background().color().name(), scheme->foreground(KColorScheme::VisitedText).color().name(), scheme->foreground(KColorScheme::LinkText).color().name());
#ifdef NO_QT_WEBENGINE
connect(page, &RKWebPage::loadFinished, [this, color_scheme_js](){
page->mainFrame()->evaluateJavaScript(color_scheme_js);
More information about the rkward-tracker
mailing list