From d8834e291bdc8613156d5883b5b3975fce9b4372 Mon Sep 17 00:00:00 2001 From: Guido Berhoerster Date: Thu, 27 Oct 2022 11:55:24 +0200 Subject: [PATCH 1/5] Expose ColorSchemeManager and ColorScheme to QML Expose a subset of the ColorSchemeManager to QML in order to allow editing of ColorSchemes from within QML. Signed-off-by: Mike Gabriel --- lib/ColorScheme.cpp | 72 ++++++++++++++++++++++-------------- lib/ColorScheme.h | 26 +++++++++---- src/qmltermwidget_plugin.cpp | 11 ++++++ 3 files changed, 74 insertions(+), 35 deletions(-) diff --git a/lib/ColorScheme.cpp b/lib/ColorScheme.cpp index f7b9af2..971a561 100644 --- a/lib/ColorScheme.cpp +++ b/lib/ColorScheme.cpp @@ -120,35 +120,12 @@ const char* const ColorScheme::translatedColorNames[TABLE_COLORS] = tr_NOOP("Color 8 (Intense)") }; -ColorScheme::ColorScheme() +ColorScheme::ColorScheme(QObject *parent) : QObject(parent) { _table = nullptr; _randomTable = nullptr; _opacity = 1.0; } -ColorScheme::ColorScheme(const ColorScheme& other) - : _opacity(other._opacity) - ,_table(nullptr) - ,_randomTable(nullptr) -{ - setName(other.name()); - setDescription(other.description()); - - if ( other._table != nullptr ) - { - for ( int i = 0 ; i < TABLE_COLORS ; i++ ) - setColorTableEntry(i,other._table[i]); - } - - if ( other._randomTable != nullptr ) - { - for ( int i = 0 ; i < TABLE_COLORS ; i++ ) - { - const RandomizationRange& range = other._randomTable[i]; - setRandomizationRange(i,range.hue,range.saturation,range.value); - } - } -} ColorScheme::~ColorScheme() { delete[] _table; @@ -206,6 +183,19 @@ ColorEntry ColorScheme::colorEntry(int index , uint randomSeed) const return entry; } +QColor ColorScheme::getColor(int index) const +{ + return colorEntry(index).color; +} +void ColorScheme::setColor(int index, QColor color) +{ + ColorEntry colorEntry = ColorScheme::colorEntry(index); + if (colorEntry.color != color) { + colorEntry.color = color; + setColorTableEntry(index, colorEntry); + Q_EMIT colorChanged(index); + } +} void ColorScheme::getColorTable(ColorEntry* table , uint randomSeed) const { for ( int i = 0 ; i < TABLE_COLORS ; i++ ) @@ -267,7 +257,7 @@ bool ColorScheme::hasDarkBackground() const // so 127 is in the middle, anything less is deemed 'dark' return backgroundColor().value() < 127; } -void ColorScheme::setOpacity(qreal opacity) { _opacity = opacity; } +void ColorScheme::setOpacity(qreal opacity) { _opacity = opacity; opacityChanged(); } qreal ColorScheme::opacity() const { return _opacity; } void ColorScheme::read(const QString & fileName) @@ -284,6 +274,21 @@ void ColorScheme::read(const QString & fileName) readColorEntry(&s, i); } } + +void ColorScheme::write(const QString & fileName) const +{ + QSettings s(fileName, QSettings::IniFormat); + s.beginGroup("General"); + + s.setValue("Description", _description); + s.setValue("Opacity", _opacity); + s.endGroup(); + + for (int i=0 ; i < TABLE_COLORS ; i++) + { + writeColorEntry(&s, i, colorTable()[i]); + } +} #if 0 // implemented upstream - user apps void ColorScheme::read(KConfig& config) @@ -403,6 +408,19 @@ void ColorScheme::readColorEntry(QSettings * s , int index) s->endGroup(); } +void ColorScheme::writeColorEntry(QSettings * s, int index, const ColorEntry& entry) const +{ + s->beginGroup(colorNameForIndex(index)); + QStringList colorList; + colorList << QString::number(entry.color.red()) << QString::number(entry.color.green()) << QString::number(entry.color.blue()); + s->setValue("Color", colorList); + s->setValue("Transparency", (bool)entry.transparent); + if (entry.fontWeight != ColorEntry::UseCurrentFormat) + { + s->setValue("Bold", entry.fontWeight == ColorEntry::Bold); + } + s->endGroup(); +} #if 0 // implemented upstream - user apps void ColorScheme::writeColorEntry(KConfig& config , const QString& colorName, const ColorEntry& entry , const RandomizationRange& random) const @@ -566,7 +584,7 @@ bool ColorSchemeManager::loadColorScheme(const QString& filePath) const QString& schemeName = info.baseName(); - ColorScheme* scheme = new ColorScheme(); + ColorScheme* scheme = new ColorScheme(this); scheme->setName(schemeName); scheme->read(filePath); @@ -649,7 +667,7 @@ QString ColorSchemeManager::findColorSchemePath(const QString& name) const return path; } -const ColorScheme* ColorSchemeManager::findColorScheme(const QString& name) +const Konsole::ColorScheme* ColorSchemeManager::findColorScheme(const QString& name) { if ( name.isEmpty() ) return defaultColorScheme(); diff --git a/lib/ColorScheme.h b/lib/ColorScheme.h index 416fe25..04359e2 100644 --- a/lib/ColorScheme.h +++ b/lib/ColorScheme.h @@ -45,15 +45,16 @@ namespace Konsole * The color scheme includes the palette of colors used to draw the text and character backgrounds * in the display and the opacity level of the display background. */ -class ColorScheme +class ColorScheme : public QObject { + Q_OBJECT + public: /** * Constructs a new color scheme which is initialised to the default color set * for Konsole. */ - ColorScheme(); - ColorScheme(const ColorScheme& other); + ColorScheme(QObject *parent = nullptr); ~ColorScheme(); /** Sets the descriptive name of the color scheme. */ @@ -74,6 +75,7 @@ public: void write(KConfig& config) const; #endif void read(const QString & filename); + Q_INVOKABLE void write(const QString & filename) const; /** Sets a single entry within the color palette. */ void setColorTableEntry(int index , const ColorEntry& entry); @@ -96,6 +98,10 @@ public: */ ColorEntry colorEntry(int index , uint randomSeed = 0) const; + Q_INVOKABLE QColor getColor(int index) const; + Q_INVOKABLE void setColor(int index, QColor color); + Q_SIGNAL void colorChanged(int index); + /** * Convenience method. Returns the * foreground color for this scheme, @@ -126,12 +132,13 @@ public: * * TODO: More documentation */ - void setOpacity(qreal opacity); + Q_INVOKABLE void setOpacity(qreal opacity); /** * Returns the opacity level for this color scheme, see setOpacity() * TODO: More documentation */ - qreal opacity() const; + Q_INVOKABLE qreal opacity() const; + Q_SIGNAL void opacityChanged(); /** * Enables randomization of the background color. This will cause @@ -177,6 +184,7 @@ private: void writeColorEntry(KConfig& config , const QString& colorName, const ColorEntry& entry,const RandomizationRange& range) const; #endif void readColorEntry(QSettings *s, int index); + void writeColorEntry(QSettings *s, int index, const ColorEntry& entry) const; // sets the amount of randomization allowed for a particular color // in the palette. creates the randomization table if @@ -221,8 +229,10 @@ public: * Manages the color schemes available for use by terminal displays. * See ColorScheme */ -class ColorSchemeManager +class ColorSchemeManager : public QObject { + Q_OBJECT + public: /** @@ -251,7 +261,7 @@ public: * The first time that a color scheme with a particular name is * requested, the configuration information is loaded from disk. */ - const ColorScheme* findColorScheme(const QString& name); + Q_INVOKABLE const Konsole::ColorScheme* findColorScheme(const QString& name); #if 0 /** @@ -291,7 +301,7 @@ public: * @param[in] path The path to KDE 4 .colorscheme or KDE 3 .schema. * @return Whether the color scheme is loaded successfully. */ - bool loadCustomColorScheme(const QString& path); + Q_INVOKABLE bool loadCustomColorScheme(const QString& path); /** * @brief Allows to add a custom location of color schemes. diff --git a/src/qmltermwidget_plugin.cpp b/src/qmltermwidget_plugin.cpp index 656b115..e9e8b4f 100644 --- a/src/qmltermwidget_plugin.cpp +++ b/src/qmltermwidget_plugin.cpp @@ -1,5 +1,6 @@ #include "qmltermwidget_plugin.h" +#include "ColorScheme.h" #include "TerminalDisplay.h" #include "ksession.h" @@ -9,11 +10,21 @@ using namespace Konsole; +static QObject *colorschememanager_provider(QQmlEngine *engine, QJSEngine *scriptEngine) +{ + Q_UNUSED(engine) + Q_UNUSED(scriptEngine) + + return ColorSchemeManager::instance(); +} + void QmltermwidgetPlugin::registerTypes(const char *uri) { // @uri org.qterminal.qmlterminal qmlRegisterType(uri, 1, 0, "QMLTermWidget"); qmlRegisterType(uri, 1, 0, "QMLTermSession"); + qmlRegisterUncreatableType(uri, 1, 0, "ColorScheme", QStringLiteral("Not instantiatable")); + qmlRegisterSingletonType(uri, 1, 0, "ColorSchemeManager", colorschememanager_provider); } void QmltermwidgetPlugin::initializeEngine(QQmlEngine *engine, const char *uri) -- 2.30.2