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