]> cloud.milkyroute.net Git - dolphin.git/blob - src/tooltipmanager.cpp
* coding style fixes
[dolphin.git] / src / tooltipmanager.cpp
1 /*******************************************************************************
2 * Copyright (C) 2008 by Konstantin Heil <konst.heil@stud.uni-heidelberg.de> *
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 "tooltipmanager.h"
21
22 #include "dolphintooltip.h"
23 #include "dolphinmodel.h"
24 #include "dolphinsortfilterproxymodel.h"
25
26 #include <kicon.h>
27 #include <ktooltip.h>
28 #include <kio/previewjob.h>
29
30 #include <QApplication>
31 #include <QDesktopWidget>
32 #include <QTimer>
33 #include <QToolTip>
34
35 const int PREVIEW_WIDTH = 256;
36 const int PREVIEW_HEIGHT = 256;
37 const int ICON_WIDTH = 128;
38 const int ICON_HEIGHT = 128;
39 const int PREVIEW_DELAY = 250;
40
41 K_GLOBAL_STATIC(DolphinBalloonTooltipDelegate, g_delegate)
42
43 ToolTipManager::ToolTipManager(QAbstractItemView* parent,
44 DolphinSortFilterProxyModel* model) :
45 QObject(parent),
46 m_view(parent),
47 m_dolphinModel(0),
48 m_proxyModel(model),
49 m_timer(0),
50 m_previewTimer(0),
51 m_waitOnPreviewTimer(0),
52 m_item(),
53 m_itemRect(),
54 m_preview(),
55 m_generatingPreview(),
56 m_previewIsLate(),
57 m_previewPass(),
58 m_emptyRenderedKToolTipItem(),
59 m_pix()
60 {
61 KToolTip::setToolTipDelegate(g_delegate);
62
63 m_dolphinModel = static_cast<DolphinModel*>(m_proxyModel->sourceModel());
64 connect(parent, SIGNAL(entered(const QModelIndex&)),
65 this, SLOT(requestToolTip(const QModelIndex&)));
66 connect(parent, SIGNAL(viewportEntered()),
67 this, SLOT(hideToolTip()));
68
69 m_timer = new QTimer(this);
70 m_timer->setSingleShot(true);
71 connect(m_timer, SIGNAL(timeout()),
72 this, SLOT(prepareToolTip()));
73
74 m_previewTimer = new QTimer(this);
75 m_previewTimer->setSingleShot(true);
76 connect(m_previewTimer, SIGNAL(timeout()),
77 this, SLOT(startPreviewJob()));
78
79 m_waitOnPreviewTimer = new QTimer(this);
80 m_waitOnPreviewTimer->setSingleShot(true);
81 connect(m_waitOnPreviewTimer, SIGNAL(timeout()),
82 this, SLOT(prepareToolTip()));
83
84 m_view->viewport()->installEventFilter(this);
85
86 m_preview = false;
87 m_generatingPreview = false;
88 m_previewIsLate = false;
89 m_previewPass = 0;
90 m_pix = QPixmap();
91 }
92
93 ToolTipManager::~ToolTipManager()
94 {
95 }
96
97 void ToolTipManager::hideTip()
98 {
99 hideToolTip();
100 }
101
102 bool ToolTipManager::eventFilter(QObject* watched, QEvent* event)
103 {
104 if ((watched == m_view->viewport()) && (event->type() == QEvent::Leave)) {
105 hideToolTip();
106 }
107
108 return QObject::eventFilter(watched, event);
109 }
110
111 void ToolTipManager::requestToolTip(const QModelIndex& index)
112 {
113 if (index.column() == DolphinModel::Name) {
114 KToolTip::hideTip();
115
116 m_itemRect = m_view->visualRect(index);
117 const QPoint pos = m_view->viewport()->mapToGlobal(m_itemRect.topLeft());
118 m_itemRect.moveTo(pos);
119
120 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
121 m_item = m_dolphinModel->itemForIndex(dirIndex);
122
123 // Only start the previewJob when the mouse has been over this item for 200msec,
124 // this prevents a lot of useless previewJobs (when passing rapidly over a lot of items).
125 m_previewTimer->start(200);
126 // reset these variables
127 m_preview = false;
128 m_previewIsLate = false;
129 m_previewPass = 0;
130
131 m_timer->start(500);
132 } else {
133 hideToolTip();
134 }
135 }
136
137 void ToolTipManager::hideToolTip()
138 {
139 m_timer->stop();
140 m_previewTimer->stop();
141 KToolTip::hideTip();
142 }
143
144 void ToolTipManager::prepareToolTip()
145 {
146 if (m_generatingPreview) {
147 if (m_previewPass == 1) {
148 // We waited 250msec and the preview is still not finished,
149 // so show the toolTip with a transparent image of maximal width.
150 // When the preview finishes, m_previewIsLate will cause
151 // a direct update of the tooltip, via m_emptyRenderedKToolTipItem.
152 QPixmap paddedImage(QSize(PREVIEW_WIDTH, 32));
153 m_previewIsLate = true;
154 paddedImage.fill(Qt::transparent);
155 KToolTipItem* toolTip = new KToolTipItem(paddedImage, m_item.getToolTipText());
156 m_emptyRenderedKToolTipItem = toolTip; // make toolTip accessible everywhere
157 showToolTip(toolTip);
158 }
159
160 ++m_previewPass;
161 m_waitOnPreviewTimer->start(250);
162 } else {
163 // The preview generation has finished, find out under which circumstances.
164 if (m_preview && m_previewIsLate) {
165 // We got a preview, but it is late, the tooltip has already been shown.
166 // So update the tooltip directly.
167 m_emptyRenderedKToolTipItem->setData(Qt::DecorationRole, KIcon(m_pix));
168 return;
169 }
170
171 KIcon icon;
172 if (m_preview) {
173 // We got a preview.
174 icon = KIcon(m_pix);
175 } else {
176 // No preview, so use an icon.
177 // Force a 128x128 icon, a 256x256 one is far too big.
178 icon = KIcon(KIcon(m_item.iconName()).pixmap(ICON_WIDTH, ICON_HEIGHT));
179 }
180
181 KToolTipItem* toolTip = new KToolTipItem(icon, m_item.getToolTipText());
182 showToolTip(toolTip);
183 }
184 }
185
186 void ToolTipManager::showToolTip(KToolTipItem* tip)
187 {
188 KStyleOptionToolTip option;
189 // TODO: get option content from KToolTip or add KToolTip::sizeHint() method
190 option.direction = QApplication::layoutDirection();
191 option.fontMetrics = QFontMetrics(QToolTip::font());
192 option.activeCorner = KStyleOptionToolTip::TopLeftCorner;
193 option.palette = QToolTip::palette();
194 option.font = QToolTip::font();
195 option.rect = QRect();
196 option.state = QStyle::State_None;
197 option.decorationSize = QSize(32, 32);
198
199 QSize size;
200 if (m_previewIsLate) {
201 QPixmap paddedImage(QSize(PREVIEW_WIDTH, PREVIEW_HEIGHT));
202 KToolTipItem* maxiTip = new KToolTipItem(paddedImage, m_item.getToolTipText());
203 size = g_delegate->sizeHint(&option, maxiTip);
204 delete maxiTip;
205 maxiTip = 0;
206 }
207 else {
208 size = g_delegate->sizeHint(&option, tip);
209 }
210 const QRect desktop = QApplication::desktop()->screenGeometry(m_itemRect.bottomRight());
211
212 // m_itemRect defines the area of the item, where the tooltip should be
213 // shown. Per default the tooltip is shown in the bottom right corner.
214 // If the tooltip content exceeds the desktop borders, it must be assured that:
215 // - the content is fully visible
216 // - the content is not drawn inside m_itemRect
217 const bool hasRoomToLeft = (m_itemRect.left() - size.width() >= desktop.left());
218 const bool hasRoomToRight = (m_itemRect.right() + size.width() <= desktop.right());
219 const bool hasRoomAbove = (m_itemRect.top() - size.height() >= desktop.top());
220 const bool hasRoomBelow = (m_itemRect.bottom() + size.height() <= desktop.bottom());
221 if (!hasRoomAbove && !hasRoomBelow && !hasRoomToLeft && !hasRoomToRight) {
222 delete tip;
223 tip = 0;
224 return;
225 }
226
227 int x = 0;
228 if (hasRoomToLeft || hasRoomToRight) {
229 x = hasRoomToRight ? m_itemRect.right() : m_itemRect.left() - size.width();
230 } else {
231 // Put the tooltip at the far right of the screen. The item will be overlapped
232 // horizontally, but the y-coordinate will be adjusted afterwards so that no overlapping
233 // occurs vertically.
234 x = desktop.right() - size.width();
235 }
236
237 int y = 0;
238 if (hasRoomBelow || hasRoomAbove) {
239 y = hasRoomBelow ? m_itemRect.bottom() : m_itemRect.top() - size.height();
240 } else {
241 // Put the tooltip at the bottom of the screen. The x-coordinate has already
242 // been adjusted, so that no overlapping with m_itemRect occurs.
243 y = desktop.bottom() - size.height();
244 }
245
246 KToolTip::showTip(QPoint(x, y), tip);
247 }
248
249
250
251 void ToolTipManager::startPreviewJob()
252 {
253 m_generatingPreview = true;
254 KIO::PreviewJob* job = KIO::filePreview(KUrl::List() << m_item.url(),
255 PREVIEW_WIDTH,
256 PREVIEW_HEIGHT);
257 job->setIgnoreMaximumSize(true);
258
259 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
260 this, SLOT(setPreviewPix(const KFileItem&, const QPixmap&)));
261 connect(job, SIGNAL(failed(const KFileItem&)),
262 this, SLOT(previewFailed(const KFileItem&)));
263 }
264
265
266 void ToolTipManager::setPreviewPix(const KFileItem& item,
267 const QPixmap& pixmap)
268 {
269 if (m_item.url() != item.url()) {
270 return;
271 }
272
273 QPixmap icon = pixmap;
274 // only paint borders if the pixmap is opaque
275 if (!icon.hasAlphaChannel()) {
276 // TODO: Make code from IconManager for drawing a border accessible for
277 // other classes (the following code has been adapted from IconManager).
278 // The frame is painted on top of the pixmap, this is needed to keep
279 // the text preview visually nice (the previewer adds ugly borders).
280 QPainter painter;
281 // make a buffer pixmap, tends to crash when 'icon' is directly painted ...
282 QPixmap framedIcon(icon.size().width(), pixmap.size().height());
283 framedIcon.fill();
284 const int width = framedIcon.width() - 1;
285 const int height = framedIcon.height() - 1;
286
287 // draw the pixmap
288 painter.begin(&framedIcon);
289 painter.drawPixmap(0,0, icon);
290
291 // draw the frame
292 painter.setRenderHint(QPainter::Antialiasing, false);
293 painter.setPen(QColor(0, 0, 0));
294 painter.drawRect(0, 0, width - 0, height - 1);
295 painter.drawRect(1, 1, width - 2, height - 2);
296 painter.setPen(QColor(255, 255, 255));
297 painter.drawRect(2, 2, width - 4, height - 4);
298
299 painter.end();
300 icon = framedIcon;
301
302 // provide an alpha channel for the frame
303 QPixmap alphaChannel(icon.size());
304 alphaChannel.fill();
305
306 QPainter alphaPainter(&alphaChannel);
307 alphaPainter.setBrush(Qt::NoBrush);
308 alphaPainter.setRenderHint(QPainter::Antialiasing, false);
309 alphaPainter.setPen(QColor(32, 32, 32));
310 alphaPainter.drawRect(0, 0, width, height);
311 alphaPainter.setPen(QColor(64, 64, 64));
312 alphaPainter.drawRect(1, 1, width - 2, height - 2);
313
314 icon.setAlphaChannel(alphaChannel);
315 }
316
317 if (m_previewIsLate) {
318 // always use the maximal width
319 QPixmap paddedImage(QSize(PREVIEW_WIDTH, icon.height()));
320 paddedImage.fill(Qt::transparent);
321 QPainter painter(&paddedImage);
322 painter.drawPixmap((PREVIEW_WIDTH - icon.width()) / 2, 0, icon);
323 m_pix = paddedImage;
324 } else {
325 m_pix = icon;
326 }
327 m_preview = true;
328 m_generatingPreview = false;
329 }
330
331 void ToolTipManager::previewFailed(const KFileItem& item)
332 {
333 Q_UNUSED(item);
334 m_generatingPreview = false;
335 }
336
337 #include "tooltipmanager.moc"