| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/gui/text/qtextlist.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 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 | : QTextBlockGroupPrivate(doc) | - | ||||||||||||
| 54 | { | - | ||||||||||||
| 55 | } never executed: end of block | 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 | /*! | - | ||||||||||||
| 104 | \fn bool QTextList::isEmpty() const | - | ||||||||||||
| 105 | \obsolete | - | ||||||||||||
| 106 | - | |||||||||||||
| 107 | Returns \c true if the list has no items; otherwise returns \c false. | - | ||||||||||||
| 108 | - | |||||||||||||
| 109 | \b{Note:} Empty lists are automatically deleted by the QTextDocument that owns | - | ||||||||||||
| 110 | them. | - | ||||||||||||
| 111 | - | |||||||||||||
| 112 | \sa count() | - | ||||||||||||
| 113 | */ | - | ||||||||||||
| 114 | - | |||||||||||||
| 115 | /*! \internal | - | ||||||||||||
| 116 | */ | - | ||||||||||||
| 117 | QTextList::QTextList(QTextDocument *doc) | - | ||||||||||||
| 118 | : QTextBlockGroup(*new QTextListPrivate(doc), doc) | - | ||||||||||||
| 119 | { | - | ||||||||||||
| 120 | } never executed: end of block | 0 | ||||||||||||
| 121 | - | |||||||||||||
| 122 | /*! | - | ||||||||||||
| 123 | \internal | - | ||||||||||||
| 124 | */ | - | ||||||||||||
| 125 | QTextList::~QTextList() | - | ||||||||||||
| 126 | { | - | ||||||||||||
| 127 | } | - | ||||||||||||
| 128 | - | |||||||||||||
| 129 | /*! | - | ||||||||||||
| 130 | Returns the number of items in the list. | - | ||||||||||||
| 131 | */ | - | ||||||||||||
| 132 | int QTextList::count() const | - | ||||||||||||
| 133 | { | - | ||||||||||||
| 134 | Q_D(const QTextList); | - | ||||||||||||
| 135 | return d->blocks.count(); never executed: return d->blocks.count(); | 0 | ||||||||||||
| 136 | } | - | ||||||||||||
| 137 | - | |||||||||||||
| 138 | /*! | - | ||||||||||||
| 139 | Returns the \a{i}-th text block in the list. | - | ||||||||||||
| 140 | - | |||||||||||||
| 141 | \sa count(), itemText() | - | ||||||||||||
| 142 | */ | - | ||||||||||||
| 143 | QTextBlock QTextList::item(int i) const | - | ||||||||||||
| 144 | { | - | ||||||||||||
| 145 | Q_D(const QTextList); | - | ||||||||||||
| 146 | if (i < 0 || i >= d->blocks.size())
| 0 | ||||||||||||
| 147 | return QTextBlock(); never executed: return QTextBlock(); | 0 | ||||||||||||
| 148 | return d->blocks.at(i); never executed: return d->blocks.at(i); | 0 | ||||||||||||
| 149 | } | - | ||||||||||||
| 150 | - | |||||||||||||
| 151 | /*! | - | ||||||||||||
| 152 | \fn void QTextList::setFormat(const QTextListFormat &format) | - | ||||||||||||
| 153 | - | |||||||||||||
| 154 | Sets the list's format to \a format. | - | ||||||||||||
| 155 | */ | - | ||||||||||||
| 156 | - | |||||||||||||
| 157 | /*! | - | ||||||||||||
| 158 | \fn QTextListFormat QTextList::format() const | - | ||||||||||||
| 159 | - | |||||||||||||
| 160 | Returns the list's format. | - | ||||||||||||
| 161 | */ | - | ||||||||||||
| 162 | - | |||||||||||||
| 163 | /*! | - | ||||||||||||
| 164 | \fn int QTextList::itemNumber(const QTextBlock &block) const | - | ||||||||||||
| 165 | - | |||||||||||||
| 166 | Returns the index of the list item that corresponds to the given \a block. | - | ||||||||||||
| 167 | Returns -1 if the block was not present in the list. | - | ||||||||||||
| 168 | */ | - | ||||||||||||
| 169 | int QTextList::itemNumber(const QTextBlock &blockIt) const | - | ||||||||||||
| 170 | { | - | ||||||||||||
| 171 | Q_D(const QTextList); | - | ||||||||||||
| 172 | return d->blocks.indexOf(blockIt); never executed: return d->blocks.indexOf(blockIt); | 0 | ||||||||||||
| 173 | } | - | ||||||||||||
| 174 | - | |||||||||||||
| 175 | /*! | - | ||||||||||||
| 176 | \fn QString QTextList::itemText(const QTextBlock &block) const | - | ||||||||||||
| 177 | - | |||||||||||||
| 178 | Returns the text of the list item that corresponds to the given \a block. | - | ||||||||||||
| 179 | */ | - | ||||||||||||
| 180 | QString QTextList::itemText(const QTextBlock &blockIt) const | - | ||||||||||||
| 181 | { | - | ||||||||||||
| 182 | Q_D(const QTextList); | - | ||||||||||||
| 183 | int item = d->blocks.indexOf(blockIt) + 1; | - | ||||||||||||
| 184 | if (item <= 0)
| 0 | ||||||||||||
| 185 | return QString(); never executed: return QString(); | 0 | ||||||||||||
| 186 | - | |||||||||||||
| 187 | QTextBlock block = d->blocks.at(item-1); | - | ||||||||||||
| 188 | QTextBlockFormat blockFormat = block.blockFormat(); | - | ||||||||||||
| 189 | - | |||||||||||||
| 190 | QString result; | - | ||||||||||||
| 191 | - | |||||||||||||
| 192 | const int style = format().style(); | - | ||||||||||||
| 193 | QString numberPrefix; | - | ||||||||||||
| 194 | QString numberSuffix = QLatin1String("."); | - | ||||||||||||
| 195 | - | |||||||||||||
| 196 | if (format().hasProperty(QTextFormat::ListNumberPrefix))
| 0 | ||||||||||||
| 197 | numberPrefix = format().numberPrefix(); never executed: numberPrefix = format().numberPrefix(); | 0 | ||||||||||||
| 198 | if (format().hasProperty(QTextFormat::ListNumberSuffix))
| 0 | ||||||||||||
| 199 | numberSuffix = format().numberSuffix(); never executed: numberSuffix = format().numberSuffix(); | 0 | ||||||||||||
| 200 | - | |||||||||||||
| 201 | switch (style) { | - | ||||||||||||
| 202 | case QTextListFormat::ListDecimal: never executed: case QTextListFormat::ListDecimal: | 0 | ||||||||||||
| 203 | result = QString::number(item); | - | ||||||||||||
| 204 | break; never executed: break; | 0 | ||||||||||||
| 205 | // from the old richtext | - | ||||||||||||
| 206 | case QTextListFormat::ListLowerAlpha: never executed: case QTextListFormat::ListLowerAlpha: | 0 | ||||||||||||
| 207 | case QTextListFormat::ListUpperAlpha: never executed: case QTextListFormat::ListUpperAlpha: | 0 | ||||||||||||
| 208 | { | - | ||||||||||||
| 209 | const char baseChar = style == QTextListFormat::ListUpperAlpha ? 'A' : 'a';
| 0 | ||||||||||||
| 210 | - | |||||||||||||
| 211 | int c = item; | - | ||||||||||||
| 212 | while (c > 0) {
| 0 | ||||||||||||
| 213 | c--; | - | ||||||||||||
| 214 | result.prepend(QChar(baseChar + (c % 26))); | - | ||||||||||||
| 215 | c /= 26; | - | ||||||||||||
| 216 | } never executed: end of block | 0 | ||||||||||||
| 217 | } | - | ||||||||||||
| 218 | break; never executed: break; | 0 | ||||||||||||
| 219 | case QTextListFormat::ListLowerRoman: never executed: case QTextListFormat::ListLowerRoman: | 0 | ||||||||||||
| 220 | case QTextListFormat::ListUpperRoman: never executed: case QTextListFormat::ListUpperRoman: | 0 | ||||||||||||
| 221 | { | - | ||||||||||||
| 222 | if (item < 5000) {
| 0 | ||||||||||||
| 223 | QByteArray romanNumeral; | - | ||||||||||||
| 224 | - | |||||||||||||
| 225 | // works for up to 4999 items | - | ||||||||||||
| 226 | static const char romanSymbolsLower[] = "iiivixxxlxcccdcmmmm"; | - | ||||||||||||
| 227 | static const char romanSymbolsUpper[] = "IIIVIXXXLXCCCDCMMMM"; | - | ||||||||||||
| 228 | QByteArray romanSymbols; // wrap to have "mid" | - | ||||||||||||
| 229 | if (style == QTextListFormat::ListLowerRoman)
| 0 | ||||||||||||
| 230 | romanSymbols = QByteArray::fromRawData(romanSymbolsLower, sizeof(romanSymbolsLower)); never executed: romanSymbols = QByteArray::fromRawData(romanSymbolsLower, sizeof(romanSymbolsLower)); | 0 | ||||||||||||
| 231 | else | - | ||||||||||||
| 232 | romanSymbols = QByteArray::fromRawData(romanSymbolsUpper, sizeof(romanSymbolsUpper)); never executed: romanSymbols = QByteArray::fromRawData(romanSymbolsUpper, sizeof(romanSymbolsUpper)); | 0 | ||||||||||||
| 233 | - | |||||||||||||
| 234 | int c[] = { 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000 }; | - | ||||||||||||
| 235 | int n = item; | - | ||||||||||||
| 236 | for (int i = 12; i >= 0; n %= c[i], i--) {
| 0 | ||||||||||||
| 237 | int q = n / c[i]; | - | ||||||||||||
| 238 | if (q > 0) {
| 0 | ||||||||||||
| 239 | int startDigit = i + (i+3)/4; | - | ||||||||||||
| 240 | int numDigits; | - | ||||||||||||
| 241 | if (i % 4) {
| 0 | ||||||||||||
| 242 | // c[i] == 4|5|9|40|50|90|400|500|900 | - | ||||||||||||
| 243 | if ((i-2) % 4) {
| 0 | ||||||||||||
| 244 | // c[i] == 4|9|40|90|400|900 => with subtraction (IV, IX, XL, XC, ...) | - | ||||||||||||
| 245 | numDigits = 2; | - | ||||||||||||
| 246 | } never executed: end of block | 0 | ||||||||||||
| 247 | else { | - | ||||||||||||
| 248 | // c[i] == 5|50|500 (V, L, D) | - | ||||||||||||
| 249 | numDigits = 1; | - | ||||||||||||
| 250 | } never executed: end of block | 0 | ||||||||||||
| 251 | } | - | ||||||||||||
| 252 | else { | - | ||||||||||||
| 253 | // c[i] == 1|10|100|1000 (I, II, III, X, XX, ...) | - | ||||||||||||
| 254 | numDigits = q; | - | ||||||||||||
| 255 | } never executed: end of block | 0 | ||||||||||||
| 256 | - | |||||||||||||
| 257 | romanNumeral.append(romanSymbols.mid(startDigit, numDigits)); | - | ||||||||||||
| 258 | } never executed: end of block | 0 | ||||||||||||
| 259 | } never executed: end of block | 0 | ||||||||||||
| 260 | result = QString::fromLatin1(romanNumeral); | - | ||||||||||||
| 261 | } never executed: end of block | 0 | ||||||||||||
| 262 | else { | - | ||||||||||||
| 263 | result = QLatin1String("?"); | - | ||||||||||||
| 264 | } never executed: end of block | 0 | ||||||||||||
| 265 | - | |||||||||||||
| 266 | } | - | ||||||||||||
| 267 | break; never executed: break; | 0 | ||||||||||||
| 268 | default: never executed: default: | 0 | ||||||||||||
| 269 | Q_ASSERT(false); | - | ||||||||||||
| 270 | } never executed: end of block | 0 | ||||||||||||
| 271 | if (blockIt.textDirection() == Qt::RightToLeft)
| 0 | ||||||||||||
| 272 | return numberSuffix + result + numberPrefix; never executed: return numberSuffix + result + numberPrefix; | 0 | ||||||||||||
| 273 | else | - | ||||||||||||
| 274 | return numberPrefix + result + numberSuffix; never executed: return numberPrefix + result + numberSuffix; | 0 | ||||||||||||
| 275 | } | - | ||||||||||||
| 276 | - | |||||||||||||
| 277 | /*! | - | ||||||||||||
| 278 | Removes the item at item position \a i from the list. When the last item in the | - | ||||||||||||
| 279 | list is removed, the list is automatically deleted by the QTextDocument that owns | - | ||||||||||||
| 280 | it. | - | ||||||||||||
| 281 | - | |||||||||||||
| 282 | \sa add(), remove() | - | ||||||||||||
| 283 | */ | - | ||||||||||||
| 284 | void QTextList::removeItem(int i) | - | ||||||||||||
| 285 | { | - | ||||||||||||
| 286 | Q_D(QTextList); | - | ||||||||||||
| 287 | if (i < 0 || i >= d->blocks.size())
| 0 | ||||||||||||
| 288 | return; never executed: return; | 0 | ||||||||||||
| 289 | - | |||||||||||||
| 290 | QTextBlock block = d->blocks.at(i); | - | ||||||||||||
| 291 | remove(block); | - | ||||||||||||
| 292 | } never executed: end of block | 0 | ||||||||||||
| 293 | - | |||||||||||||
| 294 | - | |||||||||||||
| 295 | /*! | - | ||||||||||||
| 296 | Removes the given \a block from the list. | - | ||||||||||||
| 297 | - | |||||||||||||
| 298 | \sa add(), removeItem() | - | ||||||||||||
| 299 | */ | - | ||||||||||||
| 300 | void QTextList::remove(const QTextBlock &block) | - | ||||||||||||
| 301 | { | - | ||||||||||||
| 302 | QTextBlockFormat fmt = block.blockFormat(); | - | ||||||||||||
| 303 | fmt.setIndent(fmt.indent() + format().indent()); | - | ||||||||||||
| 304 | fmt.setObjectIndex(-1); | - | ||||||||||||
| 305 | block.docHandle()->setBlockFormat(block, block, fmt, QTextDocumentPrivate::SetFormat); | - | ||||||||||||
| 306 | } never executed: end of block | 0 | ||||||||||||
| 307 | - | |||||||||||||
| 308 | /*! | - | ||||||||||||
| 309 | Makes the given \a block part of the list. | - | ||||||||||||
| 310 | - | |||||||||||||
| 311 | \sa remove(), removeItem() | - | ||||||||||||
| 312 | */ | - | ||||||||||||
| 313 | void QTextList::add(const QTextBlock &block) | - | ||||||||||||
| 314 | { | - | ||||||||||||
| 315 | QTextBlockFormat fmt = block.blockFormat(); | - | ||||||||||||
| 316 | fmt.setObjectIndex(objectIndex()); | - | ||||||||||||
| 317 | block.docHandle()->setBlockFormat(block, block, fmt, QTextDocumentPrivate::SetFormat); | - | ||||||||||||
| 318 | } never executed: end of block | 0 | ||||||||||||
| 319 | - | |||||||||||||
| 320 | QT_END_NAMESPACE | - | ||||||||||||
| Source code | Switch to Preprocessed file |