1 | // Copyright (C) 2016 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 QPLAINTEXTEDIT_P_H |
5 | #define QPLAINTEXTEDIT_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 "private/qabstractscrollarea_p.h" |
20 | #include "QtGui/qtextdocumentfragment.h" |
21 | #if QT_CONFIG(scrollbar) |
22 | #include "QtWidgets/qscrollbar.h" |
23 | #endif |
24 | #include "QtGui/qtextcursor.h" |
25 | #include "QtGui/qtextformat.h" |
26 | #if QT_CONFIG(menu) |
27 | #include "QtWidgets/qmenu.h" |
28 | #endif |
29 | #include "QtGui/qabstracttextdocumentlayout.h" |
30 | #include "QtCore/qbasictimer.h" |
31 | #include "qplaintextedit.h" |
32 | |
33 | #include "private/qwidgettextcontrol_p.h" |
34 | |
35 | QT_REQUIRE_CONFIG(textedit); |
36 | |
37 | QT_BEGIN_NAMESPACE |
38 | |
39 | class QMimeData; |
40 | |
41 | class QPlainTextEdit; |
42 | class ; |
43 | |
44 | class QPlainTextEditControl : public QWidgetTextControl |
45 | { |
46 | Q_OBJECT |
47 | public: |
48 | QPlainTextEditControl(QPlainTextEdit *parent); |
49 | |
50 | |
51 | QMimeData *createMimeDataFromSelection() const override; |
52 | bool canInsertFromMimeData(const QMimeData *source) const override; |
53 | void insertFromMimeData(const QMimeData *source) override; |
54 | int hitTest(const QPointF &point, Qt::HitTestAccuracy = Qt::FuzzyHit) const override; |
55 | QRectF blockBoundingRect(const QTextBlock &block) const override; |
56 | QString anchorAt(const QPointF &pos) const override; |
57 | inline QRectF cursorRect(const QTextCursor &cursor) const { |
58 | QRectF r = QWidgetTextControl::cursorRect(cursor); |
59 | r.setLeft(qMax(a: r.left(), b: (qreal) 0.)); |
60 | return r; |
61 | } |
62 | inline QRectF cursorRect() { return cursorRect(cursor: textCursor()); } |
63 | void ensureCursorVisible() override { |
64 | textEdit->ensureCursorVisible(); |
65 | emit microFocusChanged(); |
66 | } |
67 | |
68 | |
69 | QPlainTextEdit *textEdit; |
70 | int topBlock; |
71 | QTextBlock firstVisibleBlock() const; |
72 | |
73 | QVariant loadResource(int type, const QUrl &name) override { |
74 | return textEdit->loadResource(type, name); |
75 | } |
76 | |
77 | }; |
78 | |
79 | |
80 | class QPlainTextEditPrivate : public QAbstractScrollAreaPrivate |
81 | { |
82 | Q_DECLARE_PUBLIC(QPlainTextEdit) |
83 | public: |
84 | QPlainTextEditPrivate(); |
85 | |
86 | void init(const QString &txt = QString()); |
87 | void repaintContents(const QRectF &contentsRect); |
88 | void updatePlaceholderVisibility(); |
89 | |
90 | inline QPoint mapToContents(const QPoint &point) const |
91 | { return QPoint(point.x() + horizontalOffset(), point.y() + verticalOffset()); } |
92 | |
93 | void adjustScrollbars(); |
94 | void verticalScrollbarActionTriggered(int action); |
95 | void ensureViewportLayouted(); |
96 | void relayoutDocument(); |
97 | |
98 | void pageUpDown(QTextCursor::MoveOperation op, QTextCursor::MoveMode moveMode, bool moveCursor = true); |
99 | |
100 | inline int horizontalOffset() const |
101 | { return (q_func()->isRightToLeft() ? (hbar->maximum() - hbar->value()) : hbar->value()); } |
102 | qreal verticalOffset(int topBlock, int topLine) const; |
103 | qreal verticalOffset() const; |
104 | |
105 | inline void sendControlEvent(QEvent *e) |
106 | { control->processEvent(e, coordinateOffset: QPointF(horizontalOffset(), verticalOffset()), contextWidget: viewport); } |
107 | |
108 | void updateDefaultTextOption(); |
109 | |
110 | QBasicTimer autoScrollTimer; |
111 | #ifdef QT_KEYPAD_NAVIGATION |
112 | QBasicTimer deleteAllTimer; |
113 | #endif |
114 | QPoint autoScrollDragPos; |
115 | QString placeholderText; |
116 | |
117 | QPlainTextEditControl *control = nullptr; |
118 | qreal topLineFracture = 0; // for non-int sized fonts |
119 | qreal pageUpDownLastCursorY = 0; |
120 | QPlainTextEdit::LineWrapMode lineWrap = QPlainTextEdit::WidgetWidth; |
121 | QTextOption::WrapMode wordWrap = QTextOption::WrapAtWordBoundaryOrAnywhere; |
122 | int originalOffsetY = 0; |
123 | int topLine = 0; |
124 | |
125 | uint tabChangesFocus : 1; |
126 | uint showCursorOnInitialShow : 1; |
127 | uint backgroundVisible : 1; |
128 | uint centerOnScroll : 1; |
129 | uint inDrag : 1; |
130 | uint clickCausedFocus : 1; |
131 | uint pageUpDownLastCursorYIsValid : 1; |
132 | |
133 | void setTopLine(int visualTopLine, int dx = 0); |
134 | void setTopBlock(int newTopBlock, int newTopLine, int dx = 0); |
135 | |
136 | void ensureVisible(int position, bool center, bool forceCenter = false); |
137 | void ensureCursorVisible(bool center = false); |
138 | void updateViewport(); |
139 | |
140 | QPointer<QPlainTextDocumentLayout> documentLayoutPtr; |
141 | |
142 | void append(const QString &text, Qt::TextFormat format = Qt::AutoText); |
143 | |
144 | void cursorPositionChanged(); |
145 | void modificationChanged(bool); |
146 | inline bool isPlaceHolderTextVisible() |
147 | { |
148 | Q_Q(QPlainTextEdit); |
149 | return q->document()->isEmpty() && !q->placeholderText().isEmpty(); |
150 | } |
151 | }; |
152 | |
153 | QT_END_NAMESPACE |
154 | |
155 | #endif // QPLAINTEXTEDIT_P_H |
156 | |