From f21ad15ed638252f5b8a6871cf75e3d0c5d9c054 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20K=C3=BCgler?= Date: Thu, 9 Jan 2014 04:09:03 +0100 Subject: [PATCH] Fix up showing and hiding of the dialog ToolTipArea.containsMouse allows the use to update the tooltip way before it's shown, or when the contents of the tooltip should change within an open dialog. dismiss() and keepalive() allow handing over the tooltip dialog to another item, in order to allow for a smoother handover when a new item recycles the tooltipdialog. This avoids flickering when the tooltip moves from one item to another. --- src/declarativeimports/core/tooltip.cpp | 24 +++++++++++++++++++ src/declarativeimports/core/tooltip.h | 10 ++++++++ src/declarativeimports/core/tooltipdialog.cpp | 15 ++++++++++-- src/declarativeimports/core/tooltipdialog.h | 4 ++++ 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/declarativeimports/core/tooltip.cpp b/src/declarativeimports/core/tooltip.cpp index 3162b8bfe..5030b74ae 100644 --- a/src/declarativeimports/core/tooltip.cpp +++ b/src/declarativeimports/core/tooltip.cpp @@ -150,9 +150,32 @@ void ToolTip::setImage(const QVariant &image) emit imageChanged(); } +bool ToolTip::containsMouse() const +{ + return m_containsMouse; +} + +void ToolTip::setContainsMouse(bool contains) +{ + if (m_containsMouse != contains) { + m_containsMouse = contains; + emit containsMouseChanged(); + } + if (!contains) { + ToolTipDialog::instance()->dismiss(); + } +} + void ToolTip::hoverEnterEvent(QHoverEvent *event) { + setContainsMouse(true); + //m_showTimer->stop(); if (ToolTipDialog::instance()->isVisible()) { + // We signal the tooltipmanager that we're "potentially interested, + // and ask to keep it open for a bit, so other items get the chance + // to update the content before the tooltip hides -- this avoids + // flickering + ToolTipDialog::instance()->keepalive(); //FIXME: showToolTip needs to be renamed in sync or something like that showToolTip(); } else { @@ -162,6 +185,7 @@ void ToolTip::hoverEnterEvent(QHoverEvent *event) void ToolTip::hoverLeaveEvent(QHoverEvent *event) { + setContainsMouse(false); m_showTimer->stop(); } diff --git a/src/declarativeimports/core/tooltip.h b/src/declarativeimports/core/tooltip.h index 771589cb3..021890b61 100644 --- a/src/declarativeimports/core/tooltip.h +++ b/src/declarativeimports/core/tooltip.h @@ -58,6 +58,11 @@ class ToolTip : public QQuickItem */ Q_PROPERTY(QVariant icon READ icon WRITE setIcon NOTIFY iconChanged) + /** + * An icon for this tooltip, accepted values are an icon name, a QIcon, QImage or QPixmap + */ + Q_PROPERTY(bool m_containsMouse READ containsMouse NOTIFY containsMouseChanged) + /** * TODO: single property for images? * An image for this tooltip, accepted values are an icon name, a QIcon, QImage or QPixmap @@ -85,6 +90,9 @@ public: QVariant image() const; void setImage(const QVariant &image); + bool containsMouse() const; + void setContainsMouse(bool contains); + protected: bool childMouseEventFilter(QQuickItem *item, QEvent *event); void hoverEnterEvent(QHoverEvent *event); @@ -97,8 +105,10 @@ Q_SIGNALS: void subTextChanged(); void iconChanged(); void imageChanged(); + void containsMouseChanged(); private: + bool m_containsMouse; QWeakPointer m_mainItem; QTimer *m_showTimer; QString m_mainText; diff --git a/src/declarativeimports/core/tooltipdialog.cpp b/src/declarativeimports/core/tooltipdialog.cpp index e7d972272..366ff8044 100644 --- a/src/declarativeimports/core/tooltipdialog.cpp +++ b/src/declarativeimports/core/tooltipdialog.cpp @@ -31,7 +31,8 @@ Q_GLOBAL_STATIC(ToolTipDialog, toolTipDialogInstance) ToolTipDialog::ToolTipDialog(QQuickItem *parent) : DialogProxy(parent), - m_qmlObject(0) + m_qmlObject(0), + m_hideTimeout(4000) { setFlags(Qt::ToolTip); setLocation(Plasma::Types::Floating); @@ -69,11 +70,21 @@ QQuickItem *ToolTipDialog::loadDefaultItem() void ToolTipDialog::showEvent(QShowEvent *event) { - m_showTimer->start(4000); + m_showTimer->start(m_hideTimeout); DialogProxy::showEvent(event); } +void ToolTipDialog::dismiss() +{ + m_showTimer->start(m_hideTimeout / 20); // pretty short: 200ms +} + +void ToolTipDialog::keepalive() +{ + m_showTimer->start(m_hideTimeout); +} + ToolTipDialog* ToolTipDialog::instance() { return toolTipDialogInstance(); diff --git a/src/declarativeimports/core/tooltipdialog.h b/src/declarativeimports/core/tooltipdialog.h index ab6207e35..3bd36d4c4 100644 --- a/src/declarativeimports/core/tooltipdialog.h +++ b/src/declarativeimports/core/tooltipdialog.h @@ -48,12 +48,16 @@ public: static ToolTipDialog* instance(); + void dismiss(); + void keepalive(); + protected: void showEvent(QShowEvent *event); private: QmlObject *m_qmlObject; QTimer *m_showTimer; + int m_hideTimeout; }; #endif