| 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 | #pragma once |
| 27 | |
| 28 | #include <QKeySequence> |
| 29 | #include <QTextCursor> |
| 30 | #include <QTextLine> |
| 31 | |
| 32 | QT_BEGIN_NAMESPACE |
| 33 | class QKeyEvent; |
| 34 | class QPlainTextEdit; |
| 35 | QT_END_NAMESPACE |
| 36 | class MultiTextCursor { |
| 37 | public: |
| 38 | MultiTextCursor(); |
| 39 | explicit MultiTextCursor(const QList<QTextCursor> &cursors); |
| 40 | explicit MultiTextCursor(const QTextCursor &cursor); |
| 41 | |
| 42 | /// replace all cursors with \param cursors and the last one will be the new main cursors |
| 43 | void setCursors(const QList<QTextCursor> &cursors); |
| 44 | const QList<QTextCursor> cursors() const; |
| 45 | |
| 46 | /// \returns whether this multi cursor contains any cursor |
| 47 | bool isNull() const; |
| 48 | /// \returns whether this multi cursor contains more than one cursor |
| 49 | bool hasMultipleCursors() const; |
| 50 | /// \returns the number of cursors handled by this cursor |
| 51 | int cursorCount() const; |
| 52 | |
| 53 | /// the \param cursor that is appended by added by \brief addCursor |
| 54 | /// will be interpreted as the new main cursor |
| 55 | void addCursor(const QTextCursor &cursor); |
| 56 | /// convenience function that removes the old main cursor and appends |
| 57 | /// \param cursor as the new main cursor |
| 58 | void replaceMainCursor(const QTextCursor &cursor); |
| 59 | /// \returns the main cursor |
| 60 | QTextCursor mainCursor() const; |
| 61 | /// \returns the main cursor and removes it from this multi cursor |
| 62 | QTextCursor takeMainCursor(); |
| 63 | |
| 64 | void beginEditBlock(); |
| 65 | void endEditBlock(); |
| 66 | /// merges overlapping cursors together |
| 67 | void mergeCursors(); |
| 68 | |
| 69 | bool aCursorInThisBlock(int blocknumber); |
| 70 | |
| 71 | /// applies the move key event \param e to all cursors in this multi cursor |
| 72 | bool handleMoveKeyEvent(QKeyEvent *e);//, QPlainTextEdit *edit, bool camelCaseNavigationEnabled); |
| 73 | /// applies the move \param operation to all cursors in this multi cursor \param n times |
| 74 | /// with the move \param mode |
| 75 | void movePosition(QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode, int n = 1); |
| 76 | |
| 77 | /// \returns whether any cursor has a selection |
| 78 | bool hasSelection() const; |
| 79 | /// \returns the selected text of all cursors that have a selection separated by |
| 80 | /// a newline character |
| 81 | QString selectedText() const; |
| 82 | void clearSelection(); |
| 83 | /// removes the selected text of all cursors that have a selection from the document |
| 84 | void removeSelectedText(); |
| 85 | |
| 86 | /// inserts \param text into all cursors, potentially removing correctly selected text |
| 87 | void insertText(const QString &text, bool selectNewText = false); |
| 88 | void emplace(MultiTextCursor); |
| 89 | |
| 90 | bool operator==(const MultiTextCursor &other) const; |
| 91 | bool operator!=(const MultiTextCursor &other) const; |
| 92 | |
| 93 | //using iterator = QList<QTextCursor>::iterator; |
| 94 | //using const_iterator = QList<QTextCursor>::const_iterator; |
| 95 | |
| 96 | QList<QTextCursor>::iterator begin() { return m_cursors.begin(); } |
| 97 | QList<QTextCursor>::iterator end() { return m_cursors.end(); } |
| 98 | QList<QTextCursor>::const_iterator begin() const { return m_cursors.begin(); } |
| 99 | QList<QTextCursor>::const_iterator end() const { return m_cursors.end(); } |
| 100 | QList<QTextCursor>::const_iterator constBegin() const { return m_cursors.constBegin(); } |
| 101 | QList<QTextCursor>::const_iterator constEnd() const { return m_cursors.constEnd(); } |
| 102 | |
| 103 | static bool multiCursorAddEvent(QKeyEvent *e, QKeySequence::StandardKey matchKey); |
| 104 | |
| 105 | //private: |
| 106 | QList<QTextCursor> m_cursors; |
| 107 | }; |
| 108 |