Changeset View
Changeset View
Standalone View
Standalone View
commands/tools/selection/text/kpToolTextBackspaceCommand.cpp
Show All 39 Lines | 39 | kpToolTextBackspaceCommand::kpToolTextBackspaceCommand (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_numBackspaces (0) | 44 | m_numBackspaces (0) | ||
45 | { | 45 | { | ||
46 | viewManager ()->setTextCursorPosition (m_row, m_col); | 46 | viewManager ()->setTextCursorPosition (m_row, m_col); | ||
47 | 47 | | |||
48 | if (action == AddBackspaceNow) | 48 | if (action == AddBackspaceNow) { | ||
49 | addBackspace (); | 49 | addBackspace (); | ||
50 | } | 50 | } | ||
51 | | ||||
52 | kpToolTextBackspaceCommand::~kpToolTextBackspaceCommand () | | |||
53 | { | | |||
54 | } | 51 | } | ||
55 | 52 | | |||
53 | kpToolTextBackspaceCommand::~kpToolTextBackspaceCommand () = default; | ||||
54 | | ||||
56 | 55 | | |||
57 | // public | 56 | // public | ||
58 | void kpToolTextBackspaceCommand::addBackspace () | 57 | void kpToolTextBackspaceCommand::addBackspace () | ||
59 | { | 58 | { | ||
60 | QList <QString> textLines = textSelection ()->textLines (); | 59 | QList <QString> textLines = textSelection ()->textLines (); | ||
61 | 60 | | |||
62 | if (m_col > 0) | 61 | if (m_col > 0) | ||
63 | { | 62 | { | ||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Line(s) | |||||
105 | void kpToolTextBackspaceCommand::execute () | 104 | void kpToolTextBackspaceCommand::execute () | ||
106 | { | 105 | { | ||
107 | viewManager ()->setTextCursorPosition (m_row, m_col); | 106 | viewManager ()->setTextCursorPosition (m_row, m_col); | ||
108 | 107 | | |||
109 | m_deletedText.clear (); | 108 | m_deletedText.clear (); | ||
110 | int oldNumBackspaces = m_numBackspaces; | 109 | int oldNumBackspaces = m_numBackspaces; | ||
111 | m_numBackspaces = 0; | 110 | m_numBackspaces = 0; | ||
112 | 111 | | |||
113 | for (int i = 0; i < oldNumBackspaces; i++) | 112 | for (int i = 0; i < oldNumBackspaces; i++) { | ||
114 | addBackspace (); | 113 | addBackspace (); | ||
115 | } | 114 | } | ||
115 | } | ||||
116 | 116 | | |||
117 | // public virtual [base kpCommand] | 117 | // public virtual [base kpCommand] | ||
118 | void kpToolTextBackspaceCommand::unexecute () | 118 | void kpToolTextBackspaceCommand::unexecute () | ||
119 | { | 119 | { | ||
120 | viewManager ()->setTextCursorPosition (m_row, m_col); | 120 | viewManager ()->setTextCursorPosition (m_row, m_col); | ||
121 | 121 | | |||
122 | QList <QString> textLines = textSelection ()->textLines (); | 122 | QList <QString> textLines = textSelection ()->textLines (); | ||
123 | 123 | | |||
124 | for (int i = 0; i < static_cast<int> (m_deletedText.length ()); i++) | 124 | for (auto && i : m_deletedText) | ||
125 | { | 125 | { | ||
126 | if (m_deletedText [i] == '\n') | 126 | if (i == '\n') | ||
127 | { | 127 | { | ||
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].truncate (m_col); | 130 | textLines [m_row].truncate (m_col); | ||
131 | textLines.insert (textLines.begin () + m_row + 1, rightHalf); | 131 | textLines.insert (textLines.begin () + m_row + 1, rightHalf); | ||
132 | 132 | | |||
133 | m_row++; | 133 | m_row++; | ||
134 | m_col = 0; | 134 | m_col = 0; | ||
135 | } | 135 | } | ||
136 | else | 136 | else | ||
137 | { | 137 | { | ||
138 | const QString leftHalf = textLines [m_row].left (m_col); | 138 | const QString leftHalf = textLines [m_row].left (m_col); | ||
139 | const QString rightHalf = textLines [m_row].mid (m_col); | 139 | const QString rightHalf = textLines [m_row].mid (m_col); | ||
140 | 140 | | |||
141 | textLines [m_row] = leftHalf + m_deletedText [i] + rightHalf; | 141 | textLines [m_row] = leftHalf + i + rightHalf; | ||
142 | m_col++; | 142 | m_col++; | ||
143 | } | 143 | } | ||
144 | } | 144 | } | ||
145 | 145 | | |||
146 | m_deletedText.clear (); | 146 | m_deletedText.clear (); | ||
147 | 147 | | |||
148 | textSelection ()->setTextLines (textLines); | 148 | textSelection ()->setTextLines (textLines); | ||
149 | 149 | | |||
150 | viewManager ()->setTextCursorPosition (m_row, m_col); | 150 | viewManager ()->setTextCursorPosition (m_row, m_col); | ||
151 | } | 151 | } | ||
152 | 152 | |