Qt 6.x
The Qt SDK
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:
20 {
21 }
22};
23
72 : QTextBlockGroup(*new QTextListPrivate(doc), doc)
73{
74}
75
80{
81}
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 QByteArray romanNumeral;
190
191 // works for up to 4999 items
192 static const char romanSymbolsLower[] = "iiivixxxlxcccdcmmmm";
193 static const char romanSymbolsUpper[] = "IIIVIXXXLXCCCDCMMMM";
194 QByteArray romanSymbols; // wrap to have "mid"
196 romanSymbols = QByteArray::fromRawData(romanSymbolsLower, sizeof(romanSymbolsLower));
197 else
198 romanSymbols = QByteArray::fromRawData(romanSymbolsUpper, sizeof(romanSymbolsUpper));
199
200 int c[] = { 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000 };
201 int n = itemNumber;
202 for (int i = 12; i >= 0; n %= c[i], i--) {
203 int q = n / c[i];
204 if (q > 0) {
205 int startDigit = i + (i+3)/4;
206 int numDigits;
207 if (i % 4) {
208 // c[i] == 4|5|9|40|50|90|400|500|900
209 if ((i-2) % 4) {
210 // c[i] == 4|9|40|90|400|900 => with subtraction (IV, IX, XL, XC, ...)
211 numDigits = 2;
212 }
213 else {
214 // c[i] == 5|50|500 (V, L, D)
215 numDigits = 1;
216 }
217 }
218 else {
219 // c[i] == 1|10|100|1000 (I, II, III, X, XX, ...)
220 numDigits = q;
221 }
222
223 romanNumeral.append(romanSymbols.mid(startDigit, numDigits));
224 }
225 }
226 result = QString::fromLatin1(romanNumeral);
227 } else {
228 result = u"?"_s;
229 }
230
231 }
232 break;
233 default:
234 Q_ASSERT(false);
235 }
236 if (blockIt.textDirection() == Qt::RightToLeft)
237 return numberSuffix + result + numberPrefix;
238 else
239 return numberPrefix + result + numberSuffix;
240}
241
250{
251 Q_D(QTextList);
252 if (i < 0 || i >= d->blocks.size())
253 return;
254
255 QTextBlock block = d->blocks.at(i);
256 remove(block);
257}
258
259
266{
268 fmt.setIndent(fmt.indent() + format().indent());
269 fmt.setObjectIndex(-1);
270 const_cast<QTextDocumentPrivate *>(QTextDocumentPrivate::get(block))->setBlockFormat(block, block, fmt, QTextDocumentPrivate::SetFormat);
271}
272
278void QTextList::add(const QTextBlock &block)
279{
281 fmt.setObjectIndex(objectIndex());
282 const_cast<QTextDocumentPrivate *>(QTextDocumentPrivate::get(block))->setBlockFormat(block, block, fmt, QTextDocumentPrivate::SetFormat);
283}
284
286
287#include "moc_qtextlist.cpp"
\inmodule QtCore
Definition qbytearray.h:57
QByteArray & append(char c)
This is an overloaded member function, provided for convenience. It differs from the above function o...
QByteArray mid(qsizetype index, qsizetype len=-1) const
Returns a byte array containing len bytes from this byte array, starting at position pos.
static QByteArray fromRawData(const char *data, qsizetype size)
Constructs a QByteArray that uses the first size bytes of the data array.
Definition qbytearray.h:394
static constexpr QChar fromUcs2(char16_t c) noexcept
Definition qchar.h:98
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5710
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:7822
\reentrant
Definition qtextobject.h:53
\reentrant
QTextBlockFormat blockFormat() const
Returns the QTextBlockFormat that describes block-specific properties.
Qt::LayoutDirection textDirection() const
static const QTextDocumentPrivate * get(const QTextDocument *document)
\reentrant \inmodule QtGui
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