Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Introduce TextPropertyConfigModel and use it to handle filtering for the 'add properties' list.
Wolthera van Hövell tot Westerfliera723b8fc2351
diff --git a/plugins/dockers/textproperties/CMakeLists.txt b/plugins/dockers/textproperties/CMakeLists.txtindex 9e60258e5c..335fe95359 100644--- a/plugins/dockers/textproperties/CMakeLists.txt+++ b/plugins/dockers/textproperties/CMakeLists.txt@@ -8,6 +8,7 @@ set(KRITA_TEXTPROPERTIES_SOURCESOpenTypeFeatureModel.cppLocaleHandler.cppCssQmlUnitConverter.cpp+ TextPropertyConfigModel.cpp)qt_add_resources(KRITA_TEXTPROPERTIES_SOURCES textproperties.qrc)diff --git a/plugins/dockers/textproperties/TextPropertiesDock.cpp b/plugins/dockers/textproperties/TextPropertiesDock.cppindex d17502720a..64ac805bf5 100644--- a/plugins/dockers/textproperties/TextPropertiesDock.cpp+++ b/plugins/dockers/textproperties/TextPropertiesDock.cpp@@ -49,6 +49,7 @@#include "TagFilterProxyModelQmlWrapper.h"#include "LocaleHandler.h"#include "CssQmlUnitConverter.h"+#include "TextPropertyConfigModel.h"/// Strange place to put this, do we have a better spot?KIS_DECLARE_STATIC_INITIALIZER {@@ -71,6 +72,8 @@ KIS_DECLARE_STATIC_INITIALIZER {qmlRegisterType<CssQmlUnitConverter>("org.krita.flake.text", 1, 0, "CssQmlUnitConverter");qmlRegisterType<TagFilterProxyModelQmlWrapper>("org.krita.flake.text", 1, 0, "TagFilterProxyModelQmlWrapper");qmlRegisterType<LocaleHandler>("org.krita.flake.text", 1, 0, "LocaleHandler");+ qmlRegisterType<TextPropertyConfigModel>("org.krita.flake.text", 1, 0, "TextPropertyConfigModel");+ qmlRegisterType<TextPropertyConfigFilterModel>("org.krita.flake.text", 1, 0, "TextPropertyConfigFilterModel");}@@ -109,7 +112,7 @@ struct TextPropertiesDock::PrivateFontAxesModel axesModel;KisResourceModel *fontModel{nullptr};KisCanvasResourceProvider *provider{nullptr};-+ TextPropertyConfigModel *textPropertyConfigModel {nullptr};qreal currentDpi{72.0};};@@ -138,6 +141,7 @@ TextPropertiesDock::TextPropertiesDock()m_quickWidget->setMinimumHeight(100);d->fontModel = new KisResourceModel(ResourceType::FontFamilies);+ d->textPropertyConfigModel = new TextPropertyConfigModel(this);QList<QLocale> locales;QStringList wellFormedBCPNames;@@ -153,6 +157,7 @@ TextPropertiesDock::TextPropertiesDock()m_quickWidget->rootContext()->setContextProperty("textPropertiesModel", d->textModel);m_quickWidget->rootContext()->setContextProperty("fontStylesModel", QVariant::fromValue(&d->stylesModel));m_quickWidget->rootContext()->setContextProperty("fontAxesModel", QVariant::fromValue(&d->axesModel));+ m_quickWidget->rootContext()->setContextProperty("textPropertyConfigModel", QVariant::fromValue(d->textPropertyConfigModel));m_quickWidget->rootContext()->setContextProperty("locales", QVariant::fromValue(wellFormedBCPNames));m_quickWidget->rootContext()->setContextProperty("canvasDPI", QVariant::fromValue(d->currentDpi));connect(d->textModel, SIGNAL(textPropertyChanged()),diff --git a/plugins/dockers/textproperties/TextPropertyConfigModel.cpp b/plugins/dockers/textproperties/TextPropertyConfigModel.cppnew file mode 100644index 0000000000..c84da37ca5--- /dev/null+++ b/plugins/dockers/textproperties/TextPropertyConfigModel.cpp@@ -0,0 +1,200 @@+/*+ * SPDX-FileCopyrightText: 2025 Wolthera van Hövell tot Westerflier <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */+#include <KSharedConfig>+#include <KConfigGroup>++#include "TextPropertyConfigModel.h"++struct TextPropertyConfigModel::TextPropertyData {+ TextPropertyConfigModel::VisibilityState visibilityState;+ TextPropertyConfigModel::PropertyType propertyType;+ bool collapsed;+ QStringList searchTerms;+ QString toolTip;+ QString title;+};++const QMap<TextPropertyConfigModel::VisibilityState, QString> visibilityConfigNames = {+ {TextPropertyConfigModel::FollowDefault, "followDefault"},+ {TextPropertyConfigModel::AlwaysVisible, "alwaysVisible"},+ {TextPropertyConfigModel::NeverVisible, "neverVisible"},+ {TextPropertyConfigModel::WhenRelevant, "whenRelevant"},+ {TextPropertyConfigModel::WhenSet, "whenSet"},+};++struct TextPropertyConfigModel::Private {+ QStringList propertyNames;+ QMap<QString, TextPropertyConfigModel::TextPropertyData> propertyData;+ TextPropertyConfigModel::VisibilityState defaultVisibilityState;+};++TextPropertyConfigModel::TextPropertyConfigModel(QObject *parent)+ : QAbstractListModel{parent}+ , d(new Private)+{+}++TextPropertyConfigModel::~TextPropertyConfigModel()+{+}++int TextPropertyConfigModel::defaultVisibilityState() const {+ return int(d->defaultVisibilityState);+}++void TextPropertyConfigModel::setDefaultVisibilityState(const int state) {+ if (state == int(d->defaultVisibilityState))+ return;+ d->defaultVisibilityState = VisibilityState(state);+ emit defaultVisibilityStateChanged();+}++int TextPropertyConfigModel::rowCount(const QModelIndex &parent) const+{+ Q_UNUSED(parent)+ return d->propertyNames.size();+}++QVariant TextPropertyConfigModel::data(const QModelIndex &index, int role) const+{+ if (!index.isValid()) return QVariant();+ if (index.row() < 0 || index.row() >= d->propertyNames.size()) return QVariant();++ const QString name = d->propertyNames.at(index.row());++ const TextPropertyData data = d->propertyData.value(name);+ if (role == Name) {+ return name;+ } else if (role == Qt::DisplayRole) {+ return data.title;+ } else if (role == Qt::ToolTipRole) {+ return data.toolTip;+ } else if (role == Visibility) {+ return data.visibilityState != FollowDefault?+ int(data.visibilityState):+ d->defaultVisibilityState != FollowDefault? int(d->defaultVisibilityState): int(WhenRelevant);+ } else if (role == Collapsed) {+ return data.collapsed;+ } else if (role == SearchTerms) {+ return data.searchTerms;+ } else if (role == Type) {+ return int (data.propertyType);+ }+ return QVariant();+}++bool TextPropertyConfigModel::setData(const QModelIndex &index, const QVariant &value, int role)+{+ if (!index.isValid()) return false;+ if (index.row() < 0 || index.row() >= d->propertyNames.size()) return false;++ if (role == Visibility) {+ const QString name = d->propertyNames.at(index.row());+ d->propertyData[name].visibilityState = VisibilityState(value.toInt());+ emit dataChanged(index, index, {role});+ return true;+ } else if (role == Collapsed) {+ const QString name = d->propertyNames.at(index.row());+ d->propertyData[name].collapsed = value.toBool();+ emit dataChanged(index, index, {role});+ return true;+ }+ return false;+}++bool TextPropertyConfigModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)+{+ if (sourceParent != destinationParent) return false;+ beginMoveRows(sourceParent, sourceRow, sourceRow+count, destinationParent, destinationChild);+ for (int i = 0; i < count; i++) {+ d->propertyNames.move(sourceRow+i, destinationChild+i);+ }+ endMoveRows();+ return true;+}++QHash<int, QByteArray> TextPropertyConfigModel::roleNames() const+{+ QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();+ roles[Visibility] = "visibilityState";+ roles[SearchTerms] = "searchTerms";+ roles[Collapsed] = "collapsed";+ roles[Type] = "type";+ roles[Name] = "name";+ roles[Qt::DisplayRole] = "title";+ return roles;+}++void TextPropertyConfigModel::saveConfiguration()+{+ KConfigGroup configGroup = KSharedConfig::openConfig()->group("TextPropertiesDock");+ Q_FOREACH (const QString name, d->propertyNames) {+ const TextPropertyData data = d->propertyData.value(name);+ configGroup.writeEntry(name, visibilityConfigNames.value(data.visibilityState));+ }+}++void TextPropertyConfigModel::loadFromConfiguration() {+ KConfigGroup configGroup = KSharedConfig::openConfig()->group("TextPropertiesDock");+ Q_FOREACH (const QString name, d->propertyNames) {+ d->propertyData[name].visibilityState = visibilityConfigNames.key(configGroup.readEntry(name));+ }+}++void TextPropertyConfigModel::addProperty(const QString name, const int propertyType, const QString title, const QString toolTip, const QString searchTerms, const int visibilityState, const bool collapsed)+{+ if (d->propertyNames.contains(name)) return;+ beginInsertRows(QModelIndex(), d->propertyNames.size(), d->propertyNames.size());+ TextPropertyData data = d->propertyData.value(name, TextPropertyData());+ data.title = title;+ data.propertyType = PropertyType(propertyType);+ data.toolTip = toolTip;+ data.searchTerms = searchTerms.split(",");+ data.visibilityState = visibilityState < 0? FollowDefault: VisibilityState(visibilityState);+ data.collapsed = collapsed;+ d->propertyData.insert(name, data);+ d->propertyNames.append(name);+ endInsertRows();+}++TextPropertyConfigFilterModel::TextPropertyConfigFilterModel(QObject *parent)+ : QSortFilterProxyModel(parent) {+ setFilterCaseSensitivity(Qt::CaseInsensitive);+}++bool TextPropertyConfigFilterModel::showParagraphProperties() const {+ return m_showParagraphProperties;+}++void TextPropertyConfigFilterModel::setShowParagraphProperties(const bool show) {+ if (m_showParagraphProperties == show) return;+ m_showParagraphProperties = show;+ invalidateFilter();+ emit showParagraphPropertiesChanged();+}++void TextPropertyConfigFilterModel::setProxySourceModel(QAbstractItemModel *model) {+ if (sourceModel() == model) return;+ setSourceModel(model);+ emit proxySourceModelChanged();+}++bool TextPropertyConfigFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const+{+ QModelIndex idx = sourceModel()->index(source_row, 0, source_parent);+ if (!idx.isValid()) return false;++ const QString name = sourceModel()->data(idx, Qt::DisplayRole).toString();+ const QStringList searchTerms = sourceModel()->data(idx, TextPropertyConfigModel::SearchTerms).toStringList();+ const TextPropertyConfigModel::PropertyType type = TextPropertyConfigModel::PropertyType(sourceModel()->data(idx, TextPropertyConfigModel::Type).toInt());+ if (type == TextPropertyConfigModel::Paragraph && !m_showParagraphProperties) {+ return false;+ } else if (type == TextPropertyConfigModel::Character && m_showParagraphProperties) {+ return false;+ }++ return (name.contains(filterRegularExpression()) || searchTerms.filter(filterRegularExpression()).size()>0);+}diff --git a/plugins/dockers/textproperties/TextPropertyConfigModel.h b/plugins/dockers/textproperties/TextPropertyConfigModel.hnew file mode 100644index 0000000000..02ac4a3663--- /dev/null+++ b/plugins/dockers/textproperties/TextPropertyConfigModel.h@@ -0,0 +1,105 @@+/*+ * SPDX-FileCopyrightText: 2025 Wolthera van Hövell tot Westerflier <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */+#ifndef TEXTPROPERTYCONFIGMODEL_H+#define TEXTPROPERTYCONFIGMODEL_H++#include <QAbstractListModel>+#include <QSortFilterProxyModel>+#include <QObject>++// This model helps to keep track of the relevant properties.++class TextPropertyConfigModel : public QAbstractListModel+{+ Q_OBJECT+ Q_PROPERTY(int defaultVisibilityState READ defaultVisibilityState WRITE setDefaultVisibilityState NOTIFY defaultVisibilityStateChanged)+public:++ enum VisibilityState {+ FollowDefault = 0, ///< Follow the default property.+ WhenRelevant, ///< Show when either set or inherited.+ WhenSet, ///< Show only when set.+ AlwaysVisible, ///< Always show property.+ NeverVisible, ///< Never show property.+ };+ Q_ENUM(VisibilityState)++ enum PropertyType {+ Character, ///< This property can be applied on a character level.+ Paragraph, ///< This property only does something when applied to a paragraph.+ Mixed ///< This property can be in either.+ };+ Q_ENUM(PropertyType)++ enum Roles {+ Visibility = Qt::UserRole + 1,+ Collapsed,+ SearchTerms,+ Type,+ Name,+ };++ explicit TextPropertyConfigModel(QObject *parent = nullptr);+ ~TextPropertyConfigModel();++ int defaultVisibilityState() const;+ void setDefaultVisibilityState(const int state);++ // Call this after adding all the properties.+ Q_INVOKABLE void loadFromConfiguration();++ Q_INVOKABLE void addProperty(const QString name,+ const int propertyType = 0,+ const QString title = QString(),+ const QString toolTip = QString(),+ const QString searchTerms = QString(),+ const int visibilityState = -1,+ const bool collapsed = false);++ // QAbstractItemModel interface+public:+ int rowCount(const QModelIndex &parent) const override;+ QVariant data(const QModelIndex &index, int role) const override;+ bool setData(const QModelIndex &index, const QVariant &value, int role) override;+ bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override;+ QHash<int, QByteArray> roleNames() const override;++Q_SIGNALS:+ void defaultVisibilityStateChanged();+private Q_SLOTS:+ void saveConfiguration();+private:++ struct Private;+ struct TextPropertyData;+ const QScopedPointer<Private> d;+};++class TextPropertyConfigFilterModel : public QSortFilterProxyModel+{+ Q_OBJECT+ Q_PROPERTY(bool showParagraphProperties READ showParagraphProperties WRITE setShowParagraphProperties NOTIFY showParagraphPropertiesChanged)+ Q_PROPERTY(QAbstractItemModel *sourceModel READ sourceModel WRITE setProxySourceModel NOTIFY proxySourceModelChanged)+public:+ TextPropertyConfigFilterModel(QObject *parent = nullptr);+ // QSortFilterProxyModel interface++ bool showParagraphProperties() const;++ void setShowParagraphProperties(const bool show);++ void setProxySourceModel(QAbstractItemModel *model);++Q_SIGNALS:+ void showParagraphPropertiesChanged();+ void proxySourceModelChanged();+protected:+ bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;++ bool m_showParagraphProperties;+};++#endif // TEXTPROPERTYCONFIGMODEL_Hdiff --git a/plugins/dockers/textproperties/qml/TextProperties.qml b/plugins/dockers/textproperties/qml/TextProperties.qmlindex d3a0e28b1c..777b0827e1 100644--- a/plugins/dockers/textproperties/qml/TextProperties.qml+++ b/plugins/dockers/textproperties/qml/TextProperties.qml@@ -52,12 +52,12 @@ Rectangle {TextPropertyBaseList {id: characterPropertyList;- propertyType: TextPropertyBase.Character;+ propertyType: TextPropertyConfigModel.Character;}TextPropertyBaseList {id: paragraphPropertyList;- propertyType: TextPropertyBase.Paragraph;+ propertyType: TextPropertyConfigModel.Paragraph;}}}diff --git a/plugins/dockers/textproperties/qml/TextPropertyBaseList.qml b/plugins/dockers/textproperties/qml/TextPropertyBaseList.qmlindex 2cb4b4b06e..605cc62efd 100644--- a/plugins/dockers/textproperties/qml/TextPropertyBaseList.qml+++ b/plugins/dockers/textproperties/qml/TextPropertyBaseList.qml@@ -7,9 +7,11 @@ import QtQuick 2.15import QtQuick.Controls 2.15import QtQuick.Layouts 1.15import QtQml.Models 2.1+import org.krita.flake.text 1.0ColumnLayout {- property int propertyType: TextPropertyBase.Character;+ id: propertyBaseList;+ property int propertyType: TextPropertyConfigModel.Character;ListModel {id: propertyList;@@ -19,53 +21,60 @@ ColumnLayout {id: filteredPropertyList;}+ TextPropertyConfigFilterModel {+ id : filteredConfigModel;+ sourceModel: textPropertyConfigModel;+ showParagraphProperties: propertyBaseList.propertyType === TextPropertyConfigModel.Paragraph;+ onShowParagraphPropertiesChanged: fillPropertyList();+ filterCaseSensitivity: Qt.CaseInsensitive;+ }+Component.onCompleted: fillPropertyList();function fillPropertyList() {for (var i = 0; i < propertyWidgetModel.count; i++) {- propertyWidgetModel.get(i).parentPropertyType = propertyType;+ let prop = propertyWidgetModel.get(i);+ textPropertyConfigModel.addProperty(prop.propertyName,+ prop.propertyType,+ prop.propertyTitle,+ prop.toolTip,+ prop.searchTerms,+ );+ textPropertyConfigModel.loadFromConfiguration();+ console.log("property show", filteredConfigModel.showParagraphProperties);+ if (filteredConfigModel.showParagraphProperties && prop.propertyType === TextPropertyConfigModel.Character) {+ propertyWidgetModel.get(i).visible = false;+ } else if (!filteredConfigModel.showParagraphProperties && prop.propertyType === TextPropertyConfigModel.Paragraph) {+ propertyWidgetModel.get(i).visible = false;+ }+ }+ }++ function updateProperties() {+ for (var i = 0; i < propertyWidgetModel.count; i++) {if (propertyWidgetModel.get(i).propertyType === propertyType ||propertyWidgetModel.get(i).propertyType === TextPropertyBase.Mixed) {- propertyList.append({"title": propertyWidgetModel.get(i).propertyTitle,- "name": propertyWidgetModel.get(i).propertyName,- "toolTip": propertyWidgetModel.get(i).toolTip,- "searchTerms": propertyWidgetModel.get(i).searchTerms,- "item": propertyWidgetModel.get(i)});+ propertyWidgetModel.get(i).propertiesUpdated();} else {propertyWidgetModel.get(i).visible = false;}}- updateFilteredPropertyList();- }-- signal filterChanged;-- onFilterChanged: {- updateFilteredPropertyList();}- function updateFilteredPropertyList() {- filteredPropertyList.clear();- for (var i = 0; i < propertyList.count; i++) {- var searchText = propertySearch.text;- if (searchText.length === 0) {- filteredPropertyList.append(propertyList.get(i));- } else {- var searchTerms = propertyList.get(i).searchTerms.split(",");- if (searchTerms.find(term => term.toLowerCase().includes(searchText.toLowerCase()))) {- filteredPropertyList.append(propertyList.get(i));- }+ function isVisible(name) {+ for (var i = 0; i < propertyWidgetModel.count; i++) {+ let prop = propertyWidgetModel.get(i);+ if (name === prop.propertyName) {+ return prop.visible;}}}- function updateProperties() {+ function enableProperty(name) {for (var i = 0; i < propertyWidgetModel.count; i++) {- if (propertyWidgetModel.get(i).propertyType === propertyType ||- propertyWidgetModel.get(i).propertyType === TextPropertyBase.Mixed) {- propertyWidgetModel.get(i).propertiesUpdated();- } else {- propertyWidgetModel.get(i).visible = false;+ if (name === propertyWidgetModel.get(i).propertyName) {+ propertyWidgetModel.get(i).enableProperty();+ break;}}}@@ -162,30 +171,29 @@ ColumnLayout {id: addPropertyCmb;Layout.fillWidth: true;Layout.minimumHeight: implicitHeight;- model: filteredPropertyList;- textRole: "title";+ model: filteredConfigModel;+ textRole: "display";displayText: i18nc("@label:listbox", "Add Property");- onActivated: {- model.get(currentIndex).item.enableProperty();- }onPopupChanged: if (!addPropertyCmb.popup.visible) { propertySearch.text = ""; }- signal searchChanged;- onSearchChanged: parent.filterChanged();-PaletteControl {id: cmbPalette;colorGroup: addPropertyCmb.enabled? SystemPalette.Active: SystemPalette.Disabled;}palette: cmbPalette.palette;+ function enableProperty(name) {+ propertyBaseList.enableProperty(name);+ popup.close();+ }+delegate: ItemDelegate {id: addPropertyDelegate;width: addPropertyCmb.width;+ required property var model;required property string title;required property string name;required property int index;- required property QtObject item;required property string toolTip;contentItem: Label {enabled: addPropertyDelegate.enabled;@@ -199,13 +207,15 @@ ColumnLayout {elide: Text.ElideRight;verticalAlignment: Text.AlignVCenter}- enabled: !item.visible;+ enabled: !isVisible(name);highlighted: addPropertyCmb.highlightedIndex === index;background: Rectangle { color: highlighted? parent.palette.highlight:"transparent"; }ToolTip.text: toolTip;ToolTip.delay: Qt.styleHints.mousePressAndHoldIntervalToolTip.visible: highlighted;++ onClicked: addPropertyCmb.enableProperty(name);}popup: Popup {@@ -239,7 +249,7 @@ ColumnLayout {placeholderText: i18nc("@info:placeholder", "Search...");Layout.fillWidth: true;Layout.preferredHeight: implicitHeight;- onTextChanged: addPropertyCmb.searchChanged();+ onTextChanged: filteredConfigModel.setFilterRegularExpression(text);}}}