aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@qt.io>2021-05-04 15:48:28 +0200
committerMitch Curtis <mitch.curtis@qt.io>2021-05-11 15:47:19 +0200
commita063cd0be5e8f108a0084831856f4af8c0e9159c (patch)
treeea7f6b7c63a28debff7799df35d0c1adfda2a012 /src
parent468def83fc713e329ef9b2e150653837fe895159 (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 Pick-to: 6.1 5.15 Change-Id: I7ca3805429acb68a13ead8f3545bb84a51fb1b72 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/quicktemplates2/CMakeLists.txt1
-rw-r--r--src/quicktemplates2/qquickpane_p_p.h2
-rw-r--r--src/quicktemplates2/qquicktooltip.cpp20
-rw-r--r--src/quicktemplates2/qquicktooltippopupitem.cpp106
-rw-r--r--src/quicktemplates2/qquicktooltippopupitem_p_p.h87
5 files changed, 212 insertions, 4 deletions
diff --git a/src/quicktemplates2/CMakeLists.txt b/src/quicktemplates2/CMakeLists.txt
index 0e8f7d2c..f0103b17 100644
--- a/src/quicktemplates2/CMakeLists.txt
+++ b/src/quicktemplates2/CMakeLists.txt
@@ -110,6 +110,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