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 ***************************************************************************/
21 #include "ktooltip_p.h"
23 #include <QApplication>
35 # include <X11/Xlib.h>
36 # include <X11/extensions/shape.h>
39 // compile with XShape older than 1.0
41 const int ShapeInput
= 2;
45 class KToolTipItemPrivate
48 QMap
<int, QVariant
> map
;
52 KToolTipItem::KToolTipItem(const QString
&text
, int type
)
53 : d(new KToolTipItemPrivate
)
55 d
->map
[Qt::DisplayRole
] = text
;
59 KToolTipItem::KToolTipItem(const QIcon
&icon
, const QString
&text
, int type
)
60 : d(new KToolTipItemPrivate
)
62 d
->map
[Qt::DecorationRole
] = icon
;
63 d
->map
[Qt::DisplayRole
] = text
;
67 KToolTipItem::~KToolTipItem()
72 int KToolTipItem::type() const
77 QString
KToolTipItem::text() const
79 return data(Qt::DisplayRole
).toString();
82 QIcon
KToolTipItem::icon() const
84 return qvariant_cast
<QIcon
>(data(Qt::DecorationRole
));
87 QVariant
KToolTipItem::data(int role
) const
89 return d
->map
.value(role
);
92 void KToolTipItem::setData(int role
, const QVariant
&data
)
95 KToolTipManager::instance()->update();
100 // ----------------------------------------------------------------------------
103 KStyleOptionToolTip::KStyleOptionToolTip()
104 : fontMetrics(QApplication::font())
109 // ----------------------------------------------------------------------------
113 KToolTipDelegate::KToolTipDelegate() : QObject()
117 KToolTipDelegate::~KToolTipDelegate()
121 QSize
KToolTipDelegate::sizeHint(const KStyleOptionToolTip
*option
, const KToolTipItem
*item
) const
124 size
.rwidth() = option
->fontMetrics
.width(item
->text());
125 size
.rheight() = option
->fontMetrics
.lineSpacing();
127 QIcon icon
= item
->icon();
128 if (!icon
.isNull()) {
129 const QSize iconSize
= icon
.actualSize(option
->decorationSize
);
130 size
.rwidth() += iconSize
.width() + 4;
131 size
.rheight() = qMax(size
.height(), iconSize
.height());
134 return size
+ QSize(20, 20);
138 void KToolTipDelegate::paint(QPainter
*painter
, const KStyleOptionToolTip
*option
,
139 const KToolTipItem
*item
) const
141 bool haveAlpha
= haveAlphaChannel();
142 painter
->setRenderHint(QPainter::Antialiasing
);
146 path
.addRoundRect(option
->rect
.adjusted(0, 0, -1, -1), 25);
148 path
.addRect(option
->rect
.adjusted(0, 0, -1, -1));
150 QColor color
= option
->palette
.color(QPalette::ToolTipBase
);
151 QColor from
= color
.lighter(105);
152 QColor to
= color
.darker(120);
154 QLinearGradient
gradient(0, 0, 0, 1);
155 gradient
.setCoordinateMode(QGradient::ObjectBoundingMode
);
156 gradient
.setColorAt(0, from
);
157 gradient
.setColorAt(1, to
);
159 painter
->translate(.5, .5);
160 painter
->setPen(QPen(Qt::black
, 1));
161 painter
->setBrush(gradient
);
162 painter
->drawPath(path
);
163 painter
->translate(-.5, -.5);
166 QLinearGradient
mask(0, 0, 0, 1);
167 gradient
.setCoordinateMode(QGradient::ObjectBoundingMode
);
168 gradient
.setColorAt(0, QColor(0, 0, 0, 192));
169 gradient
.setColorAt(1, QColor(0, 0, 0, 72));
170 painter
->setCompositionMode(QPainter::CompositionMode_DestinationIn
);
171 painter
->fillRect(option
->rect
, gradient
);
172 painter
->setCompositionMode(QPainter::CompositionMode_SourceOver
);
175 QRect textRect
= option
->rect
.adjusted(10, 10, -10, -10);
177 QIcon icon
= item
->icon();
178 if (!icon
.isNull()) {
179 const QSize iconSize
= icon
.actualSize(option
->decorationSize
);
180 painter
->drawPixmap(textRect
.topLeft(), icon
.pixmap(iconSize
));
181 textRect
.adjust(iconSize
.width() + 4, 0, 0, 0);
183 painter
->drawText(textRect
, Qt::AlignLeft
| Qt::AlignVCenter
, item
->text());
186 QRegion
KToolTipDelegate::inputShape(const KStyleOptionToolTip
*option
) const
188 return QRegion(option
->rect
);
191 QRegion
KToolTipDelegate::shapeMask(const KStyleOptionToolTip
*option
) const
193 return QRegion(option
->rect
);
196 bool KToolTipDelegate::haveAlphaChannel() const
199 return QX11Info::isCompositingManagerRunning();
207 // ----------------------------------------------------------------------------
211 class KTipLabel
: public QWidget
215 void showTip(const QPoint
&pos
, const KToolTipItem
*item
);
216 void moveTip(const QPoint
&pos
);
220 void paintEvent(QPaintEvent
*);
221 QSize
sizeHint() const;
222 KStyleOptionToolTip
styleOption() const;
223 KToolTipDelegate
*delegate() const;
226 const KToolTipItem
*currentItem
;
229 KTipLabel::KTipLabel() : QWidget(0, Qt::ToolTip
)
232 if (QX11Info::isCompositingManagerRunning()) {
233 setAttribute(Qt::WA_TranslucentBackground
);
238 void KTipLabel::showTip(const QPoint
&pos
, const KToolTipItem
*item
)
245 void KTipLabel::hideTip()
251 void KTipLabel::moveTip(const QPoint
&pos
)
256 void KTipLabel::paintEvent(QPaintEvent
*)
258 KStyleOptionToolTip option
= styleOption();
259 option
.rect
= rect();
262 if (QX11Info::isCompositingManagerRunning())
263 XShapeCombineRegion(x11Info().display(), winId(), ShapeInput
, 0, 0,
264 delegate()->inputShape(&option
).handle(), ShapeSet
);
267 setMask(delegate()->shapeMask(&option
));
270 p
.setFont(option
.font
);
271 p
.setPen(QPen(option
.palette
.brush(QPalette::Text
), 0));
272 delegate()->paint(&p
, &option
, currentItem
);
275 QSize
KTipLabel::sizeHint() const
280 KStyleOptionToolTip option
= styleOption();
281 return delegate()->sizeHint(&option
, currentItem
);
284 KStyleOptionToolTip
KTipLabel::styleOption() const
286 KStyleOptionToolTip option
;
287 KToolTipManager::instance()->initStyleOption(&option
);
291 KToolTipDelegate
*KTipLabel::delegate() const
293 return KToolTipManager::instance()->delegate();
299 // ----------------------------------------------------------------------------
304 KToolTipManager
*KToolTipManager::s_instance
= 0;
306 KToolTipManager::KToolTipManager()
307 : label(new KTipLabel
), currentItem(0), m_delegate(0)
311 KToolTipManager::~KToolTipManager()
317 void KToolTipManager::showTip(const QPoint
&pos
, KToolTipItem
*item
)
320 label
->showTip(pos
, item
);
325 void KToolTipManager::hideTip()
332 void KToolTipManager::initStyleOption(KStyleOptionToolTip
*option
) const
334 option
->direction
= QApplication::layoutDirection();
335 option
->fontMetrics
= QFontMetrics(QToolTip::font());
336 option
->activeCorner
= KStyleOptionToolTip::TopLeftCorner
;
337 option
->palette
= QToolTip::palette();
338 option
->font
= QToolTip::font();
339 option
->rect
= QRect();
340 option
->state
= QStyle::State_None
;
341 option
->decorationSize
= QSize(32, 32);
344 void KToolTipManager::setDelegate(KToolTipDelegate
*delegate
)
346 m_delegate
= delegate
;
349 void KToolTipManager::update()
351 if (currentItem
== 0)
353 label
->showTip(m_tooltipPos
, currentItem
);
356 KToolTipDelegate
*KToolTipManager::delegate() const
363 // ----------------------------------------------------------------------------
369 void showText(const QPoint
&pos
, const QString
&text
, QWidget
*widget
, const QRect
&rect
)
373 KToolTipItem
*item
= new KToolTipItem(text
);
374 KToolTipManager::instance()->showTip(pos
, item
);
377 void showText(const QPoint
&pos
, const QString
&text
, QWidget
*widget
)
379 showText(pos
, text
, widget
, QRect());
382 void showTip(const QPoint
&pos
, KToolTipItem
*item
)
384 KToolTipManager::instance()->showTip(pos
, item
);
389 KToolTipManager::instance()->hideTip();
392 void setToolTipDelegate(KToolTipDelegate
*delegate
)
394 KToolTipManager::instance()->setDelegate(delegate
);