Qt 4.8
qplaintextedit.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 QPLAINTEXTEDIT_H
43 #define QPLAINTEXTEDIT_H
44 
45 #include <QtGui/qtextedit.h>
46 
47 #include <QtGui/qabstractscrollarea.h>
48 #include <QtGui/qtextdocument.h>
49 #include <QtGui/qtextoption.h>
50 #include <QtGui/qtextcursor.h>
51 #include <QtGui/qtextformat.h>
52 #include <QtGui/qabstracttextdocumentlayout.h>
53 
54 #ifndef QT_NO_TEXTEDIT
55 
57 
59 
60 QT_MODULE(Gui)
61 
62 class QStyleSheet;
63 class QTextDocument;
64 class QMenu;
66 class QMimeData;
67 
68 
70 {
71  Q_OBJECT
74  bool tabChangesFocus;
75  QString documentTitle;
76  bool undoRedoEnabled;
77  LineWrapMode lineWrapMode;
78  QDOC_PROPERTY(QTextOption::WrapMode wordWrapMode READ wordWrapMode WRITE setWordWrapMode)
79  bool readOnly;
80  QString plainText;
81  bool overwriteMode;
82  int tabStopWidth;
83  int cursorWidth;
84  Qt::TextInteractionFlags textInteractionFlags;
85  int blockCount;
86  int maximumBlockCount;
87  bool backgroundVisible;
88  bool centerOnScroll;
89 public:
90  enum LineWrapMode {
92  WidgetWidth
93  };
94 
95  explicit QPlainTextEdit(QWidget *parent = 0);
96  explicit QPlainTextEdit(const QString &text, QWidget *parent = 0);
97  virtual ~QPlainTextEdit();
98 
99  void setDocument(QTextDocument *document);
100  QTextDocument *document() const;
101 
102  void setTextCursor(const QTextCursor &cursor);
103  QTextCursor textCursor() const;
104 
105  bool isReadOnly() const;
106  void setReadOnly(bool ro);
107 
108  void setTextInteractionFlags(Qt::TextInteractionFlags flags);
109  Qt::TextInteractionFlags textInteractionFlags() const;
110 
111  void mergeCurrentCharFormat(const QTextCharFormat &modifier);
112  void setCurrentCharFormat(const QTextCharFormat &format);
113  QTextCharFormat currentCharFormat() const;
114 
115  bool tabChangesFocus() const;
116  void setTabChangesFocus(bool b);
117 
118  inline void setDocumentTitle(const QString &title)
119  { document()->setMetaInformation(QTextDocument::DocumentTitle, title); }
120  inline QString documentTitle() const
121  { return document()->metaInformation(QTextDocument::DocumentTitle); }
122 
123  inline bool isUndoRedoEnabled() const
124  { return document()->isUndoRedoEnabled(); }
125  inline void setUndoRedoEnabled(bool enable)
126  { document()->setUndoRedoEnabled(enable); }
127 
128  inline void setMaximumBlockCount(int maximum)
129  { document()->setMaximumBlockCount(maximum); }
130  inline int maximumBlockCount() const
131  { return document()->maximumBlockCount(); }
132 
133 
134  LineWrapMode lineWrapMode() const;
135  void setLineWrapMode(LineWrapMode mode);
136 
137  QTextOption::WrapMode wordWrapMode() const;
138  void setWordWrapMode(QTextOption::WrapMode policy);
139 
140  void setBackgroundVisible(bool visible);
141  bool backgroundVisible() const;
142 
143  void setCenterOnScroll(bool enabled);
144  bool centerOnScroll() const;
145 
146  bool find(const QString &exp, QTextDocument::FindFlags options = 0);
147 
148  inline QString toPlainText() const
149  { return document()->toPlainText(); }
150 
151  void ensureCursorVisible();
152 
153  virtual QVariant loadResource(int type, const QUrl &name);
154 #ifndef QT_NO_CONTEXTMENU
155  QMenu *createStandardContextMenu();
156 #endif
157 
158  QTextCursor cursorForPosition(const QPoint &pos) const;
159  QRect cursorRect(const QTextCursor &cursor) const;
160  QRect cursorRect() const;
161 
162  QString anchorAt(const QPoint &pos) const;
163 
164  bool overwriteMode() const;
165  void setOverwriteMode(bool overwrite);
166 
167  int tabStopWidth() const;
168  void setTabStopWidth(int width);
169 
170  int cursorWidth() const;
171  void setCursorWidth(int width);
172 
173  void setExtraSelections(const QList<QTextEdit::ExtraSelection> &selections);
174  QList<QTextEdit::ExtraSelection> extraSelections() const;
175 
177 
178  bool canPaste() const;
179 
180 #ifndef QT_NO_PRINTER
181  void print(QPrinter *printer) const;
182 #endif
183 
184  int blockCount() const;
185 
186 public Q_SLOTS:
187 
188  void setPlainText(const QString &text);
189 
190 #ifndef QT_NO_CLIPBOARD
191  void cut();
192  void copy();
193  void paste();
194 #endif
195 
196  void undo();
197  void redo();
198 
199  void clear();
200  void selectAll();
201 
202  void insertPlainText(const QString &text);
203 
204  void appendPlainText(const QString &text);
205  void appendHtml(const QString &html);
206 
207  void centerCursor();
208 
209 Q_SIGNALS:
210  void textChanged();
211  void undoAvailable(bool b);
212  void redoAvailable(bool b);
213  void copyAvailable(bool b);
214  void selectionChanged();
215  void cursorPositionChanged();
216 
217  void updateRequest(const QRect &rect, int dy);
218  void blockCountChanged(int newBlockCount);
219  void modificationChanged(bool);
220 
221 protected:
222  virtual bool event(QEvent *e);
223  virtual void timerEvent(QTimerEvent *e);
224  virtual void keyPressEvent(QKeyEvent *e);
225  virtual void keyReleaseEvent(QKeyEvent *e);
226  virtual void resizeEvent(QResizeEvent *e);
227  virtual void paintEvent(QPaintEvent *e);
228  virtual void mousePressEvent(QMouseEvent *e);
229  virtual void mouseMoveEvent(QMouseEvent *e);
230  virtual void mouseReleaseEvent(QMouseEvent *e);
231  virtual void mouseDoubleClickEvent(QMouseEvent *e);
232  virtual bool focusNextPrevChild(bool next);
233 #ifndef QT_NO_CONTEXTMENU
234  virtual void contextMenuEvent(QContextMenuEvent *e);
235 #endif
236 #ifndef QT_NO_DRAGANDDROP
237  virtual void dragEnterEvent(QDragEnterEvent *e);
238  virtual void dragLeaveEvent(QDragLeaveEvent *e);
239  virtual void dragMoveEvent(QDragMoveEvent *e);
240  virtual void dropEvent(QDropEvent *e);
241 #endif
242  virtual void focusInEvent(QFocusEvent *e);
243  virtual void focusOutEvent(QFocusEvent *e);
244  virtual void showEvent(QShowEvent *);
245  virtual void changeEvent(QEvent *e);
246 #ifndef QT_NO_WHEELEVENT
247  virtual void wheelEvent(QWheelEvent *e);
248 #endif
249 
250  virtual QMimeData *createMimeDataFromSelection() const;
251  virtual bool canInsertFromMimeData(const QMimeData *source) const;
252  virtual void insertFromMimeData(const QMimeData *source);
253 
254  virtual void inputMethodEvent(QInputMethodEvent *);
256 
258 
259  virtual void scrollContentsBy(int dx, int dy);
260 
261  QTextBlock firstVisibleBlock() const;
262  QPointF contentOffset() const;
263  QRectF blockBoundingRect(const QTextBlock &block) const;
264  QRectF blockBoundingGeometry(const QTextBlock &block) const;
265  QAbstractTextDocumentLayout::PaintContext getPaintContext() const;
266 
267 
268 private:
270  Q_PRIVATE_SLOT(d_func(), void _q_repaintContents(const QRectF &r))
271  Q_PRIVATE_SLOT(d_func(), void _q_adjustScrollbars())
272  Q_PRIVATE_SLOT(d_func(), void _q_verticalScrollbarActionTriggered(int))
273  Q_PRIVATE_SLOT(d_func(), void _q_cursorPositionChanged())
274 
276 };
277 
278 
281 {
282  Q_OBJECT
284  int cursorWidth;
285 
286 public:
289 
290  void draw(QPainter *, const PaintContext &);
291  int hitTest(const QPointF &, Qt::HitTestAccuracy ) const;
292 
293  int pageCount() const;
294  QSizeF documentSize() const;
295 
296  QRectF frameBoundingRect(QTextFrame *) const;
297  QRectF blockBoundingRect(const QTextBlock &block) const;
298 
299  void ensureBlockLayout(const QTextBlock &block) const;
300 
301  void setCursorWidth(int width);
302  int cursorWidth() const;
303 
304  void requestUpdate();
305 
306 protected:
307  void documentChanged(int from, int /*charsRemoved*/, int charsAdded);
308 
309 
310 private:
311  void setTextWidth(qreal newWidth);
312  qreal textWidth() const;
313  void layoutBlock(const QTextBlock &block);
314  qreal blockWidth(const QTextBlock &block);
315 
317 
320 };
321 
323 
325 
326 
327 #endif // QT_NO_TEXTEDIT
328 
329 #endif // QPLAINTEXTEDIT_H
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
bool isUndoRedoEnabled() const
The QKeyEvent class describes a key event.
Definition: qevent.h:224
QString documentTitle() const
virtual void showEvent(QShowEvent *)
This event handler can be reimplemented in a subclass to receive widget show events which are passed ...
Definition: qwidget.cpp:9842
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
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define QDOC_PROPERTY(text)
Definition: qglobal.h:2503
virtual void inputMethodEvent(QInputMethodEvent *)
This event handler, for event event, can be reimplemented in a subclass to receive Input Method compo...
Definition: qwidget.cpp:9668
#define QT_MODULE(x)
Definition: qglobal.h:2783
The QPlainTextDocumentLayout class implements a plain text layout for QTextDocument.
void dragMoveEvent(QDragMoveEvent *)
This event handler can be reimplemented in a subclass to receive drag move events (passed in event)...
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
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
The QTextFrame class represents a frame in a QTextDocument.
Definition: qtextobject.h:122
void mouseReleaseEvent(QMouseEvent *)
This event handler can be reimplemented in a subclass to receive mouse release events for the viewpor...
#define Q_GUI_EXPORT
Definition: qglobal.h:1450
HitTestAccuracy
Definition: qtextdocument.h:78
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
virtual void timerEvent(QTimerEvent *)
This event handler can be reimplemented in a subclass to receive timer events for the object...
Definition: qobject.cpp:1294
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
void dragEnterEvent(QDragEnterEvent *)
This event handler can be reimplemented in a subclass to receive drag enter events (passed in event)...
WrapMode
This enum describes how text is wrapped in a document.
Definition: qtextoption.h:102
int maximumBlockCount() const
static void clear(QVariant::Private *d)
Definition: qvariant.cpp:197
#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
virtual void keyReleaseEvent(QKeyEvent *)
This event handler, for event event, can be reimplemented in a subclass to receive key release events...
Definition: qwidget.cpp:9407
static QSize documentSize(QTextControl *control)
Definition: qtextedit.cpp:238
The QAbstractScrollArea widget provides a scrolling area with on-demand scroll bars.
#define Q_SLOTS
Definition: qobjectdefs.h:71
virtual QVariant inputMethodQuery(Qt::InputMethodQuery) const
This method is only relevant for input widgets.
Definition: qwidget.cpp:9683
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
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
virtual void focusOutEvent(QFocusEvent *)
This event handler can be reimplemented in a subclass to receive keyboard focus events (focus lost) f...
Definition: qwidget.cpp:9457
void setMaximumBlockCount(int maximum)
static QCursor * moveCursor
Definition: qdnd_x11.cpp:254
#define Q_ENUMS(x)
Definition: qobjectdefs.h:84
void wheelEvent(QWheelEvent *)
This event handler can be reimplemented in a subclass to receive wheel events for the viewport() widg...
void resizeEvent(QResizeEvent *)
This event handler can be reimplemented in a subclass to receive resize events (passed in event)...
#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
void changeEvent(QEvent *)
This event handler can be reimplemented to handle state changes.
Definition: qframe.cpp:574
void contextMenuEvent(QContextMenuEvent *)
This event handler can be reimplemented in a subclass to receive context menu events for the viewport...
void paintEvent(QPaintEvent *)
This event handler can be reimplemented in a subclass to receive paint events (passed in event)...
The QTextCursor class offers an API to access and modify QTextDocuments.
Definition: qtextcursor.h:70
void setUndoRedoEnabled(bool enable)
const char * name
The QPrinter class is a paint device that paints on a printer.
Definition: qprinter.h:66
void mousePressEvent(QMouseEvent *)
This event handler can be reimplemented in a subclass to receive mouse press events for the viewport(...
static QWidget * find(WId)
Returns a pointer to the widget with window identifer/handle id.
Definition: qwidget.cpp:2517
The QResizeEvent class contains event parameters for resize events.
Definition: qevent.h:349
The QTextBlock class provides a container for text fragments in a QTextDocument.
Definition: qtextobject.h:199
The QPlainTextEdit class provides a widget that is used to edit and display plain text...
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
#define Q_OBJECT
Definition: qobjectdefs.h:157
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
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
QString toPlainText() const
Returns the text of the text edit as plain text.
The QDragEnterEvent class provides an event which is sent to a widget when a drag and drop action ent...
Definition: qevent.h:555
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
virtual bool focusNextPrevChild(bool next)
Finds a new widget to give the keyboard focus to, as appropriate for Tab and Shift+Tab, and returns true if it can find a new widget, or false if it can&#39;t.
Definition: qwidget.cpp:6836
The QAbstractTextDocumentLayout::PaintContext class is a convenience class defining the parameters us...
void dropEvent(QDropEvent *)
This event handler can be reimplemented in a subclass to receive drop events (passed in event)...
static const QMetaObjectPrivate * priv(const uint *data)
const char * property
Definition: qwizard.cpp:138
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
Definition: qnamespace.h:54
void dragLeaveEvent(QDragLeaveEvent *)
This event handler can be reimplemented in a subclass to receive drag leave events (passed in event)...
The QTextOption class provides a description of general rich text properties.
Definition: qtextoption.h:59
void setDocumentTitle(const QString &title)
virtual void scrollContentsBy(int dx, int dy)
This virtual handler is called when the scroll bars are moved by dx, dy, and consequently the viewpor...
The QTextDocument class holds formatted text that can be viewed and edited using a QTextEdit...
bool event(QEvent *)
Reimplemented Function
void keyPressEvent(QKeyEvent *)
This function is called with key event e when key presses occur.
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
#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
#define private
Definition: qregion_qws.cpp:43
virtual void focusInEvent(QFocusEvent *)
This event handler can be reimplemented in a subclass to receive keyboard focus events (focus receive...
Definition: qwidget.cpp:9431
void mouseDoubleClickEvent(QMouseEvent *)
This event handler can be reimplemented in a subclass to receive mouse double click events for the vi...
#define enabled
The QFocusEvent class contains event parameters for widget focus events.
Definition: qevent.h:275
#define text
Definition: qobjectdefs.h:80
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
void mouseMoveEvent(QMouseEvent *)
This event handler can be reimplemented in a subclass to receive mouse move events for the viewport()...
The QAbstractTextDocumentLayout class is an abstract base class used to implement custom layouts for ...