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
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
6#include "qtextblock_p.h"
7
9
10namespace Utils {
11
12TextDocument::TextDocument(const QString &text)
13{
14 setPlainText(text);
15}
16
18{
19 return (blockNumber >= 0 && blockNumber < m_blocks.size())
20 ? m_blocks.at(blockNumber).textBlock
21 : TextBlock();
22}
23
25{
26 return findBlockByNumber(lineNumber);
27}
28
30{
31 return m_content.at(pos);
32}
33
35{
36 return m_content.size();
37}
38
40{
41 return m_blocks.isEmpty() ? TextBlock() : m_blocks.at(0).textBlock;
42}
43
45{
46 return begin();
47}
48
50{
51 return m_blocks.isEmpty() ? TextBlock() : m_blocks.last().textBlock;
52}
53
54std::optional<int> TextDocument::version() const
55{
56 return m_version;
57}
58
59void TextDocument::setVersion(std::optional<int> v)
60{
61 m_version = v;
62}
63
65{
66 return m_content;
67}
68
69void 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
101{
102 return m_modified;
103}
104
105void TextDocument::setModified(bool modified)
106{
107 m_modified = modified;
108}
109
110void TextDocument::setUserState(int blockNumber, int state)
111{
112 if (blockNumber >= 0 && blockNumber < m_blocks.size())
113 m_blocks[blockNumber].userState = state;
114}
115
116int TextDocument::userState(int blockNumber) const
117{
118 return (blockNumber >= 0 && blockNumber < m_blocks.size()) ? m_blocks[blockNumber].userState
119 : -1;
120}
121
123{
124 return &m_mutex;
125}
126
127} // namespace Utils
128
129QT_END_NAMESPACE
void setLength(int length)
void setDocument(TextDocument *document)
void setPosition(int position)
void setBlockNumber(int blockNumber)
void setModified(bool modified)
void setPlainText(const QString &text)
void setUserState(int blockNumber, int state)
TextBlock lastBlock() const
TextBlock findBlockByLineNumber(int lineNumber) const
TextBlock firstBlock() const
TextDocument(const QString &text)
int userState(int blockNumber) const
QString toPlainText() const
void setVersion(std::optional< int >)
QChar characterAt(int pos) const
QMutex * mutex() const
std::optional< int > version() const
TextBlock begin() const
int characterCount() const
TextBlock findBlockByNumber(int blockNumber) const