Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qtextdocument.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
5
#
include
"qtextdocument_p.h"
6
#
include
"qtextblock_p.h"
7
8
QT_BEGIN_NAMESPACE
9
10
namespace
Utils
{
11
12
TextDocument::TextDocument(
const
QString &text)
13
{
14
setPlainText(text);
15
}
16
17
TextBlock
TextDocument::findBlockByNumber(
int
blockNumber)
const
18
{
19
return
(blockNumber >= 0 && blockNumber < m_blocks.size())
20
? m_blocks.at(blockNumber).textBlock
21
: TextBlock();
22
}
23
24
TextBlock
TextDocument::findBlockByLineNumber(
int
lineNumber)
const
25
{
26
return
findBlockByNumber(lineNumber);
27
}
28
29
QChar TextDocument::characterAt(
int
pos)
const
30
{
31
return
m_content.at(pos);
32
}
33
34
int
TextDocument::characterCount()
const
35
{
36
return
m_content.size();
37
}
38
39
TextBlock
TextDocument::begin()
const
40
{
41
return
m_blocks.isEmpty() ? TextBlock() : m_blocks.at(0).textBlock;
42
}
43
44
TextBlock
TextDocument::firstBlock()
const
45
{
46
return
begin();
47
}
48
49
TextBlock
TextDocument::lastBlock()
const
50
{
51
return
m_blocks.isEmpty() ? TextBlock() : m_blocks.last().textBlock;
52
}
53
54
std
::optional<
int
> TextDocument::version()
const
55
{
56
return
m_version;
57
}
58
59
void
TextDocument::setVersion(
std
::optional<
int
> v)
60
{
61
m_version = v;
62
}
63
64
QString TextDocument::toPlainText()
const
65
{
66
return
m_content;
67
}
68
69
void
TextDocument::setPlainText(
const
QString &text)
70
{
71
m_content = text;
72
m_blocks.clear();
73
74
const
auto
appendToBlocks = [
this
](
int
blockNumber,
int
start,
int
length) {
75
Block block;
76
block.textBlock.setBlockNumber(blockNumber);
77
block.textBlock.setPosition(start);
78
block.textBlock.setDocument(
this
);
79
block.textBlock.setLength(length);
80
m_blocks.append(block);
81
};
82
83
int
blockStart = 0;
84
int
blockNumber = -1;
85
while
(blockStart < text.size()) {
86
int
blockEnd = text.indexOf(u'\n', blockStart) + 1;
87
if
(blockEnd == 0)
88
blockEnd = text.size();
89
appendToBlocks(++blockNumber, blockStart, blockEnd - blockStart);
90
blockStart = blockEnd;
91
}
92
// Add an empty block if the text ends with \n. This is required for retrieving
93
// the actual line of the text editor if requested, for example, in findBlockByNumber.
94
// Consider a case with text aa\nbb\n\n. You are on 4th line of the text editor and even
95
// if it is an empty line, we introduce a text block for it to maybe use later.
96
if
(text.endsWith(u'\n'))
97
appendToBlocks(++blockNumber, blockStart, 0);
98
}
99
100
bool
TextDocument::isModified()
const
101
{
102
return
m_modified;
103
}
104
105
void
TextDocument::setModified(
bool
modified)
106
{
107
m_modified = modified;
108
}
109
110
void
TextDocument::setUserState(
int
blockNumber,
int
state)
111
{
112
if
(blockNumber >= 0 && blockNumber < m_blocks.size())
113
m_blocks[blockNumber].userState = state;
114
}
115
116
int
TextDocument::userState(
int
blockNumber)
const
117
{
118
return
(blockNumber >= 0 && blockNumber < m_blocks.size()) ? m_blocks[blockNumber].userState
119
: -1;
120
}
121
122
QMutex *TextDocument::mutex()
const
123
{
124
return
&m_mutex;
125
}
126
127
}
// namespace Utils
128
129
QT_END_NAMESPACE
Utils::TextBlock
Definition
qtextblock_p.h:29
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qdesigner_internal::Utils
Definition
qdesigner_utils_p.h:526
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
qtdeclarative
src
qmlls
qtextdocument.cpp
Generated on
for Qt by
1.18.0