| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2021 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of Qt Creator. |
| 7 | ** |
| 8 | ** Commercial License Usage |
| 9 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 10 | ** accordance with the commercial license agreement provided with the |
| 11 | ** Software or, alternatively, in accordance with the terms contained in |
| 12 | ** a written agreement between you and The Qt Company. For licensing terms |
| 13 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 14 | ** information use the contact form at https://www.qt.io/contact-us. |
| 15 | ** |
| 16 | ** GNU General Public License Usage |
| 17 | ** Alternatively, this file may be used under the terms of the GNU |
| 18 | ** General Public License version 3 as published by the Free Software |
| 19 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 20 | ** included in the packaging of this file. Please review the following |
| 21 | ** information to ensure the GNU General Public License requirements will |
| 22 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 23 | ** |
| 24 | ****************************************************************************/ |
| 25 | |
| 26 | #include "multitextcursor.h" |
| 27 | #include <QKeyEvent> |
| 28 | #include <QTextBlock> |
| 29 | |
| 30 | MultiTextCursor::MultiTextCursor() {} |
| 31 | |
| 32 | MultiTextCursor::MultiTextCursor(const QList<QTextCursor> &cursors) : m_cursors(cursors) { |
| 33 | mergeCursors(); |
| 34 | } |
| 35 | |
| 36 | MultiTextCursor::MultiTextCursor(const QTextCursor &cursor) : m_cursors(){ |
| 37 | m_cursors.append(cursor); |
| 38 | } |
| 39 | |
| 40 | void MultiTextCursor::addCursor(const QTextCursor &cursor) { |
| 41 | //QTC_ASSERT(!cursor.isNull(), return); |
| 42 | m_cursors.append(cursor); |
| 43 | mergeCursors(); |
| 44 | } |
| 45 | |
| 46 | void MultiTextCursor::setCursors(const QList<QTextCursor> &cursors) { |
| 47 | m_cursors = cursors; |
| 48 | mergeCursors(); |
| 49 | } |
| 50 | |
| 51 | bool MultiTextCursor::aCursorInThisBlock(int blocknumber){ |
| 52 | for (int i=0; i<m_cursors.size(); i++){ |
| 53 | if (m_cursors.at(i).blockNumber()==blocknumber) return true; |
| 54 | } |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | void MultiTextCursor::emplace(MultiTextCursor mc){ |
| 59 | m_cursors.clear(); |
| 60 | m_cursors=mc.m_cursors; |
| 61 | mergeCursors(); |
| 62 | } |
| 63 | |
| 64 | const QList<QTextCursor> MultiTextCursor::cursors() const { |
| 65 | return m_cursors; |
| 66 | } |
| 67 | |
| 68 | void MultiTextCursor::replaceMainCursor(const QTextCursor &cursor) { |
| 69 | //QTC_ASSERT(!cursor.isNull(), return); |
| 70 | takeMainCursor(); |
| 71 | addCursor(cursor); |
| 72 | } |
| 73 | |
| 74 | QTextCursor MultiTextCursor::mainCursor() const { |
| 75 | //if (m_cursors.isEmpty()) return {}; |
| 76 | return m_cursors.last(); |
| 77 | } |
| 78 | |
| 79 | QTextCursor MultiTextCursor::takeMainCursor() { |
| 80 | //if (m_cursors.isEmpty()) return {}; |
| 81 | return m_cursors.takeLast(); |
| 82 | } |
| 83 | |
| 84 | void MultiTextCursor::beginEditBlock() { |
| 85 | //QTC_ASSERT(!m_cursors.empty(), return); |
| 86 | m_cursors.last().beginEditBlock(); |
| 87 | } |
| 88 | |
| 89 | void MultiTextCursor::endEditBlock() { |
| 90 | //QTC_ASSERT(!m_cursors.empty(), return); |
| 91 | m_cursors.last().endEditBlock(); |
| 92 | } |
| 93 | |
| 94 | bool MultiTextCursor::isNull() const { |
| 95 | return m_cursors.isEmpty(); |
| 96 | } |
| 97 | |
| 98 | bool MultiTextCursor::hasMultipleCursors() const { |
| 99 | return m_cursors.size() > 1; |
| 100 | } |
| 101 | |
| 102 | int MultiTextCursor::cursorCount() const { |
| 103 | return m_cursors.size(); |
| 104 | } |
| 105 | |
| 106 | void MultiTextCursor::movePosition(QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode, int n) { |
| 107 | //for (QTextCursor &cursor : m_cursors) cursor.movePosition(operation, mode, n); |
| 108 | for (int i=0; i<m_cursors.size(); i++) m_cursors[i].movePosition(operation, mode, n); |
| 109 | mergeCursors(); |
| 110 | } |
| 111 | |
| 112 | bool MultiTextCursor::hasSelection() const { |
| 113 | for (int i=0; i<m_cursors.size(); i++) |
| 114 | if (m_cursors.at(i).hasSelection()) return true; |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | void MultiTextCursor::clearSelection(){ |
| 119 | for (int i=0; i<m_cursors.size(); i++) m_cursors[i].clearSelection(); |
| 120 | } |
| 121 | |
| 122 | QString MultiTextCursor::selectedText() const { |
| 123 | QString text; |
| 124 | QList<QTextCursor> cursors = m_cursors; |
| 125 | #if (QT_VERSION >= 0x050000) |
| 126 | std::sort(cursors.begin(),cursors.end()); |
| 127 | #else |
| 128 | qSort(cursors); |
| 129 | #endif |
| 130 | //for (const QTextCursor &cursor : cursors) { |
| 131 | for (int i=0; i<cursors.size(); i++) { |
| 132 | const QString &cursorText = cursors[i].selectedText(); |
| 133 | if (cursorText.isEmpty()) |
| 134 | continue; |
| 135 | if (!text.isEmpty()) { |
| 136 | if (text.endsWith(QChar::ParagraphSeparator)) |
| 137 | text.chop(1); |
| 138 | text.append('\n'); |
| 139 | } |
| 140 | text.append(cursorText); |
| 141 | } |
| 142 | return text; |
| 143 | } |
| 144 | |
| 145 | void MultiTextCursor::removeSelectedText() { |
| 146 | beginEditBlock(); |
| 147 | //for (QTextCursor &c : m_cursors) |
| 148 | for (int i=0; i<m_cursors.size(); i++) |
| 149 | m_cursors[i].removeSelectedText(); |
| 150 | endEditBlock(); |
| 151 | mergeCursors(); |
| 152 | } |
| 153 | |
| 154 | static void insertAndSelect(QTextCursor &cursor, const QString &text, bool selectNewText) { |
| 155 | if (selectNewText) { |
| 156 | const int anchor = cursor.position(); |
| 157 | cursor.insertText(text); |
| 158 | const int pos = cursor.position(); |
| 159 | cursor.setPosition(anchor); |
| 160 | cursor.setPosition(pos, QTextCursor::KeepAnchor); |
| 161 | } else { |
| 162 | cursor.insertText(text); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void MultiTextCursor::insertText(const QString &text, bool selectNewText) { |
| 167 | if (m_cursors.isEmpty()) |
| 168 | return; |
| 169 | m_cursors.last().beginEditBlock(); |
| 170 | if (hasMultipleCursors()) { |
| 171 | QStringList lines = text.split('\n'); |
| 172 | if (!lines.isEmpty() && lines.last().isEmpty()) |
| 173 | lines.pop_back(); |
| 174 | int index = 0; |
| 175 | if (lines.count() == m_cursors.count()) { |
| 176 | QList<QTextCursor> cursors = m_cursors; |
| 177 | #if (QT_VERSION >= 0x050000) |
| 178 | std::sort(cursors.begin(),cursors.end()); |
| 179 | #else |
| 180 | qSort(cursors); |
| 181 | #endif |
| 182 | //Util::sort(cursors); |
| 183 | //for (QTextCursor &cursor : cursors) |
| 184 | for (int i=0; i<cursors.size(); i++) |
| 185 | insertAndSelect(cursors[i], lines.at(index++), selectNewText); |
| 186 | m_cursors.last().endEditBlock(); |
| 187 | return; |
| 188 | } |
| 189 | } |
| 190 | //for (QTextCursor &cursor : m_cursors) |
| 191 | for(int i=0; i<m_cursors.size();i++) |
| 192 | insertAndSelect(m_cursors[i], text, selectNewText); |
| 193 | m_cursors.last().endEditBlock(); |
| 194 | } |
| 195 | |
| 196 | bool equalCursors(const QTextCursor &lhs, const QTextCursor &rhs) { |
| 197 | return lhs == rhs && lhs.anchor() == rhs.anchor(); |
| 198 | } |
| 199 | |
| 200 | bool MultiTextCursor::operator==(const MultiTextCursor &other) const { |
| 201 | if (m_cursors.size() != other.m_cursors.size()) |
| 202 | return false; |
| 203 | if (m_cursors.isEmpty()) |
| 204 | return true; |
| 205 | QList<QTextCursor> thisCursors = m_cursors; |
| 206 | QList<QTextCursor> otherCursors = other.m_cursors; |
| 207 | if (!equalCursors(thisCursors.takeLast(), otherCursors.takeLast())) |
| 208 | return false; |
| 209 | //for (const QTextCursor &oc : otherCursors) { |
| 210 | for (int i=0; i<otherCursors.size();i++) { |
| 211 | //auto compare = [oc](const QTextCursor &c) { return equalCursors(oc, c); }; |
| 212 | if (!thisCursors.contains(otherCursors[i])) |
| 213 | return false; |
| 214 | } |
| 215 | return true; |
| 216 | } |
| 217 | |
| 218 | bool MultiTextCursor::operator!=(const MultiTextCursor &other) const { |
| 219 | return !operator==(other); |
| 220 | } |
| 221 | |
| 222 | static bool cursorsOverlap(const QTextCursor &c1, const QTextCursor &c2) { |
| 223 | if (c1.hasSelection()) { |
| 224 | if (c2.hasSelection()) { |
| 225 | return c2.selectionEnd() > c1.selectionStart() |
| 226 | && c2.selectionStart() < c1.selectionEnd(); |
| 227 | } |
| 228 | const int c2Pos = c2.position(); |
| 229 | return c2Pos > c1.selectionStart() && c2Pos < c1.selectionEnd(); |
| 230 | } |
| 231 | if (c2.hasSelection()) { |
| 232 | const int c1Pos = c1.position(); |
| 233 | return c1Pos > c2.selectionStart() && c1Pos < c2.selectionEnd(); |
| 234 | } |
| 235 | return c1 == c2; |
| 236 | } |
| 237 | |
| 238 | static void _mergeCursors(QTextCursor &c1, const QTextCursor &c2) { |
| 239 | if (c1.position() == c2.position() && c1.anchor() == c2.anchor()) |
| 240 | return; |
| 241 | if (c1.hasSelection()) { |
| 242 | if (!c2.hasSelection()) |
| 243 | return; |
| 244 | int pos = c1.position(); |
| 245 | int anchor = c1.anchor(); |
| 246 | if (c1.selectionStart() > c2.selectionStart()) { |
| 247 | if (pos < anchor) |
| 248 | pos = c2.selectionStart(); |
| 249 | else |
| 250 | anchor = c2.selectionStart(); |
| 251 | } |
| 252 | if (c1.selectionEnd() < c2.selectionEnd()) { |
| 253 | if (pos < anchor) |
| 254 | anchor = c2.selectionEnd(); |
| 255 | else |
| 256 | pos = c2.selectionEnd(); |
| 257 | } |
| 258 | c1.setPosition(anchor); |
| 259 | c1.setPosition(pos, QTextCursor::KeepAnchor); |
| 260 | } else { |
| 261 | c1 = c2; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | void MultiTextCursor::mergeCursors() { |
| 266 | //printf("Merge Cursors\n"); |
| 267 | QList<QTextCursor> gcursors; |
| 268 | //QTextCursor c;c.setVerticalMovementX(1); |
| 269 | int mustcol=-1; |
| 270 | for (int i=0; i<m_cursors.size(); i++){ |
| 271 | if ((mustcol!=-1)&&(m_cursors.at(i).columnNumber()!=mustcol)) |
| 272 | m_cursors[i] = QTextCursor();//.setVerticalMovementX(m_cursors.at(i).columnNumber()-mustcol);// |
| 273 | mustcol=m_cursors.at(i).columnNumber(); |
| 274 | } |
| 275 | |
| 276 | //*/ |
| 277 | for (int i=0; i<m_cursors.size(); i++) if (!m_cursors.at(i).isNull()) gcursors.append(m_cursors[i]); |
| 278 | |
| 279 | for (int i=0; i<gcursors.size(); i++){ |
| 280 | gcursors[i].setVisualNavigation(true); |
| 281 | for (int j=i+1; j<gcursors.size(); ){ |
| 282 | if (cursorsOverlap(gcursors[i], gcursors[j])) { |
| 283 | _mergeCursors(gcursors[i], gcursors[j]); |
| 284 | //printf("I merge cursors\n"); |
| 285 | gcursors.removeOne(gcursors[j]); |
| 286 | continue; |
| 287 | } |
| 288 | j++; |
| 289 | } |
| 290 | } |
| 291 | m_cursors = gcursors; |
| 292 | } |
| 293 | |
| 294 | // could go into QTextCursor... |
| 295 | static QTextLine currentTextLine(const QTextCursor &cursor) { |
| 296 | const QTextBlock block = cursor.block(); |
| 297 | //if (!block.isValid()) return {}; |
| 298 | |
| 299 | const QTextLayout *layout = block.layout(); |
| 300 | //if (!layout) return {}; |
| 301 | |
| 302 | const int relativePos = cursor.position() - block.position(); |
| 303 | return layout->lineForTextPosition(relativePos); |
| 304 | } |
| 305 | |
| 306 | bool MultiTextCursor::multiCursorAddEvent(QKeyEvent *e, QKeySequence::StandardKey matchKey) { |
| 307 | uint searchkey = (e->modifiers() | e->key()) |
| 308 | & ~(Qt::KeypadModifier |
| 309 | | Qt::GroupSwitchModifier |
| 310 | | Qt::AltModifier |
| 311 | | Qt::ShiftModifier); |
| 312 | |
| 313 | const QList<QKeySequence> bindings = QKeySequence::keyBindings(matchKey); |
| 314 | return bindings.contains(QKeySequence(searchkey)); |
| 315 | } |
| 316 | |
| 317 | bool MultiTextCursor::handleMoveKeyEvent(QKeyEvent *e){ |
| 318 | //,QPlainTextEdit *edit,bool camelCaseNavigationEnabled) { |
| 319 | //printf("%d %d \n",e->modifiers(),(e->modifiers() & Qt::AltModifier )); |
| 320 | if (e->modifiers() & Qt::AltModifier ) { |
| 321 | QTextCursor::MoveOperation op = QTextCursor::NoMove; |
| 322 | if (multiCursorAddEvent(e, QKeySequence::MoveToNextWord)) { |
| 323 | op = QTextCursor::WordRight; |
| 324 | } else if (multiCursorAddEvent(e, QKeySequence::MoveToPreviousWord)) { |
| 325 | op = QTextCursor::WordLeft; |
| 326 | } else if (multiCursorAddEvent(e, QKeySequence::MoveToEndOfBlock)) { |
| 327 | op = QTextCursor::EndOfBlock; |
| 328 | } else if (multiCursorAddEvent(e, QKeySequence::MoveToStartOfBlock)) { |
| 329 | op = QTextCursor::StartOfBlock; |
| 330 | } else if (multiCursorAddEvent(e, QKeySequence::MoveToNextLine)) { |
| 331 | op = QTextCursor::Down; |
| 332 | } else if (multiCursorAddEvent(e, QKeySequence::MoveToPreviousLine)) { |
| 333 | op = QTextCursor::Up; |
| 334 | } else if (multiCursorAddEvent(e, QKeySequence::MoveToStartOfLine)) { |
| 335 | op = QTextCursor::StartOfLine; |
| 336 | } else if (multiCursorAddEvent(e, QKeySequence::MoveToEndOfLine)) { |
| 337 | op = QTextCursor::EndOfLine; |
| 338 | } else if (multiCursorAddEvent(e, QKeySequence::MoveToStartOfDocument)) { |
| 339 | op = QTextCursor::Start; |
| 340 | } else if (multiCursorAddEvent(e, QKeySequence::MoveToEndOfDocument)) { |
| 341 | op = QTextCursor::End; |
| 342 | } else { |
| 343 | return false; |
| 344 | } |
| 345 | printf("handleMoveKeyEvent op=%d\n",op); |
| 346 | //printf("?-> %d\n",m_cursors.size()) ; |
| 347 | QList<QTextCursor> cursors = m_cursors; |
| 348 | for (int i=0; i<cursors.size(); i++){ |
| 349 | //int pos=m_cursors[i].position(); |
| 350 | //bool moved=cursors[i].movePosition(op, QTextCursor::MoveAnchor); |
| 351 | m_cursors << cursors[i]; |
| 352 | //printf("handleMoveKeyEvent op=%d %d %d [%d]=%d=!=%d %d\n", op, m_cursors.size(), cursorCount(),i,cursors[i].position(),pos,moved); |
| 353 | } |
| 354 | mergeCursors(); |
| 355 | return true; |
| 356 | } |
| 357 | |
| 358 | //for (QTextCursor &cursor : m_cursors) { |
| 359 | for (int i=0; i<m_cursors.size(); i++){ |
| 360 | QTextCursor::MoveMode mode = QTextCursor::MoveAnchor; |
| 361 | QTextCursor::MoveOperation op = QTextCursor::NoMove; |
| 362 | |
| 363 | if (e == QKeySequence::MoveToNextChar) { |
| 364 | op = QTextCursor::Right; |
| 365 | } else if (e == QKeySequence::MoveToPreviousChar) { |
| 366 | op = QTextCursor::Left; |
| 367 | } else if (e == QKeySequence::SelectNextChar) { |
| 368 | op = QTextCursor::Right; |
| 369 | mode = QTextCursor::KeepAnchor; |
| 370 | } else if (e == QKeySequence::SelectPreviousChar) { |
| 371 | op = QTextCursor::Left; |
| 372 | mode = QTextCursor::KeepAnchor; |
| 373 | } else if (e == QKeySequence::SelectNextWord) { |
| 374 | op = QTextCursor::WordRight; |
| 375 | mode = QTextCursor::KeepAnchor; |
| 376 | } else if (e == QKeySequence::SelectPreviousWord) { |
| 377 | op = QTextCursor::WordLeft; |
| 378 | mode = QTextCursor::KeepAnchor; |
| 379 | } else if (e == QKeySequence::SelectStartOfLine) { |
| 380 | op = QTextCursor::StartOfLine; |
| 381 | mode = QTextCursor::KeepAnchor; |
| 382 | } else if (e == QKeySequence::SelectEndOfLine) { |
| 383 | op = QTextCursor::EndOfLine; |
| 384 | mode = QTextCursor::KeepAnchor; |
| 385 | } else if (e == QKeySequence::SelectStartOfBlock) { |
| 386 | op = QTextCursor::StartOfBlock; |
| 387 | mode = QTextCursor::KeepAnchor; |
| 388 | } else if (e == QKeySequence::SelectEndOfBlock) { |
| 389 | op = QTextCursor::EndOfBlock; |
| 390 | mode = QTextCursor::KeepAnchor; |
| 391 | } else if (e == QKeySequence::SelectStartOfDocument) { |
| 392 | op = QTextCursor::Start; |
| 393 | mode = QTextCursor::KeepAnchor; |
| 394 | } else if (e == QKeySequence::SelectEndOfDocument) { |
| 395 | op = QTextCursor::End; |
| 396 | mode = QTextCursor::KeepAnchor; |
| 397 | } else if (e == QKeySequence::SelectPreviousLine) { |
| 398 | op = QTextCursor::Up; |
| 399 | mode = QTextCursor::KeepAnchor; |
| 400 | } else if (e == QKeySequence::SelectNextLine) { |
| 401 | op = QTextCursor::Down; |
| 402 | mode = QTextCursor::KeepAnchor; |
| 403 | { |
| 404 | QTextBlock block = m_cursors[i].block(); |
| 405 | QTextLine line = currentTextLine(m_cursors[i]); |
| 406 | if (!block.next().isValid() && line.isValid() |
| 407 | && line.lineNumber() == block.layout()->lineCount() - 1) |
| 408 | op = QTextCursor::End; |
| 409 | } |
| 410 | } else if (e == QKeySequence::MoveToNextWord) { |
| 411 | op = QTextCursor::WordRight; |
| 412 | } else if (e == QKeySequence::MoveToPreviousWord) { |
| 413 | op = QTextCursor::WordLeft; |
| 414 | } else if (e == QKeySequence::MoveToEndOfBlock) { |
| 415 | op = QTextCursor::EndOfBlock; |
| 416 | } else if (e == QKeySequence::MoveToStartOfBlock) { |
| 417 | op = QTextCursor::StartOfBlock; |
| 418 | } else if (e == QKeySequence::MoveToNextLine) { |
| 419 | op = QTextCursor::Down; |
| 420 | } else if (e == QKeySequence::MoveToPreviousLine) { |
| 421 | op = QTextCursor::Up; |
| 422 | } else if (e == QKeySequence::MoveToStartOfLine) { |
| 423 | op = QTextCursor::StartOfLine; |
| 424 | } else if (e == QKeySequence::MoveToEndOfLine) { |
| 425 | op = QTextCursor::EndOfLine; |
| 426 | } else if (e == QKeySequence::MoveToStartOfDocument) { |
| 427 | op = QTextCursor::Start; |
| 428 | } else if (e == QKeySequence::MoveToEndOfDocument) { |
| 429 | op = QTextCursor::End; |
| 430 | } else { |
| 431 | //printf("handle fail\n"); |
| 432 | return false; |
| 433 | } |
| 434 | |
| 435 | // Except for pageup and pagedown, macOS has very different behavior, we don't do it all, but |
| 436 | // here's the breakdown: |
| 437 | // Shift still works as an anchor, but only one of the other keys can be down Ctrl (Command), |
| 438 | // Alt (Option), or Meta (Control). |
| 439 | // Command/Control + Left/Right -- Move to left or right of the line |
| 440 | // + Up/Down -- Move to top bottom of the file. (Control doesn't move the cursor) |
| 441 | // Option + Left/Right -- Move one word Left/right. |
| 442 | // + Up/Down -- Begin/End of Paragraph. |
| 443 | // Home/End Top/Bottom of file. (usually don't move the cursor, but will select) |
| 444 | |
| 445 | bool visualNavigation = m_cursors[i].visualNavigation(); |
| 446 | m_cursors[i].setVisualNavigation(true); |
| 447 | bool moved = m_cursors[i].movePosition(op, mode); |
| 448 | //printf("[%d]moved %d\n",i,moved); |
| 449 | |
| 450 | if (!moved && mode == QTextCursor::MoveAnchor) |
| 451 | m_cursors[i].clearSelection(); |
| 452 | m_cursors[i].setVisualNavigation(visualNavigation); |
| 453 | } |
| 454 | mergeCursors(); |
| 455 | return true; |
| 456 | } |
| 457 |