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
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
4#include "qtextdocument_p.h"
5#include "qtextblock_p.h"
6
7namespace Utils {
8
13
15{
16 return (blockNumber >= 0 && blockNumber < m_blocks.size())
17 ? m_blocks.at(blockNumber).textBlock
18 : TextBlock();
19}
20
22{
23 return findBlockByNumber(lineNumber);
24}
25
27{
28 return m_content.at(pos);
29}
30
32{
33 return m_content.size();
34}
35
37{
38 return m_blocks.isEmpty() ? TextBlock() : m_blocks.at(0).textBlock;
39}
40
42{
43 return begin();
44}
45
47{
48 return m_blocks.isEmpty() ? TextBlock() : m_blocks.last().textBlock;
49}
50
51std::optional<int> TextDocument::version() const
52{
53 return m_version;
54}
55
56void TextDocument::setVersion(std::optional<int> v)
57{
58 m_version = v;
59}
60
62{
63 return m_content;
64}
65
67{
68 m_content = text;
69 m_blocks.clear();
70
71 const auto appendToBlocks = [this](int blockNumber, int start, int length) {
72 Block block;
73 block.textBlock.setBlockNumber(blockNumber);
74 block.textBlock.setPosition(start);
75 block.textBlock.setDocument(this);
76 block.textBlock.setLength(length);
77 m_blocks.append(block);
78 };
79
80 int blockStart = 0;
81 int blockNumber = -1;
82 while (blockStart < text.size()) {
83 int blockEnd = text.indexOf(u'\n', blockStart) + 1;
84 if (blockEnd == 0)
85 blockEnd = text.size();
86 appendToBlocks(++blockNumber, blockStart, blockEnd - blockStart);
87 blockStart = blockEnd;
88 }
89 // Add an empty block if the text ends with \n. This is required for retrieving
90 // the actual line of the text editor if requested, for example, in findBlockByNumber.
91 // Consider a case with text aa\nbb\n\n. You are on 4th line of the text editor and even
92 // if it is an empty line, we introduce a text block for it to maybe use later.
93 if (text.endsWith(u'\n'))
94 appendToBlocks(++blockNumber, blockStart, 0);
95}
96
98{
99 return m_modified;
100}
101
102void TextDocument::setModified(bool modified)
103{
104 m_modified = modified;
105}
106
107void TextDocument::setUserState(int blockNumber, int state)
108{
109 if (blockNumber >= 0 && blockNumber < m_blocks.size())
110 m_blocks[blockNumber].userState = state;
111}
112
113int TextDocument::userState(int blockNumber) const
114{
115 return (blockNumber >= 0 && blockNumber < m_blocks.size()) ? m_blocks[blockNumber].userState
116 : -1;
117}
118
120{
121 return &m_mutex;
122}
123
124} // namespace Utils
\inmodule QtCore
\inmodule QtCore
Definition qmutex.h:281
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
qsizetype indexOf(QLatin1StringView s, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition qstring.cpp:4517
qsizetype size() const noexcept
Returns the number of characters in this string.
Definition qstring.h:186
const QChar at(qsizetype i) const
Returns the character at the given index position in the string.
Definition qstring.h:1226
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition qstring.cpp:5506
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
int userState(int blockNumber) const
QString toPlainText() const
void setVersion(std::optional< int >)
bool isModified() const
QChar characterAt(int pos) const
QMutex * mutex() const
std::optional< int > version() const
TextBlock begin() const
int characterCount() const
TextDocument()=default
TextBlock findBlockByNumber(int blockNumber) const
QString text
else opt state
[0]
GLsizei const GLfloat * v
[13]
GLenum GLuint GLenum GLsizei length
GLuint start