]> cloud.milkyroute.net Git - dolphin.git/blob - src/kballoontipdelegate.cpp
don't allow to trash files which are inside trash:/ already
[dolphin.git] / src / kballoontipdelegate.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 "kballoontipdelegate.h"
21 #include <QPainterPath>
22 #include <QPainter>
23 #include <QBitmap>
24
25 QSize KBalloonTipDelegate::sizeHint(const KStyleOptionToolTip *option, const KToolTipItem *item) const
26 {
27 QSize size;
28 size.rwidth() = option->fontMetrics.width(item->text());
29 size.rheight() = option->fontMetrics.lineSpacing();
30
31 QIcon icon = item->icon();
32 if (!icon.isNull()) {
33 const QSize iconSize = icon.actualSize(option->decorationSize);
34 size.rwidth() += iconSize.width() + 4;
35 size.rheight() = qMax(size.height(), iconSize.height());
36 }
37
38 int margin = 2 * 10 + (option->activeCorner != KStyleOptionToolTip::NoCorner ? 10 : 0);
39 return size + QSize(margin, margin);
40 }
41
42 static inline void arc(QPainterPath &path, qreal cx, qreal cy, qreal radius, qreal angle, qreal sweeplength)
43 {
44 path.arcTo(cx-radius, cy-radius, radius * 2, radius * 2, angle, sweeplength);
45 }
46
47 QPainterPath KBalloonTipDelegate::createPath(const KStyleOptionToolTip *option, QRect *contents) const
48 {
49 QPainterPath path;
50 QRect rect = option->rect.adjusted(0, 0, -1, -1);
51 qreal radius = 10;
52
53 switch (option->activeCorner)
54 {
55 case KStyleOptionToolTip::TopLeftCorner:
56 rect.adjust(10, 10, 0, 0);
57 path.moveTo(option->rect.topLeft());
58 path.lineTo(rect.left() + radius, rect.top());
59 arc(path, rect.right() - radius, rect.top() + radius, radius, 90, -90);
60 arc(path, rect.right() - radius, rect.bottom() - radius, radius, 0, -90);
61 arc(path, rect.left() + radius, rect.bottom() - radius, radius, 270, -90);
62 path.lineTo(rect.left(), rect.top() + radius);
63 path.closeSubpath();
64 break;
65
66 case KStyleOptionToolTip::TopRightCorner:
67 rect.adjust(0, 10, -10, 0);
68 path.moveTo(option->rect.topRight());
69 path.lineTo(rect.right(), rect.top() + radius);
70 arc(path, rect.right() - radius, rect.bottom() - radius, radius, 0, -90);
71 arc(path, rect.left() + radius, rect.bottom() - radius, radius, 270, -90);
72 arc(path, rect.left() + radius, rect.top() + radius, radius, 180, -90);
73 path.lineTo(rect.right() - radius, rect.top());
74 path.closeSubpath();
75 break;
76
77 case KStyleOptionToolTip::BottomLeftCorner:
78 rect.adjust(10, 0, 0, -10);
79 path.moveTo(option->rect.bottomLeft());
80 path.lineTo(rect.left(), rect.bottom() - radius);
81 arc(path, rect.left() + radius, rect.top() + radius, radius, 180, -90);
82 arc(path, rect.right() - radius, rect.top() + radius, radius, 90, -90);
83 arc(path, rect.right() - radius, rect.bottom() - radius, radius, 0, -90);
84 path.lineTo(rect.left() + radius, rect.bottom());
85 path.closeSubpath();
86 break;
87
88 case KStyleOptionToolTip::BottomRightCorner:
89 rect.adjust(0, 0, -10, -10);
90 path.moveTo(option->rect.bottomRight());
91 path.lineTo(rect.right() - radius, rect.bottom());
92 arc(path, rect.left() + radius, rect.bottom() - radius, radius, 270, -90);
93 arc(path, rect.left() + radius, rect.top() + radius, radius, 180, -90);
94 arc(path, rect.right() - radius, rect.top() + radius, radius, 90, -90);
95 path.lineTo(rect.right(), rect.bottom() - radius);
96 path.closeSubpath();
97 break;
98
99 default:
100 path.moveTo(rect.left(), rect.top() + radius);
101 arc(path, rect.left() + radius, rect.top() + radius, radius, 180, -90);
102 arc(path, rect.right() - radius, rect.top() + radius, radius, 90, -90);
103 arc(path, rect.right() - radius, rect.bottom() - radius, radius, 0, -90);
104 arc(path, rect.left() + radius, rect.bottom() - radius, radius, 270, -90);
105 path.closeSubpath();
106 break;
107 }
108
109 if (contents)
110 *contents = rect.adjusted(10, 10, -10, -10);
111
112 return path;
113 }
114
115 void KBalloonTipDelegate::paint(QPainter *painter, const KStyleOptionToolTip *option, const KToolTipItem *item) const
116 {
117 QRect contents;
118 QPainterPath path = createPath(option, &contents);
119 bool alpha = haveAlphaChannel();
120
121 if (alpha) {
122 painter->setRenderHint(QPainter::Antialiasing);
123 painter->translate(.5, .5);
124 }
125
126 #if QT_VERSION >= 0x040400
127 painter->setBrush(option->palette.brush(QPalette::ToolTipBase));
128 #else
129 painter->setBrush(option->palette.brush(QPalette::Base));
130 #endif
131 painter->drawPath(path);
132
133 if (alpha)
134 painter->translate(-.5, -.5);
135
136 QIcon icon = item->icon();
137 if (!icon.isNull()) {
138 const QSize iconSize = icon.actualSize(option->decorationSize);
139 painter->drawPixmap(contents.topLeft(), icon.pixmap(iconSize));
140 contents.adjust(iconSize.width() + 4, 0, 0, 0);
141 }
142
143 painter->drawText(contents, Qt::AlignLeft | Qt::AlignVCenter, item->text());
144 }
145
146
147 QRegion KBalloonTipDelegate::inputShape(const KStyleOptionToolTip *option) const
148 {
149 QBitmap bitmap(option->rect.size());
150 bitmap.fill(Qt::color0);
151
152 QPainter p(&bitmap);
153 p.setPen(QPen(Qt::color1, 1));
154 p.setBrush(Qt::color1);
155 p.drawPath(createPath(option));
156
157 return QRegion(bitmap);
158 }
159
160 QRegion KBalloonTipDelegate::shapeMask(const KStyleOptionToolTip *option) const
161 {
162 return inputShape(option);
163 }
164