| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 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 QPLAINTEXTEDIT_H |
| 41 | #define QPLAINTEXTEDIT_H |
| 42 | |
| 43 | #include <QtWidgets/qtwidgetsglobal.h> |
| 44 | #include <QtWidgets/qtextedit.h> |
| 45 | |
| 46 | #include <QtWidgets/qabstractscrollarea.h> |
| 47 | #include <QtGui/qtextdocument.h> |
| 48 | #include <QtGui/qtextoption.h> |
| 49 | #include <QtGui/qtextcursor.h> |
| 50 | #include <QtGui/qtextformat.h> |
| 51 | #include <QtGui/qabstracttextdocumentlayout.h> |
| 52 | |
| 53 | QT_REQUIRE_CONFIG(textedit); |
| 54 | |
| 55 | QT_BEGIN_NAMESPACE |
| 56 | |
| 57 | class QStyleSheet; |
| 58 | class QTextDocument; |
| 59 | class ; |
| 60 | class QPlainTextEditPrivate; |
| 61 | class QMimeData; |
| 62 | class QPagedPaintDevice; |
| 63 | class QRegularExpression; |
| 64 | |
| 65 | class Q_WIDGETS_EXPORT QPlainTextEdit : public QAbstractScrollArea |
| 66 | { |
| 67 | Q_OBJECT |
| 68 | Q_DECLARE_PRIVATE(QPlainTextEdit) |
| 69 | Q_PROPERTY(bool tabChangesFocus READ tabChangesFocus WRITE setTabChangesFocus) |
| 70 | Q_PROPERTY(QString documentTitle READ documentTitle WRITE setDocumentTitle) |
| 71 | Q_PROPERTY(bool undoRedoEnabled READ isUndoRedoEnabled WRITE setUndoRedoEnabled) |
| 72 | Q_PROPERTY(LineWrapMode lineWrapMode READ lineWrapMode WRITE setLineWrapMode) |
| 73 | QDOC_PROPERTY(QTextOption::WrapMode wordWrapMode READ wordWrapMode WRITE setWordWrapMode) |
| 74 | Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly) |
| 75 | Q_PROPERTY(QString plainText READ toPlainText WRITE setPlainText NOTIFY textChanged USER true) |
| 76 | Q_PROPERTY(bool overwriteMode READ overwriteMode WRITE setOverwriteMode) |
| 77 | #if QT_DEPRECATED_SINCE(5, 10) |
| 78 | Q_PROPERTY(int tabStopWidth READ tabStopWidth WRITE setTabStopWidth) |
| 79 | #endif |
| 80 | Q_PROPERTY(qreal tabStopDistance READ tabStopDistance WRITE setTabStopDistance) |
| 81 | Q_PROPERTY(int cursorWidth READ cursorWidth WRITE setCursorWidth) |
| 82 | Q_PROPERTY(Qt::TextInteractionFlags textInteractionFlags READ textInteractionFlags WRITE setTextInteractionFlags) |
| 83 | Q_PROPERTY(int blockCount READ blockCount) |
| 84 | Q_PROPERTY(int maximumBlockCount READ maximumBlockCount WRITE setMaximumBlockCount) |
| 85 | Q_PROPERTY(bool backgroundVisible READ backgroundVisible WRITE setBackgroundVisible) |
| 86 | Q_PROPERTY(bool centerOnScroll READ centerOnScroll WRITE setCenterOnScroll) |
| 87 | Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText) |
| 88 | public: |
| 89 | enum LineWrapMode { |
| 90 | NoWrap, |
| 91 | WidgetWidth |
| 92 | }; |
| 93 | Q_ENUM(LineWrapMode) |
| 94 | |
| 95 | explicit QPlainTextEdit(QWidget *parent = nullptr); |
| 96 | explicit QPlainTextEdit(const QString &text, QWidget *parent = nullptr); |
| 97 | virtual ~QPlainTextEdit(); |
| 98 | |
| 99 | void setDocument(QTextDocument *document); |
| 100 | QTextDocument *document() const; |
| 101 | |
| 102 | void setPlaceholderText(const QString &placeholderText); |
| 103 | QString placeholderText() const; |
| 104 | |
| 105 | void setTextCursor(const QTextCursor &cursor); |
| 106 | QTextCursor textCursor() const; |
| 107 | |
| 108 | bool isReadOnly() const; |
| 109 | void setReadOnly(bool ro); |
| 110 | |
| 111 | void setTextInteractionFlags(Qt::TextInteractionFlags flags); |
| 112 | Qt::TextInteractionFlags textInteractionFlags() const; |
| 113 | |
| 114 | void mergeCurrentCharFormat(const QTextCharFormat &modifier); |
| 115 | void setCurrentCharFormat(const QTextCharFormat &format); |
| 116 | QTextCharFormat currentCharFormat() const; |
| 117 | |
| 118 | bool tabChangesFocus() const; |
| 119 | void setTabChangesFocus(bool b); |
| 120 | |
| 121 | inline void setDocumentTitle(const QString &title) |
| 122 | { document()->setMetaInformation(QTextDocument::DocumentTitle, title); } |
| 123 | inline QString documentTitle() const |
| 124 | { return document()->metaInformation(QTextDocument::DocumentTitle); } |
| 125 | |
| 126 | inline bool isUndoRedoEnabled() const |
| 127 | { return document()->isUndoRedoEnabled(); } |
| 128 | inline void setUndoRedoEnabled(bool enable) |
| 129 | { document()->setUndoRedoEnabled(enable); } |
| 130 | |
| 131 | inline void setMaximumBlockCount(int maximum) |
| 132 | { document()->setMaximumBlockCount(maximum); } |
| 133 | inline int maximumBlockCount() const |
| 134 | { return document()->maximumBlockCount(); } |
| 135 | |
| 136 | |
| 137 | LineWrapMode lineWrapMode() const; |
| 138 | void setLineWrapMode(LineWrapMode mode); |
| 139 | |
| 140 | QTextOption::WrapMode wordWrapMode() const; |
| 141 | void setWordWrapMode(QTextOption::WrapMode policy); |
| 142 | |
| 143 | void setBackgroundVisible(bool visible); |
| 144 | bool backgroundVisible() const; |
| 145 | |
| 146 | void setCenterOnScroll(bool enabled); |
| 147 | bool centerOnScroll() const; |
| 148 | |
| 149 | bool find(const QString &exp, QTextDocument::FindFlags options = QTextDocument::FindFlags()); |
| 150 | #ifndef QT_NO_REGEXP |
| 151 | bool find(const QRegExp &exp, QTextDocument::FindFlags options = QTextDocument::FindFlags()); |
| 152 | #endif |
| 153 | #if QT_CONFIG(regularexpression) |
| 154 | bool find(const QRegularExpression &exp, QTextDocument::FindFlags options = QTextDocument::FindFlags()); |
| 155 | #endif |
| 156 | |
| 157 | inline QString toPlainText() const |
| 158 | { return document()->toPlainText(); } |
| 159 | |
| 160 | void ensureCursorVisible(); |
| 161 | |
| 162 | virtual QVariant loadResource(int type, const QUrl &name); |
| 163 | #ifndef QT_NO_CONTEXTMENU |
| 164 | QMenu *createStandardContextMenu(); |
| 165 | QMenu *createStandardContextMenu(const QPoint &position); |
| 166 | #endif |
| 167 | |
| 168 | QTextCursor cursorForPosition(const QPoint &pos) const; |
| 169 | QRect cursorRect(const QTextCursor &cursor) const; |
| 170 | QRect cursorRect() const; |
| 171 | |
| 172 | QString anchorAt(const QPoint &pos) const; |
| 173 | |
| 174 | bool overwriteMode() const; |
| 175 | void setOverwriteMode(bool overwrite); |
| 176 | |
| 177 | #if QT_DEPRECATED_SINCE(5, 10) |
| 178 | QT_DEPRECATED int tabStopWidth() const; |
| 179 | QT_DEPRECATED void setTabStopWidth(int width); |
| 180 | #endif |
| 181 | |
| 182 | qreal tabStopDistance() const; |
| 183 | void setTabStopDistance(qreal distance); |
| 184 | |
| 185 | int cursorWidth() const; |
| 186 | void setCursorWidth(int width); |
| 187 | |
| 188 | void (const QList<QTextEdit::ExtraSelection> &selections); |
| 189 | QList<QTextEdit::ExtraSelection> () const; |
| 190 | |
| 191 | void moveCursor(QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode = QTextCursor::MoveAnchor); |
| 192 | |
| 193 | bool canPaste() const; |
| 194 | |
| 195 | void print(QPagedPaintDevice *printer) const; |
| 196 | |
| 197 | int blockCount() const; |
| 198 | QVariant inputMethodQuery(Qt::InputMethodQuery property) const override; |
| 199 | Q_INVOKABLE QVariant inputMethodQuery(Qt::InputMethodQuery query, QVariant argument) const; |
| 200 | |
| 201 | public Q_SLOTS: |
| 202 | |
| 203 | void setPlainText(const QString &text); |
| 204 | |
| 205 | #ifndef QT_NO_CLIPBOARD |
| 206 | void cut(); |
| 207 | void copy(); |
| 208 | void paste(); |
| 209 | #endif |
| 210 | |
| 211 | void undo(); |
| 212 | void redo(); |
| 213 | |
| 214 | void clear(); |
| 215 | void selectAll(); |
| 216 | |
| 217 | void insertPlainText(const QString &text); |
| 218 | |
| 219 | void appendPlainText(const QString &text); |
| 220 | void appendHtml(const QString &html); |
| 221 | |
| 222 | void centerCursor(); |
| 223 | |
| 224 | void zoomIn(int range = 1); |
| 225 | void zoomOut(int range = 1); |
| 226 | |
| 227 | Q_SIGNALS: |
| 228 | void textChanged(); |
| 229 | void undoAvailable(bool b); |
| 230 | void redoAvailable(bool b); |
| 231 | void copyAvailable(bool b); |
| 232 | void selectionChanged(); |
| 233 | void cursorPositionChanged(); |
| 234 | |
| 235 | void updateRequest(const QRect &rect, int dy); |
| 236 | void blockCountChanged(int newBlockCount); |
| 237 | void modificationChanged(bool); |
| 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 | QPlainTextEdit(QPlainTextEditPrivate &dd, QWidget *parent); |
| 275 | |
| 276 | virtual void scrollContentsBy(int dx, int dy) override; |
| 277 | virtual void doSetTextCursor(const QTextCursor &cursor); |
| 278 | |
| 279 | QTextBlock firstVisibleBlock() const; |
| 280 | QPointF contentOffset() const; |
| 281 | QRectF blockBoundingRect(const QTextBlock &block) const; |
| 282 | QRectF blockBoundingGeometry(const QTextBlock &block) const; |
| 283 | QAbstractTextDocumentLayout::PaintContext getPaintContext() const; |
| 284 | |
| 285 | void zoomInF(float range); |
| 286 | |
| 287 | private: |
| 288 | Q_DISABLE_COPY(QPlainTextEdit) |
| 289 | Q_PRIVATE_SLOT(d_func(), void _q_repaintContents(const QRectF &r)) |
| 290 | Q_PRIVATE_SLOT(d_func(), void _q_textChanged()) |
| 291 | Q_PRIVATE_SLOT(d_func(), void _q_adjustScrollbars()) |
| 292 | Q_PRIVATE_SLOT(d_func(), void _q_verticalScrollbarActionTriggered(int)) |
| 293 | Q_PRIVATE_SLOT(d_func(), void _q_cursorPositionChanged()) |
| 294 | |
| 295 | friend class QPlainTextEditControl; |
| 296 | }; |
| 297 | |
| 298 | |
| 299 | class QPlainTextDocumentLayoutPrivate; |
| 300 | class Q_WIDGETS_EXPORT QPlainTextDocumentLayout : public QAbstractTextDocumentLayout |
| 301 | { |
| 302 | Q_OBJECT |
| 303 | Q_DECLARE_PRIVATE(QPlainTextDocumentLayout) |
| 304 | Q_PROPERTY(int cursorWidth READ cursorWidth WRITE setCursorWidth) |
| 305 | |
| 306 | public: |
| 307 | QPlainTextDocumentLayout(QTextDocument *document); |
| 308 | ~QPlainTextDocumentLayout(); |
| 309 | |
| 310 | void draw(QPainter *, const PaintContext &) override; |
| 311 | int hitTest(const QPointF &, Qt::HitTestAccuracy ) const override; |
| 312 | |
| 313 | int pageCount() const override; |
| 314 | QSizeF documentSize() const override; |
| 315 | |
| 316 | QRectF frameBoundingRect(QTextFrame *) const override; |
| 317 | QRectF blockBoundingRect(const QTextBlock &block) const override; |
| 318 | |
| 319 | void ensureBlockLayout(const QTextBlock &block) const; |
| 320 | |
| 321 | void setCursorWidth(int width); |
| 322 | int cursorWidth() const; |
| 323 | |
| 324 | void requestUpdate(); |
| 325 | |
| 326 | protected: |
| 327 | void documentChanged(int from, int /*charsRemoved*/, int charsAdded) override; |
| 328 | |
| 329 | |
| 330 | private: |
| 331 | void setTextWidth(qreal newWidth); |
| 332 | qreal textWidth() const; |
| 333 | void layoutBlock(const QTextBlock &block); |
| 334 | qreal blockWidth(const QTextBlock &block); |
| 335 | |
| 336 | QPlainTextDocumentLayoutPrivate *priv() const; |
| 337 | |
| 338 | friend class QPlainTextEdit; |
| 339 | friend class QPlainTextEditPrivate; |
| 340 | }; |
| 341 | |
| 342 | QT_END_NAMESPACE |
| 343 | |
| 344 | #endif // QPLAINTEXTEDIT_H |
| 345 | |