2008-09-08 19:32:25 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2007 by Dan Meltzer <hydrogen@notyetimplemented.com>
|
|
|
|
* Copyright 2008 by Aaron Seigo <aseigo@kde.org>
|
|
|
|
* Copyright 2008 by Alexis Ménard <darktears31@gmail.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2008-07-17 09:53:31 +02:00
|
|
|
|
2008-07-16 17:17:44 +02:00
|
|
|
#include "tooltipmanager.h"
|
|
|
|
|
|
|
|
//Qt
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QGridLayout>
|
|
|
|
#include <QGraphicsView>
|
|
|
|
|
|
|
|
//KDE
|
|
|
|
#include <KWindowSystem>
|
|
|
|
|
2008-07-17 09:53:31 +02:00
|
|
|
//X11
|
|
|
|
#ifdef Q_WS_X11
|
|
|
|
#include <QtGui/QX11Info>
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <fixx11h.h>
|
|
|
|
#endif
|
|
|
|
|
2008-07-16 17:17:44 +02:00
|
|
|
//Plasma
|
2008-07-30 22:35:42 +02:00
|
|
|
#include <applet.h>
|
|
|
|
#include <containment.h>
|
2008-10-29 16:22:38 +01:00
|
|
|
#include <corona.h>
|
2008-10-29 18:46:33 +01:00
|
|
|
#include <framesvg.h>
|
2008-07-30 22:35:42 +02:00
|
|
|
#include <theme.h>
|
|
|
|
#include <view.h>
|
|
|
|
#include <private/tooltip_p.h>
|
2008-07-17 09:53:31 +02:00
|
|
|
|
2008-07-16 17:17:44 +02:00
|
|
|
namespace Plasma
|
|
|
|
{
|
2008-07-17 09:53:31 +02:00
|
|
|
|
2008-07-16 17:17:44 +02:00
|
|
|
class ToolTipManagerPrivate
|
|
|
|
{
|
|
|
|
public :
|
|
|
|
ToolTipManagerPrivate()
|
2008-09-18 23:55:09 +02:00
|
|
|
: currentWidget(0),
|
|
|
|
showTimer(0),
|
|
|
|
hideTimer(0),
|
2008-11-02 07:55:10 +01:00
|
|
|
tipWidget(new ToolTip(0)),
|
2008-10-21 03:58:01 +02:00
|
|
|
state(ToolTipManager::Activated),
|
2008-09-18 23:55:09 +02:00
|
|
|
isShown(false),
|
|
|
|
delayedHide(false)
|
2008-07-16 17:17:44 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2008-10-21 03:58:01 +02:00
|
|
|
|
2008-07-16 17:17:44 +02:00
|
|
|
~ToolTipManagerPrivate()
|
|
|
|
{
|
2008-10-21 03:58:01 +02:00
|
|
|
clearTips();
|
2008-11-02 07:55:10 +01:00
|
|
|
delete tipWidget;
|
2008-07-16 17:17:44 +02:00
|
|
|
}
|
2008-07-16 23:44:19 +02:00
|
|
|
|
2008-07-16 17:17:44 +02:00
|
|
|
void showToolTip();
|
|
|
|
void resetShownState();
|
|
|
|
|
2008-07-16 23:44:19 +02:00
|
|
|
/**
|
|
|
|
* called when a widget inside the tooltip manager is deleted
|
|
|
|
*/
|
|
|
|
void onWidgetDestroyed(QObject * object);
|
|
|
|
|
2008-10-21 03:58:01 +02:00
|
|
|
void clearTips();
|
|
|
|
|
2008-10-24 00:14:07 +02:00
|
|
|
void doDelayedHide();
|
|
|
|
|
2008-07-16 17:17:44 +02:00
|
|
|
QGraphicsWidget *currentWidget;
|
|
|
|
QTimer *showTimer;
|
|
|
|
QTimer *hideTimer;
|
2008-11-02 07:55:10 +01:00
|
|
|
QHash<QGraphicsWidget *, ToolTipContent> tooltips;
|
|
|
|
ToolTip *tipWidget;
|
2008-10-21 03:58:01 +02:00
|
|
|
ToolTipManager::State state;
|
2008-09-18 23:55:09 +02:00
|
|
|
bool isShown : 1;
|
|
|
|
bool delayedHide : 1;
|
2008-07-16 17:17:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
//TOOLTIP IMPLEMENTATION
|
|
|
|
class ToolTipManagerSingleton
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ToolTipManagerSingleton()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
ToolTipManager self;
|
|
|
|
};
|
2008-10-12 00:48:38 +02:00
|
|
|
K_GLOBAL_STATIC(ToolTipManagerSingleton, privateInstance)
|
2008-07-16 17:17:44 +02:00
|
|
|
|
|
|
|
ToolTipManager *ToolTipManager::self()
|
|
|
|
{
|
|
|
|
return &privateInstance->self;
|
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
ToolTipManager::ToolTipManager(QObject *parent)
|
2008-07-16 17:17:44 +02:00
|
|
|
: QObject(parent),
|
|
|
|
d(new ToolTipManagerPrivate)
|
|
|
|
{
|
|
|
|
d->showTimer = new QTimer(this);
|
|
|
|
d->showTimer->setSingleShot(true);
|
|
|
|
d->hideTimer = new QTimer(this);
|
|
|
|
d->hideTimer->setSingleShot(true);
|
|
|
|
|
|
|
|
connect(d->showTimer, SIGNAL(timeout()), SLOT(showToolTip()));
|
|
|
|
connect(d->hideTimer, SIGNAL(timeout()), SLOT(resetShownState()));
|
|
|
|
}
|
|
|
|
|
|
|
|
ToolTipManager::~ToolTipManager()
|
|
|
|
{
|
2008-07-22 22:21:41 +02:00
|
|
|
delete d;
|
2008-07-16 17:17:44 +02:00
|
|
|
}
|
|
|
|
|
2008-10-21 03:33:32 +02:00
|
|
|
void ToolTipManager::show(QGraphicsWidget *widget)
|
2008-07-16 17:17:44 +02:00
|
|
|
{
|
2008-09-18 23:55:09 +02:00
|
|
|
if (!d->tooltips.contains(widget)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->hideTimer->stop();
|
|
|
|
d->delayedHide = false;
|
|
|
|
d->showTimer->stop();
|
|
|
|
d->currentWidget = widget;
|
|
|
|
|
|
|
|
if (d->isShown) {
|
|
|
|
// small delay to prevent unnecessary showing when the mouse is moving quickly across items
|
|
|
|
// which can be too much for less powerful CPUs to keep up with
|
|
|
|
d->showTimer->start(200);
|
|
|
|
} else {
|
2008-10-07 13:06:15 +02:00
|
|
|
d->showTimer->start(700);
|
2008-09-18 23:55:09 +02:00
|
|
|
}
|
2008-07-16 17:17:44 +02:00
|
|
|
}
|
|
|
|
|
2008-10-21 03:33:32 +02:00
|
|
|
bool ToolTipManager::isVisible(QGraphicsWidget *widget) const
|
2008-07-16 17:17:44 +02:00
|
|
|
{
|
2008-11-02 07:55:10 +01:00
|
|
|
return d->currentWidget == widget && d->tipWidget->isVisible();
|
2008-07-16 17:17:44 +02:00
|
|
|
}
|
|
|
|
|
2008-10-24 00:14:07 +02:00
|
|
|
void ToolTipManagerPrivate::doDelayedHide()
|
2008-07-16 17:17:44 +02:00
|
|
|
{
|
2008-10-24 00:14:07 +02:00
|
|
|
showTimer->stop(); // stop the timer to show the tooltip
|
|
|
|
delayedHide = true;
|
|
|
|
hideTimer->start(250);
|
2008-07-16 17:17:44 +02:00
|
|
|
}
|
|
|
|
|
2008-10-21 03:33:32 +02:00
|
|
|
void ToolTipManager::hide(QGraphicsWidget *widget)
|
2008-07-16 17:17:44 +02:00
|
|
|
{
|
2008-11-02 07:55:10 +01:00
|
|
|
if (d->currentWidget != widget) {
|
|
|
|
return;
|
2008-07-17 01:27:34 +02:00
|
|
|
}
|
2008-11-02 07:55:10 +01:00
|
|
|
|
|
|
|
d->showTimer->stop(); // stop the timer to show the tooltip
|
|
|
|
d->delayedHide = false;
|
|
|
|
d->currentWidget = 0;
|
|
|
|
d->tipWidget->hide();
|
2008-07-16 17:17:44 +02:00
|
|
|
}
|
|
|
|
|
2008-07-16 17:18:02 +02:00
|
|
|
void ToolTipManager::registerWidget(QGraphicsWidget *widget)
|
2008-07-16 17:17:44 +02:00
|
|
|
{
|
2008-10-21 03:58:01 +02:00
|
|
|
if (d->state == Deactivated || d->tooltips.contains(widget)) {
|
2008-09-18 08:21:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-09-08 00:07:03 +02:00
|
|
|
//the tooltip is not registered we add it in our map of tooltips
|
2008-11-02 07:55:10 +01:00
|
|
|
d->tooltips.insert(widget, ToolTipContent());
|
2008-09-08 00:07:03 +02:00
|
|
|
widget->installEventFilter(this);
|
|
|
|
//connect to object destruction
|
2008-09-18 08:21:33 +02:00
|
|
|
connect(widget, SIGNAL(destroyed(QObject*)), this, SLOT(onWidgetDestroyed(QObject*)));
|
2008-07-16 17:17:44 +02:00
|
|
|
}
|
|
|
|
|
2008-07-16 17:18:02 +02:00
|
|
|
void ToolTipManager::unregisterWidget(QGraphicsWidget *widget)
|
2008-07-16 17:17:44 +02:00
|
|
|
{
|
2008-07-16 23:48:52 +02:00
|
|
|
if (!d->tooltips.contains(widget)) {
|
|
|
|
return;
|
|
|
|
}
|
2008-09-18 08:21:33 +02:00
|
|
|
|
2008-07-17 01:11:51 +02:00
|
|
|
widget->removeEventFilter(this);
|
2008-11-02 07:55:10 +01:00
|
|
|
d->tooltips.remove(widget);
|
2008-07-16 17:17:44 +02:00
|
|
|
}
|
|
|
|
|
2008-11-02 06:58:42 +01:00
|
|
|
void ToolTipManager::setContent(QGraphicsWidget *widget, const ToolTipContent &data)
|
2008-07-16 17:17:44 +02:00
|
|
|
{
|
2008-10-21 03:58:01 +02:00
|
|
|
if (d->state == Deactivated) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-09-18 08:21:33 +02:00
|
|
|
registerWidget(widget);
|
2008-11-02 07:55:10 +01:00
|
|
|
d->tooltips[widget] = data;
|
2008-07-17 01:27:34 +02:00
|
|
|
|
2008-11-02 07:55:10 +01:00
|
|
|
if (d->currentWidget == widget) {
|
|
|
|
if (data.isEmpty()) {
|
|
|
|
hide(widget);
|
2008-09-18 23:55:09 +02:00
|
|
|
}
|
2008-07-16 23:48:52 +02:00
|
|
|
|
2008-11-02 07:55:10 +01:00
|
|
|
d->tipWidget->setContent(data);
|
2008-07-17 01:27:34 +02:00
|
|
|
}
|
2008-07-16 17:17:44 +02:00
|
|
|
}
|
|
|
|
|
2008-10-24 00:14:07 +02:00
|
|
|
void ToolTipManager::clearContent(QGraphicsWidget *widget)
|
|
|
|
{
|
2008-11-02 06:58:42 +01:00
|
|
|
setContent(widget, ToolTipContent());
|
2008-10-24 00:14:07 +02:00
|
|
|
}
|
|
|
|
|
2008-10-21 03:58:01 +02:00
|
|
|
void ToolTipManager::setState(ToolTipManager::State state)
|
|
|
|
{
|
|
|
|
d->state = state;
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case Activated:
|
|
|
|
break;
|
|
|
|
case Deactivated:
|
|
|
|
d->clearTips();
|
|
|
|
//fallthrough
|
|
|
|
case Inhibited:
|
|
|
|
d->resetShownState();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ToolTipManager::State ToolTipManager::state() const
|
|
|
|
{
|
|
|
|
return d->state;
|
|
|
|
}
|
|
|
|
|
2008-07-16 23:44:19 +02:00
|
|
|
void ToolTipManagerPrivate::onWidgetDestroyed(QObject *object)
|
2008-07-16 17:18:02 +02:00
|
|
|
{
|
2008-07-16 23:44:19 +02:00
|
|
|
if (!object) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-07-16 23:58:36 +02:00
|
|
|
// we do a static_cast here since it really isn't a QGraphicsWidget by this
|
|
|
|
// point anymore since we are in the QObject dtor. we don't actually
|
|
|
|
// try and do anything with it, we just need the value of the pointer
|
|
|
|
// so this unsafe looking code is actually just fine.
|
|
|
|
//
|
|
|
|
// NOTE: DO NOT USE THE w VARIABLE FOR ANYTHING OTHER THAN COMPARING
|
|
|
|
// THE ADDRESS! ACTUALLY USING THE OBJECT WILL RESULT IN A CRASH!!!
|
2008-07-16 23:44:19 +02:00
|
|
|
QGraphicsWidget *w = static_cast<QGraphicsWidget*>(object);
|
|
|
|
|
|
|
|
if (currentWidget == w) {
|
|
|
|
currentWidget = 0;
|
|
|
|
showTimer->stop(); // stop the timer to show the tooltip
|
|
|
|
delayedHide = false;
|
|
|
|
}
|
|
|
|
|
2008-11-02 07:55:10 +01:00
|
|
|
tooltips.remove(w);
|
2008-07-16 17:18:02 +02:00
|
|
|
}
|
|
|
|
|
2008-10-21 03:58:01 +02:00
|
|
|
void ToolTipManagerPrivate::clearTips()
|
|
|
|
{
|
|
|
|
tooltips.clear();
|
|
|
|
}
|
|
|
|
|
2008-07-16 17:17:44 +02:00
|
|
|
void ToolTipManagerPrivate::resetShownState()
|
|
|
|
{
|
|
|
|
if (currentWidget) {
|
2008-11-02 07:55:10 +01:00
|
|
|
if (!tipWidget->isVisible() || delayedHide) {
|
2008-07-16 23:44:19 +02:00
|
|
|
//One might have moused out and back in again
|
|
|
|
delayedHide = false;
|
|
|
|
isShown = false;
|
2008-11-02 07:55:10 +01:00
|
|
|
tipWidget->hide();
|
2008-07-16 23:44:19 +02:00
|
|
|
currentWidget = 0;
|
2008-10-21 03:58:01 +02:00
|
|
|
}
|
2008-07-16 17:17:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTipManagerPrivate::showToolTip()
|
|
|
|
{
|
2008-10-23 00:15:49 +02:00
|
|
|
if (state != ToolTipManager::Activated ||
|
|
|
|
!currentWidget ||
|
|
|
|
QApplication::activePopupWidget() ||
|
|
|
|
QApplication::activeModalWidget()) {
|
2008-07-16 17:17:44 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-02 07:55:10 +01:00
|
|
|
ToolTipContent tooltip = tooltips.value(currentWidget);
|
2008-09-18 23:55:09 +02:00
|
|
|
bool justCreated = false;
|
|
|
|
|
2008-11-02 07:55:10 +01:00
|
|
|
if (tooltip.isEmpty()) {
|
2008-09-18 23:55:09 +02:00
|
|
|
// give the object a chance for delayed loading of the tip
|
|
|
|
QMetaObject::invokeMethod(currentWidget, "toolTipAboutToShow");
|
|
|
|
tooltip = tooltips.value(currentWidget);
|
2008-11-02 07:55:10 +01:00
|
|
|
//kDebug() << "attempt to make one ... we gots" << tooltip.isEmpty();
|
|
|
|
|
|
|
|
if (tooltip.isEmpty()) {
|
2008-09-18 23:55:09 +02:00
|
|
|
currentWidget = 0;
|
|
|
|
return;
|
|
|
|
}
|
2008-11-02 07:55:10 +01:00
|
|
|
|
|
|
|
justCreated = true;
|
2008-09-18 23:55:09 +02:00
|
|
|
}
|
|
|
|
|
2008-10-21 03:33:32 +02:00
|
|
|
//kDebug() << "about to show" << justCreated;
|
2008-11-02 07:55:10 +01:00
|
|
|
tipWidget->setContent(tooltip);
|
|
|
|
tipWidget->prepareShowing(!justCreated);
|
2008-11-02 08:25:56 +01:00
|
|
|
tipWidget->moveTo(ToolTipManager::self()->m_corona->popupPosition(currentWidget, tipWidget->size()));
|
2008-11-02 07:55:10 +01:00
|
|
|
tipWidget->show();
|
2008-10-21 03:33:32 +02:00
|
|
|
isShown = true; //ToolTip is visible
|
2008-10-26 17:55:54 +01:00
|
|
|
|
2008-11-02 07:55:10 +01:00
|
|
|
delayedHide = tooltip.autohide();
|
2008-10-26 18:39:46 +01:00
|
|
|
if (delayedHide) {
|
2008-10-26 17:55:54 +01:00
|
|
|
//kDebug() << "starting authoide";
|
|
|
|
hideTimer->start(3000);
|
|
|
|
}
|
2008-07-16 17:17:44 +02:00
|
|
|
}
|
|
|
|
|
2008-07-17 01:11:51 +02:00
|
|
|
bool ToolTipManager::eventFilter(QObject *watched, QEvent *event)
|
|
|
|
{
|
|
|
|
QGraphicsWidget * widget = dynamic_cast<QGraphicsWidget *>(watched);
|
2008-10-21 03:58:01 +02:00
|
|
|
if (d->state != Activated || !widget) {
|
2008-07-25 17:20:12 +02:00
|
|
|
return QObject::eventFilter(watched, event);
|
2008-07-17 01:11:51 +02:00
|
|
|
}
|
2008-07-17 01:48:41 +02:00
|
|
|
|
2008-07-17 01:11:51 +02:00
|
|
|
switch (event->type()) {
|
|
|
|
case QEvent::GraphicsSceneHoverMove:
|
|
|
|
// If the tooltip isn't visible, run through showing the tooltip again
|
|
|
|
// so that it only becomes visible after a stationary hover
|
2008-10-21 03:33:32 +02:00
|
|
|
if (Plasma::ToolTipManager::self()->isVisible(widget)) {
|
2008-07-17 01:11:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-07-25 17:20:12 +02:00
|
|
|
// Don't restart the show timer on a mouse move event if there hasn't
|
|
|
|
// been an enter event or the current widget has been cleared by a click
|
|
|
|
// or wheel event.
|
|
|
|
if (!d->currentWidget) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-07-17 01:11:51 +02:00
|
|
|
case QEvent::GraphicsSceneHoverEnter:
|
|
|
|
{
|
|
|
|
// Check that there is a tooltip to show
|
2008-10-21 03:01:12 +02:00
|
|
|
if (!d->tooltips.contains(widget)) {
|
2008-07-17 01:11:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the mouse is in the widget's area at the time that it is being
|
|
|
|
// created the widget can receive a hover event before it is fully
|
|
|
|
// initialized, in which case view() will return 0.
|
2008-07-17 01:48:41 +02:00
|
|
|
QGraphicsView *parentView = viewFor(widget);
|
2008-07-17 01:11:51 +02:00
|
|
|
if (parentView) {
|
2008-10-21 03:33:32 +02:00
|
|
|
show(widget);
|
2008-07-17 01:11:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case QEvent::GraphicsSceneHoverLeave:
|
2008-10-24 00:14:07 +02:00
|
|
|
d->doDelayedHide();
|
2008-07-17 01:11:51 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::GraphicsSceneMousePress:
|
|
|
|
case QEvent::GraphicsSceneWheel:
|
2008-10-21 03:33:32 +02:00
|
|
|
hide(widget);
|
2008-07-17 01:11:51 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-10-12 00:48:38 +02:00
|
|
|
return QObject::eventFilter(watched, event);
|
2008-07-17 01:11:51 +02:00
|
|
|
}
|
|
|
|
|
2008-07-17 01:48:41 +02:00
|
|
|
} // Plasma namespace
|
2008-07-16 17:17:44 +02:00
|
|
|
|
2008-07-16 23:44:19 +02:00
|
|
|
#include "tooltipmanager.moc"
|
|
|
|
|