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