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