2007-12-21 06:33:17 +01:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2008-01-03 22:02:32 +01:00
|
|
|
#include <QBitmap>
|
2007-12-21 06:33:17 +01:00
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QPixmap>
|
|
|
|
#include <QTimer>
|
2007-12-31 00:48:25 +01:00
|
|
|
#include <QGraphicsView>
|
2008-01-03 12:35:42 +01:00
|
|
|
#include <QX11Info>
|
2007-12-21 06:33:17 +01:00
|
|
|
|
|
|
|
#include <kglobal.h>
|
2008-01-03 12:35:42 +01:00
|
|
|
#include <kwindowsystem.h>
|
|
|
|
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <fixx11h.h>
|
2007-12-21 06:33:17 +01:00
|
|
|
|
2008-01-03 22:02:32 +01:00
|
|
|
#include "plasma/plasma.h"
|
|
|
|
|
2007-12-21 06:33:17 +01:00
|
|
|
namespace Plasma {
|
|
|
|
|
|
|
|
class ToolTip::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Private()
|
|
|
|
: label(0)
|
|
|
|
, imageLabel(0)
|
2008-01-03 12:35:42 +01:00
|
|
|
, preview(0)
|
|
|
|
, windowToPreview(0)
|
2007-12-30 11:17:33 +01:00
|
|
|
, currentWidget(0)
|
2007-12-21 06:33:17 +01:00
|
|
|
, isShown(false)
|
|
|
|
, showTimer(0)
|
|
|
|
, hideTimer(0)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
QLabel *label;
|
|
|
|
QLabel *imageLabel;
|
2008-01-03 12:35:42 +01:00
|
|
|
WindowPreview *preview;
|
|
|
|
WId windowToPreview;
|
2007-12-30 11:17:33 +01:00
|
|
|
Plasma::Widget *currentWidget;
|
2007-12-21 06:33:17 +01:00
|
|
|
bool isShown;
|
|
|
|
QTimer *showTimer;
|
|
|
|
QTimer *hideTimer;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ToolTipSingleton
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ToolTip self;
|
|
|
|
};
|
|
|
|
K_GLOBAL_STATIC( ToolTipSingleton, privateInstance )
|
|
|
|
|
|
|
|
ToolTip *ToolTip::instance()
|
|
|
|
{
|
|
|
|
return &privateInstance->self;
|
|
|
|
}
|
|
|
|
|
2007-12-30 11:17:33 +01:00
|
|
|
void ToolTip::show(const QPoint &location, Plasma::Widget *widget)
|
2007-12-21 06:33:17 +01:00
|
|
|
{
|
2007-12-30 11:17:33 +01:00
|
|
|
d->currentWidget = widget;
|
|
|
|
setData(widget->toolTip());
|
2007-12-21 06:33:17 +01:00
|
|
|
move(location.x(), location.y() - sizeHint().height());
|
|
|
|
if (d->isShown) {
|
2007-12-22 04:44:25 +01:00
|
|
|
// Don't delay if the tooltip is already shown(i.e. moving from one task to another)
|
|
|
|
// Qt doesn't seem to like visible tooltips moving though, so hide it and then
|
|
|
|
// immediately show it again
|
|
|
|
setVisible(false);
|
|
|
|
d->showTimer->start(0);
|
2007-12-30 11:17:33 +01:00
|
|
|
} else {
|
2007-12-21 06:33:17 +01:00
|
|
|
d->showTimer->start(1000); //Shown after a one second delay.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTip::hide()
|
|
|
|
{
|
2007-12-30 11:17:33 +01:00
|
|
|
d->currentWidget = 0;
|
2007-12-21 06:33:17 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2007-12-30 11:17:33 +01:00
|
|
|
Plasma::Widget *ToolTip::currentWidget() const
|
|
|
|
{
|
|
|
|
return d->currentWidget;
|
|
|
|
}
|
|
|
|
|
2007-12-21 06:33:17 +01:00
|
|
|
//PRIVATE FUNCTIONS
|
|
|
|
void ToolTip::slotShowToolTip()
|
|
|
|
{
|
2007-12-31 02:45:48 +01:00
|
|
|
QGraphicsView *v = d->currentWidget->view();
|
|
|
|
if (v && v->mouseGrabber()) {
|
2008-01-03 19:20:35 +01:00
|
|
|
return;
|
2007-12-31 02:45:48 +01:00
|
|
|
}
|
2007-12-31 00:48:25 +01:00
|
|
|
|
2008-01-03 19:20:35 +01:00
|
|
|
if( d->windowToPreview != 0 ) {
|
|
|
|
// show/hide the preview area
|
2008-01-03 14:59:03 +01:00
|
|
|
d->preview->show();
|
2008-01-03 19:20:35 +01:00
|
|
|
} else {
|
2008-01-03 14:59:03 +01:00
|
|
|
d->preview->hide();
|
2008-01-03 19:20:35 +01:00
|
|
|
}
|
2008-01-03 14:59:03 +01:00
|
|
|
layout()->activate();
|
|
|
|
|
2007-12-21 06:33:17 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-03 12:35:42 +01:00
|
|
|
void ToolTip::showEvent( QShowEvent* e )
|
|
|
|
{
|
|
|
|
QWidget::showEvent( e );
|
2008-01-03 14:59:03 +01:00
|
|
|
d->preview->setInfo();
|
2008-01-03 12:35:42 +01:00
|
|
|
}
|
|
|
|
|
2007-12-21 06:33:17 +01:00
|
|
|
ToolTip::ToolTip()
|
|
|
|
: QWidget(0)
|
|
|
|
, d( new Private )
|
|
|
|
{
|
|
|
|
setWindowFlags(Qt::ToolTip);
|
2008-01-03 12:35:42 +01:00
|
|
|
QGridLayout *l = new QGridLayout;
|
|
|
|
d->preview = new WindowPreview;
|
2007-12-21 06:33:17 +01:00
|
|
|
d->label = new QLabel;
|
|
|
|
d->label->setWordWrap(true);
|
|
|
|
d->imageLabel = new QLabel;
|
|
|
|
d->imageLabel->setAlignment(Qt::AlignTop | Qt::AlignLeft);
|
2008-01-03 12:35:42 +01:00
|
|
|
l->addWidget(d->preview, 0, 0, 1, 2);
|
|
|
|
l->addWidget(d->imageLabel, 1, 0);
|
|
|
|
l->addWidget(d->label, 1, 1);
|
2007-12-21 06:33:17 +01:00
|
|
|
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);
|
2008-01-03 14:59:03 +01:00
|
|
|
d->windowToPreview = data.windowToPreview;
|
|
|
|
d->preview->setWindowId( d->windowToPreview );
|
2007-12-21 06:33:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ToolTip::~ToolTip()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2008-01-03 12:35:42 +01:00
|
|
|
|
|
|
|
// A widget which reserves area for window preview and sets hints on the toplevel
|
|
|
|
// tooltip widget that tells KWin to render the preview in this area. This depends
|
|
|
|
// on KWin's TaskbarThumbnail compositing effect (which is here detected).
|
|
|
|
|
|
|
|
void WindowPreview::setWindowId( WId w )
|
|
|
|
{
|
|
|
|
if( !previewsAvailable()) {
|
|
|
|
id = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
id = w;
|
|
|
|
windowSize = QSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WindowPreview::previewsAvailable() const
|
|
|
|
{
|
|
|
|
if( !KWindowSystem::compositingActive())
|
|
|
|
return false;
|
|
|
|
// hackish way to find out if KWin has the effect enabled,
|
|
|
|
// TODO provide proper support
|
|
|
|
Display* dpy = QX11Info::display();
|
|
|
|
Atom atom = XInternAtom( dpy, "_KDE_WINDOW_PREVIEW", False );
|
|
|
|
int cnt;
|
|
|
|
Atom* list = XListProperties( dpy, DefaultRootWindow( dpy ), &cnt );
|
|
|
|
if( list != NULL ) {
|
|
|
|
bool ret = ( qFind( list, list + cnt, atom ) != list + cnt );
|
|
|
|
XFree( list );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize WindowPreview::sizeHint() const
|
|
|
|
{
|
|
|
|
if( id == 0 )
|
|
|
|
return QSize();
|
|
|
|
if( !windowSize.isValid())
|
|
|
|
readWindowSize();
|
|
|
|
QSize s = windowSize;
|
|
|
|
s.scale( 200, 150, Qt::KeepAspectRatio );
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WindowPreview::readWindowSize() const
|
|
|
|
{
|
|
|
|
Window r;
|
|
|
|
int x, y;
|
|
|
|
unsigned int w, h, b, d;
|
|
|
|
if( XGetGeometry( QX11Info::display(), id, &r, &x, &y, &w, &h, &b, &d ))
|
|
|
|
windowSize = QSize( w, h );
|
|
|
|
else
|
|
|
|
windowSize = QSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WindowPreview::setInfo()
|
|
|
|
{
|
|
|
|
Display* dpy = QX11Info::display();
|
|
|
|
Atom atom = XInternAtom( dpy, "_KDE_WINDOW_PREVIEW", False );
|
|
|
|
if( id == 0 ) {
|
|
|
|
XDeleteProperty( dpy, winId(), atom );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if( !windowSize.isValid())
|
|
|
|
readWindowSize();
|
|
|
|
if( !windowSize.isValid()) {
|
|
|
|
XDeleteProperty( dpy, winId(), atom );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Q_ASSERT( parentWidget()->isWindow()); // parent must be toplevel
|
|
|
|
long data[] = { 1, 5, id, x(), y(), width(), height() };
|
|
|
|
XChangeProperty( dpy, parentWidget()->winId(), atom, atom, 32, PropModeReplace,
|
|
|
|
reinterpret_cast< unsigned char* >( data ), sizeof( data ) / sizeof( data[ 0 ] ));
|
|
|
|
}
|
|
|
|
|
2008-01-03 22:02:32 +01:00
|
|
|
//Patterned after KickerTip
|
|
|
|
//TODO: Do something to antialias edges
|
|
|
|
//Reimplement paintEvent perhaps?
|
|
|
|
void ToolTip::resizeEvent(QResizeEvent *)
|
|
|
|
{
|
|
|
|
QBitmap mask(width(), height());
|
|
|
|
QPainter painter(&mask);
|
|
|
|
|
|
|
|
mask.fill(Qt::white);
|
|
|
|
|
|
|
|
painter.setBrush(Qt::black);
|
|
|
|
painter.setPen(Qt::black);
|
|
|
|
painter.drawPath(Plasma::roundedRectangle(mask.rect(), 10));
|
|
|
|
setMask(mask);
|
|
|
|
}
|
|
|
|
|
2007-12-21 06:33:17 +01:00
|
|
|
}
|
|
|
|
#include "tooltip_p.moc"
|