1 /***************************************************************************
2 * Copyright (C) 2008 by Fredrik Höglund <fredrik@kde.org> *
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. *
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. *
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 ***************************************************************************/
20 #include "ktooltipdelegate.h"
24 #include <QApplication>
30 # include <X11/Xlib.h>
31 # include <X11/extensions/shape.h>
35 // ----------------------------------------------------------------------------
38 KStyleOptionToolTip::KStyleOptionToolTip()
39 : fontMetrics(QApplication::font())
44 // ----------------------------------------------------------------------------
47 KToolTipDelegate::KToolTipDelegate() : QObject()
51 KToolTipDelegate::~KToolTipDelegate()
55 QSize
KToolTipDelegate::sizeHint(const KStyleOptionToolTip
&option
, const KToolTipItem
&item
) const
58 size
.rwidth() = option
.fontMetrics
.width(item
.text());
59 size
.rheight() = option
.fontMetrics
.lineSpacing();
61 QIcon icon
= item
.icon();
63 const QSize iconSize
= icon
.actualSize(option
.decorationSize
);
64 size
.rwidth() += iconSize
.width() + 4;
65 size
.rheight() = qMax(size
.height(), iconSize
.height());
68 return size
+ QSize(20, 20);
72 void KToolTipDelegate::paint(QPainter
*painter
,
73 const KStyleOptionToolTip
&option
,
74 const KToolTipItem
&item
) const
76 bool haveAlpha
= haveAlphaChannel();
77 painter
->setRenderHint(QPainter::Antialiasing
);
81 path
.addRoundRect(option
.rect
.adjusted(0, 0, -1, -1), 25);
83 path
.addRect(option
.rect
.adjusted(0, 0, -1, -1));
85 QColor color
= option
.palette
.color(QPalette::ToolTipBase
);
86 QColor from
= color
.lighter(105);
87 QColor to
= color
.darker(120);
89 QLinearGradient
gradient(0, 0, 0, 1);
90 gradient
.setCoordinateMode(QGradient::ObjectBoundingMode
);
91 gradient
.setColorAt(0, from
);
92 gradient
.setColorAt(1, to
);
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);
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
);
110 QRect textRect
= option
.rect
.adjusted(10, 10, -10, -10);
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);
118 painter
->drawText(textRect
, Qt::AlignLeft
| Qt::AlignVCenter
, item
.text());
121 QRegion
KToolTipDelegate::inputShape(const KStyleOptionToolTip
&option
) const
123 return QRegion(option
.rect
);
126 QRegion
KToolTipDelegate::shapeMask(const KStyleOptionToolTip
&option
) const
128 return QRegion(option
.rect
);
131 bool KToolTipDelegate::haveAlphaChannel() const
134 return QX11Info::isCompositingManagerRunning();