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
201 return QX11Info::isCompositingManagerRunning();
209 // ----------------------------------------------------------------------------
213 class KTipLabel
: public QWidget
217 void showTip(const QPoint
&pos
, const KToolTipItem
*item
);
218 void moveTip(const QPoint
&pos
);
222 void paintEvent(QPaintEvent
*);
223 QSize
sizeHint() const;
224 KStyleOptionToolTip
styleOption() const;
225 KToolTipDelegate
*delegate() const;
228 const KToolTipItem
*currentItem
;
231 KTipLabel::KTipLabel() : QWidget(0, Qt::ToolTip
)
234 if (QX11Info::isCompositingManagerRunning()) {
235 setAttribute(Qt::WA_TranslucentBackground
);
240 void KTipLabel::showTip(const QPoint
&pos
, const KToolTipItem
*item
)
247 void KTipLabel::hideTip()
253 void KTipLabel::moveTip(const QPoint
&pos
)
258 void KTipLabel::paintEvent(QPaintEvent
*)
260 KStyleOptionToolTip option
= styleOption();
261 option
.rect
= rect();
264 if (QX11Info::isCompositingManagerRunning())
265 XShapeCombineRegion(x11Info().display(), winId(), ShapeInput
, 0, 0,
266 delegate()->inputShape(&option
).handle(), ShapeSet
);
269 setMask(delegate()->shapeMask(&option
));
272 p
.setFont(option
.font
);
273 p
.setPen(QPen(option
.palette
.brush(QPalette::Text
), 0));
274 delegate()->paint(&p
, &option
, currentItem
);
277 QSize
KTipLabel::sizeHint() const
282 KStyleOptionToolTip option
= styleOption();
283 return delegate()->sizeHint(&option
, currentItem
);
286 KStyleOptionToolTip
KTipLabel::styleOption() const
288 KStyleOptionToolTip option
;
289 KToolTipManager::instance()->initStyleOption(&option
);
293 KToolTipDelegate
*KTipLabel::delegate() const
295 return KToolTipManager::instance()->delegate();
301 // ----------------------------------------------------------------------------
306 KToolTipManager
*KToolTipManager::s_instance
= 0;
308 KToolTipManager::KToolTipManager()
309 : label(new KTipLabel
), currentItem(0), m_delegate(0)
313 KToolTipManager::~KToolTipManager()
319 void KToolTipManager::showTip(const QPoint
&pos
, KToolTipItem
*item
)
322 label
->showTip(pos
, item
);
327 void KToolTipManager::hideTip()
334 void KToolTipManager::initStyleOption(KStyleOptionToolTip
*option
) const
336 option
->direction
= QApplication::layoutDirection();
337 option
->fontMetrics
= QFontMetrics(QToolTip::font());
338 option
->activeCorner
= KStyleOptionToolTip::TopLeftCorner
;
339 option
->palette
= QToolTip::palette();
340 option
->font
= QToolTip::font();
341 option
->rect
= QRect();
342 option
->state
= QStyle::State_None
;
343 option
->decorationSize
= QSize(32, 32);
346 void KToolTipManager::setDelegate(KToolTipDelegate
*delegate
)
348 m_delegate
= delegate
;
351 void KToolTipManager::update()
353 if (currentItem
== 0)
355 label
->showTip(m_tooltipPos
, currentItem
);
358 KToolTipDelegate
*KToolTipManager::delegate() const
365 // ----------------------------------------------------------------------------
371 void showText(const QPoint
&pos
, const QString
&text
, QWidget
*widget
, const QRect
&rect
)
375 KToolTipItem
*item
= new KToolTipItem(text
);
376 KToolTipManager::instance()->showTip(pos
, item
);
379 void showText(const QPoint
&pos
, const QString
&text
, QWidget
*widget
)
381 showText(pos
, text
, widget
, QRect());
384 void showTip(const QPoint
&pos
, KToolTipItem
*item
)
386 KToolTipManager::instance()->showTip(pos
, item
);
391 KToolTipManager::instance()->hideTip();
394 void setToolTipDelegate(KToolTipDelegate
*delegate
)
396 KToolTipManager::instance()->setDelegate(delegate
);