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 )
|
|
|
|
|
2008-01-07 05:39:36 +01:00
|
|
|
ToolTip *ToolTip::self()
|
2007-12-21 06:33:17 +01:00
|
|
|
{
|
|
|
|
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);
|
2008-01-06 01:43:49 +01:00
|
|
|
|
|
|
|
// small delay to prevent unecessary 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(150);
|
2007-12-30 11:17:33 +01:00
|
|
|
} else {
|
2008-01-06 01:43:49 +01:00
|
|
|
d->showTimer->start(500);
|
2007-12-21 06:33:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
setVisible(false);
|
2008-01-06 01:50:56 +01:00
|
|
|
d->hideTimer->start(250); //500 ms delay before we are officially "gone" to allow for the time to move between widgets
|
2007-12-21 06:33:17 +01:00
|
|
|
}
|
|
|
|
|
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-07 15:58:50 +01:00
|
|
|
void ToolTip::showEvent(QShowEvent *e)
|
2008-01-03 12:35:42 +01:00
|
|
|
{
|
2008-01-07 15:58:50 +01:00
|
|
|
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).
|
|
|
|
|
2008-01-07 15:58:50 +01:00
|
|
|
void WindowPreview::setWindowId(WId w)
|
2008-01-03 12:35:42 +01:00
|
|
|
{
|
2008-01-07 15:58:50 +01:00
|
|
|
if (!previewsAvailable()) {
|
2008-01-03 12:35:42 +01:00
|
|
|
id = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
id = w;
|
|
|
|
windowSize = QSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WindowPreview::previewsAvailable() const
|
|
|
|
{
|
2008-01-07 15:58:50 +01:00
|
|
|
if (!KWindowSystem::compositingActive()) {
|
2008-01-03 12:35:42 +01:00
|
|
|
return false;
|
2008-01-07 15:58:50 +01:00
|
|
|
}
|
2008-01-03 12:35:42 +01:00
|
|
|
// hackish way to find out if KWin has the effect enabled,
|
|
|
|
// TODO provide proper support
|
|
|
|
Display* dpy = QX11Info::display();
|
2008-01-07 15:58:50 +01:00
|
|
|
Atom atom = XInternAtom(dpy, "_KDE_WINDOW_PREVIEW", False);
|
2008-01-03 12:35:42 +01:00
|
|
|
int cnt;
|
2008-01-07 15:58:50 +01:00
|
|
|
Atom* list = XListProperties(dpy, DefaultRootWindow( dpy ), &cnt);
|
|
|
|
if (list != NULL) {
|
|
|
|
bool ret = ( qFind(list, list + cnt, atom) != list + cnt );
|
|
|
|
XFree(list);
|
2008-01-03 12:35:42 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize WindowPreview::sizeHint() const
|
|
|
|
{
|
2008-01-07 15:58:50 +01:00
|
|
|
if (id == 0) {
|
2008-01-03 12:35:42 +01:00
|
|
|
return QSize();
|
2008-01-07 15:58:50 +01:00
|
|
|
}
|
|
|
|
if (!windowSize.isValid()) {
|
2008-01-03 12:35:42 +01:00
|
|
|
readWindowSize();
|
2008-01-07 15:58:50 +01:00
|
|
|
}
|
2008-01-03 12:35:42 +01:00
|
|
|
QSize s = windowSize;
|
2008-01-07 15:58:50 +01:00
|
|
|
s.scale(200, 150, Qt::KeepAspectRatio);
|
2008-01-03 12:35:42 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WindowPreview::readWindowSize() const
|
|
|
|
{
|
|
|
|
Window r;
|
|
|
|
int x, y;
|
|
|
|
unsigned int w, h, b, d;
|
2008-01-07 15:58:50 +01:00
|
|
|
if (XGetGeometry(QX11Info::display(), id, &r, &x, &y, &w, &h, &b, &d)) {
|
2008-01-03 12:35:42 +01:00
|
|
|
windowSize = QSize( w, h );
|
2008-01-07 15:58:50 +01:00
|
|
|
} else {
|
2008-01-03 12:35:42 +01:00
|
|
|
windowSize = QSize();
|
2008-01-07 15:58:50 +01:00
|
|
|
}
|
2008-01-03 12:35:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WindowPreview::setInfo()
|
|
|
|
{
|
2008-01-07 15:58:50 +01:00
|
|
|
Display *dpy = QX11Info::display();
|
|
|
|
Atom atom = XInternAtom(dpy, "_KDE_WINDOW_PREVIEW", False);
|
|
|
|
if (id == 0) {
|
|
|
|
XDeleteProperty(dpy, winId(), atom);
|
2008-01-03 12:35:42 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-01-07 15:58:50 +01:00
|
|
|
if (!windowSize.isValid()) {
|
2008-01-03 12:35:42 +01:00
|
|
|
readWindowSize();
|
2008-01-07 15:58:50 +01:00
|
|
|
}
|
|
|
|
if (!windowSize.isValid()) {
|
|
|
|
XDeleteProperty(dpy, winId(), atom);
|
2008-01-03 12:35:42 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Q_ASSERT( parentWidget()->isWindow()); // parent must be toplevel
|
|
|
|
long data[] = { 1, 5, id, x(), y(), width(), height() };
|
2008-01-07 15:58:50 +01:00
|
|
|
XChangeProperty(dpy, parentWidget()->winId(), atom, atom, 32, PropModeReplace,
|
2008-01-03 12:35:42 +01:00
|
|
|
reinterpret_cast< unsigned char* >( data ), sizeof( data ) / sizeof( data[ 0 ] ));
|
|
|
|
}
|
|
|
|
|
2008-01-07 16:03:14 +01:00
|
|
|
void ToolTip::resizeEvent(QResizeEvent *)
|
2008-01-03 22:02:32 +01:00
|
|
|
{
|
|
|
|
QBitmap mask(width(), height());
|
2008-01-07 16:03:14 +01:00
|
|
|
QPainter painter(&mask);
|
2008-01-03 22:02:32 +01:00
|
|
|
|
|
|
|
mask.fill(Qt::white);
|
|
|
|
|
2008-01-07 16:03:14 +01:00
|
|
|
painter.setBrush(Qt::black);
|
|
|
|
painter.setPen(Qt::black);
|
2008-01-07 05:41:30 +01:00
|
|
|
|
2008-01-07 16:03:14 +01:00
|
|
|
painter.drawPath(roundedRectangle(mask.rect(), 10));
|
2008-01-03 22:02:32 +01:00
|
|
|
setMask(mask);
|
2008-01-07 16:03:14 +01:00
|
|
|
}
|
2008-01-07 05:41:30 +01:00
|
|
|
|
2008-01-07 16:03:14 +01:00
|
|
|
void ToolTip::paintEvent(QPaintEvent *)
|
|
|
|
{
|
|
|
|
QPainter painter(this);
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
|
|
|
|
//Stroke border
|
|
|
|
painter.setPen(palette().dark().color());
|
|
|
|
painter.drawPath(roundedRectangle(rect(), 10));
|
2008-01-03 22:02:32 +01:00
|
|
|
}
|
|
|
|
|
2007-12-21 06:33:17 +01:00
|
|
|
}
|
|
|
|
#include "tooltip_p.moc"
|