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