1 /**************************************************************************** 2 ** 3 ** Copyright (C) 2016 The Qt Company Ltd. 4 ** Contact: https://www.qt.io/licensing/ 5 ** 6 ** This file is part of the QtGui module of the Qt Toolkit. 7 ** 8 ** $QT_BEGIN_LICENSE:LGPL$ 9 ** Commercial License Usage 10 ** Licensees holding valid commercial Qt licenses may use this file in 11 ** accordance with the commercial license agreement provided with the 12 ** Software or, alternatively, in accordance with the terms contained in 13 ** a written agreement between you and The Qt Company. For licensing terms 14 ** and conditions see https://www.qt.io/terms-conditions. For further 15 ** information use the contact form at https://www.qt.io/contact-us. 16 ** 17 ** GNU Lesser General Public License Usage 18 ** Alternatively, this file may be used under the terms of the GNU Lesser 19 ** General Public License version 3 as published by the Free Software 20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the 21 ** packaging of this file. Please review the following information to 22 ** ensure the GNU Lesser General Public License version 3 requirements 23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. 24 ** 25 ** GNU General Public License Usage 26 ** Alternatively, this file may be used under the terms of the GNU 27 ** General Public License version 2.0 or (at your option) the GNU General 28 ** Public license version 3 or any later version approved by the KDE Free 29 ** Qt Foundation. The licenses are as published by the Free Software 30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 31 ** included in the packaging of this file. Please review the following 32 ** information to ensure the GNU General Public License requirements will 33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and 34 ** https://www.gnu.org/licenses/gpl-3.0.html. 35 ** 36 ** $QT_END_LICENSE$ 37 ** 38 ****************************************************************************/ 39 40 #ifndef QTEXTCURSOR_H 41 #define QTEXTCURSOR_H 42 43 #include <QtGui/qtguiglobal.h> 44 #include <QtCore/qstring.h> 45 #include <QtCore/qshareddata.h> 46 #include <QtGui/qtextformat.h> 47 48 QT_BEGIN_NAMESPACE 49 50 51 class QTextDocument; 52 class QTextCursorPrivate; 53 class QTextDocumentFragment; 54 class QTextCharFormat; 55 class QTextBlockFormat; 56 class QTextListFormat; 57 class QTextTableFormat; 58 class QTextFrameFormat; 59 class QTextImageFormat; 60 class QTextDocumentPrivate; 61 class QTextList; 62 class QTextTable; 63 class QTextFrame; 64 class QTextBlock; 65 66 class Q_GUI_EXPORT QTextCursor 67 { 68 public: 69 QTextCursor(); 70 explicit QTextCursor(QTextDocument *document); 71 QTextCursor(QTextDocumentPrivate *p, int pos); 72 explicit QTextCursor(QTextCursorPrivate *d); 73 explicit QTextCursor(QTextFrame *frame); 74 explicit QTextCursor(const QTextBlock &block); 75 QTextCursor(const QTextCursor &cursor); 76 QTextCursor &operator=(QTextCursor &&other) noexcept { swap(other); return *this; } 77 QTextCursor &operator=(const QTextCursor &other); 78 ~QTextCursor(); 79 swap(QTextCursor & other)80 void swap(QTextCursor &other) noexcept { qSwap(d, other.d); } 81 82 bool isNull() const; 83 84 enum MoveMode { 85 MoveAnchor, 86 KeepAnchor 87 }; 88 89 void setPosition(int pos, MoveMode mode = MoveAnchor); 90 int position() const; 91 int positionInBlock() const; 92 93 int anchor() const; 94 95 void insertText(const QString &text); 96 void insertText(const QString &text, const QTextCharFormat &format); 97 98 enum MoveOperation { 99 NoMove, 100 101 Start, 102 Up, 103 StartOfLine, 104 StartOfBlock, 105 StartOfWord, 106 PreviousBlock, 107 PreviousCharacter, 108 PreviousWord, 109 Left, 110 WordLeft, 111 112 End, 113 Down, 114 EndOfLine, 115 EndOfWord, 116 EndOfBlock, 117 NextBlock, 118 NextCharacter, 119 NextWord, 120 Right, 121 WordRight, 122 123 NextCell, 124 PreviousCell, 125 NextRow, 126 PreviousRow 127 }; 128 129 bool movePosition(MoveOperation op, MoveMode = MoveAnchor, int n = 1); 130 131 bool visualNavigation() const; 132 void setVisualNavigation(bool b); 133 134 void setVerticalMovementX(int x); 135 int verticalMovementX() const; 136 137 void setKeepPositionOnInsert(bool b); 138 bool keepPositionOnInsert() const; 139 140 void deleteChar(); 141 void deletePreviousChar(); 142 143 enum SelectionType { 144 WordUnderCursor, 145 LineUnderCursor, 146 BlockUnderCursor, 147 Document 148 }; 149 void select(SelectionType selection); 150 151 bool hasSelection() const; 152 bool hasComplexSelection() const; 153 void removeSelectedText(); 154 void clearSelection(); 155 int selectionStart() const; 156 int selectionEnd() const; 157 158 QString selectedText() const; 159 QTextDocumentFragment selection() const; 160 void selectedTableCells(int *firstRow, int *numRows, int *firstColumn, int *numColumns) const; 161 162 QTextBlock block() const; 163 164 QTextCharFormat charFormat() const; 165 void setCharFormat(const QTextCharFormat &format); 166 void mergeCharFormat(const QTextCharFormat &modifier); 167 168 QTextBlockFormat blockFormat() const; 169 void setBlockFormat(const QTextBlockFormat &format); 170 void mergeBlockFormat(const QTextBlockFormat &modifier); 171 172 QTextCharFormat blockCharFormat() const; 173 void setBlockCharFormat(const QTextCharFormat &format); 174 void mergeBlockCharFormat(const QTextCharFormat &modifier); 175 176 bool atBlockStart() const; 177 bool atBlockEnd() const; 178 bool atStart() const; 179 bool atEnd() const; 180 181 void insertBlock(); 182 void insertBlock(const QTextBlockFormat &format); 183 void insertBlock(const QTextBlockFormat &format, const QTextCharFormat &charFormat); 184 185 QTextList *insertList(const QTextListFormat &format); 186 QTextList *insertList(QTextListFormat::Style style); 187 188 QTextList *createList(const QTextListFormat &format); 189 QTextList *createList(QTextListFormat::Style style); 190 QTextList *currentList() const; 191 192 QTextTable *insertTable(int rows, int cols, const QTextTableFormat &format); 193 QTextTable *insertTable(int rows, int cols); 194 QTextTable *currentTable() const; 195 196 QTextFrame *insertFrame(const QTextFrameFormat &format); 197 QTextFrame *currentFrame() const; 198 199 void insertFragment(const QTextDocumentFragment &fragment); 200 201 #ifndef QT_NO_TEXTHTMLPARSER 202 void insertHtml(const QString &html); 203 #endif // QT_NO_TEXTHTMLPARSER 204 205 void insertImage(const QTextImageFormat &format, QTextFrameFormat::Position alignment); 206 void insertImage(const QTextImageFormat &format); 207 void insertImage(const QString &name); 208 void insertImage(const QImage &image, const QString &name = QString()); 209 210 void beginEditBlock(); 211 void joinPreviousEditBlock(); 212 void endEditBlock(); 213 214 bool operator!=(const QTextCursor &rhs) const; 215 bool operator<(const QTextCursor &rhs) const; 216 bool operator<=(const QTextCursor &rhs) const; 217 bool operator==(const QTextCursor &rhs) const; 218 bool operator>=(const QTextCursor &rhs) const; 219 bool operator>(const QTextCursor &rhs) const; 220 221 bool isCopyOf(const QTextCursor &other) const; 222 223 int blockNumber() const; 224 int columnNumber() const; 225 226 QTextDocument *document() const; 227 228 private: 229 QSharedDataPointer<QTextCursorPrivate> d; 230 friend class QTextCursorPrivate; 231 friend class QTextDocumentPrivate; 232 friend class QTextDocumentFragmentPrivate; 233 friend class QTextCopyHelper; 234 friend class QWidgetTextControlPrivate; 235 }; 236 237 Q_DECLARE_SHARED(QTextCursor) 238 239 QT_END_NAMESPACE 240 241 #endif // QTEXTCURSOR_H 242