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 
72 QT_REQUIRE_CONFIG(widgettextcontrol);
73 
74 QT_BEGIN_NAMESPACE
75 
76 
77 class QStyleSheet;
78 class QTextDocument;
79 class QMenu;
80 class QWidgetTextControlPrivate;
81 class QAbstractScrollArea;
82 class QEvent;
83 class QRegularExpression;
84 class QTimerEvent;
85 
86 class 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)
99 public:
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, bool selectionClipboard = false);
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 #ifndef QT_NO_REGEXP
121     bool find(const QRegExp &exp, QTextDocument::FindFlags options = { });
122 #endif
123 #if QT_CONFIG(regularexpression)
124     bool find(const QRegularExpression &exp, QTextDocument::FindFlags options = { });
125 #endif
126 
127     QString toPlainText() const;
128 #ifndef QT_NO_TEXTHTMLPARSER
129     QString toHtml() const;
130 #endif
131 #if QT_CONFIG(textmarkdownwriter)
132     QString toMarkdown(QTextDocument::MarkdownFeatures features = QTextDocument::MarkdownDialectGitHub) const;
133 #endif
134 
135     virtual void ensureCursorVisible();
136 
137     Q_INVOKABLE virtual QVariant loadResource(int type, const QUrl &name);
138 #ifndef QT_NO_CONTEXTMENU
139     QMenu *createStandardContextMenu(const QPointF &pos, QWidget *parent);
140 #endif
141 
142     QTextCursor cursorForPosition(const QPointF &pos) const;
143     QRectF cursorRect(const QTextCursor &cursor) const;
144     QRectF cursorRect() const;
145     QRectF selectionRect(const QTextCursor &cursor) const;
146     QRectF selectionRect() const;
147 
148     virtual QString anchorAt(const QPointF &pos) const;
149     QPointF anchorPosition(const QString &name) const;
150 
151     QString anchorAtCursor() const;
152 
153     QTextBlock blockWithMarkerAt(const QPointF &pos) const;
154 
155     bool overwriteMode() const;
156     void setOverwriteMode(bool overwrite);
157 
158     int cursorWidth() const;
159     void setCursorWidth(int width);
160 
161     bool acceptRichText() const;
162     void setAcceptRichText(bool accept);
163 
164 #if QT_CONFIG(textedit)
165     void setExtraSelections(const QList<QTextEdit::ExtraSelection> &selections);
166     QList<QTextEdit::ExtraSelection> extraSelections() const;
167 #endif
168 
169     void setTextWidth(qreal width);
170     qreal textWidth() const;
171     QSizeF size() const;
172 
173     void setOpenExternalLinks(bool open);
174     bool openExternalLinks() const;
175 
176     void setIgnoreUnusedNavigationEvents(bool ignore);
177     bool ignoreUnusedNavigationEvents() const;
178 
179     void moveCursor(QTextCursor::MoveOperation op, QTextCursor::MoveMode mode = QTextCursor::MoveAnchor);
180 
181     bool canPaste() const;
182 
183     void setCursorIsFocusIndicator(bool b);
184     bool cursorIsFocusIndicator() const;
185 
186     void setDragEnabled(bool enabled);
187     bool isDragEnabled() const;
188 
189     bool isWordSelectionEnabled() const;
190     void setWordSelectionEnabled(bool enabled);
191 
192     bool isPreediting();
193 
194     void print(QPagedPaintDevice *printer) const;
195 
196     virtual int hitTest(const QPointF &point, Qt::HitTestAccuracy accuracy) const;
197     virtual QRectF blockBoundingRect(const QTextBlock &block) const;
198     QAbstractTextDocumentLayout::PaintContext getPaintContext(QWidget *widget) const;
199 
200 public Q_SLOTS:
201     void setPlainText(const QString &text);
202 #if QT_CONFIG(textmarkdownreader)
203     void setMarkdown(const QString &text);
204 #endif
205     void setHtml(const QString &text);
206 
207 #ifndef QT_NO_CLIPBOARD
208     void cut();
209     void copy();
210     void paste(QClipboard::Mode mode = QClipboard::Clipboard);
211 #endif
212 
213     void undo();
214     void redo();
215 
216     void clear();
217     void selectAll();
218 
219     void insertPlainText(const QString &text);
220 #ifndef QT_NO_TEXTHTMLPARSER
221     void insertHtml(const QString &text);
222 #endif
223 
224     void append(const QString &text);
225     void appendHtml(const QString &html);
226     void appendPlainText(const QString &text);
227 
228     void adjustSize();
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     // control signals
240     void updateRequest(const QRectF &rect = QRectF());
241     void documentSizeChanged(const QSizeF &);
242     void blockCountChanged(int newBlockCount);
243     void visibilityRequest(const QRectF &rect);
244     void microFocusChanged();
245     void linkActivated(const QString &link);
246     void linkHovered(const QString &);
247     void blockMarkerHovered(const QTextBlock &block);
248     void modificationChanged(bool m);
249 
250 public:
251     // control properties
252     QPalette palette() const;
253     void setPalette(const QPalette &pal);
254 
255     virtual void processEvent(QEvent *e, const QTransform &transform, QWidget *contextWidget = nullptr);
256     void processEvent(QEvent *e, const QPointF &coordinateOffset = QPointF(), QWidget *contextWidget = nullptr);
257 
258     // control methods
259     void drawContents(QPainter *painter, const QRectF &rect = QRectF(), QWidget *widget = nullptr);
260 
261     void setFocus(bool focus, Qt::FocusReason = Qt::OtherFocusReason);
262 
263     virtual QVariant inputMethodQuery(Qt::InputMethodQuery property, QVariant argument) const;
264 
265     virtual QMimeData *createMimeDataFromSelection() const;
266     virtual bool canInsertFromMimeData(const QMimeData *source) const;
267     virtual void insertFromMimeData(const QMimeData *source);
268 
269     bool setFocusToAnchor(const QTextCursor &newCursor);
270     bool setFocusToNextOrPreviousAnchor(bool next);
271     bool findNextPrevAnchor(const QTextCursor& from, bool next, QTextCursor& newAnchor);
272 
273 protected:
274     virtual void timerEvent(QTimerEvent *e) override;
275 
276     virtual bool event(QEvent *e) override;
277 
278 private:
279     Q_DISABLE_COPY_MOVE(QWidgetTextControl)
280     Q_PRIVATE_SLOT(d_func(), void _q_updateCurrentCharFormatAndSelection())
281     Q_PRIVATE_SLOT(d_func(), void _q_emitCursorPosChanged(const QTextCursor &))
282     Q_PRIVATE_SLOT(d_func(), void _q_deleteSelected())
283     Q_PRIVATE_SLOT(d_func(), void _q_copyLink())
284     Q_PRIVATE_SLOT(d_func(), void _q_updateBlock(const QTextBlock &))
285     Q_PRIVATE_SLOT(d_func(), void _q_documentLayoutChanged())
286     Q_PRIVATE_SLOT(d_func(), void _q_contentsChanged(int, int, int))
287 };
288 
289 
290 #ifndef QT_NO_CONTEXTMENU
291 class QUnicodeControlCharacterMenu : public QMenu
292 {
293     Q_OBJECT
294 public:
295     QUnicodeControlCharacterMenu(QObject *editWidget, QWidget *parent);
296 
297 private Q_SLOTS:
298     void menuActionTriggered();
299 
300 private:
301     QObject *editWidget;
302 };
303 #endif // QT_NO_CONTEXTMENU
304 
305 
306 // also used by QLabel
307 class QTextEditMimeData : public QMimeData
308 {
309 public:
QTextEditMimeData(const QTextDocumentFragment & aFragment)310     inline QTextEditMimeData(const QTextDocumentFragment &aFragment) : fragment(aFragment) {}
311 
312     virtual QStringList formats() const override;
313 protected:
314     virtual QVariant retrieveData(const QString &mimeType, QVariant::Type type) const override;
315 private:
316     void setup() const;
317 
318     mutable QTextDocumentFragment fragment;
319 };
320 
321 QT_END_NAMESPACE
322 
323 #endif // QWidgetTextControl_H
324