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>
|
|
|
|
#include <QDesktopWidget>
|
|
|
|
|
|
|
|
//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>
|
|
|
|
#include <panelsvg.h>
|
|
|
|
#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),
|
|
|
|
isShown(false),
|
|
|
|
delayedHide(false)
|
2008-07-16 17:17:44 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
~ToolTipManagerPrivate()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
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 the theme of plasma has change
|
|
|
|
*/
|
|
|
|
void themeUpdated();
|
|
|
|
/**
|
|
|
|
* called when a widget inside the tooltip manager is deleted
|
|
|
|
*/
|
|
|
|
void onWidgetDestroyed(QObject * object);
|
|
|
|
|
|
|
|
|
2008-07-16 17:17:44 +02:00
|
|
|
QGraphicsWidget *currentWidget;
|
|
|
|
QTimer *showTimer;
|
|
|
|
QTimer *hideTimer;
|
2008-09-18 08:21:33 +02:00
|
|
|
QHash<QGraphicsWidget *, ToolTip *> tooltips;
|
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;
|
|
|
|
};
|
|
|
|
K_GLOBAL_STATIC( ToolTipManagerSingleton, privateInstance )
|
|
|
|
|
|
|
|
ToolTipManager *ToolTipManager::self()
|
|
|
|
{
|
|
|
|
return &privateInstance->self;
|
|
|
|
}
|
|
|
|
|
2008-07-17 01:27:34 +02:00
|
|
|
ToolTipManager::ToolTipContent::ToolTipContent()
|
|
|
|
: windowToPreview(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ToolTipManager::ToolTipContent::isEmpty() const
|
|
|
|
{
|
|
|
|
return mainText.isEmpty() && subText.isEmpty() && image.isNull() && windowToPreview == 0;
|
|
|
|
}
|
|
|
|
|
2008-07-16 17:17:44 +02:00
|
|
|
ToolTipManager::ToolTipManager(QObject* parent)
|
|
|
|
: QObject(parent),
|
|
|
|
d(new ToolTipManagerPrivate)
|
|
|
|
{
|
|
|
|
connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(themeUpdated()));
|
2008-07-16 23:44:19 +02:00
|
|
|
d->themeUpdated();
|
2008-07-16 17:17:44 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTipManager::showToolTip(QGraphicsWidget *widget)
|
|
|
|
{
|
2008-09-18 23:55:09 +02:00
|
|
|
if (!d->tooltips.contains(widget)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d->currentWidget) {
|
|
|
|
hideToolTip(d->currentWidget);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-16 17:18:02 +02:00
|
|
|
bool ToolTipManager::isWidgetToolTipDisplayed(QGraphicsWidget *widget)
|
2008-07-16 17:17:44 +02:00
|
|
|
{
|
2008-09-18 23:55:09 +02:00
|
|
|
ToolTip *tooltip = d->tooltips.value(widget);
|
|
|
|
if (tooltip) {
|
|
|
|
return tooltip->isVisible();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2008-07-16 17:17:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTipManager::delayedHideToolTip()
|
|
|
|
{
|
|
|
|
d->showTimer->stop(); // stop the timer to show the tooltip
|
|
|
|
d->delayedHide = true;
|
|
|
|
d->hideTimer->start(250);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTipManager::hideToolTip(QGraphicsWidget *widget)
|
|
|
|
{
|
2008-07-17 01:27:34 +02:00
|
|
|
ToolTip *tooltip = d->tooltips.value(widget);
|
|
|
|
if (tooltip) {
|
|
|
|
d->showTimer->stop(); // stop the timer to show the tooltip
|
|
|
|
d->delayedHide = false;
|
|
|
|
d->currentWidget = 0;
|
|
|
|
tooltip->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-09-18 08:21:33 +02:00
|
|
|
if (d->tooltips.contains(widget)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-09-08 00:07:03 +02:00
|
|
|
//the tooltip is not registered we add it in our map of tooltips
|
|
|
|
d->tooltips.insert(widget, 0);
|
|
|
|
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-07-17 01:27:34 +02:00
|
|
|
ToolTip *tooltip = d->tooltips.take(widget);
|
2008-09-18 23:55:09 +02:00
|
|
|
if (tooltip) {
|
|
|
|
tooltip->deleteLater();
|
|
|
|
}
|
2008-07-16 17:17:44 +02:00
|
|
|
}
|
|
|
|
|
2008-07-17 01:27:34 +02:00
|
|
|
void ToolTipManager::setToolTipContent(QGraphicsWidget *widget, const ToolTipContent &data)
|
2008-07-16 17:17:44 +02:00
|
|
|
{
|
2008-09-18 08:21:33 +02:00
|
|
|
registerWidget(widget);
|
2008-07-17 01:27:34 +02:00
|
|
|
|
|
|
|
ToolTip *tooltip = d->tooltips.value(widget);
|
|
|
|
|
|
|
|
if (data.isEmpty()) {
|
2008-09-18 23:55:09 +02:00
|
|
|
if (tooltip) {
|
|
|
|
tooltip->deleteLater();
|
|
|
|
}
|
2008-07-17 01:27:34 +02:00
|
|
|
d->tooltips.insert(widget, 0);
|
2008-07-16 17:17:44 +02:00
|
|
|
return;
|
|
|
|
}
|
2008-07-16 23:48:52 +02:00
|
|
|
|
2008-07-17 01:27:34 +02:00
|
|
|
if (!tooltip) {
|
2008-07-17 07:54:30 +02:00
|
|
|
tooltip = new ToolTip(widget);
|
|
|
|
d->tooltips.insert(widget, tooltip);
|
2008-07-17 01:27:34 +02:00
|
|
|
}
|
|
|
|
|
2008-07-16 23:48:52 +02:00
|
|
|
tooltip->setContent(data);
|
|
|
|
tooltip->updateTheme();
|
2008-07-16 17:17:44 +02:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:26:50 +02:00
|
|
|
void ToolTipManager::clearToolTipContent(QGraphicsWidget *widget)
|
|
|
|
{
|
|
|
|
ToolTipContent t;
|
|
|
|
setToolTipContent(widget, t);
|
|
|
|
}
|
|
|
|
|
2008-07-16 23:58:36 +02:00
|
|
|
bool ToolTipManager::widgetHasToolTip(QGraphicsWidget *widget) const
|
2008-07-16 17:52:56 +02:00
|
|
|
{
|
|
|
|
return d->tooltips.contains(widget);
|
|
|
|
}
|
|
|
|
|
2008-08-28 22:23:39 +02:00
|
|
|
void ToolTipManager::setToolTipActivated(QGraphicsWidget *widget, bool enable)
|
|
|
|
{
|
2008-09-18 08:21:33 +02:00
|
|
|
registerWidget(widget);
|
2008-08-28 22:23:39 +02:00
|
|
|
|
|
|
|
ToolTip *tooltip = d->tooltips.value(widget);
|
|
|
|
tooltip->setActivated(enable);
|
2008-08-28 22:34:03 +02:00
|
|
|
if (!enable) {
|
|
|
|
hideToolTip(widget);
|
2008-09-01 12:16:43 +02:00
|
|
|
} else if (d->currentWidget) {
|
2008-08-28 22:34:03 +02:00
|
|
|
showToolTip(widget);
|
|
|
|
}
|
2008-08-28 22:23:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ToolTipManager::isToolTipActivated(QGraphicsWidget *widget)
|
|
|
|
{
|
|
|
|
if (!d->tooltips.contains(widget)) {
|
2008-09-18 08:21:33 +02:00
|
|
|
return false;
|
2008-08-28 22:23:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ToolTip *tooltip = d->tooltips.value(widget);
|
|
|
|
return tooltip->isActivated();
|
|
|
|
}
|
|
|
|
|
2008-07-16 23:44:19 +02:00
|
|
|
void ToolTipManagerPrivate::themeUpdated()
|
2008-07-16 17:17:44 +02:00
|
|
|
{
|
2008-09-18 08:21:33 +02:00
|
|
|
QHashIterator<QGraphicsWidget*, ToolTip *> iterator(tooltips);
|
2008-07-16 23:45:52 +02:00
|
|
|
while (iterator.hasNext()) {
|
|
|
|
iterator.next();
|
2008-07-25 22:18:54 +02:00
|
|
|
if (iterator.value()) {
|
|
|
|
iterator.value()->updateTheme();
|
|
|
|
}
|
2008-07-16 23:45:52 +02:00
|
|
|
}
|
2008-07-16 17:17:44 +02:00
|
|
|
}
|
|
|
|
|
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-09-18 08:21:33 +02:00
|
|
|
QMutableHashIterator<QGraphicsWidget*, ToolTip *> iterator(tooltips);
|
2008-07-16 23:44:19 +02:00
|
|
|
while (iterator.hasNext()) {
|
|
|
|
iterator.next();
|
2008-09-18 08:21:33 +02:00
|
|
|
//kDebug() << (int)iterator.key() << (int)w;
|
2008-07-16 23:44:19 +02:00
|
|
|
if (iterator.key() == w) {
|
2008-07-17 01:27:34 +02:00
|
|
|
ToolTip *tooltip = iterator.value();
|
2008-09-18 08:21:33 +02:00
|
|
|
iterator.remove();
|
2008-07-17 01:27:34 +02:00
|
|
|
if (tooltip) {
|
2008-09-18 08:21:33 +02:00
|
|
|
//kDebug() << "deleting the tooltip!";
|
2008-07-17 01:27:34 +02:00
|
|
|
tooltip->hide();
|
2008-09-18 23:55:09 +02:00
|
|
|
tooltip->deleteLater();
|
2008-07-17 01:27:34 +02:00
|
|
|
}
|
2008-09-18 08:21:33 +02:00
|
|
|
return;
|
2008-07-16 17:18:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-16 17:17:44 +02:00
|
|
|
void ToolTipManagerPrivate::resetShownState()
|
|
|
|
{
|
|
|
|
if (currentWidget) {
|
2008-07-16 23:44:19 +02:00
|
|
|
ToolTip * tooltip = tooltips.value(currentWidget);
|
|
|
|
if (tooltip && (!tooltip->isVisible() || delayedHide)) {
|
|
|
|
//One might have moused out and back in again
|
|
|
|
delayedHide = false;
|
|
|
|
isShown = false;
|
|
|
|
tooltip->hide();
|
|
|
|
currentWidget = 0;
|
2008-07-16 17:17:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTipManagerPrivate::showToolTip()
|
|
|
|
{
|
|
|
|
if (!currentWidget) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-07-17 01:27:34 +02:00
|
|
|
ToolTip *tooltip = tooltips.value(currentWidget);
|
2008-09-18 23:55:09 +02:00
|
|
|
bool justCreated = false;
|
|
|
|
|
|
|
|
if (!tooltip) {
|
|
|
|
// give the object a chance for delayed loading of the tip
|
|
|
|
QMetaObject::invokeMethod(currentWidget, "toolTipAboutToShow");
|
|
|
|
tooltip = tooltips.value(currentWidget);
|
|
|
|
if (tooltip) {
|
|
|
|
justCreated = true;
|
|
|
|
} else {
|
|
|
|
currentWidget = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tooltip->isActivated()) {
|
2008-08-28 19:02:42 +02:00
|
|
|
tooltip->setVisible(false);
|
2008-09-18 23:55:09 +02:00
|
|
|
//kDebug() << "about to show" << justCreated;
|
|
|
|
tooltip->prepareShowing(!justCreated);
|
2008-07-17 01:48:41 +02:00
|
|
|
tooltip->move(popupPosition(currentWidget, tooltip->size()));
|
2008-07-16 17:17:44 +02:00
|
|
|
isShown = true; //ToolTip is visible
|
2008-08-28 19:02:42 +02:00
|
|
|
tooltip->setVisible(true);
|
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-07-25 17:20:12 +02:00
|
|
|
if (!widget) {
|
|
|
|
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
|
|
|
|
if (Plasma::ToolTipManager::self()->isWidgetToolTipDisplayed(widget)) {
|
|
|
|
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
|
|
|
|
if (!widgetHasToolTip(widget)) {
|
|
|
|
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) {
|
|
|
|
showToolTip(widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case QEvent::GraphicsSceneHoverLeave:
|
|
|
|
delayedHideToolTip();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::GraphicsSceneMousePress:
|
|
|
|
case QEvent::GraphicsSceneWheel:
|
|
|
|
hideToolTip(widget);
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QObject::eventFilter(watched,event);
|
|
|
|
}
|
|
|
|
|
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"
|
|
|
|
|