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"
22 #include "ktooltipdelegate.h"
24 #include <QApplication>
31 # include <X11/Xlib.h>
32 # include <X11/extensions/shape.h>
35 // compile with XShape older than 1.0
37 const int ShapeInput
= 2;
41 // ----------------------------------------------------------------------------
44 class KTipLabel
: public QWidget
48 void showTip(const QPoint
&pos
, const KToolTipItem
*item
);
49 void moveTip(const QPoint
&pos
);
53 void paintEvent(QPaintEvent
*);
54 QSize
sizeHint() const;
55 KStyleOptionToolTip
styleOption() const;
56 KToolTipDelegate
*delegate() const;
59 const KToolTipItem
*m_currentItem
;
62 KTipLabel::KTipLabel() : QWidget(0, Qt::ToolTip
)
65 if (QX11Info::isCompositingManagerRunning()) {
66 setAttribute(Qt::WA_TranslucentBackground
);
71 void KTipLabel::showTip(const QPoint
&pos
, const KToolTipItem
*item
)
78 void KTipLabel::hideTip()
84 void KTipLabel::moveTip(const QPoint
&pos
)
89 void KTipLabel::paintEvent(QPaintEvent
*)
91 KStyleOptionToolTip option
= styleOption();
95 if (QX11Info::isCompositingManagerRunning())
96 XShapeCombineRegion(x11Info().display(), winId(), ShapeInput
, 0, 0,
97 delegate()->inputShape(option
).handle(), ShapeSet
);
100 setMask(delegate()->shapeMask(option
));
103 p
.setFont(option
.font
);
104 p
.setPen(QPen(option
.palette
.brush(QPalette::Text
), 0));
105 delegate()->paint(&p
, option
, *m_currentItem
);
108 QSize
KTipLabel::sizeHint() const
113 KStyleOptionToolTip option
= styleOption();
114 return delegate()->sizeHint(option
, *m_currentItem
);
117 KStyleOptionToolTip
KTipLabel::styleOption() const
119 KStyleOptionToolTip option
;
120 KToolTipManager::instance()->initStyleOption(&option
);
124 KToolTipDelegate
*KTipLabel::delegate() const
126 return KToolTipManager::instance()->delegate();
130 // ----------------------------------------------------------------------------
133 KToolTipManager
*KToolTipManager::s_instance
= 0;
135 KToolTipManager::KToolTipManager()
136 : m_label(new KTipLabel
), m_currentItem(0), m_delegate(0)
140 KToolTipManager::~KToolTipManager()
143 delete m_currentItem
;
146 void KToolTipManager::showTip(const QPoint
&pos
, KToolTipItem
*item
)
149 m_label
->showTip(pos
, item
);
150 m_currentItem
= item
;
154 void KToolTipManager::hideTip()
157 delete m_currentItem
;
161 void KToolTipManager::initStyleOption(KStyleOptionToolTip
*option
) const
163 option
->direction
= QApplication::layoutDirection();
164 option
->fontMetrics
= QFontMetrics(QToolTip::font());
165 option
->activeCorner
= KStyleOptionToolTip::TopLeftCorner
;
166 option
->palette
= QToolTip::palette();
167 option
->font
= QToolTip::font();
168 option
->rect
= QRect();
169 option
->state
= QStyle::State_None
;
170 option
->decorationSize
= QSize(32, 32);
173 void KToolTipManager::setDelegate(KToolTipDelegate
*delegate
)
175 m_delegate
= delegate
;
178 void KToolTipManager::update()
180 if (m_currentItem
== 0)
182 m_label
->showTip(m_tooltipPos
, m_currentItem
);
185 KToolTipDelegate
*KToolTipManager::delegate() const
191 // ----------------------------------------------------------------------------
196 void showText(const QPoint
&pos
, const QString
&text
, QWidget
*widget
, const QRect
&rect
)
200 KToolTipItem
*item
= new KToolTipItem(text
);
201 KToolTipManager::instance()->showTip(pos
, item
);
204 void showText(const QPoint
&pos
, const QString
&text
, QWidget
*widget
)
206 showText(pos
, text
, widget
, QRect());
209 void showTip(const QPoint
&pos
, KToolTipItem
*item
)
211 KToolTipManager::instance()->showTip(pos
, item
);
216 KToolTipManager::instance()->hideTip();
219 void setToolTipDelegate(KToolTipDelegate
*delegate
)
221 KToolTipManager::instance()->setDelegate(delegate
);