* register widgets when data is set on them if they aren't already registered
* allocate ToolTip objects only as needed * invoke tool tip show/hide slots on the associated widget to allow for dynamic tooltip data svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=833526
This commit is contained in:
parent
24d494fd5a
commit
c1fb3655cd
33
tooltip.cpp
33
tooltip.cpp
@ -49,11 +49,12 @@ namespace Plasma {
|
||||
class ToolTipPrivate
|
||||
{
|
||||
public:
|
||||
ToolTipPrivate()
|
||||
ToolTipPrivate(QObject *s)
|
||||
: label(0)
|
||||
, imageLabel(0)
|
||||
, preview(0)
|
||||
, windowToPreview(0)
|
||||
, source(s)
|
||||
{ }
|
||||
|
||||
QLabel *label;
|
||||
@ -61,7 +62,7 @@ class ToolTipPrivate
|
||||
WindowPreview *preview;
|
||||
WId windowToPreview;
|
||||
PanelSvg *background;
|
||||
|
||||
QObject *source;
|
||||
};
|
||||
|
||||
void ToolTip::showEvent(QShowEvent *e)
|
||||
@ -70,6 +71,14 @@ void ToolTip::showEvent(QShowEvent *e)
|
||||
d->preview->setInfo();
|
||||
}
|
||||
|
||||
void ToolTip::hideEvent(QHideEvent *e)
|
||||
{
|
||||
QWidget::hideEvent(e);
|
||||
if (d->source) {
|
||||
QMetaObject::invokeMethod(d->source, SLOT(toolTipHidden()));
|
||||
}
|
||||
}
|
||||
|
||||
void ToolTip::mouseReleaseEvent(QMouseEvent* event)
|
||||
{
|
||||
if (rect().contains(event->pos())) {
|
||||
@ -77,10 +86,14 @@ void ToolTip::mouseReleaseEvent(QMouseEvent* event)
|
||||
}
|
||||
}
|
||||
|
||||
ToolTip::ToolTip()
|
||||
ToolTip::ToolTip(QObject *source)
|
||||
: QWidget(0)
|
||||
, d( new ToolTipPrivate )
|
||||
, d(new ToolTipPrivate(source))
|
||||
{
|
||||
if (source) {
|
||||
connect(source, SIGNAL(destroyed(QObject*)), this, SLOT(sourceDestroyed()));
|
||||
}
|
||||
|
||||
setWindowFlags(Qt::ToolTip);
|
||||
QGridLayout *l = new QGridLayout;
|
||||
d->preview = new WindowPreview;
|
||||
@ -97,7 +110,6 @@ ToolTip::ToolTip()
|
||||
l->addWidget(d->imageLabel, 1, 0);
|
||||
l->addWidget(d->label, 1, 1);
|
||||
setLayout(l);
|
||||
|
||||
}
|
||||
|
||||
ToolTip::~ToolTip()
|
||||
@ -120,14 +132,18 @@ void ToolTip::setContent(const ToolTipManager::ToolTipContent &data)
|
||||
|
||||
void ToolTip::prepareShowing()
|
||||
{
|
||||
if (d->source) {
|
||||
QMetaObject::invokeMethod(d->source, SLOT(toolTipAboutToShow()));
|
||||
}
|
||||
|
||||
if (d->windowToPreview != 0) {
|
||||
// show/hide the preview area
|
||||
d->preview->show();
|
||||
} else {
|
||||
d->preview->hide();
|
||||
}
|
||||
layout()->activate();
|
||||
|
||||
layout()->activate();
|
||||
resize(sizeHint());
|
||||
|
||||
if (isVisible()) {
|
||||
@ -156,6 +172,11 @@ void ToolTip::paintEvent(QPaintEvent *e)
|
||||
d->background->paintPanel(&painter, rect());
|
||||
}
|
||||
|
||||
void ToolTip::sourceDestroyed()
|
||||
{
|
||||
d->source = 0;
|
||||
}
|
||||
|
||||
void ToolTip::updateTheme()
|
||||
{
|
||||
d->background->setImagePath("widgets/tooltip");
|
||||
|
@ -38,7 +38,7 @@ class ToolTip : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
ToolTip();
|
||||
ToolTip(QObject *source);
|
||||
~ToolTip();
|
||||
void updateTheme();
|
||||
void setContent(const ToolTipManager::ToolTipContent &data);
|
||||
@ -46,11 +46,15 @@ public:
|
||||
|
||||
protected:
|
||||
void showEvent(QShowEvent *);
|
||||
void hideEvent(QHideEvent *);
|
||||
void mouseReleaseEvent(QMouseEvent *);
|
||||
|
||||
void resizeEvent(QResizeEvent *);
|
||||
void paintEvent(QPaintEvent *);
|
||||
|
||||
private Q_SLOTS:
|
||||
void sourceDestroyed();
|
||||
|
||||
private:
|
||||
ToolTipPrivate *const d;
|
||||
};
|
||||
|
@ -1,6 +1,7 @@
|
||||
/**************************************************************************
|
||||
/***************************************************************************
|
||||
* Copyright 2007 by Dan Meltzer <hydrogen@notyetimplemented.com> *
|
||||
* Copyright (C) 2008 by Alexis Ménard <darktears31@gmail.com> *
|
||||
* Copyright 2008 by Aaron Seigo <aseigo@kde.org> *
|
||||
* Copyright 2008 by Alexis Ménard <darktears31@gmail.com> *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
@ -90,6 +91,16 @@ ToolTipManager *ToolTipManager::self()
|
||||
return &privateInstance->self;
|
||||
}
|
||||
|
||||
ToolTipManager::ToolTipContent::ToolTipContent()
|
||||
: windowToPreview(0)
|
||||
{
|
||||
}
|
||||
|
||||
bool ToolTipManager::ToolTipContent::isEmpty() const
|
||||
{
|
||||
return mainText.isEmpty() && subText.isEmpty() && image.isNull() && windowToPreview == 0;
|
||||
}
|
||||
|
||||
ToolTipManager::ToolTipManager(QObject* parent)
|
||||
: QObject(parent),
|
||||
d(new ToolTipManagerPrivate)
|
||||
@ -137,8 +148,7 @@ bool ToolTipManager::isWidgetToolTipDisplayed(QGraphicsWidget *widget)
|
||||
ToolTip *tooltip = d->tooltips.value(widget);
|
||||
if (tooltip) {
|
||||
return tooltip->isVisible();
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -165,7 +175,7 @@ void ToolTipManager::registerWidget(QGraphicsWidget *widget)
|
||||
{
|
||||
if (!d->tooltips.contains(widget)) {
|
||||
//the tooltip is not registered we add it in our map of tooltips
|
||||
d->tooltips.insert(widget,new ToolTip());
|
||||
d->tooltips.insert(widget, 0);
|
||||
widget->installEventFilter(this);
|
||||
//connect to object destruction
|
||||
connect(widget, SIGNAL(destroyed(QObject *)),this,SLOT(onWidgetDestroyed(QObject *)));
|
||||
@ -184,13 +194,24 @@ void ToolTipManager::unregisterWidget(QGraphicsWidget *widget)
|
||||
}
|
||||
}
|
||||
|
||||
void ToolTipManager::setWidgetToolTipContent(QGraphicsWidget *widget,const ToolTipContent &data)
|
||||
void ToolTipManager::setToolTipContent(QGraphicsWidget *widget, const ToolTipContent &data)
|
||||
{
|
||||
if (!d->tooltips.contains(widget)) {
|
||||
return;
|
||||
registerWidget(widget);
|
||||
}
|
||||
|
||||
ToolTip *tooltip = d->tooltips.value(widget);
|
||||
|
||||
if (data.isEmpty()) {
|
||||
delete tooltip;
|
||||
d->tooltips.insert(widget, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!tooltip) {
|
||||
d->tooltips.insert(widget, new ToolTip(widget));
|
||||
}
|
||||
|
||||
tooltip->setContent(data);
|
||||
tooltip->updateTheme();
|
||||
}
|
||||
@ -236,11 +257,13 @@ void ToolTipManagerPrivate::onWidgetDestroyed(QObject *object)
|
||||
if (iterator.key() == w) {
|
||||
ToolTip *tooltip = iterator.value();
|
||||
tooltips.remove(iterator.key());
|
||||
if (tooltip) {
|
||||
tooltip->hide();
|
||||
delete tooltip;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ToolTipManagerPrivate::resetShownState()
|
||||
{
|
||||
@ -295,7 +318,10 @@ bool ToolTipManager::eventFilter(QObject *watched, QEvent *event)
|
||||
// created the widget can receive a hover event before it is fully
|
||||
// initialized, in which case view() will return 0.
|
||||
const Applet * applet = ToolTipManager::getItemItsApplet(widget);
|
||||
if (!applet) break;
|
||||
if (!applet) {
|
||||
break;
|
||||
}
|
||||
|
||||
QGraphicsView *parentView = applet->view();
|
||||
if (parentView) {
|
||||
showToolTip(widget);
|
||||
|
@ -1,5 +1,6 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2007 by Alexis Ménard <darktears31@gmail.com> *
|
||||
* Copyright 2008 by Alexis Ménard <darktears31@gmail.com> *
|
||||
* Copyright 2008 by Aaron Seigo <aseigo@kde.org> *
|
||||
* Copyright 2007 by Dan Meltzer <hydrogen@notyetimplemented.com> *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
@ -46,8 +47,19 @@ namespace Plasma
|
||||
/**
|
||||
* @short The class to manage tooltips on QGraphicsWidget in Plasma
|
||||
*
|
||||
* Manage tooltips on QGraphicsWidget. First you have to register your widget by calling registerWidget on the ToolTipManager singleton. After this you have to set the content of the tooltip by calling setWidgetToolTipContent using the struct ToolTipContent. The tooltip manager unregister automatically the widget when it is destroyed but if you want to do it manually call unregisterWidget. If you use plasma's applets show/hide of tooltip is automatically manage but if you use custom QGraphicsWidget, please call showToolTip on you hoverEnterEvent or hideToolTip on you hoverLeaveEvent.
|
||||
* Manage tooltips on QGraphicsWidget. First you have to register your widget by calling
|
||||
* registerWidget on the ToolTipManager singleton. After this you have to set the content of
|
||||
* the tooltip by calling setToolTipContent using the struct ToolTipContent. Calling
|
||||
* setToolTipContent on a widget that is not registered will cause it to be registered.
|
||||
*
|
||||
* The tooltip manager unregister automatically the widget when it is destroyed but if
|
||||
* you want to do it manually call unregisterWidget.
|
||||
*
|
||||
* When a tooltip for a widget is about to be shown, the widget's toolTipAboutToShow slot will be
|
||||
* invoked if it exists. Similarly, when a tooltip is hidden, the widget's toolTipHidden() slow
|
||||
* will be invoked if it exists. This allows widgets to provide on-demand tooltip data.
|
||||
*/
|
||||
|
||||
class PLASMA_EXPORT ToolTipManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
@ -58,7 +70,9 @@ namespace Plasma
|
||||
*/
|
||||
struct ToolTipContent
|
||||
{
|
||||
ToolTipContent() : windowToPreview( 0 ) {}
|
||||
ToolTipContent();
|
||||
bool isEmpty() const;
|
||||
|
||||
QString mainText; /**Important information, e.g. the title*/
|
||||
QString subText; /** Elaborates on the Main Text */
|
||||
QPixmap image; /** Icon to show */
|
||||
@ -120,11 +134,13 @@ namespace Plasma
|
||||
void unregisterWidget(QGraphicsWidget *widget);
|
||||
|
||||
/**
|
||||
* Function to set the content of a tooltip on a desired widget
|
||||
* Function to set the content of a tooltip on a desired widget.
|
||||
*
|
||||
* @param widget the desired widget
|
||||
* @param data the content of the tooltip
|
||||
* @param data the content of the tooltip. If an empty ToolTipContent is passed in,
|
||||
* the tooltip content will be reset.
|
||||
*/
|
||||
void setWidgetToolTipContent(QGraphicsWidget *widget,const ToolTipContent &data);
|
||||
void setToolTipContent(QGraphicsWidget *widget, const ToolTipContent &data);
|
||||
|
||||
/**
|
||||
* Function to know if widget has a tooltip registered in the tooltip manager
|
||||
|
Loading…
x
Reference in New Issue
Block a user