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
*)
94 KStyleOptionToolTip option
= styleOption();
98 if (QX11Info::isCompositingManagerRunning())
99 XShapeCombineRegion(x11Info().display(), winId(), ShapeInput
, 0, 0,
100 delegate()->inputShape(option
).handle(), ShapeSet
);
103 setMask(delegate()->shapeMask(option
));
106 p
.setFont(option
.font
);
107 p
.setPen(QPen(option
.palette
.brush(QPalette::Text
), 0));
108 delegate()->paint(&p
, option
, *m_currentItem
);
111 QSize
KTipLabel::sizeHint() const
116 KStyleOptionToolTip option
= styleOption();
117 return delegate()->sizeHint(option
, *m_currentItem
);
120 KStyleOptionToolTip
KTipLabel::styleOption() const
122 KStyleOptionToolTip option
;
123 KToolTipManager::instance()->initStyleOption(&option
);
127 KToolTipDelegate
*KTipLabel::delegate() const
129 return KToolTipManager::instance()->delegate();
133 // ----------------------------------------------------------------------------
136 KToolTipManager
*KToolTipManager::s_instance
= 0;
138 KToolTipManager::KToolTipManager()
139 : m_label(new KTipLabel
), m_currentItem(0), m_delegate(0)
143 KToolTipManager::~KToolTipManager()
146 delete m_currentItem
;
149 void KToolTipManager::showTip(const QPoint
&pos
, KToolTipItem
*item
)
152 m_label
->showTip(pos
, item
);
153 m_currentItem
= item
;
157 void KToolTipManager::hideTip()
160 delete m_currentItem
;
164 void KToolTipManager::initStyleOption(KStyleOptionToolTip
*option
) const
166 option
->direction
= QApplication::layoutDirection();
167 option
->fontMetrics
= QFontMetrics(QToolTip::font());
168 option
->activeCorner
= KStyleOptionToolTip::TopLeftCorner
;
169 option
->palette
= QToolTip::palette();
170 option
->font
= QToolTip::font();
171 option
->rect
= QRect();
172 option
->state
= QStyle::State_None
;
173 option
->decorationSize
= QSize(32, 32);
176 void KToolTipManager::setDelegate(KToolTipDelegate
*delegate
)
178 m_delegate
= delegate
;
181 void KToolTipManager::update()
183 if (m_currentItem
== 0)
185 m_label
->showTip(m_tooltipPos
, m_currentItem
);
188 KToolTipDelegate
*KToolTipManager::delegate() const
194 // ----------------------------------------------------------------------------
199 void showText(const QPoint
&pos
, const QString
&text
, QWidget
*widget
, const QRect
&rect
)
203 KToolTipItem
*item
= new KToolTipItem(text
);
204 KToolTipManager::instance()->showTip(pos
, item
);
207 void showText(const QPoint
&pos
, const QString
&text
, QWidget
*widget
)
209 showText(pos
, text
, widget
, QRect());
212 void showTip(const QPoint
&pos
, KToolTipItem
*item
)
214 KToolTipManager::instance()->showTip(pos
, item
);
219 KToolTipManager::instance()->hideTip();
222 void setToolTipDelegate(KToolTipDelegate
*delegate
)
224 KToolTipManager::instance()->setDelegate(delegate
);