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