| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 | #include "qtextlist.h"
|
|---|
| 44 | #include "qtextobject_p.h"
|
|---|
| 45 | #include "qtextcursor.h"
|
|---|
| 46 | #include "qtextdocument_p.h"
|
|---|
| 47 | #include <qdebug.h>
|
|---|
| 48 |
|
|---|
| 49 | QT_BEGIN_NAMESPACE
|
|---|
| 50 |
|
|---|
| 51 | class QTextListPrivate : public QTextBlockGroupPrivate
|
|---|
| 52 | {
|
|---|
| 53 | public:
|
|---|
| 54 | QTextListPrivate(QTextDocument *doc)
|
|---|
| 55 | : QTextBlockGroupPrivate(doc)
|
|---|
| 56 | {
|
|---|
| 57 | }
|
|---|
| 58 | };
|
|---|
| 59 |
|
|---|
| 60 | /*!
|
|---|
| 61 | \class QTextList
|
|---|
| 62 | \reentrant
|
|---|
| 63 |
|
|---|
| 64 | \brief The QTextList class provides a decorated list of items in a QTextDocument.
|
|---|
| 65 |
|
|---|
| 66 | \ingroup text
|
|---|
| 67 |
|
|---|
| 68 | A list contains a sequence of text blocks, each of which is marked with a
|
|---|
| 69 | bullet point or other symbol. Multiple levels of lists can be used, and
|
|---|
| 70 | the automatic numbering feature provides support for ordered numeric and
|
|---|
| 71 | alphabetical lists.
|
|---|
| 72 |
|
|---|
| 73 | Lists are created by using a text cursor to insert an empty list at the
|
|---|
| 74 | current position or by moving existing text into a new list.
|
|---|
| 75 | The \l{QTextCursor::insertList()} function inserts an empty block into the
|
|---|
| 76 | document at the cursor position, and makes it the first item in a list.
|
|---|
| 77 |
|
|---|
| 78 | \snippet doc/src/snippets/textdocument-lists/mainwindow.cpp 0
|
|---|
| 79 |
|
|---|
| 80 | The \l{QTextCursor::createList()} function takes the contents of the
|
|---|
| 81 | cursor's current block and turns it into the first item of a new list.
|
|---|
| 82 |
|
|---|
| 83 | The cursor's current list is found with \l{QTextCursor::currentList()}.
|
|---|
| 84 |
|
|---|
| 85 | The number of items in a list is given by count(). Each item can be
|
|---|
| 86 | obtained by its index in the list with the item() function. Similarly,
|
|---|
| 87 | the index of a given item can be found with itemNumber(). The text of
|
|---|
| 88 | each item can be found with the itemText() function.
|
|---|
| 89 |
|
|---|
| 90 | Note that the items in the list may not be adjacent elements in the
|
|---|
| 91 | document. For example, the top-level items in a multi-level list will
|
|---|
| 92 | be separated by the items in lower levels of the list.
|
|---|
| 93 |
|
|---|
| 94 | List items can be deleted by index with the removeItem() function.
|
|---|
| 95 | remove() deletes the specified item in the list.
|
|---|
| 96 |
|
|---|
| 97 | The list's format is set with setFormat() and read with format().
|
|---|
| 98 | The format describes the decoration of the list itself, and not the
|
|---|
| 99 | individual items.
|
|---|
| 100 |
|
|---|
| 101 | \sa QTextBlock, QTextListFormat, QTextCursor
|
|---|
| 102 | */
|
|---|
| 103 |
|
|---|
| 104 | /*!
|
|---|
| 105 | \fn bool QTextList::isEmpty() const
|
|---|
| 106 | \obsolete
|
|---|
| 107 |
|
|---|
| 108 | Returns true if the list has no items; otherwise returns false.
|
|---|
| 109 |
|
|---|
| 110 | \bold{Note:} Empty lists are automatically deleted by the QTextDocument that owns
|
|---|
| 111 | them.
|
|---|
| 112 |
|
|---|
| 113 | \sa count()
|
|---|
| 114 | */
|
|---|
| 115 |
|
|---|
| 116 | /*! \internal
|
|---|
| 117 | */
|
|---|
| 118 | QTextList::QTextList(QTextDocument *doc)
|
|---|
| 119 | : QTextBlockGroup(*new QTextListPrivate(doc), doc)
|
|---|
| 120 | {
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /*!
|
|---|
| 124 | \internal
|
|---|
| 125 | */
|
|---|
| 126 | QTextList::~QTextList()
|
|---|
| 127 | {
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /*!
|
|---|
| 131 | Returns the number of items in the list.
|
|---|
| 132 |
|
|---|
| 133 | \sa isEmpty()
|
|---|
| 134 | */
|
|---|
| 135 | int QTextList::count() const
|
|---|
| 136 | {
|
|---|
| 137 | Q_D(const QTextList);
|
|---|
| 138 | return d->blocks.count();
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /*!
|
|---|
| 142 | Returns the \a{i}-th text block in the list.
|
|---|
| 143 |
|
|---|
| 144 | \sa count() itemText()
|
|---|
| 145 | */
|
|---|
| 146 | QTextBlock QTextList::item(int i) const
|
|---|
| 147 | {
|
|---|
| 148 | Q_D(const QTextList);
|
|---|
| 149 | if (i < 0 || i >= d->blocks.size())
|
|---|
| 150 | return QTextBlock();
|
|---|
| 151 | return d->blocks.at(i);
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /*!
|
|---|
| 155 | \fn void QTextList::setFormat(const QTextListFormat &format)
|
|---|
| 156 |
|
|---|
| 157 | Sets the list's format to \a format.
|
|---|
| 158 | */
|
|---|
| 159 |
|
|---|
| 160 | /*!
|
|---|
| 161 | \fn QTextListFormat QTextList::format() const
|
|---|
| 162 |
|
|---|
| 163 | Returns the list's format.
|
|---|
| 164 | */
|
|---|
| 165 |
|
|---|
| 166 | /*!
|
|---|
| 167 | \fn int QTextList::itemNumber(const QTextBlock &block) const
|
|---|
| 168 |
|
|---|
| 169 | Returns the index of the list item that corresponds to the given \a block.
|
|---|
| 170 | Returns -1 if the block was not present in the list.
|
|---|
| 171 | */
|
|---|
| 172 | int QTextList::itemNumber(const QTextBlock &blockIt) const
|
|---|
| 173 | {
|
|---|
| 174 | Q_D(const QTextList);
|
|---|
| 175 | return d->blocks.indexOf(blockIt);
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | /*!
|
|---|
| 179 | \fn QString QTextList::itemText(const QTextBlock &block) const
|
|---|
| 180 |
|
|---|
| 181 | Returns the text of the list item that corresponds to the given \a block.
|
|---|
| 182 | */
|
|---|
| 183 | QString QTextList::itemText(const QTextBlock &blockIt) const
|
|---|
| 184 | {
|
|---|
| 185 | Q_D(const QTextList);
|
|---|
| 186 | int item = d->blocks.indexOf(blockIt) + 1;
|
|---|
| 187 | if (item <= 0)
|
|---|
| 188 | return QString();
|
|---|
| 189 |
|
|---|
| 190 | QTextBlock block = d->blocks.at(item-1);
|
|---|
| 191 | QTextBlockFormat blockFormat = block.blockFormat();
|
|---|
| 192 |
|
|---|
| 193 | QString result;
|
|---|
| 194 |
|
|---|
| 195 | const int style = format().style();
|
|---|
| 196 |
|
|---|
| 197 | switch (style) {
|
|---|
| 198 | case QTextListFormat::ListDecimal:
|
|---|
| 199 | result = QString::number(item);
|
|---|
| 200 | break;
|
|---|
| 201 | // from the old richtext
|
|---|
| 202 | case QTextListFormat::ListLowerAlpha:
|
|---|
| 203 | case QTextListFormat::ListUpperAlpha:
|
|---|
| 204 | {
|
|---|
| 205 | const char baseChar = style == QTextListFormat::ListUpperAlpha ? 'A' : 'a';
|
|---|
| 206 |
|
|---|
| 207 | int c = item;
|
|---|
| 208 | while (c > 0) {
|
|---|
| 209 | c--;
|
|---|
| 210 | result.prepend(QChar(baseChar + (c % 26)));
|
|---|
| 211 | c /= 26;
|
|---|
| 212 | }
|
|---|
| 213 | }
|
|---|
| 214 | break;
|
|---|
| 215 | default:
|
|---|
| 216 | Q_ASSERT(false);
|
|---|
| 217 | }
|
|---|
| 218 | if (blockFormat.layoutDirection() == Qt::RightToLeft)
|
|---|
| 219 | return result.prepend(QLatin1Char('.'));
|
|---|
| 220 | return result + QLatin1Char('.');
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | /*!
|
|---|
| 224 | Removes the item at item position \a i from the list. When the last item in the
|
|---|
| 225 | list is removed, the list is automatically deleted by the QTextDocument that owns
|
|---|
| 226 | it.
|
|---|
| 227 |
|
|---|
| 228 | \sa add(), remove()
|
|---|
| 229 | */
|
|---|
| 230 | void QTextList::removeItem(int i)
|
|---|
| 231 | {
|
|---|
| 232 | Q_D(QTextList);
|
|---|
| 233 | if (i < 0 || i >= d->blocks.size())
|
|---|
| 234 | return;
|
|---|
| 235 |
|
|---|
| 236 | QTextBlock block = d->blocks.at(i);
|
|---|
| 237 | remove(block);
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 | /*!
|
|---|
| 242 | Removes the given \a block from the list.
|
|---|
| 243 |
|
|---|
| 244 | \sa add(), removeItem()
|
|---|
| 245 | */
|
|---|
| 246 | void QTextList::remove(const QTextBlock &block)
|
|---|
| 247 | {
|
|---|
| 248 | QTextBlockFormat fmt = block.blockFormat();
|
|---|
| 249 | fmt.setIndent(fmt.indent() + format().indent());
|
|---|
| 250 | fmt.setObjectIndex(-1);
|
|---|
| 251 | block.docHandle()->setBlockFormat(block, block, fmt, QTextDocumentPrivate::SetFormat);
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | /*!
|
|---|
| 255 | Makes the given \a block part of the list.
|
|---|
| 256 |
|
|---|
| 257 | \sa remove(), removeItem()
|
|---|
| 258 | */
|
|---|
| 259 | void QTextList::add(const QTextBlock &block)
|
|---|
| 260 | {
|
|---|
| 261 | QTextBlockFormat fmt = block.blockFormat();
|
|---|
| 262 | fmt.setObjectIndex(objectIndex());
|
|---|
| 263 | block.docHandle()->setBlockFormat(block, block, fmt, QTextDocumentPrivate::SetFormat);
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | QT_END_NAMESPACE
|
|---|