]> cloud.milkyroute.net Git - dolphin.git/blob - src/tooltips/kformattedballoontipdelegate.cpp
Fixed regression when refactoring the Information Panel: Don't forget to give a visua...
[dolphin.git] / src / tooltips / 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 "ktooltipitem.h"
23 #include "ktooltip.h"
24 #include <QBitmap>
25 #include <QIcon>
26 #include <QLinearGradient>
27 #include <QTextDocument>
28 #include <kcolorscheme.h>
29
30 KFormattedBalloonTipDelegate::KFormattedBalloonTipDelegate()
31 {
32 }
33
34 KFormattedBalloonTipDelegate::~KFormattedBalloonTipDelegate()
35 {
36 }
37
38 QSize KFormattedBalloonTipDelegate::sizeHint(const KStyleOptionToolTip &option, const KToolTipItem &item) const
39 {
40 QTextDocument doc;
41 doc.setHtml(item.text());
42 const QIcon icon = item.icon();
43
44 const QSize iconSize = icon.isNull() ? QSize(0, 0) : icon.actualSize(option.decorationSize);
45 const QSize docSize = doc.size().toSize();
46 QSize contentSize = iconSize + docSize;
47
48 // assure that the content height is large enough for the icon and the document
49 contentSize.setHeight(iconSize.height() > doc.size().height() ? iconSize.height() : doc.size().height());
50 return contentSize + QSize(Border * 3, Border * 2);
51 }
52
53 void KFormattedBalloonTipDelegate::paint(QPainter *painter,
54 const KStyleOptionToolTip &option,
55 const KToolTipItem &item) const
56 {
57 QColor toColor = option.palette.brush(QPalette::ToolTipBase).color();
58 QColor fromColor = KColorScheme::shade(toColor, KColorScheme::LightShade, 0.2);
59
60 QPainterPath path = createPath(option);
61 if (haveAlphaChannel()) {
62 painter->setRenderHint(QPainter::Antialiasing);
63 painter->translate(.5, .5);
64 toColor.setAlpha(220);
65 fromColor.setAlpha(220);
66 }
67
68 QLinearGradient gradient(option.rect.topLeft(), option.rect.bottomLeft());
69 gradient.setColorAt(0.0, fromColor);
70 gradient.setColorAt(1.0, toColor);
71 painter->setPen(Qt::NoPen);
72 painter->setBrush(gradient);
73
74 painter->drawPath(path);
75
76 const QIcon icon = item.icon();
77 int x = Border;
78 const int y = Border;
79 if (!icon.isNull()) {
80 const QSize iconSize = icon.actualSize(option.decorationSize);
81 const QPoint pos(x + option.rect.x(), y + option.rect.y());
82 painter->drawPixmap(pos, icon.pixmap(iconSize));
83 x += iconSize.width() + Border;
84 }
85
86 QTextDocument doc;
87 doc.setHtml(item.text());
88 QPixmap bitmap(doc.size().toSize());
89 bitmap.fill(Qt::transparent);
90 QPainter p(&bitmap);
91 doc.drawContents(&p);
92
93 const QRect docRect(QPoint(x, y), doc.size().toSize());
94 painter->drawPixmap(docRect, bitmap);
95 }
96
97 QRegion KFormattedBalloonTipDelegate::inputShape(const KStyleOptionToolTip &option) const
98 {
99 QBitmap bitmap(option.rect.size());
100 bitmap.fill(Qt::color0);
101
102 QPainter p(&bitmap);
103 p.setPen(QPen(Qt::color1, 1));
104 p.setBrush(Qt::color1);
105 p.drawPath(createPath(option));
106
107 return QRegion(bitmap);
108 }
109
110 QRegion KFormattedBalloonTipDelegate::shapeMask(const KStyleOptionToolTip &option) const
111 {
112 return inputShape(option);
113 }
114
115 static inline void arc(QPainterPath &path, qreal cx, qreal cy, qreal radius, qreal angle, qreal sweeplength)
116 {
117 path.arcTo(cx-radius, cy-radius, radius * 2, radius * 2, angle, sweeplength);
118 }
119
120 QPainterPath KFormattedBalloonTipDelegate::createPath(const KStyleOptionToolTip& option) const
121 {
122 const QRect rect = option.rect;
123 const qreal radius = 5;
124
125 QPainterPath path;
126 path.moveTo(rect.left(), rect.top() + radius);
127 arc(path, rect.left() + radius, rect.top() + radius, radius, 180, -90);
128 arc(path, rect.right() - radius, rect.top() + radius, radius, 90, -90);
129 arc(path, rect.right() - radius, rect.bottom() - radius, radius, 0, -90);
130 arc(path, rect.left() + radius, rect.bottom() - radius, radius, 270, -90);
131 path.closeSubpath();
132
133 return path;
134 }