1/****************************************************************************
2**
3** Copyright (C) 2019 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtWidgets 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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QWIDGETTEXTCONTROL_P_H
41#define QWIDGETTEXTCONTROL_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QtWidgets/private/qtwidgetsglobal_p.h>
55#include <QtGui/qtextdocument.h>
56#include <QtGui/qtextoption.h>
57#include <QtGui/qtextcursor.h>
58#include <QtGui/qtextformat.h>
59#if QT_CONFIG(textedit)
60#include <QtWidgets/qtextedit.h>
61#endif
62#if QT_CONFIG(menu)
63#include <QtWidgets/qmenu.h>
64#endif
65#include <QtCore/qrect.h>
66#include <QtGui/qabstracttextdocumentlayout.h>
67#include <QtGui/qtextdocumentfragment.h>
68#include <QtGui/qclipboard.h>
69#include <QtCore/qmimedata.h>
70#include <QtGui/private/qinputcontrol_p.h>
71
72QT_REQUIRE_CONFIG(widgettextcontrol);
73
74QT_BEGIN_NAMESPACE
75
76
77class QStyleSheet;
78class QTextDocument;
79class QMenu;
80class QWidgetTextControlPrivate;
81class QAbstractScrollArea;
82class QEvent;
83class QRegularExpression;
84class QTimerEvent;
85
86class Q_WIDGETS_EXPORT QWidgetTextControl : public QInputControl
87{
88 Q_OBJECT
89 Q_DECLARE_PRIVATE(QWidgetTextControl)
90#ifndef QT_NO_TEXTHTMLPARSER
91 Q_PROPERTY(QString html READ toHtml WRITE setHtml NOTIFY textChanged USER true)
92#endif
93 Q_PROPERTY(bool overwriteMode READ overwriteMode WRITE setOverwriteMode)
94 Q_PROPERTY(bool acceptRichText READ acceptRichText WRITE setAcceptRichText)
95 Q_PROPERTY(int cursorWidth READ cursorWidth WRITE setCursorWidth)
96 Q_PROPERTY(Qt::TextInteractionFlags textInteractionFlags READ textInteractionFlags WRITE setTextInteractionFlags)
97 Q_PROPERTY(bool openExternalLinks READ openExternalLinks WRITE setOpenExternalLinks)
98 Q_PROPERTY(bool ignoreUnusedNavigationEvents READ ignoreUnusedNavigationEvents WRITE setIgnoreUnusedNavigationEvents)
99public:
100 explicit QWidgetTextControl(QObject *parent = nullptr);
101 explicit QWidgetTextControl(const QString &text, QObject *parent = nullptr);
102 explicit QWidgetTextControl(QTextDocument *doc, QObject *parent = nullptr);
103 virtual ~QWidgetTextControl();
104
105 void setDocument(QTextDocument *document);
106 QTextDocument *document() const;
107
108 void setTextCursor(const QTextCursor &cursor);
109 QTextCursor textCursor() const;
110
111 void setTextInteractionFlags(Qt::TextInteractionFlags flags);
112 Qt::TextInteractionFlags textInteractionFlags() const;
113
114 void mergeCurrentCharFormat(const QTextCharFormat &modifier);
115
116 void setCurrentCharFormat(const QTextCharFormat &format);
117 QTextCharFormat currentCharFormat() const;
118
119 bool find(const QString &exp, QTextDocument::FindFlags options = { });
120#if QT_CONFIG(regularexpression)
121 bool find(const QRegularExpression &exp, QTextDocument::FindFlags options = { });
122#endif
123
124 QString toPlainText() const;
125#ifndef QT_NO_TEXTHTMLPARSER
126 QString toHtml() const;
127#endif
128#if QT_CONFIG(textmarkdownwriter)
129 QString toMarkdown(QTextDocument::MarkdownFeatures features = QTextDocument::MarkdownDialectGitHub) const;
130#endif
131
132 virtual void ensureCursorVisible();
133
134 Q_INVOKABLE virtual QVariant loadResource(int type, const QUrl &name);
135#ifndef QT_NO_CONTEXTMENU
136 QMenu *createStandardContextMenu(const QPointF &pos, QWidget *parent);
137#endif
138
139 QTextCursor cursorForPosition(const QPointF &pos) const;
140 QRectF cursorRect(const QTextCursor &cursor) const;
141 QRectF cursorRect() const;
142 QRectF selectionRect(const QTextCursor &cursor) const;
143 QRectF selectionRect() const;
144
145 virtual QString anchorAt(const QPointF &pos) const;
146 QPointF anchorPosition(const QString &name) const;
147
148 QString anchorAtCursor() const;
149
150 QTextBlock blockWithMarkerAt(const QPointF &pos) const;
151
152 bool overwriteMode() const;
153 void setOverwriteMode(bool overwrite);
154
155 int cursorWidth() const;
156 void setCursorWidth(int width);
157
158 bool acceptRichText() const;
159 void setAcceptRichText(bool accept);
160
161#if QT_CONFIG(textedit)
162 void setExtraSelections(const QList<QTextEdit::ExtraSelection> &selections);
163 QList<QTextEdit::ExtraSelection> extraSelections() const;
164#endif
165
166 void setTextWidth(qreal width);
167 qreal textWidth() const;
168 QSizeF size() const;
169
170 void setOpenExternalLinks(bool open);
171 bool openExternalLinks() const;
172
173 void setIgnoreUnusedNavigationEvents(bool ignore);
174 bool ignoreUnusedNavigationEvents() const;
175
176 void moveCursor(QTextCursor::MoveOperation op, QTextCursor::MoveMode mode = QTextCursor::MoveAnchor);
177
178 bool canPaste() const;
179
180 void setCursorIsFocusIndicator(bool b);
181 bool cursorIsFocusIndicator() const;
182
183 void setDragEnabled(bool enabled);
184 bool isDragEnabled() const;
185
186 bool isWordSelectionEnabled() const;
187 void setWordSelectionEnabled(bool enabled);
188
189 bool isPreediting();
190
191 void print(QPagedPaintDevice *printer) const;
192
193 virtual int hitTest(const QPointF &point, Qt::HitTestAccuracy accuracy) const;
194 virtual QRectF blockBoundingRect(const QTextBlock &block) const;
195 QAbstractTextDocumentLayout::PaintContext getPaintContext(QWidget *widget) const;
196
197public Q_SLOTS:
198 void setPlainText(const QString &text);
199#if QT_CONFIG(textmarkdownreader)
200 void setMarkdown(const QString &text);
201#endif
202 void setHtml(const QString &text);
203
204#ifndef QT_NO_CLIPBOARD
205 void cut();
206 void copy();
207 void paste(QClipboard::Mode mode = QClipboard::Clipboard);
208#endif
209
210 void undo();
211 void redo();
212
213 void clear();
214 void selectAll();
215
216 void insertPlainText(const QString &text);
217#ifndef QT_NO_TEXTHTMLPARSER
218 void insertHtml(const QString &text);
219#endif
220
221 void append(const QString &text);
222 void appendHtml(const QString &html);
223 void appendPlainText(const QString &text);
224
225 void adjustSize();
226
227Q_SIGNALS:
228 void textChanged();
229 void undoAvailable(bool b);
230 void redoAvailable(bool b);
231 void currentCharFormatChanged(const QTextCharFormat &format);
232 void copyAvailable(bool b);
233 void selectionChanged();
234 void cursorPositionChanged();
235
236 // control signals
237 void updateRequest(const QRectF &rect = QRectF());
238 void documentSizeChanged(const QSizeF &);
239 void blockCountChanged(int newBlockCount);
240 void visibilityRequest(const QRectF &rect);
241 void microFocusChanged();
242 void linkActivated(const QString &link);
243 void linkHovered(const QString &);
244 void blockMarkerHovered(const QTextBlock &block);
245 void modificationChanged(bool m);
246
247public:
248 // control properties
249 QPalette palette() const;
250 void setPalette(const QPalette &pal);
251
252 virtual void processEvent(QEvent *e, const QTransform &transform, QWidget *contextWidget = nullptr);
253 void processEvent(QEvent *e, const QPointF &coordinateOffset = QPointF(), QWidget *contextWidget = nullptr);
254
255 // control methods
256 void drawContents(QPainter *painter, const QRectF &rect = QRectF(), QWidget *widget = nullptr);
257
258 void setFocus(bool focus, Qt::FocusReason = Qt::OtherFocusReason);
259
260 virtual QVariant inputMethodQuery(Qt::InputMethodQuery property, QVariant argument) const;
261
262 virtual QMimeData *createMimeDataFromSelection() const;
263 virtual bool canInsertFromMimeData(const QMimeData *source) const;
264 virtual void insertFromMimeData(const QMimeData *source);
265
266 bool setFocusToAnchor(const QTextCursor &newCursor);
267 bool setFocusToNextOrPreviousAnchor(bool next);
268 bool findNextPrevAnchor(const QTextCursor& from, bool next, QTextCursor& newAnchor);
269
270protected:
271 virtual void timerEvent(QTimerEvent *e) override;
272
273 virtual bool event(QEvent *e) override;
274
275private:
276 Q_DISABLE_COPY_MOVE(QWidgetTextControl)
277 Q_PRIVATE_SLOT(d_func(), void _q_updateCurrentCharFormatAndSelection())
278 Q_PRIVATE_SLOT(d_func(), void _q_emitCursorPosChanged(const QTextCursor &))
279 Q_PRIVATE_SLOT(d_func(), void _q_deleteSelected())
280 Q_PRIVATE_SLOT(d_func(), void _q_copyLink())
281 Q_PRIVATE_SLOT(d_func(), void _q_updateBlock(const QTextBlock &))
282 Q_PRIVATE_SLOT(d_func(), void _q_documentLayoutChanged())
283 Q_PRIVATE_SLOT(d_func(), void _q_contentsChanged(int, int, int))
284};
285
286
287#ifndef QT_NO_CONTEXTMENU
288class QUnicodeControlCharacterMenu : public QMenu
289{
290 Q_OBJECT
291public:
292 QUnicodeControlCharacterMenu(QObject *editWidget, QWidget *parent);
293
294private Q_SLOTS:
295 void menuActionTriggered();
296
297private:
298 QObject *editWidget;
299};
300#endif // QT_NO_CONTEXTMENU
301
302
303// also used by QLabel
304class QTextEditMimeData : public QMimeData
305{
306public:
307 inline QTextEditMimeData(const QTextDocumentFragment &aFragment) : fragment(aFragment) {}
308
309 virtual QStringList formats() const override;
310protected:
311 virtual QVariant retrieveData(const QString &mimeType, QMetaType type) const override;
312private:
313 void setup() const;
314
315 mutable QTextDocumentFragment fragment;
316};
317
318QT_END_NAMESPACE
319
320#endif // QWidgetTextControl_H
321