Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtextcursor.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
7#include "qtextblock_p.h"
8
10
11namespace Utils {
12
13class TextFrame;
14class TextTable;
15class TextTableCell;
16
17TextCursor::TextCursor(TextDocument *document) : m_document(document) { }
18
20{
21 Q_UNUSED(n);
22 switch (op) {
23 case NoMove:
24 return true;
25 case Start:
26 m_position = 0;
27 break;
29 while (--n >= 0) {
30 if (m_position == 0)
31 return false;
32 --m_position;
33 }
34 break;
35 case End:
36 m_position = m_document->characterCount();
37 break;
38 case NextCharacter:
39 while (--n >= 0) {
40 if (m_position == m_document->characterCount())
41 return false;
42 ++m_position;
43 }
44 break;
45 }
46
47 if (mode == MoveAnchor)
48 m_anchor = m_position;
49
50 return false;
51}
52
53int TextCursor::position() const
54{
55 return m_position;
56}
57
59{
60 m_position = pos;
61 if (mode == MoveAnchor)
62 m_anchor = pos;
63}
64
66{
67 return m_document->toPlainText().mid(qMin(m_position, m_anchor), qAbs(m_position - m_anchor));
68}
69
71{
72 m_anchor = m_position;
73}
74
76{
77 return m_document;
78}
79
80void TextCursor::insertText(const QString &text)
81{
82 const QString orig = m_document->toPlainText();
83 const QString left = orig.left(qMin(m_position, m_anchor));
84 const QString right = orig.mid(qMax(m_position, m_anchor));
85 m_document->setPlainText(left + text + right);
86}
87
89{
90 TextBlock current = m_document->firstBlock();
91 while (current.isValid()) {
92 if (current.position() <= position()
93 && current.position() + current.length() > current.position())
94 break;
95 current = current.next();
96 }
97 return current;
98}
99
101{
102 return m_position - block().position();
103}
104
106{
108}
109
111{
112 insertText(QString());
113}
114
116{
117 return qMax(m_position, m_anchor);
118}
119
120bool TextCursor::isNull() const
121{
122 return m_document == nullptr;
123}
124
125} // namespace Utils
126
127QT_END_NAMESPACE
TextBlock next() const
int length() const
bool isValid() const
int position() const
int blockNumber() const
int selectionEnd() const
bool movePosition(MoveOperation op, MoveMode=MoveAnchor, int n=1)
TextCursor(TextDocument *document)
int blockNumber() const
int positionInBlock() const
TextBlock block() const
void insertText(const QString &text)
int position() const
TextDocument * document() const
QString selectedText() const
void setPosition(int pos, MoveMode mode=MoveAnchor)
bool isNull() const
TextBlock firstBlock() const
int characterCount() const