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++
Allow setting the sample text, before text and after text.
Wolthera van Hövell tot Westerflierf47014e844bc
diff --git a/libs/flake/resources/KoCssStylePreset.cpp b/libs/flake/resources/KoCssStylePreset.cppindex 1b84482959..724272ef64 100644--- a/libs/flake/resources/KoCssStylePreset.cpp+++ b/libs/flake/resources/KoCssStylePreset.cpp@@ -26,13 +26,14 @@ const QString STYLE_TYPE = "style_type";const QString STYLE_TYPE_PARAGRAPH = "paragraph";const QString STYLE_TYPE_CHARACTER = "character";+const QString SAMPLE_PLACEHOLDER = i18nc("info:placeholder", "Style Sample");struct KoCssStylePreset::Private {Private(): shape(new KoSvgTextShape()){- shape->insertText(0, i18nc("info:placeholder", "Style Sample"));+ shape->insertText(0, SAMPLE_PLACEHOLDER);}~Private() {}@@ -110,6 +111,93 @@ void KoCssStylePreset::setStyleType(const QString &type)addMetaData(STYLE_TYPE, type);}+QString KoCssStylePreset::sampleText() const+{+ const QVector<int> treeIndex = d->shape->findTreeIndexForPropertyId(KoSvgTextProperties::KraTextStyleType);+ if (treeIndex.isEmpty()) return QString();+ QPair<int, int> pos = d->shape->findRangeForTreeIndex(treeIndex);+ pos.first = d->shape->indexForPos(pos.first);+ pos.second = d->shape->indexForPos(pos.second);+ return d->shape->plainText().mid(pos.first, pos.second-pos.first);+}++void KoCssStylePreset::setSampleText(const QString &sample, const KoSvgTextProperties &props, const QString &before, const QString &after)+{+ KoSvgTextProperties modifiedProps = props;++ KoSvgTextShape *sampleText = new KoSvgTextShape();+ sampleText->insertText(0, sample.isEmpty()? name().isEmpty()? SAMPLE_PLACEHOLDER: name(): sample);+ const QString type = styleType().isEmpty()? STYLE_TYPE_CHARACTER: styleType();+ setStyleType(type);++ bool removeParagraph = type == STYLE_TYPE_CHARACTER;++ // Remove properties that cannot be edited.+ Q_FOREACH(KoSvgTextProperties::PropertyId p, modifiedProps.properties()) {+ if (KoSvgTextProperties::propertyIsBlockOnly(p)) {+ if (removeParagraph) {+ modifiedProps.removeProperty(p);+ }+ } else {+ if (!removeParagraph) {+ modifiedProps.removeProperty(p);+ }+ }+ }+ // This one is added after removing, because otherwise, the type is removed...+ modifiedProps.setProperty(KoSvgTextProperties::KraTextStyleType, type);++ sampleText->setPropertiesAtPos(-1, modifiedProps);++ if (type == STYLE_TYPE_PARAGRAPH) {+ d->shape.reset(sampleText);+ }++ if (before.isEmpty() && after.isEmpty()) {+ d->shape.reset(sampleText);+ } else {+ KoSvgTextShape *newShape = new KoSvgTextShape();+ KoSvgTextProperties paraProps = KoSvgTextProperties::defaultProperties();+ // Set whitespace rule to pre-wrap.+ paraProps.setProperty(KoSvgTextProperties::TextCollapseId, KoSvgText::Preserve);+ paraProps.setProperty(KoSvgTextProperties::TextWrapId, KoSvgText::Wrap);+ newShape->setPropertiesAtPos(-1, paraProps);+ if (!after.isEmpty()) {+ newShape->insertText(0, after);+ }+ if (!before.isEmpty()) {+ newShape->insertText(0, before);+ }++ newShape->insertRichText(newShape->posForIndex(before.size()), sampleText);+ d->shape.reset(newShape);+ }+ d->shape->debugParsing();+ // Don't d->shape->cleanup() here, as it *can* lead to the sample getting cleaned up.+ updateThumbnail();+ setValid(true);+}++QString KoCssStylePreset::beforeText() const+{+ // handle paragraph.+ const QVector<int> treeIndex = d->shape->findTreeIndexForPropertyId(KoSvgTextProperties::KraTextStyleType);+ QPair<int, int> pos = d->shape->findRangeForTreeIndex(treeIndex);+ if (treeIndex.isEmpty()) return QString();+ pos.first = d->shape->indexForPos(pos.first);+ return d->shape->plainText().mid(0, pos.first);+}++QString KoCssStylePreset::afterText() const+{+ // handle paragraph+ const QVector<int> treeIndex = d->shape->findTreeIndexForPropertyId(KoSvgTextProperties::KraTextStyleType);+ QPair<int, int> pos = d->shape->findRangeForTreeIndex(treeIndex);+ if (treeIndex.isEmpty()) return QString();+ pos.second = d->shape->indexForPos(pos.second);+ return d->shape->plainText().mid(pos.second);+}+QString KoCssStylePreset::sampleSvg() const{QMap<QString, QVariant> m = metadata();@@ -158,7 +246,7 @@ bool KoCssStylePreset::loadFromDevice(QIODevice *dev, KisResourcesInterfaceSP reQString styleType = STYLE_TYPE_PARAGRAPH;if (!treeIndex.isEmpty()) {KoSvgTextProperties props = d->shape->propertiesForTreeIndex(treeIndex);- styleType = props.propertyOrDefault(KoSvgTextProperties::KraTextStyleType).toString();+ styleType = props.property(KoSvgTextProperties::KraTextStyleType).toString();}addMetaData(STYLE_TYPE, styleType);@@ -182,28 +270,6 @@ bool KoCssStylePreset::saveToDevice(QIODevice *dev) constd->shape->setAdditionalAttribute(DESC, m[DESCRIPTION].toString());d->shape->setAdditionalAttribute(TITLE, name());- const QVector<int> treeIndex = d->shape->findTreeIndexForPropertyId(KoSvgTextProperties::KraTextStyleType);- if (!treeIndex.isEmpty()) {- KoSvgTextProperties props = d->shape->propertiesForTreeIndex(treeIndex);- bool removeParagraph = styleType() == STYLE_TYPE_CHARACTER;-- // Remove properties that cannot be edited.- Q_FOREACH(KoSvgTextProperties::PropertyId p, props.properties()) {- if (KoSvgTextProperties::propertyIsBlockOnly(p)) {- if (removeParagraph) {- props.removeProperty(p);- }- } else {- if (!removeParagraph) {- props.removeProperty(p);- }- }- }-- props.setProperty(KoSvgTextProperties::KraTextStyleType, styleType());- d->shape->setPropertiesAtTreeIndex(treeIndex, props);- }-SvgWriter writer({d->shape.data()});return writer.save(*dev, d->shape->boundingRect().size());}diff --git a/libs/flake/resources/KoCssStylePreset.h b/libs/flake/resources/KoCssStylePreset.hindex 74b7120695..e28682ec96 100644--- a/libs/flake/resources/KoCssStylePreset.h+++ b/libs/flake/resources/KoCssStylePreset.h@@ -28,6 +28,7 @@ public:KoCssStylePreset &operator=(const KoCssStylePreset &rhs) = delete;~KoCssStylePreset();+ /// The actual text properties.KoSvgTextProperties properties();void setProperties(const KoSvgTextProperties &properties);@@ -35,12 +36,32 @@ public:QString description() const;void setDescription(QString description);+ /// Set the style type, type is either "paragraph" or "character".QString styleType() const;void setStyleType(const QString &type);+ /// The sample text that is being styled by this preset.+ QString sampleText() const;++ /// The text displayed before the sample. Only relevant when in Character mode.+ QString beforeText() const;++ /// The text displayed after the sample, only relevant when in character mode.+ QString afterText() const;+/// Returns the sample svg metadata. Use updateThumbnail to update it.QString sampleSvg() const;+ /**+ * @brief setSampleText+ * This allows setting the visible sample text. In effect, this reconstructs the internal text+ * with before, styled and after text.+ * @param before -- unstyled before text, this is only set when the styletype is character.+ * @param sample -- text of the styled sample.+ * @param after -- unstyled after text, this is only set when the styletype is character.+ */+ void setSampleText(const QString &sample, const KoSvgTextProperties &properties, const QString &before = "", const QString &after = "");+// KoResource interfacepublic:KoResourceSP clone() const override;diff --git a/libs/flake/text/KoSvgTextShape.cpp b/libs/flake/text/KoSvgTextShape.cppindex 3a9ee34ead..a3f35e6542 100644--- a/libs/flake/text/KoSvgTextShape.cpp+++ b/libs/flake/text/KoSvgTextShape.cpp@@ -729,7 +729,7 @@ int KoSvgTextShape::posForPointLineSensitive(QPointF point)return initialPos;}-int KoSvgTextShape::posForIndex(int index, bool firstIndex, bool skipSynthetic)+int KoSvgTextShape::posForIndex(int index, bool firstIndex, bool skipSynthetic) const{int pos = -1;if (d->cursorPos.isEmpty() || index < 0) {@@ -752,7 +752,7 @@ int KoSvgTextShape::posForIndex(int index, bool firstIndex, bool skipSynthetic)return pos;}-int KoSvgTextShape::indexForPos(int pos)+int KoSvgTextShape::indexForPos(int pos) const{if (d->cursorPos.isEmpty() || pos < 0) {return -1;@@ -1013,7 +1013,7 @@ bool KoSvgTextShape::insertRichText(int pos, const KoSvgTextShape *richText)}if (pos > -1 && !d->cursorPos.isEmpty()) {- CursorPos cursorPos = d->cursorPos.at(pos);+ CursorPos cursorPos = d->cursorPos.at(qMin(d->cursorPos.size()-1, pos));CharacterResult res = d->result.at(cursorPos.cluster);index = res.plaintTextIndex;oldIndex = cursorPos.index;@@ -1137,6 +1137,30 @@ bool KoSvgTextShape::setPropertiesAtTreeIndex(const QVector<int> treeIndex, consreturn success;}+QPair<int, int> KoSvgTextShape::findRangeForTreeIndex(const QVector<int> treeIndex) const+{+ QVector<int> idx = treeIndex;+ if (idx.isEmpty()) {+ return qMakePair(-1, -1);+ }+ if (idx.size() == 1) {+ return qMakePair(0, d->cursorPos.size());+ }+ idx.removeFirst();++ int startIndex = 0;+ int endIndex = 0;+ for (auto it = d->textData.childBegin(); it != d->textData.childEnd(); it++) {+ auto child = d->iteratorForTreeIndex(idx, it);+ if (child != d->textData.childEnd()) {+ // count children+ d->startIndexOfIterator(d->textData.childBegin(), child, startIndex);+ endIndex = d->numChars(child) + startIndex;+ }+ }+ return qMakePair(posForIndex(startIndex), posForIndex(endIndex));+}+KoSvgTextProperties KoSvgTextShape::textProperties() const{return KisForestDetail::size(d->textData)? d->textData.childBegin()->properties: KoSvgTextProperties();@@ -1453,6 +1477,7 @@ void KoSvgTextShape::debugParsing()qDebug() << QString(spaces + "+") << it->text;qDebug() << QString(spaces + "|") << it->properties.convertToSvgTextAttributes();+ qDebug() << QString(spaces + "| PropertyType:") << it->properties.property(KoSvgTextProperties::KraTextStyleType).toString();qDebug() << QString(spaces + "| Fill set: ") << it->properties.hasProperty(KoSvgTextProperties::FillId);qDebug() << QString(spaces + "| Stroke set: ") << it->properties.hasProperty(KoSvgTextProperties::StrokeId);qDebug() << QString(spaces + "| Opacity: ") << it->properties.property(KoSvgTextProperties::Opacity);diff --git a/libs/flake/text/KoSvgTextShape.h b/libs/flake/text/KoSvgTextShape.hindex bbb1a6253f..60b7526161 100644--- a/libs/flake/text/KoSvgTextShape.h+++ b/libs/flake/text/KoSvgTextShape.h@@ -246,14 +246,14 @@ public:* @param skipSynthetic -- whether to skip over synthetic cursorPositions.* @return the cursor -- position for an index.*/- int posForIndex(int index, bool firstIndex = false, bool skipSynthetic = false);+ int posForIndex(int index, bool firstIndex = false, bool skipSynthetic = false) const;/*** @brief indexForPos* get the string index for a given cursor position.* @param pos the cursor position.* @return the index in the string.*/- int indexForPos(int pos);+ int indexForPos(int pos) const;/*** @brief initialTextPosition@@ -374,6 +374,17 @@ public:*/bool setPropertiesAtTreeIndex(const QVector<int> treeIndex, const KoSvgTextProperties props);+ /**+ * @brief findStartAndEndPosForTreeIndex+ * Find the start and end cursor position for a given tree index.+ * @param treeIndex the tree index to find the range for. This range will encompass any children.+ * The treeindex will always start with the index of the paragraph (0)+ * and an empty tree index is considered invalid.+ * @return A QPair<int,int> describing cursor position range encompassed by the tree index and it's children.+ * Will return {-1, -1} when the tree index is invalid.+ */+ QPair<int, int> findRangeForTreeIndex(const QVector<int> treeIndex) const;+/*--------------- Properties ---------------*/KoSvgTextProperties textProperties() const;diff --git a/libs/flake/text/KoSvgTextShape_p.h b/libs/flake/text/KoSvgTextShape_p.hindex b3662c08fe..2d429c944d 100644--- a/libs/flake/text/KoSvgTextShape_p.h+++ b/libs/flake/text/KoSvgTextShape_p.h@@ -756,6 +756,21 @@ public:}return KisForestDetail::childEnd(parent);}++ static bool startIndexOfIterator(KisForest<KoSvgTextContentElement>::child_iterator parent, KisForest<KoSvgTextContentElement>::child_iterator target, int ¤tIndex) {+ for (auto child = KisForestDetail::childBegin(parent); child != KisForestDetail::childEnd(parent); child++) {+ if (child == target) {+ return true;+ } else if ((KisForestDetail::childBegin(child) != KisForestDetail::childEnd(child))) {+ if (startIndexOfIterator(child, target, currentIndex)) {+ return true;+ }+ } else {+ currentIndex += numChars(child);+ }+ }+ return false;+ }};#endif // KO_SVG_TEXT_SHAPE_P_Hdiff --git a/plugins/dockers/textproperties/CssStylePresetEditDialog.cpp b/plugins/dockers/textproperties/CssStylePresetEditDialog.cppindex b3c5bbd915..6023f65440 100644--- a/plugins/dockers/textproperties/CssStylePresetEditDialog.cpp+++ b/plugins/dockers/textproperties/CssStylePresetEditDialog.cpp@@ -72,6 +72,7 @@ CssStylePresetEditDialog::~CssStylePresetEditDialog()void CssStylePresetEditDialog::setCurrentResource(KoCssStylePresetSP resource){if (resource == m_currentResource) return;+ m_blockUpdates = true;m_currentResource = resource;KoSvgTextProperties properties = m_currentResource->properties();KoSvgTextPropertyData textData;@@ -82,20 +83,23 @@ void CssStylePresetEditDialog::setCurrentResource(KoCssStylePresetSP resource)m_quickWidget->rootObject()->setProperty("presetDescription", m_currentResource->description());m_quickWidget->rootObject()->setProperty("presetSample", m_currentResource->sampleSvg());m_quickWidget->rootObject()->setProperty("styleType", m_currentResource->styleType());++ m_quickWidget->rootObject()->setProperty("beforeSample", m_currentResource->beforeText());+ m_quickWidget->rootObject()->setProperty("sampleText", m_currentResource->sampleText());+ m_quickWidget->rootObject()->setProperty("afterSample", m_currentResource->afterText());+QMetaObject::invokeMethod(m_quickWidget->rootObject(), "setProperties");+ m_blockUpdates = false;}KoCssStylePresetSP CssStylePresetEditDialog::currentResource(){if (m_currentResource) {- KoSvgTextPropertyData textData = m_model->textData.get();- m_currentResource->setProperties(textData.commonProperties);+ slotUpdateTextProperties();const QString title = m_quickWidget->rootObject()->property("presetTitle").toString();const QString description = m_quickWidget->rootObject()->property("presetDescription").toString();- const QString styleType = m_quickWidget->rootObject()->property("styleType").toString();m_currentResource->setName(title);m_currentResource->setDescription(description);- m_currentResource->setStyleType(styleType);}return m_currentResource;}@@ -107,10 +111,21 @@ void CssStylePresetEditDialog::setDpi(const double dpi)void CssStylePresetEditDialog::slotUpdateTextProperties(){- if (m_currentResource) {+ if (m_currentResource && !m_blockUpdates) {+ const QString before = m_quickWidget->rootObject()->property("beforeSample").toString();+ const QString sample = m_quickWidget->rootObject()->property("sampleText").toString();+ const QString after = m_quickWidget->rootObject()->property("afterSample").toString();+ const QString styleType = m_quickWidget->rootObject()->property("styleType").toString();++ bool shouldUpdateSample = (before != m_currentResource->beforeText()+ || sample != m_currentResource->sampleText()+ || after != m_currentResource->afterText());+KoSvgTextPropertyData textData = m_model->textData.get();- if (m_currentResource->properties() != textData.commonProperties) {- m_currentResource->setProperties(textData.commonProperties);++ if (m_currentResource->properties() != textData.commonProperties || shouldUpdateSample) {+ m_currentResource->setStyleType(styleType);+ m_currentResource->setSampleText(sample, textData.commonProperties, before, after);m_quickWidget->rootObject()->setProperty("presetSample", m_currentResource->sampleSvg());QMetaObject::invokeMethod(m_quickWidget->rootObject(), "setProperties");}diff --git a/plugins/dockers/textproperties/CssStylePresetEditDialog.h b/plugins/dockers/textproperties/CssStylePresetEditDialog.hindex 89b40e6b61..64394cbf5c 100644--- a/plugins/dockers/textproperties/CssStylePresetEditDialog.h+++ b/plugins/dockers/textproperties/CssStylePresetEditDialog.h@@ -33,6 +33,7 @@ private:QQuickWidget *m_quickWidget {0};KoSvgTextPropertiesModel *m_model;KoCssStylePresetSP m_currentResource;+ bool m_blockUpdates {false};};#endif // CSSSTYLEPRESETEDITDIALOG_Hdiff --git a/plugins/dockers/textproperties/qml/CssStylePresetEdit.qml b/plugins/dockers/textproperties/qml/CssStylePresetEdit.qmlindex cfbf0adef8..c9db331a15 100644--- a/plugins/dockers/textproperties/qml/CssStylePresetEdit.qml+++ b/plugins/dockers/textproperties/qml/CssStylePresetEdit.qml@@ -17,6 +17,11 @@ Control {property alias presetSample: presetSampleLabel.svgData;property string styleType: "paragraph";property double canvasDPI: 72.0;++ property alias beforeSample: beforeSampleField.text;+ property alias sampleText: sampleField.text;+ property alias afterSample: afterSampleField.text;+onStyleTypeChanged: {styleTypeCmb.currentIndex = styleTypeCmb.indexOfValue(styleType === "paragraph"? TextPropertyConfigModel.Paragraph: TextPropertyConfigModel.Character);}@@ -84,7 +89,7 @@ Control {}background: Rectangle {- border.color: presetDescriptionArea.focus? palette.highlight: palette.mid;+ border.color: presetDescriptionArea.activeFocus? palette.highlight: palette.mid;border.width: 1;color: palette.base;}@@ -106,6 +111,31 @@ Control {textRole: "text";onActivated: styleEdit.styleType = currentValue === TextPropertyConfigModel.Character? "character": "paragraph";}+ Label {+ text: i18nc("label:Title", "Sample Text:");+ }+ RowLayout {+ Layout.columnSpan: 2;+ Layout.fillWidth: true;+ TextField {+ id: beforeSampleField;+ Layout.fillWidth: true;+ placeholderText: i18nc("info:placeholder", "Before...");+ onFocusChanged: if (!activeFocus) mainWindow.slotUpdateTextProperties();+ }+ TextField {+ id: sampleField;+ Layout.fillWidth: true;+ placeholderText: presetTitleField.text.length == 0? i18nc("info:placeholder", "Sample..."): presetTitleField.text;+ onFocusChanged: if (!activeFocus) mainWindow.slotUpdateTextProperties();+ }+ TextField {+ id: afterSampleField;+ Layout.fillWidth: true;+ placeholderText: i18nc("info:placeholder", "... After");+ onFocusChanged: if (!activeFocus) mainWindow.slotUpdateTextProperties();+ }+ }}TextPropertyBaseList {Layout.fillWidth: true;