diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2021-05-04 15:48:28 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-05-11 15:04:25 +0000 |
commit | a2b56c3661d76302e818864868e2dba656185b75 (patch) | |
tree | bdaec79b7910032582fcaff73f2fbb994d4ad8af | |
parent | 5d562efcdb8912b5538d211f04a08fdfefa49cfe (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: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/quicktemplates2/CMakeLists.txt | 1 | ||||
-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-- | tests/auto/controls/data/tst_tooltip.qml | 18 |
6 files changed, 230 insertions, 4 deletions
diff --git a/src/quicktemplates2/CMakeLists.txt b/src/quicktemplates2/CMakeLists.txt index a5052ee4..47633513 100644 --- a/src/quicktemplates2/CMakeLists.txt +++ b/src/quicktemplates2/CMakeLists.txt @@ -111,6 +111,7 @@ qt_internal_add_module(QuickTemplates2 qquicktoolbutton.cpp qquicktoolbutton_p.h qquicktoolseparator.cpp qquicktoolseparator_p.h qquicktooltip.cpp qquicktooltip_p.h + qquicktooltippopupitem.cpp qquicktooltippopupitem_p_p.h qquickvelocitycalculator.cpp qquickvelocitycalculator_p_p.h qtquicktemplates2global.cpp qtquicktemplates2global_p.h diff --git a/src/quicktemplates2/qquickpane_p_p.h b/src/quicktemplates2/qquickpane_p_p.h index bd89aee2..7dac4f3c 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 96c57354..a6ad371a 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(); @@ -141,6 +144,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); @@ -175,9 +191,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..c3a48f6f --- /dev/null +++ b/src/quicktemplates2/qquicktooltippopupitem.cpp @@ -0,0 +1,106 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $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..d81c309d --- /dev/null +++ b/src/quicktemplates2/qquicktooltippopupitem_p_p.h @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** 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 http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $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/tests/auto/controls/data/tst_tooltip.qml b/tests/auto/controls/data/tst_tooltip.qml index b4e992a4..2f98a5f3 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 } @@ -446,6 +451,19 @@ TestCase { 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) + } + function test_timeoutAfterOpened() { let control = createTemporaryObject(toolTipWithExitTransition, testCase, { timeout: 1, exit: null }) verify(control) |