Coverage Report

Created: 2025-07-12 07:23

/src/qtbase/src/gui/text/qtextlist.cpp
Line
Count
Source (jump to first uncovered line)
1
/****************************************************************************
2
**
3
** Copyright (C) 2016 The Qt Company Ltd.
4
** Contact: https://www.qt.io/licensing/
5
**
6
** This file is part of the QtGui module of the Qt Toolkit.
7
**
8
** $QT_BEGIN_LICENSE:LGPL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and The Qt Company. For licensing terms
14
** and conditions see https://www.qt.io/terms-conditions. For further
15
** information use the contact form at https://www.qt.io/contact-us.
16
**
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 3 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL3 included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 3 requirements
23
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24
**
25
** GNU General Public License Usage
26
** Alternatively, this file may be used under the terms of the GNU
27
** General Public License version 2.0 or (at your option) the GNU General
28
** Public license version 3 or any later version approved by the KDE Free
29
** Qt Foundation. The licenses are as published by the Free Software
30
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31
** included in the packaging of this file. Please review the following
32
** information to ensure the GNU General Public License requirements will
33
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34
** https://www.gnu.org/licenses/gpl-3.0.html.
35
**
36
** $QT_END_LICENSE$
37
**
38
****************************************************************************/
39
40
41
#include "qtextlist.h"
42
#include "qtextobject_p.h"
43
#include "qtextcursor.h"
44
#include "qtextdocument_p.h"
45
#include <qdebug.h>
46
47
QT_BEGIN_NAMESPACE
48
49
class QTextListPrivate : public QTextBlockGroupPrivate
50
{
51
public:
52
    QTextListPrivate(QTextDocument *doc)
53
0
        : QTextBlockGroupPrivate(doc)
54
0
    {
55
0
    }
56
};
57
58
/*!
59
    \class QTextList
60
    \reentrant
61
62
    \brief The QTextList class provides a decorated list of items in a QTextDocument.
63
    \inmodule QtGui
64
65
    \ingroup richtext-processing
66
67
    A list contains a sequence of text blocks, each of which is marked with a
68
    bullet point or other symbol. Multiple levels of lists can be used, and
69
    the automatic numbering feature provides support for ordered numeric and
70
    alphabetical lists.
71
72
    Lists are created by using a text cursor to insert an empty list at the
73
    current position or by moving existing text into a new list.
74
    The \l{QTextCursor::insertList()} function inserts an empty block into the
75
    document at the cursor position, and makes it the first item in a list.
76
77
    \snippet textdocument-lists/mainwindow.cpp 0
78
79
    The \l{QTextCursor::createList()} function takes the contents of the
80
    cursor's current block and turns it into the first item of a new list.
81
82
    The cursor's current list is found with \l{QTextCursor::currentList()}.
83
84
    The number of items in a list is given by count(). Each item can be
85
    obtained by its index in the list with the item() function. Similarly,
86
    the index of a given item can be found with itemNumber(). The text of
87
    each item can be found with the itemText() function.
88
89
    Note that the items in the list may not be adjacent elements in the
90
    document. For example, the top-level items in a multi-level list will
91
    be separated by the items in lower levels of the list.
92
93
    List items can be deleted by index with the removeItem() function.
94
    remove() deletes the specified item in the list.
95
96
    The list's format is set with setFormat() and read with format().
97
    The format describes the decoration of the list itself, and not the
98
    individual items.
99
100
    \sa QTextBlock, QTextListFormat, QTextCursor
101
*/
102
103
#if QT_DEPRECATED_SINCE(5, 13)
104
/*!
105
    \fn bool QTextList::isEmpty() const
106
    \obsolete
107
108
    Returns \c true if the list has no items; otherwise returns \c false.
109
110
    \b{Note:} Empty lists are automatically deleted by the QTextDocument that owns
111
    them.
112
113
    \sa count()
114
*/
115
#endif
116
117
/*! \internal
118
 */
119
QTextList::QTextList(QTextDocument *doc)
120
0
    : QTextBlockGroup(*new QTextListPrivate(doc), doc)
121
0
{
122
0
}
123
124
/*!
125
  \internal
126
*/
127
QTextList::~QTextList()
128
0
{
129
0
}
130
131
/*!
132
    Returns the number of items in the list.
133
*/
134
int QTextList::count() const
135
0
{
136
0
    Q_D(const QTextList);
137
0
    return d->blocks.count();
138
0
}
139
140
/*!
141
    Returns the \a{i}-th text block in the list.
142
143
    \sa count(), itemText()
144
*/
145
QTextBlock QTextList::item(int i) const
146
0
{
147
0
    Q_D(const QTextList);
148
0
    if (i < 0 || i >= d->blocks.size())
149
0
        return QTextBlock();
150
0
    return d->blocks.at(i);
151
0
}
152
153
/*!
154
    \fn void QTextList::setFormat(const QTextListFormat &format)
155
156
    Sets the list's format to \a format.
157
*/
158
159
/*!
160
    \fn QTextListFormat QTextList::format() const
161
162
    Returns the list's format.
163
*/
164
165
/*!
166
    \fn int QTextList::itemNumber(const QTextBlock &block) const
167
168
    Returns the index of the list item that corresponds to the given \a block.
169
    Returns -1 if the block was not present in the list.
170
*/
171
int QTextList::itemNumber(const QTextBlock &blockIt) const
172
0
{
173
0
    Q_D(const QTextList);
174
0
    return d->blocks.indexOf(blockIt);
175
0
}
176
177
/*!
178
    \fn QString QTextList::itemText(const QTextBlock &block) const
179
180
    Returns the text of the list item that corresponds to the given \a block.
181
*/
182
QString QTextList::itemText(const QTextBlock &blockIt) const
183
0
{
184
0
    Q_D(const QTextList);
185
0
    int item = d->blocks.indexOf(blockIt) + 1;
186
0
    if (item <= 0)
187
0
        return QString();
188
189
0
    QTextBlock block = d->blocks.at(item-1);
190
0
    QTextBlockFormat blockFormat = block.blockFormat();
191
192
0
    QString result;
193
194
0
    const int style = format().style();
195
0
    QString numberPrefix;
196
0
    QString numberSuffix = QLatin1String(".");
197
198
0
    if (format().hasProperty(QTextFormat::ListNumberPrefix))
199
0
        numberPrefix = format().numberPrefix();
200
0
    if (format().hasProperty(QTextFormat::ListNumberSuffix))
201
0
        numberSuffix = format().numberSuffix();
202
203
0
    switch (style) {
204
0
        case QTextListFormat::ListDecimal:
205
0
            result = QString::number(item);
206
0
            break;
207
            // from the old richtext
208
0
        case QTextListFormat::ListLowerAlpha:
209
0
        case QTextListFormat::ListUpperAlpha:
210
0
            {
211
0
                const char baseChar = style == QTextListFormat::ListUpperAlpha ? 'A' : 'a';
212
213
0
                int c = item;
214
0
                while (c > 0) {
215
0
                    c--;
216
0
                    result.prepend(QChar(baseChar + (c % 26)));
217
0
                    c /= 26;
218
0
                }
219
0
            }
220
0
            break;
221
0
        case QTextListFormat::ListLowerRoman:
222
0
        case QTextListFormat::ListUpperRoman:
223
0
            {
224
0
                if (item < 5000) {
225
0
                    QByteArray romanNumeral;
226
227
                    // works for up to 4999 items
228
0
                    static const char romanSymbolsLower[] = "iiivixxxlxcccdcmmmm";
229
0
                    static const char romanSymbolsUpper[] = "IIIVIXXXLXCCCDCMMMM";
230
0
                    QByteArray romanSymbols; // wrap to have "mid"
231
0
                    if (style == QTextListFormat::ListLowerRoman)
232
0
                        romanSymbols = QByteArray::fromRawData(romanSymbolsLower, sizeof(romanSymbolsLower));
233
0
                    else
234
0
                        romanSymbols = QByteArray::fromRawData(romanSymbolsUpper, sizeof(romanSymbolsUpper));
235
236
0
                    int c[] = { 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000 };
237
0
                    int n = item;
238
0
                    for (int i = 12; i >= 0; n %= c[i], i--) {
239
0
                        int q = n / c[i];
240
0
                        if (q > 0) {
241
0
                            int startDigit = i + (i+3)/4;
242
0
                            int numDigits;
243
0
                            if (i % 4) {
244
                                // c[i] == 4|5|9|40|50|90|400|500|900
245
0
                                if ((i-2) % 4) {
246
                                    // c[i] == 4|9|40|90|400|900 => with subtraction (IV, IX, XL, XC, ...)
247
0
                                    numDigits = 2;
248
0
                                }
249
0
                                else {
250
                                    // c[i] == 5|50|500 (V, L, D)
251
0
                                    numDigits = 1;
252
0
                                }
253
0
                            }
254
0
                            else {
255
                                // c[i] == 1|10|100|1000 (I, II, III, X, XX, ...)
256
0
                                numDigits = q;
257
0
                            }
258
259
0
                            romanNumeral.append(romanSymbols.mid(startDigit, numDigits));
260
0
                        }
261
0
                    }
262
0
                    result = QString::fromLatin1(romanNumeral);
263
0
                }
264
0
                else {
265
0
                    result = QLatin1String("?");
266
0
                }
267
268
0
            }
269
0
            break;
270
0
        default:
271
0
            Q_ASSERT(false);
272
0
    }
273
0
    if (blockIt.textDirection() == Qt::RightToLeft)
274
0
        return numberSuffix + result + numberPrefix;
275
0
    else
276
0
        return numberPrefix + result + numberSuffix;
277
0
}
278
279
/*!
280
    Removes the item at item position \a i from the list. When the last item in the
281
    list is removed, the list is automatically deleted by the QTextDocument that owns
282
    it.
283
284
    \sa add(), remove()
285
*/
286
void QTextList::removeItem(int i)
287
0
{
288
0
    Q_D(QTextList);
289
0
    if (i < 0 || i >= d->blocks.size())
290
0
        return;
291
292
0
    QTextBlock block = d->blocks.at(i);
293
0
    remove(block);
294
0
}
295
296
297
/*!
298
    Removes the given \a block from the list.
299
300
    \sa add(), removeItem()
301
*/
302
void QTextList::remove(const QTextBlock &block)
303
0
{
304
0
    QTextBlockFormat fmt = block.blockFormat();
305
0
    fmt.setIndent(fmt.indent() + format().indent());
306
0
    fmt.setObjectIndex(-1);
307
0
    block.docHandle()->setBlockFormat(block, block, fmt, QTextDocumentPrivate::SetFormat);
308
0
}
309
310
/*!
311
    Makes the given \a block part of the list.
312
313
    \sa remove(), removeItem()
314
*/
315
void QTextList::add(const QTextBlock &block)
316
0
{
317
0
    QTextBlockFormat fmt = block.blockFormat();
318
0
    fmt.setObjectIndex(objectIndex());
319
0
    block.docHandle()->setBlockFormat(block, block, fmt, QTextDocumentPrivate::SetFormat);
320
0
}
321
322
QT_END_NAMESPACE
323
324
#include "moc_qtextlist.cpp"