]> cloud.milkyroute.net Git - dolphin.git/blob - src/tooltips/ktooltip.cpp
Fixed regression when refactoring the Information Panel: Don't forget to give a visua...
[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 KStyleOptionToolTip option = styleOption();
92 option.rect = rect();
93
94 #ifdef Q_WS_X11
95 if (QX11Info::isCompositingManagerRunning())
96 XShapeCombineRegion(x11Info().display(), winId(), ShapeInput, 0, 0,
97 delegate()->inputShape(option).handle(), ShapeSet);
98 else
99 #endif
100 setMask(delegate()->shapeMask(option));
101
102 QPainter p(this);
103 p.setFont(option.font);
104 p.setPen(QPen(option.palette.brush(QPalette::Text), 0));
105 delegate()->paint(&p, option, *m_currentItem);
106 }
107
108 QSize KTipLabel::sizeHint() const
109 {
110 if (!m_currentItem)
111 return QSize();
112
113 KStyleOptionToolTip option = styleOption();
114 return delegate()->sizeHint(option, *m_currentItem);
115 }
116
117 KStyleOptionToolTip KTipLabel::styleOption() const
118 {
119 KStyleOptionToolTip option;
120 KToolTipManager::instance()->initStyleOption(&option);
121 return option;
122 }
123
124 KToolTipDelegate *KTipLabel::delegate() const
125 {
126 return KToolTipManager::instance()->delegate();
127 }
128
129
130 // ----------------------------------------------------------------------------
131
132
133 KToolTipManager *KToolTipManager::s_instance = 0;
134
135 KToolTipManager::KToolTipManager()
136 : m_label(new KTipLabel), m_currentItem(0), m_delegate(0)
137 {
138 }
139
140 KToolTipManager::~KToolTipManager()
141 {
142 delete m_label;
143 delete m_currentItem;
144 }
145
146 void KToolTipManager::showTip(const QPoint &pos, KToolTipItem *item)
147 {
148 hideTip();
149 m_label->showTip(pos, item);
150 m_currentItem = item;
151 m_tooltipPos = pos;
152 }
153
154 void KToolTipManager::hideTip()
155 {
156 m_label->hideTip();
157 delete m_currentItem;
158 m_currentItem = 0;
159 }
160
161 void KToolTipManager::initStyleOption(KStyleOptionToolTip *option) const
162 {
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);
171 }
172
173 void KToolTipManager::setDelegate(KToolTipDelegate *delegate)
174 {
175 m_delegate = delegate;
176 }
177
178 void KToolTipManager::update()
179 {
180 if (m_currentItem == 0)
181 return;
182 m_label->showTip(m_tooltipPos, m_currentItem);
183 }
184
185 KToolTipDelegate *KToolTipManager::delegate() const
186 {
187 return m_delegate;
188 }
189
190
191 // ----------------------------------------------------------------------------
192
193
194 namespace KToolTip
195 {
196 void showText(const QPoint &pos, const QString &text, QWidget *widget, const QRect &rect)
197 {
198 Q_UNUSED(widget)
199 Q_UNUSED(rect)
200 KToolTipItem *item = new KToolTipItem(text);
201 KToolTipManager::instance()->showTip(pos, item);
202 }
203
204 void showText(const QPoint &pos, const QString &text, QWidget *widget)
205 {
206 showText(pos, text, widget, QRect());
207 }
208
209 void showTip(const QPoint &pos, KToolTipItem *item)
210 {
211 KToolTipManager::instance()->showTip(pos, item);
212 }
213
214 void hideTip()
215 {
216 KToolTipManager::instance()->hideTip();
217 }
218
219 void setToolTipDelegate(KToolTipDelegate *delegate)
220 {
221 KToolTipManager::instance()->setDelegate(delegate);
222 }
223 }
224