Qt 4.8
qtextedit.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 
59 
61 
62 QT_MODULE(Gui)
63 
64 class QStyleSheet;
65 class QTextDocument;
66 class QMenu;
67 class QTextEditPrivate;
68 class QMimeData;
69 
71 {
72  Q_OBJECT
74  Q_FLAGS(AutoFormatting)
76  AutoFormatting autoFormatting;
77  bool tabChangesFocus;
78  QString documentTitle;
79  bool undoRedoEnabled;
80  LineWrapMode lineWrapMode;
81  QDOC_PROPERTY(QTextOption::WrapMode wordWrapMode READ wordWrapMode WRITE setWordWrapMode)
82  int lineWrapColumnOrWidth;
83  bool readOnly;
84 #ifndef QT_NO_TEXTHTMLPARSER
86 #endif
92  Qt::TextInteractionFlags textInteractionFlags;
93 public:
94  enum LineWrapMode {
98  FixedColumnWidth
99  };
100 
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 
223  {
226  };
227  void setExtraSelections(const QList<ExtraSelection> &selections);
228  QList<ExtraSelection> extraSelections() const;
229 
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)
340  if (wo)
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:
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 &))
420 };
421 
423 
425 
427 
428 #endif // QT_NO_TEXTEDIT
429 
430 #endif // QTEXTEDIT_H
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
bool acceptRichText
whether the text edit accepts rich text insertions by the user
Definition: qtextedit.h:90
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
#define QT_MOC_COMPAT
Definition: qglobal.h:1128
The QKeyEvent class describes a key event.
Definition: qevent.h:224
int type
Definition: qmetatype.cpp:239
The QTextCharFormat class provides formatting information for characters in a QTextDocument.
Definition: qtextformat.h:372
double qreal
Definition: qglobal.h:1193
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define QDOC_PROPERTY(text)
Definition: qglobal.h:2503
EventRef event
#define QT_MODULE(x)
Definition: qglobal.h:2783
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
#define Q_DECLARE_FLAGS(Flags, Enum)
The Q_DECLARE_FLAGS() macro expands to.
Definition: qglobal.h:2348
void setDocumentTitle(const QString &title)
Definition: qtextedit.h:168
Qt::TextInteractionFlags textInteractionFlags
Specifies how the widget should interact with user input.
Definition: qtextedit.h:92
The QWheelEvent class contains parameters that describe a wheel event.
Definition: qevent.h:139
The QContextMenuEvent class contains parameters that describe a context menu event.
Definition: qevent.h:396
#define Q_GUI_EXPORT
Definition: qglobal.h:1450
QTextCursor cursor
A cursor that contains a selection in a QTextDocument.
Definition: qtextedit.h:224
void setModified(bool m=true)
static qreal position(QGraphicsObject *item, QDeclarativeAnchorLine::AnchorLine anchorLine)
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
WrapMode
This enum describes how text is wrapped in a document.
Definition: qtextoption.h:102
static void clear(QVariant::Private *d)
Definition: qvariant.cpp:197
int select(int, fd_set *, fd_set *, fd_set *, struct timeval *)
bool isModified() const
#define Q_DISABLE_COPY(Class)
Disables the use of copy constructors and assignment operators for the given Class.
Definition: qglobal.h:2523
#define Q_PRIVATE_SLOT(d, signature)
Definition: qobjectdefs.h:73
long ASN1_INTEGER_get ASN1_INTEGER * a
The QAbstractScrollArea widget provides a scrolling area with on-demand scroll bars.
#define Q_SLOTS
Definition: qobjectdefs.h:71
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QDragMoveEvent class provides an event which is sent while a drag and drop action is in progress...
Definition: qevent.h:530
#define Q_SIGNALS
Definition: qobjectdefs.h:72
QString selectedText() const
Returns the current selection&#39;s text (which may be empty).
QString toPlainText() const
Returns the plain text contained in the document.
QString documentTitle() const
Definition: qtextedit.h:170
void setMetaInformation(MetaInformation info, const QString &)
Sets the document&#39;s meta information of the type specified by info to the given string.
static QCursor * moveCursor
Definition: qdnd_x11.cpp:254
#define Q_ENUMS(x)
Definition: qobjectdefs.h:84
QString toPlainText() const
Returns the text of the text edit as plain text.
Definition: qtextedit.h:189
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
MoveMode
If the anchor() is kept where it is and the position() is moved, the text in between will be selected...
Definition: qtextcursor.h:85
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
The Q_DECLARE_OPERATORS_FOR_FLAGS() macro declares global operator|() functions for Flags...
Definition: qglobal.h:2355
AutoFormattingFlag
Definition: qtextedit.h:101
bool hasSelection() const
Returns true if the cursor contains a selection; otherwise returns false.
bool isUndoRedoEnabled() const
Definition: qtextedit.h:173
The QTextCursor class offers an API to access and modify QTextDocuments.
Definition: qtextcursor.h:70
void undo(QTextCursor *cursor)
Undoes the last editing operation on the document if undo is available.
const char * name
The QPrinter class is a paint device that paints on a printer.
Definition: qprinter.h:66
bool isUndoRedoEnabled() const
static QWidget * find(WId)
Returns a pointer to the widget with window identifer/handle id.
Definition: qwidget.cpp:2517
bool overwriteMode
whether text entered by the user will overwrite existing text
Definition: qtextedit.h:88
The QResizeEvent class contains event parameters for resize events.
Definition: qevent.h:349
The QDragLeaveEvent class provides an event that is sent to a widget when a drag and drop action leav...
Definition: qevent.h:577
The QShowEvent class provides an event that is sent when a widget is shown.
Definition: qevent.h:380
The QMimeData class provides a container for data that records information about its MIME type...
Definition: qmimedata.h:57
bool isUndoAvailable() const
Returns true if undo is available; otherwise returns false.
void redo(QTextCursor *cursor)
Redoes the last editing operation on the document if redo is available.
#define Q_OBJECT
Definition: qobjectdefs.h:157
int cursorWidth
This property specifies the width of the cursor in pixels.
Definition: qtextedit.h:91
TextFormat
Definition: qnamespace.h:1310
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:85
InputMethodQuery
Definition: qnamespace.h:1541
The QInputMethodEvent class provides parameters for input method events.
Definition: qevent.h:431
int tabStopWidth
the tab stop width in pixels
Definition: qtextedit.h:89
QString html
This property provides an HTML interface to the text of the text edit.
Definition: qtextedit.h:85
The QDropEvent class provides an event which is sent when a drag and drop action is completed...
Definition: qevent.h:476
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
The QTimerEvent class contains parameters that describe a timer event.
Definition: qcoreevent.h:341
The QFont class specifies a font used for drawing text.
Definition: qfont.h:64
The QDragEnterEvent class provides an event which is sent to a widget when a drag and drop action ent...
Definition: qevent.h:555
QString metaInformation(MetaInformation info) const
Returns meta information about the document of the type specified by info.
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
void setUndoRedoEnabled(bool enable)
const char * property
Definition: qwizard.cpp:138
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
The QTextEdit::ExtraSelection structure provides a way of specifying a character format for a given s...
Definition: qtextedit.h:222
The QTextOption class provides a description of general rich text properties.
Definition: qtextoption.h:59
The QTextDocument class holds formatted text that can be viewed and edited using a QTextEdit...
QString plainText
This property gets and sets the text editor&#39;s contents as plain text.
Definition: qtextedit.h:87
#define Q_FLAGS(x)
Definition: qobjectdefs.h:85
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
QTextCharFormat format
A format that is used to specify a foreground or background brush/color for the selection.
Definition: qtextedit.h:225
void setUndoRedoEnabled(bool enable)
Definition: qtextedit.h:175
#define class
The QPaintEvent class contains event parameters for paint events.
Definition: qevent.h:298
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
#define QT_END_HEADER
Definition: qglobal.h:137
QString toHtml() const
Definition: qtextedit.h:192
static float pointSize(const QFontDef &fd, int dpi)
Definition: qfont_win.cpp:90
QString toHtml(const QByteArray &encoding=QByteArray()) const
Returns a string containing an HTML representation of the document.
The QFocusEvent class contains event parameters for widget focus events.
Definition: qevent.h:275
The QTextEdit class provides a widget that is used to edit and display both plain and rich text...
Definition: qtextedit.h:70
#define text
Definition: qobjectdefs.h:80
bool isRedoAvailable() const
Returns true if redo is available; otherwise returns false.
The QList class is a template class that provides lists.
Definition: qdatastream.h:62