]> cloud.milkyroute.net Git - dolphin.git/blob - src/tooltips/ktooltipdelegate.cpp
- The selection changed timer only needs to be created for a DolphinView instance.
[dolphin.git] / src / tooltips / ktooltipdelegate.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 "ktooltipdelegate.h"
21
22 #include "ktooltip.h"
23
24 #include <QApplication>
25 #include <QPainter>
26 #include <QIcon>
27
28 #ifdef Q_WS_X11
29 # include <QX11Info>
30 # include <X11/Xlib.h>
31 # include <X11/extensions/shape.h>
32 #endif
33
34
35 // ----------------------------------------------------------------------------
36
37
38 KStyleOptionToolTip::KStyleOptionToolTip()
39 : fontMetrics(QApplication::font())
40 {
41 }
42
43
44 // ----------------------------------------------------------------------------
45
46
47 KToolTipDelegate::KToolTipDelegate() : QObject()
48 {
49 }
50
51 KToolTipDelegate::~KToolTipDelegate()
52 {
53 }
54
55 QSize KToolTipDelegate::sizeHint(const KStyleOptionToolTip &option, const KToolTipItem &item) const
56 {
57 QSize size;
58 size.rwidth() = option.fontMetrics.width(item.text());
59 size.rheight() = option.fontMetrics.lineSpacing();
60
61 QIcon icon = item.icon();
62 if (!icon.isNull()) {
63 const QSize iconSize = icon.actualSize(option.decorationSize);
64 size.rwidth() += iconSize.width() + 4;
65 size.rheight() = qMax(size.height(), iconSize.height());
66 }
67
68 return size + QSize(20, 20);
69
70 }
71
72 void KToolTipDelegate::paint(QPainter *painter,
73 const KStyleOptionToolTip &option,
74 const KToolTipItem &item) const
75 {
76 bool haveAlpha = haveAlphaChannel();
77 painter->setRenderHint(QPainter::Antialiasing);
78
79 QPainterPath path;
80 if (haveAlpha)
81 path.addRoundRect(option.rect.adjusted(0, 0, -1, -1), 25);
82 else
83 path.addRect(option.rect.adjusted(0, 0, -1, -1));
84
85 QColor color = option.palette.color(QPalette::ToolTipBase);
86 QColor from = color.lighter(105);
87 QColor to = color.darker(120);
88
89 QLinearGradient gradient(0, 0, 0, 1);
90 gradient.setCoordinateMode(QGradient::ObjectBoundingMode);
91 gradient.setColorAt(0, from);
92 gradient.setColorAt(1, to);
93
94 painter->translate(.5, .5);
95 painter->setPen(QPen(Qt::black, 1));
96 painter->setBrush(gradient);
97 painter->drawPath(path);
98 painter->translate(-.5, -.5);
99
100 if (haveAlpha) {
101 QLinearGradient mask(0, 0, 0, 1);
102 gradient.setCoordinateMode(QGradient::ObjectBoundingMode);
103 gradient.setColorAt(0, QColor(0, 0, 0, 192));
104 gradient.setColorAt(1, QColor(0, 0, 0, 72));
105 painter->setCompositionMode(QPainter::CompositionMode_DestinationIn);
106 painter->fillRect(option.rect, gradient);
107 painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
108 }
109
110 QRect textRect = option.rect.adjusted(10, 10, -10, -10);
111
112 QIcon icon = item.icon();
113 if (!icon.isNull()) {
114 const QSize iconSize = icon.actualSize(option.decorationSize);
115 painter->drawPixmap(textRect.topLeft(), icon.pixmap(iconSize));
116 textRect.adjust(iconSize.width() + 4, 0, 0, 0);
117 }
118 painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, item.text());
119 }
120
121 QRegion KToolTipDelegate::inputShape(const KStyleOptionToolTip &option) const
122 {
123 return QRegion(option.rect);
124 }
125
126 QRegion KToolTipDelegate::shapeMask(const KStyleOptionToolTip &option) const
127 {
128 return QRegion(option.rect);
129 }
130
131 bool KToolTipDelegate::haveAlphaChannel() const
132 {
133 #ifdef Q_WS_X11
134 return QX11Info::isCompositingManagerRunning();
135 #else
136 return false;
137 #endif
138 }