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 QLINEEDIT_P_H
5#define QLINEEDIT_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
20#include "private/qwidget_p.h"
21#include "QtWidgets/qlineedit.h"
22#if QT_CONFIG(toolbutton)
23#include "QtWidgets/qtoolbutton.h"
24#endif
25#include "QtGui/qtextlayout.h"
26#include "QtGui/qicon.h"
27#include "QtWidgets/qstyleoption.h"
28#include "QtCore/qbasictimer.h"
29#if QT_CONFIG(completer)
30#include "QtWidgets/qcompleter.h"
31#endif
32#include "QtCore/qpointer.h"
33#include "QtCore/qmimedata.h"
34#include <QtCore/qmargins.h>
35
36#include "private/qwidgetlinecontrol_p.h"
37
38#include <algorithm>
39
40QT_REQUIRE_CONFIG(lineedit);
41
42QT_BEGIN_NAMESPACE
43
44class QLineEditPrivate;
45
46// QLineEditIconButton: This is a simple helper class that represents clickable icons that fade in with text
47#if QT_CONFIG(toolbutton)
48class Q_AUTOTEST_EXPORT QLineEditIconButton : public QToolButton
49{
50 Q_OBJECT
51 Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity)
52public:
53 explicit QLineEditIconButton(QWidget *parent = nullptr);
54
55 qreal opacity() const { return m_opacity; }
56 void setOpacity(qreal value);
57#if QT_CONFIG(animation)
58 void animateShow(bool visible);
59
60 bool shouldHideWithText() const;
61 void setHideWithText(bool hide);
62 bool needsSpace() const {
63 if (m_fadingOut)
64 return false;
65 return isVisibleTo(parentWidget());
66 }
67#endif
68
69protected:
70 void actionEvent(QActionEvent *e) override;
71 void paintEvent(QPaintEvent *event) override;
72
73private slots:
74 void updateCursor();
75
76#if QT_CONFIG(animation)
77 void onAnimationFinished();
78#endif
79
80private:
81#if QT_CONFIG(animation)
82 void startOpacityAnimation(qreal endValue);
83#endif
84 QLineEditPrivate *lineEditPrivate() const;
85
86 qreal m_opacity;
87
88#if QT_CONFIG(animation)
89 bool m_hideWithText = false;
90 bool m_fadingOut = false;
91#endif
92
93};
94#endif // QT_CONFIG(toolbutton)
95
96class Q_AUTOTEST_EXPORT QLineEditPrivate : public QWidgetPrivate
97{
98 Q_DECLARE_PUBLIC(QLineEdit)
99public:
100 enum SideWidgetFlag {
101 SideWidgetFadeInWithText = 0x1,
102 SideWidgetCreatedByWidgetAction = 0x2,
103 SideWidgetClearButton = 0x4
104 };
105
106 struct SideWidgetEntry {
107 explicit SideWidgetEntry(QWidget *w = nullptr, QAction *a = nullptr, int _flags = 0) : widget(w), action(a), flags(_flags) {}
108
109 QWidget *widget;
110 QAction *action;
111 int flags;
112 };
113 typedef std::vector<SideWidgetEntry> SideWidgetEntryList;
114
115 struct SideWidgetParameters {
116 int iconSize;
117 int widgetWidth;
118 int widgetHeight;
119 int margin;
120 };
121
122 QLineEditPrivate()
123 : control(nullptr), frame(1), contextMenuEnabled(1), cursorVisible(0),
124 dragEnabled(0), clickCausedFocus(0), edited(0), hscroll(0), vscroll(0),
125 alignment(Qt::AlignLeading | Qt::AlignVCenter),
126 textMargins{0, 0, 0, 0},
127 lastTextSize(0), mouseYThreshold(0)
128 {
129 }
130
131 ~QLineEditPrivate()
132 {
133 }
134
135 QWidgetLineControl *control;
136
137#ifndef QT_NO_CONTEXTMENU
138 QPointer<QAction> selectAllAction;
139#endif
140 void init(const QString&);
141 void initMouseYThreshold();
142
143 QRect adjustedControlRect(const QRect &) const;
144
145 int xToPos(int x, QTextLine::CursorPosition = QTextLine::CursorBetweenCharacters) const;
146 bool inSelection(int x) const;
147 QRect cursorRect() const;
148 void setCursorVisible(bool visible);
149 void setText(const QString& text);
150
151 QString textBeforeCursor(int curPos) const;
152 QString textAfterCursor(int curPos) const;
153 void updatePasswordEchoEditing(bool);
154
155 void resetInputMethod();
156
157 inline bool shouldEnableInputMethod() const
158 {
159#if defined (Q_OS_ANDROID)
160 return !control->isReadOnly() || control->isSelectableByMouse();
161#else
162 return !control->isReadOnly();
163#endif
164 }
165 inline bool shouldShowPlaceholderText() const
166 {
167 return control->text().isEmpty() && control->preeditAreaText().isEmpty()
168 && !((alignment & Qt::AlignHCenter) && q_func()->hasFocus());
169 }
170
171 static inline QLineEditPrivate *get(QLineEdit *lineEdit) {
172 return lineEdit->d_func();
173 }
174
175 QPoint tripleClick;
176 QBasicTimer tripleClickTimer;
177 uint frame : 1;
178 uint contextMenuEnabled : 1;
179 uint cursorVisible : 1;
180 uint dragEnabled : 1;
181 uint clickCausedFocus : 1;
182 uint edited : 1;
183 int hscroll;
184 int vscroll;
185 uint alignment;
186 static const int verticalMargin;
187 static const int horizontalMargin;
188
189 bool sendMouseEventToInputContext(QMouseEvent *e);
190
191 QRect adjustedContentsRect() const;
192
193 void _q_handleWindowActivate();
194 void _q_textEdited(const QString &);
195 void _q_cursorPositionChanged(int, int);
196#ifdef QT_KEYPAD_NAVIGATION
197 void _q_editFocusChange(bool);
198#endif
199 void _q_selectionChanged();
200 void _q_updateNeeded(const QRect &);
201#if QT_CONFIG(completer)
202 void _q_completionHighlighted(const QString &);
203#endif
204 QPoint mousePressPos;
205#if QT_CONFIG(draganddrop)
206 QBasicTimer dndTimer;
207 void drag();
208#endif
209 void _q_textChanged(const QString &);
210 void _q_clearButtonClicked();
211 void _q_controlEditingFinished();
212
213 QMargins textMargins; // use effectiveTextMargins() in case of icon.
214
215 QString placeholderText;
216
217#if QT_CONFIG(action)
218 QWidget *addAction(QAction *newAction, QAction *before, QLineEdit::ActionPosition, int flags = 0);
219 void removeAction(QAction *action);
220#endif
221 SideWidgetParameters sideWidgetParameters() const;
222 QIcon clearButtonIcon() const;
223 void setClearButtonEnabled(bool enabled);
224 void positionSideWidgets();
225 inline bool hasSideWidgets() const { return !leadingSideWidgets.empty() || !trailingSideWidgets.empty(); }
226 inline const SideWidgetEntryList &leftSideWidgetList() const
227 { return q_func()->layoutDirection() == Qt::LeftToRight ? leadingSideWidgets : trailingSideWidgets; }
228 inline const SideWidgetEntryList &rightSideWidgetList() const
229 { return q_func()->layoutDirection() == Qt::LeftToRight ? trailingSideWidgets : leadingSideWidgets; }
230
231 QMargins effectiveTextMargins() const;
232
233private:
234 struct SideWidgetLocation {
235 QLineEdit::ActionPosition position;
236 int index;
237
238 bool isValid() const { return index >= 0; }
239 };
240 friend class QTypeInfo<SideWidgetLocation>;
241
242#if QT_CONFIG(action)
243 SideWidgetLocation findSideWidget(const QAction *a) const;
244#endif
245
246 SideWidgetEntryList leadingSideWidgets;
247 SideWidgetEntryList trailingSideWidgets;
248 int lastTextSize;
249 int mouseYThreshold;
250};
251Q_DECLARE_TYPEINFO(QLineEditPrivate::SideWidgetEntry, Q_PRIMITIVE_TYPE);
252Q_DECLARE_TYPEINFO(QLineEditPrivate::SideWidgetLocation, Q_PRIMITIVE_TYPE);
253
254QT_END_NAMESPACE
255
256#endif // QLINEEDIT_P_H
257

source code of qtbase/src/widgets/widgets/qlineedit_p.h