Skip to content

armgit / krita

Public Imported from GitHub
0

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++

Improve property handling of insert rich text into textshape functionality.

Wolthera van Hövell tot Westerflier9e6d1f3ab4ce
diff --git a/libs/flake/text/KoSvgTextShape.cpp b/libs/flake/text/KoSvgTextShape.cpp
index 5d0c1007d5..e8a9e618d3 100644
--- a/libs/flake/text/KoSvgTextShape.cpp
+++ b/libs/flake/text/KoSvgTextShape.cpp
@@ -1144,7 +1144,7 @@ std::unique_ptr<KoSvgTextShape> KoSvgTextShape::copyRange(int index, int length)
return std::unique_ptr<KoSvgTextShape>(clone);
}
-bool KoSvgTextShape::insertRichText(int pos, const KoSvgTextShape *richText)
+bool KoSvgTextShape::insertRichText(int pos, const KoSvgTextShape *richText, bool inheritPropertiesIfPossible)
{
bool success = false;
int currentIndex = 0;
@@ -1167,18 +1167,37 @@ bool KoSvgTextShape::insertRichText(int pos, const KoSvgTextShape *richText)
KoSvgTextShape::Private::splitContentElement(this->d->textData, insertionIndex);
auto it = d->findTextContentElementForIndex(d->textData, currentIndex, insertionIndex);
+ auto richTextIt = d->textData.childEnd();
if (it != d->textData.depthFirstTailEnd()) {
- d->textData.move(richText->d->textData.childBegin(), siblingCurrent(it));
+ richTextIt = d->textData.move(richText->d->textData.childBegin(), siblingCurrent(it));
success = true;
} else {
currentIndex = 0;
it = d->findTextContentElementForIndex(d->textData, currentIndex, elementIndex);
if (it != d->textData.depthFirstTailEnd()) {
- d->textData.move(richText->d->textData.childBegin(), siblingEnd(siblingCurrent(it)));
+ richTextIt = d->textData.move(richText->d->textData.childBegin(), siblingEnd(siblingCurrent(it)));
success = true;
}
}
+ if (richTextIt != d->textData.childEnd()) {
+ for (auto p = richTextIt->properties.properties().begin(); p != richTextIt->properties.properties().end(); p++) {
+ if (KoSvgTextProperties::propertyIsBlockOnly(*p)) {
+ richTextIt->properties.removeProperty(*p);
+ }
+ }
+ auto parentIt = KisForestDetail::hierarchyBegin(richTextIt);
+ auto parentEnd = KisForestDetail::hierarchyEnd(richTextIt);
+ parentIt++;
+ if (inheritPropertiesIfPossible && parentIt!= parentEnd) {
+ for (auto p = richTextIt->properties.properties().begin(); p != richTextIt->properties.properties().end(); p++) {
+ if (richTextIt->properties.inheritsProperty(*p, parentIt->properties)) {
+ richTextIt->properties.removeProperty(*p);
+ }
+ }
+ }
+ }
+
if (success) {
notifyChanged();
shapeChangedPriv(ContentChanged);
diff --git a/libs/flake/text/KoSvgTextShape.h b/libs/flake/text/KoSvgTextShape.h
index 9e31e80d19..93dc575cf9 100644
--- a/libs/flake/text/KoSvgTextShape.h
+++ b/libs/flake/text/KoSvgTextShape.h
@@ -454,10 +454,11 @@ public:
* @brief insertRichText
* Insert rich text at the given cursor pos. This will first split contents at the given pos before inserting the new rich text.
* @param pos -- cursor pos to insert at.
- * @param richText -- KoSvgTextShape with rich text data. TODO: make this const once KisForest constness issues have been fixed.
+ * @param richText -- KoSvgTextShape with rich text data.
+ * @param inheritPropertiesIfPossible -- toggle whether to inherit rich text if possible.
* @return whether insertion happened successfully.
*/
- bool insertRichText(int pos, const KoSvgTextShape *richText);
+ bool insertRichText(int pos, const KoSvgTextShape *richText, bool inheritPropertiesIfPossible = false);
/// Cleans up the internal text data. Used by undo commands.
void cleanUp();
diff --git a/plugins/tools/svgtexttool/SvgTextCursor.cpp b/plugins/tools/svgtexttool/SvgTextCursor.cpp
index d2b7dfc849..7f76e0bc01 100644
--- a/plugins/tools/svgtexttool/SvgTextCursor.cpp
+++ b/plugins/tools/svgtexttool/SvgTextCursor.cpp
@@ -597,7 +597,7 @@ void SvgTextCursor::insertText(QString text)
}
}
-void SvgTextCursor::insertRichText(KoSvgTextShape *insert)
+void SvgTextCursor::insertRichText(KoSvgTextShape *insert, bool inheritPropertiesIfPossible)
{
if (d->shape) {
//KUndo2Command *parentCmd = new KUndo2Command;
diff --git a/plugins/tools/svgtexttool/SvgTextCursor.h b/plugins/tools/svgtexttool/SvgTextCursor.h
index 78be096c8e..455e2b86d8 100644
--- a/plugins/tools/svgtexttool/SvgTextCursor.h
+++ b/plugins/tools/svgtexttool/SvgTextCursor.h
@@ -182,7 +182,7 @@ public:
void insertText(QString text);
/// Insert rich text at getPos();
- void insertRichText(KoSvgTextShape *insert);
+ void insertRichText(KoSvgTextShape *insert, bool inheritPropertiesIfPossible = false);
/**
* @brief removeText
diff --git a/plugins/tools/svgtexttool/SvgTextInsertRichCommand.cpp b/plugins/tools/svgtexttool/SvgTextInsertRichCommand.cpp
index c39c0b52e9..b16379d4f1 100644
--- a/plugins/tools/svgtexttool/SvgTextInsertRichCommand.cpp
+++ b/plugins/tools/svgtexttool/SvgTextInsertRichCommand.cpp
@@ -7,12 +7,13 @@
#include "KoSvgTextShapeMarkupConverter.h"
#include <KoShapeBulkActionLock.h>
-SvgTextInsertRichCommand::SvgTextInsertRichCommand(KoSvgTextShape *shape, KoSvgTextShape *insert, int pos, int anchor, KUndo2Command *parent)
+SvgTextInsertRichCommand::SvgTextInsertRichCommand(KoSvgTextShape *shape, KoSvgTextShape *insert, int pos, int anchor, bool inheritPropertiesIfPossible, KUndo2Command *parent)
: KUndo2Command(parent)
, m_shape(shape)
, m_insert(insert)
, m_pos(pos)
, m_anchor(anchor)
+ , m_inheritPropertiesIfPossible(inheritPropertiesIfPossible)
{
setText(kundo2_i18n("Insert Rich Text"));
}
@@ -24,7 +25,7 @@ void SvgTextInsertRichCommand::redo()
int oldIndex = qMax(0, m_shape->indexForPos(m_pos));
m_textData = m_shape->getMemento();
- m_shape->insertRichText(m_pos, m_insert);
+ m_shape->insertRichText(m_pos, m_insert, m_inheritPropertiesIfPossible);
m_shape->cleanUp();
KoShapeBulkActionLock::bulkShapesUpdate(lock.unlock());
diff --git a/plugins/tools/svgtexttool/SvgTextInsertRichCommand.h b/plugins/tools/svgtexttool/SvgTextInsertRichCommand.h
index d7e239fad8..deef6e0647 100644
--- a/plugins/tools/svgtexttool/SvgTextInsertRichCommand.h
+++ b/plugins/tools/svgtexttool/SvgTextInsertRichCommand.h
@@ -13,7 +13,7 @@
class KRITATOOLSVGTEXT_EXPORT SvgTextInsertRichCommand : public KUndo2Command
{
public:
- SvgTextInsertRichCommand(KoSvgTextShape *shape, KoSvgTextShape *insert, int pos, int anchor, KUndo2Command *parent = 0);
+ SvgTextInsertRichCommand(KoSvgTextShape *shape, KoSvgTextShape *insert, int pos, int anchor, bool inheritPropertiesIfPossible = false, KUndo2Command *parent = 0);
~SvgTextInsertRichCommand() override = default;
void redo() override;
@@ -29,6 +29,7 @@ private:
int m_pos;
int m_anchor;
+ bool m_inheritPropertiesIfPossible;
KoSvgTextShapeMementoSP m_textData;
};
diff --git a/plugins/tools/svgtexttool/SvgTextTool.cpp b/plugins/tools/svgtexttool/SvgTextTool.cpp
index 15381afc2c..673252a296 100644
--- a/plugins/tools/svgtexttool/SvgTextTool.cpp
+++ b/plugins/tools/svgtexttool/SvgTextTool.cpp
@@ -378,7 +378,7 @@ void SvgTextTool::insertRichText(KoSvgTextShape *richText, bool replaceLastGlyph
m_textCursor.setPos(m_textCursor.getPos(), m_textCursor.getPos());
m_textCursor.moveCursor(m_textCursor.getPos() == 0? SvgTextCursor::MoveNextChar : SvgTextCursor::MovePreviousChar, false);
}
- m_textCursor.insertRichText(richText);
+ m_textCursor.insertRichText(richText, true);
}
void SvgTextTool::slotTextEditorClosed()