summaryrefslogtreecommitdiffstats
path: root/src/widgets/widgets/qlineedit_p.cpp
diff options
context:
space:
mode:
authorVitaly Fanaskov <vitaly.fanaskov@qt.io>2018-11-22 14:39:51 +0100
committerVitaly Fanaskov <vitaly.fanaskov@qt.io>2018-11-26 16:03:33 +0000
commitba13c6c08f30a4c2f188f69deeaf4ca6a020d7a1 (patch)
tree9514aa7670bf143de6d258a429a5d02d494dee0d /src/widgets/widgets/qlineedit_p.cpp
parent70d131af33c8b411f430d0699b4294b5976db8e3 (diff)
Fix calculation of text margin if line edit contains side widgets
The previous implementation leads to infinite chain of showing/hidden line edit under circumstances described in QTBUG-54676. We basically got the situation when size hint were calculated differently depending on the line edit visibility state. In this case toolbar layout have to show/hide extension button and line edit a lot of times and can never leave this "loop" (please note, that the chain is much more complicated in reality): Resize toolbar -> Set layout geometry -> Size is OK to display line edit -> Set layout geometry -> Hide extension button -> Set layout geometry (wrong size is calculated here, so "run out of space") -> Hide line edit -> Set layout geometry -> Show extension button -> Set layout geometry - > Size is OK to display line edit ... And we're in the "loop" Clear button is hidden if there is no text in a line edit. In the previous implementation, the button was always visible, only opacity was changing in order to "hide" the button. It resulted to incorrect size hints (regular and minimum). In the current implementation the button is really hidden/shown, and size hints calculated correctly. Also updated unit test for line edit. Remove code duplication in functions for calculation text margin Fixes: QTBUG-54676 Change-Id: I4549c9ea98e10b750ba855a07037f6392276358b Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'src/widgets/widgets/qlineedit_p.cpp')
-rw-r--r--src/widgets/widgets/qlineedit_p.cpp98
1 files changed, 70 insertions, 28 deletions
diff --git a/src/widgets/widgets/qlineedit_p.cpp b/src/widgets/widgets/qlineedit_p.cpp
index df8d534afa..6dcb2dd693 100644
--- a/src/widgets/widgets/qlineedit_p.cpp
+++ b/src/widgets/widgets/qlineedit_p.cpp
@@ -392,9 +392,47 @@ void QLineEditIconButton::setOpacity(qreal value)
}
#if QT_CONFIG(animation)
+bool QLineEditIconButton::shouldHideWithText() const
+{
+ return m_hideWithText;
+}
+
+void QLineEditIconButton::setHideWithText(bool hide)
+{
+ m_hideWithText = hide;
+}
+
+void QLineEditIconButton::onAnimationFinished()
+{
+ if (shouldHideWithText() && isVisible() && !m_wasHidden) {
+ hide();
+
+ // Invalidate previous geometry to take into account new size of side widgets
+ if (auto le = lineEditPrivate())
+ le->updateGeometry_helper(true);
+ }
+}
+
+void QLineEditIconButton::animateShow(bool visible)
+{
+ m_wasHidden = visible;
+
+ if (shouldHideWithText() && !isVisible()) {
+ show();
+
+ // Invalidate previous geometry to take into account new size of side widgets
+ if (auto le = lineEditPrivate())
+ le->updateGeometry_helper(true);
+ }
+
+ startOpacityAnimation(visible ? 1.0 : 0.0);
+}
+
void QLineEditIconButton::startOpacityAnimation(qreal endValue)
{
QPropertyAnimation *animation = new QPropertyAnimation(this, QByteArrayLiteral("opacity"));
+ connect(animation, &QPropertyAnimation::finished, this, &QLineEditIconButton::onAnimationFinished);
+
animation->setDuration(160);
animation->setEndValue(endValue);
animation->start(QAbstractAnimation::DeleteWhenStopped);
@@ -409,6 +447,16 @@ void QLineEditIconButton::updateCursor()
}
#endif // QT_CONFIG(toolbutton)
+#if QT_CONFIG(animation) && QT_CONFIG(toolbutton)
+static void displayWidgets(const QLineEditPrivate::SideWidgetEntryList &widgets, bool display)
+{
+ for (const auto &e : widgets) {
+ if (e.flags & QLineEditPrivate::SideWidgetFadeInWithText)
+ static_cast<QLineEditIconButton *>(e.widget)->animateShow(display);
+ }
+}
+#endif
+
void QLineEditPrivate::_q_textChanged(const QString &text)
{
if (hasSideWidgets()) {
@@ -416,15 +464,9 @@ void QLineEditPrivate::_q_textChanged(const QString &text)
if (!newTextSize || !lastTextSize) {
lastTextSize = newTextSize;
#if QT_CONFIG(animation) && QT_CONFIG(toolbutton)
- const bool fadeIn = newTextSize > 0;
- for (const SideWidgetEntry &e : leadingSideWidgets) {
- if (e.flags & SideWidgetFadeInWithText)
- static_cast<QLineEditIconButton *>(e.widget)->animateShow(fadeIn);
- }
- for (const SideWidgetEntry &e : trailingSideWidgets) {
- if (e.flags & SideWidgetFadeInWithText)
- static_cast<QLineEditIconButton *>(e.widget)->animateShow(fadeIn);
- }
+ const bool display = newTextSize > 0;
+ displayWidgets(leadingSideWidgets, display);
+ displayWidgets(trailingSideWidgets, display);
#endif
}
}
@@ -541,8 +583,15 @@ QWidget *QLineEditPrivate::addAction(QAction *newAction, QAction *before, QLineE
QLineEditIconButton *toolButton = new QLineEditIconButton(q);
toolButton->setIcon(newAction->icon());
toolButton->setOpacity(lastTextSize > 0 || !(flags & SideWidgetFadeInWithText) ? 1 : 0);
- if (flags & SideWidgetClearButton)
+ if (flags & SideWidgetClearButton) {
QObject::connect(toolButton, SIGNAL(clicked()), q, SLOT(_q_clearButtonClicked()));
+
+#if QT_CONFIG(animation)
+ // The clear button is handled only by this widget. The button should be really
+ // shown/hidden in order to calculate size hints correctly.
+ toolButton->setHideWithText(true);
+#endif
+ }
toolButton->setDefaultAction(newAction);
w = toolButton;
#else
@@ -606,33 +655,26 @@ void QLineEditPrivate::removeAction(QAction *action)
#endif // QT_CONFIG(action)
}
-static bool isSideWidgetVisible(const QLineEditPrivate::SideWidgetEntry &e)
+static int effectiveTextMargin(int defaultMargin, const QLineEditPrivate::SideWidgetEntryList &widgets,
+ const QLineEditPrivate::SideWidgetParameters &parameters)
{
- return e.widget->isVisible();
+ if (widgets.empty())
+ return defaultMargin;
+
+ return defaultMargin + (parameters.margin + parameters.widgetWidth) *
+ int(std::count_if(widgets.begin(), widgets.end(),
+ [](const QLineEditPrivate::SideWidgetEntry &e) {
+ return e.widget->isVisibleTo(e.widget->parentWidget()); }));
}
int QLineEditPrivate::effectiveLeftTextMargin() const
{
- int result = leftTextMargin;
- if (!leftSideWidgetList().empty()) {
- const SideWidgetParameters p = sideWidgetParameters();
- result += (p.margin + p.widgetWidth)
- * int(std::count_if(leftSideWidgetList().begin(), leftSideWidgetList().end(),
- isSideWidgetVisible));
- }
- return result;
+ return effectiveTextMargin(leftTextMargin, leftSideWidgetList(), sideWidgetParameters());
}
int QLineEditPrivate::effectiveRightTextMargin() const
{
- int result = rightTextMargin;
- if (!rightSideWidgetList().empty()) {
- const SideWidgetParameters p = sideWidgetParameters();
- result += (p.margin + p.widgetWidth)
- * int(std::count_if(rightSideWidgetList().begin(), rightSideWidgetList().end(),
- isSideWidgetVisible));
- }
- return result;
+ return effectiveTextMargin(rightTextMargin, rightSideWidgetList(), sideWidgetParameters());
}