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"
9
10#include <QRegExp>
11
12#include "kis_command_ids.h"
13SvgTextInsertCommand::SvgTextInsertCommand(KoSvgTextShape *shape, int pos, int anchor, QString text, KUndo2Command *parent)
14 : KUndo2Command(parent)
15 , m_shape(shape)
16 , m_pos(pos)
17 , m_anchor(anchor)
18 , m_text(text)
19{
20 setText(kundo2_i18n("Insert Text"));
21
22 QRegExp exp;
23 // This replaces...
24 // - carriage return
25 // - linefeed-carriage return
26 // - carriage return-linefeed
27 // - line separator
28 // - paragraph separator
29 // - vertical tab/line tab
30 // with a single linefeed to avoid them from being added to the textShape.
31 exp.setPattern("[\\r|\\r\\n|\\x2029|\\x2028\\x000b]");
32
33#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
34 text.replace(exp, "\n");
35#else
36 exp.replaceIn(text, "\n");
37#endif
38 m_text = text;
39}
40
42{
43 QRectF updateRect = m_shape->boundingRect();
44 // Index defaults to -1 when there's no text in the shape.
45 int oldIndex = qMax(0, m_shape->indexForPos(m_pos));
46
51
52 int pos = m_shape->posForIndex(oldIndex + m_text.size(), false, false);
54
55}
56
63
68
70{
71 const SvgTextInsertCommand *command = dynamic_cast<const SvgTextInsertCommand*>(other);
72
73
74 if (!command || command->m_shape != m_shape) {
75 return false;
76 }
77 int oldIndex = m_shape->indexForPos(m_pos);
78 int otherOldIndex = m_shape->indexForPos(command->m_pos);
79 if (oldIndex + m_text.size() != otherOldIndex) {
80 return false;
81 }
82
83 m_text += command->m_text;
84 return true;
85}
void setText(const KUndo2MagicString &text)
KUndo2MagicString text() const
virtual void updateAbsolute(const QRectF &rect) const
Definition KoShape.cpp:616
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.
QRectF boundingRect() const override
Get the bounding box of the shape.
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
KUndo2MagicString kundo2_i18n(const char *text)