📄 qtextobject.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qtextobject.h"#include "qtextobject_p.h"#include "qtextdocument.h"#include "qtextformat_p.h"#include "qtextdocument_p.h"#include "qtextcursor.h"#include "qtextlist.h"#include "qabstracttextdocumentlayout.h"#include "qdebug.h"// ### DOC: We ought to explain the CONCEPT of objectIndexes if// relevant to the public API/*! \class QTextObject \brief The QTextObject class is a base class for different kinds of objects that can group parts of a QTextDocument together. \ingroup text The common grouping text objects are lists (QTextList), frames (QTextFrame), and tables (QTextTable). A text object has an associated format() and document(). There are essentially two kinds of text objects: those that are used with blocks (block formats), and those that are used with characters (character formats). The first kind are derived from QTextBlockGroup, and the second kind from QTextFrame. You rarely need to use this class directly. When creating custom text objects, you will also need to reimplement QTextDocument::createObject() which acts as a factory method for creating text objects. \sa QTextDocument*//*! \fn QTextObject::QTextObject(QTextDocument *document) Creates a new QTextObject for the given \a document. \warning This function should never be called directly, but only from QTextDocument::createObject().*/QTextObject::QTextObject(QTextDocument *doc) : QObject(*new QTextObjectPrivate, doc){}/*! \fn QTextObject::QTextObject(QTextObjectPrivate &p, QTextDocument *document) \internal*/QTextObject::QTextObject(QTextObjectPrivate &p, QTextDocument *doc) :QObject(p, doc){}/*! Destroys the text object. \warning Text objects are owned by the document, so you should never destroy them yourself.*/QTextObject::~QTextObject(){}/*! Returns the text object's format. \sa setFormat() document()*/QTextFormat QTextObject::format() const{ Q_D(const QTextObject); return d->pieceTable->formatCollection()->objectFormat(d->objectIndex);}/*! Returns the index of the object's format in the document's internal list of formats. \sa QTextDocument::object()*/int QTextObject::formatIndex() const{ Q_D(const QTextObject); return d->pieceTable->formatCollection()->objectFormatIndex(d->objectIndex);}/*! Sets the text object's \a format. \sa format()*/void QTextObject::setFormat(const QTextFormat &format){ Q_D(QTextObject); int idx = d->pieceTable->formatCollection()->indexForFormat(format); d->pieceTable->changeObjectFormat(this, idx);}/*! Returns the object index of this object. This can be used together with QTextFormat::setObjectIndex().*/int QTextObject::objectIndex() const{ Q_D(const QTextObject); return d->objectIndex;}/*! Returns the document this object belongs to. \sa format()*/QTextDocument *QTextObject::document() const{ return qobject_cast<QTextDocument *>(parent());}/*! \internal*/QTextDocumentPrivate *QTextObject::docHandle() const{ return qobject_cast<const QTextDocument *>(parent())->docHandle();}/*! \class QTextBlockGroup \brief The QTextBlockGroup class provides a container for text blocks within a QTextDocument. \ingroup text Block groups can be used to organize blocks of text within a document. They maintain an up-to-date list of the text blocks that belong to them, even when text blocks are being edited. Each group has a parent document which is specified when the group is constructed. Text blocks can be inserted into a group with blockInserted(), and removed with blockRemoved(). If a block's format is changed, blockFormatChanged() is called. The list of blocks in the group is returned by blockList(). Note that the blocks in the list are not necessarily adjacent elements in the document; for example, the top-level items in a multi-level list will be separated by the items in lower levels of the list. \sa QTextBlock QTextDocument*/void QTextBlockGroupPrivate::markBlocksDirty(){ for (int i = 0; i < blocks.count(); ++i) { const QTextBlock &block = blocks.at(i); pieceTable->documentChange(block.position(), block.length()); }}/*! \fn QTextBlockGroup::QTextBlockGroup(QTextDocument *document) Creates a new new block group for the given \a document. \warning This function should only be called from QTextDocument::createObject().*/QTextBlockGroup::QTextBlockGroup(QTextDocument *doc) : QTextObject(*new QTextBlockGroupPrivate, doc){}/*! \internal*/QTextBlockGroup::QTextBlockGroup(QTextBlockGroupPrivate &p, QTextDocument *doc) : QTextObject(p, doc){}/*! Destroys this block group; the blocks are not deleted, they simply don't belong to this block anymore.*/QTextBlockGroup::~QTextBlockGroup(){}// ### DOC: Shouldn't this be insertBlock()?/*! Appends the given \a block to the end of the group. \warning If you reimplement this function you must call the base class implementation.*/void QTextBlockGroup::blockInserted(const QTextBlock &block){ Q_D(QTextBlockGroup); QTextBlockGroupPrivate::BlockList::Iterator it = qLowerBound(d->blocks.begin(), d->blocks.end(), block); d->blocks.insert(it, block); d->markBlocksDirty();}// ### DOC: Shouldn't this be removeBlock()?/*! Removes the given \a block from the group; the block itself is not deleted, it simply isn't a member of this group anymore.*/void QTextBlockGroup::blockRemoved(const QTextBlock &block){ Q_D(QTextBlockGroup); d->blocks.removeAll(block); d->markBlocksDirty(); if (d->blocks.isEmpty()) { document()->docHandle()->deleteObject(this); return; }}/*! This function is called whenever the specified \a block of text is changed. The text block is a member of this group. The base class implementation does nothing.*/void QTextBlockGroup::blockFormatChanged(const QTextBlock &){}/*! Returns a (possibly empty) list of all the blocks that are part of the block group.*/QList<QTextBlock> QTextBlockGroup::blockList() const{ Q_D(const QTextBlockGroup); return d->blocks;}QTextFrameLayoutData::~QTextFrameLayoutData(){}/*! \class QTextFrame \brief The QTextFrame class represents a frame in a QTextDocument. \ingroup text Text frames provide structure for the text in a document. They are used as generic containers for other document elements. Frames are usually created by using QTextCursor::insertFrame(). \omit Each frame in a document consists of a frame start character, QChar(0xFDD0), followed by the frame's contents, followed by a frame end character, QChar(0xFDD1). The character formats of the start and end character contain a reference to the frame object's objectIndex. \endomit Frames can be used to create hierarchical structures in rich text documents. Each document has a root frame (QTextDocument::rootFrame()), and each frame beneath the root frame has a parent frame and a (possibly empty) list of child frames. The parent frame can be found with parentFrame(), and the childFrames() function provides a list of child frames. Each frame contains at least one text block to enable text cursors to insert new document elements within. As a result, the QTextFrame::iterator class is used to traverse both the blocks and child frames within a given frame. The first and last child elements in the frame can be found with begin() and end(). A frame also has a format (specified using QTextFrameFormat) which can be set with setFormat() and read with format(). Text cursors can be obtained that point to the first and last valid cursor positions within a frame; use the firstCursorPosition() and lastCursorPosition() functions for this. The frame's extent in the document can be found with firstPosition() and lastPosition(). You can iterate over a frame's contents using the QTextFrame::iterator class: this provides read-only access to its internal list of text blocks and child frames. \sa QTextCursor QTextDocument*//*! \typedef QTextFrame::Iterator Qt-style synonym for QTextFrame::iterator.*//*! \fn QTextFrame *QTextFrame::iterator::parentFrame() const Returns the parent frame of the current frame. \sa currentFrame() QTextFrame::parentFrame()*//*! \fn bool QTextFrame::iterator::operator==(const iterator &other) const Retuns true if the iterator is the same as the \a other iterator; otherwise returns false.*//*! \fn bool QTextFrame::iterator::operator!=(const iterator &other) const Retuns true if the iterator is different from the \a other iterator; otherwise returns false.*//*! \fn QTextFrame::iterator QTextFrame::iterator::operator++(int) The postfix ++ operator (\c{i++}) advances the iterator to the next item in the text frame, and returns an iterator to the old item.*//*! \fn QTextFrame::iterator QTextFrame::iterator::operator--(int) The postfix -- operator (\c{i--}) makes the preceding item in the current frame, and returns an iterator to the old item.*//*! \fn void QTextFrame::setFrameFormat(const QTextFrameFormat &format) Sets the frame's \a format. \sa frameFormat()*//*! \fn QTextFrameFormat QTextFrame::frameFormat() const Returns the frame's format. \sa setFrameFormat()*//*! \fn QTextFrame::QTextFrame(QTextDocument *document) Creates a new empty frame for the text \a document.*/QTextFrame::QTextFrame(QTextDocument *doc) : QTextObject(*new QTextFramePrivate, doc){ Q_D(QTextFrame); d->fragment_start = 0; d->fragment_end = 0; d->parentFrame = 0; d->layoutData = 0;}// ### DOC: What does this do to child frames?/*! Destroys the frame, and removes it from the document's layout.*/QTextFrame::~QTextFrame(){ Q_D(QTextFrame); delete d->layoutData;}/*! \internal*/QTextFrame::QTextFrame(QTextFramePrivate &p, QTextDocument *doc) : QTextObject(p, doc){ Q_D(QTextFrame); d->fragment_start = 0; d->fragment_end = 0; d->parentFrame = 0; d->layoutData = 0;}/*! Returns a (possibly empty) list of the frame's child frames. \sa parentFrame()*/QList<QTextFrame *> QTextFrame::childFrames() const{ Q_D(const QTextFrame); return d->childFrames;}/*! Returns the frame's parent frame. If the frame is the root frame of a document, this will return 0. \sa childFrames() QTextDocument::rootFrame()*/QTextFrame *QTextFrame::parentFrame() const{ Q_D(const QTextFrame); return d->parentFrame;}/*! Returns the first cursor position inside the frame. \sa lastCursorPosition() firstPosition() lastPosition()*/QTextCursor QTextFrame::firstCursorPosition() const{ Q_D(const QTextFrame); return QTextCursor(d->pieceTable, firstPosition());}/*! Returns the last cursor position inside the frame. \sa firstCursorPosition() firstPosition() lastPosition()*/QTextCursor QTextFrame::lastCursorPosition() const{ Q_D(const QTextFrame); return QTextCursor(d->pieceTable, lastPosition());}/*! Returns the first document position inside the frame. \sa lastPosition() firstCursorPosition() lastCursorPosition()*/int QTextFrame::firstPosition() const{ Q_D(const QTextFrame); if (!d->fragment_start) return 0; return d->pieceTable->fragmentMap().position(d->fragment_start) + 1;}/*! Returns the last document position inside the frame. \sa firstPosition() firstCursorPosition() lastCursorPosition()*/int QTextFrame::lastPosition() const{ Q_D(const QTextFrame); if (!d->fragment_end) return d->pieceTable->length() - 1; return d->pieceTable->fragmentMap().position(d->fragment_end);}/*! \internal*/QTextFrameLayoutData *QTextFrame::layoutData() const{ Q_D(const QTextFrame); return d->layoutData;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -