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