Krita Source Code Documentation
Loading...
Searching...
No Matches
SvgTextInsertCommand.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
7#include "KoSvgTextShape.h"
10
11#include <QRegularExpression>
12
13#include "kis_command_ids.h"
14SvgTextInsertCommand::SvgTextInsertCommand(KoSvgTextShape *shape, int pos, int anchor, QString text, KUndo2Command *parent)
15 : KUndo2Command(parent)
16 , m_shape(shape)
17 , m_pos(pos)
18 , m_anchor(anchor)
19 , m_text(text)
20{
21 setText(kundo2_i18n("Insert Text"));
22
24}
25
27{
28 QRegularExpression exp;
29 // This replaces...
30 // - carriage return
31 // - carriage return-linefeed
32 // - line separator
33 // - paragraph separator
34 // - vertical tab/line tab
35 // with a single linefeed to avoid them from being added to the textShape.
36 // NOTE: \r\n comes **before** \r to ensure that full match
37 // is preferred to the partial match!
38 exp.setPattern("(\\r\\n|\\r|\\x{2029}|\\x{2028}|\\x{000b})");
39 text.replace(exp, "\n");
40 return text;
41}
42
44{
46 // Index defaults to -1 when there's no text in the shape.
47 int oldIndex = qMax(0, m_shape->indexForPos(m_pos));
48
53
54 int pos = m_shape->posForIndex(oldIndex + m_text.size(), false, false);
56
57}
58
65
70
72{
73 const SvgTextInsertCommand *command = dynamic_cast<const SvgTextInsertCommand*>(other);
74
75
76 if (!command || command->m_shape != m_shape) {
77 return false;
78 }
79 int oldIndex = m_shape->indexForPos(m_pos);
80 int otherOldIndex = m_shape->indexForPos(command->m_pos);
81 if (oldIndex + m_text.size() != otherOldIndex) {
82 return false;
83 }
84
85 m_text += command->m_text;
86 return true;
87}
void setText(const KUndo2MagicString &text)
KUndo2MagicString text() const
static void bulkShapesUpdate(const UpdatesList &updates)
void notifyCursorPosChanged(int pos, int anchor)
Notify that the cursor position has changed.
KoSvgTextShapeMementoSP getMemento()
Get a memento holding the current textdata and layout info.
void setMemento(const KoSvgTextShapeMementoSP memento)
Set the text data and layout info, reset listening cursors to 0.
int indexForPos(int pos) const
indexForPos get the string index for a given cursor position.
void cleanUp()
Cleans up the internal text data. Used by undo commands.
int posForIndex(int index, bool firstIndex=false, bool skipSynthetic=false) const
posForIndex Get the cursor position for a given index in a string.
bool insertText(int pos, QString text)
insertText Insert a text somewhere in the KoTextShape.
SvgTextInsertCommand(KoSvgTextShape *shape, int pos, int anchor, QString text, KUndo2Command *parent=0)
int id() const override
KoSvgTextShapeMementoSP m_textData
bool mergeWith(const KUndo2Command *other) override
static QString filterInputUnicodeString(QString text)
KUndo2MagicString kundo2_i18n(const char *text)