]> cloud.milkyroute.net Git - dolphin.git/blob - src/ktooltip.cpp
prevent that the user can open more than one instance of the settings dialog
[dolphin.git] / src / 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() : QWidget(0, Qt::ToolTip) {}
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 void QWidgetLabel::showTip(const QPoint &pos, const KToolTipItem *item)
259 {
260 currentItem = item;
261 move(pos);
262 show();
263 }
264
265 void QWidgetLabel::hideTip()
266 {
267 hide();
268 currentItem = 0;
269 }
270
271 void QWidgetLabel::moveTip(const QPoint &pos)
272 {
273 move(pos);
274 }
275
276 void QWidgetLabel::paintEvent(QPaintEvent*)
277 {
278 KStyleOptionToolTip option = styleOption();
279 option.rect = rect();
280
281 setMask(delegate()->shapeMask(&option));
282
283 QPainter p(this);
284 p.setFont(option.font);
285 p.setPen(QPen(option.palette.brush(QPalette::Text), 0));
286 delegate()->paint(&p, &option, currentItem);
287 }
288
289 QSize QWidgetLabel::sizeHint() const
290 {
291 if (!currentItem)
292 return QSize();
293
294 KStyleOptionToolTip option = styleOption();
295 return delegate()->sizeHint(&option, currentItem);
296 }
297
298
299
300 // ----------------------------------------------------------------------------
301
302
303
304 #ifdef Q_WS_X11
305
306 // X11 specific label that displays the tip in an ARGB window.
307 class ArgbLabel : public KAbstractToolTipLabel
308 {
309 public:
310 ArgbLabel(Visual *visual, int depth);
311 ~ArgbLabel();
312
313 void showTip(const QPoint &pos, const KToolTipItem *item);
314 void moveTip(const QPoint &pos);
315 void hideTip();
316
317 private:
318 Window window;
319 Colormap colormap;
320 Picture picture;
321 bool mapped;
322 };
323
324 ArgbLabel::ArgbLabel(Visual *visual, int depth)
325 {
326 Display *dpy = QX11Info::display();
327 Window root = QX11Info::appRootWindow();
328 colormap = XCreateColormap(dpy, QX11Info::appRootWindow(), visual, AllocNone);
329
330 XSetWindowAttributes attr;
331 attr.border_pixel = 0;
332 attr.background_pixel = 0;
333 attr.colormap = colormap;
334 attr.override_redirect = True;
335
336 window = XCreateWindow(dpy, root, 0, 0, 1, 1, 0, depth, InputOutput, visual,
337 CWBorderPixel | CWBackPixel | CWColormap |
338 CWOverrideRedirect, &attr);
339
340 // ### TODO: Set the WM hints so KWin can identify this window as a
341 // tooltip.
342
343 XRenderPictFormat *format = XRenderFindVisualFormat(dpy, visual);
344 picture = XRenderCreatePicture(dpy, window, format, 0, 0);
345
346 mapped = false;
347 }
348
349 ArgbLabel::~ArgbLabel()
350 {
351 Display *dpy = QX11Info::display();
352 XRenderFreePicture(dpy, picture);
353 XDestroyWindow(dpy, window);
354 XFreeColormap(dpy, colormap);
355 }
356
357 void ArgbLabel::showTip(const QPoint &pos, const KToolTipItem *item)
358 {
359 Display *dpy = QX11Info::display();
360 KStyleOptionToolTip option = styleOption();
361 const QSize size = delegate()->sizeHint(&option, item);
362 option.rect = QRect(QPoint(), size);
363
364 QPixmap pixmap(size);
365 pixmap.fill(Qt::transparent);
366
367 QPainter p(&pixmap);
368 p.setFont(option.font);
369 p.setPen(QPen(option.palette.brush(QPalette::Text), 0));
370 delegate()->paint(&p, &option, item);
371
372 // Resize, position and show the window.
373 XMoveResizeWindow(dpy, window, pos.x(), pos.y(), size.width(), size.height());
374
375 if (KToolTipManager::instance()->haveAlphaChannel()) {
376 const QRegion region = delegate()->inputShape(&option);
377 XShapeCombineRegion(dpy, window, ShapeInput, 0, 0, region.handle(), ShapeSet);
378 } else {
379 const QRegion region = delegate()->shapeMask(&option);
380 XShapeCombineRegion(dpy, window, ShapeBounding, 0, 0, region.handle(), ShapeSet);
381 }
382
383 XMapWindow(dpy, window);
384
385 // Blit the pixmap with the tip contents to the window.
386 // Since the window is override-redirect and an ARGB32 window,
387 // which always has an offscreen pixmap, there's no need to
388 // wait for an Expose event, or to process those.
389 XRenderComposite(dpy, PictOpSrc, pixmap.x11PictureHandle(), None,
390 picture, 0, 0, 0, 0, 0, 0, size.width(), size.height());
391
392 mapped = true;
393 }
394
395 void ArgbLabel::moveTip(const QPoint &pos)
396 {
397 if (mapped)
398 XMoveWindow(QX11Info::display(), window, pos.x(), pos.y());
399 }
400
401 void ArgbLabel::hideTip()
402 {
403 if (mapped) {
404 Display *dpy = QX11Info::display();
405 XUnmapWindow(dpy, window);
406 mapped = false;
407 }
408 }
409
410 #endif // Q_WS_X11
411
412
413
414
415 // ----------------------------------------------------------------------------
416
417
418
419
420 KToolTipManager *KToolTipManager::s_instance = 0;
421
422 KToolTipManager::KToolTipManager()
423 : label(0), currentItem(0), m_delegate(0)
424 {
425 #ifdef Q_WS_X11
426 Display *dpy = QX11Info::display();
427 int screen = DefaultScreen(dpy);
428 int depth = DefaultDepth(dpy, screen);
429 Visual *visual = DefaultVisual(dpy, screen);
430 net_wm_cm_s0 = XInternAtom(dpy, "_NET_WM_CM_S0", False);
431 haveArgbVisual = false;
432
433 int nvi;
434 XVisualInfo templ;
435 templ.screen = screen;
436 templ.depth = 32;
437 templ.c_class = TrueColor;
438 XVisualInfo *xvi = XGetVisualInfo(dpy, VisualScreenMask | VisualDepthMask |
439 VisualClassMask, &templ, &nvi);
440
441 for (int i = 0; i < nvi; ++i)
442 {
443 XRenderPictFormat *format = XRenderFindVisualFormat(dpy, xvi[i].visual);
444 if (format->type == PictTypeDirect && format->direct.alphaMask)
445 {
446 visual = xvi[i].visual;
447 depth = xvi[i].depth;
448 haveArgbVisual = true;
449 break;
450 }
451 }
452
453 if (haveArgbVisual)
454 label = new ArgbLabel(visual, depth);
455 else
456 #endif
457 label = new QWidgetLabel();
458 }
459
460 KToolTipManager::~KToolTipManager()
461 {
462 delete label;
463 delete currentItem;
464 }
465
466 void KToolTipManager::showTip(const QPoint &pos, KToolTipItem *item)
467 {
468 hideTip();
469 label->showTip(pos, item);
470 currentItem = item;
471 m_tooltipPos = pos;
472 }
473
474 void KToolTipManager::hideTip()
475 {
476 label->hideTip();
477 delete currentItem;
478 currentItem = 0;
479 }
480
481 void KToolTipManager::initStyleOption(KStyleOptionToolTip *option) const
482 {
483 option->direction = QApplication::layoutDirection();
484 option->fontMetrics = QFontMetrics(QToolTip::font());
485 option->activeCorner = KStyleOptionToolTip::TopLeftCorner;
486 option->palette = QToolTip::palette();
487 option->font = QToolTip::font();
488 option->rect = QRect();
489 option->state = QStyle::State_None;
490 option->decorationSize = QSize(32, 32);
491 }
492
493 bool KToolTipManager::haveAlphaChannel() const
494 {
495 #ifdef Q_WS_X11
496 // ### This is a synchronous call - ideally we'd use a selection
497 // watcher to avoid it.
498 return haveArgbVisual &&
499 XGetSelectionOwner(QX11Info::display(), net_wm_cm_s0) != None;
500 #else
501 return false;
502 #endif
503 }
504
505 void KToolTipManager::setDelegate(KToolTipDelegate *delegate)
506 {
507 m_delegate = delegate;
508 }
509
510 void KToolTipManager::update()
511 {
512 if (currentItem == 0)
513 return;
514 label->showTip(m_tooltipPos, currentItem);
515 }
516
517 KToolTipDelegate *KToolTipManager::delegate() const
518 {
519 return m_delegate;
520 }
521
522
523
524 // ----------------------------------------------------------------------------
525
526
527
528 namespace KToolTip
529 {
530 void showText(const QPoint &pos, const QString &text, QWidget *widget, const QRect &rect)
531 {
532 Q_UNUSED(widget)
533 Q_UNUSED(rect)
534 KToolTipItem *item = new KToolTipItem(text);
535 KToolTipManager::instance()->showTip(pos, item);
536 }
537
538 void showText(const QPoint &pos, const QString &text, QWidget *widget)
539 {
540 showText(pos, text, widget, QRect());
541 }
542
543 void showTip(const QPoint &pos, KToolTipItem *item)
544 {
545 KToolTipManager::instance()->showTip(pos, item);
546 }
547
548 void hideTip()
549 {
550 KToolTipManager::instance()->hideTip();
551 }
552
553 void setToolTipDelegate(KToolTipDelegate *delegate)
554 {
555 KToolTipManager::instance()->setDelegate(delegate);
556 }
557 }
558