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 ***************************************************************************/
22 #include <QApplication>
34 # include <X11/Xlib.h>
35 # include <X11/extensions/shape.h>
38 #include "ktooltip_p.h"
41 // compile with XShape older than 1.0
43 const int ShapeInput
= 2;
47 class KToolTipItemPrivate
50 QMap
<int, QVariant
> map
;
54 KToolTipItem::KToolTipItem(const QString
&text
, int type
)
55 : d(new KToolTipItemPrivate
)
57 d
->map
[Qt::DisplayRole
] = text
;
61 KToolTipItem::KToolTipItem(const QIcon
&icon
, const QString
&text
, int type
)
62 : d(new KToolTipItemPrivate
)
64 d
->map
[Qt::DecorationRole
] = icon
;
65 d
->map
[Qt::DisplayRole
] = text
;
69 KToolTipItem::~KToolTipItem()
74 int KToolTipItem::type() const
79 QString
KToolTipItem::text() const
81 return data(Qt::DisplayRole
).toString();
84 QIcon
KToolTipItem::icon() const
86 return qvariant_cast
<QIcon
>(data(Qt::DecorationRole
));
89 QVariant
KToolTipItem::data(int role
) const
91 return d
->map
.value(role
);
94 void KToolTipItem::setData(int role
, const QVariant
&data
)
97 KToolTipManager::instance()->update();
102 // ----------------------------------------------------------------------------
105 KStyleOptionToolTip::KStyleOptionToolTip()
106 : fontMetrics(QApplication::font())
111 // ----------------------------------------------------------------------------
115 KToolTipDelegate::KToolTipDelegate() : QObject()
119 KToolTipDelegate::~KToolTipDelegate()
123 QSize
KToolTipDelegate::sizeHint(const KStyleOptionToolTip
*option
, const KToolTipItem
*item
) const
126 size
.rwidth() = option
->fontMetrics
.width(item
->text());
127 size
.rheight() = option
->fontMetrics
.lineSpacing();
129 QIcon icon
= item
->icon();
130 if (!icon
.isNull()) {
131 const QSize iconSize
= icon
.actualSize(option
->decorationSize
);
132 size
.rwidth() += iconSize
.width() + 4;
133 size
.rheight() = qMax(size
.height(), iconSize
.height());
136 return size
+ QSize(20, 20);
140 void KToolTipDelegate::paint(QPainter
*painter
, const KStyleOptionToolTip
*option
,
141 const KToolTipItem
*item
) const
143 bool haveAlpha
= haveAlphaChannel();
144 painter
->setRenderHint(QPainter::Antialiasing
);
148 path
.addRoundRect(option
->rect
.adjusted(0, 0, -1, -1), 25);
150 path
.addRect(option
->rect
.adjusted(0, 0, -1, -1));
152 QColor color
= option
->palette
.color(QPalette::ToolTipBase
);
153 QColor from
= color
.lighter(105);
154 QColor to
= color
.darker(120);
156 QLinearGradient
gradient(0, 0, 0, 1);
157 gradient
.setCoordinateMode(QGradient::ObjectBoundingMode
);
158 gradient
.setColorAt(0, from
);
159 gradient
.setColorAt(1, to
);
161 painter
->translate(.5, .5);
162 painter
->setPen(QPen(Qt::black
, 1));
163 painter
->setBrush(gradient
);
164 painter
->drawPath(path
);
165 painter
->translate(-.5, -.5);
168 QLinearGradient
mask(0, 0, 0, 1);
169 gradient
.setCoordinateMode(QGradient::ObjectBoundingMode
);
170 gradient
.setColorAt(0, QColor(0, 0, 0, 192));
171 gradient
.setColorAt(1, QColor(0, 0, 0, 72));
172 painter
->setCompositionMode(QPainter::CompositionMode_DestinationIn
);
173 painter
->fillRect(option
->rect
, gradient
);
174 painter
->setCompositionMode(QPainter::CompositionMode_SourceOver
);
177 QRect textRect
= option
->rect
.adjusted(10, 10, -10, -10);
179 QIcon icon
= item
->icon();
180 if (!icon
.isNull()) {
181 const QSize iconSize
= icon
.actualSize(option
->decorationSize
);
182 painter
->drawPixmap(textRect
.topLeft(), icon
.pixmap(iconSize
));
183 textRect
.adjust(iconSize
.width() + 4, 0, 0, 0);
185 painter
->drawText(textRect
, Qt::AlignLeft
| Qt::AlignVCenter
, item
->text());
188 QRegion
KToolTipDelegate::inputShape(const KStyleOptionToolTip
*option
) const
190 return QRegion(option
->rect
);
193 QRegion
KToolTipDelegate::shapeMask(const KStyleOptionToolTip
*option
) const
195 return QRegion(option
->rect
);
198 bool KToolTipDelegate::haveAlphaChannel() const
200 return QX11Info::isCompositingManagerRunning();
205 // ----------------------------------------------------------------------------
209 class KTipLabel
: public QWidget
213 void showTip(const QPoint
&pos
, const KToolTipItem
*item
);
214 void moveTip(const QPoint
&pos
);
218 void paintEvent(QPaintEvent
*);
219 QSize
sizeHint() const;
220 KStyleOptionToolTip
styleOption() const;
221 KToolTipDelegate
*delegate() const;
224 const KToolTipItem
*currentItem
;
227 KTipLabel::KTipLabel() : QWidget(0, Qt::ToolTip
)
229 if (QX11Info::isCompositingManagerRunning()) {
230 setAttribute(Qt::WA_TranslucentBackground
);
234 void KTipLabel::showTip(const QPoint
&pos
, const KToolTipItem
*item
)
241 void KTipLabel::hideTip()
247 void KTipLabel::moveTip(const QPoint
&pos
)
252 void KTipLabel::paintEvent(QPaintEvent
*)
254 KStyleOptionToolTip option
= styleOption();
255 option
.rect
= rect();
257 if (QX11Info::isCompositingManagerRunning())
258 XShapeCombineRegion(x11Info().display(), winId(), ShapeInput
, 0, 0,
259 delegate()->inputShape(&option
).handle(), ShapeSet
);
261 setMask(delegate()->shapeMask(&option
));
264 p
.setFont(option
.font
);
265 p
.setPen(QPen(option
.palette
.brush(QPalette::Text
), 0));
266 delegate()->paint(&p
, &option
, currentItem
);
269 QSize
KTipLabel::sizeHint() const
274 KStyleOptionToolTip option
= styleOption();
275 return delegate()->sizeHint(&option
, currentItem
);
278 KStyleOptionToolTip
KTipLabel::styleOption() const
280 KStyleOptionToolTip option
;
281 KToolTipManager::instance()->initStyleOption(&option
);
285 KToolTipDelegate
*KTipLabel::delegate() const
287 return KToolTipManager::instance()->delegate();
293 // ----------------------------------------------------------------------------
298 KToolTipManager
*KToolTipManager::s_instance
= 0;
300 KToolTipManager::KToolTipManager()
301 : label(new KTipLabel
), currentItem(0), m_delegate(0)
305 KToolTipManager::~KToolTipManager()
311 void KToolTipManager::showTip(const QPoint
&pos
, KToolTipItem
*item
)
314 label
->showTip(pos
, item
);
319 void KToolTipManager::hideTip()
326 void KToolTipManager::initStyleOption(KStyleOptionToolTip
*option
) const
328 option
->direction
= QApplication::layoutDirection();
329 option
->fontMetrics
= QFontMetrics(QToolTip::font());
330 option
->activeCorner
= KStyleOptionToolTip::TopLeftCorner
;
331 option
->palette
= QToolTip::palette();
332 option
->font
= QToolTip::font();
333 option
->rect
= QRect();
334 option
->state
= QStyle::State_None
;
335 option
->decorationSize
= QSize(32, 32);
338 void KToolTipManager::setDelegate(KToolTipDelegate
*delegate
)
340 m_delegate
= delegate
;
343 void KToolTipManager::update()
345 if (currentItem
== 0)
347 label
->showTip(m_tooltipPos
, currentItem
);
350 KToolTipDelegate
*KToolTipManager::delegate() const
357 // ----------------------------------------------------------------------------
363 void showText(const QPoint
&pos
, const QString
&text
, QWidget
*widget
, const QRect
&rect
)
367 KToolTipItem
*item
= new KToolTipItem(text
);
368 KToolTipManager::instance()->showTip(pos
, item
);
371 void showText(const QPoint
&pos
, const QString
&text
, QWidget
*widget
)
373 showText(pos
, text
, widget
, QRect());
376 void showTip(const QPoint
&pos
, KToolTipItem
*item
)
378 KToolTipManager::instance()->showTip(pos
, item
);
383 KToolTipManager::instance()->hideTip();
386 void setToolTipDelegate(KToolTipDelegate
*delegate
)
388 KToolTipManager::instance()->setDelegate(delegate
);