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++
Fix undo for KoSvgTextSetTextPathOnRangeCommand
Wolthera van Hövell tot Westerflier6cb84378eb6b
diff --git a/libs/flake/commands/KoSvgTextAddRemoveShapeCommands.cpp b/libs/flake/commands/KoSvgTextAddRemoveShapeCommands.cppindex bc63c40565..b693861c17 100644--- a/libs/flake/commands/KoSvgTextAddRemoveShapeCommands.cpp+++ b/libs/flake/commands/KoSvgTextAddRemoveShapeCommands.cpp@@ -25,6 +25,7 @@ struct KoSvgTextAddRemoveShapeCommandImpl::Private {~Private() {}KoSvgTextShape *textShape = nullptr;KoShape* shape = nullptr;+ QList<KoShape*> oldTextPaths;std::optional<KoShapeContainer*> originalShapeParent;KoSvgTextShapeMementoSP memento;int startPos = -1;@@ -50,12 +51,17 @@ KoSvgTextAddRemoveShapeCommandImpl::KoSvgTextAddRemoveShapeCommandImpl(KoSvgTextd->type = Subtract;} else if (d->textShape->shapeInContours(shape)){d->type = TextPath;+ KoSvgTextNodeIndex idx = textShape->nodeForTextPath(shape);+ QPair<int, int> range = textShape->findRangeForNodeIndex(idx);+ startPos = range.first;+ endPos = range.second;} else {d->type = Unknown;}} else {d->type = type;}+ d->oldTextPaths = d->textShape->textPathsAtRange(startPos, endPos);}KoSvgTextAddRemoveShapeCommandImpl::~KoSvgTextAddRemoveShapeCommandImpl()@@ -82,6 +88,11 @@ void KoSvgTextAddRemoveShapeCommandImpl::partB()}if (!d->removeCommand) {+ Q_FOREACH(KoShape *path, d->oldTextPaths) {+ if (!d->textShape->shapeInContours(path)) {+ d->textShape->addTextPathAtEnd(path);+ }+ }d->textShape->setMemento(d->memento);} else {d->textShape->relayout();@@ -107,11 +118,16 @@ void KoSvgTextAddRemoveShapeCommandImpl::partA()d->textShape->addShapeContours({d->shape}, true);} else if (d->type == Subtract) {d->textShape->addShapeContours({d->shape}, false);- } else if (!d->removeCommand && d->type == TextPath) {+ } else if (d->type == TextPath) {d->textShape->setTextPathOnRange(d->shape, d->startPos, d->endPos);}if (d->removeCommand) {+ Q_FOREACH(KoShape *path, d->oldTextPaths) {+ if (!d->textShape->shapeInContours(path)) {+ d->textShape->addTextPathAtEnd(path);+ }+ }d->textShape->setMemento(d->memento);} else {d->textShape->relayout();diff --git a/libs/flake/tests/TestSvgTextShape.cpp b/libs/flake/tests/TestSvgTextShape.cppindex c7b12db87f..cae30a5896 100644--- a/libs/flake/tests/TestSvgTextShape.cpp+++ b/libs/flake/tests/TestSvgTextShape.cpp@@ -844,27 +844,32 @@ void TestSvgTextShape::testTextPathOnRange()KoSvgTextSetTextPathOnRangeCommand *cmd = new KoSvgTextSetTextPathOnRangeCommand(textShape, path, startPos, endPos);paintShapes(textShape, {path}, QString("ddd_%1_00_initial.png").arg(QTest::currentDataTag()));- qDebug() << ppVar(path->name());++ KoSvgTextNodeIndex originalIndex = textShape->topLevelNodeForPos((startPos+endPos)/2);+ KoPathShape *originalTextPath = dynamic_cast<KoPathShape*>(originalIndex.textPath());+ const QString originalIndexTextPathString = originalTextPath? originalTextPath->toString(): QString();cmd->redo();- qDebug() << ppVar(path->name());KoSvgTextNodeIndex idx = textShape->topLevelNodeForPos((startPos+endPos)/2);KoPathShape *textPath = dynamic_cast<KoPathShape*>(idx.textPath());- qDebug() << ppVar(textPath->name());- QCOMPARE(textPath, path);+ QVERIFY(textPath);+ QCOMPARE(textPath->toString(), path->toString());paintShapes(textShape, {path}, QString("ddd_%1_10_redo.png").arg(QTest::currentDataTag()));- // KoPathShape *textPath = dynamic_cast<KoPathShape*>(idx.textPath());- // QVERIFY(textPath);- // QCOMPARE(textPath->toString(), path->toString());-cmd->undo();+ KoSvgTextNodeIndex idx2 = textShape->topLevelNodeForPos((startPos+endPos)/2);+ if (!originalIndexTextPathString.isEmpty()) {+ KoPathShape *currentTextPath = dynamic_cast<KoPathShape*>(idx2.textPath());+ QVERIFY(currentTextPath);+ QCOMPARE(originalIndexTextPathString, currentTextPath->toString());+ } else {+ QVERIFY(!idx2.textPath());+ }+paintShapes(textShape, {path}, QString("ddd_%1_20_undo.png").arg(QTest::currentDataTag()));- qDebug() << ppVar(path->name());-}KISTEST_MAIN(TestSvgTextShape)diff --git a/libs/flake/text/KoSvgTextLoader.cpp b/libs/flake/text/KoSvgTextLoader.cppindex ffe241099a..fc0c54a670 100644--- a/libs/flake/text/KoSvgTextLoader.cpp+++ b/libs/flake/text/KoSvgTextLoader.cpp@@ -118,22 +118,7 @@ void KoSvgTextLoader::setStyleInfo(KoShape *s)void KoSvgTextLoader::setTextPathOnCurrentNode(KoShape *s){if (!KisForestDetail::isEnd(d->currentNode) && s) {- if (s->name().isEmpty()) {- int count = 0;- while(s->name().isEmpty()) {- QString newName("path"+QString::number(count));- bool nameIsUnique = true;- Q_FOREACH(KoShape *shape, d->shape->d->textPaths) {- if (shape->name() == newName) {- nameIsUnique = false;- }- }- if(nameIsUnique) {- s->setName(newName);- }- count+=1;- }- }+ d->shape->d->makeTextPathNameUnique(d->shape->d->textPaths, s);d->currentNode->textPathId = s->name();s->addDependee(d->shape);d->shape->d->textPaths.append(s);diff --git a/libs/flake/text/KoSvgTextShape.cpp b/libs/flake/text/KoSvgTextShape.cppindex a30347fc8a..dad17aa43e 100644--- a/libs/flake/text/KoSvgTextShape.cpp+++ b/libs/flake/text/KoSvgTextShape.cpp@@ -56,8 +56,7 @@ public:const QMap<int, int> &logicalToVisualCursorPos,const QString &plainText,const bool &isBidi,- const QPointF &initialTextPosition,- const QList<KoShape*> textPaths)+ const QPointF &initialTextPosition): KoSvgTextShapeMemento(), textData(textData), result(result)@@ -67,7 +66,6 @@ public:, plainText(plainText), isBidi(isBidi), initialTextPosition(initialTextPosition)- , textPaths(textPaths){}@@ -86,8 +84,6 @@ private:QString plainText;bool isBidi = false;QPointF initialTextPosition = QPointF();-- QList<KoShape *> textPaths;};struct KoSvgTextNodeIndex::Private {@@ -1484,6 +1480,19 @@ KoSvgTextNodeIndex KoSvgTextShape::topLevelNodeForPos(int pos) constreturn d->createTextNodeIndex(parent);}+KoSvgTextNodeIndex KoSvgTextShape::nodeForTextPath(KoShape *textPath) const+{+ auto root = d->textData.childBegin();+ if (d->isLoading || d->cursorPos.isEmpty()) return d->createTextNodeIndex(root);++ for (auto child = childBegin(root); child != childEnd(root); child++) {+ if (child->textPathId == textPath->name()) {+ return d->createTextNodeIndex(child);+ }+ }+ return d->createTextNodeIndex(root);+}+KoSvgTextProperties KoSvgTextShape::textProperties() const{return KisForestDetail::size(d->textData)? d->textData.childBegin()->properties: KoSvgTextProperties();@@ -1822,8 +1831,7 @@ KoSvgTextShapeMementoSP KoSvgTextShape::getMemento()d->logicalToVisualCursorPos,d->plainText,d->isBidi,- d->initialTextPosition,- d->textPaths));+ d->initialTextPosition));}void KoSvgTextShape::setMementoImpl(const KoSvgTextShapeMementoSP memento)@@ -1843,14 +1851,14 @@ void KoSvgTextShape::setMementoImpl(const KoSvgTextShapeMementoSP memento)d->isBidi = impl->isBidi;d->initialTextPosition = impl->initialTextPosition;- // This is a problem :|- d->textPaths = impl->textPaths;- Q_FOREACH(KoShape * shape, d->textPaths) {- if (!shape->hasDependee(this)) {- shape->addDependee(this);- }- if (!d->shapeGroup->shapes().contains(shape)) {- d->shapeGroup->addShape(shape);+ // Ensure that any text paths exist.+ auto root = d->textData.childBegin();+ for (auto child = childBegin(root); child != childEnd(root); child++) {+ if (!child->textPathId.isEmpty()) {+ KIS_SAFE_ASSERT_RECOVER(Private::textPathByName(child->textPathId, d->textPaths)) {+ qDebug() << "missing path is" << child->textPathId;+ child->textPathId = QString();+ }}}}@@ -2334,22 +2342,7 @@ bool KoSvgTextShape::setTextPathOnRange(KoShape *textPath, const int startPos, cPrivate::splitTree(d->textData, endIndex, true);int currentIndex = 0;- bool textPathNameUnique = false;- int textPathNumber = d->textPaths.size();- while(!textPathNameUnique) {- QString newTextPathName("textPath"+QString::number(textPathNumber));- textPathNameUnique = true;- Q_FOREACH(KoShape *shape, d->textPaths) {- if (shape->name() == newTextPathName) {- textPathNameUnique = false;- textPathNumber += 1;- break;- }- }- if (textPathNameUnique) {- textPath->setName(newTextPathName);- }- }+ Private::makeTextPathNameUnique(d->textPaths, textPath);KoSvgTextContentElement textPathElement;textPathElement.textPathId = textPath->name();d->shapeGroup->addShape(textPath);@@ -2418,6 +2411,48 @@ bool KoSvgTextShape::setTextPathOnRange(KoShape *textPath, const int startPos, creturn true;}+QList<KoShape *> KoSvgTextShape::textPathsAtRange(const int startPos, const int endPos)+{+ const int finalPos = d->cursorPos.size() - 1;+ const int startIndex = (startPos == endPos && startPos < 0)? 0: d->cursorPos.at(qBound(0, startPos, finalPos)).index;+ const int endIndex = (startPos == endPos && startPos < 0)? finalPos: d->cursorPos.at(qBound(0, endPos, finalPos)).index;+ QList<KoShape*> textPaths;+ int currentIndex = 0;+ auto startElement = Private::findTextContentElementForIndex(d->textData, currentIndex, startIndex, true);+ currentIndex = 0;+ auto endElement = Private::findTextContentElementForIndex(d->textData, currentIndex, endIndex, true);++ auto first = startElement.node()? Private::findTopLevelParent(d->textData.childBegin(), KisForestDetail::siblingCurrent(startElement))+ : childEnd(d->textData.childBegin());+ auto last = endElement.node()? Private::findTopLevelParent(d->textData.childBegin(), KisForestDetail::siblingCurrent(endElement))+ : childEnd(d->textData.childBegin());+ if (last != childEnd(d->textData.childBegin())) {+ last++;+ }+ for (auto child = first; (child != last && child != KisForestDetail::siblingEnd(first)); child++) {+ if (KoShape *path = Private::textPathByName(child->textPathId, d->textPaths)) {+ textPaths.append(path);+ }+ }+ return textPaths;+}++void KoSvgTextShape::addTextPathAtEnd(KoShape *textPath)+{+ auto root = d->textData.childBegin();+ if (root == d->textData.childEnd()) return;+ if (d->textPaths.contains(textPath)) return;++ Private::makeTextPathNameUnique(d->textPaths, textPath);+ KoSvgTextContentElement textPathElement;+ textPathElement.textPathId = textPath->name();+ d->shapeGroup->addShape(textPath);+ d->textPaths.append(textPath);+ textPath->addDependee(this);++ d->textData.insert(childEnd(root), textPathElement);+}+QList<KoShape *> KoSvgTextShape::shapesInside() const{return d->shapesInside;diff --git a/libs/flake/text/KoSvgTextShape.h b/libs/flake/text/KoSvgTextShape.hindex cec65af02a..90beadc2e4 100644--- a/libs/flake/text/KoSvgTextShape.h+++ b/libs/flake/text/KoSvgTextShape.h@@ -145,6 +145,23 @@ public:*/bool setTextPathOnRange(KoShape *textPath, const int startPos = -1, const int endPos = -1);+ /**+ * @brief textPathsAtRange+ * Get a list of textPaths at the given range. This includes textPaths whose+ * node is only partially overlapped by the range.+ * @param posStart -- Start of the range, as cursor pos.+ * @param posEnd -- End of the range, as cursor pos.+ * @return list of KoShapes that are text paths.+ */+ QList<KoShape*> textPathsAtRange(const int startPos = -1, const int endPos = -1);++ /**+ * @brief addTextPathAtEnd+ * add a textpath node at the end of the text.+ * @param textPath -- text path to add.+ */+ void addTextPathAtEnd(KoShape *textPath);+/*** @brief shapesInside* @return the list of shapes that make up the content area.diff --git a/libs/flake/text/KoSvgTextShape_p.h b/libs/flake/text/KoSvgTextShape_p.hindex c878e544b1..10363c5045 100644--- a/libs/flake/text/KoSvgTextShape_p.h+++ b/libs/flake/text/KoSvgTextShape_p.h@@ -1342,6 +1342,26 @@ public:}}}++ static void makeTextPathNameUnique(QList<KoShape*> textPaths, KoShape *textPath) {+ bool textPathNameUnique = false;+ int textPathNumber = textPaths.size();+ QString newTextPathName = textPath->name();+ while(!textPathNameUnique) {+ textPathNameUnique = true;+ Q_FOREACH(KoShape *shape, textPaths) {+ if (shape->name() == newTextPathName) {+ textPathNameUnique = false;+ textPathNumber += 1;+ break;+ }+ }+ if (textPathNameUnique) {+ textPath->setName(newTextPathName);+ }+ newTextPathName = QString("textPath"+QString::number(textPathNumber));+ }+ }};#endif // KO_SVG_TEXT_SHAPE_P_H