| 1 |
From d8834e291bdc8613156d5883b5b3975fce9b4372 Mon Sep 17 00:00:00 2001 |
| 2 |
From: Guido Berhoerster <[email protected]> |
| 3 |
Date: Thu, 27 Oct 2022 11:55:24 +0200 |
| 4 |
Subject: [PATCH 1/5] Expose ColorSchemeManager and ColorScheme to QML |
| 5 |
|
| 6 |
Expose a subset of the ColorSchemeManager to QML in order to allow editing of |
| 7 |
ColorSchemes from within QML. |
| 8 |
|
| 9 |
Signed-off-by: Mike Gabriel <[email protected]> |
| 10 |
--- |
| 11 |
lib/ColorScheme.cpp | 72 ++++++++++++++++++++++-------------- |
| 12 |
lib/ColorScheme.h | 26 +++++++++---- |
| 13 |
src/qmltermwidget_plugin.cpp | 11 ++++++ |
| 14 |
3 files changed, 74 insertions(+), 35 deletions(-) |
| 15 |
|
| 16 |
diff --git a/lib/ColorScheme.cpp b/lib/ColorScheme.cpp |
| 17 |
index f7b9af2..971a561 100644 |
| 18 |
--- a/lib/ColorScheme.cpp |
| 19 |
+++ b/lib/ColorScheme.cpp |
| 20 |
@@ -120,35 +120,12 @@ const char* const ColorScheme::translatedColorNames[TABLE_COLORS] = |
| 21 |
tr_NOOP("Color 8 (Intense)") |
| 22 |
}; |
| 23 |
|
| 24 |
-ColorScheme::ColorScheme() |
| 25 |
+ColorScheme::ColorScheme(QObject *parent) : QObject(parent) |
| 26 |
{ |
| 27 |
_table = nullptr; |
| 28 |
_randomTable = nullptr; |
| 29 |
_opacity = 1.0; |
| 30 |
} |
| 31 |
-ColorScheme::ColorScheme(const ColorScheme& other) |
| 32 |
- : _opacity(other._opacity) |
| 33 |
- ,_table(nullptr) |
| 34 |
- ,_randomTable(nullptr) |
| 35 |
-{ |
| 36 |
- setName(other.name()); |
| 37 |
- setDescription(other.description()); |
| 38 |
- |
| 39 |
- if ( other._table != nullptr ) |
| 40 |
- { |
| 41 |
- for ( int i = 0 ; i < TABLE_COLORS ; i++ ) |
| 42 |
- setColorTableEntry(i,other._table[i]); |
| 43 |
- } |
| 44 |
- |
| 45 |
- if ( other._randomTable != nullptr ) |
| 46 |
- { |
| 47 |
- for ( int i = 0 ; i < TABLE_COLORS ; i++ ) |
| 48 |
- { |
| 49 |
- const RandomizationRange& range = other._randomTable[i]; |
| 50 |
- setRandomizationRange(i,range.hue,range.saturation,range.value); |
| 51 |
- } |
| 52 |
- } |
| 53 |
-} |
| 54 |
ColorScheme::~ColorScheme() |
| 55 |
{ |
| 56 |
delete[] _table; |
| 57 |
@@ -206,6 +183,19 @@ ColorEntry ColorScheme::colorEntry(int index , uint randomSeed) const |
| 58 |
|
| 59 |
return entry; |
| 60 |
} |
| 61 |
+QColor ColorScheme::getColor(int index) const |
| 62 |
+{ |
| 63 |
+ return colorEntry(index).color; |
| 64 |
+} |
| 65 |
+void ColorScheme::setColor(int index, QColor color) |
| 66 |
+{ |
| 67 |
+ ColorEntry colorEntry = ColorScheme::colorEntry(index); |
| 68 |
+ if (colorEntry.color != color) { |
| 69 |
+ colorEntry.color = color; |
| 70 |
+ setColorTableEntry(index, colorEntry); |
| 71 |
+ Q_EMIT colorChanged(index); |
| 72 |
+ } |
| 73 |
+} |
| 74 |
void ColorScheme::getColorTable(ColorEntry* table , uint randomSeed) const |
| 75 |
{ |
| 76 |
for ( int i = 0 ; i < TABLE_COLORS ; i++ ) |
| 77 |
@@ -267,7 +257,7 @@ bool ColorScheme::hasDarkBackground() const |
| 78 |
// so 127 is in the middle, anything less is deemed 'dark' |
| 79 |
return backgroundColor().value() < 127; |
| 80 |
} |
| 81 |
-void ColorScheme::setOpacity(qreal opacity) { _opacity = opacity; } |
| 82 |
+void ColorScheme::setOpacity(qreal opacity) { _opacity = opacity; opacityChanged(); } |
| 83 |
qreal ColorScheme::opacity() const { return _opacity; } |
| 84 |
|
| 85 |
void ColorScheme::read(const QString & fileName) |
| 86 |
@@ -284,6 +274,21 @@ void ColorScheme::read(const QString & fileName) |
| 87 |
readColorEntry(&s, i); |
| 88 |
} |
| 89 |
} |
| 90 |
+ |
| 91 |
+void ColorScheme::write(const QString & fileName) const |
| 92 |
+{ |
| 93 |
+ QSettings s(fileName, QSettings::IniFormat); |
| 94 |
+ s.beginGroup("General"); |
| 95 |
+ |
| 96 |
+ s.setValue("Description", _description); |
| 97 |
+ s.setValue("Opacity", _opacity); |
| 98 |
+ s.endGroup(); |
| 99 |
+ |
| 100 |
+ for (int i=0 ; i < TABLE_COLORS ; i++) |
| 101 |
+ { |
| 102 |
+ writeColorEntry(&s, i, colorTable()[i]); |
| 103 |
+ } |
| 104 |
+} |
| 105 |
#if 0 |
| 106 |
// implemented upstream - user apps |
| 107 |
void ColorScheme::read(KConfig& config) |
| 108 |
@@ -403,6 +408,19 @@ void ColorScheme::readColorEntry(QSettings * s , int index) |
| 109 |
|
| 110 |
s->endGroup(); |
| 111 |
} |
| 112 |
+void ColorScheme::writeColorEntry(QSettings * s, int index, const ColorEntry& entry) const |
| 113 |
+{ |
| 114 |
+ s->beginGroup(colorNameForIndex(index)); |
| 115 |
+ QStringList colorList; |
| 116 |
+ colorList << QString::number(entry.color.red()) << QString::number(entry.color.green()) << QString::number(entry.color.blue()); |
| 117 |
+ s->setValue("Color", colorList); |
| 118 |
+ s->setValue("Transparency", (bool)entry.transparent); |
| 119 |
+ if (entry.fontWeight != ColorEntry::UseCurrentFormat) |
| 120 |
+ { |
| 121 |
+ s->setValue("Bold", entry.fontWeight == ColorEntry::Bold); |
| 122 |
+ } |
| 123 |
+ s->endGroup(); |
| 124 |
+} |
| 125 |
#if 0 |
| 126 |
// implemented upstream - user apps |
| 127 |
void ColorScheme::writeColorEntry(KConfig& config , const QString& colorName, const ColorEntry& entry , const RandomizationRange& random) const |
| 128 |
@@ -566,7 +584,7 @@ bool ColorSchemeManager::loadColorScheme(const QString& filePath) |
| 129 |
|
| 130 |
const QString& schemeName = info.baseName(); |
| 131 |
|
| 132 |
- ColorScheme* scheme = new ColorScheme(); |
| 133 |
+ ColorScheme* scheme = new ColorScheme(this); |
| 134 |
scheme->setName(schemeName); |
| 135 |
scheme->read(filePath); |
| 136 |
|
| 137 |
@@ -649,7 +667,7 @@ QString ColorSchemeManager::findColorSchemePath(const QString& name) const |
| 138 |
|
| 139 |
return path; |
| 140 |
} |
| 141 |
-const ColorScheme* ColorSchemeManager::findColorScheme(const QString& name) |
| 142 |
+const Konsole::ColorScheme* ColorSchemeManager::findColorScheme(const QString& name) |
| 143 |
{ |
| 144 |
if ( name.isEmpty() ) |
| 145 |
return defaultColorScheme(); |
| 146 |
diff --git a/lib/ColorScheme.h b/lib/ColorScheme.h |
| 147 |
index 416fe25..04359e2 100644 |
| 148 |
--- a/lib/ColorScheme.h |
| 149 |
+++ b/lib/ColorScheme.h |
| 150 |
@@ -45,15 +45,16 @@ namespace Konsole |
| 151 |
* The color scheme includes the palette of colors used to draw the text and character backgrounds |
| 152 |
* in the display and the opacity level of the display background. |
| 153 |
*/ |
| 154 |
-class ColorScheme |
| 155 |
+class ColorScheme : public QObject |
| 156 |
{ |
| 157 |
+ Q_OBJECT |
| 158 |
+ |
| 159 |
public: |
| 160 |
/** |
| 161 |
* Constructs a new color scheme which is initialised to the default color set |
| 162 |
* for Konsole. |
| 163 |
*/ |
| 164 |
- ColorScheme(); |
| 165 |
- ColorScheme(const ColorScheme& other); |
| 166 |
+ ColorScheme(QObject *parent = nullptr); |
| 167 |
~ColorScheme(); |
| 168 |
|
| 169 |
/** Sets the descriptive name of the color scheme. */ |
| 170 |
@@ -74,6 +75,7 @@ public: |
| 171 |
void write(KConfig& config) const; |
| 172 |
#endif |
| 173 |
void read(const QString & filename); |
| 174 |
+ Q_INVOKABLE void write(const QString & filename) const; |
| 175 |
|
| 176 |
/** Sets a single entry within the color palette. */ |
| 177 |
void setColorTableEntry(int index , const ColorEntry& entry); |
| 178 |
@@ -96,6 +98,10 @@ public: |
| 179 |
*/ |
| 180 |
ColorEntry colorEntry(int index , uint randomSeed = 0) const; |
| 181 |
|
| 182 |
+ Q_INVOKABLE QColor getColor(int index) const; |
| 183 |
+ Q_INVOKABLE void setColor(int index, QColor color); |
| 184 |
+ Q_SIGNAL void colorChanged(int index); |
| 185 |
+ |
| 186 |
/** |
| 187 |
* Convenience method. Returns the |
| 188 |
* foreground color for this scheme, |
| 189 |
@@ -126,12 +132,13 @@ public: |
| 190 |
* |
| 191 |
* TODO: More documentation |
| 192 |
*/ |
| 193 |
- void setOpacity(qreal opacity); |
| 194 |
+ Q_INVOKABLE void setOpacity(qreal opacity); |
| 195 |
/** |
| 196 |
* Returns the opacity level for this color scheme, see setOpacity() |
| 197 |
* TODO: More documentation |
| 198 |
*/ |
| 199 |
- qreal opacity() const; |
| 200 |
+ Q_INVOKABLE qreal opacity() const; |
| 201 |
+ Q_SIGNAL void opacityChanged(); |
| 202 |
|
| 203 |
/** |
| 204 |
* Enables randomization of the background color. This will cause |
| 205 |
@@ -177,6 +184,7 @@ private: |
| 206 |
void writeColorEntry(KConfig& config , const QString& colorName, const ColorEntry& entry,const RandomizationRange& range) const; |
| 207 |
#endif |
| 208 |
void readColorEntry(QSettings *s, int index); |
| 209 |
+ void writeColorEntry(QSettings *s, int index, const ColorEntry& entry) const; |
| 210 |
|
| 211 |
// sets the amount of randomization allowed for a particular color |
| 212 |
// in the palette. creates the randomization table if |
| 213 |
@@ -221,8 +229,10 @@ public: |
| 214 |
* Manages the color schemes available for use by terminal displays. |
| 215 |
* See ColorScheme |
| 216 |
*/ |
| 217 |
-class ColorSchemeManager |
| 218 |
+class ColorSchemeManager : public QObject |
| 219 |
{ |
| 220 |
+ Q_OBJECT |
| 221 |
+ |
| 222 |
public: |
| 223 |
|
| 224 |
/** |
| 225 |
@@ -251,7 +261,7 @@ public: |
| 226 |
* The first time that a color scheme with a particular name is |
| 227 |
* requested, the configuration information is loaded from disk. |
| 228 |
*/ |
| 229 |
- const ColorScheme* findColorScheme(const QString& name); |
| 230 |
+ Q_INVOKABLE const Konsole::ColorScheme* findColorScheme(const QString& name); |
| 231 |
|
| 232 |
#if 0 |
| 233 |
/** |
| 234 |
@@ -291,7 +301,7 @@ public: |
| 235 |
* @param[in] path The path to KDE 4 .colorscheme or KDE 3 .schema. |
| 236 |
* @return Whether the color scheme is loaded successfully. |
| 237 |
*/ |
| 238 |
- bool loadCustomColorScheme(const QString& path); |
| 239 |
+ Q_INVOKABLE bool loadCustomColorScheme(const QString& path); |
| 240 |
|
| 241 |
/** |
| 242 |
* @brief Allows to add a custom location of color schemes. |
| 243 |
diff --git a/src/qmltermwidget_plugin.cpp b/src/qmltermwidget_plugin.cpp |
| 244 |
index 656b115..e9e8b4f 100644 |
| 245 |
--- a/src/qmltermwidget_plugin.cpp |
| 246 |
+++ b/src/qmltermwidget_plugin.cpp |
| 247 |
@@ -1,5 +1,6 @@ |
| 248 |
#include "qmltermwidget_plugin.h" |
| 249 |
|
| 250 |
+#include "ColorScheme.h" |
| 251 |
#include "TerminalDisplay.h" |
| 252 |
#include "ksession.h" |
| 253 |
|
| 254 |
@@ -9,11 +10,21 @@ |
| 255 |
|
| 256 |
using namespace Konsole; |
| 257 |
|
| 258 |
+static QObject *colorschememanager_provider(QQmlEngine *engine, QJSEngine *scriptEngine) |
| 259 |
+{ |
| 260 |
+ Q_UNUSED(engine) |
| 261 |
+ Q_UNUSED(scriptEngine) |
| 262 |
+ |
| 263 |
+ return ColorSchemeManager::instance(); |
| 264 |
+} |
| 265 |
+ |
| 266 |
void QmltermwidgetPlugin::registerTypes(const char *uri) |
| 267 |
{ |
| 268 |
// @uri org.qterminal.qmlterminal |
| 269 |
qmlRegisterType<TerminalDisplay>(uri, 1, 0, "QMLTermWidget"); |
| 270 |
qmlRegisterType<KSession>(uri, 1, 0, "QMLTermSession"); |
| 271 |
+ qmlRegisterUncreatableType<const Konsole::ColorScheme>(uri, 1, 0, "ColorScheme", QStringLiteral("Not instantiatable")); |
| 272 |
+ qmlRegisterSingletonType<ColorSchemeManager>(uri, 1, 0, "ColorSchemeManager", colorschememanager_provider); |
| 273 |
} |
| 274 |
|
| 275 |
void QmltermwidgetPlugin::initializeEngine(QQmlEngine *engine, const char *uri) |
| 276 |
-- |
| 277 |
2.30.2 |
| 278 |
|