Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/widgets/qlineedit.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 | #include "qlineedit.h" | - | ||||||||||||
41 | #include "qlineedit_p.h" | - | ||||||||||||
42 | - | |||||||||||||
43 | #ifndef QT_NO_LINEEDIT | - | ||||||||||||
44 | #include "qaction.h" | - | ||||||||||||
45 | #include "qapplication.h" | - | ||||||||||||
46 | #include "qclipboard.h" | - | ||||||||||||
47 | #include "qdrag.h" | - | ||||||||||||
48 | #include "qdrawutil.h" | - | ||||||||||||
49 | #include "qevent.h" | - | ||||||||||||
50 | #include "qfontmetrics.h" | - | ||||||||||||
51 | #include "qstylehints.h" | - | ||||||||||||
52 | #include "qmenu.h" | - | ||||||||||||
53 | #include "qpainter.h" | - | ||||||||||||
54 | #include "qpixmap.h" | - | ||||||||||||
55 | #include "qpointer.h" | - | ||||||||||||
56 | #include "qstringlist.h" | - | ||||||||||||
57 | #include "qstyle.h" | - | ||||||||||||
58 | #include "qstyleoption.h" | - | ||||||||||||
59 | #include "qtimer.h" | - | ||||||||||||
60 | #include "qvalidator.h" | - | ||||||||||||
61 | #include "qvariant.h" | - | ||||||||||||
62 | #include "qvector.h" | - | ||||||||||||
63 | #include "qwhatsthis.h" | - | ||||||||||||
64 | #include "qdebug.h" | - | ||||||||||||
65 | #include "qtextedit.h" | - | ||||||||||||
66 | #include <private/qtextedit_p.h> | - | ||||||||||||
67 | #ifndef QT_NO_ACCESSIBILITY | - | ||||||||||||
68 | #include "qaccessible.h" | - | ||||||||||||
69 | #endif | - | ||||||||||||
70 | #include "qabstractitemview.h" | - | ||||||||||||
71 | #include "private/qstylesheetstyle_p.h" | - | ||||||||||||
72 | - | |||||||||||||
73 | #ifndef QT_NO_SHORTCUT | - | ||||||||||||
74 | #include "private/qapplication_p.h" | - | ||||||||||||
75 | #include "private/qshortcutmap_p.h" | - | ||||||||||||
76 | #include "qkeysequence.h" | - | ||||||||||||
77 | #define ACCEL_KEY(k) (!qApp->d_func()->shortcutMap.hasShortcutForKeySequence(k) ? \ | - | ||||||||||||
78 | QLatin1Char('\t') + QKeySequence(k).toString(QKeySequence::NativeText) : QString()) | - | ||||||||||||
79 | #else | - | ||||||||||||
80 | #define ACCEL_KEY(k) QString() | - | ||||||||||||
81 | #endif | - | ||||||||||||
82 | - | |||||||||||||
83 | #include <limits.h> | - | ||||||||||||
84 | #ifdef DrawText | - | ||||||||||||
85 | #undef DrawText | - | ||||||||||||
86 | #endif | - | ||||||||||||
87 | - | |||||||||||||
88 | QT_BEGIN_NAMESPACE | - | ||||||||||||
89 | - | |||||||||||||
90 | #ifdef Q_DEAD_CODE_FROM_QT4_MAC | - | ||||||||||||
91 | extern void qt_mac_secure_keyboard(bool); //qapplication_mac.cpp | - | ||||||||||||
92 | #endif | - | ||||||||||||
93 | - | |||||||||||||
94 | /*! | - | ||||||||||||
95 | Initialize \a option with the values from this QLineEdit. This method | - | ||||||||||||
96 | is useful for subclasses when they need a QStyleOptionFrame, but don't want | - | ||||||||||||
97 | to fill in all the information themselves. | - | ||||||||||||
98 | - | |||||||||||||
99 | \sa QStyleOption::initFrom() | - | ||||||||||||
100 | */ | - | ||||||||||||
101 | void QLineEdit::initStyleOption(QStyleOptionFrame *option) const | - | ||||||||||||
102 | { | - | ||||||||||||
103 | if (!option) | - | ||||||||||||
104 | return; | - | ||||||||||||
105 | - | |||||||||||||
106 | Q_D(const QLineEdit); | - | ||||||||||||
107 | option->initFrom(this); | - | ||||||||||||
108 | option->rect = contentsRect(); | - | ||||||||||||
109 | option->lineWidth = d->frame ? style()->pixelMetric(QStyle::PM_DefaultFrameWidth, option, this) | - | ||||||||||||
110 | : 0; | - | ||||||||||||
111 | option->midLineWidth = 0; | - | ||||||||||||
112 | option->state |= QStyle::State_Sunken; | - | ||||||||||||
113 | if (d->control->isReadOnly()) | - | ||||||||||||
114 | option->state |= QStyle::State_ReadOnly; | - | ||||||||||||
115 | #ifdef QT_KEYPAD_NAVIGATION | - | ||||||||||||
116 | if (hasEditFocus()) | - | ||||||||||||
117 | option->state |= QStyle::State_HasEditFocus; | - | ||||||||||||
118 | #endif | - | ||||||||||||
119 | option->features = QStyleOptionFrame::None; | - | ||||||||||||
120 | } | - | ||||||||||||
121 | - | |||||||||||||
122 | /*! | - | ||||||||||||
123 | \class QLineEdit | - | ||||||||||||
124 | \brief The QLineEdit widget is a one-line text editor. | - | ||||||||||||
125 | - | |||||||||||||
126 | \ingroup basicwidgets | - | ||||||||||||
127 | \inmodule QtWidgets | - | ||||||||||||
128 | - | |||||||||||||
129 | A line edit allows the user to enter and edit a single line of | - | ||||||||||||
130 | plain text with a useful collection of editing functions, | - | ||||||||||||
131 | including undo and redo, cut and paste, and drag and drop (see | - | ||||||||||||
132 | \l setDragEnabled()). | - | ||||||||||||
133 | - | |||||||||||||
134 | By changing the echoMode() of a line edit, it can also be used as | - | ||||||||||||
135 | a "write-only" field, for inputs such as passwords. | - | ||||||||||||
136 | - | |||||||||||||
137 | The length of the text can be constrained to maxLength(). The text | - | ||||||||||||
138 | can be arbitrarily constrained using a validator() or an | - | ||||||||||||
139 | inputMask(), or both. When switching between a validator and an input mask | - | ||||||||||||
140 | on the same line edit, it is best to clear the validator or input mask to | - | ||||||||||||
141 | prevent undefined behavior. | - | ||||||||||||
142 | - | |||||||||||||
143 | - | |||||||||||||
144 | A related class is QTextEdit which allows multi-line, rich text | - | ||||||||||||
145 | editing. | - | ||||||||||||
146 | - | |||||||||||||
147 | You can change the text with setText() or insert(). The text is | - | ||||||||||||
148 | retrieved with text(); the displayed text (which may be different, | - | ||||||||||||
149 | see \l{EchoMode}) is retrieved with displayText(). Text can be | - | ||||||||||||
150 | selected with setSelection() or selectAll(), and the selection can | - | ||||||||||||
151 | be cut(), copy()ied and paste()d. The text can be aligned with | - | ||||||||||||
152 | setAlignment(). | - | ||||||||||||
153 | - | |||||||||||||
154 | When the text changes the textChanged() signal is emitted; when | - | ||||||||||||
155 | the text changes other than by calling setText() the textEdited() | - | ||||||||||||
156 | signal is emitted; when the cursor is moved the | - | ||||||||||||
157 | cursorPositionChanged() signal is emitted; and when the Return or | - | ||||||||||||
158 | Enter key is pressed the returnPressed() signal is emitted. | - | ||||||||||||
159 | - | |||||||||||||
160 | When editing is finished, either because the line edit lost focus | - | ||||||||||||
161 | or Return/Enter is pressed the editingFinished() signal is | - | ||||||||||||
162 | emitted. | - | ||||||||||||
163 | - | |||||||||||||
164 | Note that if there is a validator set on the line edit, the | - | ||||||||||||
165 | returnPressed()/editingFinished() signals will only be emitted if | - | ||||||||||||
166 | the validator returns QValidator::Acceptable. | - | ||||||||||||
167 | - | |||||||||||||
168 | By default, QLineEdits have a frame as specified by platform | - | ||||||||||||
169 | style guides; you can turn it off by calling | - | ||||||||||||
170 | setFrame(false). | - | ||||||||||||
171 | - | |||||||||||||
172 | The default key bindings are described below. The line edit also | - | ||||||||||||
173 | provides a context menu (usually invoked by a right mouse click) | - | ||||||||||||
174 | that presents some of these editing options. | - | ||||||||||||
175 | \target desc | - | ||||||||||||
176 | \table | - | ||||||||||||
177 | \header \li Keypress \li Action | - | ||||||||||||
178 | \row \li Left Arrow \li Moves the cursor one character to the left. | - | ||||||||||||
179 | \row \li Shift+Left Arrow \li Moves and selects text one character to the left. | - | ||||||||||||
180 | \row \li Right Arrow \li Moves the cursor one character to the right. | - | ||||||||||||
181 | \row \li Shift+Right Arrow \li Moves and selects text one character to the right. | - | ||||||||||||
182 | \row \li Home \li Moves the cursor to the beginning of the line. | - | ||||||||||||
183 | \row \li End \li Moves the cursor to the end of the line. | - | ||||||||||||
184 | \row \li Backspace \li Deletes the character to the left of the cursor. | - | ||||||||||||
185 | \row \li Ctrl+Backspace \li Deletes the word to the left of the cursor. | - | ||||||||||||
186 | \row \li Delete \li Deletes the character to the right of the cursor. | - | ||||||||||||
187 | \row \li Ctrl+Delete \li Deletes the word to the right of the cursor. | - | ||||||||||||
188 | \row \li Ctrl+A \li Select all. | - | ||||||||||||
189 | \row \li Ctrl+C \li Copies the selected text to the clipboard. | - | ||||||||||||
190 | \row \li Ctrl+Insert \li Copies the selected text to the clipboard. | - | ||||||||||||
191 | \row \li Ctrl+K \li Deletes to the end of the line. | - | ||||||||||||
192 | \row \li Ctrl+V \li Pastes the clipboard text into line edit. | - | ||||||||||||
193 | \row \li Shift+Insert \li Pastes the clipboard text into line edit. | - | ||||||||||||
194 | \row \li Ctrl+X \li Deletes the selected text and copies it to the clipboard. | - | ||||||||||||
195 | \row \li Shift+Delete \li Deletes the selected text and copies it to the clipboard. | - | ||||||||||||
196 | \row \li Ctrl+Z \li Undoes the last operation. | - | ||||||||||||
197 | \row \li Ctrl+Y \li Redoes the last undone operation. | - | ||||||||||||
198 | \endtable | - | ||||||||||||
199 | - | |||||||||||||
200 | Any other key sequence that represents a valid character, will | - | ||||||||||||
201 | cause the character to be inserted into the line edit. | - | ||||||||||||
202 | - | |||||||||||||
203 | \table 100% | - | ||||||||||||
204 | \row \li \inlineimage macintosh-lineedit.png Screenshot of a Macintosh style line edit | - | ||||||||||||
205 | \li A line edit shown in the \l{Macintosh Style Widget Gallery}{Macintosh widget style}. | - | ||||||||||||
206 | \row \li \inlineimage windowsvista-lineedit.png Screenshot of a Windows Vista style line edit | - | ||||||||||||
207 | \li A line edit shown in the \l{Windows Vista Style Widget Gallery}{Windows Vista widget style}. | - | ||||||||||||
208 | \row \li \inlineimage fusion-lineedit.png Screenshot of a Fusion style line edit | - | ||||||||||||
209 | \li A line edit shown in the \l{Fusion Style Widget Gallery}{Fusion widget style}. | - | ||||||||||||
210 | \endtable | - | ||||||||||||
211 | - | |||||||||||||
212 | \sa QTextEdit, QLabel, QComboBox, {fowler}{GUI Design Handbook: Field, Entry}, {Line Edits Example} | - | ||||||||||||
213 | */ | - | ||||||||||||
214 | - | |||||||||||||
215 | - | |||||||||||||
216 | /*! | - | ||||||||||||
217 | \fn void QLineEdit::textChanged(const QString &text) | - | ||||||||||||
218 | - | |||||||||||||
219 | This signal is emitted whenever the text changes. The \a text | - | ||||||||||||
220 | argument is the new text. | - | ||||||||||||
221 | - | |||||||||||||
222 | Unlike textEdited(), this signal is also emitted when the text is | - | ||||||||||||
223 | changed programmatically, for example, by calling setText(). | - | ||||||||||||
224 | */ | - | ||||||||||||
225 | - | |||||||||||||
226 | /*! | - | ||||||||||||
227 | \fn void QLineEdit::textEdited(const QString &text) | - | ||||||||||||
228 | - | |||||||||||||
229 | This signal is emitted whenever the text is edited. The \a text | - | ||||||||||||
230 | argument is the new text. | - | ||||||||||||
231 | - | |||||||||||||
232 | Unlike textChanged(), this signal is not emitted when the text is | - | ||||||||||||
233 | changed programmatically, for example, by calling setText(). | - | ||||||||||||
234 | */ | - | ||||||||||||
235 | - | |||||||||||||
236 | /*! | - | ||||||||||||
237 | \fn void QLineEdit::cursorPositionChanged(int old, int new) | - | ||||||||||||
238 | - | |||||||||||||
239 | This signal is emitted whenever the cursor moves. The previous | - | ||||||||||||
240 | position is given by \a old, and the new position by \a new. | - | ||||||||||||
241 | - | |||||||||||||
242 | \sa setCursorPosition(), cursorPosition() | - | ||||||||||||
243 | */ | - | ||||||||||||
244 | - | |||||||||||||
245 | /*! | - | ||||||||||||
246 | \fn void QLineEdit::selectionChanged() | - | ||||||||||||
247 | - | |||||||||||||
248 | This signal is emitted whenever the selection changes. | - | ||||||||||||
249 | - | |||||||||||||
250 | \sa hasSelectedText(), selectedText() | - | ||||||||||||
251 | */ | - | ||||||||||||
252 | - | |||||||||||||
253 | /*! | - | ||||||||||||
254 | Constructs a line edit with no text. | - | ||||||||||||
255 | - | |||||||||||||
256 | The maximum text length is set to 32767 characters. | - | ||||||||||||
257 | - | |||||||||||||
258 | The \a parent argument is sent to the QWidget constructor. | - | ||||||||||||
259 | - | |||||||||||||
260 | \sa setText(), setMaxLength() | - | ||||||||||||
261 | */ | - | ||||||||||||
262 | QLineEdit::QLineEdit(QWidget* parent) | - | ||||||||||||
263 | : QWidget(*new QLineEditPrivate, parent,0) | - | ||||||||||||
264 | { | - | ||||||||||||
265 | Q_D(QLineEdit); | - | ||||||||||||
266 | d->init(QString()); | - | ||||||||||||
267 | } | - | ||||||||||||
268 | - | |||||||||||||
269 | /*! | - | ||||||||||||
270 | Constructs a line edit containing the text \a contents. | - | ||||||||||||
271 | - | |||||||||||||
272 | The cursor position is set to the end of the line and the maximum | - | ||||||||||||
273 | text length to 32767 characters. | - | ||||||||||||
274 | - | |||||||||||||
275 | The \a parent and argument is sent to the QWidget | - | ||||||||||||
276 | constructor. | - | ||||||||||||
277 | - | |||||||||||||
278 | \sa text(), setMaxLength() | - | ||||||||||||
279 | */ | - | ||||||||||||
280 | QLineEdit::QLineEdit(const QString& contents, QWidget* parent) | - | ||||||||||||
281 | : QWidget(*new QLineEditPrivate, parent, 0) | - | ||||||||||||
282 | { | - | ||||||||||||
283 | Q_D(QLineEdit); | - | ||||||||||||
284 | d->init(contents); | - | ||||||||||||
285 | } | - | ||||||||||||
286 | - | |||||||||||||
287 | - | |||||||||||||
288 | - | |||||||||||||
289 | /*! | - | ||||||||||||
290 | Destroys the line edit. | - | ||||||||||||
291 | */ | - | ||||||||||||
292 | - | |||||||||||||
293 | QLineEdit::~QLineEdit() | - | ||||||||||||
294 | { | - | ||||||||||||
295 | } | - | ||||||||||||
296 | - | |||||||||||||
297 | - | |||||||||||||
298 | /*! | - | ||||||||||||
299 | \property QLineEdit::text | - | ||||||||||||
300 | \brief the line edit's text | - | ||||||||||||
301 | - | |||||||||||||
302 | Setting this property clears the selection, clears the undo/redo | - | ||||||||||||
303 | history, moves the cursor to the end of the line and resets the | - | ||||||||||||
304 | \l modified property to false. The text is not validated when | - | ||||||||||||
305 | inserted with setText(). | - | ||||||||||||
306 | - | |||||||||||||
307 | The text is truncated to maxLength() length. | - | ||||||||||||
308 | - | |||||||||||||
309 | By default, this property contains an empty string. | - | ||||||||||||
310 | - | |||||||||||||
311 | \sa insert(), clear() | - | ||||||||||||
312 | */ | - | ||||||||||||
313 | QString QLineEdit::text() const | - | ||||||||||||
314 | { | - | ||||||||||||
315 | Q_D(const QLineEdit); | - | ||||||||||||
316 | return d->control->text(); | - | ||||||||||||
317 | } | - | ||||||||||||
318 | - | |||||||||||||
319 | void QLineEdit::setText(const QString& text) | - | ||||||||||||
320 | { | - | ||||||||||||
321 | Q_D(QLineEdit); | - | ||||||||||||
322 | d->control->setText(text); | - | ||||||||||||
323 | } | - | ||||||||||||
324 | - | |||||||||||||
325 | /*! | - | ||||||||||||
326 | \since 4.7 | - | ||||||||||||
327 | - | |||||||||||||
328 | \property QLineEdit::placeholderText | - | ||||||||||||
329 | \brief the line edit's placeholder text | - | ||||||||||||
330 | - | |||||||||||||
331 | Setting this property makes the line edit display a grayed-out | - | ||||||||||||
332 | placeholder text as long as the line edit is empty. | - | ||||||||||||
333 | - | |||||||||||||
334 | Normally, an empty line edit shows the placeholder text even | - | ||||||||||||
335 | when it has focus. However, if the content is horizontally | - | ||||||||||||
336 | centered, the placeholder text is not displayed under | - | ||||||||||||
337 | the cursor when the line edit has focus. | - | ||||||||||||
338 | - | |||||||||||||
339 | By default, this property contains an empty string. | - | ||||||||||||
340 | - | |||||||||||||
341 | \sa text() | - | ||||||||||||
342 | */ | - | ||||||||||||
343 | QString QLineEdit::placeholderText() const | - | ||||||||||||
344 | { | - | ||||||||||||
345 | Q_D(const QLineEdit); | - | ||||||||||||
346 | return d->placeholderText; | - | ||||||||||||
347 | } | - | ||||||||||||
348 | - | |||||||||||||
349 | void QLineEdit::setPlaceholderText(const QString& placeholderText) | - | ||||||||||||
350 | { | - | ||||||||||||
351 | Q_D(QLineEdit); | - | ||||||||||||
352 | if (d->placeholderText != placeholderText) { | - | ||||||||||||
353 | d->placeholderText = placeholderText; | - | ||||||||||||
354 | if (d->shouldShowPlaceholderText()) | - | ||||||||||||
355 | update(); | - | ||||||||||||
356 | } | - | ||||||||||||
357 | } | - | ||||||||||||
358 | - | |||||||||||||
359 | /*! | - | ||||||||||||
360 | \property QLineEdit::displayText | - | ||||||||||||
361 | \brief the displayed text | - | ||||||||||||
362 | - | |||||||||||||
363 | If \l echoMode is \l Normal this returns the same as text(); if | - | ||||||||||||
364 | \l EchoMode is \l Password or \l PasswordEchoOnEdit it returns a string of | - | ||||||||||||
365 | platform-dependent password mask characters text().length() in size, | - | ||||||||||||
366 | e.g. "******"; if \l EchoMode is \l NoEcho returns an empty string, "". | - | ||||||||||||
367 | - | |||||||||||||
368 | By default, this property contains an empty string. | - | ||||||||||||
369 | - | |||||||||||||
370 | \sa setEchoMode(), text(), EchoMode | - | ||||||||||||
371 | */ | - | ||||||||||||
372 | - | |||||||||||||
373 | QString QLineEdit::displayText() const | - | ||||||||||||
374 | { | - | ||||||||||||
375 | Q_D(const QLineEdit); | - | ||||||||||||
376 | return d->control->displayText(); | - | ||||||||||||
377 | } | - | ||||||||||||
378 | - | |||||||||||||
379 | - | |||||||||||||
380 | /*! | - | ||||||||||||
381 | \property QLineEdit::maxLength | - | ||||||||||||
382 | \brief the maximum permitted length of the text | - | ||||||||||||
383 | - | |||||||||||||
384 | If the text is too long, it is truncated at the limit. | - | ||||||||||||
385 | - | |||||||||||||
386 | If truncation occurs any selected text will be unselected, the | - | ||||||||||||
387 | cursor position is set to 0 and the first part of the string is | - | ||||||||||||
388 | shown. | - | ||||||||||||
389 | - | |||||||||||||
390 | If the line edit has an input mask, the mask defines the maximum | - | ||||||||||||
391 | string length. | - | ||||||||||||
392 | - | |||||||||||||
393 | By default, this property contains a value of 32767. | - | ||||||||||||
394 | - | |||||||||||||
395 | \sa inputMask | - | ||||||||||||
396 | */ | - | ||||||||||||
397 | - | |||||||||||||
398 | int QLineEdit::maxLength() const | - | ||||||||||||
399 | { | - | ||||||||||||
400 | Q_D(const QLineEdit); | - | ||||||||||||
401 | return d->control->maxLength(); | - | ||||||||||||
402 | } | - | ||||||||||||
403 | - | |||||||||||||
404 | void QLineEdit::setMaxLength(int maxLength) | - | ||||||||||||
405 | { | - | ||||||||||||
406 | Q_D(QLineEdit); | - | ||||||||||||
407 | d->control->setMaxLength(maxLength); | - | ||||||||||||
408 | } | - | ||||||||||||
409 | - | |||||||||||||
410 | /*! | - | ||||||||||||
411 | \property QLineEdit::frame | - | ||||||||||||
412 | \brief whether the line edit draws itself with a frame | - | ||||||||||||
413 | - | |||||||||||||
414 | If enabled (the default) the line edit draws itself inside a | - | ||||||||||||
415 | frame, otherwise the line edit draws itself without any frame. | - | ||||||||||||
416 | */ | - | ||||||||||||
417 | bool QLineEdit::hasFrame() const | - | ||||||||||||
418 | { | - | ||||||||||||
419 | Q_D(const QLineEdit); | - | ||||||||||||
420 | return d->frame; | - | ||||||||||||
421 | } | - | ||||||||||||
422 | - | |||||||||||||
423 | /*! | - | ||||||||||||
424 | \enum QLineEdit::ActionPosition | - | ||||||||||||
425 | - | |||||||||||||
426 | This enum type describes how a line edit should display the action widgets to be | - | ||||||||||||
427 | added. | - | ||||||||||||
428 | - | |||||||||||||
429 | \value LeadingPosition The widget is displayed to the left of the text | - | ||||||||||||
430 | when using layout direction \c Qt::LeftToRight or to | - | ||||||||||||
431 | the right when using \c Qt::RightToLeft, respectively. | - | ||||||||||||
432 | - | |||||||||||||
433 | \value TrailingPosition The widget is displayed to the right of the text | - | ||||||||||||
434 | when using layout direction \c Qt::LeftToRight or to | - | ||||||||||||
435 | the left when using \c Qt::RightToLeft, respectively. | - | ||||||||||||
436 | - | |||||||||||||
437 | \sa addAction(), removeAction(), QWidget::layoutDirection | - | ||||||||||||
438 | - | |||||||||||||
439 | \since 5.2 | - | ||||||||||||
440 | */ | - | ||||||||||||
441 | - | |||||||||||||
442 | /*! | - | ||||||||||||
443 | \overload | - | ||||||||||||
444 | - | |||||||||||||
445 | Adds the \a action to the list of actions at the \a position. | - | ||||||||||||
446 | - | |||||||||||||
447 | \since 5.2 | - | ||||||||||||
448 | */ | - | ||||||||||||
449 | - | |||||||||||||
450 | void QLineEdit::addAction(QAction *action, ActionPosition position) | - | ||||||||||||
451 | { | - | ||||||||||||
452 | Q_D(QLineEdit); | - | ||||||||||||
453 | QWidget::addAction(action); | - | ||||||||||||
454 | d->addAction(action, 0, position); | - | ||||||||||||
455 | } | - | ||||||||||||
456 | - | |||||||||||||
457 | /*! | - | ||||||||||||
458 | \overload | - | ||||||||||||
459 | - | |||||||||||||
460 | Creates a new action with the given \a icon at the \a position. | - | ||||||||||||
461 | - | |||||||||||||
462 | \since 5.2 | - | ||||||||||||
463 | */ | - | ||||||||||||
464 | - | |||||||||||||
465 | QAction *QLineEdit::addAction(const QIcon &icon, ActionPosition position) | - | ||||||||||||
466 | { | - | ||||||||||||
467 | QAction *result = new QAction(icon, QString(), this); | - | ||||||||||||
468 | addAction(result, position); | - | ||||||||||||
469 | return result; | - | ||||||||||||
470 | } | - | ||||||||||||
471 | - | |||||||||||||
472 | /*! | - | ||||||||||||
473 | \property QLineEdit::clearButtonEnabled | - | ||||||||||||
474 | \brief Whether the line edit displays a clear button when it is not empty. | - | ||||||||||||
475 | - | |||||||||||||
476 | If enabled, the line edit displays a trailing \e clear button when it contains | - | ||||||||||||
477 | some text, otherwise the line edit does not show a clear button (the | - | ||||||||||||
478 | default). | - | ||||||||||||
479 | - | |||||||||||||
480 | \sa addAction(), removeAction() | - | ||||||||||||
481 | \since 5.2 | - | ||||||||||||
482 | */ | - | ||||||||||||
483 | - | |||||||||||||
484 | static const char clearButtonActionNameC[] = "_q_qlineeditclearaction"; | - | ||||||||||||
485 | - | |||||||||||||
486 | void QLineEdit::setClearButtonEnabled(bool enable) | - | ||||||||||||
487 | { | - | ||||||||||||
488 | Q_D(QLineEdit); | - | ||||||||||||
489 | if (enable == isClearButtonEnabled()) | - | ||||||||||||
490 | return; | - | ||||||||||||
491 | if (enable) { | - | ||||||||||||
492 | QAction *clearAction = new QAction(d->clearButtonIcon(), QString(), this); | - | ||||||||||||
493 | clearAction->setEnabled(!isReadOnly()); | - | ||||||||||||
494 | clearAction->setObjectName(QLatin1String(clearButtonActionNameC)); | - | ||||||||||||
495 | d->addAction(clearAction, 0, QLineEdit::TrailingPosition, QLineEditPrivate::SideWidgetClearButton | QLineEditPrivate::SideWidgetFadeInWithText); | - | ||||||||||||
496 | } else { | - | ||||||||||||
497 | QAction *clearAction = findChild<QAction *>(QLatin1String(clearButtonActionNameC)); | - | ||||||||||||
498 | Q_ASSERT(clearAction); | - | ||||||||||||
499 | d->removeAction(clearAction); | - | ||||||||||||
500 | delete clearAction; | - | ||||||||||||
501 | } | - | ||||||||||||
502 | } | - | ||||||||||||
503 | - | |||||||||||||
504 | bool QLineEdit::isClearButtonEnabled() const | - | ||||||||||||
505 | { | - | ||||||||||||
506 | return findChild<QAction *>(QLatin1String(clearButtonActionNameC)); | - | ||||||||||||
507 | } | - | ||||||||||||
508 | - | |||||||||||||
509 | void QLineEdit::setFrame(bool enable) | - | ||||||||||||
510 | { | - | ||||||||||||
511 | Q_D(QLineEdit); | - | ||||||||||||
512 | d->frame = enable; | - | ||||||||||||
513 | update(); | - | ||||||||||||
514 | updateGeometry(); | - | ||||||||||||
515 | } | - | ||||||||||||
516 | - | |||||||||||||
517 | - | |||||||||||||
518 | /*! | - | ||||||||||||
519 | \enum QLineEdit::EchoMode | - | ||||||||||||
520 | - | |||||||||||||
521 | This enum type describes how a line edit should display its | - | ||||||||||||
522 | contents. | - | ||||||||||||
523 | - | |||||||||||||
524 | \value Normal Display characters as they are entered. This is the | - | ||||||||||||
525 | default. | - | ||||||||||||
526 | \value NoEcho Do not display anything. This may be appropriate | - | ||||||||||||
527 | for passwords where even the length of the | - | ||||||||||||
528 | password should be kept secret. | - | ||||||||||||
529 | \value Password Display platform-dependent password mask characters instead | - | ||||||||||||
530 | of the characters actually entered. | - | ||||||||||||
531 | \value PasswordEchoOnEdit Display characters as they are entered | - | ||||||||||||
532 | while editing otherwise display characters as with | - | ||||||||||||
533 | \c Password. | - | ||||||||||||
534 | - | |||||||||||||
535 | \sa setEchoMode(), echoMode() | - | ||||||||||||
536 | */ | - | ||||||||||||
537 | - | |||||||||||||
538 | - | |||||||||||||
539 | /*! | - | ||||||||||||
540 | \property QLineEdit::echoMode | - | ||||||||||||
541 | \brief the line edit's echo mode | - | ||||||||||||
542 | - | |||||||||||||
543 | The echo mode determines how the text entered in the line edit is | - | ||||||||||||
544 | displayed (or echoed) to the user. | - | ||||||||||||
545 | - | |||||||||||||
546 | The most common setting is \l Normal, in which the text entered by the | - | ||||||||||||
547 | user is displayed verbatim, but QLineEdit also supports modes that allow | - | ||||||||||||
548 | the entered text to be suppressed or obscured: these include \l NoEcho, | - | ||||||||||||
549 | \l Password and \l PasswordEchoOnEdit. | - | ||||||||||||
550 | - | |||||||||||||
551 | The widget's display and the ability to copy or drag the text is | - | ||||||||||||
552 | affected by this setting. | - | ||||||||||||
553 | - | |||||||||||||
554 | By default, this property is set to \l Normal. | - | ||||||||||||
555 | - | |||||||||||||
556 | \sa EchoMode, displayText() | - | ||||||||||||
557 | */ | - | ||||||||||||
558 | - | |||||||||||||
559 | QLineEdit::EchoMode QLineEdit::echoMode() const | - | ||||||||||||
560 | { | - | ||||||||||||
561 | Q_D(const QLineEdit); | - | ||||||||||||
562 | return (EchoMode) d->control->echoMode(); | - | ||||||||||||
563 | } | - | ||||||||||||
564 | - | |||||||||||||
565 | void QLineEdit::setEchoMode(EchoMode mode) | - | ||||||||||||
566 | { | - | ||||||||||||
567 | Q_D(QLineEdit); | - | ||||||||||||
568 | if (mode == (EchoMode)d->control->echoMode())
| 0 | ||||||||||||
569 | return; never executed: return; | 0 | ||||||||||||
570 | Qt::InputMethodHints imHints = inputMethodHints(); | - | ||||||||||||
571 | ifimHints.setFlag(Qt::ImhHiddenText, mode == Password || mode == NoEcho) { | - | ||||||||||||
imHints |= Qt::ImhHiddenText; | ||||||||||||||
} else {); | ||||||||||||||
572 | imHints&= ~.setFlag(Qt::ImhHiddenText; | - | ||||||||||||
} | ||||||||||||||
if (ImhNoAutoUppercase, mode != Normal) {); | ||||||||||||||
573 | imHints|=.setFlag(Qt::ImhNoAutoUppercase |Qt::ImhNoPredictiveText| Qt::ImhSensitiveData, mode != Normal); | - | ||||||||||||
574 | } else {imHints&= ~(Qt::ImhNoAutoUppercase | Qt::ImhNoPredictiveText |.setFlag(Qt::ImhSensitiveData, mode != Normal); | - | ||||||||||||
575 | }setInputMethodHints(imHints); | - | ||||||||||||
576 | d->control->setEchoMode(mode); | - | ||||||||||||
577 | update(); | - | ||||||||||||
578 | #ifdef Q_DEAD_CODE_FROM_QT4_MAC | - | ||||||||||||
579 | if (hasFocus()) | - | ||||||||||||
580 | qt_mac_secure_keyboard(mode == Password || mode == NoEcho); | - | ||||||||||||
581 | #endif | - | ||||||||||||
582 | } never executed: end of block | 0 | ||||||||||||
583 | - | |||||||||||||
584 | - | |||||||||||||
585 | #ifndef QT_NO_VALIDATOR | - | ||||||||||||
586 | /*! | - | ||||||||||||
587 | Returns a pointer to the current input validator, or 0 if no | - | ||||||||||||
588 | validator has been set. | - | ||||||||||||
589 | - | |||||||||||||
590 | \sa setValidator() | - | ||||||||||||
591 | */ | - | ||||||||||||
592 | - | |||||||||||||
593 | const QValidator * QLineEdit::validator() const | - | ||||||||||||
594 | { | - | ||||||||||||
595 | Q_D(const QLineEdit); | - | ||||||||||||
596 | return d->control->validator(); | - | ||||||||||||
597 | } | - | ||||||||||||
598 | - | |||||||||||||
599 | /*! | - | ||||||||||||
600 | Sets this line edit to only accept input that the validator, \a v, | - | ||||||||||||
601 | will accept. This allows you to place any arbitrary constraints on | - | ||||||||||||
602 | the text which may be entered. | - | ||||||||||||
603 | - | |||||||||||||
604 | If \a v == 0, setValidator() removes the current input validator. | - | ||||||||||||
605 | The initial setting is to have no input validator (i.e. any input | - | ||||||||||||
606 | is accepted up to maxLength()). | - | ||||||||||||
607 | - | |||||||||||||
608 | \sa validator(), hasAcceptableInput(), QIntValidator, QDoubleValidator, QRegExpValidator | - | ||||||||||||
609 | */ | - | ||||||||||||
610 | - | |||||||||||||
611 | void QLineEdit::setValidator(const QValidator *v) | - | ||||||||||||
612 | { | - | ||||||||||||
613 | Q_D(QLineEdit); | - | ||||||||||||
614 | d->control->setValidator(v); | - | ||||||||||||
615 | } | - | ||||||||||||
616 | #endif // QT_NO_VALIDATOR | - | ||||||||||||
617 | - | |||||||||||||
618 | #ifndef QT_NO_COMPLETER | - | ||||||||||||
619 | /*! | - | ||||||||||||
620 | \since 4.2 | - | ||||||||||||
621 | - | |||||||||||||
622 | Sets this line edit to provide auto completions from the completer, \a c. | - | ||||||||||||
623 | The completion mode is set using QCompleter::setCompletionMode(). | - | ||||||||||||
624 | - | |||||||||||||
625 | To use a QCompleter with a QValidator or QLineEdit::inputMask, you need to | - | ||||||||||||
626 | ensure that the model provided to QCompleter contains valid entries. You can | - | ||||||||||||
627 | use the QSortFilterProxyModel to ensure that the QCompleter's model contains | - | ||||||||||||
628 | only valid entries. | - | ||||||||||||
629 | - | |||||||||||||
630 | If \a c == 0, setCompleter() removes the current completer, effectively | - | ||||||||||||
631 | disabling auto completion. | - | ||||||||||||
632 | - | |||||||||||||
633 | \sa QCompleter | - | ||||||||||||
634 | */ | - | ||||||||||||
635 | void QLineEdit::setCompleter(QCompleter *c) | - | ||||||||||||
636 | { | - | ||||||||||||
637 | Q_D(QLineEdit); | - | ||||||||||||
638 | if (c == d->control->completer()) | - | ||||||||||||
639 | return; | - | ||||||||||||
640 | if (d->control->completer()) { | - | ||||||||||||
641 | disconnect(d->control->completer(), 0, this, 0); | - | ||||||||||||
642 | d->control->completer()->setWidget(0); | - | ||||||||||||
643 | if (d->control->completer()->parent() == this) | - | ||||||||||||
644 | delete d->control->completer(); | - | ||||||||||||
645 | } | - | ||||||||||||
646 | d->control->setCompleter(c); | - | ||||||||||||
647 | if (!c) | - | ||||||||||||
648 | return; | - | ||||||||||||
649 | if (c->widget() == 0) | - | ||||||||||||
650 | c->setWidget(this); | - | ||||||||||||
651 | if (hasFocus()) { | - | ||||||||||||
652 | QObject::connect(d->control->completer(), SIGNAL(activated(QString)), | - | ||||||||||||
653 | this, SLOT(setText(QString))); | - | ||||||||||||
654 | QObject::connect(d->control->completer(), SIGNAL(highlighted(QString)), | - | ||||||||||||
655 | this, SLOT(_q_completionHighlighted(QString))); | - | ||||||||||||
656 | } | - | ||||||||||||
657 | } | - | ||||||||||||
658 | - | |||||||||||||
659 | /*! | - | ||||||||||||
660 | \since 4.2 | - | ||||||||||||
661 | - | |||||||||||||
662 | Returns the current QCompleter that provides completions. | - | ||||||||||||
663 | */ | - | ||||||||||||
664 | QCompleter *QLineEdit::completer() const | - | ||||||||||||
665 | { | - | ||||||||||||
666 | Q_D(const QLineEdit); | - | ||||||||||||
667 | return d->control->completer(); | - | ||||||||||||
668 | } | - | ||||||||||||
669 | - | |||||||||||||
670 | #endif // QT_NO_COMPLETER | - | ||||||||||||
671 | - | |||||||||||||
672 | /*! | - | ||||||||||||
673 | Returns a recommended size for the widget. | - | ||||||||||||
674 | - | |||||||||||||
675 | The width returned, in pixels, is usually enough for about 15 to | - | ||||||||||||
676 | 20 characters. | - | ||||||||||||
677 | */ | - | ||||||||||||
678 | - | |||||||||||||
679 | QSize QLineEdit::sizeHint() const | - | ||||||||||||
680 | { | - | ||||||||||||
681 | Q_D(const QLineEdit); | - | ||||||||||||
682 | ensurePolished(); | - | ||||||||||||
683 | QFontMetrics fm(font()); | - | ||||||||||||
684 | int h = qMax(fm.height(), 14) + 2*d->verticalMargin | - | ||||||||||||
685 | + d->topTextMargin + d->bottomTextMargin | - | ||||||||||||
686 | + d->topmargin + d->bottommargin; | - | ||||||||||||
687 | int w = fm.width(QLatin1Char('x')) * 17 + 2*d->horizontalMargin | - | ||||||||||||
688 | + d->effectiveLeftTextMargin() + d->effectiveRightTextMargin() | - | ||||||||||||
689 | + d->leftmargin + d->rightmargin; // "some" | - | ||||||||||||
690 | QStyleOptionFrame opt; | - | ||||||||||||
691 | initStyleOption(&opt); | - | ||||||||||||
692 | return (style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(w, h). | - | ||||||||||||
693 | expandedTo(QApplication::globalStrut()), this)); | - | ||||||||||||
694 | } | - | ||||||||||||
695 | - | |||||||||||||
696 | - | |||||||||||||
697 | /*! | - | ||||||||||||
698 | Returns a minimum size for the line edit. | - | ||||||||||||
699 | - | |||||||||||||
700 | The width returned is enough for at least one character. | - | ||||||||||||
701 | */ | - | ||||||||||||
702 | - | |||||||||||||
703 | QSize QLineEdit::minimumSizeHint() const | - | ||||||||||||
704 | { | - | ||||||||||||
705 | Q_D(const QLineEdit); | - | ||||||||||||
706 | ensurePolished(); | - | ||||||||||||
707 | QFontMetrics fm = fontMetrics(); | - | ||||||||||||
708 | int h = fm.height() + qMax(2*d->verticalMargin, fm.leading()) | - | ||||||||||||
709 | + d->topTextMargin + d->bottomTextMargin | - | ||||||||||||
710 | + d->topmargin + d->bottommargin; | - | ||||||||||||
711 | int w = fm.maxWidth() | - | ||||||||||||
712 | + d->effectiveLeftTextMargin() + d->effectiveRightTextMargin() | - | ||||||||||||
713 | + d->leftmargin + d->rightmargin; | - | ||||||||||||
714 | QStyleOptionFrame opt; | - | ||||||||||||
715 | initStyleOption(&opt); | - | ||||||||||||
716 | return (style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(w, h). | - | ||||||||||||
717 | expandedTo(QApplication::globalStrut()), this)); | - | ||||||||||||
718 | } | - | ||||||||||||
719 | - | |||||||||||||
720 | - | |||||||||||||
721 | /*! | - | ||||||||||||
722 | \property QLineEdit::cursorPosition | - | ||||||||||||
723 | \brief the current cursor position for this line edit | - | ||||||||||||
724 | - | |||||||||||||
725 | Setting the cursor position causes a repaint when appropriate. | - | ||||||||||||
726 | - | |||||||||||||
727 | By default, this property contains a value of 0. | - | ||||||||||||
728 | */ | - | ||||||||||||
729 | - | |||||||||||||
730 | int QLineEdit::cursorPosition() const | - | ||||||||||||
731 | { | - | ||||||||||||
732 | Q_D(const QLineEdit); | - | ||||||||||||
733 | return d->control->cursorPosition(); | - | ||||||||||||
734 | } | - | ||||||||||||
735 | - | |||||||||||||
736 | void QLineEdit::setCursorPosition(int pos) | - | ||||||||||||
737 | { | - | ||||||||||||
738 | Q_D(QLineEdit); | - | ||||||||||||
739 | d->control->setCursorPosition(pos); | - | ||||||||||||
740 | } | - | ||||||||||||
741 | - | |||||||||||||
742 | /*! | - | ||||||||||||
743 | Returns the cursor position under the point \a pos. | - | ||||||||||||
744 | */ | - | ||||||||||||
745 | // ### What should this do if the point is outside of contentsRect? Currently returns 0. | - | ||||||||||||
746 | int QLineEdit::cursorPositionAt(const QPoint &pos) | - | ||||||||||||
747 | { | - | ||||||||||||
748 | Q_D(QLineEdit); | - | ||||||||||||
749 | return d->xToPos(pos.x()); | - | ||||||||||||
750 | } | - | ||||||||||||
751 | - | |||||||||||||
752 | - | |||||||||||||
753 | - | |||||||||||||
754 | /*! | - | ||||||||||||
755 | \property QLineEdit::alignment | - | ||||||||||||
756 | \brief the alignment of the line edit | - | ||||||||||||
757 | - | |||||||||||||
758 | Both horizontal and vertical alignment is allowed here, Qt::AlignJustify | - | ||||||||||||
759 | will map to Qt::AlignLeft. | - | ||||||||||||
760 | - | |||||||||||||
761 | By default, this property contains a combination of Qt::AlignLeft and Qt::AlignVCenter. | - | ||||||||||||
762 | - | |||||||||||||
763 | \sa Qt::Alignment | - | ||||||||||||
764 | */ | - | ||||||||||||
765 | - | |||||||||||||
766 | Qt::Alignment QLineEdit::alignment() const | - | ||||||||||||
767 | { | - | ||||||||||||
768 | Q_D(const QLineEdit); | - | ||||||||||||
769 | return QFlag(d->alignment); | - | ||||||||||||
770 | } | - | ||||||||||||
771 | - | |||||||||||||
772 | void QLineEdit::setAlignment(Qt::Alignment alignment) | - | ||||||||||||
773 | { | - | ||||||||||||
774 | Q_D(QLineEdit); | - | ||||||||||||
775 | d->alignment = alignment; | - | ||||||||||||
776 | update(); | - | ||||||||||||
777 | } | - | ||||||||||||
778 | - | |||||||||||||
779 | - | |||||||||||||
780 | /*! | - | ||||||||||||
781 | Moves the cursor forward \a steps characters. If \a mark is true | - | ||||||||||||
782 | each character moved over is added to the selection; if \a mark is | - | ||||||||||||
783 | false the selection is cleared. | - | ||||||||||||
784 | - | |||||||||||||
785 | \sa cursorBackward() | - | ||||||||||||
786 | */ | - | ||||||||||||
787 | - | |||||||||||||
788 | void QLineEdit::cursorForward(bool mark, int steps) | - | ||||||||||||
789 | { | - | ||||||||||||
790 | Q_D(QLineEdit); | - | ||||||||||||
791 | d->control->cursorForward(mark, steps); | - | ||||||||||||
792 | } | - | ||||||||||||
793 | - | |||||||||||||
794 | - | |||||||||||||
795 | /*! | - | ||||||||||||
796 | Moves the cursor back \a steps characters. If \a mark is true each | - | ||||||||||||
797 | character moved over is added to the selection; if \a mark is | - | ||||||||||||
798 | false the selection is cleared. | - | ||||||||||||
799 | - | |||||||||||||
800 | \sa cursorForward() | - | ||||||||||||
801 | */ | - | ||||||||||||
802 | void QLineEdit::cursorBackward(bool mark, int steps) | - | ||||||||||||
803 | { | - | ||||||||||||
804 | cursorForward(mark, -steps); | - | ||||||||||||
805 | } | - | ||||||||||||
806 | - | |||||||||||||
807 | /*! | - | ||||||||||||
808 | Moves the cursor one word forward. If \a mark is true, the word is | - | ||||||||||||
809 | also selected. | - | ||||||||||||
810 | - | |||||||||||||
811 | \sa cursorWordBackward() | - | ||||||||||||
812 | */ | - | ||||||||||||
813 | void QLineEdit::cursorWordForward(bool mark) | - | ||||||||||||
814 | { | - | ||||||||||||
815 | Q_D(QLineEdit); | - | ||||||||||||
816 | d->control->cursorWordForward(mark); | - | ||||||||||||
817 | } | - | ||||||||||||
818 | - | |||||||||||||
819 | /*! | - | ||||||||||||
820 | Moves the cursor one word backward. If \a mark is true, the word | - | ||||||||||||
821 | is also selected. | - | ||||||||||||
822 | - | |||||||||||||
823 | \sa cursorWordForward() | - | ||||||||||||
824 | */ | - | ||||||||||||
825 | - | |||||||||||||
826 | void QLineEdit::cursorWordBackward(bool mark) | - | ||||||||||||
827 | { | - | ||||||||||||
828 | Q_D(QLineEdit); | - | ||||||||||||
829 | d->control->cursorWordBackward(mark); | - | ||||||||||||
830 | } | - | ||||||||||||
831 | - | |||||||||||||
832 | - | |||||||||||||
833 | /*! | - | ||||||||||||
834 | If no text is selected, deletes the character to the left of the | - | ||||||||||||
835 | text cursor and moves the cursor one position to the left. If any | - | ||||||||||||
836 | text is selected, the cursor is moved to the beginning of the | - | ||||||||||||
837 | selected text and the selected text is deleted. | - | ||||||||||||
838 | - | |||||||||||||
839 | \sa del() | - | ||||||||||||
840 | */ | - | ||||||||||||
841 | void QLineEdit::backspace() | - | ||||||||||||
842 | { | - | ||||||||||||
843 | Q_D(QLineEdit); | - | ||||||||||||
844 | d->control->backspace(); | - | ||||||||||||
845 | } | - | ||||||||||||
846 | - | |||||||||||||
847 | /*! | - | ||||||||||||
848 | If no text is selected, deletes the character to the right of the | - | ||||||||||||
849 | text cursor. If any text is selected, the cursor is moved to the | - | ||||||||||||
850 | beginning of the selected text and the selected text is deleted. | - | ||||||||||||
851 | - | |||||||||||||
852 | \sa backspace() | - | ||||||||||||
853 | */ | - | ||||||||||||
854 | - | |||||||||||||
855 | void QLineEdit::del() | - | ||||||||||||
856 | { | - | ||||||||||||
857 | Q_D(QLineEdit); | - | ||||||||||||
858 | d->control->del(); | - | ||||||||||||
859 | } | - | ||||||||||||
860 | - | |||||||||||||
861 | /*! | - | ||||||||||||
862 | Moves the text cursor to the beginning of the line unless it is | - | ||||||||||||
863 | already there. If \a mark is true, text is selected towards the | - | ||||||||||||
864 | first position; otherwise, any selected text is unselected if the | - | ||||||||||||
865 | cursor is moved. | - | ||||||||||||
866 | - | |||||||||||||
867 | \sa end() | - | ||||||||||||
868 | */ | - | ||||||||||||
869 | - | |||||||||||||
870 | void QLineEdit::home(bool mark) | - | ||||||||||||
871 | { | - | ||||||||||||
872 | Q_D(QLineEdit); | - | ||||||||||||
873 | d->control->home(mark); | - | ||||||||||||
874 | } | - | ||||||||||||
875 | - | |||||||||||||
876 | /*! | - | ||||||||||||
877 | Moves the text cursor to the end of the line unless it is already | - | ||||||||||||
878 | there. If \a mark is true, text is selected towards the last | - | ||||||||||||
879 | position; otherwise, any selected text is unselected if the cursor | - | ||||||||||||
880 | is moved. | - | ||||||||||||
881 | - | |||||||||||||
882 | \sa home() | - | ||||||||||||
883 | */ | - | ||||||||||||
884 | - | |||||||||||||
885 | void QLineEdit::end(bool mark) | - | ||||||||||||
886 | { | - | ||||||||||||
887 | Q_D(QLineEdit); | - | ||||||||||||
888 | d->control->end(mark); | - | ||||||||||||
889 | } | - | ||||||||||||
890 | - | |||||||||||||
891 | - | |||||||||||||
892 | /*! | - | ||||||||||||
893 | \property QLineEdit::modified | - | ||||||||||||
894 | \brief whether the line edit's contents has been modified by the user | - | ||||||||||||
895 | - | |||||||||||||
896 | The modified flag is never read by QLineEdit; it has a default value | - | ||||||||||||
897 | of false and is changed to true whenever the user changes the line | - | ||||||||||||
898 | edit's contents. | - | ||||||||||||
899 | - | |||||||||||||
900 | This is useful for things that need to provide a default value but | - | ||||||||||||
901 | do not start out knowing what the default should be (perhaps it | - | ||||||||||||
902 | depends on other fields on the form). Start the line edit without | - | ||||||||||||
903 | the best default, and when the default is known, if modified() | - | ||||||||||||
904 | returns \c false (the user hasn't entered any text), insert the | - | ||||||||||||
905 | default value. | - | ||||||||||||
906 | - | |||||||||||||
907 | Calling setText() resets the modified flag to false. | - | ||||||||||||
908 | */ | - | ||||||||||||
909 | - | |||||||||||||
910 | bool QLineEdit::isModified() const | - | ||||||||||||
911 | { | - | ||||||||||||
912 | Q_D(const QLineEdit); | - | ||||||||||||
913 | return d->control->isModified(); | - | ||||||||||||
914 | } | - | ||||||||||||
915 | - | |||||||||||||
916 | void QLineEdit::setModified(bool modified) | - | ||||||||||||
917 | { | - | ||||||||||||
918 | Q_D(QLineEdit); | - | ||||||||||||
919 | d->control->setModified(modified); | - | ||||||||||||
920 | } | - | ||||||||||||
921 | - | |||||||||||||
922 | /*! | - | ||||||||||||
923 | \property QLineEdit::hasSelectedText | - | ||||||||||||
924 | \brief whether there is any text selected | - | ||||||||||||
925 | - | |||||||||||||
926 | hasSelectedText() returns \c true if some or all of the text has been | - | ||||||||||||
927 | selected by the user; otherwise returns \c false. | - | ||||||||||||
928 | - | |||||||||||||
929 | By default, this property is \c false. | - | ||||||||||||
930 | - | |||||||||||||
931 | \sa selectedText() | - | ||||||||||||
932 | */ | - | ||||||||||||
933 | - | |||||||||||||
934 | - | |||||||||||||
935 | bool QLineEdit::hasSelectedText() const | - | ||||||||||||
936 | { | - | ||||||||||||
937 | Q_D(const QLineEdit); | - | ||||||||||||
938 | return d->control->hasSelectedText(); | - | ||||||||||||
939 | } | - | ||||||||||||
940 | - | |||||||||||||
941 | /*! | - | ||||||||||||
942 | \property QLineEdit::selectedText | - | ||||||||||||
943 | \brief the selected text | - | ||||||||||||
944 | - | |||||||||||||
945 | If there is no selected text this property's value is | - | ||||||||||||
946 | an empty string. | - | ||||||||||||
947 | - | |||||||||||||
948 | By default, this property contains an empty string. | - | ||||||||||||
949 | - | |||||||||||||
950 | \sa hasSelectedText() | - | ||||||||||||
951 | */ | - | ||||||||||||
952 | - | |||||||||||||
953 | QString QLineEdit::selectedText() const | - | ||||||||||||
954 | { | - | ||||||||||||
955 | Q_D(const QLineEdit); | - | ||||||||||||
956 | return d->control->selectedText(); | - | ||||||||||||
957 | } | - | ||||||||||||
958 | - | |||||||||||||
959 | /*! | - | ||||||||||||
960 | selectionStart() returns the index of the first selected character in the | - | ||||||||||||
961 | line edit or -1 if no text is selected. | - | ||||||||||||
962 | - | |||||||||||||
963 | \sa selectedText() | - | ||||||||||||
964 | */ | - | ||||||||||||
965 | - | |||||||||||||
966 | int QLineEdit::selectionStart() const | - | ||||||||||||
967 | { | - | ||||||||||||
968 | Q_D(const QLineEdit); | - | ||||||||||||
969 | return d->control->selectionStart(); | - | ||||||||||||
970 | } | - | ||||||||||||
971 | - | |||||||||||||
972 | - | |||||||||||||
973 | - | |||||||||||||
974 | - | |||||||||||||
975 | /*! | - | ||||||||||||
976 | Selects text from position \a start and for \a length characters. | - | ||||||||||||
977 | Negative lengths are allowed. | - | ||||||||||||
978 | - | |||||||||||||
979 | \sa deselect(), selectAll(), selectedText() | - | ||||||||||||
980 | */ | - | ||||||||||||
981 | - | |||||||||||||
982 | void QLineEdit::setSelection(int start, int length) | - | ||||||||||||
983 | { | - | ||||||||||||
984 | Q_D(QLineEdit); | - | ||||||||||||
985 | if (Q_UNLIKELY(start < 0 || start > (int)d->control->end())())) {
| 0 | ||||||||||||
986 | qWarning("QLineEdit::setSelection: Invalid start position (%d)", start); | - | ||||||||||||
987 | return; never executed: return; | 0 | ||||||||||||
988 | } | - | ||||||||||||
989 | - | |||||||||||||
990 | d->control->setSelection(start, length); | - | ||||||||||||
991 | - | |||||||||||||
992 | if (d->control->hasSelectedText()){
| 0 | ||||||||||||
993 | QStyleOptionFrame opt; | - | ||||||||||||
994 | initStyleOption(&opt); | - | ||||||||||||
995 | if (!style()->styleHint(QStyle::SH_BlinkCursorWhenTextSelected, &opt, this))
| 0 | ||||||||||||
996 | d->setCursorVisible(false); never executed: d->setCursorVisible(false); | 0 | ||||||||||||
997 | } never executed: end of block | 0 | ||||||||||||
998 | } never executed: end of block | 0 | ||||||||||||
999 | - | |||||||||||||
1000 | - | |||||||||||||
1001 | /*! | - | ||||||||||||
1002 | \property QLineEdit::undoAvailable | - | ||||||||||||
1003 | \brief whether undo is available | - | ||||||||||||
1004 | - | |||||||||||||
1005 | Undo becomes available once the user has modified the text in the line edit. | - | ||||||||||||
1006 | - | |||||||||||||
1007 | By default, this property is \c false. | - | ||||||||||||
1008 | */ | - | ||||||||||||
1009 | - | |||||||||||||
1010 | bool QLineEdit::isUndoAvailable() const | - | ||||||||||||
1011 | { | - | ||||||||||||
1012 | Q_D(const QLineEdit); | - | ||||||||||||
1013 | return d->control->isUndoAvailable(); | - | ||||||||||||
1014 | } | - | ||||||||||||
1015 | - | |||||||||||||
1016 | /*! | - | ||||||||||||
1017 | \property QLineEdit::redoAvailable | - | ||||||||||||
1018 | \brief whether redo is available | - | ||||||||||||
1019 | - | |||||||||||||
1020 | Redo becomes available once the user has performed one or more undo operations | - | ||||||||||||
1021 | on text in the line edit. | - | ||||||||||||
1022 | - | |||||||||||||
1023 | By default, this property is \c false. | - | ||||||||||||
1024 | */ | - | ||||||||||||
1025 | - | |||||||||||||
1026 | bool QLineEdit::isRedoAvailable() const | - | ||||||||||||
1027 | { | - | ||||||||||||
1028 | Q_D(const QLineEdit); | - | ||||||||||||
1029 | return d->control->isRedoAvailable(); | - | ||||||||||||
1030 | } | - | ||||||||||||
1031 | - | |||||||||||||
1032 | /*! | - | ||||||||||||
1033 | \property QLineEdit::dragEnabled | - | ||||||||||||
1034 | \brief whether the lineedit starts a drag if the user presses and | - | ||||||||||||
1035 | moves the mouse on some selected text | - | ||||||||||||
1036 | - | |||||||||||||
1037 | Dragging is disabled by default. | - | ||||||||||||
1038 | */ | - | ||||||||||||
1039 | - | |||||||||||||
1040 | bool QLineEdit::dragEnabled() const | - | ||||||||||||
1041 | { | - | ||||||||||||
1042 | Q_D(const QLineEdit); | - | ||||||||||||
1043 | return d->dragEnabled; | - | ||||||||||||
1044 | } | - | ||||||||||||
1045 | - | |||||||||||||
1046 | void QLineEdit::setDragEnabled(bool b) | - | ||||||||||||
1047 | { | - | ||||||||||||
1048 | Q_D(QLineEdit); | - | ||||||||||||
1049 | d->dragEnabled = b; | - | ||||||||||||
1050 | } | - | ||||||||||||
1051 | - | |||||||||||||
1052 | /*! | - | ||||||||||||
1053 | \property QLineEdit::cursorMoveStyle | - | ||||||||||||
1054 | \brief the movement style of cursor in this line edit | - | ||||||||||||
1055 | \since 4.8 | - | ||||||||||||
1056 | - | |||||||||||||
1057 | When this property is set to Qt::VisualMoveStyle, the line edit will use visual | - | ||||||||||||
1058 | movement style. Pressing the left arrow key will always cause the cursor to move | - | ||||||||||||
1059 | left, regardless of the text's writing direction. The same behavior applies to | - | ||||||||||||
1060 | right arrow key. | - | ||||||||||||
1061 | - | |||||||||||||
1062 | When the property is Qt::LogicalMoveStyle (the default), within a LTR text block, | - | ||||||||||||
1063 | increase cursor position when pressing left arrow key, decrease cursor position | - | ||||||||||||
1064 | when pressing the right arrow key. If the text block is right to left, the opposite | - | ||||||||||||
1065 | behavior applies. | - | ||||||||||||
1066 | */ | - | ||||||||||||
1067 | - | |||||||||||||
1068 | Qt::CursorMoveStyle QLineEdit::cursorMoveStyle() const | - | ||||||||||||
1069 | { | - | ||||||||||||
1070 | Q_D(const QLineEdit); | - | ||||||||||||
1071 | return d->control->cursorMoveStyle(); | - | ||||||||||||
1072 | } | - | ||||||||||||
1073 | - | |||||||||||||
1074 | void QLineEdit::setCursorMoveStyle(Qt::CursorMoveStyle style) | - | ||||||||||||
1075 | { | - | ||||||||||||
1076 | Q_D(QLineEdit); | - | ||||||||||||
1077 | d->control->setCursorMoveStyle(style); | - | ||||||||||||
1078 | } | - | ||||||||||||
1079 | - | |||||||||||||
1080 | /*! | - | ||||||||||||
1081 | \property QLineEdit::acceptableInput | - | ||||||||||||
1082 | \brief whether the input satisfies the inputMask and the | - | ||||||||||||
1083 | validator. | - | ||||||||||||
1084 | - | |||||||||||||
1085 | By default, this property is \c true. | - | ||||||||||||
1086 | - | |||||||||||||
1087 | \sa setInputMask(), setValidator() | - | ||||||||||||
1088 | */ | - | ||||||||||||
1089 | bool QLineEdit::hasAcceptableInput() const | - | ||||||||||||
1090 | { | - | ||||||||||||
1091 | Q_D(const QLineEdit); | - | ||||||||||||
1092 | return d->control->hasAcceptableInput(); | - | ||||||||||||
1093 | } | - | ||||||||||||
1094 | - | |||||||||||||
1095 | /*! | - | ||||||||||||
1096 | Sets the margins around the text inside the frame to have the | - | ||||||||||||
1097 | sizes \a left, \a top, \a right, and \a bottom. | - | ||||||||||||
1098 | \since 4.5 | - | ||||||||||||
1099 | - | |||||||||||||
1100 | See also getTextMargins(). | - | ||||||||||||
1101 | */ | - | ||||||||||||
1102 | void QLineEdit::setTextMargins(int left, int top, int right, int bottom) | - | ||||||||||||
1103 | { | - | ||||||||||||
1104 | Q_D(QLineEdit); | - | ||||||||||||
1105 | d->leftTextMargin = left; | - | ||||||||||||
1106 | d->topTextMargin = top; | - | ||||||||||||
1107 | d->rightTextMargin = right; | - | ||||||||||||
1108 | d->bottomTextMargin = bottom; | - | ||||||||||||
1109 | updateGeometry(); | - | ||||||||||||
1110 | update(); | - | ||||||||||||
1111 | } | - | ||||||||||||
1112 | - | |||||||||||||
1113 | /*! | - | ||||||||||||
1114 | \since 4.6 | - | ||||||||||||
1115 | Sets the \a margins around the text inside the frame. | - | ||||||||||||
1116 | - | |||||||||||||
1117 | See also textMargins(). | - | ||||||||||||
1118 | */ | - | ||||||||||||
1119 | void QLineEdit::setTextMargins(const QMargins &margins) | - | ||||||||||||
1120 | { | - | ||||||||||||
1121 | setTextMargins(margins.left(), margins.top(), margins.right(), margins.bottom()); | - | ||||||||||||
1122 | } | - | ||||||||||||
1123 | - | |||||||||||||
1124 | /*! | - | ||||||||||||
1125 | Returns the widget's text margins for \a left, \a top, \a right, and \a bottom. | - | ||||||||||||
1126 | \since 4.5 | - | ||||||||||||
1127 | - | |||||||||||||
1128 | \sa setTextMargins() | - | ||||||||||||
1129 | */ | - | ||||||||||||
1130 | void QLineEdit::getTextMargins(int *left, int *top, int *right, int *bottom) const | - | ||||||||||||
1131 | { | - | ||||||||||||
1132 | Q_D(const QLineEdit); | - | ||||||||||||
1133 | if (left) | - | ||||||||||||
1134 | *left = d->leftTextMargin; | - | ||||||||||||
1135 | if (top) | - | ||||||||||||
1136 | *top = d->topTextMargin; | - | ||||||||||||
1137 | if (right) | - | ||||||||||||
1138 | *right = d->rightTextMargin; | - | ||||||||||||
1139 | if (bottom) | - | ||||||||||||
1140 | *bottom = d->bottomTextMargin; | - | ||||||||||||
1141 | } | - | ||||||||||||
1142 | - | |||||||||||||
1143 | /*! | - | ||||||||||||
1144 | \since 4.6 | - | ||||||||||||
1145 | Returns the widget's text margins. | - | ||||||||||||
1146 | - | |||||||||||||
1147 | \sa setTextMargins() | - | ||||||||||||
1148 | */ | - | ||||||||||||
1149 | QMargins QLineEdit::textMargins() const | - | ||||||||||||
1150 | { | - | ||||||||||||
1151 | Q_D(const QLineEdit); | - | ||||||||||||
1152 | return QMargins(d->leftTextMargin, d->topTextMargin, d->rightTextMargin, d->bottomTextMargin); | - | ||||||||||||
1153 | } | - | ||||||||||||
1154 | - | |||||||||||||
1155 | /*! | - | ||||||||||||
1156 | \property QLineEdit::inputMask | - | ||||||||||||
1157 | \brief The validation input mask | - | ||||||||||||
1158 | - | |||||||||||||
1159 | If no mask is set, inputMask() returns an empty string. | - | ||||||||||||
1160 | - | |||||||||||||
1161 | Sets the QLineEdit's validation mask. Validators can be used | - | ||||||||||||
1162 | instead of, or in conjunction with masks; see setValidator(). | - | ||||||||||||
1163 | - | |||||||||||||
1164 | Unset the mask and return to normal QLineEdit operation by passing | - | ||||||||||||
1165 | an empty string (""). | - | ||||||||||||
1166 | - | |||||||||||||
1167 | The table below shows the characters that can be used in an input mask. | - | ||||||||||||
1168 | A space character, the default character for a blank, is needed for cases | - | ||||||||||||
1169 | where a character is \e{permitted but not required}. | - | ||||||||||||
1170 | - | |||||||||||||
1171 | \table | - | ||||||||||||
1172 | \header \li Character \li Meaning | - | ||||||||||||
1173 | \row \li \c A \li ASCII alphabetic character required. A-Z, a-z. | - | ||||||||||||
1174 | \row \li \c a \li ASCII alphabetic character permitted but not required. | - | ||||||||||||
1175 | \row \li \c N \li ASCII alphanumeric character required. A-Z, a-z, 0-9. | - | ||||||||||||
1176 | \row \li \c n \li ASCII alphanumeric character permitted but not required. | - | ||||||||||||
1177 | \row \li \c X \li Any character required. | - | ||||||||||||
1178 | \row \li \c x \li Any character permitted but not required. | - | ||||||||||||
1179 | \row \li \c 9 \li ASCII digit required. 0-9. | - | ||||||||||||
1180 | \row \li \c 0 \li ASCII digit permitted but not required. | - | ||||||||||||
1181 | \row \li \c D \li ASCII digit required. 1-9. | - | ||||||||||||
1182 | \row \li \c d \li ASCII digit permitted but not required (1-9). | - | ||||||||||||
1183 | \row \li \c # \li ASCII digit or plus/minus sign permitted but not required. | - | ||||||||||||
1184 | \row \li \c H \li Hexadecimal character required. A-F, a-f, 0-9. | - | ||||||||||||
1185 | \row \li \c h \li Hexadecimal character permitted but not required. | - | ||||||||||||
1186 | \row \li \c B \li Binary character required. 0-1. | - | ||||||||||||
1187 | \row \li \c b \li Binary character permitted but not required. | - | ||||||||||||
1188 | \row \li \c > \li All following alphabetic characters are uppercased. | - | ||||||||||||
1189 | \row \li \c < \li All following alphabetic characters are lowercased. | - | ||||||||||||
1190 | \row \li \c ! \li Switch off case conversion. | - | ||||||||||||
1191 | \row \li \tt{\\} \li Use \tt{\\} to escape the special | - | ||||||||||||
1192 | characters listed above to use them as | - | ||||||||||||
1193 | separators. | - | ||||||||||||
1194 | \endtable | - | ||||||||||||
1195 | - | |||||||||||||
1196 | The mask consists of a string of mask characters and separators, | - | ||||||||||||
1197 | optionally followed by a semicolon and the character used for | - | ||||||||||||
1198 | blanks. The blank characters are always removed from the text | - | ||||||||||||
1199 | after editing. | - | ||||||||||||
1200 | - | |||||||||||||
1201 | Examples: | - | ||||||||||||
1202 | \table | - | ||||||||||||
1203 | \header \li Mask \li Notes | - | ||||||||||||
1204 | \row \li \c 000.000.000.000;_ \li IP address; blanks are \c{_}. | - | ||||||||||||
1205 | \row \li \c HH:HH:HH:HH:HH:HH;_ \li MAC address | - | ||||||||||||
1206 | \row \li \c 0000-00-00 \li ISO Date; blanks are \c space | - | ||||||||||||
1207 | \row \li \c >AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;# \li License number; | - | ||||||||||||
1208 | blanks are \c - and all (alphabetic) characters are converted to | - | ||||||||||||
1209 | uppercase. | - | ||||||||||||
1210 | \endtable | - | ||||||||||||
1211 | - | |||||||||||||
1212 | To get range control (e.g., for an IP address) use masks together | - | ||||||||||||
1213 | with \l{setValidator()}{validators}. | - | ||||||||||||
1214 | - | |||||||||||||
1215 | \sa maxLength | - | ||||||||||||
1216 | */ | - | ||||||||||||
1217 | QString QLineEdit::inputMask() const | - | ||||||||||||
1218 | { | - | ||||||||||||
1219 | Q_D(const QLineEdit); | - | ||||||||||||
1220 | return d->control->inputMask(); | - | ||||||||||||
1221 | } | - | ||||||||||||
1222 | - | |||||||||||||
1223 | void QLineEdit::setInputMask(const QString &inputMask) | - | ||||||||||||
1224 | { | - | ||||||||||||
1225 | Q_D(QLineEdit); | - | ||||||||||||
1226 | d->control->setInputMask(inputMask); | - | ||||||||||||
1227 | } | - | ||||||||||||
1228 | - | |||||||||||||
1229 | /*! | - | ||||||||||||
1230 | Selects all the text (i.e. highlights it) and moves the cursor to | - | ||||||||||||
1231 | the end. This is useful when a default value has been inserted | - | ||||||||||||
1232 | because if the user types before clicking on the widget, the | - | ||||||||||||
1233 | selected text will be deleted. | - | ||||||||||||
1234 | - | |||||||||||||
1235 | \sa setSelection(), deselect() | - | ||||||||||||
1236 | */ | - | ||||||||||||
1237 | - | |||||||||||||
1238 | void QLineEdit::selectAll() | - | ||||||||||||
1239 | { | - | ||||||||||||
1240 | Q_D(QLineEdit); | - | ||||||||||||
1241 | d->control->selectAll(); | - | ||||||||||||
1242 | } | - | ||||||||||||
1243 | - | |||||||||||||
1244 | /*! | - | ||||||||||||
1245 | Deselects any selected text. | - | ||||||||||||
1246 | - | |||||||||||||
1247 | \sa setSelection(), selectAll() | - | ||||||||||||
1248 | */ | - | ||||||||||||
1249 | - | |||||||||||||
1250 | void QLineEdit::deselect() | - | ||||||||||||
1251 | { | - | ||||||||||||
1252 | Q_D(QLineEdit); | - | ||||||||||||
1253 | d->control->deselect(); | - | ||||||||||||
1254 | } | - | ||||||||||||
1255 | - | |||||||||||||
1256 | - | |||||||||||||
1257 | /*! | - | ||||||||||||
1258 | Deletes any selected text, inserts \a newText, and validates the | - | ||||||||||||
1259 | result. If it is valid, it sets it as the new contents of the line | - | ||||||||||||
1260 | edit. | - | ||||||||||||
1261 | - | |||||||||||||
1262 | \sa setText(), clear() | - | ||||||||||||
1263 | */ | - | ||||||||||||
1264 | void QLineEdit::insert(const QString &newText) | - | ||||||||||||
1265 | { | - | ||||||||||||
1266 | // q->resetInputContext(); //#### FIX ME IN QT | - | ||||||||||||
1267 | Q_D(QLineEdit); | - | ||||||||||||
1268 | d->control->insert(newText); | - | ||||||||||||
1269 | } | - | ||||||||||||
1270 | - | |||||||||||||
1271 | /*! | - | ||||||||||||
1272 | Clears the contents of the line edit. | - | ||||||||||||
1273 | - | |||||||||||||
1274 | \sa setText(), insert() | - | ||||||||||||
1275 | */ | - | ||||||||||||
1276 | void QLineEdit::clear() | - | ||||||||||||
1277 | { | - | ||||||||||||
1278 | Q_D(QLineEdit); | - | ||||||||||||
1279 | d->resetInputMethod(); | - | ||||||||||||
1280 | d->control->clear(); | - | ||||||||||||
1281 | } | - | ||||||||||||
1282 | - | |||||||||||||
1283 | /*! | - | ||||||||||||
1284 | Undoes the last operation if undo is \l{QLineEdit::undoAvailable}{available}. Deselects any current | - | ||||||||||||
1285 | selection, and updates the selection start to the current cursor | - | ||||||||||||
1286 | position. | - | ||||||||||||
1287 | */ | - | ||||||||||||
1288 | void QLineEdit::undo() | - | ||||||||||||
1289 | { | - | ||||||||||||
1290 | Q_D(QLineEdit); | - | ||||||||||||
1291 | d->resetInputMethod(); | - | ||||||||||||
1292 | d->control->undo(); | - | ||||||||||||
1293 | } | - | ||||||||||||
1294 | - | |||||||||||||
1295 | /*! | - | ||||||||||||
1296 | Redoes the last operation if redo is \l{QLineEdit::redoAvailable}{available}. | - | ||||||||||||
1297 | */ | - | ||||||||||||
1298 | void QLineEdit::redo() | - | ||||||||||||
1299 | { | - | ||||||||||||
1300 | Q_D(QLineEdit); | - | ||||||||||||
1301 | d->resetInputMethod(); | - | ||||||||||||
1302 | d->control->redo(); | - | ||||||||||||
1303 | } | - | ||||||||||||
1304 | - | |||||||||||||
1305 | - | |||||||||||||
1306 | /*! | - | ||||||||||||
1307 | \property QLineEdit::readOnly | - | ||||||||||||
1308 | \brief whether the line edit is read only. | - | ||||||||||||
1309 | - | |||||||||||||
1310 | In read-only mode, the user can still copy the text to the | - | ||||||||||||
1311 | clipboard, or drag and drop the text (if echoMode() is \l Normal), | - | ||||||||||||
1312 | but cannot edit it. | - | ||||||||||||
1313 | - | |||||||||||||
1314 | QLineEdit does not show a cursor in read-only mode. | - | ||||||||||||
1315 | - | |||||||||||||
1316 | By default, this property is \c false. | - | ||||||||||||
1317 | - | |||||||||||||
1318 | \sa setEnabled() | - | ||||||||||||
1319 | */ | - | ||||||||||||
1320 | - | |||||||||||||
1321 | bool QLineEdit::isReadOnly() const | - | ||||||||||||
1322 | { | - | ||||||||||||
1323 | Q_D(const QLineEdit); | - | ||||||||||||
1324 | return d->control->isReadOnly(); | - | ||||||||||||
1325 | } | - | ||||||||||||
1326 | - | |||||||||||||
1327 | void QLineEdit::setReadOnly(bool enable) | - | ||||||||||||
1328 | { | - | ||||||||||||
1329 | Q_D(QLineEdit); | - | ||||||||||||
1330 | if (d->control->isReadOnly() != enable) { | - | ||||||||||||
1331 | d->control->setReadOnly(enable); | - | ||||||||||||
1332 | d->setClearButtonEnabled(!enable); | - | ||||||||||||
1333 | setAttribute(Qt::WA_MacShowFocusRect, !enable); | - | ||||||||||||
1334 | setAttribute(Qt::WA_InputMethodEnabled, d->shouldEnableInputMethod()); | - | ||||||||||||
1335 | #ifndef QT_NO_CURSOR | - | ||||||||||||
1336 | setCursor(enable ? Qt::ArrowCursor : Qt::IBeamCursor); | - | ||||||||||||
1337 | #endif | - | ||||||||||||
1338 | QEvent event(QEvent::ReadOnlyChange); | - | ||||||||||||
1339 | QCoreApplication::sendEvent(this, &event); | - | ||||||||||||
1340 | update(); | - | ||||||||||||
1341 | } | - | ||||||||||||
1342 | } | - | ||||||||||||
1343 | - | |||||||||||||
1344 | - | |||||||||||||
1345 | #ifndef QT_NO_CLIPBOARD | - | ||||||||||||
1346 | /*! | - | ||||||||||||
1347 | Copies the selected text to the clipboard and deletes it, if there | - | ||||||||||||
1348 | is any, and if echoMode() is \l Normal. | - | ||||||||||||
1349 | - | |||||||||||||
1350 | If the current validator disallows deleting the selected text, | - | ||||||||||||
1351 | cut() will copy without deleting. | - | ||||||||||||
1352 | - | |||||||||||||
1353 | \sa copy(), paste(), setValidator() | - | ||||||||||||
1354 | */ | - | ||||||||||||
1355 | - | |||||||||||||
1356 | void QLineEdit::cut() | - | ||||||||||||
1357 | { | - | ||||||||||||
1358 | if (hasSelectedText()) { | - | ||||||||||||
1359 | copy(); | - | ||||||||||||
1360 | del(); | - | ||||||||||||
1361 | } | - | ||||||||||||
1362 | } | - | ||||||||||||
1363 | - | |||||||||||||
1364 | - | |||||||||||||
1365 | /*! | - | ||||||||||||
1366 | Copies the selected text to the clipboard, if there is any, and if | - | ||||||||||||
1367 | echoMode() is \l Normal. | - | ||||||||||||
1368 | - | |||||||||||||
1369 | \sa cut(), paste() | - | ||||||||||||
1370 | */ | - | ||||||||||||
1371 | - | |||||||||||||
1372 | void QLineEdit::copy() const | - | ||||||||||||
1373 | { | - | ||||||||||||
1374 | Q_D(const QLineEdit); | - | ||||||||||||
1375 | d->control->copy(); | - | ||||||||||||
1376 | } | - | ||||||||||||
1377 | - | |||||||||||||
1378 | /*! | - | ||||||||||||
1379 | Inserts the clipboard's text at the cursor position, deleting any | - | ||||||||||||
1380 | selected text, providing the line edit is not \l{QLineEdit::readOnly}{read-only}. | - | ||||||||||||
1381 | - | |||||||||||||
1382 | If the end result would not be acceptable to the current | - | ||||||||||||
1383 | \l{setValidator()}{validator}, nothing happens. | - | ||||||||||||
1384 | - | |||||||||||||
1385 | \sa copy(), cut() | - | ||||||||||||
1386 | */ | - | ||||||||||||
1387 | - | |||||||||||||
1388 | void QLineEdit::paste() | - | ||||||||||||
1389 | { | - | ||||||||||||
1390 | Q_D(QLineEdit); | - | ||||||||||||
1391 | d->control->paste(); | - | ||||||||||||
1392 | } | - | ||||||||||||
1393 | - | |||||||||||||
1394 | #endif // !QT_NO_CLIPBOARD | - | ||||||||||||
1395 | - | |||||||||||||
1396 | /*! \reimp | - | ||||||||||||
1397 | */ | - | ||||||||||||
1398 | bool QLineEdit::event(QEvent * e) | - | ||||||||||||
1399 | { | - | ||||||||||||
1400 | Q_D(QLineEdit); | - | ||||||||||||
1401 | if (e->type() == QEvent::Timer) {
| 0 | ||||||||||||
1402 | // ### Qt6: move to timerEvent, is here for binary compatibility | - | ||||||||||||
1403 | int timerId = ((QTimerEvent*)e)->timerId(); | - | ||||||||||||
1404 | if (false) { dead code: { } | - | ||||||||||||
1405 | #ifndef QT_NO_DRAGANDDROP dead code: { } | - | ||||||||||||
1406 | } else if (timerId == d->dndTimer.timerId()) {
dead code: { } | - | ||||||||||||
1407 | d->drag(); | - | ||||||||||||
1408 | #endif | - | ||||||||||||
1409 | } never executed: end of block | 0 | ||||||||||||
1410 | else if (timerId == d->tripleClickTimer.timerId())
| 0 | ||||||||||||
1411 | d->tripleClickTimer.stop(); never executed: d->tripleClickTimer.stop(); | 0 | ||||||||||||
1412 | } else if (e->type() == QEvent::ContextMenu) { never executed: end of block
| 0 | ||||||||||||
1413 | #ifndef QT_NO_IM | - | ||||||||||||
1414 | if (d->control->composeMode())
| 0 | ||||||||||||
1415 | return true; never executed: return true; | 0 | ||||||||||||
1416 | #endif | - | ||||||||||||
1417 | //d->separate(); | - | ||||||||||||
1418 | } else if (e->type() == QEvent::WindowActivate) { never executed: end of block
| 0 | ||||||||||||
1419 | QTimer::singleShot(0, this, SLOT(_q_handleWindowActivate())); | - | ||||||||||||
1420 | #ifndef QT_NO_SHORTCUT | - | ||||||||||||
1421 | } else if (e->type() == QEvent::ShortcutOverride) { never executed: end of block
| 0 | ||||||||||||
1422 | QKeyEvent *ke = static_cast<QKeyEvent*>(e); | - | ||||||||||||
1423 | d->control->processShortcutOverrideEvent(ke); | - | ||||||||||||
1424 | #endif | - | ||||||||||||
1425 | } else if (e->type() == QEvent::KeyRelease) { never executed: end of block
| 0 | ||||||||||||
1426 | d->control->setCursorBlinkPeriod(QApplication::cursorFlashTime());updateCursorBlinking(); | - | ||||||||||||
1427 | } else if (e->type() == QEvent::Show) { never executed: end of block
| 0 | ||||||||||||
1428 | //In order to get the cursor blinking if QComboBox::setEditable is called when the combobox has focus | - | ||||||||||||
1429 | if (hasFocus()) {
| 0 | ||||||||||||
1430 | d->control->setCursorBlinkPeriodsetBlinkingCursorEnabled(QApplication::cursorFlashTime());true); | - | ||||||||||||
1431 | QStyleOptionFrame opt; | - | ||||||||||||
1432 | initStyleOption(&opt); | - | ||||||||||||
1433 | if ((!hasSelectedText() && d->control->preeditAreaText().isEmpty())
| 0 | ||||||||||||
1434 | || style()->styleHint(QStyle::SH_BlinkCursorWhenTextSelected, &opt, this))
| 0 | ||||||||||||
1435 | d->setCursorVisible(true); never executed: d->setCursorVisible(true); | 0 | ||||||||||||
1436 | } never executed: end of block | 0 | ||||||||||||
1437 | } else if (e->type() == QEvent::ActionRemoved) { never executed: end of block
| 0 | ||||||||||||
1438 | d->removeAction(static_cast<QActionEvent *>(e)->action()); | - | ||||||||||||
1439 | } else if (e->type() == QEvent::Resize) { never executed: end of block
| 0 | ||||||||||||
1440 | d->positionSideWidgets(); | - | ||||||||||||
1441 | } never executed: end of block | 0 | ||||||||||||
1442 | #ifdef QT_KEYPAD_NAVIGATION | - | ||||||||||||
1443 | if (QApplication::keypadNavigationEnabled()) { | - | ||||||||||||
1444 | if (e->type() == QEvent::EnterEditFocus) { | - | ||||||||||||
1445 | end(false); | - | ||||||||||||
1446 | d->setCursorVisible(true); | - | ||||||||||||
1447 | d->control->setCursorBlinkPeriodsetCursorBlinkEnabled(QApplication::cursorFlashTime());true); | - | ||||||||||||
1448 | } else if (e->type() == QEvent::LeaveEditFocus) { | - | ||||||||||||
1449 | d->setCursorVisible(false); | - | ||||||||||||
1450 | d->control->setCursorBlinkPeriodsetCursorBlinkEnabled(0false); | - | ||||||||||||
1451 | if (d->control->hasAcceptableInput() || d->control->fixup()) | - | ||||||||||||
1452 | emit editingFinished(); | - | ||||||||||||
1453 | } | - | ||||||||||||
1454 | } | - | ||||||||||||
1455 | #endif | - | ||||||||||||
1456 | return QWidget::event(e); never executed: return QWidget::event(e); | 0 | ||||||||||||
1457 | } | - | ||||||||||||
1458 | - | |||||||||||||
1459 | /*! \reimp | - | ||||||||||||
1460 | */ | - | ||||||||||||
1461 | void QLineEdit::mousePressEvent(QMouseEvent* e) | - | ||||||||||||
1462 | { | - | ||||||||||||
1463 | Q_D(QLineEdit); | - | ||||||||||||
1464 | - | |||||||||||||
1465 | d->mousePressPos = e->pos(); | - | ||||||||||||
1466 | - | |||||||||||||
1467 | if (d->sendMouseEventToInputContext(e)) | - | ||||||||||||
1468 | return; | - | ||||||||||||
1469 | if (e->button() == Qt::RightButton) | - | ||||||||||||
1470 | return; | - | ||||||||||||
1471 | #ifdef QT_KEYPAD_NAVIGATION | - | ||||||||||||
1472 | if (QApplication::keypadNavigationEnabled() && !hasEditFocus()) { | - | ||||||||||||
1473 | setEditFocus(true); | - | ||||||||||||
1474 | // Get the completion list to pop up. | - | ||||||||||||
1475 | if (d->control->completer()) | - | ||||||||||||
1476 | d->control->completer()->complete(); | - | ||||||||||||
1477 | } | - | ||||||||||||
1478 | #endif | - | ||||||||||||
1479 | if (d->tripleClickTimer.isActive() && (e->pos() - d->tripleClick).manhattanLength() < | - | ||||||||||||
1480 | QApplication::startDragDistance()) { | - | ||||||||||||
1481 | selectAll(); | - | ||||||||||||
1482 | return; | - | ||||||||||||
1483 | } | - | ||||||||||||
1484 | bool mark = e->modifiers() & Qt::ShiftModifier; | - | ||||||||||||
1485 | #ifdef Q_OS_ANDROID | - | ||||||||||||
1486 | mark = mark && (d->imHints & Qt::ImhNoPredictiveText); | - | ||||||||||||
1487 | #endif // Q_OS_ANDROID | - | ||||||||||||
1488 | int cursor = d->xToPos(e->pos().x()); | - | ||||||||||||
1489 | #ifndef QT_NO_DRAGANDDROP | - | ||||||||||||
1490 | if (!mark && d->dragEnabled && d->control->echoMode() == Normal && | - | ||||||||||||
1491 | e->button() == Qt::LeftButton && d->inSelection(e->pos().x())) { | - | ||||||||||||
1492 | if (!d->dndTimer.isActive()) | - | ||||||||||||
1493 | d->dndTimer.start(QApplication::startDragTime(), this); | - | ||||||||||||
1494 | } else | - | ||||||||||||
1495 | #endif | - | ||||||||||||
1496 | { | - | ||||||||||||
1497 | d->control->moveCursor(cursor, mark); | - | ||||||||||||
1498 | } | - | ||||||||||||
1499 | } | - | ||||||||||||
1500 | - | |||||||||||||
1501 | /*! \reimp | - | ||||||||||||
1502 | */ | - | ||||||||||||
1503 | void QLineEdit::mouseMoveEvent(QMouseEvent * e) | - | ||||||||||||
1504 | { | - | ||||||||||||
1505 | Q_D(QLineEdit); | - | ||||||||||||
1506 | - | |||||||||||||
1507 | if (e->buttons() & Qt::LeftButton) { | - | ||||||||||||
1508 | #ifndef QT_NO_DRAGANDDROP | - | ||||||||||||
1509 | if (d->dndTimer.isActive()) { | - | ||||||||||||
1510 | if ((d->mousePressPos - e->pos()).manhattanLength() > QApplication::startDragDistance()) | - | ||||||||||||
1511 | d->drag(); | - | ||||||||||||
1512 | } else | - | ||||||||||||
1513 | #endif | - | ||||||||||||
1514 | { | - | ||||||||||||
1515 | #ifndef Q_OS_ANDROID | - | ||||||||||||
1516 | const bool select = true; | - | ||||||||||||
1517 | #else | - | ||||||||||||
1518 | const bool select = (d->imHints & Qt::ImhNoPredictiveText); | - | ||||||||||||
1519 | #endif | - | ||||||||||||
1520 | #ifndef QT_NO_IM | - | ||||||||||||
1521 | if (d->control->composeMode() && select) { | - | ||||||||||||
1522 | int startPos = d->xToPos(d->mousePressPos.x()); | - | ||||||||||||
1523 | int currentPos = d->xToPos(e->pos().x()); | - | ||||||||||||
1524 | if (startPos != currentPos) | - | ||||||||||||
1525 | d->control->setSelection(startPos, currentPos - startPos); | - | ||||||||||||
1526 | - | |||||||||||||
1527 | } else | - | ||||||||||||
1528 | #endif | - | ||||||||||||
1529 | { | - | ||||||||||||
1530 | d->control->moveCursor(d->xToPos(e->pos().x()), select); | - | ||||||||||||
1531 | } | - | ||||||||||||
1532 | } | - | ||||||||||||
1533 | } | - | ||||||||||||
1534 | - | |||||||||||||
1535 | d->sendMouseEventToInputContext(e); | - | ||||||||||||
1536 | } | - | ||||||||||||
1537 | - | |||||||||||||
1538 | /*! \reimp | - | ||||||||||||
1539 | */ | - | ||||||||||||
1540 | void QLineEdit::mouseReleaseEvent(QMouseEvent* e) | - | ||||||||||||
1541 | { | - | ||||||||||||
1542 | Q_D(QLineEdit); | - | ||||||||||||
1543 | if (d->sendMouseEventToInputContext(e)) | - | ||||||||||||
1544 | return; | - | ||||||||||||
1545 | #ifndef QT_NO_DRAGANDDROP | - | ||||||||||||
1546 | if (e->button() == Qt::LeftButton) { | - | ||||||||||||
1547 | if (d->dndTimer.isActive()) { | - | ||||||||||||
1548 | d->dndTimer.stop(); | - | ||||||||||||
1549 | deselect(); | - | ||||||||||||
1550 | return; | - | ||||||||||||
1551 | } | - | ||||||||||||
1552 | } | - | ||||||||||||
1553 | #endif | - | ||||||||||||
1554 | #ifndef QT_NO_CLIPBOARD | - | ||||||||||||
1555 | if (QApplication::clipboard()->supportsSelection()) { | - | ||||||||||||
1556 | if (e->button() == Qt::LeftButton) { | - | ||||||||||||
1557 | d->control->copy(QClipboard::Selection); | - | ||||||||||||
1558 | } else if (!d->control->isReadOnly() && e->button() == Qt::MidButton) { | - | ||||||||||||
1559 | deselect(); | - | ||||||||||||
1560 | insert(QApplication::clipboard()->text(QClipboard::Selection)); | - | ||||||||||||
1561 | } | - | ||||||||||||
1562 | } | - | ||||||||||||
1563 | #endif | - | ||||||||||||
1564 | - | |||||||||||||
1565 | if (!isReadOnly() && rect().contains(e->pos())) | - | ||||||||||||
1566 | d->handleSoftwareInputPanel(e->button(), d->clickCausedFocus); | - | ||||||||||||
1567 | d->clickCausedFocus = 0; | - | ||||||||||||
1568 | } | - | ||||||||||||
1569 | - | |||||||||||||
1570 | /*! \reimp | - | ||||||||||||
1571 | */ | - | ||||||||||||
1572 | void QLineEdit::mouseDoubleClickEvent(QMouseEvent* e) | - | ||||||||||||
1573 | { | - | ||||||||||||
1574 | Q_D(QLineEdit); | - | ||||||||||||
1575 | - | |||||||||||||
1576 | if (e->button() == Qt::LeftButton) { | - | ||||||||||||
1577 | int position = d->xToPos(e->pos().x()); | - | ||||||||||||
1578 | - | |||||||||||||
1579 | // exit composition mode | - | ||||||||||||
1580 | #ifndef QT_NO_IM | - | ||||||||||||
1581 | if (d->control->composeMode()) { | - | ||||||||||||
1582 | int preeditPos = d->control->cursor(); | - | ||||||||||||
1583 | int posInPreedit = position - d->control->cursor(); | - | ||||||||||||
1584 | int preeditLength = d->control->preeditAreaText().length(); | - | ||||||||||||
1585 | bool positionOnPreedit = false; | - | ||||||||||||
1586 | - | |||||||||||||
1587 | if (posInPreedit >= 0 && posInPreedit <= preeditLength) | - | ||||||||||||
1588 | positionOnPreedit = true; | - | ||||||||||||
1589 | - | |||||||||||||
1590 | int textLength = d->control->end(); | - | ||||||||||||
1591 | d->control->commitPreedit(); | - | ||||||||||||
1592 | int sizeChange = d->control->end() - textLength; | - | ||||||||||||
1593 | - | |||||||||||||
1594 | if (positionOnPreedit) { | - | ||||||||||||
1595 | if (sizeChange == 0) | - | ||||||||||||
1596 | position = -1; // cancel selection, word disappeared | - | ||||||||||||
1597 | else | - | ||||||||||||
1598 | // ensure not selecting after preedit if event happened there | - | ||||||||||||
1599 | position = qBound(preeditPos, position, preeditPos + sizeChange); | - | ||||||||||||
1600 | } else if (position > preeditPos) { | - | ||||||||||||
1601 | // adjust positions after former preedit by how much text changed | - | ||||||||||||
1602 | position += (sizeChange - preeditLength); | - | ||||||||||||
1603 | } | - | ||||||||||||
1604 | } | - | ||||||||||||
1605 | #endif | - | ||||||||||||
1606 | - | |||||||||||||
1607 | if (position >= 0) | - | ||||||||||||
1608 | d->control->selectWordAtPos(position); | - | ||||||||||||
1609 | - | |||||||||||||
1610 | d->tripleClickTimer.start(QApplication::doubleClickInterval(), this); | - | ||||||||||||
1611 | d->tripleClick = e->pos(); | - | ||||||||||||
1612 | } else { | - | ||||||||||||
1613 | d->sendMouseEventToInputContext(e); | - | ||||||||||||
1614 | } | - | ||||||||||||
1615 | } | - | ||||||||||||
1616 | - | |||||||||||||
1617 | /*! | - | ||||||||||||
1618 | \fn void QLineEdit::returnPressed() | - | ||||||||||||
1619 | - | |||||||||||||
1620 | This signal is emitted when the Return or Enter key is pressed. | - | ||||||||||||
1621 | Note that if there is a validator() or inputMask() set on the line | - | ||||||||||||
1622 | edit, the returnPressed() signal will only be emitted if the input | - | ||||||||||||
1623 | follows the inputMask() and the validator() returns | - | ||||||||||||
1624 | QValidator::Acceptable. | - | ||||||||||||
1625 | */ | - | ||||||||||||
1626 | - | |||||||||||||
1627 | /*! | - | ||||||||||||
1628 | \fn void QLineEdit::editingFinished() | - | ||||||||||||
1629 | - | |||||||||||||
1630 | This signal is emitted when the Return or Enter key is pressed or | - | ||||||||||||
1631 | the line edit loses focus. Note that if there is a validator() or | - | ||||||||||||
1632 | inputMask() set on the line edit and enter/return is pressed, the | - | ||||||||||||
1633 | editingFinished() signal will only be emitted if the input follows | - | ||||||||||||
1634 | the inputMask() and the validator() returns QValidator::Acceptable. | - | ||||||||||||
1635 | */ | - | ||||||||||||
1636 | - | |||||||||||||
1637 | /*! | - | ||||||||||||
1638 | Converts the given key press \a event into a line edit action. | - | ||||||||||||
1639 | - | |||||||||||||
1640 | If Return or Enter is pressed and the current text is valid (or | - | ||||||||||||
1641 | can be \l{QValidator::fixup()}{made valid} by the | - | ||||||||||||
1642 | validator), the signal returnPressed() is emitted. | - | ||||||||||||
1643 | - | |||||||||||||
1644 | The default key bindings are listed in the class's detailed | - | ||||||||||||
1645 | description. | - | ||||||||||||
1646 | */ | - | ||||||||||||
1647 | - | |||||||||||||
1648 | void QLineEdit::keyPressEvent(QKeyEvent *event) | - | ||||||||||||
1649 | { | - | ||||||||||||
1650 | Q_D(QLineEdit); | - | ||||||||||||
1651 | #ifdef QT_KEYPAD_NAVIGATION | - | ||||||||||||
1652 | bool select = false; | - | ||||||||||||
1653 | switch (event->key()) { | - | ||||||||||||
1654 | case Qt::Key_Select: | - | ||||||||||||
1655 | if (QApplication::keypadNavigationEnabled()) { | - | ||||||||||||
1656 | if (hasEditFocus()) { | - | ||||||||||||
1657 | setEditFocus(false); | - | ||||||||||||
1658 | if (d->control->completer() && d->control->completer()->popup()->isVisible()) | - | ||||||||||||
1659 | d->control->completer()->popup()->hide(); | - | ||||||||||||
1660 | select = true; | - | ||||||||||||
1661 | } | - | ||||||||||||
1662 | } | - | ||||||||||||
1663 | break; | - | ||||||||||||
1664 | case Qt::Key_Back: | - | ||||||||||||
1665 | case Qt::Key_No: | - | ||||||||||||
1666 | if (!QApplication::keypadNavigationEnabled() || !hasEditFocus()) { | - | ||||||||||||
1667 | event->ignore(); | - | ||||||||||||
1668 | return; | - | ||||||||||||
1669 | } | - | ||||||||||||
1670 | break; | - | ||||||||||||
1671 | default: | - | ||||||||||||
1672 | if (QApplication::keypadNavigationEnabled()) { | - | ||||||||||||
1673 | if (!hasEditFocus() && !(event->modifiers() & Qt::ControlModifier)) { | - | ||||||||||||
1674 | if (!event->text().isEmpty() && event->text().at(0).isPrint() | - | ||||||||||||
1675 | && !isReadOnly()) | - | ||||||||||||
1676 | setEditFocus(true); | - | ||||||||||||
1677 | else { | - | ||||||||||||
1678 | event->ignore(); | - | ||||||||||||
1679 | return; | - | ||||||||||||
1680 | } | - | ||||||||||||
1681 | } | - | ||||||||||||
1682 | } | - | ||||||||||||
1683 | } | - | ||||||||||||
1684 | - | |||||||||||||
1685 | - | |||||||||||||
1686 | - | |||||||||||||
1687 | if (QApplication::keypadNavigationEnabled() && !select && !hasEditFocus()) { | - | ||||||||||||
1688 | setEditFocus(true); | - | ||||||||||||
1689 | if (event->key() == Qt::Key_Select) | - | ||||||||||||
1690 | return; // Just start. No action. | - | ||||||||||||
1691 | } | - | ||||||||||||
1692 | #endif | - | ||||||||||||
1693 | d->control->processKeyEvent(event); | - | ||||||||||||
1694 | if (event->isAccepted()) {
| 0 | ||||||||||||
1695 | if (layoutDirection() != d->control->layoutDirection())
| 0 | ||||||||||||
1696 | setLayoutDirection(d->control->layoutDirection()); never executed: setLayoutDirection(d->control->layoutDirection()); | 0 | ||||||||||||
1697 | d->control->setCursorBlinkPeriod(0);updateCursorBlinking(); | - | ||||||||||||
1698 | } never executed: end of block | 0 | ||||||||||||
1699 | } never executed: end of block | 0 | ||||||||||||
1700 | - | |||||||||||||
1701 | /*! | - | ||||||||||||
1702 | \since 4.4 | - | ||||||||||||
1703 | - | |||||||||||||
1704 | Returns a rectangle that includes the lineedit cursor. | - | ||||||||||||
1705 | */ | - | ||||||||||||
1706 | QRect QLineEdit::cursorRect() const | - | ||||||||||||
1707 | { | - | ||||||||||||
1708 | Q_D(const QLineEdit); | - | ||||||||||||
1709 | return d->cursorRect(); | - | ||||||||||||
1710 | } | - | ||||||||||||
1711 | - | |||||||||||||
1712 | /*! \reimp | - | ||||||||||||
1713 | */ | - | ||||||||||||
1714 | void QLineEdit::inputMethodEvent(QInputMethodEvent *e) | - | ||||||||||||
1715 | { | - | ||||||||||||
1716 | Q_D(QLineEdit); | - | ||||||||||||
1717 | if (d->control->isReadOnly()) { | - | ||||||||||||
1718 | e->ignore(); | - | ||||||||||||
1719 | return; | - | ||||||||||||
1720 | } | - | ||||||||||||
1721 | - | |||||||||||||
1722 | if (echoMode() == PasswordEchoOnEdit && !d->control->passwordEchoEditing()) { | - | ||||||||||||
1723 | // Clear the edit and reset to normal echo mode while entering input | - | ||||||||||||
1724 | // method data; the echo mode switches back when the edit loses focus. | - | ||||||||||||
1725 | // ### changes a public property, resets current content. | - | ||||||||||||
1726 | d->updatePasswordEchoEditing(true); | - | ||||||||||||
1727 | clear(); | - | ||||||||||||
1728 | } | - | ||||||||||||
1729 | - | |||||||||||||
1730 | #ifdef QT_KEYPAD_NAVIGATION | - | ||||||||||||
1731 | // Focus in if currently in navigation focus on the widget | - | ||||||||||||
1732 | // Only focus in on preedits, to allow input methods to | - | ||||||||||||
1733 | // commit text as they focus out without interfering with focus | - | ||||||||||||
1734 | if (QApplication::keypadNavigationEnabled() | - | ||||||||||||
1735 | && hasFocus() && !hasEditFocus() | - | ||||||||||||
1736 | && !e->preeditString().isEmpty()) | - | ||||||||||||
1737 | setEditFocus(true); | - | ||||||||||||
1738 | #endif | - | ||||||||||||
1739 | - | |||||||||||||
1740 | d->control->processInputMethodEvent(e); | - | ||||||||||||
1741 | - | |||||||||||||
1742 | #ifndef QT_NO_COMPLETER | - | ||||||||||||
1743 | if (!e->commitString().isEmpty()) | - | ||||||||||||
1744 | d->control->complete(Qt::Key_unknown); | - | ||||||||||||
1745 | #endif | - | ||||||||||||
1746 | } | - | ||||||||||||
1747 | - | |||||||||||||
1748 | /*!\reimp | - | ||||||||||||
1749 | */ | - | ||||||||||||
1750 | QVariant QLineEdit::inputMethodQuery(Qt::InputMethodQuery property) const | - | ||||||||||||
1751 | { | - | ||||||||||||
1752 | return inputMethodQuery(property, QVariant()); never executed: return inputMethodQuery(property, QVariant()); | 0 | ||||||||||||
1753 | } | - | ||||||||||||
1754 | - | |||||||||||||
1755 | /*!\internal | - | ||||||||||||
1756 | */ | - | ||||||||||||
1757 | QVariant QLineEdit::inputMethodQuery(Qt::InputMethodQuery property, QVariant argument) const | - | ||||||||||||
1758 | { | - | ||||||||||||
1759 | Q_D(const QLineEdit); | - | ||||||||||||
1760 | switch(property) { | - | ||||||||||||
1761 | case Qt::ImCursorRectangle: never executed: case Qt::ImCursorRectangle: | 0 | ||||||||||||
1762 | return d->cursorRect(); never executed: return d->cursorRect(); | 0 | ||||||||||||
1763 | case Qt::ImAnchorRectangle: never executed: case Qt::ImAnchorRectangle: | 0 | ||||||||||||
1764 | return d->adjustedControlRect(d->control->anchorRect()); never executed: return d->adjustedControlRect(d->control->anchorRect()); | 0 | ||||||||||||
1765 | case Qt::ImFont: never executed: case Qt::ImFont: | 0 | ||||||||||||
1766 | return font(); never executed: return font(); | 0 | ||||||||||||
1767 | case Qt::ImCursorPosition: { never executed: case Qt::ImCursorPosition: | 0 | ||||||||||||
1768 | const QPointF pt = argument.toPointF(); | - | ||||||||||||
1769 | if (!pt.isNull())
| 0 | ||||||||||||
1770 | return QVariant(d->xToPos(pt.x(), QTextLine::CursorBetweenCharacters)); never executed: return QVariant(d->xToPos(pt.x(), QTextLine::CursorBetweenCharacters)); | 0 | ||||||||||||
1771 | return QVariant(d->control->cursor()); } never executed: return QVariant(d->control->cursor()); | 0 | ||||||||||||
1772 | case Qt::ImSurroundingText: never executed: case Qt::ImSurroundingText: | 0 | ||||||||||||
1773 | return QVariant(d->control->text()); never executed: return QVariant(d->control->text()); | 0 | ||||||||||||
1774 | case Qt::ImCurrentSelection: never executed: case Qt::ImCurrentSelection: | 0 | ||||||||||||
1775 | return QVariant(selectedText()); never executed: return QVariant(selectedText()); | 0 | ||||||||||||
1776 | case Qt::ImMaximumTextLength: never executed: case Qt::ImMaximumTextLength: | 0 | ||||||||||||
1777 | return QVariant(maxLength()); never executed: return QVariant(maxLength()); | 0 | ||||||||||||
1778 | case Qt::ImAnchorPosition: never executed: case Qt::ImAnchorPosition: | 0 | ||||||||||||
1779 | if (d->control->selectionStart() == d->control->selectionEnd())
| 0 | ||||||||||||
1780 | return QVariant(d->control->cursor()); never executed: return QVariant(d->control->cursor()); | 0 | ||||||||||||
1781 | else if (d->control->selectionStart() == d->control->cursor())
| 0 | ||||||||||||
1782 | return QVariant(d->control->selectionEnd()); never executed: return QVariant(d->control->selectionEnd()); | 0 | ||||||||||||
1783 | else | - | ||||||||||||
1784 | return QVariant(d->control->selectionStart()); never executed: return QVariant(d->control->selectionStart()); | 0 | ||||||||||||
1785 | default: never executed: default: | 0 | ||||||||||||
1786 | return QWidget::inputMethodQuery(property); never executed: return QWidget::inputMethodQuery(property); | 0 | ||||||||||||
1787 | } | - | ||||||||||||
1788 | } | - | ||||||||||||
1789 | - | |||||||||||||
1790 | /*!\reimp | - | ||||||||||||
1791 | */ | - | ||||||||||||
1792 | - | |||||||||||||
1793 | void QLineEdit::focusInEvent(QFocusEvent *e) | - | ||||||||||||
1794 | { | - | ||||||||||||
1795 | Q_D(QLineEdit); | - | ||||||||||||
1796 | if (e->reason() == Qt::TabFocusReason ||
| 0 | ||||||||||||
1797 | e->reason() == Qt::BacktabFocusReason ||
| 0 | ||||||||||||
1798 | e->reason() == Qt::ShortcutFocusReason) {
| 0 | ||||||||||||
1799 | if (!d->control->inputMask().isEmpty())
| 0 | ||||||||||||
1800 | d->control->moveCursor(d->control->nextMaskBlank(0)); never executed: d->control->moveCursor(d->control->nextMaskBlank(0)); | 0 | ||||||||||||
1801 | else if (!d->control->hasSelectedText())
| 0 | ||||||||||||
1802 | selectAll(); never executed: selectAll(); | 0 | ||||||||||||
1803 | } else if (e->reason() == Qt::MouseFocusReason) { never executed: end of block
| 0 | ||||||||||||
1804 | d->clickCausedFocus = 1; | - | ||||||||||||
1805 | } never executed: end of block | 0 | ||||||||||||
1806 | #ifdef QT_KEYPAD_NAVIGATION | - | ||||||||||||
1807 | if (!QApplication::keypadNavigationEnabled() || (hasEditFocus() && ( e->reason() == Qt::PopupFocusReason))) { | - | ||||||||||||
1808 | #endif | - | ||||||||||||
1809 | d->control->setCursorBlinkPeriodsetBlinkingCursorEnabled(QApplication::cursorFlashTime());true); | - | ||||||||||||
1810 | QStyleOptionFrame opt; | - | ||||||||||||
1811 | initStyleOption(&opt); | - | ||||||||||||
1812 | if((!hasSelectedText() && d->control->preeditAreaText().isEmpty())
| 0 | ||||||||||||
1813 | || style()->styleHint(QStyle::SH_BlinkCursorWhenTextSelected, &opt, this))
| 0 | ||||||||||||
1814 | d->setCursorVisible(true); never executed: d->setCursorVisible(true); | 0 | ||||||||||||
1815 | #ifdef Q_DEAD_CODE_FROM_QT4_MAC | - | ||||||||||||
1816 | if (d->control->echoMode() == Password || d->control->echoMode() == NoEcho) | - | ||||||||||||
1817 | qt_mac_secure_keyboard(true); | - | ||||||||||||
1818 | #endif | - | ||||||||||||
1819 | #ifdef QT_KEYPAD_NAVIGATION | - | ||||||||||||
1820 | d->control->setCancelText(d->control->text()); | - | ||||||||||||
1821 | } | - | ||||||||||||
1822 | #endif | - | ||||||||||||
1823 | #ifndef QT_NO_COMPLETER | - | ||||||||||||
1824 | if (d->control->completer()) {
| 0 | ||||||||||||
1825 | d->control->completer()->setWidget(this); | - | ||||||||||||
1826 | QObject::connect(d->control->completer(), SIGNAL(activated(QString)), | - | ||||||||||||
1827 | this, SLOT(setText(QString))); | - | ||||||||||||
1828 | QObject::connect(d->control->completer(), SIGNAL(highlighted(QString)), | - | ||||||||||||
1829 | this, SLOT(_q_completionHighlighted(QString))); | - | ||||||||||||
1830 | } never executed: end of block | 0 | ||||||||||||
1831 | #endif | - | ||||||||||||
1832 | update(); | - | ||||||||||||
1833 | } never executed: end of block | 0 | ||||||||||||
1834 | - | |||||||||||||
1835 | /*!\reimp | - | ||||||||||||
1836 | */ | - | ||||||||||||
1837 | - | |||||||||||||
1838 | void QLineEdit::focusOutEvent(QFocusEvent *e) | - | ||||||||||||
1839 | { | - | ||||||||||||
1840 | Q_D(QLineEdit); | - | ||||||||||||
1841 | if (d->control->passwordEchoEditing()) {
| 0 | ||||||||||||
1842 | // Reset the echomode back to PasswordEchoOnEdit when the widget loses | - | ||||||||||||
1843 | // focus. | - | ||||||||||||
1844 | d->updatePasswordEchoEditing(false); | - | ||||||||||||
1845 | } never executed: end of block | 0 | ||||||||||||
1846 | - | |||||||||||||
1847 | Qt::FocusReason reason = e->reason(); | - | ||||||||||||
1848 | if (reason != Qt::ActiveWindowFocusReason &&
| 0 | ||||||||||||
1849 | reason != Qt::PopupFocusReason)
| 0 | ||||||||||||
1850 | deselect(); never executed: deselect(); | 0 | ||||||||||||
1851 | - | |||||||||||||
1852 | d->setCursorVisible(false); | - | ||||||||||||
1853 | d->control->setCursorBlinkPeriodsetBlinkingCursorEnabled(0false); | - | ||||||||||||
1854 | #ifdef QT_KEYPAD_NAVIGATION | - | ||||||||||||
1855 | // editingFinished() is already emitted on LeaveEditFocus | - | ||||||||||||
1856 | if (!QApplication::keypadNavigationEnabled()) | - | ||||||||||||
1857 | #endif | - | ||||||||||||
1858 | if (reason != Qt::PopupFocusReason
| 0 | ||||||||||||
1859 | || !(QApplication::activePopupWidget() && QApplication::activePopupWidget()->parentWidget() == this)) {
| 0 | ||||||||||||
1860 | if (hasAcceptableInput() || d->control->fixup())
| 0 | ||||||||||||
1861 | emit editingFinished(); never executed: editingFinished(); | 0 | ||||||||||||
1862 | } never executed: end of block | 0 | ||||||||||||
1863 | #ifdef Q_DEAD_CODE_FROM_QT4_MAC | - | ||||||||||||
1864 | if (d->control->echoMode() == Password || d->control->echoMode() == NoEcho) | - | ||||||||||||
1865 | qt_mac_secure_keyboard(false); | - | ||||||||||||
1866 | #endif | - | ||||||||||||
1867 | #ifdef QT_KEYPAD_NAVIGATION | - | ||||||||||||
1868 | d->control->setCancelText(QString()); | - | ||||||||||||
1869 | #endif | - | ||||||||||||
1870 | #ifndef QT_NO_COMPLETER | - | ||||||||||||
1871 | if (d->control->completer()) {
| 0 | ||||||||||||
1872 | QObject::disconnect(d->control->completer(), 0, this, 0); | - | ||||||||||||
1873 | } never executed: end of block | 0 | ||||||||||||
1874 | #endif | - | ||||||||||||
1875 | QWidget::focusOutEvent(e); | - | ||||||||||||
1876 | } never executed: end of block | 0 | ||||||||||||
1877 | - | |||||||||||||
1878 | /*!\reimp | - | ||||||||||||
1879 | */ | - | ||||||||||||
1880 | void QLineEdit::paintEvent(QPaintEvent *) | - | ||||||||||||
1881 | { | - | ||||||||||||
1882 | Q_D(QLineEdit); | - | ||||||||||||
1883 | QPainter p(this); | - | ||||||||||||
1884 | QPalette pal = palette(); | - | ||||||||||||
1885 | - | |||||||||||||
1886 | QStyleOptionFrame panel; | - | ||||||||||||
1887 | initStyleOption(&panel); | - | ||||||||||||
1888 | style()->drawPrimitive(QStyle::PE_PanelLineEdit, &panel, &p, this); | - | ||||||||||||
1889 | QRect r = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this); | - | ||||||||||||
1890 | r.setX(r.x() + d->effectiveLeftTextMargin()); | - | ||||||||||||
1891 | r.setY(r.y() + d->topTextMargin); | - | ||||||||||||
1892 | r.setRight(r.right() - d->effectiveRightTextMargin()); | - | ||||||||||||
1893 | r.setBottom(r.bottom() - d->bottomTextMargin); | - | ||||||||||||
1894 | p.setClipRect(r); | - | ||||||||||||
1895 | - | |||||||||||||
1896 | QFontMetrics fm = fontMetrics(); | - | ||||||||||||
1897 | Qt::Alignment va = QStyle::visualAlignment(d->control->layoutDirection(), QFlag(d->alignment)); | - | ||||||||||||
1898 | switch (va & Qt::AlignVertical_Mask) { | - | ||||||||||||
1899 | case Qt::AlignBottom: | - | ||||||||||||
1900 | d->vscroll = r.y() + r.height() - fm.height() - d->verticalMargin; | - | ||||||||||||
1901 | break; | - | ||||||||||||
1902 | case Qt::AlignTop: | - | ||||||||||||
1903 | d->vscroll = r.y() + d->verticalMargin; | - | ||||||||||||
1904 | break; | - | ||||||||||||
1905 | default: | - | ||||||||||||
1906 | //center | - | ||||||||||||
1907 | d->vscroll = r.y() + (r.height() - fm.height() + 1) / 2; | - | ||||||||||||
1908 | break; | - | ||||||||||||
1909 | } | - | ||||||||||||
1910 | QRect lineRect(r.x() + d->horizontalMargin, d->vscroll, r.width() - 2*d->horizontalMargin, fm.height()); | - | ||||||||||||
1911 | - | |||||||||||||
1912 | if (d->shouldShowPlaceholderText()) { | - | ||||||||||||
1913 | if (!d->placeholderText.isEmpty()) { | - | ||||||||||||
1914 | QColor col = pal.text().color(); | - | ||||||||||||
1915 | col.setAlpha(128); | - | ||||||||||||
1916 | QPen oldpen = p.pen(); | - | ||||||||||||
1917 | p.setPen(col); | - | ||||||||||||
1918 | QString elidedText = fm.elidedText(d->placeholderText, Qt::ElideRight, lineRect.width()); | - | ||||||||||||
1919 | p.drawText(lineRect, va, elidedText); | - | ||||||||||||
1920 | p.setPen(oldpen); | - | ||||||||||||
1921 | } | - | ||||||||||||
1922 | } | - | ||||||||||||
1923 | - | |||||||||||||
1924 | int cix = qRound(d->control->cursorToX()); | - | ||||||||||||
1925 | - | |||||||||||||
1926 | // horizontal scrolling. d->hscroll is the left indent from the beginning | - | ||||||||||||
1927 | // of the text line to the left edge of lineRect. we update this value | - | ||||||||||||
1928 | // depending on the delta from the last paint event; in effect this means | - | ||||||||||||
1929 | // the below code handles all scrolling based on the textline (widthUsed), | - | ||||||||||||
1930 | // the line edit rect (lineRect) and the cursor position (cix). | - | ||||||||||||
1931 | int widthUsed = qRound(d->control->naturalTextWidth()) + 1; | - | ||||||||||||
1932 | if (widthUsed <= lineRect.width()) { | - | ||||||||||||
1933 | // text fits in lineRect; use hscroll for alignment | - | ||||||||||||
1934 | switch (va & ~(Qt::AlignAbsolute|Qt::AlignVertical_Mask)) { | - | ||||||||||||
1935 | case Qt::AlignRight: | - | ||||||||||||
1936 | d->hscroll = widthUsed - lineRect.width() + 1; | - | ||||||||||||
1937 | break; | - | ||||||||||||
1938 | case Qt::AlignHCenter: | - | ||||||||||||
1939 | d->hscroll = (widthUsed - lineRect.width()) / 2; | - | ||||||||||||
1940 | break; | - | ||||||||||||
1941 | default: | - | ||||||||||||
1942 | // Left | - | ||||||||||||
1943 | d->hscroll = 0; | - | ||||||||||||
1944 | break; | - | ||||||||||||
1945 | } | - | ||||||||||||
1946 | } else if (cix - d->hscroll >= lineRect.width()) { | - | ||||||||||||
1947 | // text doesn't fit, cursor is to the right of lineRect (scroll right) | - | ||||||||||||
1948 | d->hscroll = cix - lineRect.width() + 1; | - | ||||||||||||
1949 | } else if (cix - d->hscroll < 0 && d->hscroll < widthUsed) { | - | ||||||||||||
1950 | // text doesn't fit, cursor is to the left of lineRect (scroll left) | - | ||||||||||||
1951 | d->hscroll = cix; | - | ||||||||||||
1952 | } else if (widthUsed - d->hscroll < lineRect.width()) { | - | ||||||||||||
1953 | // text doesn't fit, text document is to the left of lineRect; align | - | ||||||||||||
1954 | // right | - | ||||||||||||
1955 | d->hscroll = widthUsed - lineRect.width() + 1; | - | ||||||||||||
1956 | } else { | - | ||||||||||||
1957 | //in case the text is bigger than the lineedit, the hscroll can never be negative | - | ||||||||||||
1958 | d->hscroll = qMax(0, d->hscroll); | - | ||||||||||||
1959 | } | - | ||||||||||||
1960 | - | |||||||||||||
1961 | // the y offset is there to keep the baseline constant in case we have script changes in the text. | - | ||||||||||||
1962 | QPoint topLeft = lineRect.topLeft() - QPoint(d->hscroll, d->control->ascent() - fm.ascent()); | - | ||||||||||||
1963 | - | |||||||||||||
1964 | // draw text, selections and cursors | - | ||||||||||||
1965 | #ifndef QT_NO_STYLE_STYLESHEET | - | ||||||||||||
1966 | if (QStyleSheetStyle* cssStyle = qobject_cast<QStyleSheetStyle*>(style())) { | - | ||||||||||||
1967 | cssStyle->styleSheetPalette(this, &panel, &pal); | - | ||||||||||||
1968 | } | - | ||||||||||||
1969 | #endif | - | ||||||||||||
1970 | p.setPen(pal.text().color()); | - | ||||||||||||
1971 | - | |||||||||||||
1972 | int flags = QWidgetLineControl::DrawText; | - | ||||||||||||
1973 | - | |||||||||||||
1974 | #ifdef QT_KEYPAD_NAVIGATION | - | ||||||||||||
1975 | if (!QApplication::keypadNavigationEnabled() || hasEditFocus()) | - | ||||||||||||
1976 | #endif | - | ||||||||||||
1977 | if (d->control->hasSelectedText() || (d->cursorVisible && !d->control->inputMask().isEmpty() && !d->control->isReadOnly())){ | - | ||||||||||||
1978 | flags |= QWidgetLineControl::DrawSelections; | - | ||||||||||||
1979 | // Palette only used for selections/mask and may not be in sync | - | ||||||||||||
1980 | if (d->control->palette() != pal | - | ||||||||||||
1981 | || d->control->palette().currentColorGroup() != pal.currentColorGroup()) | - | ||||||||||||
1982 | d->control->setPalette(pal); | - | ||||||||||||
1983 | } | - | ||||||||||||
1984 | - | |||||||||||||
1985 | // Asian users see an IM selection text as cursor on candidate | - | ||||||||||||
1986 | // selection phase of input method, so the ordinary cursor should be | - | ||||||||||||
1987 | // invisible if we have a preedit string. | - | ||||||||||||
1988 | if (d->cursorVisible && !d->control->isReadOnly()) | - | ||||||||||||
1989 | flags |= QWidgetLineControl::DrawCursor; | - | ||||||||||||
1990 | - | |||||||||||||
1991 | d->control->setCursorWidth(style()->pixelMetric(QStyle::PM_TextCursorWidth)); | - | ||||||||||||
1992 | d->control->draw(&p, topLeft, r, flags); | - | ||||||||||||
1993 | - | |||||||||||||
1994 | } | - | ||||||||||||
1995 | - | |||||||||||||
1996 | - | |||||||||||||
1997 | #ifndef QT_NO_DRAGANDDROP | - | ||||||||||||
1998 | /*!\reimp | - | ||||||||||||
1999 | */ | - | ||||||||||||
2000 | void QLineEdit::dragMoveEvent(QDragMoveEvent *e) | - | ||||||||||||
2001 | { | - | ||||||||||||
2002 | Q_D(QLineEdit); | - | ||||||||||||
2003 | if (!d->control->isReadOnly() && e->mimeData()->hasFormat(QLatin1String("text/plain"))) { | - | ||||||||||||
2004 | e->acceptProposedAction(); | - | ||||||||||||
2005 | d->control->moveCursor(d->xToPos(e->pos().x()), false); | - | ||||||||||||
2006 | d->cursorVisible = true; | - | ||||||||||||
2007 | update(); | - | ||||||||||||
2008 | } | - | ||||||||||||
2009 | } | - | ||||||||||||
2010 | - | |||||||||||||
2011 | /*!\reimp */ | - | ||||||||||||
2012 | void QLineEdit::dragEnterEvent(QDragEnterEvent * e) | - | ||||||||||||
2013 | { | - | ||||||||||||
2014 | QLineEdit::dragMoveEvent(e); | - | ||||||||||||
2015 | } | - | ||||||||||||
2016 | - | |||||||||||||
2017 | /*!\reimp */ | - | ||||||||||||
2018 | void QLineEdit::dragLeaveEvent(QDragLeaveEvent *) | - | ||||||||||||
2019 | { | - | ||||||||||||
2020 | Q_D(QLineEdit); | - | ||||||||||||
2021 | if (d->cursorVisible) { | - | ||||||||||||
2022 | d->cursorVisible = false; | - | ||||||||||||
2023 | update(); | - | ||||||||||||
2024 | } | - | ||||||||||||
2025 | } | - | ||||||||||||
2026 | - | |||||||||||||
2027 | /*!\reimp */ | - | ||||||||||||
2028 | void QLineEdit::dropEvent(QDropEvent* e) | - | ||||||||||||
2029 | { | - | ||||||||||||
2030 | Q_D(QLineEdit); | - | ||||||||||||
2031 | QString str = e->mimeData()->text(); | - | ||||||||||||
2032 | - | |||||||||||||
2033 | if (!str.isNull() && !d->control->isReadOnly()) { | - | ||||||||||||
2034 | if (e->source() == this && e->dropAction() == Qt::CopyAction) | - | ||||||||||||
2035 | deselect(); | - | ||||||||||||
2036 | int cursorPos = d->xToPos(e->pos().x()); | - | ||||||||||||
2037 | int selStart = cursorPos; | - | ||||||||||||
2038 | int oldSelStart = d->control->selectionStart(); | - | ||||||||||||
2039 | int oldSelEnd = d->control->selectionEnd(); | - | ||||||||||||
2040 | d->control->moveCursor(cursorPos, false); | - | ||||||||||||
2041 | d->cursorVisible = false; | - | ||||||||||||
2042 | e->acceptProposedAction(); | - | ||||||||||||
2043 | insert(str); | - | ||||||||||||
2044 | if (e->source() == this) { | - | ||||||||||||
2045 | if (e->dropAction() == Qt::MoveAction) { | - | ||||||||||||
2046 | if (selStart > oldSelStart && selStart <= oldSelEnd) | - | ||||||||||||
2047 | setSelection(oldSelStart, str.length()); | - | ||||||||||||
2048 | else if (selStart > oldSelEnd) | - | ||||||||||||
2049 | setSelection(selStart - str.length(), str.length()); | - | ||||||||||||
2050 | else | - | ||||||||||||
2051 | setSelection(selStart, str.length()); | - | ||||||||||||
2052 | } else { | - | ||||||||||||
2053 | setSelection(selStart, str.length()); | - | ||||||||||||
2054 | } | - | ||||||||||||
2055 | } | - | ||||||||||||
2056 | } else { | - | ||||||||||||
2057 | e->ignore(); | - | ||||||||||||
2058 | update(); | - | ||||||||||||
2059 | } | - | ||||||||||||
2060 | } | - | ||||||||||||
2061 | - | |||||||||||||
2062 | #endif // QT_NO_DRAGANDDROP | - | ||||||||||||
2063 | - | |||||||||||||
2064 | #ifndef QT_NO_CONTEXTMENU | - | ||||||||||||
2065 | /*! | - | ||||||||||||
2066 | Shows the standard context menu created with | - | ||||||||||||
2067 | createStandardContextMenu(). | - | ||||||||||||
2068 | - | |||||||||||||
2069 | If you do not want the line edit to have a context menu, you can set | - | ||||||||||||
2070 | its \l contextMenuPolicy to Qt::NoContextMenu. If you want to | - | ||||||||||||
2071 | customize the context menu, reimplement this function. If you want | - | ||||||||||||
2072 | to extend the standard context menu, reimplement this function, call | - | ||||||||||||
2073 | createStandardContextMenu() and extend the menu returned. | - | ||||||||||||
2074 | - | |||||||||||||
2075 | \snippet code/src_gui_widgets_qlineedit.cpp 0 | - | ||||||||||||
2076 | - | |||||||||||||
2077 | The \a event parameter is used to obtain the position where | - | ||||||||||||
2078 | the mouse cursor was when the event was generated. | - | ||||||||||||
2079 | - | |||||||||||||
2080 | \sa setContextMenuPolicy() | - | ||||||||||||
2081 | */ | - | ||||||||||||
2082 | void QLineEdit::contextMenuEvent(QContextMenuEvent *event) | - | ||||||||||||
2083 | { | - | ||||||||||||
2084 | if (QMenu *menu = createStandardContextMenu()) { | - | ||||||||||||
2085 | menu->setAttribute(Qt::WA_DeleteOnClose); | - | ||||||||||||
2086 | menu->popup(event->globalPos()); | - | ||||||||||||
2087 | } | - | ||||||||||||
2088 | } | - | ||||||||||||
2089 | - | |||||||||||||
2090 | static inline void setActionIcon(QAction *action, const QString &name) | - | ||||||||||||
2091 | { | - | ||||||||||||
2092 | const QIcon icon = QIcon::fromTheme(name); | - | ||||||||||||
2093 | if (!icon.isNull()) | - | ||||||||||||
2094 | action->setIcon(icon); | - | ||||||||||||
2095 | } | - | ||||||||||||
2096 | - | |||||||||||||
2097 | /*! This function creates the standard context menu which is shown | - | ||||||||||||
2098 | when the user clicks on the line edit with the right mouse | - | ||||||||||||
2099 | button. It is called from the default contextMenuEvent() handler. | - | ||||||||||||
2100 | The popup menu's ownership is transferred to the caller. | - | ||||||||||||
2101 | */ | - | ||||||||||||
2102 | - | |||||||||||||
2103 | QMenu *QLineEdit::createStandardContextMenu() | - | ||||||||||||
2104 | { | - | ||||||||||||
2105 | Q_D(QLineEdit); | - | ||||||||||||
2106 | QMenu *popup = new QMenu(this); | - | ||||||||||||
2107 | popup->setObjectName(QLatin1String("qt_edit_menu")); | - | ||||||||||||
2108 | QAction *action = 0; | - | ||||||||||||
2109 | - | |||||||||||||
2110 | if (!isReadOnly()) { | - | ||||||||||||
2111 | action = popup->addAction(QLineEdit::tr("&Undo") + ACCEL_KEY(QKeySequence::Undo)); | - | ||||||||||||
2112 | action->setEnabled(d->control->isUndoAvailable()); | - | ||||||||||||
2113 | setActionIcon(action, QStringLiteral("edit-undo")); | - | ||||||||||||
2114 | connect(action, SIGNAL(triggered()), SLOT(undo())); | - | ||||||||||||
2115 | - | |||||||||||||
2116 | action = popup->addAction(QLineEdit::tr("&Redo") + ACCEL_KEY(QKeySequence::Redo)); | - | ||||||||||||
2117 | action->setEnabled(d->control->isRedoAvailable()); | - | ||||||||||||
2118 | setActionIcon(action, QStringLiteral("edit-redo")); | - | ||||||||||||
2119 | connect(action, SIGNAL(triggered()), SLOT(redo())); | - | ||||||||||||
2120 | - | |||||||||||||
2121 | popup->addSeparator(); | - | ||||||||||||
2122 | } | - | ||||||||||||
2123 | - | |||||||||||||
2124 | #ifndef QT_NO_CLIPBOARD | - | ||||||||||||
2125 | if (!isReadOnly()) { | - | ||||||||||||
2126 | action = popup->addAction(QLineEdit::tr("Cu&t") + ACCEL_KEY(QKeySequence::Cut)); | - | ||||||||||||
2127 | action->setEnabled(!d->control->isReadOnly() && d->control->hasSelectedText() | - | ||||||||||||
2128 | && d->control->echoMode() == QLineEdit::Normal); | - | ||||||||||||
2129 | setActionIcon(action, QStringLiteral("edit-cut")); | - | ||||||||||||
2130 | connect(action, SIGNAL(triggered()), SLOT(cut())); | - | ||||||||||||
2131 | } | - | ||||||||||||
2132 | - | |||||||||||||
2133 | action = popup->addAction(QLineEdit::tr("&Copy") + ACCEL_KEY(QKeySequence::Copy)); | - | ||||||||||||
2134 | action->setEnabled(d->control->hasSelectedText() | - | ||||||||||||
2135 | && d->control->echoMode() == QLineEdit::Normal); | - | ||||||||||||
2136 | setActionIcon(action, QStringLiteral("edit-copy")); | - | ||||||||||||
2137 | connect(action, SIGNAL(triggered()), SLOT(copy())); | - | ||||||||||||
2138 | - | |||||||||||||
2139 | if (!isReadOnly()) { | - | ||||||||||||
2140 | action = popup->addAction(QLineEdit::tr("&Paste") + ACCEL_KEY(QKeySequence::Paste)); | - | ||||||||||||
2141 | action->setEnabled(!d->control->isReadOnly() && !QApplication::clipboard()->text().isEmpty()); | - | ||||||||||||
2142 | setActionIcon(action, QStringLiteral("edit-paste")); | - | ||||||||||||
2143 | connect(action, SIGNAL(triggered()), SLOT(paste())); | - | ||||||||||||
2144 | } | - | ||||||||||||
2145 | #endif | - | ||||||||||||
2146 | - | |||||||||||||
2147 | if (!isReadOnly()) { | - | ||||||||||||
2148 | action = popup->addAction(QLineEdit::tr("Delete")); | - | ||||||||||||
2149 | action->setEnabled(!d->control->isReadOnly() && !d->control->text().isEmpty() && d->control->hasSelectedText()); | - | ||||||||||||
2150 | setActionIcon(action, QStringLiteral("edit-delete")); | - | ||||||||||||
2151 | connect(action, SIGNAL(triggered()), d->control, SLOT(_q_deleteSelected())); | - | ||||||||||||
2152 | } | - | ||||||||||||
2153 | - | |||||||||||||
2154 | if (!popup->isEmpty()) | - | ||||||||||||
2155 | popup->addSeparator(); | - | ||||||||||||
2156 | - | |||||||||||||
2157 | action = popup->addAction(QLineEdit::tr("Select All") + ACCEL_KEY(QKeySequence::SelectAll)); | - | ||||||||||||
2158 | action->setEnabled(!d->control->text().isEmpty() && !d->control->allSelected()); | - | ||||||||||||
2159 | d->selectAllAction = action; | - | ||||||||||||
2160 | connect(action, SIGNAL(triggered()), SLOT(selectAll())); | - | ||||||||||||
2161 | - | |||||||||||||
2162 | if (!d->control->isReadOnly() && QGuiApplication::styleHints()->useRtlExtensions()) { | - | ||||||||||||
2163 | popup->addSeparator(); | - | ||||||||||||
2164 | QUnicodeControlCharacterMenu *ctrlCharacterMenu = new QUnicodeControlCharacterMenu(this, popup); | - | ||||||||||||
2165 | popup->addMenu(ctrlCharacterMenu); | - | ||||||||||||
2166 | } | - | ||||||||||||
2167 | return popup; | - | ||||||||||||
2168 | } | - | ||||||||||||
2169 | #endif // QT_NO_CONTEXTMENU | - | ||||||||||||
2170 | - | |||||||||||||
2171 | /*! \reimp */ | - | ||||||||||||
2172 | void QLineEdit::changeEvent(QEvent *ev) | - | ||||||||||||
2173 | { | - | ||||||||||||
2174 | Q_D(QLineEdit); | - | ||||||||||||
2175 | switch(ev->type()) | - | ||||||||||||
2176 | { | - | ||||||||||||
2177 | case QEvent::ActivationChange: never executed: case QEvent::ActivationChange: | 0 | ||||||||||||
2178 | if (!palette().isEqual(QPalette::Active, QPalette::Inactive))
| 0 | ||||||||||||
2179 | update(); never executed: update(); | 0 | ||||||||||||
2180 | break; never executed: break; | 0 | ||||||||||||
2181 | case QEvent::FontChange: never executed: case QEvent::FontChange: | 0 | ||||||||||||
2182 | d->control->setFont(font()); | - | ||||||||||||
2183 | break; never executed: break; | 0 | ||||||||||||
2184 | case QEvent::StyleChange: never executed: case QEvent::StyleChange: | 0 | ||||||||||||
2185 | { | - | ||||||||||||
2186 | QStyleOptionFrame opt; | - | ||||||||||||
2187 | initStyleOption(&opt); | - | ||||||||||||
2188 | d->control->setPasswordCharacter(style()->styleHint(QStyle::SH_LineEdit_PasswordCharacter, &opt, this)); | - | ||||||||||||
2189 | d->control->setPasswordMaskDelay(style()->styleHint(QStyle::SH_LineEdit_PasswordMaskDelay, &opt, this)); | - | ||||||||||||
2190 | } | - | ||||||||||||
2191 | update(); | - | ||||||||||||
2192 | break; never executed: break; | 0 | ||||||||||||
2193 | case QEvent::LayoutDirectionChange: never executed: case QEvent::LayoutDirectionChange: | 0 | ||||||||||||
2194 | foreachfor (const QLineEditPrivate::SideWidgetEntryauto &e ,: d->trailingSideWidgets) { // Refresh icon to show arrow in right direction. | - | ||||||||||||
2195 | if (e.flags & QLineEditPrivate::SideWidgetClearButton)
| 0 | ||||||||||||
2196 | static_cast<QLineEditIconButton *>(e.widget)->setIcon(d->clearButtonIcon()); never executed: static_cast<QLineEditIconButton *>(e.widget)->setIcon(d->clearButtonIcon()); | 0 | ||||||||||||
2197 | } never executed: end of block | 0 | ||||||||||||
2198 | d->positionSideWidgets(); | - | ||||||||||||
2199 | break; never executed: break; | 0 | ||||||||||||
2200 | default: never executed: default: | 0 | ||||||||||||
2201 | break; never executed: break; | 0 | ||||||||||||
2202 | } | - | ||||||||||||
2203 | QWidget::changeEvent(ev); | - | ||||||||||||
2204 | } never executed: end of block | 0 | ||||||||||||
2205 | - | |||||||||||||
2206 | QT_END_NAMESPACE | - | ||||||||||||
2207 | - | |||||||||||||
2208 | #include "moc_qlineedit.cpp" | - | ||||||||||||
2209 | - | |||||||||||||
2210 | #endif // QT_NO_LINEEDIT | - | ||||||||||||
Source code | Switch to Preprocessed file |