]> cloud.milkyroute.net Git - dolphin.git/blob - src/tooltips/ktooltip.cpp
Forward port for SVN commit 996129: Fix possible crash when no current item is given...
[dolphin.git] / src / tooltips / ktooltip.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 by Fredrik Höglund <fredrik@kde.org> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "ktooltip.h"
21 #include "ktooltip_p.h"
22 #include "ktooltipdelegate.h"
23
24 #include <QApplication>
25 #include <QPainter>
26 #include <QWidget>
27 #include <QToolTip>
28
29 #ifdef Q_WS_X11
30 # include <QX11Info>
31 # include <X11/Xlib.h>
32 # include <X11/extensions/shape.h>
33 #endif
34
35 // compile with XShape older than 1.0
36 #ifndef ShapeInput
37 const int ShapeInput = 2;
38 #endif
39
40
41 // ----------------------------------------------------------------------------
42
43
44 class KTipLabel : public QWidget
45 {
46 public:
47 KTipLabel();
48 void showTip(const QPoint &pos, const KToolTipItem *item);
49 void moveTip(const QPoint &pos);
50 void hideTip();
51
52 private:
53 void paintEvent(QPaintEvent*);
54 QSize sizeHint() const;
55 KStyleOptionToolTip styleOption() const;
56 KToolTipDelegate *delegate() const;
57
58 private:
59 const KToolTipItem *m_currentItem;
60 };
61
62 KTipLabel::KTipLabel() : QWidget(0, Qt::ToolTip)
63 {
64 #ifdef Q_WS_X11
65 if (QX11Info::isCompositingManagerRunning()) {
66 setAttribute(Qt::WA_TranslucentBackground);
67 }
68 #endif
69 }
70
71 void KTipLabel::showTip(const QPoint &pos, const KToolTipItem *item)
72 {
73 m_currentItem = item;
74 move(pos);
75 show();
76 }
77
78 void KTipLabel::hideTip()
79 {
80 hide();
81 m_currentItem = 0;
82 }
83
84 void KTipLabel::moveTip(const QPoint &pos)
85 {
86 move(pos);
87 }
88
89 void KTipLabel::paintEvent(QPaintEvent*)
90 {
91 if (!m_currentItem)
92 return;
93
94 KStyleOptionToolTip option = styleOption();
95 option.rect = rect();
96
97 #ifdef Q_WS_X11
98 if (QX11Info::isCompositingManagerRunning())
99 XShapeCombineRegion(x11Info().display(), winId(), ShapeInput, 0, 0,
100 delegate()->inputShape(option).handle(), ShapeSet);
101 else
102 #endif
103 setMask(delegate()->shapeMask(option));
104
105 QPainter p(this);
106 p.setFont(option.font);
107 p.setPen(QPen(option.palette.brush(QPalette::Text), 0));
108 delegate()->paint(&p, option, *m_currentItem);
109 }
110
111 QSize KTipLabel::sizeHint() const
112 {
113 if (!m_currentItem)
114 return QSize();
115
116 KStyleOptionToolTip option = styleOption();
117 return delegate()->sizeHint(option, *m_currentItem);
118 }
119
120 KStyleOptionToolTip KTipLabel::styleOption() const
121 {
122 KStyleOptionToolTip option;
123 KToolTipManager::instance()->initStyleOption(&option);
124 return option;
125 }
126
127 KToolTipDelegate *KTipLabel::delegate() const
128 {
129 return KToolTipManager::instance()->delegate();
130 }
131
132
133 // ----------------------------------------------------------------------------
134
135
136 KToolTipManager *KToolTipManager::s_instance = 0;
137
138 KToolTipManager::KToolTipManager()
139 : m_label(new KTipLabel), m_currentItem(0), m_delegate(0)
140 {
141 }
142
143 KToolTipManager::~KToolTipManager()
144 {
145 delete m_label;
146 delete m_currentItem;
147 }
148
149 void KToolTipManager::showTip(const QPoint &pos, KToolTipItem *item)
150 {
151 hideTip();
152 m_label->showTip(pos, item);
153 m_currentItem = item;
154 m_tooltipPos = pos;
155 }
156
157 void KToolTipManager::hideTip()
158 {
159 m_label->hideTip();
160 delete m_currentItem;
161 m_currentItem = 0;
162 }
163
164 void KToolTipManager::initStyleOption(KStyleOptionToolTip *option) const
165 {
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);
174 }
175
176 void KToolTipManager::setDelegate(KToolTipDelegate *delegate)
177 {
178 m_delegate = delegate;
179 }
180
181 void KToolTipManager::update()
182 {
183 if (m_currentItem == 0)
184 return;
185 m_label->showTip(m_tooltipPos, m_currentItem);
186 }
187
188 KToolTipDelegate *KToolTipManager::delegate() const
189 {
190 return m_delegate;
191 }
192
193
194 // ----------------------------------------------------------------------------
195
196
197 namespace KToolTip
198 {
199 void showText(const QPoint &pos, const QString &text, QWidget *widget, const QRect &rect)
200 {
201 Q_UNUSED(widget)
202 Q_UNUSED(rect)
203 KToolTipItem *item = new KToolTipItem(text);
204 KToolTipManager::instance()->showTip(pos, item);
205 }
206
207 void showText(const QPoint &pos, const QString &text, QWidget *widget)
208 {
209 showText(pos, text, widget, QRect());
210 }
211
212 void showTip(const QPoint &pos, KToolTipItem *item)
213 {
214 KToolTipManager::instance()->showTip(pos, item);
215 }
216
217 void hideTip()
218 {
219 KToolTipManager::instance()->hideTip();
220 }
221
222 void setToolTipDelegate(KToolTipDelegate *delegate)
223 {
224 KToolTipManager::instance()->setDelegate(delegate);
225 }
226 }
227