Changeset View
Changeset View
Standalone View
Standalone View
commands/tools/selection/text/kpToolTextDeleteCommand.cpp
Show All 39 Lines | 39 | kpToolTextDeleteCommand::kpToolTextDeleteCommand (const QString &name, | |||
---|---|---|---|---|---|
40 | int row, int col, Action action, | 40 | int row, int col, Action action, | ||
41 | kpCommandEnvironment *environ) | 41 | kpCommandEnvironment *environ) | ||
42 | : kpNamedCommand (name, environ), | 42 | : kpNamedCommand (name, environ), | ||
43 | m_row (row), m_col (col), | 43 | m_row (row), m_col (col), | ||
44 | m_numDeletes (0) | 44 | m_numDeletes (0) | ||
45 | { | 45 | { | ||
46 | viewManager ()->setTextCursorPosition (m_row, m_col); | 46 | viewManager ()->setTextCursorPosition (m_row, m_col); | ||
47 | 47 | | |||
48 | if (action == AddDeleteNow) | 48 | if (action == AddDeleteNow) { | ||
49 | addDelete (); | 49 | addDelete (); | ||
50 | } | 50 | } | ||
51 | | ||||
52 | kpToolTextDeleteCommand::~kpToolTextDeleteCommand () | | |||
53 | { | | |||
54 | } | 51 | } | ||
55 | 52 | | |||
53 | kpToolTextDeleteCommand::~kpToolTextDeleteCommand () = default; | ||||
54 | | ||||
56 | 55 | | |||
57 | // public | 56 | // public | ||
58 | void kpToolTextDeleteCommand::addDelete () | 57 | void kpToolTextDeleteCommand::addDelete () | ||
59 | { | 58 | { | ||
60 | QList <QString> textLines = textSelection ()->textLines (); | 59 | QList <QString> textLines = textSelection ()->textLines (); | ||
61 | 60 | | |||
62 | if (m_col < static_cast<int> (textLines [m_row].length ())) | 61 | if (m_col < static_cast<int> (textLines [m_row].length ())) | ||
63 | { | 62 | { | ||
Show All 33 Lines | |||||
97 | void kpToolTextDeleteCommand::execute () | 96 | void kpToolTextDeleteCommand::execute () | ||
98 | { | 97 | { | ||
99 | viewManager ()->setTextCursorPosition (m_row, m_col); | 98 | viewManager ()->setTextCursorPosition (m_row, m_col); | ||
100 | 99 | | |||
101 | m_deletedText.clear (); | 100 | m_deletedText.clear (); | ||
102 | int oldNumDeletes = m_numDeletes; | 101 | int oldNumDeletes = m_numDeletes; | ||
103 | m_numDeletes = 0; | 102 | m_numDeletes = 0; | ||
104 | 103 | | |||
105 | for (int i = 0; i < oldNumDeletes; i++) | 104 | for (int i = 0; i < oldNumDeletes; i++) { | ||
106 | addDelete (); | 105 | addDelete (); | ||
107 | } | 106 | } | ||
107 | } | ||||
108 | 108 | | |||
109 | // public virtual [base kpCommand] | 109 | // public virtual [base kpCommand] | ||
110 | void kpToolTextDeleteCommand::unexecute () | 110 | void kpToolTextDeleteCommand::unexecute () | ||
111 | { | 111 | { | ||
112 | viewManager ()->setTextCursorPosition (m_row, m_col); | 112 | viewManager ()->setTextCursorPosition (m_row, m_col); | ||
113 | 113 | | |||
114 | QList <QString> textLines = textSelection ()->textLines (); | 114 | QList <QString> textLines = textSelection ()->textLines (); | ||
115 | 115 | | |||
116 | for (int i = 0; i < static_cast<int> (m_deletedText.length ()); i++) | 116 | for (auto && i : m_deletedText) | ||
117 | { | 117 | { | ||
118 | if (m_deletedText [i] == '\n') | 118 | if (i == '\n') | ||
119 | { | 119 | { | ||
120 | const QString rightHalf = textLines [m_row].mid (m_col); | 120 | const QString rightHalf = textLines [m_row].mid (m_col); | ||
121 | 121 | | |||
122 | textLines [m_row].truncate (m_col); | 122 | textLines [m_row].truncate (m_col); | ||
123 | textLines.insert (textLines.begin () + m_row + 1, rightHalf); | 123 | textLines.insert (textLines.begin () + m_row + 1, rightHalf); | ||
124 | } | 124 | } | ||
125 | else | 125 | else | ||
126 | { | 126 | { | ||
127 | const QString leftHalf = textLines [m_row].left (m_col); | 127 | const QString leftHalf = textLines [m_row].left (m_col); | ||
128 | const QString rightHalf = textLines [m_row].mid (m_col); | 128 | const QString rightHalf = textLines [m_row].mid (m_col); | ||
129 | 129 | | |||
130 | textLines [m_row] = leftHalf + m_deletedText [i] + rightHalf; | 130 | textLines [m_row] = leftHalf + i + rightHalf; | ||
131 | } | 131 | } | ||
132 | } | 132 | } | ||
133 | 133 | | |||
134 | m_deletedText.clear (); | 134 | m_deletedText.clear (); | ||
135 | 135 | | |||
136 | textSelection ()->setTextLines (textLines); | 136 | textSelection ()->setTextLines (textLines); | ||
137 | 137 | | |||
138 | viewManager ()->setTextCursorPosition (m_row, m_col); | 138 | viewManager ()->setTextCursorPosition (m_row, m_col); | ||
139 | } | 139 | } | ||
140 | 140 | |