Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#include "qtextcursor_p.h"
5#include "qtextdocument_p.h"
6#include "qtextblock_p.h"
7
8namespace Utils {
9
10class TextFrame;
11class TextTable;
12class TextTableCell;
13
14TextCursor::TextCursor(TextDocument *document) : m_document(document) { }
15
17{
18 Q_UNUSED(n);
19 switch (op) {
20 case NoMove:
21 return true;
22 case Start:
23 m_position = 0;
24 break;
26 while (--n >= 0) {
27 if (m_position == 0)
28 return false;
29 --m_position;
30 }
31 break;
32 case End:
33 m_position = m_document->characterCount();
34 break;
35 case NextCharacter:
36 while (--n >= 0) {
37 if (m_position == m_document->characterCount())
38 return false;
39 ++m_position;
40 }
41 break;
42 }
43
44 if (mode == MoveAnchor)
45 m_anchor = m_position;
46
47 return false;
48}
49
51{
52 return m_position;
53}
54
56{
57 m_position = pos;
58 if (mode == MoveAnchor)
59 m_anchor = pos;
60}
61
63{
64 return m_document->toPlainText().mid(qMin(m_position, m_anchor), qAbs(m_position - m_anchor));
65}
66
68{
69 m_anchor = m_position;
70}
71
73{
74 return m_document;
75}
76
78{
79 const QString orig = m_document->toPlainText();
80 const QString left = orig.left(qMin(m_position, m_anchor));
81 const QString right = orig.mid(qMax(m_position, m_anchor));
82 m_document->setPlainText(left + text + right);
83}
84
86{
87 TextBlock current = m_document->firstBlock();
88 while (current.isValid()) {
89 if (current.position() <= position()
90 && current.position() + current.length() > current.position())
91 break;
92 current = current.next();
93 }
94 return current;
95}
96
98{
99 return m_position - block().position();
100}
101
103{
104 return block().blockNumber();
105}
106
111
113{
114 return qMax(m_position, m_anchor);
115}
116
118{
119 return m_document == nullptr;
120}
121
122} // namespace Utils
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString left(qsizetype n) const &
Definition qstring.h:363
QString mid(qsizetype position, qsizetype n=-1) const &
Definition qstring.cpp:5300
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)
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
void setPlainText(const QString &text)
TextBlock firstBlock() const
QString toPlainText() const
int characterCount() const
QString text
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
constexpr T qAbs(const T &t)
Definition qnumeric.h:328
GLenum mode
GLdouble GLdouble right
GLint left
GLfloat n
#define Q_UNUSED(x)