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.
This commit is contained in:
Sebastian Kügler 2014-01-09 04:09:03 +01:00
parent b8fc3aea7f
commit f21ad15ed6
4 changed files with 51 additions and 2 deletions

View File

@ -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();
}

View File

@ -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<QQuickItem> m_mainItem;
QTimer *m_showTimer;
QString m_mainText;

View File

@ -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();

View File

@ -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