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 QLINEEDIT_H
41 #define QLINEEDIT_H
42 
43 #include <QtWidgets/qtwidgetsglobal.h>
44 #include <QtWidgets/qframe.h>
45 #include <QtGui/qtextcursor.h>
46 #include <QtCore/qstring.h>
47 #include <QtCore/qmargins.h>
48 
49 QT_REQUIRE_CONFIG(lineedit);
50 
51 QT_BEGIN_NAMESPACE
52 
53 class QValidator;
54 class QMenu;
55 class QLineEditPrivate;
56 class QCompleter;
57 class QStyleOptionFrame;
58 class QAbstractSpinBox;
59 class QDateTimeEdit;
60 class QIcon;
61 class QToolButton;
62 
63 class Q_WIDGETS_EXPORT QLineEdit : public QWidget
64 {
65     Q_OBJECT
66 
67     Q_PROPERTY(QString inputMask READ inputMask WRITE setInputMask)
68     Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged USER true)
69     Q_PROPERTY(int maxLength READ maxLength WRITE setMaxLength)
70     Q_PROPERTY(bool frame READ hasFrame WRITE setFrame)
71     Q_PROPERTY(EchoMode echoMode READ echoMode WRITE setEchoMode)
72     Q_PROPERTY(QString displayText READ displayText)
73     Q_PROPERTY(int cursorPosition READ cursorPosition WRITE setCursorPosition)
74     Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
75     Q_PROPERTY(bool modified READ isModified WRITE setModified DESIGNABLE false)
76     Q_PROPERTY(bool hasSelectedText READ hasSelectedText)
77     Q_PROPERTY(QString selectedText READ selectedText)
78     Q_PROPERTY(bool dragEnabled READ dragEnabled WRITE setDragEnabled)
79     Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
80     Q_PROPERTY(bool undoAvailable READ isUndoAvailable)
81     Q_PROPERTY(bool redoAvailable READ isRedoAvailable)
82     Q_PROPERTY(bool acceptableInput READ hasAcceptableInput)
83     Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText)
84     Q_PROPERTY(Qt::CursorMoveStyle cursorMoveStyle READ cursorMoveStyle WRITE setCursorMoveStyle)
85     Q_PROPERTY(bool clearButtonEnabled READ isClearButtonEnabled WRITE setClearButtonEnabled)
86 public:
87     enum ActionPosition {
88         LeadingPosition,
89         TrailingPosition
90     };
91     Q_ENUM(ActionPosition)
92 
93     explicit QLineEdit(QWidget *parent = nullptr);
94     explicit QLineEdit(const QString &, QWidget *parent = nullptr);
95     ~QLineEdit();
96 
97     QString text() const;
98 
99     QString displayText() const;
100 
101     QString placeholderText() const;
102     void setPlaceholderText(const QString &);
103 
104     int maxLength() const;
105     void setMaxLength(int);
106 
107     void setFrame(bool);
108     bool hasFrame() const;
109 
110     void setClearButtonEnabled(bool enable);
111     bool isClearButtonEnabled() const;
112 
113     enum EchoMode { Normal, NoEcho, Password, PasswordEchoOnEdit };
114     Q_ENUM(EchoMode)
115     EchoMode echoMode() const;
116     void setEchoMode(EchoMode);
117 
118     bool isReadOnly() const;
119     void setReadOnly(bool);
120 
121 #ifndef QT_NO_VALIDATOR
122     void setValidator(const QValidator *);
123     const QValidator * validator() const;
124 #endif
125 
126 #if QT_CONFIG(completer)
127     void setCompleter(QCompleter *completer);
128     QCompleter *completer() const;
129 #endif
130 
131     QSize sizeHint() const override;
132     QSize minimumSizeHint() const override;
133 
134     int cursorPosition() const;
135     void setCursorPosition(int);
136     int cursorPositionAt(const QPoint &pos);
137 
138     void setAlignment(Qt::Alignment flag);
139     Qt::Alignment alignment() const;
140 
141     void cursorForward(bool mark, int steps = 1);
142     void cursorBackward(bool mark, int steps = 1);
143     void cursorWordForward(bool mark);
144     void cursorWordBackward(bool mark);
145     void backspace();
146     void del();
147     void home(bool mark);
148     void end(bool mark);
149 
150     bool isModified() const;
151     void setModified(bool);
152 
153     void setSelection(int, int);
154     bool hasSelectedText() const;
155     QString selectedText() const;
156     int selectionStart() const;
157     int selectionEnd() const;
158     int selectionLength() const;
159 
160     bool isUndoAvailable() const;
161     bool isRedoAvailable() const;
162 
163     void setDragEnabled(bool b);
164     bool dragEnabled() const;
165 
166     void setCursorMoveStyle(Qt::CursorMoveStyle style);
167     Qt::CursorMoveStyle cursorMoveStyle() const;
168 
169     QString inputMask() const;
170     void setInputMask(const QString &inputMask);
171     bool hasAcceptableInput() const;
172 
173     void setTextMargins(int left, int top, int right, int bottom);
174     void setTextMargins(const QMargins &margins);
175 #if QT_DEPRECATED_SINCE(5, 14)
176     QT_DEPRECATED_X("use textMargins()")
177     void getTextMargins(int *left, int *top, int *right, int *bottom) const;
178 #endif
179     QMargins textMargins() const;
180 
181 #if QT_CONFIG(action)
182     using QWidget::addAction;
183     void addAction(QAction *action, ActionPosition position);
184     QAction *addAction(const QIcon &icon, ActionPosition position);
185 #endif
186 
187 public Q_SLOTS:
188     void setText(const QString &);
189     void clear();
190     void selectAll();
191     void undo();
192     void redo();
193 #ifndef QT_NO_CLIPBOARD
194     void cut();
195     void copy() const;
196     void paste();
197 #endif
198 
199 public:
200     void deselect();
201     void insert(const QString &);
202 #ifndef QT_NO_CONTEXTMENU
203     QMenu *createStandardContextMenu();
204 #endif
205 
206 Q_SIGNALS:
207     void textChanged(const QString &);
208     void textEdited(const QString &);
209     void cursorPositionChanged(int, int);
210     void returnPressed();
211     void editingFinished();
212     void selectionChanged();
213     void inputRejected();
214 
215 protected:
216     void mousePressEvent(QMouseEvent *) override;
217     void mouseMoveEvent(QMouseEvent *) override;
218     void mouseReleaseEvent(QMouseEvent *) override;
219     void mouseDoubleClickEvent(QMouseEvent *) override;
220     void keyPressEvent(QKeyEvent *) override;
221     void focusInEvent(QFocusEvent *) override;
222     void focusOutEvent(QFocusEvent *) override;
223     void paintEvent(QPaintEvent *) override;
224 #if QT_CONFIG(draganddrop)
225     void dragEnterEvent(QDragEnterEvent *) override;
226     void dragMoveEvent(QDragMoveEvent *e) override;
227     void dragLeaveEvent(QDragLeaveEvent *e) override;
228     void dropEvent(QDropEvent *) override;
229 #endif
230     void changeEvent(QEvent *) override;
231 #ifndef QT_NO_CONTEXTMENU
232     void contextMenuEvent(QContextMenuEvent *) override;
233 #endif
234 
235     void inputMethodEvent(QInputMethodEvent *) override;
236     void initStyleOption(QStyleOptionFrame *option) const;
237 public:
238     QVariant inputMethodQuery(Qt::InputMethodQuery) const override;
239     Q_INVOKABLE QVariant inputMethodQuery(Qt::InputMethodQuery property, QVariant argument) const;
240     bool event(QEvent *) override;
241 protected:
242     QRect cursorRect() const;
243 
244 public:
245 
246 private:
247     friend class QAbstractSpinBox;
248     friend class QAccessibleLineEdit;
249     friend class QComboBox;
250 #ifdef QT_KEYPAD_NAVIGATION
251     friend class QDateTimeEdit;
252 #endif
253     Q_DISABLE_COPY(QLineEdit)
254     Q_DECLARE_PRIVATE(QLineEdit)
255     Q_PRIVATE_SLOT(d_func(), void _q_handleWindowActivate())
256     Q_PRIVATE_SLOT(d_func(), void _q_textEdited(const QString &))
257     Q_PRIVATE_SLOT(d_func(), void _q_cursorPositionChanged(int, int))
258 #if QT_CONFIG(completer)
259     Q_PRIVATE_SLOT(d_func(), void _q_completionHighlighted(const QString &))
260 #endif
261 #ifdef QT_KEYPAD_NAVIGATION
262     Q_PRIVATE_SLOT(d_func(), void _q_editFocusChange(bool))
263 #endif
264     Q_PRIVATE_SLOT(d_func(), void _q_selectionChanged())
265     Q_PRIVATE_SLOT(d_func(), void _q_updateNeeded(const QRect &))
266     Q_PRIVATE_SLOT(d_func(), void _q_textChanged(const QString &))
267     Q_PRIVATE_SLOT(d_func(), void _q_clearButtonClicked())
268 };
269 
270 QT_END_NAMESPACE
271 
272 #endif // QLINEEDIT_H
273