qtextdocument.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtGui module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QTEXTDOCUMENT_H
00043 #define QTEXTDOCUMENT_H
00044 
00045 #include <QtCore/qobject.h>
00046 #include <QtCore/qsize.h>
00047 #include <QtCore/qrect.h>
00048 #include <QtGui/qfont.h>
00049 
00050 QT_BEGIN_HEADER
00051 
00052 QT_BEGIN_NAMESPACE
00053 
00054 QT_MODULE(Gui)
00055 
00056 class QTextFormatCollection;
00057 class QTextListFormat;
00058 class QRect;
00059 class QPainter;
00060 class QPrinter;
00061 class QAbstractTextDocumentLayout;
00062 class QPoint;
00063 class QTextCursor;
00064 class QTextObject;
00065 class QTextFormat;
00066 class QTextFrame;
00067 class QTextBlock;
00068 class QTextCodec;
00069 class QUrl;
00070 class QVariant;
00071 class QRectF;
00072 class QTextOption;
00073 
00074 template<typename T> class QVector;
00075 
00076 namespace Qt
00077 {
00078     enum HitTestAccuracy { ExactHit, FuzzyHit };
00079     enum WhiteSpaceMode {
00080         WhiteSpaceNormal,
00081         WhiteSpacePre,
00082         WhiteSpaceNoWrap,
00083         WhiteSpaceModeUndefined = -1
00084     };
00085 
00086     Q_GUI_EXPORT bool mightBeRichText(const QString&);
00087     Q_GUI_EXPORT QString escape(const QString& plain);
00088     Q_GUI_EXPORT QString convertFromPlainText(const QString &plain, WhiteSpaceMode mode = WhiteSpacePre);
00089 
00090 #ifndef QT_NO_TEXTCODEC
00091     Q_GUI_EXPORT QTextCodec *codecForHtml(const QByteArray &ba);
00092 #endif
00093 }
00094 
00095 class Q_GUI_EXPORT QAbstractUndoItem
00096 {
00097 public:
00098     virtual ~QAbstractUndoItem() = 0;
00099     virtual void undo() = 0;
00100     virtual void redo() = 0;
00101 };
00102 
00103 inline QAbstractUndoItem::~QAbstractUndoItem()
00104 {
00105 }
00106 
00107 class QTextDocumentPrivate;
00108 
00109 class Q_GUI_EXPORT QTextDocument : public QObject
00110 {
00111     Q_OBJECT
00112 
00113     Q_PROPERTY(bool undoRedoEnabled READ isUndoRedoEnabled WRITE setUndoRedoEnabled)
00114     Q_PROPERTY(bool modified READ isModified WRITE setModified DESIGNABLE false)
00115     Q_PROPERTY(QSizeF pageSize READ pageSize WRITE setPageSize)
00116     Q_PROPERTY(QFont defaultFont READ defaultFont WRITE setDefaultFont)
00117     Q_PROPERTY(bool useDesignMetrics READ useDesignMetrics WRITE setUseDesignMetrics)
00118     Q_PROPERTY(QSizeF size READ size)
00119     Q_PROPERTY(qreal textWidth READ textWidth WRITE setTextWidth)
00120     Q_PROPERTY(int blockCount READ blockCount)
00121     Q_PROPERTY(qreal indentWidth READ indentWidth WRITE setIndentWidth)
00122 #ifndef QT_NO_CSSPARSER
00123     Q_PROPERTY(QString defaultStyleSheet READ defaultStyleSheet WRITE setDefaultStyleSheet)
00124 #endif
00125     Q_PROPERTY(int maximumBlockCount READ maximumBlockCount WRITE setMaximumBlockCount)
00126     Q_PROPERTY(qreal documentMargin READ documentMargin WRITE setDocumentMargin)
00127     QDOC_PROPERTY(QTextOption defaultTextOption READ defaultTextOption WRITE setDefaultTextOption)
00128 
00129 public:
00130     explicit QTextDocument(QObject *parent = 0);
00131     explicit QTextDocument(const QString &text, QObject *parent = 0);
00132     ~QTextDocument();
00133 
00134     QTextDocument *clone(QObject *parent = 0) const;
00135 
00136     bool isEmpty() const;
00137     virtual void clear();
00138 
00139     void setUndoRedoEnabled(bool enable);
00140     bool isUndoRedoEnabled() const;
00141 
00142     bool isUndoAvailable() const;
00143     bool isRedoAvailable() const;
00144 
00145     int availableUndoSteps() const;
00146     int availableRedoSteps() const;
00147 
00148     int revision() const;
00149 
00150     void setDocumentLayout(QAbstractTextDocumentLayout *layout);
00151     QAbstractTextDocumentLayout *documentLayout() const;
00152 
00153     enum MetaInformation {
00154         DocumentTitle,
00155         DocumentUrl
00156     };
00157     void setMetaInformation(MetaInformation info, const QString &);
00158     QString metaInformation(MetaInformation info) const;
00159 
00160 #ifndef QT_NO_TEXTHTMLPARSER
00161     QString toHtml(const QByteArray &encoding = QByteArray()) const;
00162     void setHtml(const QString &html);
00163 #endif
00164 
00165     QString toPlainText() const;
00166     void setPlainText(const QString &text);
00167 
00168     QChar characterAt(int pos) const;
00169 
00170     enum FindFlag
00171     {
00172         FindBackward        = 0x00001,
00173         FindCaseSensitively = 0x00002,
00174         FindWholeWords      = 0x00004
00175     };
00176     Q_DECLARE_FLAGS(FindFlags, FindFlag)
00177 
00178     QTextCursor find(const QString &subString, int from = 0, FindFlags options = 0) const;
00179     QTextCursor find(const QString &subString, const QTextCursor &from, FindFlags options = 0) const;
00180 
00181     QTextCursor find(const QRegExp &expr, int from = 0, FindFlags options = 0) const;
00182     QTextCursor find(const QRegExp &expr, const QTextCursor &from, FindFlags options = 0) const;
00183 
00184     QTextFrame *frameAt(int pos) const;
00185     QTextFrame *rootFrame() const;
00186 
00187     QTextObject *object(int objectIndex) const;
00188     QTextObject *objectForFormat(const QTextFormat &) const;
00189 
00190     QTextBlock findBlock(int pos) const;
00191     QTextBlock findBlockByNumber(int blockNumber) const;
00192     QTextBlock findBlockByLineNumber(int blockNumber) const;
00193     QTextBlock begin() const;
00194     QTextBlock end() const;
00195 
00196     QTextBlock firstBlock() const;
00197     QTextBlock lastBlock() const;
00198 
00199     void setPageSize(const QSizeF &size);
00200     QSizeF pageSize() const;
00201 
00202     void setDefaultFont(const QFont &font);
00203     QFont defaultFont() const;
00204 
00205     int pageCount() const;
00206 
00207     bool isModified() const;
00208 
00209 #ifndef QT_NO_PRINTER
00210     void print(QPrinter *printer) const;
00211 #endif
00212 
00213     enum ResourceType {
00214         HtmlResource  = 1,
00215         ImageResource = 2,
00216         StyleSheetResource = 3,
00217 
00218         UserResource  = 100
00219     };
00220 
00221     QVariant resource(int type, const QUrl &name) const;
00222     void addResource(int type, const QUrl &name, const QVariant &resource);
00223 
00224     QVector<QTextFormat> allFormats() const;
00225 
00226     void markContentsDirty(int from, int length);
00227 
00228     void setUseDesignMetrics(bool b);
00229     bool useDesignMetrics() const;
00230 
00231     void drawContents(QPainter *painter, const QRectF &rect = QRectF());
00232 
00233     void setTextWidth(qreal width);
00234     qreal textWidth() const;
00235 
00236     qreal idealWidth() const;
00237 
00238     qreal indentWidth() const;
00239     void setIndentWidth(qreal width);
00240 
00241     qreal documentMargin() const;
00242     void setDocumentMargin(qreal margin);
00243 
00244     void adjustSize();
00245     QSizeF size() const;
00246 
00247     int blockCount() const;
00248     int lineCount() const;
00249     int characterCount() const;
00250 
00251 #ifndef QT_NO_CSSPARSER
00252     void setDefaultStyleSheet(const QString &sheet);
00253     QString defaultStyleSheet() const;
00254 #endif
00255 
00256     void undo(QTextCursor *cursor);
00257     void redo(QTextCursor *cursor);
00258 
00259     enum Stacks {
00260         UndoStack = 0x01,
00261         RedoStack = 0x02,
00262         UndoAndRedoStacks = UndoStack | RedoStack
00263     };
00264     void clearUndoRedoStacks(Stacks historyToClear = UndoAndRedoStacks);
00265 
00266     int maximumBlockCount() const;
00267     void setMaximumBlockCount(int maximum);
00268 
00269     QTextOption defaultTextOption() const;
00270     void setDefaultTextOption(const QTextOption &option);
00271 
00272 Q_SIGNALS:
00273     void contentsChange(int from, int charsRemoves, int charsAdded);
00274     void contentsChanged();
00275     void undoAvailable(bool);
00276     void redoAvailable(bool);
00277     void undoCommandAdded();
00278     void modificationChanged(bool m);
00279     void cursorPositionChanged(const QTextCursor &cursor);
00280     void blockCountChanged(int newBlockCount);
00281 
00282     void documentLayoutChanged();
00283 
00284 public Q_SLOTS:
00285     void undo();
00286     void redo();
00287     void appendUndoItem(QAbstractUndoItem *);
00288     void setModified(bool m = true);
00289 
00290 protected:
00291     virtual QTextObject *createObject(const QTextFormat &f);
00292     virtual QVariant loadResource(int type, const QUrl &name);
00293 
00294     QTextDocument(QTextDocumentPrivate &dd, QObject *parent);
00295 public:
00296     QTextDocumentPrivate *docHandle() const;
00297 private:
00298     Q_DISABLE_COPY(QTextDocument)
00299     Q_DECLARE_PRIVATE(QTextDocument)
00300     friend class QTextObjectPrivate;
00301 };
00302 
00303 Q_DECLARE_OPERATORS_FOR_FLAGS(QTextDocument::FindFlags)
00304 
00305 QT_END_NAMESPACE
00306 
00307 QT_END_HEADER
00308 
00309 #endif // QTEXTDOCUMENT_H