]> cloud.milkyroute.net Git - dolphin.git/blob - src/tooltips/ktooltip.cpp
a lot of more KUrl::path() -> KUrl::toLocalFile() changes (mostly after a check for...
[dolphin.git] / src / tooltips / ktooltip.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 by Fredrik Höglund <fredrik@kde.org> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "ktooltip.h"
21
22 #include <QApplication>
23 #include <QMap>
24 #include <QPixmap>
25 #include <QPainter>
26 #include <QVariant>
27 #include <QIcon>
28 #include <QWidget>
29 #include <QToolTip>
30 #include <QDebug>
31
32 #ifdef Q_WS_X11
33 # include <QX11Info>
34 # include <X11/Xlib.h>
35 # include <X11/extensions/Xrender.h>
36 # include <X11/extensions/shape.h>
37 #endif
38
39 #include "ktooltip_p.h"
40
41
42 // compile with XShape older than 1.0
43 #ifndef ShapeInput
44 const int ShapeInput = 2;
45 #endif
46
47
48 class KToolTipItemPrivate
49 {
50 public:
51 QMap<int, QVariant> map;
52 int type;
53 };
54
55 KToolTipItem::KToolTipItem(const QString &text, int type)
56 : d(new KToolTipItemPrivate)
57 {
58 d->map[Qt::DisplayRole] = text;
59 d->type = type;
60 }
61
62 KToolTipItem::KToolTipItem(const QIcon &icon, const QString &text, int type)
63 : d(new KToolTipItemPrivate)
64 {
65 d->map[Qt::DecorationRole] = icon;
66 d->map[Qt::DisplayRole] = text;
67 d->type = type;
68 }
69
70 KToolTipItem::~KToolTipItem()
71 {
72 delete d;
73 }
74
75 int KToolTipItem::type() const
76 {
77 return d->type;
78 }
79
80 QString KToolTipItem::text() const
81 {
82 return data(Qt::DisplayRole).toString();
83 }
84
85 QIcon KToolTipItem::icon() const
86 {
87 return qvariant_cast<QIcon>(data(Qt::DecorationRole));
88 }
89
90 QVariant KToolTipItem::data(int role) const
91 {
92 return d->map.value(role);
93 }
94
95 void KToolTipItem::setData(int role, const QVariant &data)
96 {
97 d->map[role] = data;
98 KToolTipManager::instance()->update();
99 }
100
101
102
103 // ----------------------------------------------------------------------------
104
105
106 KStyleOptionToolTip::KStyleOptionToolTip()
107 : fontMetrics(QApplication::font())
108 {
109 }
110
111
112 // ----------------------------------------------------------------------------
113
114
115
116 KToolTipDelegate::KToolTipDelegate() : QObject()
117 {
118 }
119
120 KToolTipDelegate::~KToolTipDelegate()
121 {
122 }
123
124 QSize KToolTipDelegate::sizeHint(const KStyleOptionToolTip *option, const KToolTipItem *item) const
125 {
126 QSize size;
127 size.rwidth() = option->fontMetrics.width(item->text());
128 size.rheight() = option->fontMetrics.lineSpacing();
129
130 QIcon icon = item->icon();
131 if (!icon.isNull()) {
132 const QSize iconSize = icon.actualSize(option->decorationSize);
133 size.rwidth() += iconSize.width() + 4;
134 size.rheight() = qMax(size.height(), iconSize.height());
135 }
136
137 return size + QSize(20, 20);
138
139 }
140
141 void KToolTipDelegate::paint(QPainter *painter, const KStyleOptionToolTip *option,
142 const KToolTipItem *item) const
143 {
144 bool haveAlpha = haveAlphaChannel();
145 painter->setRenderHint(QPainter::Antialiasing);
146
147 QPainterPath path;
148 if (haveAlpha)
149 path.addRoundRect(option->rect.adjusted(0, 0, -1, -1), 25);
150 else
151 path.addRect(option->rect.adjusted(0, 0, -1, -1));
152
153 QColor color = option->palette.color(QPalette::ToolTipBase);
154 QColor from = color.lighter(105);
155 QColor to = color.darker(120);
156
157 QLinearGradient gradient(0, 0, 0, 1);
158 gradient.setCoordinateMode(QGradient::ObjectBoundingMode);
159 gradient.setColorAt(0, from);
160 gradient.setColorAt(1, to);
161
162 painter->translate(.5, .5);
163 painter->setPen(QPen(Qt::black, 1));
164 painter->setBrush(gradient);
165 painter->drawPath(path);
166 painter->translate(-.5, -.5);
167
168 if (haveAlpha) {
169 QLinearGradient mask(0, 0, 0, 1);
170 gradient.setCoordinateMode(QGradient::ObjectBoundingMode);
171 gradient.setColorAt(0, QColor(0, 0, 0, 192));
172 gradient.setColorAt(1, QColor(0, 0, 0, 72));
173 painter->setCompositionMode(QPainter::CompositionMode_DestinationIn);
174 painter->fillRect(option->rect, gradient);
175 painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
176 }
177
178 QRect textRect = option->rect.adjusted(10, 10, -10, -10);
179
180 QIcon icon = item->icon();
181 if (!icon.isNull()) {
182 const QSize iconSize = icon.actualSize(option->decorationSize);
183 painter->drawPixmap(textRect.topLeft(), icon.pixmap(iconSize));
184 textRect.adjust(iconSize.width() + 4, 0, 0, 0);
185 }
186 painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, item->text());
187 }
188
189 QRegion KToolTipDelegate::inputShape(const KStyleOptionToolTip *option) const
190 {
191 return QRegion(option->rect);
192 }
193
194 QRegion KToolTipDelegate::shapeMask(const KStyleOptionToolTip *option) const
195 {
196 return QRegion(option->rect);
197 }
198
199 bool KToolTipDelegate::haveAlphaChannel() const
200 {
201 return KToolTipManager::instance()->haveAlphaChannel();
202 }
203
204
205
206 // ----------------------------------------------------------------------------
207
208
209
210 class KAbstractToolTipLabel
211 {
212 public:
213 KAbstractToolTipLabel() {}
214 virtual ~KAbstractToolTipLabel() {}
215
216 virtual void showTip(const QPoint &pos, const KToolTipItem *item) = 0;
217 virtual void moveTip(const QPoint &pos) = 0;
218 virtual void hideTip() = 0;
219
220 protected:
221 KStyleOptionToolTip styleOption() const;
222 KToolTipDelegate *delegate() const;
223 };
224
225 KStyleOptionToolTip KAbstractToolTipLabel::styleOption() const
226 {
227 KStyleOptionToolTip option;
228 KToolTipManager::instance()->initStyleOption(&option);
229 return option;
230 }
231
232 KToolTipDelegate *KAbstractToolTipLabel::delegate() const
233 {
234 return KToolTipManager::instance()->delegate();
235 }
236
237
238 // ----------------------------------------------------------------------------
239
240
241
242 class QWidgetLabel : public QWidget, public KAbstractToolTipLabel
243 {
244 public:
245 QWidgetLabel();
246 void showTip(const QPoint &pos, const KToolTipItem *item);
247 void moveTip(const QPoint &pos);
248 void hideTip();
249
250 private:
251 void paintEvent(QPaintEvent*);
252 QSize sizeHint() const;
253
254 private:
255 const KToolTipItem *currentItem;
256 };
257
258 QWidgetLabel::QWidgetLabel() : QWidget(0, Qt::ToolTip)
259 {
260 if (KToolTipManager::instance()->haveAlphaChannel()) {
261 setAttribute(Qt::WA_TranslucentBackground);
262 }
263 }
264
265 void QWidgetLabel::showTip(const QPoint &pos, const KToolTipItem *item)
266 {
267 currentItem = item;
268 move(pos);
269 show();
270 }
271
272 void QWidgetLabel::hideTip()
273 {
274 hide();
275 currentItem = 0;
276 }
277
278 void QWidgetLabel::moveTip(const QPoint &pos)
279 {
280 move(pos);
281 }
282
283 void QWidgetLabel::paintEvent(QPaintEvent*)
284 {
285 KStyleOptionToolTip option = styleOption();
286 option.rect = rect();
287
288 setMask(delegate()->shapeMask(&option));
289
290 QPainter p(this);
291 p.setFont(option.font);
292 p.setPen(QPen(option.palette.brush(QPalette::Text), 0));
293 delegate()->paint(&p, &option, currentItem);
294 }
295
296 QSize QWidgetLabel::sizeHint() const
297 {
298 if (!currentItem)
299 return QSize();
300
301 KStyleOptionToolTip option = styleOption();
302 return delegate()->sizeHint(&option, currentItem);
303 }
304
305
306
307 // ----------------------------------------------------------------------------
308
309
310
311 #ifdef Q_WS_X11
312
313 // X11 specific label that displays the tip in an ARGB window.
314 class ArgbLabel : public KAbstractToolTipLabel
315 {
316 public:
317 ArgbLabel(Visual *visual, int depth);
318 ~ArgbLabel();
319
320 void showTip(const QPoint &pos, const KToolTipItem *item);
321 void moveTip(const QPoint &pos);
322 void hideTip();
323
324 private:
325 Window window;
326 Colormap colormap;
327 Picture picture;
328 bool mapped;
329 };
330
331 ArgbLabel::ArgbLabel(Visual *visual, int depth)
332 {
333 Display *dpy = QX11Info::display();
334 Window root = QX11Info::appRootWindow();
335 colormap = XCreateColormap(dpy, QX11Info::appRootWindow(), visual, AllocNone);
336
337 XSetWindowAttributes attr;
338 attr.border_pixel = 0;
339 attr.background_pixel = 0;
340 attr.colormap = colormap;
341 attr.override_redirect = True;
342
343 window = XCreateWindow(dpy, root, 0, 0, 1, 1, 0, depth, InputOutput, visual,
344 CWBorderPixel | CWBackPixel | CWColormap |
345 CWOverrideRedirect, &attr);
346
347 // ### TODO: Set the WM hints so KWin can identify this window as a
348 // tooltip.
349
350 XRenderPictFormat *format = XRenderFindVisualFormat(dpy, visual);
351 picture = XRenderCreatePicture(dpy, window, format, 0, 0);
352
353 mapped = false;
354 }
355
356 ArgbLabel::~ArgbLabel()
357 {
358 Display *dpy = QX11Info::display();
359 XRenderFreePicture(dpy, picture);
360 XDestroyWindow(dpy, window);
361 XFreeColormap(dpy, colormap);
362 }
363
364 void ArgbLabel::showTip(const QPoint &pos, const KToolTipItem *item)
365 {
366 Display *dpy = QX11Info::display();
367 KStyleOptionToolTip option = styleOption();
368 const QSize size = delegate()->sizeHint(&option, item);
369 option.rect = QRect(QPoint(), size);
370
371 QPixmap pixmap(size);
372 pixmap.fill(Qt::transparent);
373
374 QPainter p(&pixmap);
375 p.setFont(option.font);
376 p.setPen(QPen(option.palette.brush(QPalette::Text), 0));
377 delegate()->paint(&p, &option, item);
378
379 // Resize, position and show the window.
380 XMoveResizeWindow(dpy, window, pos.x(), pos.y(), size.width(), size.height());
381
382 if (KToolTipManager::instance()->haveAlphaChannel()) {
383 const QRegion region = delegate()->inputShape(&option);
384 XShapeCombineRegion(dpy, window, ShapeInput, 0, 0, region.handle(), ShapeSet);
385 } else {
386 const QRegion region = delegate()->shapeMask(&option);
387 XShapeCombineRegion(dpy, window, ShapeBounding, 0, 0, region.handle(), ShapeSet);
388 }
389
390 XMapWindow(dpy, window);
391
392 // Blit the pixmap with the tip contents to the window.
393 // Since the window is override-redirect and an ARGB32 window,
394 // which always has an offscreen pixmap, there's no need to
395 // wait for an Expose event, or to process those.
396 XRenderComposite(dpy, PictOpSrc, pixmap.x11PictureHandle(), None,
397 picture, 0, 0, 0, 0, 0, 0, size.width(), size.height());
398
399 mapped = true;
400 }
401
402 void ArgbLabel::moveTip(const QPoint &pos)
403 {
404 if (mapped)
405 XMoveWindow(QX11Info::display(), window, pos.x(), pos.y());
406 }
407
408 void ArgbLabel::hideTip()
409 {
410 if (mapped) {
411 Display *dpy = QX11Info::display();
412 XUnmapWindow(dpy, window);
413 mapped = false;
414 }
415 }
416
417 #endif // Q_WS_X11
418
419
420
421
422 // ----------------------------------------------------------------------------
423
424
425
426
427 KToolTipManager *KToolTipManager::s_instance = 0;
428
429 KToolTipManager::KToolTipManager()
430 : label(0), currentItem(0), m_delegate(0)
431 {
432 #ifdef Q_WS_X11
433 Display *dpy = QX11Info::display();
434 int screen = DefaultScreen(dpy);
435 int depth = DefaultDepth(dpy, screen);
436 Visual *visual = DefaultVisual(dpy, screen);
437 net_wm_cm_s0 = XInternAtom(dpy, "_NET_WM_CM_S0", False);
438 haveArgbVisual = false;
439
440 int nvi;
441 XVisualInfo templ;
442 templ.screen = screen;
443 templ.depth = 32;
444 templ.c_class = TrueColor;
445 XVisualInfo *xvi = XGetVisualInfo(dpy, VisualScreenMask | VisualDepthMask |
446 VisualClassMask, &templ, &nvi);
447
448 for (int i = 0; i < nvi; ++i)
449 {
450 XRenderPictFormat *format = XRenderFindVisualFormat(dpy, xvi[i].visual);
451 if (format->type == PictTypeDirect && format->direct.alphaMask)
452 {
453 visual = xvi[i].visual;
454 depth = xvi[i].depth;
455 haveArgbVisual = true;
456 break;
457 }
458 }
459
460 if (haveArgbVisual)
461 label = new ArgbLabel(visual, depth);
462 else
463 #endif
464 label = new QWidgetLabel();
465 }
466
467 KToolTipManager::~KToolTipManager()
468 {
469 delete label;
470 delete currentItem;
471 }
472
473 void KToolTipManager::showTip(const QPoint &pos, KToolTipItem *item)
474 {
475 hideTip();
476 label->showTip(pos, item);
477 currentItem = item;
478 m_tooltipPos = pos;
479 }
480
481 void KToolTipManager::hideTip()
482 {
483 label->hideTip();
484 delete currentItem;
485 currentItem = 0;
486 }
487
488 void KToolTipManager::initStyleOption(KStyleOptionToolTip *option) const
489 {
490 option->direction = QApplication::layoutDirection();
491 option->fontMetrics = QFontMetrics(QToolTip::font());
492 option->activeCorner = KStyleOptionToolTip::TopLeftCorner;
493 option->palette = QToolTip::palette();
494 option->font = QToolTip::font();
495 option->rect = QRect();
496 option->state = QStyle::State_None;
497 option->decorationSize = QSize(32, 32);
498 }
499
500 bool KToolTipManager::haveAlphaChannel() const
501 {
502 #ifdef Q_WS_X11
503 // ### This is a synchronous call - ideally we'd use a selection
504 // watcher to avoid it.
505 return haveArgbVisual &&
506 XGetSelectionOwner(QX11Info::display(), net_wm_cm_s0) != None;
507 #else
508 return false;
509 #endif
510 }
511
512 void KToolTipManager::setDelegate(KToolTipDelegate *delegate)
513 {
514 m_delegate = delegate;
515 }
516
517 void KToolTipManager::update()
518 {
519 if (currentItem == 0)
520 return;
521 label->showTip(m_tooltipPos, currentItem);
522 }
523
524 KToolTipDelegate *KToolTipManager::delegate() const
525 {
526 return m_delegate;
527 }
528
529
530
531 // ----------------------------------------------------------------------------
532
533
534
535 namespace KToolTip
536 {
537 void showText(const QPoint &pos, const QString &text, QWidget *widget, const QRect &rect)
538 {
539 Q_UNUSED(widget)
540 Q_UNUSED(rect)
541 KToolTipItem *item = new KToolTipItem(text);
542 KToolTipManager::instance()->showTip(pos, item);
543 }
544
545 void showText(const QPoint &pos, const QString &text, QWidget *widget)
546 {
547 showText(pos, text, widget, QRect());
548 }
549
550 void showTip(const QPoint &pos, KToolTipItem *item)
551 {
552 KToolTipManager::instance()->showTip(pos, item);
553 }
554
555 void hideTip()
556 {
557 KToolTipManager::instance()->hideTip();
558 }
559
560 void setToolTipDelegate(KToolTipDelegate *delegate)
561 {
562 KToolTipManager::instance()->setDelegate(delegate);
563 }
564 }
565