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