1 | // Copyright (C) 2019 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QTEXTEDIT_H |
5 | #define QTEXTEDIT_H |
6 | |
7 | #include <QtWidgets/qtwidgetsglobal.h> |
8 | #include <QtWidgets/qabstractscrollarea.h> |
9 | #include <QtGui/qtextdocument.h> |
10 | #include <QtGui/qtextoption.h> |
11 | #include <QtGui/qtextcursor.h> |
12 | #include <QtGui/qtextformat.h> |
13 | |
14 | QT_REQUIRE_CONFIG(textedit); |
15 | |
16 | QT_BEGIN_NAMESPACE |
17 | |
18 | class QStyleSheet; |
19 | class QTextDocument; |
20 | class ; |
21 | class QTextEditPrivate; |
22 | class QMimeData; |
23 | class QPagedPaintDevice; |
24 | class QRegularExpression; |
25 | |
26 | class Q_WIDGETS_EXPORT QTextEdit : public QAbstractScrollArea |
27 | { |
28 | Q_OBJECT |
29 | Q_DECLARE_PRIVATE(QTextEdit) |
30 | Q_PROPERTY(AutoFormatting autoFormatting READ autoFormatting WRITE setAutoFormatting) |
31 | Q_PROPERTY(bool tabChangesFocus READ tabChangesFocus WRITE setTabChangesFocus) |
32 | Q_PROPERTY(QString documentTitle READ documentTitle WRITE setDocumentTitle) |
33 | Q_PROPERTY(bool undoRedoEnabled READ isUndoRedoEnabled WRITE setUndoRedoEnabled) |
34 | Q_PROPERTY(LineWrapMode lineWrapMode READ lineWrapMode WRITE setLineWrapMode) |
35 | QDOC_PROPERTY(QTextOption::WrapMode wordWrapMode READ wordWrapMode WRITE setWordWrapMode) |
36 | Q_PROPERTY(int lineWrapColumnOrWidth READ lineWrapColumnOrWidth WRITE setLineWrapColumnOrWidth) |
37 | Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly) |
38 | #if QT_CONFIG(textmarkdownreader) && QT_CONFIG(textmarkdownwriter) |
39 | Q_PROPERTY(QString markdown READ toMarkdown WRITE setMarkdown NOTIFY textChanged) |
40 | #endif |
41 | #ifndef QT_NO_TEXTHTMLPARSER |
42 | Q_PROPERTY(QString html READ toHtml WRITE setHtml NOTIFY textChanged USER true) |
43 | #endif |
44 | Q_PROPERTY(QString plainText READ toPlainText WRITE setPlainText DESIGNABLE false) |
45 | Q_PROPERTY(bool overwriteMode READ overwriteMode WRITE setOverwriteMode) |
46 | Q_PROPERTY(qreal tabStopDistance READ tabStopDistance WRITE setTabStopDistance) |
47 | Q_PROPERTY(bool acceptRichText READ acceptRichText WRITE setAcceptRichText) |
48 | Q_PROPERTY(int cursorWidth READ cursorWidth WRITE setCursorWidth) |
49 | Q_PROPERTY(Qt::TextInteractionFlags textInteractionFlags READ textInteractionFlags |
50 | WRITE setTextInteractionFlags) |
51 | Q_PROPERTY(QTextDocument *document READ document WRITE setDocument DESIGNABLE false) |
52 | Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText) |
53 | public: |
54 | enum LineWrapMode { |
55 | NoWrap, |
56 | WidgetWidth, |
57 | FixedPixelWidth, |
58 | FixedColumnWidth |
59 | }; |
60 | Q_ENUM(LineWrapMode) |
61 | |
62 | enum AutoFormattingFlag { |
63 | AutoNone = 0, |
64 | AutoBulletList = 0x00000001, |
65 | AutoAll = 0xffffffff |
66 | }; |
67 | |
68 | Q_DECLARE_FLAGS(AutoFormatting, AutoFormattingFlag) |
69 | Q_FLAG(AutoFormatting) |
70 | |
71 | explicit QTextEdit(QWidget *parent = nullptr); |
72 | explicit QTextEdit(const QString &text, QWidget *parent = nullptr); |
73 | virtual ~QTextEdit(); |
74 | |
75 | void setDocument(QTextDocument *document); |
76 | QTextDocument *document() const; |
77 | |
78 | void setPlaceholderText(const QString &placeholderText); |
79 | QString placeholderText() const; |
80 | |
81 | void setTextCursor(const QTextCursor &cursor); |
82 | QTextCursor textCursor() const; |
83 | |
84 | bool isReadOnly() const; |
85 | void setReadOnly(bool ro); |
86 | |
87 | void setTextInteractionFlags(Qt::TextInteractionFlags flags); |
88 | Qt::TextInteractionFlags textInteractionFlags() const; |
89 | |
90 | qreal fontPointSize() const; |
91 | QString fontFamily() const; |
92 | int fontWeight() const; |
93 | bool fontUnderline() const; |
94 | bool fontItalic() const; |
95 | QColor textColor() const; |
96 | QColor textBackgroundColor() const; |
97 | QFont currentFont() const; |
98 | Qt::Alignment alignment() const; |
99 | |
100 | void mergeCurrentCharFormat(const QTextCharFormat &modifier); |
101 | |
102 | void setCurrentCharFormat(const QTextCharFormat &format); |
103 | QTextCharFormat currentCharFormat() const; |
104 | |
105 | AutoFormatting autoFormatting() const; |
106 | void setAutoFormatting(AutoFormatting features); |
107 | |
108 | bool tabChangesFocus() const; |
109 | void setTabChangesFocus(bool b); |
110 | |
111 | inline void setDocumentTitle(const QString &title) |
112 | { document()->setMetaInformation(info: QTextDocument::DocumentTitle, title); } |
113 | inline QString documentTitle() const |
114 | { return document()->metaInformation(info: QTextDocument::DocumentTitle); } |
115 | |
116 | inline bool isUndoRedoEnabled() const |
117 | { return document()->isUndoRedoEnabled(); } |
118 | inline void setUndoRedoEnabled(bool enable) |
119 | { document()->setUndoRedoEnabled(enable); } |
120 | |
121 | LineWrapMode lineWrapMode() const; |
122 | void setLineWrapMode(LineWrapMode mode); |
123 | |
124 | int lineWrapColumnOrWidth() const; |
125 | void setLineWrapColumnOrWidth(int w); |
126 | |
127 | QTextOption::WrapMode wordWrapMode() const; |
128 | void setWordWrapMode(QTextOption::WrapMode policy); |
129 | |
130 | bool find(const QString &exp, QTextDocument::FindFlags options = QTextDocument::FindFlags()); |
131 | #if QT_CONFIG(regularexpression) |
132 | bool find(const QRegularExpression &exp, QTextDocument::FindFlags options = QTextDocument::FindFlags()); |
133 | #endif |
134 | |
135 | QString toPlainText() const; |
136 | #ifndef QT_NO_TEXTHTMLPARSER |
137 | QString toHtml() const; |
138 | #endif |
139 | #if QT_CONFIG(textmarkdownwriter) |
140 | QString toMarkdown(QTextDocument::MarkdownFeatures features = QTextDocument::MarkdownDialectGitHub) const; |
141 | #endif |
142 | |
143 | void ensureCursorVisible(); |
144 | |
145 | Q_INVOKABLE virtual QVariant loadResource(int type, const QUrl &name); |
146 | #ifndef QT_NO_CONTEXTMENU |
147 | QMenu *createStandardContextMenu(); |
148 | QMenu *createStandardContextMenu(const QPoint &position); |
149 | #endif |
150 | |
151 | QTextCursor cursorForPosition(const QPoint &pos) const; |
152 | QRect cursorRect(const QTextCursor &cursor) const; |
153 | QRect cursorRect() const; |
154 | |
155 | QString anchorAt(const QPoint& pos) const; |
156 | |
157 | bool overwriteMode() const; |
158 | void setOverwriteMode(bool overwrite); |
159 | |
160 | qreal tabStopDistance() const; |
161 | void setTabStopDistance(qreal distance); |
162 | |
163 | int cursorWidth() const; |
164 | void setCursorWidth(int width); |
165 | |
166 | bool acceptRichText() const; |
167 | void setAcceptRichText(bool accept); |
168 | |
169 | struct |
170 | { |
171 | QTextCursor ; |
172 | QTextCharFormat ; |
173 | }; |
174 | void (const QList<ExtraSelection> &selections); |
175 | QList<ExtraSelection> () const; |
176 | |
177 | void moveCursor(QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode = QTextCursor::MoveAnchor); |
178 | |
179 | bool canPaste() const; |
180 | |
181 | void print(QPagedPaintDevice *printer) const; |
182 | |
183 | QVariant inputMethodQuery(Qt::InputMethodQuery property) const override; |
184 | Q_INVOKABLE QVariant inputMethodQuery(Qt::InputMethodQuery query, QVariant argument) const; |
185 | |
186 | public Q_SLOTS: |
187 | void setFontPointSize(qreal s); |
188 | void setFontFamily(const QString &fontFamily); |
189 | void setFontWeight(int w); |
190 | void setFontUnderline(bool b); |
191 | void setFontItalic(bool b); |
192 | void setTextColor(const QColor &c); |
193 | void setTextBackgroundColor(const QColor &c); |
194 | void setCurrentFont(const QFont &f); |
195 | void setAlignment(Qt::Alignment a); |
196 | |
197 | void setPlainText(const QString &text); |
198 | #ifndef QT_NO_TEXTHTMLPARSER |
199 | void setHtml(const QString &text); |
200 | #endif |
201 | #if QT_CONFIG(textmarkdownreader) |
202 | void setMarkdown(const QString &markdown); |
203 | #endif |
204 | void setText(const QString &text); |
205 | |
206 | #ifndef QT_NO_CLIPBOARD |
207 | void cut(); |
208 | void copy(); |
209 | void paste(); |
210 | #endif |
211 | |
212 | void undo(); |
213 | void redo(); |
214 | |
215 | void clear(); |
216 | void selectAll(); |
217 | |
218 | void insertPlainText(const QString &text); |
219 | #ifndef QT_NO_TEXTHTMLPARSER |
220 | void insertHtml(const QString &text); |
221 | #endif // QT_NO_TEXTHTMLPARSER |
222 | |
223 | void append(const QString &text); |
224 | |
225 | void scrollToAnchor(const QString &name); |
226 | |
227 | void zoomIn(int range = 1); |
228 | void zoomOut(int range = 1); |
229 | |
230 | Q_SIGNALS: |
231 | void textChanged(); |
232 | void undoAvailable(bool b); |
233 | void redoAvailable(bool b); |
234 | void currentCharFormatChanged(const QTextCharFormat &format); |
235 | void copyAvailable(bool b); |
236 | void selectionChanged(); |
237 | void cursorPositionChanged(); |
238 | |
239 | protected: |
240 | virtual bool event(QEvent *e) override; |
241 | virtual void timerEvent(QTimerEvent *e) override; |
242 | virtual void keyPressEvent(QKeyEvent *e) override; |
243 | virtual void keyReleaseEvent(QKeyEvent *e) override; |
244 | virtual void resizeEvent(QResizeEvent *e) override; |
245 | virtual void paintEvent(QPaintEvent *e) override; |
246 | virtual void mousePressEvent(QMouseEvent *e) override; |
247 | virtual void mouseMoveEvent(QMouseEvent *e) override; |
248 | virtual void mouseReleaseEvent(QMouseEvent *e) override; |
249 | virtual void mouseDoubleClickEvent(QMouseEvent *e) override; |
250 | virtual bool focusNextPrevChild(bool next) override; |
251 | #ifndef QT_NO_CONTEXTMENU |
252 | virtual void (QContextMenuEvent *e) override; |
253 | #endif |
254 | #if QT_CONFIG(draganddrop) |
255 | virtual void dragEnterEvent(QDragEnterEvent *e) override; |
256 | virtual void dragLeaveEvent(QDragLeaveEvent *e) override; |
257 | virtual void dragMoveEvent(QDragMoveEvent *e) override; |
258 | virtual void dropEvent(QDropEvent *e) override; |
259 | #endif |
260 | virtual void focusInEvent(QFocusEvent *e) override; |
261 | virtual void focusOutEvent(QFocusEvent *e) override; |
262 | virtual void showEvent(QShowEvent *) override; |
263 | virtual void changeEvent(QEvent *e) override; |
264 | #if QT_CONFIG(wheelevent) |
265 | virtual void wheelEvent(QWheelEvent *e) override; |
266 | #endif |
267 | |
268 | virtual QMimeData *createMimeDataFromSelection() const; |
269 | virtual bool canInsertFromMimeData(const QMimeData *source) const; |
270 | virtual void insertFromMimeData(const QMimeData *source); |
271 | |
272 | virtual void inputMethodEvent(QInputMethodEvent *) override; |
273 | |
274 | QTextEdit(QTextEditPrivate &dd, QWidget *parent); |
275 | |
276 | virtual void scrollContentsBy(int dx, int dy) override; |
277 | virtual void doSetTextCursor(const QTextCursor &cursor); |
278 | |
279 | void zoomInF(float range); |
280 | |
281 | private: |
282 | Q_DISABLE_COPY(QTextEdit) |
283 | Q_PRIVATE_SLOT(d_func(), void _q_repaintContents(const QRectF &r)) |
284 | Q_PRIVATE_SLOT(d_func(), void _q_currentCharFormatChanged(const QTextCharFormat &)) |
285 | Q_PRIVATE_SLOT(d_func(), void _q_adjustScrollbars()) |
286 | Q_PRIVATE_SLOT(d_func(), void _q_ensureVisible(const QRectF &)) |
287 | Q_PRIVATE_SLOT(d_func(), void _q_cursorPositionChanged()) |
288 | #if QT_CONFIG(cursor) |
289 | Q_PRIVATE_SLOT(d_func(), void _q_hoveredBlockWithMarkerChanged(const QTextBlock &)) |
290 | #endif |
291 | friend class QTextEditControl; |
292 | friend class QTextDocument; |
293 | friend class QWidgetTextControl; |
294 | }; |
295 | |
296 | Q_DECLARE_OPERATORS_FOR_FLAGS(QTextEdit::AutoFormatting) |
297 | |
298 | QT_END_NAMESPACE |
299 | |
300 | #endif // QTEXTEDIT_H |
301 | |