src/gui/widgets/qtextedit.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef QTEXTEDIT_H
       
    43 #define QTEXTEDIT_H
       
    44 
       
    45 #include <QtGui/qabstractscrollarea.h>
       
    46 #include <QtGui/qtextdocument.h>
       
    47 #include <QtGui/qtextoption.h>
       
    48 #include <QtGui/qtextcursor.h>
       
    49 #include <QtGui/qtextformat.h>
       
    50 
       
    51 #ifndef QT_NO_TEXTEDIT
       
    52 
       
    53 #ifdef QT3_SUPPORT
       
    54 #include <QtGui/qtextobject.h>
       
    55 #include <QtGui/qtextlayout.h>
       
    56 #endif
       
    57 
       
    58 QT_BEGIN_HEADER
       
    59 
       
    60 QT_BEGIN_NAMESPACE
       
    61 
       
    62 QT_MODULE(Gui)
       
    63 
       
    64 class QStyleSheet;
       
    65 class QTextDocument;
       
    66 class QMenu;
       
    67 class QTextEditPrivate;
       
    68 class QMimeData;
       
    69 
       
    70 class Q_GUI_EXPORT QTextEdit : public QAbstractScrollArea
       
    71 {
       
    72     Q_OBJECT
       
    73     Q_DECLARE_PRIVATE(QTextEdit)
       
    74     Q_FLAGS(AutoFormatting)
       
    75     Q_ENUMS(LineWrapMode)
       
    76     Q_PROPERTY(AutoFormatting autoFormatting READ autoFormatting WRITE setAutoFormatting)
       
    77     Q_PROPERTY(bool tabChangesFocus READ tabChangesFocus WRITE setTabChangesFocus)
       
    78     Q_PROPERTY(QString documentTitle READ documentTitle WRITE setDocumentTitle)
       
    79     Q_PROPERTY(bool undoRedoEnabled READ isUndoRedoEnabled WRITE setUndoRedoEnabled)
       
    80     Q_PROPERTY(LineWrapMode lineWrapMode READ lineWrapMode WRITE setLineWrapMode)
       
    81     QDOC_PROPERTY(QTextOption::WrapMode wordWrapMode READ wordWrapMode WRITE setWordWrapMode)
       
    82     Q_PROPERTY(int lineWrapColumnOrWidth READ lineWrapColumnOrWidth WRITE setLineWrapColumnOrWidth)
       
    83     Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
       
    84 #ifndef QT_NO_TEXTHTMLPARSER
       
    85     Q_PROPERTY(QString html READ toHtml WRITE setHtml NOTIFY textChanged USER true)
       
    86 #endif
       
    87     Q_PROPERTY(QString plainText READ toPlainText WRITE setPlainText DESIGNABLE false)
       
    88     Q_PROPERTY(bool overwriteMode READ overwriteMode WRITE setOverwriteMode)
       
    89     Q_PROPERTY(int tabStopWidth READ tabStopWidth WRITE setTabStopWidth)
       
    90     Q_PROPERTY(bool acceptRichText READ acceptRichText WRITE setAcceptRichText)
       
    91     Q_PROPERTY(int cursorWidth READ cursorWidth WRITE setCursorWidth)
       
    92     Q_PROPERTY(Qt::TextInteractionFlags textInteractionFlags READ textInteractionFlags WRITE setTextInteractionFlags)
       
    93 public:
       
    94     enum LineWrapMode {
       
    95         NoWrap,
       
    96         WidgetWidth,
       
    97         FixedPixelWidth,
       
    98         FixedColumnWidth
       
    99     };
       
   100 
       
   101     enum AutoFormattingFlag {
       
   102         AutoNone = 0,
       
   103         AutoBulletList = 0x00000001,
       
   104         AutoAll = 0xffffffff
       
   105     };
       
   106 
       
   107     Q_DECLARE_FLAGS(AutoFormatting, AutoFormattingFlag)
       
   108 
       
   109 #if defined(QT3_SUPPORT)
       
   110     enum CursorAction {
       
   111         MoveBackward,
       
   112         MoveForward,
       
   113         MoveWordBackward,
       
   114         MoveWordForward,
       
   115         MoveUp,
       
   116         MoveDown,
       
   117         MoveLineStart,
       
   118         MoveLineEnd,
       
   119         MoveHome,
       
   120         MoveEnd,
       
   121         MovePageUp,
       
   122         MovePageDown
       
   123 #if !defined(Q_MOC_RUN)
       
   124         ,
       
   125         MovePgUp = MovePageUp,
       
   126         MovePgDown = MovePageDown
       
   127 #endif
       
   128     };
       
   129 #endif
       
   130 
       
   131     explicit QTextEdit(QWidget *parent = 0);
       
   132     explicit QTextEdit(const QString &text, QWidget *parent = 0);
       
   133     virtual ~QTextEdit();
       
   134 
       
   135     void setDocument(QTextDocument *document);
       
   136     QTextDocument *document() const;
       
   137 
       
   138     void setTextCursor(const QTextCursor &cursor);
       
   139     QTextCursor textCursor() const;
       
   140 
       
   141     bool isReadOnly() const;
       
   142     void setReadOnly(bool ro);
       
   143 
       
   144     void setTextInteractionFlags(Qt::TextInteractionFlags flags);
       
   145     Qt::TextInteractionFlags textInteractionFlags() const;
       
   146 
       
   147     qreal fontPointSize() const;
       
   148     QString fontFamily() const;
       
   149     int fontWeight() const;
       
   150     bool fontUnderline() const;
       
   151     bool fontItalic() const;
       
   152     QColor textColor() const;
       
   153     QColor textBackgroundColor() const;
       
   154     QFont currentFont() const;
       
   155     Qt::Alignment alignment() const;
       
   156 
       
   157     void mergeCurrentCharFormat(const QTextCharFormat &modifier);
       
   158 
       
   159     void setCurrentCharFormat(const QTextCharFormat &format);
       
   160     QTextCharFormat currentCharFormat() const;
       
   161 
       
   162     AutoFormatting autoFormatting() const;
       
   163     void setAutoFormatting(AutoFormatting features);
       
   164 
       
   165     bool tabChangesFocus() const;
       
   166     void setTabChangesFocus(bool b);
       
   167 
       
   168     inline void setDocumentTitle(const QString &title)
       
   169     { document()->setMetaInformation(QTextDocument::DocumentTitle, title); }
       
   170     inline QString documentTitle() const
       
   171     { return document()->metaInformation(QTextDocument::DocumentTitle); }
       
   172 
       
   173     inline bool isUndoRedoEnabled() const
       
   174     { return document()->isUndoRedoEnabled(); }
       
   175     inline void setUndoRedoEnabled(bool enable)
       
   176     { document()->setUndoRedoEnabled(enable); }
       
   177 
       
   178     LineWrapMode lineWrapMode() const;
       
   179     void setLineWrapMode(LineWrapMode mode);
       
   180 
       
   181     int lineWrapColumnOrWidth() const;
       
   182     void setLineWrapColumnOrWidth(int w);
       
   183 
       
   184     QTextOption::WrapMode wordWrapMode() const;
       
   185     void setWordWrapMode(QTextOption::WrapMode policy);
       
   186 
       
   187     bool find(const QString &exp, QTextDocument::FindFlags options = 0);
       
   188 
       
   189     inline QString toPlainText() const
       
   190     { return document()->toPlainText(); }
       
   191 #ifndef QT_NO_TEXTHTMLPARSER
       
   192     inline QString toHtml() const
       
   193     { return document()->toHtml(); }
       
   194 #endif
       
   195 
       
   196     void ensureCursorVisible();
       
   197 
       
   198     virtual QVariant loadResource(int type, const QUrl &name);
       
   199 #ifndef QT_NO_CONTEXTMENU
       
   200     QMenu *createStandardContextMenu();
       
   201     QMenu *createStandardContextMenu(const QPoint &position);
       
   202 #endif
       
   203 
       
   204     QTextCursor cursorForPosition(const QPoint &pos) const;
       
   205     QRect cursorRect(const QTextCursor &cursor) const;
       
   206     QRect cursorRect() const;
       
   207 
       
   208     QString anchorAt(const QPoint& pos) const;
       
   209 
       
   210     bool overwriteMode() const;
       
   211     void setOverwriteMode(bool overwrite);
       
   212 
       
   213     int tabStopWidth() const;
       
   214     void setTabStopWidth(int width);
       
   215 
       
   216     int cursorWidth() const;
       
   217     void setCursorWidth(int width);
       
   218 
       
   219     bool acceptRichText() const;
       
   220     void setAcceptRichText(bool accept);
       
   221 
       
   222     struct ExtraSelection
       
   223     {
       
   224         QTextCursor cursor;
       
   225         QTextCharFormat format;
       
   226     };
       
   227     void setExtraSelections(const QList<ExtraSelection> &selections);
       
   228     QList<ExtraSelection> extraSelections() const;
       
   229 
       
   230     void moveCursor(QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode = QTextCursor::MoveAnchor);
       
   231 
       
   232     bool canPaste() const;
       
   233 
       
   234 #ifndef QT_NO_PRINTER
       
   235     void print(QPrinter *printer) const;
       
   236 #endif
       
   237 
       
   238 public Q_SLOTS:
       
   239     void setFontPointSize(qreal s);
       
   240     void setFontFamily(const QString &fontFamily);
       
   241     void setFontWeight(int w);
       
   242     void setFontUnderline(bool b);
       
   243     void setFontItalic(bool b);
       
   244     void setTextColor(const QColor &c);
       
   245     void setTextBackgroundColor(const QColor &c);
       
   246     void setCurrentFont(const QFont &f);
       
   247     void setAlignment(Qt::Alignment a);
       
   248 
       
   249     void setPlainText(const QString &text);
       
   250 #ifndef QT_NO_TEXTHTMLPARSER
       
   251     void setHtml(const QString &text);
       
   252 #endif
       
   253     void setText(const QString &text);
       
   254 
       
   255 #ifndef QT_NO_CLIPBOARD
       
   256     void cut();
       
   257     void copy();
       
   258     void paste();
       
   259 #endif
       
   260 
       
   261     void undo();
       
   262     void redo();
       
   263 
       
   264     void clear();
       
   265     void selectAll();
       
   266 
       
   267     void insertPlainText(const QString &text);
       
   268 #ifndef QT_NO_TEXTHTMLPARSER
       
   269     void insertHtml(const QString &text);
       
   270 #endif // QT_NO_TEXTHTMLPARSER
       
   271 
       
   272     void append(const QString &text);
       
   273 
       
   274     void scrollToAnchor(const QString &name);
       
   275 
       
   276     void zoomIn(int range = 1);
       
   277     void zoomOut(int range = 1);
       
   278 
       
   279 Q_SIGNALS:
       
   280     void textChanged();
       
   281     void undoAvailable(bool b);
       
   282     void redoAvailable(bool b);
       
   283     void currentCharFormatChanged(const QTextCharFormat &format);
       
   284     void copyAvailable(bool b);
       
   285     void selectionChanged();
       
   286     void cursorPositionChanged();
       
   287 
       
   288 protected:
       
   289     virtual bool event(QEvent *e);
       
   290     virtual void timerEvent(QTimerEvent *e);
       
   291     virtual void keyPressEvent(QKeyEvent *e);
       
   292     virtual void keyReleaseEvent(QKeyEvent *e);
       
   293     virtual void resizeEvent(QResizeEvent *e);
       
   294     virtual void paintEvent(QPaintEvent *e);
       
   295     virtual void mousePressEvent(QMouseEvent *e);
       
   296     virtual void mouseMoveEvent(QMouseEvent *e);
       
   297     virtual void mouseReleaseEvent(QMouseEvent *e);
       
   298     virtual void mouseDoubleClickEvent(QMouseEvent *e);
       
   299     virtual bool focusNextPrevChild(bool next);
       
   300 #ifndef QT_NO_CONTEXTMENU
       
   301     virtual void contextMenuEvent(QContextMenuEvent *e);
       
   302 #endif
       
   303 #ifndef QT_NO_DRAGANDDROP
       
   304     virtual void dragEnterEvent(QDragEnterEvent *e);
       
   305     virtual void dragLeaveEvent(QDragLeaveEvent *e);
       
   306     virtual void dragMoveEvent(QDragMoveEvent *e);
       
   307     virtual void dropEvent(QDropEvent *e);
       
   308 #endif
       
   309     virtual void focusInEvent(QFocusEvent *e);
       
   310     virtual void focusOutEvent(QFocusEvent *e);
       
   311     virtual void showEvent(QShowEvent *);
       
   312     virtual void changeEvent(QEvent *e);
       
   313 #ifndef QT_NO_WHEELEVENT
       
   314     virtual void wheelEvent(QWheelEvent *e);
       
   315 #endif
       
   316 
       
   317     virtual QMimeData *createMimeDataFromSelection() const;
       
   318     virtual bool canInsertFromMimeData(const QMimeData *source) const;
       
   319     virtual void insertFromMimeData(const QMimeData *source);
       
   320 
       
   321     virtual void inputMethodEvent(QInputMethodEvent *);
       
   322     QVariant inputMethodQuery(Qt::InputMethodQuery property) const;
       
   323 
       
   324     QTextEdit(QTextEditPrivate &dd, QWidget *parent);
       
   325 
       
   326     virtual void scrollContentsBy(int dx, int dy);
       
   327 
       
   328 #ifdef QT3_SUPPORT
       
   329 Q_SIGNALS:
       
   330     QT_MOC_COMPAT void currentFontChanged(const QFont &f);
       
   331     QT_MOC_COMPAT void currentColorChanged(const QColor &c);
       
   332 
       
   333 public:
       
   334     QT3_SUPPORT_CONSTRUCTOR QTextEdit(QWidget *parent, const char *name);
       
   335     inline QT3_SUPPORT bool find(const QString &exp, bool cs, bool wo)
       
   336     {
       
   337         QTextDocument::FindFlags flags = 0;
       
   338         if (cs)
       
   339             flags |= QTextDocument::FindCaseSensitively;
       
   340         if (wo)
       
   341             flags |= QTextDocument::FindWholeWords;
       
   342         return find(exp, flags);
       
   343     }
       
   344 
       
   345     inline QT3_SUPPORT void sync() {}
       
   346 
       
   347     QT3_SUPPORT void moveCursor(CursorAction action, QTextCursor::MoveMode mode = QTextCursor::MoveAnchor);
       
   348     QT3_SUPPORT void moveCursor(CursorAction action, bool select);
       
   349 
       
   350     enum KeyboardAction {
       
   351         ActionBackspace,
       
   352         ActionDelete,
       
   353         ActionReturn,
       
   354         ActionKill,
       
   355         ActionWordBackspace,
       
   356         ActionWordDelete
       
   357     };
       
   358 
       
   359     QT3_SUPPORT void doKeyboardAction(KeyboardAction action);
       
   360 
       
   361     QT3_SUPPORT QString text() const;
       
   362     QT3_SUPPORT void setTextFormat(Qt::TextFormat);
       
   363     QT3_SUPPORT Qt::TextFormat textFormat() const;
       
   364 
       
   365     inline QT3_SUPPORT void setBold(bool b) { setFontWeight(b ? QFont::Bold : QFont::Normal); }
       
   366     inline QT3_SUPPORT void setUnderline(bool b) { setFontUnderline(b); }
       
   367     inline QT3_SUPPORT void setItalic(bool i) { setFontItalic(i); }
       
   368     inline QT3_SUPPORT void setFamily(const QString &family) { setFontFamily(family); }
       
   369     inline QT3_SUPPORT void setPointSize(int size) { setFontPointSize(size); }
       
   370 
       
   371     inline QT3_SUPPORT bool italic() const { return fontItalic(); }
       
   372     inline QT3_SUPPORT bool bold() const { return fontWeight() >= QFont::Bold; }
       
   373     inline QT3_SUPPORT bool underline() const { return fontUnderline(); }
       
   374     inline QT3_SUPPORT QString family() const { return fontFamily(); }
       
   375     inline QT3_SUPPORT int pointSize() const { return (int)(fontPointSize()+0.5); }
       
   376 
       
   377     inline QT3_SUPPORT bool hasSelectedText() const
       
   378     { return textCursor().hasSelection(); }
       
   379     inline QT3_SUPPORT QString selectedText() const
       
   380     { return textCursor().selectedText(); }
       
   381 
       
   382     inline QT3_SUPPORT bool isUndoAvailable() const
       
   383     { return document()->isUndoAvailable(); }
       
   384     inline QT3_SUPPORT bool isRedoAvailable() const
       
   385     { return document()->isRedoAvailable(); }
       
   386 
       
   387     inline QT3_SUPPORT void insert(const QString &text)
       
   388     { insertPlainText(text); }
       
   389 
       
   390     inline QT3_SUPPORT bool isModified() const
       
   391     { return document()->isModified(); }
       
   392 
       
   393     inline QT3_SUPPORT QColor color() const
       
   394     { return textColor(); }
       
   395 
       
   396 public Q_SLOTS:
       
   397     inline QT_MOC_COMPAT void setModified(bool m = true)
       
   398     { document()->setModified(m); }
       
   399 public:
       
   400     inline QT3_SUPPORT void undo() const
       
   401     { document()->undo(); }
       
   402     inline QT3_SUPPORT void redo() const
       
   403     { document()->redo(); }
       
   404 
       
   405 public Q_SLOTS:
       
   406     inline QT_MOC_COMPAT void setColor(const QColor &c)
       
   407     { setTextColor(c); }
       
   408 
       
   409 #endif
       
   410 
       
   411 private:
       
   412     Q_DISABLE_COPY(QTextEdit)
       
   413     Q_PRIVATE_SLOT(d_func(), void _q_repaintContents(const QRectF &r))
       
   414     Q_PRIVATE_SLOT(d_func(), void _q_currentCharFormatChanged(const QTextCharFormat &))
       
   415     Q_PRIVATE_SLOT(d_func(), void _q_adjustScrollbars())
       
   416     Q_PRIVATE_SLOT(d_func(), void _q_ensureVisible(const QRectF &))
       
   417     friend class QTextEditControl;
       
   418     friend class QTextDocument;
       
   419     friend class QTextControl;
       
   420 };
       
   421 
       
   422 Q_DECLARE_OPERATORS_FOR_FLAGS(QTextEdit::AutoFormatting)
       
   423 
       
   424 QT_END_NAMESPACE
       
   425 
       
   426 QT_END_HEADER
       
   427 
       
   428 #endif // QT_NO_TEXTEDIT
       
   429 
       
   430 #endif // QTEXTEDIT_H