qtextedit.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 QTEXTEDIT_H
00043 #define QTEXTEDIT_H
00044 
00045 #include <QtGui/qabstractscrollarea.h>
00046 #include <QtGui/qtextdocument.h>
00047 #include <QtGui/qtextoption.h>
00048 #include <QtGui/qtextcursor.h>
00049 #include <QtGui/qtextformat.h>
00050 
00051 #ifndef QT_NO_TEXTEDIT
00052 
00053 #ifdef QT3_SUPPORT
00054 #include <QtGui/qtextobject.h>
00055 #include <QtGui/qtextlayout.h>
00056 #endif
00057 
00058 QT_BEGIN_HEADER
00059 
00060 QT_BEGIN_NAMESPACE
00061 
00062 QT_MODULE(Gui)
00063 
00064 class QStyleSheet;
00065 class QTextDocument;
00066 class QMenu;
00067 class QTextEditPrivate;
00068 class QMimeData;
00069 
00070 class Q_GUI_EXPORT QTextEdit : public QAbstractScrollArea
00071 {
00072     Q_OBJECT
00073     Q_DECLARE_PRIVATE(QTextEdit)
00074     Q_FLAGS(AutoFormatting)
00075     Q_ENUMS(LineWrapMode)
00076     Q_PROPERTY(AutoFormatting autoFormatting READ autoFormatting WRITE setAutoFormatting)
00077     Q_PROPERTY(bool tabChangesFocus READ tabChangesFocus WRITE setTabChangesFocus)
00078     Q_PROPERTY(QString documentTitle READ documentTitle WRITE setDocumentTitle)
00079     Q_PROPERTY(bool undoRedoEnabled READ isUndoRedoEnabled WRITE setUndoRedoEnabled)
00080     Q_PROPERTY(LineWrapMode lineWrapMode READ lineWrapMode WRITE setLineWrapMode)
00081     QDOC_PROPERTY(QTextOption::WrapMode wordWrapMode READ wordWrapMode WRITE setWordWrapMode)
00082     Q_PROPERTY(int lineWrapColumnOrWidth READ lineWrapColumnOrWidth WRITE setLineWrapColumnOrWidth)
00083     Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
00084 #ifndef QT_NO_TEXTHTMLPARSER
00085     Q_PROPERTY(QString html READ toHtml WRITE setHtml NOTIFY textChanged USER true)
00086 #endif
00087     Q_PROPERTY(QString plainText READ toPlainText WRITE setPlainText DESIGNABLE false)
00088     Q_PROPERTY(bool overwriteMode READ overwriteMode WRITE setOverwriteMode)
00089     Q_PROPERTY(int tabStopWidth READ tabStopWidth WRITE setTabStopWidth)
00090     Q_PROPERTY(bool acceptRichText READ acceptRichText WRITE setAcceptRichText)
00091     Q_PROPERTY(int cursorWidth READ cursorWidth WRITE setCursorWidth)
00092     Q_PROPERTY(Qt::TextInteractionFlags textInteractionFlags READ textInteractionFlags WRITE setTextInteractionFlags)
00093 public:
00094     enum LineWrapMode {
00095         NoWrap,
00096         WidgetWidth,
00097         FixedPixelWidth,
00098         FixedColumnWidth
00099     };
00100 
00101     enum AutoFormattingFlag {
00102         AutoNone = 0,
00103         AutoBulletList = 0x00000001,
00104         AutoAll = 0xffffffff
00105     };
00106 
00107     Q_DECLARE_FLAGS(AutoFormatting, AutoFormattingFlag)
00108 
00109 #if defined(QT3_SUPPORT)
00110     enum CursorAction {
00111         MoveBackward,
00112         MoveForward,
00113         MoveWordBackward,
00114         MoveWordForward,
00115         MoveUp,
00116         MoveDown,
00117         MoveLineStart,
00118         MoveLineEnd,
00119         MoveHome,
00120         MoveEnd,
00121         MovePageUp,
00122         MovePageDown
00123 #if !defined(Q_MOC_RUN)
00124         ,
00125         MovePgUp = MovePageUp,
00126         MovePgDown = MovePageDown
00127 #endif
00128     };
00129 #endif
00130 
00131     explicit QTextEdit(QWidget *parent = 0);
00132     explicit QTextEdit(const QString &text, QWidget *parent = 0);
00133     virtual ~QTextEdit();
00134 
00135     void setDocument(QTextDocument *document);
00136     QTextDocument *document() const;
00137 
00138     void setTextCursor(const QTextCursor &cursor);
00139     QTextCursor textCursor() const;
00140 
00141     bool isReadOnly() const;
00142     void setReadOnly(bool ro);
00143 
00144     void setTextInteractionFlags(Qt::TextInteractionFlags flags);
00145     Qt::TextInteractionFlags textInteractionFlags() const;
00146 
00147     qreal fontPointSize() const;
00148     QString fontFamily() const;
00149     int fontWeight() const;
00150     bool fontUnderline() const;
00151     bool fontItalic() const;
00152     QColor textColor() const;
00153     QColor textBackgroundColor() const;
00154     QFont currentFont() const;
00155     Qt::Alignment alignment() const;
00156 
00157     void mergeCurrentCharFormat(const QTextCharFormat &modifier);
00158 
00159     void setCurrentCharFormat(const QTextCharFormat &format);
00160     QTextCharFormat currentCharFormat() const;
00161 
00162     AutoFormatting autoFormatting() const;
00163     void setAutoFormatting(AutoFormatting features);
00164 
00165     bool tabChangesFocus() const;
00166     void setTabChangesFocus(bool b);
00167 
00168     inline void setDocumentTitle(const QString &title)
00169     { document()->setMetaInformation(QTextDocument::DocumentTitle, title); }
00170     inline QString documentTitle() const
00171     { return document()->metaInformation(QTextDocument::DocumentTitle); }
00172 
00173     inline bool isUndoRedoEnabled() const
00174     { return document()->isUndoRedoEnabled(); }
00175     inline void setUndoRedoEnabled(bool enable)
00176     { document()->setUndoRedoEnabled(enable); }
00177 
00178     LineWrapMode lineWrapMode() const;
00179     void setLineWrapMode(LineWrapMode mode);
00180 
00181     int lineWrapColumnOrWidth() const;
00182     void setLineWrapColumnOrWidth(int w);
00183 
00184     QTextOption::WrapMode wordWrapMode() const;
00185     void setWordWrapMode(QTextOption::WrapMode policy);
00186 
00187     bool find(const QString &exp, QTextDocument::FindFlags options = 0);
00188 
00189     inline QString toPlainText() const
00190     { return document()->toPlainText(); }
00191 #ifndef QT_NO_TEXTHTMLPARSER
00192     inline QString toHtml() const
00193     { return document()->toHtml(); }
00194 #endif
00195 
00196     void ensureCursorVisible();
00197 
00198     virtual QVariant loadResource(int type, const QUrl &name);
00199 #ifndef QT_NO_CONTEXTMENU
00200     QMenu *createStandardContextMenu();
00201     QMenu *createStandardContextMenu(const QPoint &position);
00202 #endif
00203 
00204     QTextCursor cursorForPosition(const QPoint &pos) const;
00205     QRect cursorRect(const QTextCursor &cursor) const;
00206     QRect cursorRect() const;
00207 
00208     QString anchorAt(const QPoint& pos) const;
00209 
00210     bool overwriteMode() const;
00211     void setOverwriteMode(bool overwrite);
00212 
00213     int tabStopWidth() const;
00214     void setTabStopWidth(int width);
00215 
00216     int cursorWidth() const;
00217     void setCursorWidth(int width);
00218 
00219     bool acceptRichText() const;
00220     void setAcceptRichText(bool accept);
00221 
00222     struct ExtraSelection
00223     {
00224         QTextCursor cursor;
00225         QTextCharFormat format;
00226     };
00227     void setExtraSelections(const QList<ExtraSelection> &selections);
00228     QList<ExtraSelection> extraSelections() const;
00229 
00230     void moveCursor(QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode = QTextCursor::MoveAnchor);
00231 
00232     bool canPaste() const;
00233 
00234 #ifndef QT_NO_PRINTER
00235     void print(QPrinter *printer) const;
00236 #endif
00237 
00238 public Q_SLOTS:
00239     void setFontPointSize(qreal s);
00240     void setFontFamily(const QString &fontFamily);
00241     void setFontWeight(int w);
00242     void setFontUnderline(bool b);
00243     void setFontItalic(bool b);
00244     void setTextColor(const QColor &c);
00245     void setTextBackgroundColor(const QColor &c);
00246     void setCurrentFont(const QFont &f);
00247     void setAlignment(Qt::Alignment a);
00248 
00249     void setPlainText(const QString &text);
00250 #ifndef QT_NO_TEXTHTMLPARSER
00251     void setHtml(const QString &text);
00252 #endif
00253     void setText(const QString &text);
00254 
00255 #ifndef QT_NO_CLIPBOARD
00256     void cut();
00257     void copy();
00258     void paste();
00259 #endif
00260 
00261     void undo();
00262     void redo();
00263 
00264     void clear();
00265     void selectAll();
00266 
00267     void insertPlainText(const QString &text);
00268 #ifndef QT_NO_TEXTHTMLPARSER
00269     void insertHtml(const QString &text);
00270 #endif // QT_NO_TEXTHTMLPARSER
00271 
00272     void append(const QString &text);
00273 
00274     void scrollToAnchor(const QString &name);
00275 
00276     void zoomIn(int range = 1);
00277     void zoomOut(int range = 1);
00278 
00279 Q_SIGNALS:
00280     void textChanged();
00281     void undoAvailable(bool b);
00282     void redoAvailable(bool b);
00283     void currentCharFormatChanged(const QTextCharFormat &format);
00284     void copyAvailable(bool b);
00285     void selectionChanged();
00286     void cursorPositionChanged();
00287 
00288 protected:
00289     virtual bool event(QEvent *e);
00290     virtual void timerEvent(QTimerEvent *e);
00291     virtual void keyPressEvent(QKeyEvent *e);
00292     virtual void keyReleaseEvent(QKeyEvent *e);
00293     virtual void resizeEvent(QResizeEvent *e);
00294     virtual void paintEvent(QPaintEvent *e);
00295     virtual void mousePressEvent(QMouseEvent *e);
00296     virtual void mouseMoveEvent(QMouseEvent *e);
00297     virtual void mouseReleaseEvent(QMouseEvent *e);
00298     virtual void mouseDoubleClickEvent(QMouseEvent *e);
00299     virtual bool focusNextPrevChild(bool next);
00300 #ifndef QT_NO_CONTEXTMENU
00301     virtual void contextMenuEvent(QContextMenuEvent *e);
00302 #endif
00303 #ifndef QT_NO_DRAGANDDROP
00304     virtual void dragEnterEvent(QDragEnterEvent *e);
00305     virtual void dragLeaveEvent(QDragLeaveEvent *e);
00306     virtual void dragMoveEvent(QDragMoveEvent *e);
00307     virtual void dropEvent(QDropEvent *e);
00308 #endif
00309     virtual void focusInEvent(QFocusEvent *e);
00310     virtual void focusOutEvent(QFocusEvent *e);
00311     virtual void showEvent(QShowEvent *);
00312     virtual void changeEvent(QEvent *e);
00313 #ifndef QT_NO_WHEELEVENT
00314     virtual void wheelEvent(QWheelEvent *e);
00315 #endif
00316 
00317     virtual QMimeData *createMimeDataFromSelection() const;
00318     virtual bool canInsertFromMimeData(const QMimeData *source) const;
00319     virtual void insertFromMimeData(const QMimeData *source);
00320 
00321     virtual void inputMethodEvent(QInputMethodEvent *);
00322     QVariant inputMethodQuery(Qt::InputMethodQuery property) const;
00323 
00324     QTextEdit(QTextEditPrivate &dd, QWidget *parent);
00325 
00326     virtual void scrollContentsBy(int dx, int dy);
00327 
00328 #ifdef QT3_SUPPORT
00329 Q_SIGNALS:
00330     QT_MOC_COMPAT void currentFontChanged(const QFont &f);
00331     QT_MOC_COMPAT void currentColorChanged(const QColor &c);
00332 
00333 public:
00334     QT3_SUPPORT_CONSTRUCTOR QTextEdit(QWidget *parent, const char *name);
00335     inline QT3_SUPPORT bool find(const QString &exp, bool cs, bool wo)
00336     {
00337         QTextDocument::FindFlags flags = 0;
00338         if (cs)
00339             flags |= QTextDocument::FindCaseSensitively;
00340         if (wo)
00341             flags |= QTextDocument::FindWholeWords;
00342         return find(exp, flags);
00343     }
00344 
00345     inline QT3_SUPPORT void sync() {}
00346 
00347     QT3_SUPPORT void moveCursor(CursorAction action, QTextCursor::MoveMode mode = QTextCursor::MoveAnchor);
00348     QT3_SUPPORT void moveCursor(CursorAction action, bool select);
00349 
00350     enum KeyboardAction {
00351         ActionBackspace,
00352         ActionDelete,
00353         ActionReturn,
00354         ActionKill,
00355         ActionWordBackspace,
00356         ActionWordDelete
00357     };
00358 
00359     QT3_SUPPORT void doKeyboardAction(KeyboardAction action);
00360 
00361     QT3_SUPPORT QString text() const;
00362     QT3_SUPPORT void setTextFormat(Qt::TextFormat);
00363     QT3_SUPPORT Qt::TextFormat textFormat() const;
00364 
00365     inline QT3_SUPPORT void setBold(bool b) { setFontWeight(b ? QFont::Bold : QFont::Normal); }
00366     inline QT3_SUPPORT void setUnderline(bool b) { setFontUnderline(b); }
00367     inline QT3_SUPPORT void setItalic(bool i) { setFontItalic(i); }
00368     inline QT3_SUPPORT void setFamily(const QString &family) { setFontFamily(family); }
00369     inline QT3_SUPPORT void setPointSize(int size) { setFontPointSize(size); }
00370 
00371     inline QT3_SUPPORT bool italic() const { return fontItalic(); }
00372     inline QT3_SUPPORT bool bold() const { return fontWeight() >= QFont::Bold; }
00373     inline QT3_SUPPORT bool underline() const { return fontUnderline(); }
00374     inline QT3_SUPPORT QString family() const { return fontFamily(); }
00375     inline QT3_SUPPORT int pointSize() const { return (int)(fontPointSize()+0.5); }
00376 
00377     inline QT3_SUPPORT bool hasSelectedText() const
00378     { return textCursor().hasSelection(); }
00379     inline QT3_SUPPORT QString selectedText() const
00380     { return textCursor().selectedText(); }
00381 
00382     inline QT3_SUPPORT bool isUndoAvailable() const
00383     { return document()->isUndoAvailable(); }
00384     inline QT3_SUPPORT bool isRedoAvailable() const
00385     { return document()->isRedoAvailable(); }
00386 
00387     inline QT3_SUPPORT void insert(const QString &text)
00388     { insertPlainText(text); }
00389 
00390     inline QT3_SUPPORT bool isModified() const
00391     { return document()->isModified(); }
00392 
00393     inline QT3_SUPPORT QColor color() const
00394     { return textColor(); }
00395 
00396 public Q_SLOTS:
00397     inline QT_MOC_COMPAT void setModified(bool m = true)
00398     { document()->setModified(m); }
00399 public:
00400     inline QT3_SUPPORT void undo() const
00401     { document()->undo(); }
00402     inline QT3_SUPPORT void redo() const
00403     { document()->redo(); }
00404 
00405 public Q_SLOTS:
00406     inline QT_MOC_COMPAT void setColor(const QColor &c)
00407     { setTextColor(c); }
00408 
00409 #endif
00410 
00411 private:
00412     Q_DISABLE_COPY(QTextEdit)
00413     Q_PRIVATE_SLOT(d_func(), void _q_repaintContents(const QRectF &r))
00414     Q_PRIVATE_SLOT(d_func(), void _q_currentCharFormatChanged(const QTextCharFormat &))
00415     Q_PRIVATE_SLOT(d_func(), void _q_adjustScrollbars())
00416     Q_PRIVATE_SLOT(d_func(), void _q_ensureVisible(const QRectF &))
00417     friend class QTextEditControl;
00418     friend class QTextDocument;
00419     friend class QTextControl;
00420 };
00421 
00422 Q_DECLARE_OPERATORS_FOR_FLAGS(QTextEdit::AutoFormatting)
00423 
00424 QT_END_NAMESPACE
00425 
00426 QT_END_HEADER
00427 
00428 #endif // QT_NO_TEXTEDIT
00429 
00430 #endif // QTEXTEDIT_H