]> cloud.milkyroute.net Git - dolphin.git/blob - src/kformattedballoontipdelegate.cpp
improved look of tooltips
[dolphin.git] / src / kformattedballoontipdelegate.cpp
1 /*******************************************************************************
2 * Copyright (C) 2008 by Fredrik Höglund <fredrik@kde.org> *
3 * Copyright (C) 2008 by Konstantin Heil <konst.heil@stud.uni-heidelberg.de> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 *******************************************************************************/
20
21 #include "kformattedballoontipdelegate.h"
22 #include <QBitmap>
23 #include <QLinearGradient>
24 #include <QTextDocument>
25 #include <kcolorscheme.h>
26 #include <kdebug.h>
27
28 QSize KFormattedBalloonTipDelegate::sizeHint(const KStyleOptionToolTip *option, const KToolTipItem *item) const
29 {
30 QTextDocument doc;
31 doc.setHtml(item->text());
32 QIcon icon = item->icon();
33
34 QSize iconSize = (icon.isNull()) ? QSize(0,0) : icon.actualSize(option->decorationSize);
35 QSize docSize = doc.size().toSize();
36 QSize contentSize = iconSize + docSize;
37
38 // Re-adjust contentSize's height so that it is the maximum of the icon
39 // and doc sizes.
40 contentSize.setHeight( iconSize.height() > doc.size().height() ? iconSize.height() : doc.size().height());
41 return contentSize + QSize(20+5,20+1);
42 }
43
44 void KFormattedBalloonTipDelegate::paint(QPainter *painter, const KStyleOptionToolTip *option, const KToolTipItem *item) const
45 {
46 QRect contents;
47 QPainterPath path = createPath(option, &contents);
48 if (haveAlphaChannel()) {
49 painter->setRenderHint(QPainter::Antialiasing);
50 painter->translate(.5, .5);
51 }
52
53 #if QT_VERSION >= 0x040400
54 const QColor toColor = option->palette.brush(QPalette::ToolTipBase).color();
55 #else
56 const QColor toColor = option->palette.brush(QPalette::Base).color();
57 #endif
58 const QColor fromColor = KColorScheme::shade(toColor, KColorScheme::LightShade, 0.2);
59
60 QLinearGradient gradient(option->rect.topLeft(), option->rect.bottomLeft());
61 gradient.setColorAt(0.0, fromColor);
62 gradient.setColorAt(1.0, toColor);
63 painter->setPen(Qt::NoPen);
64 painter->setBrush(gradient);
65
66 painter->drawPath(path);
67
68 QIcon icon = item->icon();
69 if (!icon.isNull()) {
70 const QSize iconSize = icon.actualSize(option->decorationSize);
71 painter->drawPixmap(contents.topLeft(), icon.pixmap(iconSize));
72 contents.adjust(iconSize.width() + 4, 0, 0, 0);
73 }
74
75 QTextDocument doc;
76 doc.setHtml(item->text());
77 QPixmap bitmap(doc.size().toSize());
78 bitmap.fill(Qt::transparent);
79 QPainter p(&bitmap);
80 doc.drawContents(&p);
81
82 // Ensure doc text is not stretched when icon is larger.
83 contents.setSize(doc.size().toSize());
84
85 painter->drawPixmap(contents, bitmap);
86 }
87
88 QRegion KFormattedBalloonTipDelegate::inputShape(const KStyleOptionToolTip *option) const
89 {
90 QBitmap bitmap(option->rect.size()+QSize(20,20));
91 bitmap.fill(Qt::color0);
92
93 QPainter p(&bitmap);
94 p.setPen(QPen(Qt::color1, 1));
95 p.setBrush(Qt::color1);
96 p.drawPath(createPath(option, 0));
97
98 return QRegion(bitmap);
99 }
100
101 QRegion KFormattedBalloonTipDelegate::shapeMask(const KStyleOptionToolTip *option) const
102 {
103 return inputShape(option);
104 }
105
106 static inline void arc(QPainterPath &path, qreal cx, qreal cy, qreal radius, qreal angle, qreal sweeplength)
107 {
108 path.arcTo(cx-radius, cy-radius, radius * 2, radius * 2, angle, sweeplength);
109 }
110
111 QPainterPath KFormattedBalloonTipDelegate::createPath(const KStyleOptionToolTip *option, QRect *contents) const
112 {
113 QPainterPath path;
114 QRect rect = option->rect.adjusted(0, 0, -1, -1);
115 const qreal radius = 5;
116
117 path.moveTo(rect.left(), rect.top() + radius);
118 arc(path, rect.left() + radius, rect.top() + radius, radius, 180, -90);
119 arc(path, rect.right() - radius, rect.top() + radius, radius, 90, -90);
120 arc(path, rect.right() - radius, rect.bottom() - radius, radius, 0, -90);
121 arc(path, rect.left() + radius, rect.bottom() - radius, radius, 270, -90);
122 path.closeSubpath();
123
124 if (contents)
125 *contents = rect.adjusted(radius, radius, -radius, -radius);
126
127 return path;
128 }