]> cloud.milkyroute.net Git - dolphin.git/blob - src/tooltips/ktooltip.cpp
increase the acceleration when autoscrolling
[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 #include "ktooltip_p.h"
22
23 #include <QApplication>
24 #include <QMap>
25 #include <QPixmap>
26 #include <QPainter>
27 #include <QVariant>
28 #include <QIcon>
29 #include <QWidget>
30 #include <QToolTip>
31 #include <QDebug>
32
33 #ifdef Q_WS_X11
34 # include <QX11Info>
35 # include <X11/Xlib.h>
36 # include <X11/extensions/shape.h>
37 #endif
38
39 // compile with XShape older than 1.0
40 #ifndef ShapeInput
41 const int ShapeInput = 2;
42 #endif
43
44
45 class KToolTipItemPrivate
46 {
47 public:
48 QMap<int, QVariant> map;
49 int type;
50 };
51
52 KToolTipItem::KToolTipItem(const QString &text, int type)
53 : d(new KToolTipItemPrivate)
54 {
55 d->map[Qt::DisplayRole] = text;
56 d->type = type;
57 }
58
59 KToolTipItem::KToolTipItem(const QIcon &icon, const QString &text, int type)
60 : d(new KToolTipItemPrivate)
61 {
62 d->map[Qt::DecorationRole] = icon;
63 d->map[Qt::DisplayRole] = text;
64 d->type = type;
65 }
66
67 KToolTipItem::~KToolTipItem()
68 {
69 delete d;
70 }
71
72 int KToolTipItem::type() const
73 {
74 return d->type;
75 }
76
77 QString KToolTipItem::text() const
78 {
79 return data(Qt::DisplayRole).toString();
80 }
81
82 QIcon KToolTipItem::icon() const
83 {
84 return qvariant_cast<QIcon>(data(Qt::DecorationRole));
85 }
86
87 QVariant KToolTipItem::data(int role) const
88 {
89 return d->map.value(role);
90 }
91
92 void KToolTipItem::setData(int role, const QVariant &data)
93 {
94 d->map[role] = data;
95 KToolTipManager::instance()->update();
96 }
97
98
99
100 // ----------------------------------------------------------------------------
101
102
103 KStyleOptionToolTip::KStyleOptionToolTip()
104 : fontMetrics(QApplication::font())
105 {
106 }
107
108
109 // ----------------------------------------------------------------------------
110
111
112
113 KToolTipDelegate::KToolTipDelegate() : QObject()
114 {
115 }
116
117 KToolTipDelegate::~KToolTipDelegate()
118 {
119 }
120
121 QSize KToolTipDelegate::sizeHint(const KStyleOptionToolTip *option, const KToolTipItem *item) const
122 {
123 QSize size;
124 size.rwidth() = option->fontMetrics.width(item->text());
125 size.rheight() = option->fontMetrics.lineSpacing();
126
127 QIcon icon = item->icon();
128 if (!icon.isNull()) {
129 const QSize iconSize = icon.actualSize(option->decorationSize);
130 size.rwidth() += iconSize.width() + 4;
131 size.rheight() = qMax(size.height(), iconSize.height());
132 }
133
134 return size + QSize(20, 20);
135
136 }
137
138 void KToolTipDelegate::paint(QPainter *painter, const KStyleOptionToolTip *option,
139 const KToolTipItem *item) const
140 {
141 bool haveAlpha = haveAlphaChannel();
142 painter->setRenderHint(QPainter::Antialiasing);
143
144 QPainterPath path;
145 if (haveAlpha)
146 path.addRoundRect(option->rect.adjusted(0, 0, -1, -1), 25);
147 else
148 path.addRect(option->rect.adjusted(0, 0, -1, -1));
149
150 QColor color = option->palette.color(QPalette::ToolTipBase);
151 QColor from = color.lighter(105);
152 QColor to = color.darker(120);
153
154 QLinearGradient gradient(0, 0, 0, 1);
155 gradient.setCoordinateMode(QGradient::ObjectBoundingMode);
156 gradient.setColorAt(0, from);
157 gradient.setColorAt(1, to);
158
159 painter->translate(.5, .5);
160 painter->setPen(QPen(Qt::black, 1));
161 painter->setBrush(gradient);
162 painter->drawPath(path);
163 painter->translate(-.5, -.5);
164
165 if (haveAlpha) {
166 QLinearGradient mask(0, 0, 0, 1);
167 gradient.setCoordinateMode(QGradient::ObjectBoundingMode);
168 gradient.setColorAt(0, QColor(0, 0, 0, 192));
169 gradient.setColorAt(1, QColor(0, 0, 0, 72));
170 painter->setCompositionMode(QPainter::CompositionMode_DestinationIn);
171 painter->fillRect(option->rect, gradient);
172 painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
173 }
174
175 QRect textRect = option->rect.adjusted(10, 10, -10, -10);
176
177 QIcon icon = item->icon();
178 if (!icon.isNull()) {
179 const QSize iconSize = icon.actualSize(option->decorationSize);
180 painter->drawPixmap(textRect.topLeft(), icon.pixmap(iconSize));
181 textRect.adjust(iconSize.width() + 4, 0, 0, 0);
182 }
183 painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, item->text());
184 }
185
186 QRegion KToolTipDelegate::inputShape(const KStyleOptionToolTip *option) const
187 {
188 return QRegion(option->rect);
189 }
190
191 QRegion KToolTipDelegate::shapeMask(const KStyleOptionToolTip *option) const
192 {
193 return QRegion(option->rect);
194 }
195
196 bool KToolTipDelegate::haveAlphaChannel() const
197 {
198 #ifdef Q_WS_X11
199 return QX11Info::isCompositingManagerRunning();
200 #else
201 return false;
202 #endif
203 }
204
205
206
207 // ----------------------------------------------------------------------------
208
209
210
211 class KTipLabel : public QWidget
212 {
213 public:
214 KTipLabel();
215 void showTip(const QPoint &pos, const KToolTipItem *item);
216 void moveTip(const QPoint &pos);
217 void hideTip();
218
219 private:
220 void paintEvent(QPaintEvent*);
221 QSize sizeHint() const;
222 KStyleOptionToolTip styleOption() const;
223 KToolTipDelegate *delegate() const;
224
225 private:
226 const KToolTipItem *currentItem;
227 };
228
229 KTipLabel::KTipLabel() : QWidget(0, Qt::ToolTip)
230 {
231 #ifdef Q_WS_X11
232 if (QX11Info::isCompositingManagerRunning()) {
233 setAttribute(Qt::WA_TranslucentBackground);
234 }
235 #endif
236 }
237
238 void KTipLabel::showTip(const QPoint &pos, const KToolTipItem *item)
239 {
240 currentItem = item;
241 move(pos);
242 show();
243 }
244
245 void KTipLabel::hideTip()
246 {
247 hide();
248 currentItem = 0;
249 }
250
251 void KTipLabel::moveTip(const QPoint &pos)
252 {
253 move(pos);
254 }
255
256 void KTipLabel::paintEvent(QPaintEvent*)
257 {
258 KStyleOptionToolTip option = styleOption();
259 option.rect = rect();
260
261 #ifdef Q_WS_X11
262 if (QX11Info::isCompositingManagerRunning())
263 XShapeCombineRegion(x11Info().display(), winId(), ShapeInput, 0, 0,
264 delegate()->inputShape(&option).handle(), ShapeSet);
265 else
266 #endif
267 setMask(delegate()->shapeMask(&option));
268
269 QPainter p(this);
270 p.setFont(option.font);
271 p.setPen(QPen(option.palette.brush(QPalette::Text), 0));
272 delegate()->paint(&p, &option, currentItem);
273 }
274
275 QSize KTipLabel::sizeHint() const
276 {
277 if (!currentItem)
278 return QSize();
279
280 KStyleOptionToolTip option = styleOption();
281 return delegate()->sizeHint(&option, currentItem);
282 }
283
284 KStyleOptionToolTip KTipLabel::styleOption() const
285 {
286 KStyleOptionToolTip option;
287 KToolTipManager::instance()->initStyleOption(&option);
288 return option;
289 }
290
291 KToolTipDelegate *KTipLabel::delegate() const
292 {
293 return KToolTipManager::instance()->delegate();
294 }
295
296
297
298
299 // ----------------------------------------------------------------------------
300
301
302
303
304 KToolTipManager *KToolTipManager::s_instance = 0;
305
306 KToolTipManager::KToolTipManager()
307 : label(new KTipLabel), currentItem(0), m_delegate(0)
308 {
309 }
310
311 KToolTipManager::~KToolTipManager()
312 {
313 delete label;
314 delete currentItem;
315 }
316
317 void KToolTipManager::showTip(const QPoint &pos, KToolTipItem *item)
318 {
319 hideTip();
320 label->showTip(pos, item);
321 currentItem = item;
322 m_tooltipPos = pos;
323 }
324
325 void KToolTipManager::hideTip()
326 {
327 label->hideTip();
328 delete currentItem;
329 currentItem = 0;
330 }
331
332 void KToolTipManager::initStyleOption(KStyleOptionToolTip *option) const
333 {
334 option->direction = QApplication::layoutDirection();
335 option->fontMetrics = QFontMetrics(QToolTip::font());
336 option->activeCorner = KStyleOptionToolTip::TopLeftCorner;
337 option->palette = QToolTip::palette();
338 option->font = QToolTip::font();
339 option->rect = QRect();
340 option->state = QStyle::State_None;
341 option->decorationSize = QSize(32, 32);
342 }
343
344 void KToolTipManager::setDelegate(KToolTipDelegate *delegate)
345 {
346 m_delegate = delegate;
347 }
348
349 void KToolTipManager::update()
350 {
351 if (currentItem == 0)
352 return;
353 label->showTip(m_tooltipPos, currentItem);
354 }
355
356 KToolTipDelegate *KToolTipManager::delegate() const
357 {
358 return m_delegate;
359 }
360
361
362
363 // ----------------------------------------------------------------------------
364
365
366
367 namespace KToolTip
368 {
369 void showText(const QPoint &pos, const QString &text, QWidget *widget, const QRect &rect)
370 {
371 Q_UNUSED(widget)
372 Q_UNUSED(rect)
373 KToolTipItem *item = new KToolTipItem(text);
374 KToolTipManager::instance()->showTip(pos, item);
375 }
376
377 void showText(const QPoint &pos, const QString &text, QWidget *widget)
378 {
379 showText(pos, text, widget, QRect());
380 }
381
382 void showTip(const QPoint &pos, KToolTipItem *item)
383 {
384 KToolTipManager::instance()->showTip(pos, item);
385 }
386
387 void hideTip()
388 {
389 KToolTipManager::instance()->hideTip();
390 }
391
392 void setToolTipDelegate(KToolTipDelegate *delegate)
393 {
394 KToolTipManager::instance()->setDelegate(delegate);
395 }
396 }
397