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