Add a generic tooltip widget to Plasma::Widget and implement it in the tasks applet.

Applets wishing to use this tooltip should Create a Plasma::ToolTipData struct 
(defined in widget.h) and pass it to Widget::setToolTip().  Showing and hiding
the tooltip is all handled automatically.

While the basic idea is found in this patch, the look still definatly needs some
work.  Still a definate work in progress.

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=751113
This commit is contained in:
Dan Meltzer 2007-12-21 05:33:17 +00:00
parent 420c0a1996
commit 357656f660
7 changed files with 263 additions and 0 deletions

View File

@ -65,6 +65,7 @@ set(plasma_LIB_SRCS
widgets/widget.cpp widgets/widget.cpp
widgets/signalplotter.cpp widgets/signalplotter.cpp
widgets/meter.cpp widgets/meter.cpp
widgets/tooltip.cpp
) )
kde4_add_ui_files ( kde4_add_ui_files (

View File

@ -214,6 +214,7 @@ void CheckBox::hoverLeaveEvent ( QGraphicsSceneHoverEvent * event )
event->accept(); event->accept();
d->hasMouse= false; d->hasMouse= false;
update(); update();
Widget::hoverEnterEvent(event);
} }
void CheckBox::hoverEnterEvent ( QGraphicsSceneHoverEvent * event) void CheckBox::hoverEnterEvent ( QGraphicsSceneHoverEvent * event)
@ -221,6 +222,7 @@ void CheckBox::hoverEnterEvent ( QGraphicsSceneHoverEvent * event)
event->accept(); event->accept();
d->hasMouse = true; d->hasMouse = true;
update(); update();
Widget::hoverLeaveEvent(event);
} }
/* /*

View File

@ -201,6 +201,7 @@ void RadioButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
event->accept(); event->accept();
d->mouseOver = false; d->mouseOver = false;
update(); update();
Widget::hoverLeaveEvent(event);
} }
void RadioButton::hoverEnterEvent(QGraphicsSceneHoverEvent *event) void RadioButton::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
@ -208,6 +209,7 @@ void RadioButton::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
event->accept(); event->accept();
d->mouseOver = true; d->mouseOver = true;
update(); update();
Widget::hoverEnterEvent(event);
} }

133
widgets/tooltip.cpp Normal file
View File

@ -0,0 +1,133 @@
/*
* Copyright 2007 by Dan Meltzer <hydrogen@notyetimplemented.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* This program 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 General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "tooltip_p.h"
#include <QHBoxLayout>
#include <QLabel>
#include <QPixmap>
#include <QTimer>
#include <kglobal.h>
namespace Plasma {
class ToolTip::Private
{
public:
Private()
: label(0)
, imageLabel(0)
, isShown(false)
, showTimer(0)
, hideTimer(0)
{ }
QLabel *label;
QLabel *imageLabel;
bool isShown;
QTimer *showTimer;
QTimer *hideTimer;
};
class ToolTipSingleton
{
public:
ToolTip self;
};
K_GLOBAL_STATIC( ToolTipSingleton, privateInstance )
ToolTip *ToolTip::instance()
{
return &privateInstance->self;
}
void ToolTip::show(const QPoint &location, const Plasma::ToolTipData &data)
{
setData(data);
move(location.x(), location.y() - sizeHint().height());
if (d->isShown) {
setVisible(true); // Don't delay if the tooltip is already shown(i.e. moving from one task to another)
}
else {
d->showTimer->start(1000); //Shown after a one second delay.
}
}
void ToolTip::hide()
{
d->showTimer->stop(); //Mouse out, stop the timer to show the tooltip
if (!isVisible()) {
d->isShown = false;
}
setVisible(false);
d->hideTimer->start(500); //500 ms delay before we are officially "gone" to allow for the time to move between widgets
}
//PRIVATE FUNCTIONS
void ToolTip::slotShowToolTip()
{
d->isShown = true; //ToolTip is visible
setVisible(true);
}
void ToolTip::slotResetTimer()
{
if (!isVisible()) { //One might have moused out and back in again
d->isShown = false;
}
}
ToolTip::ToolTip()
: QWidget(0)
, d( new Private )
{
setWindowFlags(Qt::ToolTip);
QHBoxLayout *l = new QHBoxLayout;
d->label = new QLabel;
d->label->setWordWrap(true);
d->imageLabel = new QLabel;
d->imageLabel->setAlignment(Qt::AlignTop | Qt::AlignLeft);
l->addWidget(d->imageLabel);
l->addWidget(d->label);
setLayout(l);
d->showTimer = new QTimer(this);
d->showTimer->setSingleShot(true);
d->hideTimer = new QTimer(this);
d->hideTimer->setSingleShot(true);
connect(d->showTimer, SIGNAL(timeout()), SLOT(slotShowToolTip()));
connect(d->hideTimer, SIGNAL(timeout()), SLOT(slotResetTimer()));
}
void ToolTip::setData(const Plasma::ToolTipData &data)
{
d->label->setText("<qt><h3>" + data.mainText + "</h3><p>" +
data.subText + "</p></qt>");
d->imageLabel->setPixmap(data.image);
}
ToolTip::~ToolTip()
{
delete d;
}
}
#include "tooltip_p.moc"

64
widgets/tooltip_p.h Normal file
View File

@ -0,0 +1,64 @@
/*
* Copyright 2007 by Dan Meltzer <hydrogen@notyetimplemented.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* This program 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 General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef PLASMATOOLTIP_H
#define PLASMATOOLTIP_H
#include <plasma/widgets/widget.h> //ToolTipData struct
#include <QWidget> // base class
#include <QPixmap> // stack allocated
#include <QPoint> // stack allocated
#include <QString> // stack allocated
namespace Plasma {
class ToolTipData;
/**
@author Dan Meltzer
* A Singleton tooltip. Before calling show it is necessary to
* call setLocation and setData
*/
class ToolTip : public QWidget
{
Q_OBJECT
public:
ToolTip();
~ToolTip();
static ToolTip *instance();
void show(const QPoint &location, const Plasma::ToolTipData &data);
void hide();
private Q_SLOTS:
void slotResetTimer();
void slotShowToolTip();
private:
void setData(const Plasma::ToolTipData &data);
class Private;
Private *const d;
};
}
#endif

View File

@ -26,7 +26,9 @@
#include <QApplication> #include <QApplication>
#include <QGraphicsScene> #include <QGraphicsScene>
#include <QGraphicsSceneHoverEvent>
#include <QGraphicsView> #include <QGraphicsView>
#include <QHelpEvent>
#include <QList> #include <QList>
#include <QPainter> #include <QPainter>
#include <QPixmapCache> #include <QPixmapCache>
@ -36,6 +38,7 @@
#include "layouts/freelayout.h" #include "layouts/freelayout.h"
#include "plasma/plasma.h" #include "plasma/plasma.h"
#include "tooltip_p.h"
namespace Plasma namespace Plasma
{ {
@ -71,6 +74,7 @@ class Widget::Private
bool wasMovable; bool wasMovable;
bool shouldPaint(QPainter *painter, const QTransform &transform); bool shouldPaint(QPainter *painter, const QTransform &transform);
ToolTipData toolTip;
}; };
QGraphicsItem* Widget::graphicsItem() QGraphicsItem* Widget::graphicsItem()
@ -495,6 +499,21 @@ void Widget::paintWidget(QPainter *painter, const QStyleOptionGraphicsItem *opti
// Replaced by widget's own function // Replaced by widget's own function
} }
ToolTipData Widget::toolTip() const
{
return d->toolTip;
}
void Widget::setToolTip(const ToolTipData &tip)
{
d->toolTip = tip;
if (ToolTip::instance()->isVisible()) {
QPoint viewPos = view()->mapFromScene(scenePos());
QPoint globalPos = view()->mapToGlobal(viewPos);
ToolTip::instance()->show(globalPos, d->toolTip);
}
}
QVariant Widget::itemChange(GraphicsItemChange change, const QVariant &value) QVariant Widget::itemChange(GraphicsItemChange change, const QVariant &value)
{ {
if (change == QGraphicsItem::ItemChildRemovedChange) { if (change == QGraphicsItem::ItemChildRemovedChange) {
@ -530,5 +549,23 @@ void Widget::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
} }
} }
void Widget::hoverEnterEvent(QGraphicsSceneHoverEvent *e)
{
if (d->toolTip.image.isNull() &&
d->toolTip.subText.isEmpty() &&
d->toolTip.mainText.isEmpty()) {
return; //Nothing to show
}
QPoint viewPos = view()->mapFromScene(scenePos());
QPoint globalPos = view()->mapToGlobal(viewPos);
ToolTip::instance()->show(globalPos, d->toolTip);
}
void Widget::hoverLeaveEvent(QGraphicsSceneHoverEvent *e)
{
ToolTip::instance()->hide();
}
} // Plasma namespace } // Plasma namespace

View File

@ -23,20 +23,31 @@
#define WIDGET_H_ #define WIDGET_H_
#include <QtGui/QGraphicsItem> #include <QtGui/QGraphicsItem>
#include <QPixmap>
#include <QtCore/QRectF> #include <QtCore/QRectF>
#include <QtCore/QSizeF> #include <QtCore/QSizeF>
#include <QtCore/QString>
#include <plasma/layouts/layoutitem.h> #include <plasma/layouts/layoutitem.h>
#include <plasma/plasma_export.h> #include <plasma/plasma_export.h>
class QGraphicsView; class QGraphicsView;
class QGraphicsSceneHoverEvent;
namespace Plasma namespace Plasma
{ {
struct PLASMA_EXPORT ToolTipData
{
QString mainText; //Important information
QString subText; //Elaborates on the Main Text
QPixmap image; // Icon to show;
};
class Layout; class Layout;
/** /**
* Base class for all Widgets in Plasma. * Base class for all Widgets in Plasma.
* *
@ -272,6 +283,17 @@ protected:
*/ */
virtual void paintWidget(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); virtual void paintWidget(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
/**
* The Data from the tooltip
* @returns A ToolTip::Data object with current information
*/
ToolTipData toolTip() const;
/**
* Setter for data shown in tooltip
* @param data a ToolTip::Data object containing icon and text
*/
void setToolTip( const ToolTipData &dt );
/** /**
* Reimplemented from QGraphicsItem * Reimplemented from QGraphicsItem
*/ */
@ -281,6 +303,8 @@ protected:
void setSize(qreal width, qreal height); void setSize(qreal width, qreal height);
void setSize(const QSizeF& size); void setSize(const QSizeF& size);
void managingLayoutChanged(); void managingLayoutChanged();
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
private: private:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);