9#if QT_CONFIG(itemviews)
10#include "qabstractitemview.h"
12#if QT_CONFIG(draganddrop)
16# include "qwidgetaction.h"
19#if QT_CONFIG(accessibility)
20#include "qaccessible.h"
27#if QT_CONFIG(animation)
28#include <qpropertyanimation.h>
30#include <qstylehints.h>
31#include <qvalidator.h>
35const int QLineEditPrivate::verticalMargin(1);
36const int QLineEditPrivate::horizontalMargin(2);
39QRect QLineEditPrivate::adjustedControlRect(
const QRect &rect)
const
41 QRect widgetRect = !rect.isEmpty() ? rect : q_func()->rect();
42 QRect cr = adjustedContentsRect();
43 int cix = cr.x() - hscroll + horizontalMargin;
44 return widgetRect.translated(QPoint(cix, vscroll - control->ascent() + q_func()->fontMetrics().ascent()));
47int QLineEditPrivate::xToPos(
int x, QTextLine::CursorPosition betweenOrOn)
const
49 QRect cr = adjustedContentsRect();
50 x-= cr.x() - hscroll + horizontalMargin;
51 return control->xToPos(x, betweenOrOn);
54QString QLineEditPrivate::textBeforeCursor(
int curPos)
const
56 const QString &text = control->text();
57 return text.mid(0, curPos);
60QString QLineEditPrivate::textAfterCursor(
int curPos)
const
62 const QString &text = control->text();
63 return text.mid(curPos);
66bool QLineEditPrivate::inSelection(
int x)
const
68 x -= adjustedContentsRect().x() - hscroll + horizontalMargin;
69 return control->inSelection(x);
72QRect QLineEditPrivate::cursorRect()
const
74 return adjustedControlRect(control->cursorRect());
77#if QT_CONFIG(completer)
78void QLineEditPrivate::connectCompleter()
81 QObject::connect(control->completer(), qOverload<
const QString &>(&QCompleter::activated),
82 q, &QLineEdit::setText);
83 QObjectPrivate::connect(control->completer(), qOverload<
const QString &>(&QCompleter::highlighted),
84 this, &QLineEditPrivate::completionHighlighted);
87void QLineEditPrivate::disconnectCompleter()
90 QObject::disconnect(control->completer(), qOverload<
const QString &>(&QCompleter::activated),
91 q, &QLineEdit::setText);
92 QObjectPrivate::disconnect(control->completer(), qOverload<
const QString &>(&QCompleter::highlighted),
93 this, &QLineEditPrivate::completionHighlighted);
96void QLineEditPrivate::completionHighlighted(
const QString &newText)
99 if (control->completer()->completionMode() != QCompleter::InlineCompletion) {
102 int c = control->cursor();
103 QString text = control->text();
104 q->setText(QStringView{text}.left(c) + QStringView{newText}.mid(c));
105 control->moveCursor(control->end(),
false);
106 control->moveCursor(c,
true);
112void QLineEditPrivate::handleWindowActivate()
115 if (!q->hasFocus() && control->hasSelectedText())
119void QLineEditPrivate::textEdited(
const QString &text)
123 emit q->textEdited(text);
124#if QT_CONFIG(completer)
125 if (control->completer()
126 && control->completer()->completionMode() != QCompleter::InlineCompletion)
127 control->complete(-1);
131void QLineEditPrivate::cursorPositionChanged(
int from,
int to)
135 emit q->cursorPositionChanged(from, to);
138void QLineEditPrivate::selectionChanged()
141 if (control->preeditAreaText().isEmpty()) {
142 QStyleOptionFrame opt;
143 q->initStyleOption(&opt);
144 bool showCursor = control->hasSelectedText() ?
145 q->style()->styleHint(QStyle::SH_BlinkCursorWhenTextSelected, &opt, q):
147 setCursorVisible(showCursor);
150 emit q->selectionChanged();
151#if QT_CONFIG(accessibility)
152 QAccessibleTextSelectionEvent ev(q, control->selectionStart(), control->selectionEnd());
153 ev.setCursorPosition(control->cursorPosition());
154 QAccessible::updateAccessibility(&ev);
158void QLineEditPrivate::updateNeeded(
const QRect &rect)
160 q_func()->update(adjustedControlRect(rect));
163void QLineEditPrivate::init(
const QString& txt)
167 const auto qUpdateMicroFocus = [q]()
169 q->updateMicroFocus();
171 control =
new QWidgetLineControl(txt);
172 control->setParent(q);
173 control->setFont(q->font());
174 QObject::connect(control, &QWidgetLineControl::textChanged,
175 q, &QLineEdit::textChanged);
176 QObjectPrivate::connect(control, &QWidgetLineControl::textEdited,
177 this, &QLineEditPrivate::textEdited);
178 QObjectPrivate::connect(control, &QWidgetLineControl::cursorPositionChanged,
179 this, &QLineEditPrivate::cursorPositionChanged);
180 QObjectPrivate::connect(control, &QWidgetLineControl::selectionChanged,
181 this, &QLineEditPrivate::selectionChanged);
182 QObjectPrivate::connect(control, &QWidgetLineControl::editingFinished,
183 this, &QLineEditPrivate::controlEditingFinished);
184 QObject::connect(control, &QWidgetLineControl::cursorPositionChanged,
185 q, qUpdateMicroFocus);
187 QObject::connect(control, &QWidgetLineControl::textChanged,
188 q, qUpdateMicroFocus);
190 QObject::connect(control, &QWidgetLineControl::updateMicroFocus,
191 q, qUpdateMicroFocus);
194 QObject::connect(control, &QWidgetLineControl::selectionChanged,
195 q, qOverload<>(&QLineEdit::update));
197 QObject::connect(control, &QWidgetLineControl::selectionChanged,
198 q, qUpdateMicroFocus);
200 QObject::connect(control, &QWidgetLineControl::displayTextChanged,
201 q, qOverload<>(&QLineEdit::update));
203 QObjectPrivate::connect(control, &QWidgetLineControl::updateNeeded,
204 this, &QLineEditPrivate::updateNeeded);
205 QObject::connect(control, &QWidgetLineControl::inputRejected,
206 q, &QLineEdit::inputRejected);
208 QStyleOptionFrame opt;
209 q->initStyleOption(&opt);
210 control->setPasswordCharacter(
char16_t(q->style()->styleHint(QStyle::SH_LineEdit_PasswordCharacter, &opt, q)));
211 control->setPasswordMaskDelay(q->style()->styleHint(QStyle::SH_LineEdit_PasswordMaskDelay, &opt, q));
213 q->setCursor(Qt::IBeamCursor);
215 q->setFocusPolicy(Qt::StrongFocus);
216 q->setAttribute(Qt::WA_InputMethodEnabled);
219 q->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed, QSizePolicy::LineEdit));
220 q->setBackgroundRole(QPalette::Base);
221 q->setAttribute(Qt::WA_KeyCompression);
222 q->setMouseTracking(
true);
223 q->setAcceptDrops(
true);
225 q->setAttribute(Qt::WA_MacShowFocusRect);
227 initMouseYThreshold();
230void QLineEditPrivate::initMouseYThreshold()
232 mouseYThreshold = QGuiApplication::styleHints()->mouseQuickSelectionThreshold();
235QRect QLineEditPrivate::adjustedContentsRect()
const
237 Q_Q(
const QLineEdit);
238 QStyleOptionFrame opt;
239 q->initStyleOption(&opt);
240 QRect r = q->style()->subElementRect(QStyle::SE_LineEditContents, &opt, q);
241 r = r.marginsRemoved(effectiveTextMargins());
245void QLineEditPrivate::setCursorVisible(
bool visible)
248 if ((
bool)cursorVisible == visible)
250 cursorVisible = visible;
251 if (control->inputMask().isEmpty())
252 q->update(cursorRect());
257void QLineEditPrivate::setText(
const QString& text)
260 control->setText(text);
263void QLineEditPrivate::updatePasswordEchoEditing(
bool editing)
266 control->updatePasswordEchoEditing(editing);
267 q->setAttribute(Qt::WA_InputMethodEnabled, shouldEnableInputMethod());
270void QLineEditPrivate::resetInputMethod()
273 if (q->hasFocus() && qApp) {
274 QGuiApplication::inputMethod()->reset();
279
280
281
282
285
286
287
288
289bool QLineEditPrivate::sendMouseEventToInputContext( QMouseEvent *e )
292 if ( control->composeMode() ) {
293 int tmp_cursor = xToPos(e->position().toPoint().x());
294 int mousePos = tmp_cursor - control->cursor();
295 if ( mousePos < 0 || mousePos > control->preeditAreaText().size() )
299 if (e->type() == QEvent::MouseButtonRelease)
300 QGuiApplication::inputMethod()->invokeAction(QInputMethod::Click, mousePos);
312#if QT_CONFIG(draganddrop)
313void QLineEditPrivate::drag()
317 QMimeData *data =
new QMimeData;
318 data->setText(control->selectedText());
319 QDrag *drag =
new QDrag(q);
320 drag->setMimeData(data);
321 Qt::DropAction action = drag->exec(Qt::CopyAction);
322 if (action == Qt::MoveAction && !control->isReadOnly() && drag->target() != q)
323 control->removeSelection();
328#if QT_CONFIG(toolbutton)
329QLineEditIconButton::QLineEditIconButton(QWidget *parent)
330 : QToolButton(parent)
333 setFocusPolicy(Qt::NoFocus);
336QLineEditPrivate *QLineEditIconButton::lineEditPrivate()
const
338 QLineEdit *le = qobject_cast<QLineEdit *>(parentWidget());
339 return le ?
static_cast<QLineEditPrivate *>(qt_widget_private(le)) :
nullptr;
342void QLineEditIconButton::paintEvent(QPaintEvent *)
344 QPainter painter(
this);
345 QIcon::Mode mode = QIcon::Disabled;
347 mode = isDown() ? QIcon::Active : QIcon::Normal;
348 const QLineEditPrivate *lep = lineEditPrivate();
349 const int iconWidth = lep ? lep->sideWidgetParameters().iconSize : 16;
350 const QSize iconSize(iconWidth, iconWidth);
351 QRect pixmapRect = QRect(QPoint(0, 0), iconSize);
352 pixmapRect.moveCenter(rect().center());
353 painter.setOpacity(m_opacity);
354 icon().paint(&painter, pixmapRect, Qt::AlignCenter, mode, QIcon::Off);
357void QLineEditIconButton::actionEvent(QActionEvent *e)
360 case QEvent::ActionChanged: {
361 const auto *action = e->action();
362 if (isVisibleTo(parentWidget()) != action->isVisible()) {
363 setVisible(action->isVisible());
364 if (QLineEditPrivate *lep = lineEditPrivate())
365 lep->positionSideWidgets();
372 QToolButton::actionEvent(e);
375void QLineEditIconButton::setOpacity(qreal value)
377 if (!qFuzzyCompare(m_opacity, value)) {
384#if QT_CONFIG(animation)
385bool QLineEditIconButton::shouldHideWithText()
const
387 return m_hideWithText;
390void QLineEditIconButton::setHideWithText(
bool hide)
392 m_hideWithText = hide;
395void QLineEditIconButton::onAnimationFinished()
397 if (shouldHideWithText() && isVisible() && m_fadingOut) {
402 if (
auto le = lineEditPrivate())
403 le->updateGeometry_helper(
true);
407void QLineEditIconButton::animateShow(
bool visible)
409 m_fadingOut = !visible;
411 if (shouldHideWithText() && !isVisible()) {
415 if (
auto le = lineEditPrivate())
416 le->updateGeometry_helper(
true);
419 startOpacityAnimation(visible ? 1.0 : 0.0);
422void QLineEditIconButton::startOpacityAnimation(qreal endValue)
424 QPropertyAnimation *animation =
new QPropertyAnimation(
this, QByteArrayLiteral(
"opacity"),
this);
425 connect(animation, &QPropertyAnimation::finished,
this, &QLineEditIconButton::onAnimationFinished);
427 animation->setDuration(160);
428 animation->setEndValue(endValue);
429 animation->start(QAbstractAnimation::DeleteWhenStopped);
433void QLineEditIconButton::updateCursor()
436 setCursor(qFuzzyCompare(m_opacity, qreal(1.0)) || !parentWidget() ? QCursor(Qt::ArrowCursor) : parentWidget()->cursor());
441#if QT_CONFIG(animation) && QT_CONFIG(toolbutton)
442static void displayWidgets(
const QLineEditPrivate::SideWidgetEntryList &widgets,
bool display)
444 for (
const auto &e : widgets) {
445 if (e.flags & QLineEditPrivate::SideWidgetFadeInWithText)
446 static_cast<QLineEditIconButton *>(e.widget)->animateShow(display);
451void QLineEditPrivate::textChanged(
const QString &text)
453 if (hasSideWidgets()) {
454 const int newTextSize = text.size();
455 if (!newTextSize || !lastTextSize) {
456 lastTextSize = newTextSize;
457#if QT_CONFIG(animation) && QT_CONFIG(toolbutton)
458 const bool display = newTextSize > 0;
459 displayWidgets(leadingSideWidgets, display);
460 displayWidgets(trailingSideWidgets, display);
466void QLineEditPrivate::clearButtonClicked()
469 if (!q->text().isEmpty()) {
471 textEdited(QString());
475void QLineEditPrivate::controlEditingFinished()
479 emit q->returnPressed();
480 emit q->editingFinished();
483QLineEditPrivate::SideWidgetParameters QLineEditPrivate::sideWidgetParameters()
const
485 Q_Q(
const QLineEdit);
486 SideWidgetParameters result;
487 result.iconSize = q->style()->pixelMetric(QStyle::PM_LineEditIconSize,
nullptr, q);
488 result.margin = q->style()->pixelMetric(QStyle::PM_LineEditIconMargin,
nullptr, q);
489 result.widgetWidth = result.iconSize + 6;
490 result.widgetHeight = result.iconSize + 2;
494QIcon QLineEditPrivate::clearButtonIcon()
const
496 Q_Q(
const QLineEdit);
497 QStyleOptionFrame styleOption;
498 q->initStyleOption(&styleOption);
499 return q->style()->standardIcon(QStyle::SP_LineEditClearButton, &styleOption, q);
502void QLineEditPrivate::setClearButtonEnabled(
bool enabled)
505 for (
const SideWidgetEntry &e : trailingSideWidgets) {
506 if (e.flags & SideWidgetClearButton) {
507 e.action->setEnabled(enabled);
516void QLineEditPrivate::positionSideWidgets()
519 if (hasSideWidgets()) {
520 const QRect contentRect = q->rect();
521 const SideWidgetParameters p = sideWidgetParameters();
522 const int delta = p.margin + p.widgetWidth;
523 QRect widgetGeometry(QPoint(p.margin, (contentRect.height() - p.widgetHeight) / 2),
524 QSize(p.widgetWidth, p.widgetHeight));
525 for (
const SideWidgetEntry &e : leftSideWidgetList()) {
526 e.widget->setGeometry(widgetGeometry);
528 if (e.action->isVisible())
529 widgetGeometry.moveLeft(widgetGeometry.left() + delta);
534 widgetGeometry.moveLeft(contentRect.width() - p.widgetWidth - p.margin);
535 for (
const SideWidgetEntry &e : rightSideWidgetList()) {
536 e.widget->setGeometry(widgetGeometry);
538 if (e.action->isVisible())
539 widgetGeometry.moveLeft(widgetGeometry.left() - delta);
546QLineEditPrivate::SideWidgetLocation QLineEditPrivate::findSideWidget(
const QAction *a)
const
549 for (
const auto &e : leadingSideWidgets) {
551 return {QLineEdit::LeadingPosition, i};
555 for (
const auto &e : trailingSideWidgets) {
557 return {QLineEdit::TrailingPosition, i};
560 return {QLineEdit::LeadingPosition, -1};
563QWidget *QLineEditPrivate::addAction(QAction *newAction, QAction *before, QLineEdit::ActionPosition position,
int flags)
568 if (!hasSideWidgets()) {
569 QObjectPrivate::connect(q, &QLineEdit::textChanged,
570 this, &QLineEditPrivate::textChanged);
571 lastTextSize = q->text().size();
573 QWidget *w =
nullptr;
576 if (QWidgetAction *widgetAction = qobject_cast<QWidgetAction *>(newAction)) {
577 if ((w = widgetAction->requestWidget(q)))
578 flags |= SideWidgetCreatedByWidgetAction;
581#if QT_CONFIG(toolbutton)
582 QLineEditIconButton *toolButton =
new QLineEditIconButton(q);
583 toolButton->setIcon(newAction->icon());
584 toolButton->setOpacity(lastTextSize > 0 || !(flags & SideWidgetFadeInWithText) ? 1 : 0);
585 if (flags & SideWidgetClearButton) {
586 QObjectPrivate::connect(toolButton, &QToolButton::clicked,
587 this, &QLineEditPrivate::clearButtonClicked);
589#if QT_CONFIG(animation)
592 toolButton->setHideWithText(
true);
595 toolButton->setDefaultAction(newAction);
603 if (!before && !(flags & SideWidgetClearButton) && position == QLineEdit::TrailingPosition) {
604 for (
const SideWidgetEntry &e : trailingSideWidgets) {
605 if (e.flags & SideWidgetClearButton) {
621 const SideWidgetLocation loc = {position, -1};
622 const auto location = before ? findSideWidget(before) : loc;
624 const auto location = before ? findSideWidget(before) : SideWidgetLocation{position, -1};
627 SideWidgetEntryList &list = location.position == QLineEdit::TrailingPosition ? trailingSideWidgets : leadingSideWidgets;
628 list.insert(location.isValid() ? list.begin() + location.index : list.end(),
629 SideWidgetEntry(w, newAction, flags));
630 positionSideWidgets();
635void QLineEditPrivate::removeAction(QAction *action)
638 const auto location = findSideWidget(action);
639 if (!location.isValid())
641 SideWidgetEntryList &list = location.position == QLineEdit::TrailingPosition ? trailingSideWidgets : leadingSideWidgets;
642 SideWidgetEntry entry = list[location.index];
643 list.erase(list.begin() + location.index);
644 if (entry.flags & SideWidgetCreatedByWidgetAction) {
649 if (
const auto a = qobject_cast<QWidgetAction*>(entry.action))
650 a->releaseWidget(entry.widget);
654 positionSideWidgets();
655 if (!hasSideWidgets())
656 QObjectPrivate::connect(q, &QLineEdit::textChanged,
657 this, &QLineEditPrivate::textChanged);
663 const QLineEditPrivate::SideWidgetParameters ¶meters)
666 return defaultMargin;
668 const auto visibleSideWidgetCount =
std::count_if(widgets.begin(), widgets.end(),
669 [](
const QLineEditPrivate::SideWidgetEntry &e) {
670#if QT_CONFIG(toolbutton) && QT_CONFIG(animation)
672 if (
auto* iconButton = qobject_cast<QLineEditIconButton*>(e.widget))
673 return iconButton->needsSpace();
676 return e.widget->isVisibleTo(e.widget->parentWidget());
679 return defaultMargin + (parameters.margin + parameters.widgetWidth) * visibleSideWidgetCount;
682QMargins QLineEditPrivate::effectiveTextMargins()
const
684 return {effectiveTextMargin(textMargins.left(), leftSideWidgetList(), sideWidgetParameters()),
686 effectiveTextMargin(textMargins.right(), rightSideWidgetList(), sideWidgetParameters()),
687 textMargins.bottom()};
693#include "moc_qlineedit_p.cpp"
Combined button and popup list for selecting options.
static int effectiveTextMargin(int defaultMargin, const QLineEditPrivate::SideWidgetEntryList &widgets, const QLineEditPrivate::SideWidgetParameters ¶meters)