1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qlineedit.h" |
5 | #include "qlineedit_p.h" |
6 | |
7 | #include "qvariant.h" |
8 | #if QT_CONFIG(itemviews) |
9 | #include "qabstractitemview.h" |
10 | #endif |
11 | #if QT_CONFIG(draganddrop) |
12 | #include "qdrag.h" |
13 | #endif |
14 | #if QT_CONFIG(action) |
15 | # include "qwidgetaction.h" |
16 | #endif |
17 | #include "qclipboard.h" |
18 | #if QT_CONFIG(accessibility) |
19 | #include "qaccessible.h" |
20 | #endif |
21 | #ifndef QT_NO_IM |
22 | #include "qinputmethod.h" |
23 | #include "qlist.h" |
24 | #endif |
25 | #include <qpainter.h> |
26 | #if QT_CONFIG(animation) |
27 | #include <qpropertyanimation.h> |
28 | #endif |
29 | #include <qstylehints.h> |
30 | #include <qvalidator.h> |
31 | |
32 | QT_BEGIN_NAMESPACE |
33 | |
34 | const int QLineEditPrivate::verticalMargin(1); |
35 | const int QLineEditPrivate::horizontalMargin(2); |
36 | |
37 | // Needs to be kept in sync with QLineEdit::paintEvent |
38 | QRect QLineEditPrivate::adjustedControlRect(const QRect &rect) const |
39 | { |
40 | QRect widgetRect = !rect.isEmpty() ? rect : q_func()->rect(); |
41 | QRect cr = adjustedContentsRect(); |
42 | int cix = cr.x() - hscroll + horizontalMargin; |
43 | return widgetRect.translated(p: QPoint(cix, vscroll - control->ascent() + q_func()->fontMetrics().ascent())); |
44 | } |
45 | |
46 | int QLineEditPrivate::xToPos(int x, QTextLine::CursorPosition betweenOrOn) const |
47 | { |
48 | QRect cr = adjustedContentsRect(); |
49 | x-= cr.x() - hscroll + horizontalMargin; |
50 | return control->xToPos(x, betweenOrOn); |
51 | } |
52 | |
53 | QString QLineEditPrivate::textBeforeCursor(int curPos) const |
54 | { |
55 | const QString &text = control->text(); |
56 | return text.mid(position: 0, n: curPos); |
57 | } |
58 | |
59 | QString QLineEditPrivate::textAfterCursor(int curPos) const |
60 | { |
61 | const QString &text = control->text(); |
62 | return text.mid(position: curPos); |
63 | } |
64 | |
65 | bool QLineEditPrivate::inSelection(int x) const |
66 | { |
67 | x -= adjustedContentsRect().x() - hscroll + horizontalMargin; |
68 | return control->inSelection(x); |
69 | } |
70 | |
71 | QRect QLineEditPrivate::cursorRect() const |
72 | { |
73 | return adjustedControlRect(rect: control->cursorRect()); |
74 | } |
75 | |
76 | #if QT_CONFIG(completer) |
77 | void QLineEditPrivate::connectCompleter() |
78 | { |
79 | Q_Q(const QLineEdit); |
80 | QObject::connect(sender: control->completer(), signal: qOverload<const QString &>(&QCompleter::activated), |
81 | context: q, slot: &QLineEdit::setText); |
82 | QObjectPrivate::connect(sender: control->completer(), signal: qOverload<const QString &>(&QCompleter::highlighted), |
83 | receiverPrivate: this, slot: &QLineEditPrivate::completionHighlighted); |
84 | } |
85 | |
86 | void QLineEditPrivate::disconnectCompleter() |
87 | { |
88 | Q_Q(const QLineEdit); |
89 | QObject::disconnect(sender: control->completer(), signal: qOverload<const QString &>(&QCompleter::activated), |
90 | receiver: q, slot: &QLineEdit::setText); |
91 | QObjectPrivate::disconnect(sender: control->completer(), signal: qOverload<const QString &>(&QCompleter::highlighted), |
92 | receiverPrivate: this, slot: &QLineEditPrivate::completionHighlighted); |
93 | } |
94 | |
95 | void QLineEditPrivate::completionHighlighted(const QString &newText) |
96 | { |
97 | Q_Q(QLineEdit); |
98 | if (control->completer()->completionMode() != QCompleter::InlineCompletion) { |
99 | q->setText(newText); |
100 | } else { |
101 | int c = control->cursor(); |
102 | QString text = control->text(); |
103 | q->setText(QStringView{text}.left(n: c) + QStringView{newText}.mid(pos: c)); |
104 | control->moveCursor(pos: control->end(), mark: false); |
105 | #ifndef Q_OS_ANDROID |
106 | const bool mark = true; |
107 | #else |
108 | const bool mark = (imHints & Qt::ImhNoPredictiveText); |
109 | #endif |
110 | control->moveCursor(pos: c, mark); |
111 | } |
112 | } |
113 | |
114 | #endif // QT_CONFIG(completer) |
115 | |
116 | void QLineEditPrivate::handleWindowActivate() |
117 | { |
118 | Q_Q(QLineEdit); |
119 | if (!q->hasFocus() && control->hasSelectedText()) |
120 | control->deselect(); |
121 | } |
122 | |
123 | void QLineEditPrivate::textEdited(const QString &text) |
124 | { |
125 | Q_Q(QLineEdit); |
126 | edited = true; |
127 | emit q->textEdited(text); |
128 | #if QT_CONFIG(completer) |
129 | if (control->completer() |
130 | && control->completer()->completionMode() != QCompleter::InlineCompletion) |
131 | control->complete(key: -1); // update the popup on cut/paste/del |
132 | #endif |
133 | } |
134 | |
135 | void QLineEditPrivate::cursorPositionChanged(int from, int to) |
136 | { |
137 | Q_Q(QLineEdit); |
138 | q->update(); |
139 | emit q->cursorPositionChanged(from, to); |
140 | } |
141 | |
142 | #ifdef QT_KEYPAD_NAVIGATION |
143 | void QLineEditPrivate::editFocusChange(bool e) |
144 | { |
145 | Q_Q(QLineEdit); |
146 | q->setEditFocus(e); |
147 | } |
148 | #endif |
149 | |
150 | void QLineEditPrivate::selectionChanged() |
151 | { |
152 | Q_Q(QLineEdit); |
153 | if (control->preeditAreaText().isEmpty()) { |
154 | QStyleOptionFrame opt; |
155 | q->initStyleOption(option: &opt); |
156 | bool showCursor = control->hasSelectedText() ? |
157 | q->style()->styleHint(stylehint: QStyle::SH_BlinkCursorWhenTextSelected, opt: &opt, widget: q): |
158 | q->hasFocus(); |
159 | setCursorVisible(showCursor); |
160 | } |
161 | |
162 | emit q->selectionChanged(); |
163 | #if QT_CONFIG(accessibility) |
164 | QAccessibleTextSelectionEvent ev(q, control->selectionStart(), control->selectionEnd()); |
165 | ev.setCursorPosition(control->cursorPosition()); |
166 | QAccessible::updateAccessibility(event: &ev); |
167 | #endif |
168 | } |
169 | |
170 | void QLineEditPrivate::updateNeeded(const QRect &rect) |
171 | { |
172 | q_func()->update(adjustedControlRect(rect)); |
173 | } |
174 | |
175 | void QLineEditPrivate::init(const QString& txt) |
176 | { |
177 | Q_Q(QLineEdit); |
178 | |
179 | const auto qUpdateMicroFocus = [q]() |
180 | { |
181 | q->updateMicroFocus(); |
182 | }; |
183 | control = new QWidgetLineControl(txt); |
184 | control->setParent(q); |
185 | control->setFont(q->font()); |
186 | QObject::connect(sender: control, signal: &QWidgetLineControl::textChanged, |
187 | context: q, slot: &QLineEdit::textChanged); |
188 | QObjectPrivate::connect(sender: control, signal: &QWidgetLineControl::textEdited, |
189 | receiverPrivate: this, slot: &QLineEditPrivate::textEdited); |
190 | QObjectPrivate::connect(sender: control, signal: &QWidgetLineControl::cursorPositionChanged, |
191 | receiverPrivate: this, slot: &QLineEditPrivate::cursorPositionChanged); |
192 | QObjectPrivate::connect(sender: control, signal: &QWidgetLineControl::selectionChanged, |
193 | receiverPrivate: this, slot: &QLineEditPrivate::selectionChanged); |
194 | QObjectPrivate::connect(sender: control, signal: &QWidgetLineControl::editingFinished, |
195 | receiverPrivate: this, slot: &QLineEditPrivate::controlEditingFinished); |
196 | #ifdef QT_KEYPAD_NAVIGATION |
197 | QObject::connect(control, &QWidgetLineControl::editFocusChange, |
198 | this, &QLineEditPrivate::editFocusChange); |
199 | #endif |
200 | QObject::connect(sender: control, signal: &QWidgetLineControl::cursorPositionChanged, |
201 | context: q, slot: qUpdateMicroFocus); |
202 | |
203 | QObject::connect(sender: control, signal: &QWidgetLineControl::textChanged, |
204 | context: q, slot: qUpdateMicroFocus); |
205 | |
206 | QObject::connect(sender: control, signal: &QWidgetLineControl::updateMicroFocus, |
207 | context: q, slot: qUpdateMicroFocus); |
208 | |
209 | // for now, going completely overboard with updates. |
210 | QObject::connect(sender: control, signal: &QWidgetLineControl::selectionChanged, |
211 | context: q, slot: qOverload<>(&QLineEdit::update)); |
212 | |
213 | QObject::connect(sender: control, signal: &QWidgetLineControl::selectionChanged, |
214 | context: q, slot: qUpdateMicroFocus); |
215 | |
216 | QObject::connect(sender: control, signal: &QWidgetLineControl::displayTextChanged, |
217 | context: q, slot: qOverload<>(&QLineEdit::update)); |
218 | |
219 | QObjectPrivate::connect(sender: control, signal: &QWidgetLineControl::updateNeeded, |
220 | receiverPrivate: this, slot: &QLineEditPrivate::updateNeeded); |
221 | QObject::connect(sender: control, signal: &QWidgetLineControl::inputRejected, |
222 | context: q, slot: &QLineEdit::inputRejected); |
223 | |
224 | QStyleOptionFrame opt; |
225 | q->initStyleOption(option: &opt); |
226 | control->setPasswordCharacter(char16_t(q->style()->styleHint(stylehint: QStyle::SH_LineEdit_PasswordCharacter, opt: &opt, widget: q))); |
227 | control->setPasswordMaskDelay(q->style()->styleHint(stylehint: QStyle::SH_LineEdit_PasswordMaskDelay, opt: &opt, widget: q)); |
228 | #ifndef QT_NO_CURSOR |
229 | q->setCursor(Qt::IBeamCursor); |
230 | #endif |
231 | q->setFocusPolicy(Qt::StrongFocus); |
232 | q->setAttribute(Qt::WA_InputMethodEnabled); |
233 | // Specifies that this widget can use more, but is able to survive on |
234 | // less, horizontal space; and is fixed vertically. |
235 | q->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed, QSizePolicy::LineEdit)); |
236 | q->setBackgroundRole(QPalette::Base); |
237 | q->setAttribute(Qt::WA_KeyCompression); |
238 | q->setMouseTracking(true); |
239 | q->setAcceptDrops(true); |
240 | |
241 | q->setAttribute(Qt::WA_MacShowFocusRect); |
242 | |
243 | initMouseYThreshold(); |
244 | } |
245 | |
246 | void QLineEditPrivate::initMouseYThreshold() |
247 | { |
248 | mouseYThreshold = QGuiApplication::styleHints()->mouseQuickSelectionThreshold(); |
249 | } |
250 | |
251 | QRect QLineEditPrivate::adjustedContentsRect() const |
252 | { |
253 | Q_Q(const QLineEdit); |
254 | QStyleOptionFrame opt; |
255 | q->initStyleOption(option: &opt); |
256 | QRect r = q->style()->subElementRect(subElement: QStyle::SE_LineEditContents, option: &opt, widget: q); |
257 | r = r.marginsRemoved(margins: effectiveTextMargins()); |
258 | return r; |
259 | } |
260 | |
261 | void QLineEditPrivate::setCursorVisible(bool visible) |
262 | { |
263 | Q_Q(QLineEdit); |
264 | if ((bool)cursorVisible == visible) |
265 | return; |
266 | cursorVisible = visible; |
267 | if (control->inputMask().isEmpty()) |
268 | q->update(cursorRect()); |
269 | else |
270 | q->update(); |
271 | } |
272 | |
273 | void QLineEditPrivate::setText(const QString& text) |
274 | { |
275 | edited = true; |
276 | control->setText(text); |
277 | } |
278 | |
279 | void QLineEditPrivate::updatePasswordEchoEditing(bool editing) |
280 | { |
281 | Q_Q(QLineEdit); |
282 | control->updatePasswordEchoEditing(editing); |
283 | q->setAttribute(Qt::WA_InputMethodEnabled, on: shouldEnableInputMethod()); |
284 | } |
285 | |
286 | void QLineEditPrivate::resetInputMethod() |
287 | { |
288 | Q_Q(QLineEdit); |
289 | if (q->hasFocus() && qApp) { |
290 | QGuiApplication::inputMethod()->reset(); |
291 | } |
292 | } |
293 | |
294 | /*! |
295 | This function is not intended as polymorphic usage. Just a shared code |
296 | fragment that calls QInputMethod::invokeAction for this |
297 | class. |
298 | */ |
299 | bool QLineEditPrivate::sendMouseEventToInputContext( QMouseEvent *e ) |
300 | { |
301 | #if !defined QT_NO_IM |
302 | if ( control->composeMode() ) { |
303 | int tmp_cursor = xToPos(x: e->position().toPoint().x()); |
304 | int mousePos = tmp_cursor - control->cursor(); |
305 | if ( mousePos < 0 || mousePos > control->preeditAreaText().size() ) |
306 | mousePos = -1; |
307 | |
308 | if (mousePos >= 0) { |
309 | if (e->type() == QEvent::MouseButtonRelease) |
310 | QGuiApplication::inputMethod()->invokeAction(a: QInputMethod::Click, cursorPosition: mousePos); |
311 | |
312 | return true; |
313 | } |
314 | } |
315 | #else |
316 | Q_UNUSED(e); |
317 | #endif |
318 | |
319 | return false; |
320 | } |
321 | |
322 | #if QT_CONFIG(draganddrop) |
323 | void QLineEditPrivate::drag() |
324 | { |
325 | Q_Q(QLineEdit); |
326 | dndTimer.stop(); |
327 | QMimeData *data = new QMimeData; |
328 | data->setText(control->selectedText()); |
329 | QDrag *drag = new QDrag(q); |
330 | drag->setMimeData(data); |
331 | Qt::DropAction action = drag->exec(supportedActions: Qt::CopyAction); |
332 | if (action == Qt::MoveAction && !control->isReadOnly() && drag->target() != q) |
333 | control->removeSelection(); |
334 | } |
335 | #endif // QT_CONFIG(draganddrop) |
336 | |
337 | |
338 | #if QT_CONFIG(toolbutton) |
339 | QLineEditIconButton::QLineEditIconButton(QWidget *parent) |
340 | : QToolButton(parent) |
341 | , m_opacity(0) |
342 | { |
343 | setFocusPolicy(Qt::NoFocus); |
344 | } |
345 | |
346 | QLineEditPrivate *QLineEditIconButton::lineEditPrivate() const |
347 | { |
348 | QLineEdit *le = qobject_cast<QLineEdit *>(object: parentWidget()); |
349 | return le ? static_cast<QLineEditPrivate *>(qt_widget_private(widget: le)) : nullptr; |
350 | } |
351 | |
352 | void QLineEditIconButton::paintEvent(QPaintEvent *) |
353 | { |
354 | QPainter painter(this); |
355 | QIcon::Mode state = QIcon::Disabled; |
356 | if (isEnabled()) |
357 | state = isDown() ? QIcon::Active : QIcon::Normal; |
358 | const QLineEditPrivate *lep = lineEditPrivate(); |
359 | const int iconWidth = lep ? lep->sideWidgetParameters().iconSize : 16; |
360 | const QSize iconSize(iconWidth, iconWidth); |
361 | const QPixmap iconPixmap = icon().pixmap(size: iconSize, devicePixelRatio: devicePixelRatio(), mode: state, state: QIcon::Off); |
362 | QRect pixmapRect = QRect(QPoint(0, 0), iconSize); |
363 | pixmapRect.moveCenter(p: rect().center()); |
364 | painter.setOpacity(m_opacity); |
365 | painter.drawPixmap(r: pixmapRect, pm: iconPixmap); |
366 | } |
367 | |
368 | void QLineEditIconButton::actionEvent(QActionEvent *e) |
369 | { |
370 | switch (e->type()) { |
371 | case QEvent::ActionChanged: { |
372 | const auto *action = e->action(); |
373 | if (isVisibleTo(parentWidget()) != action->isVisible()) { |
374 | setVisible(action->isVisible()); |
375 | if (QLineEditPrivate *lep = lineEditPrivate()) |
376 | lep->positionSideWidgets(); |
377 | } |
378 | } |
379 | break; |
380 | default: |
381 | break; |
382 | } |
383 | QToolButton::actionEvent(e); |
384 | } |
385 | |
386 | void QLineEditIconButton::setOpacity(qreal value) |
387 | { |
388 | if (!qFuzzyCompare(p1: m_opacity, p2: value)) { |
389 | m_opacity = value; |
390 | updateCursor(); |
391 | update(); |
392 | } |
393 | } |
394 | |
395 | #if QT_CONFIG(animation) |
396 | bool QLineEditIconButton::shouldHideWithText() const |
397 | { |
398 | return m_hideWithText; |
399 | } |
400 | |
401 | void QLineEditIconButton::setHideWithText(bool hide) |
402 | { |
403 | m_hideWithText = hide; |
404 | } |
405 | |
406 | void QLineEditIconButton::onAnimationFinished() |
407 | { |
408 | if (shouldHideWithText() && isVisible() && m_fadingOut) { |
409 | hide(); |
410 | m_fadingOut = false; |
411 | |
412 | // Invalidate previous geometry to take into account new size of side widgets |
413 | if (auto le = lineEditPrivate()) |
414 | le->updateGeometry_helper(forceUpdate: true); |
415 | } |
416 | } |
417 | |
418 | void QLineEditIconButton::animateShow(bool visible) |
419 | { |
420 | m_fadingOut = !visible; |
421 | |
422 | if (shouldHideWithText() && !isVisible()) { |
423 | show(); |
424 | |
425 | // Invalidate previous geometry to take into account new size of side widgets |
426 | if (auto le = lineEditPrivate()) |
427 | le->updateGeometry_helper(forceUpdate: true); |
428 | } |
429 | |
430 | startOpacityAnimation(endValue: visible ? 1.0 : 0.0); |
431 | } |
432 | |
433 | void QLineEditIconButton::startOpacityAnimation(qreal endValue) |
434 | { |
435 | QPropertyAnimation *animation = new QPropertyAnimation(this, QByteArrayLiteral("opacity" ), this); |
436 | connect(sender: animation, signal: &QPropertyAnimation::finished, context: this, slot: &QLineEditIconButton::onAnimationFinished); |
437 | |
438 | animation->setDuration(160); |
439 | animation->setEndValue(endValue); |
440 | animation->start(policy: QAbstractAnimation::DeleteWhenStopped); |
441 | } |
442 | #endif |
443 | |
444 | void QLineEditIconButton::updateCursor() |
445 | { |
446 | #ifndef QT_NO_CURSOR |
447 | setCursor(qFuzzyCompare(p1: m_opacity, p2: qreal(1.0)) || !parentWidget() ? QCursor(Qt::ArrowCursor) : parentWidget()->cursor()); |
448 | #endif |
449 | } |
450 | #endif // QT_CONFIG(toolbutton) |
451 | |
452 | #if QT_CONFIG(animation) && QT_CONFIG(toolbutton) |
453 | static void displayWidgets(const QLineEditPrivate::SideWidgetEntryList &widgets, bool display) |
454 | { |
455 | for (const auto &e : widgets) { |
456 | if (e.flags & QLineEditPrivate::SideWidgetFadeInWithText) |
457 | static_cast<QLineEditIconButton *>(e.widget)->animateShow(visible: display); |
458 | } |
459 | } |
460 | #endif |
461 | |
462 | void QLineEditPrivate::textChanged(const QString &text) |
463 | { |
464 | if (hasSideWidgets()) { |
465 | const int newTextSize = text.size(); |
466 | if (!newTextSize || !lastTextSize) { |
467 | lastTextSize = newTextSize; |
468 | #if QT_CONFIG(animation) && QT_CONFIG(toolbutton) |
469 | const bool display = newTextSize > 0; |
470 | displayWidgets(widgets: leadingSideWidgets, display); |
471 | displayWidgets(widgets: trailingSideWidgets, display); |
472 | #endif |
473 | } |
474 | } |
475 | } |
476 | |
477 | void QLineEditPrivate::clearButtonClicked() |
478 | { |
479 | Q_Q(QLineEdit); |
480 | if (!q->text().isEmpty()) { |
481 | q->clear(); |
482 | textEdited(text: QString()); |
483 | } |
484 | } |
485 | |
486 | void QLineEditPrivate::controlEditingFinished() |
487 | { |
488 | Q_Q(QLineEdit); |
489 | edited = false; |
490 | emit q->returnPressed(); |
491 | emit q->editingFinished(); |
492 | } |
493 | |
494 | QLineEditPrivate::SideWidgetParameters QLineEditPrivate::sideWidgetParameters() const |
495 | { |
496 | Q_Q(const QLineEdit); |
497 | SideWidgetParameters result; |
498 | result.iconSize = q->style()->pixelMetric(metric: QStyle::PM_LineEditIconSize, option: nullptr, widget: q); |
499 | result.margin = q->style()->pixelMetric(metric: QStyle::PM_LineEditIconMargin, option: nullptr, widget: q); |
500 | result.widgetWidth = result.iconSize + 6; |
501 | result.widgetHeight = result.iconSize + 2; |
502 | return result; |
503 | } |
504 | |
505 | QIcon QLineEditPrivate::clearButtonIcon() const |
506 | { |
507 | Q_Q(const QLineEdit); |
508 | QStyleOptionFrame styleOption; |
509 | q->initStyleOption(option: &styleOption); |
510 | return q->style()->standardIcon(standardIcon: QStyle::SP_LineEditClearButton, option: &styleOption, widget: q); |
511 | } |
512 | |
513 | void QLineEditPrivate::setClearButtonEnabled(bool enabled) |
514 | { |
515 | #if QT_CONFIG(action) |
516 | for (const SideWidgetEntry &e : trailingSideWidgets) { |
517 | if (e.flags & SideWidgetClearButton) { |
518 | e.action->setEnabled(enabled); |
519 | break; |
520 | } |
521 | } |
522 | #else |
523 | Q_UNUSED(enabled); |
524 | #endif |
525 | } |
526 | |
527 | void QLineEditPrivate::positionSideWidgets() |
528 | { |
529 | Q_Q(QLineEdit); |
530 | if (hasSideWidgets()) { |
531 | const QRect contentRect = q->rect(); |
532 | const SideWidgetParameters p = sideWidgetParameters(); |
533 | const int delta = p.margin + p.widgetWidth; |
534 | QRect widgetGeometry(QPoint(p.margin, (contentRect.height() - p.widgetHeight) / 2), |
535 | QSize(p.widgetWidth, p.widgetHeight)); |
536 | for (const SideWidgetEntry &e : leftSideWidgetList()) { |
537 | e.widget->setGeometry(widgetGeometry); |
538 | #if QT_CONFIG(action) |
539 | if (e.action->isVisible()) |
540 | widgetGeometry.moveLeft(pos: widgetGeometry.left() + delta); |
541 | #else |
542 | Q_UNUSED(delta); |
543 | #endif |
544 | } |
545 | widgetGeometry.moveLeft(pos: contentRect.width() - p.widgetWidth - p.margin); |
546 | for (const SideWidgetEntry &e : rightSideWidgetList()) { |
547 | e.widget->setGeometry(widgetGeometry); |
548 | #if QT_CONFIG(action) |
549 | if (e.action->isVisible()) |
550 | widgetGeometry.moveLeft(pos: widgetGeometry.left() - delta); |
551 | #endif |
552 | } |
553 | } |
554 | } |
555 | |
556 | #if QT_CONFIG(action) |
557 | QLineEditPrivate::SideWidgetLocation QLineEditPrivate::findSideWidget(const QAction *a) const |
558 | { |
559 | int i = 0; |
560 | for (const auto &e : leadingSideWidgets) { |
561 | if (a == e.action) |
562 | return {.position: QLineEdit::LeadingPosition, .index: i}; |
563 | ++i; |
564 | } |
565 | i = 0; |
566 | for (const auto &e : trailingSideWidgets) { |
567 | if (a == e.action) |
568 | return {.position: QLineEdit::TrailingPosition, .index: i}; |
569 | ++i; |
570 | } |
571 | return {.position: QLineEdit::LeadingPosition, .index: -1}; |
572 | } |
573 | |
574 | QWidget *QLineEditPrivate::addAction(QAction *newAction, QAction *before, QLineEdit::ActionPosition position, int flags) |
575 | { |
576 | Q_Q(QLineEdit); |
577 | if (!newAction) |
578 | return nullptr; |
579 | if (!hasSideWidgets()) { // initial setup. |
580 | QObjectPrivate::connect(sender: q, signal: &QLineEdit::textChanged, |
581 | receiverPrivate: this, slot: &QLineEditPrivate::textChanged); |
582 | lastTextSize = q->text().size(); |
583 | } |
584 | QWidget *w = nullptr; |
585 | // Store flags about QWidgetAction here since removeAction() may be called from ~QAction, |
586 | // in which a qobject_cast<> no longer works. |
587 | if (QWidgetAction *widgetAction = qobject_cast<QWidgetAction *>(object: newAction)) { |
588 | if ((w = widgetAction->requestWidget(parent: q))) |
589 | flags |= SideWidgetCreatedByWidgetAction; |
590 | } |
591 | if (!w) { |
592 | #if QT_CONFIG(toolbutton) |
593 | QLineEditIconButton *toolButton = new QLineEditIconButton(q); |
594 | toolButton->setIcon(newAction->icon()); |
595 | toolButton->setOpacity(lastTextSize > 0 || !(flags & SideWidgetFadeInWithText) ? 1 : 0); |
596 | if (flags & SideWidgetClearButton) { |
597 | QObjectPrivate::connect(sender: toolButton, signal: &QToolButton::clicked, |
598 | receiverPrivate: this, slot: &QLineEditPrivate::clearButtonClicked); |
599 | |
600 | #if QT_CONFIG(animation) |
601 | // The clear button is handled only by this widget. The button should be really |
602 | // shown/hidden in order to calculate size hints correctly. |
603 | toolButton->setHideWithText(true); |
604 | #endif |
605 | } |
606 | toolButton->setDefaultAction(newAction); |
607 | w = toolButton; |
608 | #else |
609 | return nullptr; |
610 | #endif |
611 | } |
612 | |
613 | // QTBUG-59957: clear button should be the leftmost action. |
614 | if (!before && !(flags & SideWidgetClearButton) && position == QLineEdit::TrailingPosition) { |
615 | for (const SideWidgetEntry &e : trailingSideWidgets) { |
616 | if (e.flags & SideWidgetClearButton) { |
617 | before = e.action; |
618 | break; |
619 | } |
620 | } |
621 | } |
622 | |
623 | // If there is a 'before' action, it takes preference |
624 | |
625 | // There's a bug in GHS compiler that causes internal error on the following code. |
626 | // The affected GHS compiler versions are 2016.5.4 and 2017.1. GHS internal reference |
627 | // to track the progress of this issue is TOOLS-26637. |
628 | // This temporary workaround allows to compile with GHS toolchain and should be |
629 | // removed when GHS provides a patch to fix the compiler issue. |
630 | |
631 | #if defined(Q_CC_GHS) |
632 | const SideWidgetLocation loc = {position, -1}; |
633 | const auto location = before ? findSideWidget(before) : loc; |
634 | #else |
635 | const auto location = before ? findSideWidget(a: before) : SideWidgetLocation{.position: position, .index: -1}; |
636 | #endif |
637 | |
638 | SideWidgetEntryList &list = location.position == QLineEdit::TrailingPosition ? trailingSideWidgets : leadingSideWidgets; |
639 | list.insert(position: location.isValid() ? list.begin() + location.index : list.end(), |
640 | x: SideWidgetEntry(w, newAction, flags)); |
641 | positionSideWidgets(); |
642 | w->show(); |
643 | return w; |
644 | } |
645 | |
646 | void QLineEditPrivate::removeAction(QAction *action) |
647 | { |
648 | Q_Q(QLineEdit); |
649 | const auto location = findSideWidget(a: action); |
650 | if (!location.isValid()) |
651 | return; |
652 | SideWidgetEntryList &list = location.position == QLineEdit::TrailingPosition ? trailingSideWidgets : leadingSideWidgets; |
653 | SideWidgetEntry entry = list[location.index]; |
654 | list.erase(position: list.begin() + location.index); |
655 | if (entry.flags & SideWidgetCreatedByWidgetAction) |
656 | static_cast<QWidgetAction *>(entry.action)->releaseWidget(widget: entry.widget); |
657 | else |
658 | delete entry.widget; |
659 | positionSideWidgets(); |
660 | if (!hasSideWidgets()) // Last widget, remove connection |
661 | QObjectPrivate::connect(sender: q, signal: &QLineEdit::textChanged, |
662 | receiverPrivate: this, slot: &QLineEditPrivate::textChanged); |
663 | q->update(); |
664 | } |
665 | #endif // QT_CONFIG(action) |
666 | |
667 | static int effectiveTextMargin(int defaultMargin, const QLineEditPrivate::SideWidgetEntryList &widgets, |
668 | const QLineEditPrivate::SideWidgetParameters ¶meters) |
669 | { |
670 | if (widgets.empty()) |
671 | return defaultMargin; |
672 | |
673 | const auto visibleSideWidgetCount = std::count_if(first: widgets.begin(), last: widgets.end(), |
674 | pred: [](const QLineEditPrivate::SideWidgetEntry &e) { |
675 | #if QT_CONFIG(toolbutton) && QT_CONFIG(animation) |
676 | // a button that's fading out doesn't get any space |
677 | if (auto* iconButton = qobject_cast<QLineEditIconButton*>(object: e.widget)) |
678 | return iconButton->needsSpace(); |
679 | |
680 | #endif |
681 | return e.widget->isVisibleTo(e.widget->parentWidget()); |
682 | }); |
683 | |
684 | return defaultMargin + (parameters.margin + parameters.widgetWidth) * visibleSideWidgetCount; |
685 | } |
686 | |
687 | QMargins QLineEditPrivate::effectiveTextMargins() const |
688 | { |
689 | return {effectiveTextMargin(defaultMargin: textMargins.left(), widgets: leftSideWidgetList(), parameters: sideWidgetParameters()), |
690 | textMargins.top(), |
691 | effectiveTextMargin(defaultMargin: textMargins.right(), widgets: rightSideWidgetList(), parameters: sideWidgetParameters()), |
692 | textMargins.bottom()}; |
693 | } |
694 | |
695 | |
696 | QT_END_NAMESPACE |
697 | |
698 | #include "moc_qlineedit_p.cpp" |
699 | |