Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/widgets/qlineedit_p.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||
16 | ** | - | ||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||
24 | ** | - | ||||||||||||
25 | ** GNU General Public License Usage | - | ||||||||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
35 | ** | - | ||||||||||||
36 | ** $QT_END_LICENSE$ | - | ||||||||||||
37 | ** | - | ||||||||||||
38 | ****************************************************************************/ | - | ||||||||||||
39 | - | |||||||||||||
40 | #include "qlineedit.h" | - | ||||||||||||
41 | #include "qlineedit_p.h" | - | ||||||||||||
42 | - | |||||||||||||
43 | #ifndef QT_NO_LINEEDIT | - | ||||||||||||
44 | - | |||||||||||||
45 | #include "qvariant.h" | - | ||||||||||||
46 | #include "qabstractitemview.h" | - | ||||||||||||
47 | #include "qdrag.h" | - | ||||||||||||
48 | #include "qwidgetaction.h" | - | ||||||||||||
49 | #include "qclipboard.h" | - | ||||||||||||
50 | #ifndef QT_NO_ACCESSIBILITY | - | ||||||||||||
51 | #include "qaccessible.h" | - | ||||||||||||
52 | #endif | - | ||||||||||||
53 | #ifndef QT_NO_IM | - | ||||||||||||
54 | #include "qinputmethod.h" | - | ||||||||||||
55 | #include "qlist.h" | - | ||||||||||||
56 | #endif | - | ||||||||||||
57 | #include <qpropertyanimation.h> | - | ||||||||||||
58 | - | |||||||||||||
59 | QT_BEGIN_NAMESPACE | - | ||||||||||||
60 | - | |||||||||||||
61 | const int QLineEditPrivate::verticalMargin(1); | - | ||||||||||||
62 | const int QLineEditPrivate::horizontalMargin(2); | - | ||||||||||||
63 | - | |||||||||||||
64 | QRect QLineEditPrivate::adjustedControlRect(const QRect &rect) const | - | ||||||||||||
65 | { | - | ||||||||||||
66 | QRect widgetRect = !rect.isEmpty() ? rect : q_func()->rect(); | - | ||||||||||||
67 | QRect cr = adjustedContentsRect(); | - | ||||||||||||
68 | int cix = cr.x() - hscroll + horizontalMargin; | - | ||||||||||||
69 | return widgetRect.translated(QPoint(cix, vscroll)); | - | ||||||||||||
70 | } | - | ||||||||||||
71 | - | |||||||||||||
72 | int QLineEditPrivate::xToPos(int x, QTextLine::CursorPosition betweenOrOn) const | - | ||||||||||||
73 | { | - | ||||||||||||
74 | QRect cr = adjustedContentsRect(); | - | ||||||||||||
75 | x-= cr.x() - hscroll + horizontalMargin; | - | ||||||||||||
76 | return control->xToPos(x, betweenOrOn); | - | ||||||||||||
77 | } | - | ||||||||||||
78 | - | |||||||||||||
79 | bool QLineEditPrivate::inSelection(int x) const | - | ||||||||||||
80 | { | - | ||||||||||||
81 | x -= adjustedContentsRect().x() - hscroll + horizontalMargin; | - | ||||||||||||
82 | return control->inSelection(x); | - | ||||||||||||
83 | } | - | ||||||||||||
84 | - | |||||||||||||
85 | QRect QLineEditPrivate::cursorRect() const | - | ||||||||||||
86 | { | - | ||||||||||||
87 | return adjustedControlRect(control->cursorRect()); | - | ||||||||||||
88 | } | - | ||||||||||||
89 | - | |||||||||||||
90 | #ifndef QT_NO_COMPLETER | - | ||||||||||||
91 | - | |||||||||||||
92 | void QLineEditPrivate::_q_completionHighlighted(const QString &newText) | - | ||||||||||||
93 | { | - | ||||||||||||
94 | Q_Q(QLineEdit); | - | ||||||||||||
95 | if (control->completer()->completionMode() != QCompleter::InlineCompletion) {
| 0 | ||||||||||||
96 | q->setText(newText); | - | ||||||||||||
97 | } else { never executed: end of block | 0 | ||||||||||||
98 | int c = control->cursor(); | - | ||||||||||||
99 | QString text = control->text(); | - | ||||||||||||
100 | q->setText(text.leftleftRef(c) + newText.midmidRef(c)); | - | ||||||||||||
101 | control->moveCursor(control->end(), false); | - | ||||||||||||
102 | #ifndef Q_OS_ANDROID | - | ||||||||||||
103 | const bool mark = true; | - | ||||||||||||
104 | #else | - | ||||||||||||
105 | const bool mark = (imHints & Qt::ImhNoPredictiveText); | - | ||||||||||||
106 | #endif | - | ||||||||||||
107 | control->moveCursor(c, mark); | - | ||||||||||||
108 | } never executed: end of block | 0 | ||||||||||||
109 | } | - | ||||||||||||
110 | - | |||||||||||||
111 | #endif // QT_NO_COMPLETER | - | ||||||||||||
112 | - | |||||||||||||
113 | void QLineEditPrivate::_q_handleWindowActivate() | - | ||||||||||||
114 | { | - | ||||||||||||
115 | Q_Q(QLineEdit); | - | ||||||||||||
116 | if (!q->hasFocus() && control->hasSelectedText()) | - | ||||||||||||
117 | control->deselect(); | - | ||||||||||||
118 | } | - | ||||||||||||
119 | - | |||||||||||||
120 | void QLineEditPrivate::_q_textEdited(const QString &text) | - | ||||||||||||
121 | { | - | ||||||||||||
122 | Q_Q(QLineEdit); | - | ||||||||||||
123 | emit q->textEdited(text); | - | ||||||||||||
124 | #ifndef QT_NO_COMPLETER | - | ||||||||||||
125 | if (control->completer() | - | ||||||||||||
126 | && control->completer()->completionMode() != QCompleter::InlineCompletion) | - | ||||||||||||
127 | control->complete(-1); // update the popup on cut/paste/del | - | ||||||||||||
128 | #endif | - | ||||||||||||
129 | } | - | ||||||||||||
130 | - | |||||||||||||
131 | void QLineEditPrivate::_q_cursorPositionChanged(int from, int to) | - | ||||||||||||
132 | { | - | ||||||||||||
133 | Q_Q(QLineEdit); | - | ||||||||||||
134 | q->update(); | - | ||||||||||||
135 | emit q->cursorPositionChanged(from, to); | - | ||||||||||||
136 | } | - | ||||||||||||
137 | - | |||||||||||||
138 | #ifdef QT_KEYPAD_NAVIGATION | - | ||||||||||||
139 | void QLineEditPrivate::_q_editFocusChange(bool e) | - | ||||||||||||
140 | { | - | ||||||||||||
141 | Q_Q(QLineEdit); | - | ||||||||||||
142 | q->setEditFocus(e); | - | ||||||||||||
143 | } | - | ||||||||||||
144 | #endif | - | ||||||||||||
145 | - | |||||||||||||
146 | void QLineEditPrivate::_q_selectionChanged() | - | ||||||||||||
147 | { | - | ||||||||||||
148 | Q_Q(QLineEdit); | - | ||||||||||||
149 | if (control->preeditAreaText().isEmpty()) { | - | ||||||||||||
150 | QStyleOptionFrame opt; | - | ||||||||||||
151 | q->initStyleOption(&opt); | - | ||||||||||||
152 | bool showCursor = control->hasSelectedText() ? | - | ||||||||||||
153 | q->style()->styleHint(QStyle::SH_BlinkCursorWhenTextSelected, &opt, q): | - | ||||||||||||
154 | q->hasFocus(); | - | ||||||||||||
155 | setCursorVisible(showCursor); | - | ||||||||||||
156 | } | - | ||||||||||||
157 | - | |||||||||||||
158 | emit q->selectionChanged(); | - | ||||||||||||
159 | #ifndef QT_NO_ACCESSIBILITY | - | ||||||||||||
160 | QAccessibleTextSelectionEvent ev(q, control->selectionStart(), control->selectionEnd()); | - | ||||||||||||
161 | ev.setCursorPosition(control->cursorPosition()); | - | ||||||||||||
162 | QAccessible::updateAccessibility(&ev); | - | ||||||||||||
163 | #endif | - | ||||||||||||
164 | } | - | ||||||||||||
165 | - | |||||||||||||
166 | void QLineEditPrivate::_q_updateNeeded(const QRect &rect) | - | ||||||||||||
167 | { | - | ||||||||||||
168 | q_func()->update(adjustedControlRect(rect)); | - | ||||||||||||
169 | } | - | ||||||||||||
170 | - | |||||||||||||
171 | void QLineEditPrivate::init(const QString& txt) | - | ||||||||||||
172 | { | - | ||||||||||||
173 | Q_Q(QLineEdit); | - | ||||||||||||
174 | control = new QWidgetLineControl(txt); | - | ||||||||||||
175 | control->setParent(q); | - | ||||||||||||
176 | control->setFont(q->font()); | - | ||||||||||||
177 | QObject::connect(control, SIGNAL(textChanged(QString)), | - | ||||||||||||
178 | q, SIGNAL(textChanged(QString))); | - | ||||||||||||
179 | QObject::connect(control, SIGNAL(textEdited(QString)), | - | ||||||||||||
180 | q, SLOT(_q_textEdited(QString))); | - | ||||||||||||
181 | QObject::connect(control, SIGNAL(cursorPositionChanged(int,int)), | - | ||||||||||||
182 | q, SLOT(_q_cursorPositionChanged(int,int))); | - | ||||||||||||
183 | QObject::connect(control, SIGNAL(selectionChanged()), | - | ||||||||||||
184 | q, SLOT(_q_selectionChanged())); | - | ||||||||||||
185 | QObject::connect(control, SIGNAL(accepted()), | - | ||||||||||||
186 | q, SIGNAL(returnPressed())); | - | ||||||||||||
187 | QObject::connect(control, SIGNAL(editingFinished()), | - | ||||||||||||
188 | q, SIGNAL(editingFinished())); | - | ||||||||||||
189 | #ifdef QT_KEYPAD_NAVIGATION | - | ||||||||||||
190 | QObject::connect(control, SIGNAL(editFocusChange(bool)), | - | ||||||||||||
191 | q, SLOT(_q_editFocusChange(bool))); | - | ||||||||||||
192 | #endif | - | ||||||||||||
193 | QObject::connect(control, SIGNAL(cursorPositionChanged(int,int)), | - | ||||||||||||
194 | q, SLOT(updateMicroFocus())); | - | ||||||||||||
195 | - | |||||||||||||
196 | QObject::connect(control, SIGNAL(textChanged(QString)), | - | ||||||||||||
197 | q, SLOT(updateMicroFocus())); | - | ||||||||||||
198 | - | |||||||||||||
199 | // for now, going completely overboard with updates. | - | ||||||||||||
200 | QObject::connect(control, SIGNAL(selectionChanged()), | - | ||||||||||||
201 | q, SLOT(update())); | - | ||||||||||||
202 | - | |||||||||||||
203 | QObject::connect(control, SIGNAL(selectionChanged()), | - | ||||||||||||
204 | q, SLOT(updateMicroFocus())); | - | ||||||||||||
205 | - | |||||||||||||
206 | QObject::connect(control, SIGNAL(displayTextChanged(QString)), | - | ||||||||||||
207 | q, SLOT(update())); | - | ||||||||||||
208 | - | |||||||||||||
209 | QObject::connect(control, SIGNAL(updateNeeded(QRect)), | - | ||||||||||||
210 | q, SLOT(_q_updateNeeded(QRect))); | - | ||||||||||||
211 | - | |||||||||||||
212 | QStyleOptionFrame opt; | - | ||||||||||||
213 | q->initStyleOption(&opt); | - | ||||||||||||
214 | control->setPasswordCharacter(q->style()->styleHint(QStyle::SH_LineEdit_PasswordCharacter, &opt, q)); | - | ||||||||||||
215 | control->setPasswordMaskDelay(q->style()->styleHint(QStyle::SH_LineEdit_PasswordMaskDelay, &opt, q)); | - | ||||||||||||
216 | #ifndef QT_NO_CURSOR | - | ||||||||||||
217 | q->setCursor(Qt::IBeamCursor); | - | ||||||||||||
218 | #endif | - | ||||||||||||
219 | q->setFocusPolicy(Qt::StrongFocus); | - | ||||||||||||
220 | q->setAttribute(Qt::WA_InputMethodEnabled); | - | ||||||||||||
221 | // Specifies that this widget can use more, but is able to survive on | - | ||||||||||||
222 | // less, horizontal space; and is fixed vertically. | - | ||||||||||||
223 | q->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed, QSizePolicy::LineEdit)); | - | ||||||||||||
224 | q->setBackgroundRole(QPalette::Base); | - | ||||||||||||
225 | q->setAttribute(Qt::WA_KeyCompression); | - | ||||||||||||
226 | q->setMouseTracking(true); | - | ||||||||||||
227 | q->setAcceptDrops(true); | - | ||||||||||||
228 | - | |||||||||||||
229 | q->setAttribute(Qt::WA_MacShowFocusRect); | - | ||||||||||||
230 | } | - | ||||||||||||
231 | - | |||||||||||||
232 | QRect QLineEditPrivate::adjustedContentsRect() const | - | ||||||||||||
233 | { | - | ||||||||||||
234 | Q_Q(const QLineEdit); | - | ||||||||||||
235 | QStyleOptionFrame opt; | - | ||||||||||||
236 | q->initStyleOption(&opt); | - | ||||||||||||
237 | QRect r = q->style()->subElementRect(QStyle::SE_LineEditContents, &opt, q); | - | ||||||||||||
238 | r.setX(r.x() + effectiveLeftTextMargin()); | - | ||||||||||||
239 | r.setY(r.y() + topTextMargin); | - | ||||||||||||
240 | r.setRight(r.right() - effectiveRightTextMargin()); | - | ||||||||||||
241 | r.setBottom(r.bottom() - bottomTextMargin); | - | ||||||||||||
242 | return r; | - | ||||||||||||
243 | } | - | ||||||||||||
244 | - | |||||||||||||
245 | void QLineEditPrivate::setCursorVisible(bool visible) | - | ||||||||||||
246 | { | - | ||||||||||||
247 | Q_Q(QLineEdit); | - | ||||||||||||
248 | if ((bool)cursorVisible == visible) | - | ||||||||||||
249 | return; | - | ||||||||||||
250 | cursorVisible = visible; | - | ||||||||||||
251 | if (control->inputMask().isEmpty()) | - | ||||||||||||
252 | q->update(cursorRect()); | - | ||||||||||||
253 | else | - | ||||||||||||
254 | q->update(); | - | ||||||||||||
255 | } | - | ||||||||||||
256 | - | |||||||||||||
257 | void QLineEditPrivate::updatePasswordEchoEditing(bool editing) | - | ||||||||||||
258 | { | - | ||||||||||||
259 | Q_Q(QLineEdit); | - | ||||||||||||
260 | control->updatePasswordEchoEditing(editing); | - | ||||||||||||
261 | q->setAttribute(Qt::WA_InputMethodEnabled, shouldEnableInputMethod()); | - | ||||||||||||
262 | } | - | ||||||||||||
263 | - | |||||||||||||
264 | void QLineEditPrivate::resetInputMethod() | - | ||||||||||||
265 | { | - | ||||||||||||
266 | Q_Q(QLineEdit); | - | ||||||||||||
267 | if (q->hasFocus() && qApp) { | - | ||||||||||||
268 | QGuiApplication::inputMethod()->reset(); | - | ||||||||||||
269 | } | - | ||||||||||||
270 | } | - | ||||||||||||
271 | - | |||||||||||||
272 | /*! | - | ||||||||||||
273 | This function is not intended as polymorphic usage. Just a shared code | - | ||||||||||||
274 | fragment that calls QInputMethod::invokeAction for this | - | ||||||||||||
275 | class. | - | ||||||||||||
276 | */ | - | ||||||||||||
277 | bool QLineEditPrivate::sendMouseEventToInputContext( QMouseEvent *e ) | - | ||||||||||||
278 | { | - | ||||||||||||
279 | #if !defined QT_NO_IM | - | ||||||||||||
280 | if ( control->composeMode() ) { | - | ||||||||||||
281 | int tmp_cursor = xToPos(e->pos().x()); | - | ||||||||||||
282 | int mousePos = tmp_cursor - control->cursor(); | - | ||||||||||||
283 | if ( mousePos < 0 || mousePos > control->preeditAreaText().length() ) | - | ||||||||||||
284 | mousePos = -1; | - | ||||||||||||
285 | - | |||||||||||||
286 | if (mousePos >= 0) { | - | ||||||||||||
287 | if (e->type() == QEvent::MouseButtonRelease) | - | ||||||||||||
288 | QGuiApplication::inputMethod()->invokeAction(QInputMethod::Click, mousePos); | - | ||||||||||||
289 | - | |||||||||||||
290 | return true; | - | ||||||||||||
291 | } | - | ||||||||||||
292 | } | - | ||||||||||||
293 | #else | - | ||||||||||||
294 | Q_UNUSED(e); | - | ||||||||||||
295 | #endif | - | ||||||||||||
296 | - | |||||||||||||
297 | return false; | - | ||||||||||||
298 | } | - | ||||||||||||
299 | - | |||||||||||||
300 | #ifndef QT_NO_DRAGANDDROP | - | ||||||||||||
301 | void QLineEditPrivate::drag() | - | ||||||||||||
302 | { | - | ||||||||||||
303 | Q_Q(QLineEdit); | - | ||||||||||||
304 | dndTimer.stop(); | - | ||||||||||||
305 | QMimeData *data = new QMimeData; | - | ||||||||||||
306 | data->setText(control->selectedText()); | - | ||||||||||||
307 | QDrag *drag = new QDrag(q); | - | ||||||||||||
308 | drag->setMimeData(data); | - | ||||||||||||
309 | Qt::DropAction action = drag->start(); | - | ||||||||||||
310 | if (action == Qt::MoveAction && !control->isReadOnly() && drag->target() != q) | - | ||||||||||||
311 | control->removeSelection(); | - | ||||||||||||
312 | } | - | ||||||||||||
313 | - | |||||||||||||
314 | #endif // QT_NO_DRAGANDDROP | - | ||||||||||||
315 | - | |||||||||||||
316 | QLineEditIconButton::QLineEditIconButton(QWidget *parent) | - | ||||||||||||
317 | : QToolButton(parent) | - | ||||||||||||
318 | , m_opacity(0) | - | ||||||||||||
319 | { | - | ||||||||||||
320 | setFocusPolicy(Qt::NoFocus); | - | ||||||||||||
321 | } | - | ||||||||||||
322 | - | |||||||||||||
323 | QLineEditPrivate *QLineEditIconButton::lineEditPrivate() const | - | ||||||||||||
324 | { | - | ||||||||||||
325 | QLineEdit *le = qobject_cast<QLineEdit *>(parentWidget()); | - | ||||||||||||
326 | return le ? static_cast<QLineEditPrivate *>(qt_widget_private(le)) : Q_NULLPTR; | - | ||||||||||||
327 | } | - | ||||||||||||
328 | - | |||||||||||||
329 | void QLineEditIconButton::paintEvent(QPaintEvent *) | - | ||||||||||||
330 | { | - | ||||||||||||
331 | QPainter painter(this); | - | ||||||||||||
332 | QWindow *window = Q_NULLPTR; | - | ||||||||||||
333 | if (const QWidget *nativeParent = nativeParentWidget()) | - | ||||||||||||
334 | window = nativeParent->windowHandle(); | - | ||||||||||||
335 | // Note isDown should really use the active state but in most styles | - | ||||||||||||
336 | // this has no proper feedback | - | ||||||||||||
337 | QIcon::Mode state = QIcon::Disabled; | - | ||||||||||||
338 | if (isEnabled()) | - | ||||||||||||
339 | state = isDown() ? QIcon::Selected : QIcon::Normal; | - | ||||||||||||
340 | const QLineEditPrivate *lep = lineEditPrivate(); | - | ||||||||||||
341 | const int iconWidth = lep ? lep->sideWidgetParameters().iconSize : 16; | - | ||||||||||||
342 | const QSize iconSize(iconWidth, iconWidth); | - | ||||||||||||
343 | const QPixmap iconPixmap = icon().pixmap(window, iconSize, state, QIcon::Off); | - | ||||||||||||
344 | QRect pixmapRect = QRect(QPoint(0, 0), iconSize); | - | ||||||||||||
345 | pixmapRect.moveCenter(rect().center()); | - | ||||||||||||
346 | painter.setOpacity(m_opacity); | - | ||||||||||||
347 | painter.drawPixmap(pixmapRect, iconPixmap); | - | ||||||||||||
348 | } | - | ||||||||||||
349 | - | |||||||||||||
350 | void QLineEditIconButton::actionEvent(QActionEvent *e) | - | ||||||||||||
351 | { | - | ||||||||||||
352 | switch (e->type()) { | - | ||||||||||||
353 | case QEvent::ActionChanged: { | - | ||||||||||||
354 | const QAction *action = e->action(); | - | ||||||||||||
355 | if (isVisibleTo(parentWidget()) != action->isVisible()) { | - | ||||||||||||
356 | setVisible(action->isVisible()); | - | ||||||||||||
357 | if (QLineEditPrivate *lep = lineEditPrivate()) | - | ||||||||||||
358 | lep->positionSideWidgets(); | - | ||||||||||||
359 | } | - | ||||||||||||
360 | } | - | ||||||||||||
361 | break; | - | ||||||||||||
362 | default: | - | ||||||||||||
363 | break; | - | ||||||||||||
364 | } | - | ||||||||||||
365 | QToolButton::actionEvent(e); | - | ||||||||||||
366 | } | - | ||||||||||||
367 | - | |||||||||||||
368 | void QLineEditIconButton::setOpacity(qreal value) | - | ||||||||||||
369 | { | - | ||||||||||||
370 | if (!qFuzzyCompare(m_opacity, value)) { | - | ||||||||||||
371 | m_opacity = value; | - | ||||||||||||
372 | updateCursor(); | - | ||||||||||||
373 | update(); | - | ||||||||||||
374 | } | - | ||||||||||||
375 | } | - | ||||||||||||
376 | - | |||||||||||||
377 | #ifndef QT_NO_ANIMATION | - | ||||||||||||
378 | void QLineEditIconButton::startOpacityAnimation(qreal endValue) | - | ||||||||||||
379 | { | - | ||||||||||||
380 | QPropertyAnimation *animation = new QPropertyAnimation(this, QByteArrayLiteral("opacity")); | - | ||||||||||||
381 | animation->setDuration(160); | - | ||||||||||||
382 | animation->setEndValue(endValue); | - | ||||||||||||
383 | animation->start(QAbstractAnimation::DeleteWhenStopped); | - | ||||||||||||
384 | } | - | ||||||||||||
385 | #endif | - | ||||||||||||
386 | - | |||||||||||||
387 | void QLineEditIconButton::updateCursor() | - | ||||||||||||
388 | { | - | ||||||||||||
389 | #ifndef QT_NO_CURSOR | - | ||||||||||||
390 | setCursor(qFuzzyCompare(m_opacity, qreal(1.0)) || !parentWidget() ? QCursor(Qt::ArrowCursor) : parentWidget()->cursor()); | - | ||||||||||||
391 | #endif | - | ||||||||||||
392 | } | - | ||||||||||||
393 | - | |||||||||||||
394 | void QLineEditPrivate::_q_textChanged(const QString &text) | - | ||||||||||||
395 | { | - | ||||||||||||
396 | if (hasSideWidgets()) {
| 0 | ||||||||||||
397 | const int newTextSize = text.size(); | - | ||||||||||||
398 | if (!newTextSize || !lastTextSize) {
| 0 | ||||||||||||
399 | lastTextSize = newTextSize; | - | ||||||||||||
400 | #ifndef QT_NO_ANIMATION | - | ||||||||||||
401 | const bool fadeIn = newTextSize > 0; | - | ||||||||||||
402 | foreachfor (const SideWidgetEntry &e ,: leadingSideWidgets) { | - | ||||||||||||
403 | if (e.flags & SideWidgetFadeInWithText)
| 0 | ||||||||||||
404 | static_cast<QLineEditIconButton *>(e.widget)->animateShow(fadeIn); never executed: static_cast<QLineEditIconButton *>(e.widget)->animateShow(fadeIn); | 0 | ||||||||||||
405 | } never executed: end of block | 0 | ||||||||||||
406 | foreachfor (const SideWidgetEntry &e ,: trailingSideWidgets) { | - | ||||||||||||
407 | if (e.flags & SideWidgetFadeInWithText)
| 0 | ||||||||||||
408 | static_cast<QLineEditIconButton *>(e.widget)->animateShow(fadeIn); never executed: static_cast<QLineEditIconButton *>(e.widget)->animateShow(fadeIn); | 0 | ||||||||||||
409 | } never executed: end of block | 0 | ||||||||||||
410 | #endif | - | ||||||||||||
411 | } never executed: end of block | 0 | ||||||||||||
412 | } never executed: end of block | 0 | ||||||||||||
413 | } never executed: end of block | 0 | ||||||||||||
414 | - | |||||||||||||
415 | void QLineEditPrivate::_q_clearButtonClicked() | - | ||||||||||||
416 | { | - | ||||||||||||
417 | Q_Q(QLineEdit); | - | ||||||||||||
418 | if (!q->text().isEmpty()) { | - | ||||||||||||
419 | q->clear(); | - | ||||||||||||
420 | emit q->textEdited(QString()); | - | ||||||||||||
421 | } | - | ||||||||||||
422 | } | - | ||||||||||||
423 | - | |||||||||||||
424 | QLineEditPrivate::SideWidgetParameters QLineEditPrivate::sideWidgetParameters() const | - | ||||||||||||
425 | { | - | ||||||||||||
426 | Q_Q(const QLineEdit); | - | ||||||||||||
427 | SideWidgetParameters result; | - | ||||||||||||
428 | result.iconSize = q->height() < 34 ? 16 : 32; | - | ||||||||||||
429 | result.margin = result.iconSize / 4; | - | ||||||||||||
430 | result.widgetWidth = result.iconSize + 6; | - | ||||||||||||
431 | result.widgetHeight = result.iconSize + 2; | - | ||||||||||||
432 | return result; | - | ||||||||||||
433 | } | - | ||||||||||||
434 | - | |||||||||||||
435 | QIcon QLineEditPrivate::clearButtonIcon() const | - | ||||||||||||
436 | { | - | ||||||||||||
437 | Q_Q(const QLineEdit); | - | ||||||||||||
438 | QStyleOptionFrame styleOption; | - | ||||||||||||
439 | q->initStyleOption(&styleOption); | - | ||||||||||||
440 | return q->style()->standardIcon(QStyle::SP_LineEditClearButton, &styleOption, q); | - | ||||||||||||
441 | } | - | ||||||||||||
442 | - | |||||||||||||
443 | void QLineEditPrivate::setClearButtonEnabled(bool enabled) | - | ||||||||||||
444 | { | - | ||||||||||||
445 | foreachfor (const SideWidgetEntry &e ,: trailingSideWidgets) { | - | ||||||||||||
446 | if (e.flags & SideWidgetClearButton) {
| 0 | ||||||||||||
447 | e.action->setEnabled(enabled); | - | ||||||||||||
448 | break; never executed: break; | 0 | ||||||||||||
449 | } | - | ||||||||||||
450 | } never executed: end of block | 0 | ||||||||||||
451 | } never executed: end of block | 0 | ||||||||||||
452 | - | |||||||||||||
453 | void QLineEditPrivate::positionSideWidgets() | - | ||||||||||||
454 | { | - | ||||||||||||
455 | Q_Q(QLineEdit); | - | ||||||||||||
456 | if (hasSideWidgets()) {
| 0 | ||||||||||||
457 | const QRect contentRect = q->rect(); | - | ||||||||||||
458 | const SideWidgetParameters p = sideWidgetParameters(); | - | ||||||||||||
459 | const int delta = p.margin + p.widgetWidth; | - | ||||||||||||
460 | QRect widgetGeometry(QPoint(p.margin, (contentRect.height() - p.widgetHeight) / 2), | - | ||||||||||||
461 | QSize(p.widgetWidth, p.widgetHeight)); | - | ||||||||||||
462 | foreachfor (const SideWidgetEntry &e ,: leftSideWidgetList()) { | - | ||||||||||||
463 | e.widget->setGeometry(widgetGeometry); | - | ||||||||||||
464 | if (e.action->isVisible())
| 0 | ||||||||||||
465 | widgetGeometry.moveLeft(widgetGeometry.left() + delta); never executed: widgetGeometry.moveLeft(widgetGeometry.left() + delta); | 0 | ||||||||||||
466 | } never executed: end of block | 0 | ||||||||||||
467 | widgetGeometry.moveLeft(contentRect.width() - p.widgetWidth - p.margin); | - | ||||||||||||
468 | foreachfor (const SideWidgetEntry &e ,: rightSideWidgetList()) { | - | ||||||||||||
469 | e.widget->setGeometry(widgetGeometry); | - | ||||||||||||
470 | if (e.action->isVisible())
| 0 | ||||||||||||
471 | widgetGeometry.moveLeft(widgetGeometry.left() - delta); never executed: widgetGeometry.moveLeft(widgetGeometry.left() - delta); | 0 | ||||||||||||
472 | } never executed: end of block | 0 | ||||||||||||
473 | } never executed: end of block | 0 | ||||||||||||
474 | } never executed: end of block | 0 | ||||||||||||
475 | - | |||||||||||||
476 | QLineEditPrivate::PositionIndexPair QLineEditPrivate::findSideWidget(const QAction *a) const | - | ||||||||||||
477 | { | - | ||||||||||||
478 | for (int i = 0; | - | ||||||||||||
479 | i <for (const auto &e : leadingSideWidgets.size(); ++i) { | - | ||||||||||||
480 | if (a == leadingSideWidgetse.at(i).action)
| 0 | ||||||||||||
481 | return PositionIndexPair(QLineEdit::LeadingPosition, i); never executed: return PositionIndexPair(QLineEdit::LeadingPosition, i); | 0 | ||||||||||||
482 | ++i; | - | ||||||||||||
483 | } never executed: end of block | 0 | ||||||||||||
484 | for (inti = 0; | - | ||||||||||||
485 | i <for (const auto &e : trailingSideWidgets.size(); ++i) { | - | ||||||||||||
486 | if (a == trailingSideWidgetse.at(i).action)
| 0 | ||||||||||||
487 | return PositionIndexPair(QLineEdit::TrailingPosition, i); never executed: return PositionIndexPair(QLineEdit::TrailingPosition, i); | 0 | ||||||||||||
488 | ++i; | - | ||||||||||||
489 | } never executed: end of block | 0 | ||||||||||||
490 | return PositionIndexPair(QLineEdit::LeadingPosition, -1); never executed: return PositionIndexPair(QLineEdit::LeadingPosition, -1); | 0 | ||||||||||||
491 | } | - | ||||||||||||
492 | - | |||||||||||||
493 | QWidget *QLineEditPrivate::addAction(QAction *newAction, QAction *before, QLineEdit::ActionPosition position, int flags) | - | ||||||||||||
494 | { | - | ||||||||||||
495 | Q_Q(QLineEdit); | - | ||||||||||||
496 | if (!newAction)
| 0 | ||||||||||||
497 | return 0; never executed: return 0; | 0 | ||||||||||||
498 | if (!hasSideWidgets()) { // initial setup.
| 0 | ||||||||||||
499 | QObject::connect(q, SIGNAL(textChanged(QString)), q, SLOT(_q_textChanged(QString))); | - | ||||||||||||
500 | lastTextSize = q->text().size(); | - | ||||||||||||
501 | } never executed: end of block | 0 | ||||||||||||
502 | QWidget *w = 0; | - | ||||||||||||
503 | // Store flags about QWidgetAction here since removeAction() may be called from ~QAction, | - | ||||||||||||
504 | // in which a qobject_cast<> no longer works. | - | ||||||||||||
505 | if (QWidgetAction *widgetAction = qobject_cast<QWidgetAction *>(newAction)) {
| 0 | ||||||||||||
506 | if ((w = widgetAction->requestWidget(q)))
| 0 | ||||||||||||
507 | flags |= SideWidgetCreatedByWidgetAction; never executed: flags |= SideWidgetCreatedByWidgetAction; | 0 | ||||||||||||
508 | } never executed: end of block | 0 | ||||||||||||
509 | if (!w) {
| 0 | ||||||||||||
510 | QLineEditIconButton *toolButton = new QLineEditIconButton(q); | - | ||||||||||||
511 | toolButton->setIcon(newAction->icon()); | - | ||||||||||||
512 | toolButton->setOpacity(lastTextSize > 0 || !(flags & SideWidgetFadeInWithText) ? 1 : 0); | - | ||||||||||||
513 | if (flags & SideWidgetClearButton)
| 0 | ||||||||||||
514 | QObject::connect(toolButton, SIGNAL(clicked()), q, SLOT(_q_clearButtonClicked())); never executed: QObject::connect(toolButton, qFlagLocation("2""clicked()" "\0" __FILE__ ":" "514"), q, qFlagLocation("1""_q_clearButtonClicked()" "\0" __FILE__ ":" "514")); | 0 | ||||||||||||
515 | toolButton->setDefaultAction(newAction); | - | ||||||||||||
516 | w = toolButton; | - | ||||||||||||
517 | } never executed: end of block | 0 | ||||||||||||
518 | // If there is a 'before' action, it takes preference | - | ||||||||||||
519 | PositionIndexPair positionIndex = before ? findSideWidget(before) : PositionIndexPair(position, -1);
| 0 | ||||||||||||
520 | SideWidgetEntryList &list = positionIndex.first == QLineEdit::TrailingPosition ? trailingSideWidgets : leadingSideWidgets;
| 0 | ||||||||||||
521 | if (positionIndex.second < 0)
| 0 | ||||||||||||
522 | positionIndex.second = int(list.size();()); never executed: positionIndex.second = int(list.size()); | 0 | ||||||||||||
523 | list.insert(list.begin() + positionIndex.second, SideWidgetEntry(w, newAction, flags)); | - | ||||||||||||
524 | positionSideWidgets(); | - | ||||||||||||
525 | w->show(); | - | ||||||||||||
526 | return w; never executed: return w; | 0 | ||||||||||||
527 | } | - | ||||||||||||
528 | - | |||||||||||||
529 | void QLineEditPrivate::removeAction(QAction *action) | - | ||||||||||||
530 | { | - | ||||||||||||
531 | Q_Q(QLineEdit); | - | ||||||||||||
532 | const PositionIndexPair positionIndex = findSideWidget(action); | - | ||||||||||||
533 | if (positionIndex.second == -1)
| 0 | ||||||||||||
534 | return; never executed: return; | 0 | ||||||||||||
535 | SideWidgetEntryList &list = positionIndex.first == QLineEdit::TrailingPosition ? trailingSideWidgets : leadingSideWidgets;
| 0 | ||||||||||||
536 | SideWidgetEntry entry = list[positionIndex.takeAtsecond]; | - | ||||||||||||
537 | list.erase(list.begin() + positionIndex.second); | - | ||||||||||||
538 | if (entry.flags & SideWidgetCreatedByWidgetAction)
| 0 | ||||||||||||
539 | static_cast<QWidgetAction *>(entry.action)->releaseWidget(entry.widget); never executed: static_cast<QWidgetAction *>(entry.action)->releaseWidget(entry.widget); | 0 | ||||||||||||
540 | else | - | ||||||||||||
541 | delete entry.widget; never executed: delete entry.widget; | 0 | ||||||||||||
542 | positionSideWidgets(); | - | ||||||||||||
543 | if (!hasSideWidgets()) // Last widget, remove connection
| 0 | ||||||||||||
544 | QObject::disconnect(q, SIGNAL(textChanged(QString)), q, SLOT(_q_textChanged(QString))); never executed: QObject::disconnect(q, qFlagLocation("2""textChanged(QString)" "\0" __FILE__ ":" "544"), q, qFlagLocation("1""_q_textChanged(QString)" "\0" __FILE__ ":" "544")); | 0 | ||||||||||||
545 | q->update(); | - | ||||||||||||
546 | } never executed: end of block | 0 | ||||||||||||
547 | - | |||||||||||||
548 | static bool isSideWidgetVisible(const QLineEditPrivate::SideWidgetEntry &e) | - | ||||||||||||
549 | { | - | ||||||||||||
550 | return e.widget->isVisible(); never executed: return e.widget->isVisible(); | 0 | ||||||||||||
551 | } | - | ||||||||||||
552 | - | |||||||||||||
553 | int QLineEditPrivate::effectiveLeftTextMargin() const | - | ||||||||||||
554 | { | - | ||||||||||||
555 | int result = leftTextMargin; | - | ||||||||||||
556 | if (!leftSideWidgetList().empty()) {
| 0 | ||||||||||||
557 | const SideWidgetParameters p = sideWidgetParameters(); | - | ||||||||||||
558 | result += (p.margin + p.widgetWidth) | - | ||||||||||||
559 | * int(std::count_if(leftSideWidgetList().begin(), leftSideWidgetList().end(), | - | ||||||||||||
560 | isSideWidgetVisible)); | - | ||||||||||||
561 | } never executed: end of block | 0 | ||||||||||||
562 | return result; never executed: return result; | 0 | ||||||||||||
563 | } | - | ||||||||||||
564 | - | |||||||||||||
565 | int QLineEditPrivate::effectiveRightTextMargin() const | - | ||||||||||||
566 | { | - | ||||||||||||
567 | int result = rightTextMargin; | - | ||||||||||||
568 | if (!rightSideWidgetList().empty()) {
| 0 | ||||||||||||
569 | const SideWidgetParameters p = sideWidgetParameters(); | - | ||||||||||||
570 | result += (p.margin + p.widgetWidth) | - | ||||||||||||
571 | * int(std::count_if(rightSideWidgetList().begin(), rightSideWidgetList().end(), | - | ||||||||||||
572 | isSideWidgetVisible)); | - | ||||||||||||
573 | } never executed: end of block | 0 | ||||||||||||
574 | return result; never executed: return result; | 0 | ||||||||||||
575 | } | - | ||||||||||||
576 | - | |||||||||||||
577 | - | |||||||||||||
578 | QT_END_NAMESPACE | - | ||||||||||||
579 | - | |||||||||||||
580 | #include "moc_qlineedit_p.cpp" | - | ||||||||||||
581 | - | |||||||||||||
582 | #endif | - | ||||||||||||
Source code | Switch to Preprocessed file |