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>
|
2008-01-12 06:32:07 +01:00
|
|
|
#include <QMouseEvent>
|
2007-12-21 06:33:17 +01:00
|
|
|
#include <QPixmap>
|
|
|
|
#include <QTimer>
|
2007-12-31 00:48:25 +01:00
|
|
|
#include <QGraphicsView>
|
2008-01-20 00:30:14 +01:00
|
|
|
#ifdef Q_WS_X11
|
2008-01-03 12:35:42 +01:00
|
|
|
#include <QX11Info>
|
2008-01-20 00:30:14 +01:00
|
|
|
#endif
|
2007-12-21 06:33:17 +01:00
|
|
|
|
2008-02-05 19:26:17 +01:00
|
|
|
#include <KDebug>
|
|
|
|
#include <KGlobal>
|
|
|
|
#include <KWindowSystem>
|
2008-02-05 01:01:15 +01:00
|
|
|
#include <plasma/theme.h>
|
2008-02-07 22:24:20 +01:00
|
|
|
#include <plasma/svgpanel.h>
|
2008-01-03 12:35:42 +01:00
|
|
|
|
2008-01-20 00:30:14 +01:00
|
|
|
#ifdef Q_WS_X11
|
2008-01-03 12:35:42 +01:00
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <fixx11h.h>
|
2008-01-20 00:30:14 +01:00
|
|
|
#endif
|
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)
|
2008-02-05 19:26:17 +01:00
|
|
|
, delayedHide(false)
|
2007-12-21 06:33:17 +01:00
|
|
|
, 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;
|
2008-02-05 19:26:17 +01:00
|
|
|
bool delayedHide;
|
2007-12-21 06:33:17 +01:00
|
|
|
QTimer *showTimer;
|
|
|
|
QTimer *hideTimer;
|
2008-02-07 22:24:20 +01:00
|
|
|
|
|
|
|
SvgPanel *background;
|
|
|
|
|
2007-12-21 06:33:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-02-05 19:26:17 +01:00
|
|
|
void ToolTip::show(Plasma::Widget *widget)
|
2007-12-21 06:33:17 +01:00
|
|
|
{
|
2008-02-05 19:26:17 +01:00
|
|
|
d->hideTimer->stop();
|
|
|
|
d->delayedHide = false;
|
2007-12-30 11:17:33 +01:00
|
|
|
d->currentWidget = widget;
|
2008-02-05 19:26:17 +01:00
|
|
|
d->showTimer->stop();
|
2008-01-21 12:42:08 +01:00
|
|
|
|
2007-12-21 06:33:17 +01:00
|
|
|
if (d->isShown) {
|
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
|
2008-02-05 19:26:17 +01:00
|
|
|
d->showTimer->start(200);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-05 19:26:17 +01:00
|
|
|
void ToolTip::delayedHide()
|
|
|
|
{
|
|
|
|
d->showTimer->stop(); // stop the timer to show the tooltip
|
|
|
|
d->delayedHide = true;
|
|
|
|
d->hideTimer->start(250);
|
|
|
|
}
|
|
|
|
|
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
|
2008-02-05 19:26:17 +01:00
|
|
|
d->delayedHide = false;
|
2007-12-21 06:33:17 +01:00
|
|
|
setVisible(false);
|
2008-02-03 17:57:00 +01:00
|
|
|
d->hideTimer->start(250); //250 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
|
2008-02-05 19:26:17 +01:00
|
|
|
void ToolTip::showToolTip()
|
2007-12-21 06:33:17 +01:00
|
|
|
{
|
2008-02-05 19:26:17 +01:00
|
|
|
if (!d->currentWidget || !d->currentWidget->toolTip()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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-02-05 19:26:17 +01:00
|
|
|
setData(*d->currentWidget->toolTip());
|
|
|
|
|
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();
|
|
|
|
|
2008-02-11 18:35:21 +01:00
|
|
|
resize(sizeHint());
|
|
|
|
move(d->currentWidget->popupPosition(size()));
|
|
|
|
|
2008-02-06 02:25:59 +01:00
|
|
|
if (isVisible()) {
|
|
|
|
d->preview->setInfo();
|
|
|
|
} else {
|
|
|
|
setVisible(true);
|
|
|
|
}
|
|
|
|
|
2007-12-21 06:33:17 +01:00
|
|
|
d->isShown = true; //ToolTip is visible
|
|
|
|
}
|
|
|
|
|
2008-02-05 19:26:17 +01:00
|
|
|
void ToolTip::resetShownState()
|
2007-12-21 06:33:17 +01:00
|
|
|
{
|
2008-02-05 19:26:17 +01:00
|
|
|
if (!isVisible() || //One might have moused out and back in again
|
|
|
|
d->delayedHide) {
|
|
|
|
d->delayedHide = false;
|
2007-12-21 06:33:17 +01:00
|
|
|
d->isShown = false;
|
2008-02-05 19:26:17 +01:00
|
|
|
d->currentWidget = 0;
|
|
|
|
setVisible(false);
|
2007-12-21 06:33:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-01-12 06:32:07 +01:00
|
|
|
void ToolTip::mouseReleaseEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
if (rect().contains(event->pos())) {
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2008-01-10 21:25:37 +01:00
|
|
|
d->label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
|
2007-12-21 06:33:17 +01:00
|
|
|
d->label->setWordWrap(true);
|
|
|
|
d->imageLabel = new QLabel;
|
|
|
|
d->imageLabel->setAlignment(Qt::AlignTop | Qt::AlignLeft);
|
2008-02-07 22:24:20 +01:00
|
|
|
|
|
|
|
d->background = new SvgPanel("widgets/tooltip", this);
|
|
|
|
d->background->setBorderFlags(SvgPanel::DrawAllBorders);
|
|
|
|
|
|
|
|
connect(d->background, SIGNAL(repaintNeeded()), this, SLOT(update()));
|
|
|
|
|
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);
|
|
|
|
|
2008-02-05 19:26:17 +01:00
|
|
|
connect(d->showTimer, SIGNAL(timeout()), SLOT(showToolTip()));
|
|
|
|
connect(d->hideTimer, SIGNAL(timeout()), SLOT(resetShownState()));
|
|
|
|
|
2008-02-08 21:50:55 +01:00
|
|
|
connect(Plasma::Theme::self(), SIGNAL(changed()), this, SLOT(themeUpdated()));
|
|
|
|
themeUpdated();
|
2008-02-05 19:26:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ToolTip::~ToolTip()
|
|
|
|
{
|
|
|
|
delete d;
|
2007-12-21 06:33:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTip::setData(const Plasma::ToolTipData &data)
|
|
|
|
{
|
2008-02-03 17:57:00 +01:00
|
|
|
//reset our size
|
2008-02-05 19:26:17 +01:00
|
|
|
d->label->setText("<qt><b>" + data.mainText + "</b><br>" + data.subText + "</qt>");
|
2007-12-21 06:33:17 +01:00
|
|
|
d->imageLabel->setPixmap(data.image);
|
2008-01-03 14:59:03 +01:00
|
|
|
d->windowToPreview = data.windowToPreview;
|
|
|
|
d->preview->setWindowId( d->windowToPreview );
|
2008-02-04 21:59:39 +01:00
|
|
|
|
2008-02-05 19:26:17 +01:00
|
|
|
if (isVisible()) {
|
|
|
|
resize(sizeHint());
|
|
|
|
|
|
|
|
if (d->currentWidget) {
|
|
|
|
move(d->currentWidget->popupPosition(size()));
|
|
|
|
}
|
|
|
|
}
|
2007-12-21 06:33:17 +01:00
|
|
|
}
|
|
|
|
|
2008-02-08 21:50:55 +01:00
|
|
|
void ToolTip::themeUpdated()
|
2007-12-21 06:33:17 +01:00
|
|
|
{
|
2008-02-08 21:50:55 +01:00
|
|
|
const int topHeight = d->background->marginSize(Plasma::TopMargin);
|
|
|
|
const int leftWidth = d->background->marginSize(Plasma::LeftMargin);
|
|
|
|
const int rightWidth = d->background->marginSize(Plasma::RightMargin);
|
|
|
|
const int bottomHeight = d->background->marginSize(Plasma::BottomMargin);
|
|
|
|
setContentsMargins(leftWidth, topHeight, rightWidth, bottomHeight);
|
|
|
|
|
2008-02-05 19:26:17 +01:00
|
|
|
// Make the tooltip use Plasma's colorscheme
|
|
|
|
QPalette plasmaPalette = QPalette();
|
|
|
|
plasmaPalette.setColor(QPalette::Window, Plasma::Theme::self()->backgroundColor());
|
|
|
|
plasmaPalette.setColor(QPalette::WindowText, Plasma::Theme::self()->textColor());
|
|
|
|
setAutoFillBackground(true);
|
|
|
|
setPalette(plasmaPalette);
|
2007-12-21 06:33:17 +01:00
|
|
|
}
|
|
|
|
|
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;
|
2008-02-03 14:06:34 +01:00
|
|
|
readWindowSize();
|
2008-01-03 12:35:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-20 00:30:14 +01:00
|
|
|
#ifdef Q_WS_X11
|
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;
|
|
|
|
}
|
2008-01-20 00:30:14 +01:00
|
|
|
#endif
|
2008-01-03 12:35:42 +01:00
|
|
|
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
|
|
|
|
{
|
2008-01-20 00:30:14 +01:00
|
|
|
#ifdef Q_WS_X11
|
2008-01-03 12:35:42 +01:00
|
|
|
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-20 00:30:14 +01:00
|
|
|
#else
|
|
|
|
windowSize = QSize();
|
|
|
|
#endif
|
2008-01-03 12:35:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WindowPreview::setInfo()
|
|
|
|
{
|
2008-01-20 00:30:14 +01:00
|
|
|
#ifdef Q_WS_X11
|
2008-01-07 15:58:50 +01:00
|
|
|
Display *dpy = QX11Info::display();
|
|
|
|
Atom atom = XInternAtom(dpy, "_KDE_WINDOW_PREVIEW", False);
|
|
|
|
if (id == 0) {
|
2008-02-26 14:00:49 +01:00
|
|
|
XDeleteProperty(dpy, parentWidget()->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()) {
|
2008-02-26 14:00:49 +01:00
|
|
|
XDeleteProperty(dpy, parentWidget()->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-20 00:30:14 +01:00
|
|
|
#endif
|
2008-01-03 12:35:42 +01:00
|
|
|
}
|
|
|
|
|
2008-02-17 08:47:52 +01:00
|
|
|
void ToolTip::resizeEvent(QResizeEvent *e)
|
2008-01-03 22:02:32 +01:00
|
|
|
{
|
2008-02-17 08:47:52 +01:00
|
|
|
QWidget::resizeEvent(e);
|
2008-02-07 22:24:20 +01:00
|
|
|
d->background->resize(size());
|
|
|
|
|
2008-03-05 22:03:27 +01:00
|
|
|
setMask(d->background->mask());
|
2008-01-07 16:03:14 +01:00
|
|
|
}
|
2008-01-07 05:41:30 +01:00
|
|
|
|
2008-02-07 22:24:20 +01:00
|
|
|
void ToolTip::paintEvent(QPaintEvent *e)
|
2008-01-07 16:03:14 +01:00
|
|
|
{
|
|
|
|
QPainter painter(this);
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
2008-02-07 22:24:20 +01:00
|
|
|
painter.setClipRect(e->rect());
|
|
|
|
painter.setCompositionMode(QPainter::CompositionMode_Source );
|
|
|
|
painter.fillRect(rect(), Qt::transparent);
|
2008-01-07 16:03:14 +01:00
|
|
|
|
2008-02-07 22:24:20 +01:00
|
|
|
d->background->paint(&painter, rect());
|
2008-01-03 22:02:32 +01:00
|
|
|
}
|
|
|
|
|
2007-12-21 06:33:17 +01:00
|
|
|
}
|
|
|
|
#include "tooltip_p.moc"
|