]> cloud.milkyroute.net Git - dolphin.git/blob - src/kformattedballoontipdelegate.cpp
consider the protocol and directory capabilities for file actions like Rename, Delete...
[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 <QTextDocument>
24
25 QSize KFormattedBalloonTipDelegate::sizeHint(const KStyleOptionToolTip *option, const KToolTipItem *item) const
26 {
27 QTextDocument doc;
28 doc.setHtml(item->text());
29 QIcon icon = item->icon();
30 QSize is = (icon.isNull()) ? QSize(0,0) : QSize(icon.actualSize(option->decorationSize).width(),0);
31 return doc.size().toSize()+is+QSize(20+5,20+1);
32 }
33
34 void KFormattedBalloonTipDelegate::paint(QPainter *painter, const KStyleOptionToolTip *option, const KToolTipItem *item) const
35 {
36 QRect contents;
37 QPainterPath path = createPath(option, &contents);
38 bool alpha = haveAlphaChannel();
39
40 if (alpha) {
41 painter->setRenderHint(QPainter::Antialiasing);
42 painter->translate(.5, .5);
43 }
44
45 #if QT_VERSION >= 0x040400
46 painter->setBrush(option->palette.brush(QPalette::ToolTipBase));
47 #else
48 painter->setBrush(option->palette.brush(QPalette::Base));
49 #endif
50 painter->drawPath(path);
51
52 QIcon icon = item->icon();
53 if (!icon.isNull()) {
54 const QSize iconSize = icon.actualSize(option->decorationSize);
55 painter->drawPixmap(contents.topLeft(), icon.pixmap(iconSize));
56 contents.adjust(iconSize.width() + 4, 0, 0, 0);
57 }
58
59 QTextDocument doc;
60 doc.setHtml(item->text());
61 QPixmap bitmap(doc.size().toSize());
62 bitmap.fill(Qt::transparent);
63 QPainter p(&bitmap);
64 doc.drawContents(&p);
65
66 painter->drawPixmap(contents, bitmap);
67 }
68
69 QRegion KFormattedBalloonTipDelegate::inputShape(const KStyleOptionToolTip *option) const
70 {
71 QBitmap bitmap(option->rect.size()+QSize(20,20));
72 bitmap.fill(Qt::color0);
73
74 QPainter p(&bitmap);
75 p.setPen(QPen(Qt::color1, 1));
76 p.setBrush(Qt::color1);
77 p.drawPath(createPath(option, 0));
78
79 return QRegion(bitmap);
80 }
81
82 QRegion KFormattedBalloonTipDelegate::shapeMask(const KStyleOptionToolTip *option) const
83 {
84 return inputShape(option);
85 }
86
87 static inline void arc(QPainterPath &path, qreal cx, qreal cy, qreal radius, qreal angle, qreal sweeplength)
88 {
89 path.arcTo(cx-radius, cy-radius, radius * 2, radius * 2, angle, sweeplength);
90 }
91
92 QPainterPath KFormattedBalloonTipDelegate::createPath(const KStyleOptionToolTip *option, QRect *contents) const
93 {
94 QPainterPath path;
95 QRect rect = option->rect.adjusted(0, 0, -1, -1);
96 qreal radius = 10;
97
98 path.moveTo(rect.left(), rect.top() + radius);
99 arc(path, rect.left() + radius, rect.top() + radius, radius, 180, -90);
100 arc(path, rect.right() - radius, rect.top() + radius, radius, 90, -90);
101 arc(path, rect.right() - radius, rect.bottom() - radius, radius, 0, -90);
102 arc(path, rect.left() + radius, rect.bottom() - radius, radius, 270, -90);
103 path.closeSubpath();
104
105 if (contents)
106 *contents = rect.adjusted(10, 10, -10, -10);
107
108 return path;
109 }