|
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 |
|
47 #include "qabstractitemview.h" |
|
48 #include "qclipboard.h" |
|
49 #ifndef QT_NO_ACCESSIBILITY |
|
50 #include "qaccessible.h" |
|
51 #endif |
|
52 #ifndef QT_NO_IM |
|
53 #include "qinputcontext.h" |
|
54 #include "qlist.h" |
|
55 #endif |
|
56 |
|
57 QT_BEGIN_NAMESPACE |
|
58 |
|
59 const int QLineEditPrivate::verticalMargin(1); |
|
60 const int QLineEditPrivate::horizontalMargin(2); |
|
61 |
|
62 int QLineEditPrivate::xToPos(int x, QTextLine::CursorPosition betweenOrOn) const |
|
63 { |
|
64 QRect cr = adjustedContentsRect(); |
|
65 x-= cr.x() - hscroll + horizontalMargin; |
|
66 return control->xToPos(x, betweenOrOn); |
|
67 } |
|
68 |
|
69 QRect QLineEditPrivate::cursorRect() const |
|
70 { |
|
71 QRect cr = adjustedContentsRect(); |
|
72 int cix = cr.x() - hscroll + horizontalMargin; |
|
73 QRect crect = control->cursorRect(); |
|
74 crect.moveTo(crect.topLeft() + QPoint(cix, vscroll)); |
|
75 return crect; |
|
76 } |
|
77 |
|
78 #ifndef QT_NO_COMPLETER |
|
79 |
|
80 void QLineEditPrivate::_q_completionHighlighted(QString newText) |
|
81 { |
|
82 Q_Q(QLineEdit); |
|
83 if (control->completer()->completionMode() != QCompleter::InlineCompletion) { |
|
84 q->setText(newText); |
|
85 } else { |
|
86 int c = control->cursor(); |
|
87 QString text = control->text(); |
|
88 q->setText(text.left(c) + newText.mid(c)); |
|
89 control->moveCursor(control->end(), false); |
|
90 control->moveCursor(c, true); |
|
91 } |
|
92 } |
|
93 |
|
94 #endif // QT_NO_COMPLETER |
|
95 |
|
96 void QLineEditPrivate::_q_handleWindowActivate() |
|
97 { |
|
98 Q_Q(QLineEdit); |
|
99 if (!q->hasFocus() && control->hasSelectedText()) |
|
100 control->deselect(); |
|
101 } |
|
102 |
|
103 void QLineEditPrivate::_q_textEdited(const QString &text) |
|
104 { |
|
105 Q_Q(QLineEdit); |
|
106 #ifndef QT_NO_COMPLETER |
|
107 if (control->completer() && |
|
108 control->completer()->completionMode() != QCompleter::InlineCompletion) |
|
109 control->complete(-1); // update the popup on cut/paste/del |
|
110 #endif |
|
111 emit q->textEdited(text); |
|
112 } |
|
113 |
|
114 void QLineEditPrivate::_q_cursorPositionChanged(int from, int to) |
|
115 { |
|
116 Q_Q(QLineEdit); |
|
117 q->update(); |
|
118 emit q->cursorPositionChanged(from, to); |
|
119 } |
|
120 |
|
121 #ifdef QT_KEYPAD_NAVIGATION |
|
122 void QLineEditPrivate::_q_editFocusChange(bool e) |
|
123 { |
|
124 Q_Q(QLineEdit); |
|
125 q->setEditFocus(e); |
|
126 } |
|
127 #endif |
|
128 |
|
129 void QLineEditPrivate::init(const QString& txt) |
|
130 { |
|
131 Q_Q(QLineEdit); |
|
132 control = new QLineControl(txt); |
|
133 control->setFont(q->font()); |
|
134 QObject::connect(control, SIGNAL(textChanged(const QString &)), |
|
135 q, SIGNAL(textChanged(const QString &))); |
|
136 QObject::connect(control, SIGNAL(textEdited(const QString &)), |
|
137 q, SLOT(_q_textEdited(const QString &))); |
|
138 QObject::connect(control, SIGNAL(cursorPositionChanged(int, int)), |
|
139 q, SLOT(_q_cursorPositionChanged(int, int))); |
|
140 QObject::connect(control, SIGNAL(selectionChanged()), |
|
141 q, SIGNAL(selectionChanged())); |
|
142 QObject::connect(control, SIGNAL(accepted()), |
|
143 q, SIGNAL(returnPressed())); |
|
144 QObject::connect(control, SIGNAL(editingFinished()), |
|
145 q, SIGNAL(editingFinished())); |
|
146 #ifdef QT_KEYPAD_NAVIGATION |
|
147 QObject::connect(control, SIGNAL(editFocusChange(bool)), |
|
148 q, SLOT(_q_editFocusChange(bool))); |
|
149 #endif |
|
150 QObject::connect(control, SIGNAL(cursorPositionChanged(int, int)), |
|
151 q, SLOT(updateMicroFocus())); |
|
152 |
|
153 // for now, going completely overboard with updates. |
|
154 QObject::connect(control, SIGNAL(selectionChanged()), |
|
155 q, SLOT(update())); |
|
156 |
|
157 QObject::connect(control, SIGNAL(displayTextChanged(const QString &)), |
|
158 q, SLOT(update())); |
|
159 |
|
160 QObject::connect(control, SIGNAL(updateNeeded(const QRect &)), |
|
161 q, SLOT(update())); |
|
162 |
|
163 QStyleOptionFrameV2 opt; |
|
164 q->initStyleOption(&opt); |
|
165 control->setPasswordCharacter(q->style()->styleHint(QStyle::SH_LineEdit_PasswordCharacter, &opt, q)); |
|
166 #ifndef QT_NO_CURSOR |
|
167 q->setCursor(Qt::IBeamCursor); |
|
168 #endif |
|
169 q->setFocusPolicy(Qt::StrongFocus); |
|
170 q->setAttribute(Qt::WA_InputMethodEnabled); |
|
171 // Specifies that this widget can use more, but is able to survive on |
|
172 // less, horizontal space; and is fixed vertically. |
|
173 q->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed, QSizePolicy::LineEdit)); |
|
174 q->setBackgroundRole(QPalette::Base); |
|
175 q->setAttribute(Qt::WA_KeyCompression); |
|
176 q->setMouseTracking(true); |
|
177 q->setAcceptDrops(true); |
|
178 |
|
179 q->setAttribute(Qt::WA_MacShowFocusRect); |
|
180 } |
|
181 |
|
182 QRect QLineEditPrivate::adjustedContentsRect() const |
|
183 { |
|
184 Q_Q(const QLineEdit); |
|
185 QStyleOptionFrameV2 opt; |
|
186 q->initStyleOption(&opt); |
|
187 QRect r = q->style()->subElementRect(QStyle::SE_LineEditContents, &opt, q); |
|
188 r.setX(r.x() + leftTextMargin); |
|
189 r.setY(r.y() + topTextMargin); |
|
190 r.setRight(r.right() - rightTextMargin); |
|
191 r.setBottom(r.bottom() - bottomTextMargin); |
|
192 return r; |
|
193 } |
|
194 |
|
195 void QLineEditPrivate::setCursorVisible(bool visible) |
|
196 { |
|
197 Q_Q(QLineEdit); |
|
198 if ((bool)cursorVisible == visible) |
|
199 return; |
|
200 cursorVisible = visible; |
|
201 QRect r = cursorRect(); |
|
202 if (control->inputMask().isEmpty()) |
|
203 q->update(r); |
|
204 else |
|
205 q->update(); |
|
206 } |
|
207 |
|
208 void QLineEditPrivate::updatePasswordEchoEditing(bool editing) |
|
209 { |
|
210 Q_Q(QLineEdit); |
|
211 control->updatePasswordEchoEditing(editing); |
|
212 q->setAttribute(Qt::WA_InputMethodEnabled, shouldEnableInputMethod()); |
|
213 } |
|
214 |
|
215 /*! |
|
216 This function is not intended as polymorphic usage. Just a shared code |
|
217 fragment that calls QInputContext::mouseHandler for this |
|
218 class. |
|
219 */ |
|
220 bool QLineEditPrivate::sendMouseEventToInputContext( QMouseEvent *e ) |
|
221 { |
|
222 #if !defined QT_NO_IM |
|
223 Q_Q(QLineEdit); |
|
224 if ( control->composeMode() ) { |
|
225 int tmp_cursor = xToPos(e->pos().x()); |
|
226 int mousePos = tmp_cursor - control->cursor(); |
|
227 if ( mousePos < 0 || mousePos > control->preeditAreaText().length() ) { |
|
228 mousePos = -1; |
|
229 // don't send move events outside the preedit area |
|
230 if ( e->type() == QEvent::MouseMove ) |
|
231 return true; |
|
232 } |
|
233 |
|
234 QInputContext *qic = q->inputContext(); |
|
235 if ( qic ) |
|
236 // may be causing reset() in some input methods |
|
237 qic->mouseHandler(mousePos, e); |
|
238 if (!control->preeditAreaText().isEmpty()) |
|
239 return true; |
|
240 } |
|
241 #else |
|
242 Q_UNUSED(e); |
|
243 #endif |
|
244 |
|
245 return false; |
|
246 } |
|
247 |
|
248 #ifndef QT_NO_DRAGANDDROP |
|
249 void QLineEditPrivate::drag() |
|
250 { |
|
251 Q_Q(QLineEdit); |
|
252 dndTimer.stop(); |
|
253 QMimeData *data = new QMimeData; |
|
254 data->setText(control->selectedText()); |
|
255 QDrag *drag = new QDrag(q); |
|
256 drag->setMimeData(data); |
|
257 Qt::DropAction action = drag->start(); |
|
258 if (action == Qt::MoveAction && !control->isReadOnly() && drag->target() != q) |
|
259 control->removeSelection(); |
|
260 } |
|
261 |
|
262 #endif // QT_NO_DRAGANDDROP |
|
263 |
|
264 QT_END_NAMESPACE |
|
265 |
|
266 #endif |