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++
Simplify the glyphPaletteProxy model and use it inside the QML
Wolthera van Hövell tot Westerflier486f32253bea
diff --git a/libs/flake/text/KoFontGlyphModel.cpp b/libs/flake/text/KoFontGlyphModel.cppindex f28db6bbff..820d8602de 100644--- a/libs/flake/text/KoFontGlyphModel.cpp+++ b/libs/flake/text/KoFontGlyphModel.cpp@@ -272,8 +272,8 @@ struct KoFontGlyphModel::Private {-KoFontGlyphModel::KoFontGlyphModel()- : QAbstractItemModel()+KoFontGlyphModel::KoFontGlyphModel(QObject *parent)+ : QAbstractItemModel(parent), d(new Private){}diff --git a/libs/flake/text/KoFontGlyphModel.h b/libs/flake/text/KoFontGlyphModel.hindex b720da75e1..a8c3871567 100644--- a/libs/flake/text/KoFontGlyphModel.h+++ b/libs/flake/text/KoFontGlyphModel.h@@ -29,7 +29,7 @@ public:OpenType};- KoFontGlyphModel();+ KoFontGlyphModel(QObject *parent = nullptr);~KoFontGlyphModel();enum Roles {diff --git a/plugins/tools/svgtexttool/Plugin.cpp b/plugins/tools/svgtexttool/Plugin.cppindex d03ca142a6..627bed20ed 100644--- a/plugins/tools/svgtexttool/Plugin.cpp+++ b/plugins/tools/svgtexttool/Plugin.cpp@@ -11,10 +11,18 @@#include <KoShapeRegistry.h>#include <KoToolRegistry.h>+#include <KisStaticInitializer.h>#include "SvgTextToolFactory.h"+#include "glyphpalette/SvgTextLabel.h"+#include "glyphpalette/GlyphPaletteProxyModel.h"+K_PLUGIN_FACTORY_WITH_JSON(PluginFactory, "krita_tool_svgtext.json", registerPlugin<Plugin>();)+KIS_DECLARE_STATIC_INITIALIZER {+ qmlRegisterType<SvgTextLabel>("org.krita.tools.text", 1, 0, "SvgTextLabel");+ qmlRegisterType<GlyphPaletteProxyModel>("org.krita.tools.text", 1, 0, "GlyphPaletteProxyModel");+}Plugin::Plugin(QObject *parent, const QVariantList &): QObject(parent){diff --git a/plugins/tools/svgtexttool/SvgTextCursor.cpp b/plugins/tools/svgtexttool/SvgTextCursor.cppindex e8e979738b..0bda29fc3f 100644--- a/plugins/tools/svgtexttool/SvgTextCursor.cpp+++ b/plugins/tools/svgtexttool/SvgTextCursor.cpp@@ -327,12 +327,12 @@ void SvgTextCursor::removeLastCodePoint()}}-KoSvgTextProperties SvgTextCursor::currentTextProperties() const+QPair<KoSvgTextProperties, KoSvgTextProperties> SvgTextCursor::currentTextProperties() const{if (d->shape) {- return d->shape->propertiesForPos(d->pos);+ return QPair<KoSvgTextProperties, KoSvgTextProperties>(d->shape->propertiesForPos(d->pos), d->shape->propertiesForPos(d->pos, true));}- return KoSvgTextProperties();+ return QPair<KoSvgTextProperties, KoSvgTextProperties>();}QList<KoSvgTextProperties> SvgTextCursor::propertiesForRange() constdiff --git a/plugins/tools/svgtexttool/SvgTextCursor.h b/plugins/tools/svgtexttool/SvgTextCursor.hindex 7f41a2547d..bf372db889 100644--- a/plugins/tools/svgtexttool/SvgTextCursor.h+++ b/plugins/tools/svgtexttool/SvgTextCursor.h@@ -129,7 +129,11 @@ public:void removeLastCodePoint();- KoSvgTextProperties currentTextProperties() const;+ /**+ * @brief currentTextProperties+ * @return a qpair, where the first is the properties without inheritance, and the second, with inheritance.+ */+ QPair<KoSvgTextProperties, KoSvgTextProperties> currentTextProperties() const;QList<KoSvgTextProperties> propertiesForRange() const;diff --git a/plugins/tools/svgtexttool/glyphpalette/GlyphPalette.qml b/plugins/tools/svgtexttool/glyphpalette/GlyphPalette.qmlindex 3de474b727..4784b7f2dd 100644--- a/plugins/tools/svgtexttool/glyphpalette/GlyphPalette.qml+++ b/plugins/tools/svgtexttool/glyphpalette/GlyphPalette.qml@@ -12,9 +12,8 @@ import org.krita.tools.text 1.0Rectangle {id: root;anchors.fill: parent;- property QtObject model;- property QtObject charMapModel;- property QtObject charMapFilterModel;+ property QtObject model: glyphModel;+ property GlyphPaletteProxyModel charMapModel: charMapProxyModel;property int currentIndex: 0;property var fontFamilies: [];@@ -54,10 +53,11 @@ Rectangle {id: glyphAlts;model: DelegateModel {id: glyphAltModel- model: root.model+ model: root.model;property alias rIndex: root.currentIndex;property var defaultIndex: modelIndex(-1);onRIndexChanged: {+ // This first line is necessary to reset the model to the root index.rootIndex = defaultIndex;rootIndex = modelIndex(root.currentIndex);}@@ -71,7 +71,6 @@ Rectangle {fontWeight: root.fontWeight;onGlyphDoubleClicked: (index)=> {mainWindow.slotInsertRichText(root.currentIndex, index, true)};}-}focus: true;clip: true;@@ -92,25 +91,27 @@ Rectangle {spacing: 1;ListView {id: charMapFilter- model: charMapFilterModel;+ model: root.charMapModel.blockLabels;Layout.minimumWidth: 100;Layout.maximumWidth: 200;Layout.fillHeight: true;Layout.fillWidth: true;onCurrentIndexChanged: {- mainWindow.slotChangeFilter(currentIndex);+ root.charMapModel.blockFilter = currentIndex;}clip: true;delegate: ItemDelegate {- width: parent.width;- text: model.display;+ width: charMapFilter !== undefined? charMapFilter.width: 0;+ text: modelData.name;highlighted: ListView.isCurrentItem;onClicked: {- charMapFilter.currentIndex = index;+ charMapFilter.currentIndex = modelData.value;}}+ ScrollBar.vertical: ScrollBar {+ }}ColumnLayout {@@ -123,7 +124,7 @@ Rectangle {TextField {id: searchInput;onTextEdited: {- mainWindow.slotChangeSearchText(text);+ root.charMapModel.searchText = text;}placeholderText: i18nc("@info:placeholder", "Search...");Layout.fillWidth: true;diff --git a/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.cpp b/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.cppindex 338aaa29ac..87870d8f60 100644--- a/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.cpp+++ b/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.cpp@@ -17,18 +17,10 @@#include <KoFontRegistry.h>#include <KoSvgText.h>-#include <KisStaticInitializer.h>-#include "glyphpalette/SvgTextLabel.h"-KIS_DECLARE_STATIC_INITIALIZER {- qmlRegisterType<SvgTextLabel>("org.krita.tools.text", 1, 0, "SvgTextLabel");--}-GlyphPaletteDialog::GlyphPaletteDialog(QWidget *parent): KoDialog(parent)- , m_model(new KoFontGlyphModel)- , m_charMapModel(new GlyphPaletteProxyModel)- , m_filters(new QStandardItemModel)+ , m_model(new KoFontGlyphModel(this))+ , m_charMapModel(new GlyphPaletteProxyModel(this)){setMinimumSize(500, 300);@@ -37,11 +29,6 @@ GlyphPaletteDialog::GlyphPaletteDialog(QWidget *parent)m_quickWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);m_quickWidget->engine()->rootContext()->setContextProperty("mainWindow", this);m_quickWidget->engine()->rootContext()->setContextObject(new KLocalizedContext(this));- m_quickWidget->engine()->addImportPath(KoResourcePaths::getApplicationRoot() + "/lib/qml/");- m_quickWidget->engine()->addImportPath(KoResourcePaths::getApplicationRoot() + "/lib64/qml/");-- m_quickWidget->engine()->addPluginPath(KoResourcePaths::getApplicationRoot() + "/lib/qml/");- m_quickWidget->engine()->addPluginPath(KoResourcePaths::getApplicationRoot() + "/lib64/qml/");/*// Default to fusion style unless the user forces another style@@ -49,33 +36,34 @@ GlyphPaletteDialog::GlyphPaletteDialog(QWidget *parent)QQuickStyle::setStyle(QStringLiteral("Fusion"));}*/+ m_quickWidget->engine()->addImportPath(KoResourcePaths::getApplicationRoot() + "/lib/qml/");+ m_quickWidget->engine()->addImportPath(KoResourcePaths::getApplicationRoot() + "/lib64/qml/");- m_quickWidget->setPalette(this->palette());- m_quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);- m_quickWidget->setSource(QUrl("qrc:/GlyphPalette.qml"));+ m_quickWidget->engine()->addPluginPath(KoResourcePaths::getApplicationRoot() + "/lib/qml/");+ m_quickWidget->engine()->addPluginPath(KoResourcePaths::getApplicationRoot() + "/lib64/qml/");- this->setWindowTitle(i18nc("@windowTitle", "Glyph Palette"));+ m_quickWidget->setPalette(this->palette());+ this->setWindowTitle(i18nc("@title:window", "Glyph Palette"));m_charMapModel->setSourceModel(m_model);- slotUpdateFilterList();- connect(m_model, SIGNAL(modelReset()), this, SLOT(slotUpdateFilterList()));+ connect(m_model, SIGNAL(modelReset()), m_charMapModel, SLOT(emitBlockLabelsChanged()));- if (m_quickWidget->rootObject()) {- m_quickWidget->rootObject()->setProperty("model", QVariant::fromValue(m_model));- m_quickWidget->rootObject()->setProperty("charMapModel", QVariant::fromValue(m_charMapModel));- m_quickWidget->rootObject()->setProperty("charMapFilterModel", QVariant::fromValue(m_filters));- }+ m_quickWidget->rootContext()->setContextProperty("glyphModel", QVariant::fromValue(m_model));+ m_quickWidget->rootContext()->setContextProperty("charMapProxyModel", QVariant::fromValue(m_charMapModel));++ m_quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);+ m_quickWidget->setSource(QUrl("qrc:/GlyphPalette.qml"));if (!m_quickWidget->errors().empty()) {qWarning() << "Errors in " << windowTitle() << ":" << m_quickWidget->errors();}}-void GlyphPaletteDialog::setGlyphModelFromProperties(KoSvgTextProperties properties, QString text)+void GlyphPaletteDialog::setGlyphModelFromProperties(const QPair<KoSvgTextProperties, KoSvgTextProperties> &properties, const QString &text){- if (m_lastUsedProperties.property(KoSvgTextProperties::FontFamiliesId) == properties.property(KoSvgTextProperties::FontFamiliesId)- && m_lastUsedProperties.property(KoSvgTextProperties::FontWeightId) == properties.property(KoSvgTextProperties::FontWeightId)- && m_lastUsedProperties.property(KoSvgTextProperties::FontStyleId) == properties.property(KoSvgTextProperties::FontStyleId)- && m_lastUsedProperties.property(KoSvgTextProperties::FontStretchId) == properties.property(KoSvgTextProperties::FontStretchId)) {+ if (m_lastUsedProperties.property(KoSvgTextProperties::FontFamiliesId) == properties.second.property(KoSvgTextProperties::FontFamiliesId)+ && m_lastUsedProperties.property(KoSvgTextProperties::FontWeightId) == properties.second.property(KoSvgTextProperties::FontWeightId)+ && m_lastUsedProperties.property(KoSvgTextProperties::FontStyleId) == properties.second.property(KoSvgTextProperties::FontStyleId)+ && m_lastUsedProperties.property(KoSvgTextProperties::FontStretchId) == properties.second.property(KoSvgTextProperties::FontStretchId)) {if (m_model && m_model->rowCount() > 0) {QModelIndex idx = m_model->indexForString(text);if (idx.isValid() && m_quickWidget->rootObject()) {@@ -84,32 +72,35 @@ void GlyphPaletteDialog::setGlyphModelFromProperties(KoSvgTextProperties propert}}}- qreal res = 72.0;+ const qreal res = 72.0;QVector<int> lengths;- const QFont::Style style = QFont::Style(properties.propertyOrDefault(KoSvgTextProperties::FontStyleId).toInt());- KoSvgText::AutoValue fontSizeAdjust = properties.propertyOrDefault(KoSvgTextProperties::FontSizeAdjustId).value<KoSvgText::AutoValue>();- if (properties.hasProperty(KoSvgTextProperties::KraTextVersionId)) {- fontSizeAdjust.isAuto = (properties.property(KoSvgTextProperties::KraTextVersionId).toInt() < 3);+ const KoSvgText::CssFontStyleData style = properties.second.propertyOrDefault(KoSvgTextProperties::FontStyleId).value<KoSvgText::CssFontStyleData>();+ KoSvgText::AutoValue fontSizeAdjust = properties.second.propertyOrDefault(KoSvgTextProperties::FontSizeAdjustId).value<KoSvgText::AutoValue>();+ if (properties.second.hasProperty(KoSvgTextProperties::KraTextVersionId)) {+ fontSizeAdjust.isAuto = (properties.second.property(KoSvgTextProperties::KraTextVersionId).toInt() < 3);}- QStringList families = properties.property(KoSvgTextProperties::FontFamiliesId).toStringList();- qreal size = properties.propertyOrDefault(KoSvgTextProperties::FontSizeId).toReal();- int weight = properties.propertyOrDefault(KoSvgTextProperties::FontWeightId).toInt();+ QStringList families = properties.second.property(KoSvgTextProperties::FontFamiliesId).toStringList();+ qreal size = properties.second.propertyOrDefault(KoSvgTextProperties::FontSizeId).toReal();+ int weight = properties.second.propertyOrDefault(KoSvgTextProperties::FontWeightId).toInt();const std::vector<FT_FaceSP> faces = KoFontRegistry::instance()->facesForCSSValues(- properties.property(KoSvgTextProperties::FontFamiliesId).toStringList(),+ properties.second.property(KoSvgTextProperties::FontFamiliesId).toStringList(),lengths,- properties.fontAxisSettings(),+ properties.second.fontAxisSettings(),text,static_cast<quint32>(res),static_cast<quint32>(res),size,fontSizeAdjust.isAuto ? 1.0 : fontSizeAdjust.customValue,- properties.propertyOrDefault(KoSvgTextProperties::FontWeightId).toInt(),- properties.propertyOrDefault(KoSvgTextProperties::FontStretchId).toInt(),- style != QFont::StyleNormal);+ properties.second.propertyOrDefault(KoSvgTextProperties::FontWeightId).toInt(),+ properties.second.propertyOrDefault(KoSvgTextProperties::FontStretchId).toInt(),+ style.style,+ style.slantValue.isAuto? 14: style.slantValue.customValue);++ if (faces.empty()) return;m_model->setFace(faces.front());+QModelIndex idx = m_model->indexForString(text);if (m_quickWidget->rootObject()) {- QString name = faces.front().data()->family_name;m_quickWidget->rootObject()->setProperty("fontFamilies", QVariant::fromValue(families));m_quickWidget->rootObject()->setProperty("fontSize", QVariant::fromValue(size));m_quickWidget->rootObject()->setProperty("fontWeight", QVariant::fromValue(weight));@@ -118,7 +109,7 @@ void GlyphPaletteDialog::setGlyphModelFromProperties(KoSvgTextProperties propertm_quickWidget->rootObject()->setProperty("currentIndex", QVariant::fromValue(idx.row()));}}- m_lastUsedProperties = properties;+ m_lastUsedProperties = properties.first;}void GlyphPaletteDialog::slotInsertRichText(int charRow, int glyphRow, bool replace)@@ -148,30 +139,3 @@ void GlyphPaletteDialog::slotInsertRichText(int charRow, int glyphRow, bool repl}}--void GlyphPaletteDialog::slotChangeFilter(int filterRow)-{- QModelIndex idx = m_filters->index(filterRow, 0);- if (idx.isValid()) {- const int filter = idx.data(Qt::UserRole+1).toInt();- m_charMapModel->setBlockFilter(filter);- }-}--void GlyphPaletteDialog::slotChangeSearchText(QString searchText)-{- m_charMapModel->setSearchText(searchText);-}--void GlyphPaletteDialog::slotUpdateFilterList()-{- m_filters->clear();- m_charMapModel->setBlockFilter(0);- for (int i = 0; i< m_charMapModel->filterLabels().size(); i++) {- int key = m_charMapModel->filterLabels().keys().at(i);- QString label = m_charMapModel->filterLabels().value(key);- QStandardItem *item = new QStandardItem(label);- item->setData(key, Qt::UserRole+1);- m_filters->appendRow(item);- }-}diff --git a/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.h b/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.hindex 6fc733daf1..3b42177bbd 100644--- a/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.h+++ b/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteDialog.h@@ -22,24 +22,18 @@ class GlyphPaletteDialog: public KoDialogpublic:GlyphPaletteDialog(QWidget *parent = nullptr);- void setGlyphModelFromProperties(KoSvgTextProperties properties, QString text = QString());+ void setGlyphModelFromProperties(const QPair<KoSvgTextProperties, KoSvgTextProperties> &properties, const QString &text);Q_SIGNALS:void signalInsertRichText(KoSvgTextShape *text, bool replace);public Q_SLOTS:void slotInsertRichText(int charRow, int glyphRow = -1, bool replace = false);-- void slotChangeFilter(int filterRow);- void slotChangeSearchText(QString searchText);-- void slotUpdateFilterList();private:QQuickWidget *m_quickWidget {0};KoSvgTextProperties m_lastUsedProperties;KoFontGlyphModel *m_model;GlyphPaletteProxyModel *m_charMapModel;- QStandardItemModel *m_filters;};#endif // GLYPHPALETTEDIALOG_Hdiff --git a/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteProxyModel.cpp b/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteProxyModel.cppindex c3788ff94d..d1e1393866 100644--- a/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteProxyModel.cpp+++ b/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteProxyModel.cpp@@ -35,14 +35,15 @@ QString GlyphPaletteProxyModel::searchText() constreturn d->searchText;}-QMap<int, QString> GlyphPaletteProxyModel::filterLabels()+QVariantList GlyphPaletteProxyModel::blockLabels() const{- QMap<int, QString> labels;+ QVariantList labels;+KoFontGlyphModel *model = qobject_cast<KoFontGlyphModel*>(sourceModel());- labels.insert(0, i18nc("@title", "All glyphs"));+ labels.append(QVariantMap({{"name", i18nc("@title", "All glyphs")}, {"value", 0}}));if (model) {for (int i=0; i < model->blocks().size(); i++) {- labels.insert(i+1, model->blocks().at(i).name);+ labels.append(QVariantMap({{"name", model->blocks().at(i).name}, {"value", i+1}}));}}return labels;@@ -83,6 +84,11 @@ void GlyphPaletteProxyModel::setBlockFilter(int filter)}}+void GlyphPaletteProxyModel::emitBlockLabelsChanged()+{+ emit blockLabelsChanged();+}+bool GlyphPaletteProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const{if (sourceParent.isValid()) return true;diff --git a/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteProxyModel.h b/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteProxyModel.hindex aa3034ec12..0fa9a8c921 100644--- a/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteProxyModel.h+++ b/plugins/tools/svgtexttool/glyphpalette/GlyphPaletteProxyModel.h@@ -15,6 +15,7 @@ class GlyphPaletteProxyModel: public QSortFilterProxyModel{Q_OBJECT+ Q_PROPERTY(QVariantList blockLabels READ blockLabels NOTIFY blockLabelsChanged)Q_PROPERTY(int blockFilter READ blockFilter WRITE setBlockFilter NOTIFY blockFilterChanged)Q_PROPERTY(QString searchText READ searchText() WRITE setSearchText NOTIFY searchTextChanged)public:@@ -24,13 +25,16 @@ public:int blockFilter() const;QString searchText() const;- QMap<int, QString> filterLabels();+ QVariantList blockLabels() const;public Q_SLOTS:void setSearchText(const QString &text);void setBlockFilter(int filter);+ void emitBlockLabelsChanged();+Q_SIGNALS:+ void blockLabelsChanged();void blockFilterChanged();void searchTextChanged();