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