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
qtextlist.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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
5#include "qtextlist.h"
6#include "qtextobject_p.h"
7#include "qtextcursor.h"
8#include "qtextdocument_p.h"
9#include <qdebug.h>
10
12
13using namespace Qt::StringLiterals;
14
16{
17public:
22};
23
75
82
87{
88 Q_D(const QTextList);
89 return d->blocks.size();
90}
91
98{
99 Q_D(const QTextList);
100 if (i < 0 || i >= d->blocks.size())
101 return QTextBlock();
102 return d->blocks.at(i);
103}
104
123int QTextList::itemNumber(const QTextBlock &blockIt) const
124{
125 Q_D(const QTextList);
126 return d->blocks.indexOf(blockIt);
127}
128
135{
136 Q_D(const QTextList);
137 int item = d->blocks.indexOf(blockIt) + 1;
138 if (item <= 0)
139 return QString();
140
141 QTextBlock block = d->blocks.at(item-1);
142 QTextBlockFormat blockFormat = block.blockFormat();
143
145
146 const int style = format().style();
147 QString numberPrefix;
148 QString numberSuffix = u"."_s;
149
150 // the number of the item might be offset by start, which defaults to 1
151 const int itemNumber = item + format().start() - 1;
152
153 if (format().hasProperty(QTextFormat::ListNumberPrefix))
154 numberPrefix = format().numberPrefix();
155 if (format().hasProperty(QTextFormat::ListNumberSuffix))
156 numberSuffix = format().numberSuffix();
157
158 switch (style) {
161 break;
162 // from the old richtext
165 {
166 // match the html default behavior of falling back to decimal numbers
167 if (itemNumber < 1) {
169 break;
170 }
171
172 const char baseChar = style == QTextListFormat::ListUpperAlpha ? 'A' : 'a';
173
174 int c = itemNumber;
175 while (c > 0) {
176 c--;
177 result.prepend(QChar::fromUcs2(baseChar + (c % 26)));
178 c /= 26;
179 }
180 }
181 break;
184 {
185 // match the html default behavior of falling back to decimal numbers
186 if (itemNumber < 1) {
188 } else if (itemNumber < 5000) {
189 QString romanNumeral;
190
191 // works for up to 4999 items
192 QLatin1StringView romanSymbols;
194 romanSymbols = "iiivixxxlxcccdcmmmm"_L1;
195 else
196 romanSymbols = "IIIVIXXXLXCCCDCMMMM"_L1;
197
198 int c[] = { 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000 };
199 int n = itemNumber;
200 for (int i = 12; i >= 0; n %= c[i], i--) {
201 int q = n / c[i];
202 if (q > 0) {
203 int startDigit = i + (i+3)/4;
204 int numDigits;
205 if (i % 4) {
206 // c[i] == 4|5|9|40|50|90|400|500|900
207 if ((i-2) % 4) {
208 // c[i] == 4|9|40|90|400|900 => with subtraction (IV, IX, XL, XC, ...)
209 numDigits = 2;
210 }
211 else {
212 // c[i] == 5|50|500 (V, L, D)
213 numDigits = 1;
214 }
215 }
216 else {
217 // c[i] == 1|10|100|1000 (I, II, III, X, XX, ...)
218 numDigits = q;
219 }
220
221 romanNumeral.append(romanSymbols.sliced(startDigit, numDigits));
222 }
223 }
224 result = std::move(romanNumeral);
225 } else {
226 result = u"?"_s;
227 }
228
229 }
230 break;
231 default:
232 Q_ASSERT(false);
233 }
234 if (blockIt.textDirection() == Qt::RightToLeft)
235 return numberSuffix + result + numberPrefix;
236 else
237 return numberPrefix + result + numberSuffix;
238}
239
248{
249 Q_D(QTextList);
250 if (i < 0 || i >= d->blocks.size())
251 return;
252
253 QTextBlock block = d->blocks.at(i);
254 remove(block);
255}
256
257
264{
266 fmt.setIndent(fmt.indent() + format().indent());
267 fmt.setObjectIndex(-1);
268 const_cast<QTextDocumentPrivate *>(QTextDocumentPrivate::get(block))->setBlockFormat(block, block, fmt, QTextDocumentPrivate::SetFormat);
269}
270
276void QTextList::add(const QTextBlock &block)
277{
280 const_cast<QTextDocumentPrivate *>(QTextDocumentPrivate::get(block))->setBlockFormat(block, block, fmt, QTextDocumentPrivate::SetFormat);
281}
282
284
285#include "moc_qtextlist.cpp"
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:8084
void setIndent(int indent)
Sets the paragraph's indentation.
\reentrant
Definition qtextobject.h:53
\reentrant
QTextBlockFormat blockFormat() const
Returns the QTextBlockFormat that describes block-specific properties.
static const QTextDocumentPrivate * get(const QTextDocument *document)
\reentrant \inmodule QtGui
void setObjectIndex(int object)
Sets the format object's object index.
QString numberSuffix() const
Style style() const
Returns the list format's style.
int start() const
QString numberPrefix() const
QTextListPrivate(QTextDocument *doc)
Definition qtextlist.cpp:18
\reentrant
Definition qtextlist.h:18
QTextList(QTextDocument *doc)
Definition qtextlist.cpp:71
int count() const
Returns the number of items in the list.
Definition qtextlist.cpp:86
int itemNumber(const QTextBlock &) const
Returns the index of the list item that corresponds to the given block.
void add(const QTextBlock &block)
Makes the given block part of the list.
void removeItem(int i)
Removes the item at item position i from the list.
void remove(const QTextBlock &)
Removes the given block from the list.
QTextListFormat format() const
Returns the list's format.
Definition qtextlist.h:37
QTextBlock item(int i) const
Returns the {i}-th text block in the list.
Definition qtextlist.cpp:97
QString itemText(const QTextBlock &) const
Returns the text of the list item that corresponds to the given block.
int objectIndex() const
Returns the object index of this object.
Combined button and popup list for selecting options.
@ RightToLeft
GLfloat n
GLint GLsizei GLsizei GLenum format
const GLubyte * c
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLuint64EXT * result
[6]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QVideoFrameFormat::PixelFormat fmt
static int numDigits(qlonglong n)
QGraphicsItem * item