diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2021-05-04 15:48:28 +0200 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@qt.io> | 2021-06-09 15:52:57 +0200 |
commit | 10ede696d063de6bd7d3484bdfc8b83cb2ebdd68 (patch) | |
tree | 6e0d23c67b106341904ee7b4d14f2a3aba1b6951 | |
parent | 104cd475f255a826671e4b624e97dd39e05e3fba (diff) |
ToolTip: use contentWidth of Text contentItem to account for newlines
By default, QQuickPopupItem uses the implicitWidth of its contentItem,
which is too large in the case of a ToolTip with newlines in the text.
In that case, contentWidth refers to the width of the text including
newlines, so we use that instead.
[ChangeLog][Controls][ToolTip] The implicit width of ToolTips now
accounts for newlines in the text. If you want to use the old behavior,
set ToolTip's contentWidth to implicitContentWidth.
Fixes: QTBUG-83630
Change-Id: I7ca3805429acb68a13ead8f3545bb84a51fb1b72
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit a063cd0be5e8f108a0084831856f4af8c0e9159c)
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
-rw-r--r-- | src/quicktemplates2/qquickpane_p_p.h | 2 | ||||
-rw-r--r-- | src/quicktemplates2/qquicktooltip.cpp | 20 | ||||
-rw-r--r-- | src/quicktemplates2/qquicktooltippopupitem.cpp | 106 | ||||
-rw-r--r-- | src/quicktemplates2/qquicktooltippopupitem_p_p.h | 87 | ||||
-rw-r--r-- | src/quicktemplates2/quicktemplates2.pri | 2 | ||||
-rw-r--r-- | tests/auto/controls/data/tst_tooltip.qml | 22 |
6 files changed, 233 insertions, 6 deletions
diff --git a/src/quicktemplates2/qquickpane_p_p.h b/src/quicktemplates2/qquickpane_p_p.h index 579371f4..4d82cc32 100644 --- a/src/quicktemplates2/qquickpane_p_p.h +++ b/src/quicktemplates2/qquickpane_p_p.h @@ -75,7 +75,7 @@ public: void contentChildrenChange(); - void updateContentWidth(); + virtual void updateContentWidth(); void updateContentHeight(); bool hasContentWidth = false; diff --git a/src/quicktemplates2/qquicktooltip.cpp b/src/quicktemplates2/qquicktooltip.cpp index 19c4cc83..cb08127c 100644 --- a/src/quicktemplates2/qquicktooltip.cpp +++ b/src/quicktemplates2/qquicktooltip.cpp @@ -38,6 +38,7 @@ #include "qquickpopup_p_p.h" #include "qquickpopupitem_p_p.h" #include "qquickcontrol_p_p.h" +#include "qquicktooltippopupitem_p_p.h" #include <QtCore/qbasictimer.h> #include <QtQml/qqmlinfo.h> @@ -124,6 +125,8 @@ class QQuickToolTipPrivate : public QQuickPopupPrivate Q_DECLARE_PUBLIC(QQuickToolTip) public: + void init() override; + void startDelay(); void stopDelay(); @@ -137,6 +140,19 @@ public: QBasicTimer timeoutTimer; }; +void QQuickToolTipPrivate::init() +{ + Q_Q(QQuickToolTip); + popupItem = new QQuickToolTipPopupItem(q); + popupItem->setVisible(false); + q->setParentItem(qobject_cast<QQuickItem *>(parent)); + connectToPopupItem(); + + allowVerticalFlip = true; + allowHorizontalFlip = true; + popupItem->setHoverEnabled(false); // QTBUG-63644 +} + void QQuickToolTipPrivate::startDelay() { Q_Q(QQuickToolTip); @@ -165,9 +181,7 @@ QQuickToolTip::QQuickToolTip(QQuickItem *parent) : QQuickPopup(*(new QQuickToolTipPrivate), parent) { Q_D(QQuickToolTip); - d->allowVerticalFlip = true; - d->allowHorizontalFlip = true; - d->popupItem->setHoverEnabled(false); // QTBUG-63644 + d->init(); } /*! diff --git a/src/quicktemplates2/qquicktooltippopupitem.cpp b/src/quicktemplates2/qquicktooltippopupitem.cpp new file mode 100644 index 00000000..49455ca9 --- /dev/null +++ b/src/quicktemplates2/qquicktooltippopupitem.cpp @@ -0,0 +1,106 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:COMM$ +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** $QT_END_LICENSE$ +** +** +** +** +** +** +** +** +** +** +** +** +** +** +** +** +****************************************************************************/ + +#include "qquicktooltippopupitem_p_p.h" + +#include <QtQuickTemplates2/private/qquickpopup_p.h> + +QT_BEGIN_NAMESPACE + +QQuickToolTipPopupItemPrivate::QQuickToolTipPopupItemPrivate(QQuickPopup *popup) + : QQuickPopupItemPrivate(popup) +{ +} + +qreal QQuickToolTipPopupItemPrivate::getContentWidth() const +{ + auto textItem = qobject_cast<QQuickText*>(contentItem); + if (textItem) + return textItem->contentWidth(); + + return contentItem ? contentItem->implicitWidth() : 0; +} + +void QQuickToolTipPopupItemPrivate::updateContentWidth() +{ + Q_Q(QQuickToolTipPopupItem); + // Don't need to calculate the implicit contentWidth if an explicit one was set. + if (hasContentWidth) + return; + + auto textItem = qobject_cast<QQuickText*>(contentItem); + if (!textItem) { + // It's not a Text item, so use the base contentWidth logic (i.e. use implicitWidth). + QQuickPopupItemPrivate::updateContentWidth(); + return; + } + + const qreal oldContentWidth = contentWidth; + const qreal newContentWidth = textItem->contentWidth(); + if (qFuzzyCompare(oldContentWidth, newContentWidth)) + return; + + contentWidth = newContentWidth; + q->contentSizeChange(QSizeF(contentWidth, contentHeight), QSizeF(oldContentWidth, contentHeight)); + emit q->contentWidthChanged(); +} + +QQuickToolTipPopupItem::QQuickToolTipPopupItem(QQuickPopup *popup) + : QQuickPopupItem(*(new QQuickToolTipPopupItemPrivate(popup))) +{ +} + +void QQuickToolTipPopupItem::contentItemChange(QQuickItem *newItem, QQuickItem *oldItem) +{ + Q_D(QQuickToolTipPopupItem); + QQuickPopupItem::contentItemChange(newItem, oldItem); + + // Text's implicitWidth does not account for newlines and hence is too large, + // so we need to listen to contentWidth's change signals. + auto oldTextItem = qobject_cast<QQuickText*>(oldItem); + if (oldTextItem) { + QObjectPrivate::disconnect(oldTextItem, &QQuickText::contentWidthChanged, + d, &QQuickToolTipPopupItemPrivate::updateContentWidth); + } + + auto newTextItem = qobject_cast<QQuickText*>(newItem); + if (newTextItem) { + QObjectPrivate::connect(newTextItem, &QQuickText::contentWidthChanged, + d, &QQuickToolTipPopupItemPrivate::updateContentWidth); + } +} + +QT_END_NAMESPACE diff --git a/src/quicktemplates2/qquicktooltippopupitem_p_p.h b/src/quicktemplates2/qquicktooltippopupitem_p_p.h new file mode 100644 index 00000000..a86dfd9d --- /dev/null +++ b/src/quicktemplates2/qquicktooltippopupitem_p_p.h @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:COMM$ +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** $QT_END_LICENSE$ +** +** +** +** +** +** +** +** +** +** +** +** +** +** +** +** +****************************************************************************/ + +#ifndef QQUICKTOOLTIPPOPUPITEM_P_P_H +#define QQUICKTOOLTIPPOPUPITEM_P_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtQuickTemplates2/private/qquickpopupitem_p.h> +#include <QtQuickTemplates2/private/qquickpopupitem_p_p.h> + +QT_BEGIN_NAMESPACE + +class QQuickPopup; +class QQuickToolTipPopupItemPrivate; + +class QQuickToolTipPopupItem : public QQuickPopupItem +{ + Q_OBJECT + +public: + explicit QQuickToolTipPopupItem(QQuickPopup *popup); + +protected: + void contentItemChange(QQuickItem *newItem, QQuickItem *oldItem) override; + +private: + Q_DISABLE_COPY(QQuickToolTipPopupItem) + Q_DECLARE_PRIVATE(QQuickToolTipPopupItem) +}; + +class QQuickToolTipPopupItemPrivate : public QQuickPopupItemPrivate +{ + Q_DECLARE_PUBLIC(QQuickToolTipPopupItem) + +public: + QQuickToolTipPopupItemPrivate(QQuickPopup *popup); + + qreal getContentWidth() const override; + void updateContentWidth() override; +}; + +QT_END_NAMESPACE + +#endif // QQUICKTOOLTIPPOPUPITEM_P_P_H diff --git a/src/quicktemplates2/quicktemplates2.pri b/src/quicktemplates2/quicktemplates2.pri index ba09591b..28eec3ef 100644 --- a/src/quicktemplates2/quicktemplates2.pri +++ b/src/quicktemplates2/quicktemplates2.pri @@ -99,6 +99,7 @@ HEADERS += \ $$PWD/qquicktoolbutton_p.h \ $$PWD/qquicktoolseparator_p.h \ $$PWD/qquicktooltip_p.h \ + $$PWD/qquicktooltippopupitem_p_p.h \ $$PWD/qquickvelocitycalculator_p_p.h SOURCES += \ @@ -172,6 +173,7 @@ SOURCES += \ $$PWD/qquicktoolbutton.cpp \ $$PWD/qquicktoolseparator.cpp \ $$PWD/qquicktooltip.cpp \ + $$PWD/qquicktooltippopupitem.cpp \ $$PWD/qquickvelocitycalculator.cpp qtConfig(quick-listview):qtConfig(quick-pathview) { diff --git a/tests/auto/controls/data/tst_tooltip.qml b/tests/auto/controls/data/tst_tooltip.qml index 6d45b09d..154eb2bf 100644 --- a/tests/auto/controls/data/tst_tooltip.qml +++ b/tests/auto/controls/data/tst_tooltip.qml @@ -75,6 +75,11 @@ TestCase { SignalSpy { } } + Component { + id: itemComponent + Item {} + } + QtObject { id: object } @@ -218,8 +223,8 @@ TestCase { } function test_warning() { - ignoreWarning(Qt.resolvedUrl("tst_tooltip.qml") + ":78:5: QML QtObject: ToolTip must be attached to an Item") - ignoreWarning("<Unknown File>:1:30: QML ToolTip: cannot find any window to open popup in.") + ignoreWarning(new RegExp(".*QML QtObject: ToolTip must be attached to an Item")) + ignoreWarning(new RegExp(".*QML ToolTip: cannot find any window to open popup in.")) object.ToolTip.show("") // don't crash (QTBUG-56243) } @@ -474,4 +479,17 @@ TestCase { compare(item.ToolTip.toolTip.contentItem.wrapMode, Text.Wrap) verify(item.ToolTip.toolTip.contentItem.width < item.ToolTip.toolTip.contentItem.implicitWidth) } + + // QTBUG-83630: Test that newlines are accounted for in the implicit contentWidth. + function test_newLines() { + var item = createTemporaryObject(itemComponent, testCase) + verify(item) + + item.ToolTip.show("This is one line of text\nThis is another line of text") + + // The implicitWidth of the Text item for the text above will be larger than + // its contentWidth. ToolTip's implicitWidth uses contentWidth in its calculation, + // so we check that it's less than the Text's implicitWidth. + verify(item.ToolTip.toolTip.implicitWidth < item.ToolTip.toolTip.contentItem.implicitWidth) + } } |