]> cloud.milkyroute.net Git - dolphin.git/blob - src/tooltipmanager.cpp
fixed issue that the zoom slider might not have enough vertical space when having...
[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 m_waitOnPreviewTimer->stop();
109 KToolTip::hideTip();
110
111 m_itemRect = m_view->visualRect(index);
112 const QPoint pos = m_view->viewport()->mapToGlobal(m_itemRect.topLeft());
113 m_itemRect.moveTo(pos);
114
115 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
116 m_item = m_dolphinModel->itemForIndex(dirIndex);
117
118 // Only start the previewJob when the mouse has been over this item for 200msec,
119 // this prevents a lot of useless previewJobs (when passing rapidly over a lot of items).
120 m_previewTimer->start(200);
121 // reset these variables
122 m_preview = false;
123 m_previewIsLate = false;
124 m_previewPass = 0;
125
126 m_timer->start(500);
127 } else {
128 hideToolTip();
129 }
130 }
131
132 void ToolTipManager::hideToolTip()
133 {
134 m_timer->stop();
135 m_previewTimer->stop();
136 m_waitOnPreviewTimer->stop();
137 m_previewIsLate = false;
138 KToolTip::hideTip();
139 }
140
141 void ToolTipManager::prepareToolTip()
142 {
143 if (m_generatingPreview) {
144 if (m_previewPass == 1) {
145 // We waited 250msec and the preview is still not finished,
146 // so show the toolTip with a transparent image of maximal width.
147 // When the preview finishes, m_previewIsLate will cause
148 // a direct update of the tooltip, via m_emptyRenderedKToolTipItem.
149 QPixmap paddedImage(QSize(PREVIEW_WIDTH, 32));
150 m_previewIsLate = true;
151 paddedImage.fill(Qt::transparent);
152 KToolTipItem* toolTip = new KToolTipItem(paddedImage, m_item.getToolTipText());
153 m_emptyRenderedKToolTipItem = toolTip; // make toolTip accessible everywhere
154 showToolTip(toolTip);
155 }
156
157 ++m_previewPass;
158 m_waitOnPreviewTimer->start(250);
159 } else {
160 // The preview generation has finished, find out under which circumstances.
161 if (m_preview && m_previewIsLate) {
162 // We got a preview, but it is late, the tooltip has already been shown.
163 // So update the tooltip directly.
164 if (m_emptyRenderedKToolTipItem != 0) {
165 m_emptyRenderedKToolTipItem->setData(Qt::DecorationRole, KIcon(m_pix));
166 }
167 return;
168 }
169
170 KIcon icon;
171 if (m_preview) {
172 // We got a preview.
173 icon = KIcon(m_pix);
174 } else {
175 // No preview, so use an icon.
176 // Force a 128x128 icon, a 256x256 one is far too big.
177 icon = KIcon(KIcon(m_item.iconName()).pixmap(ICON_WIDTH, ICON_HEIGHT));
178 }
179
180 KToolTipItem* toolTip = new KToolTipItem(icon, m_item.getToolTipText());
181 showToolTip(toolTip);
182 }
183 }
184
185 void ToolTipManager::showToolTip(KToolTipItem* tip)
186 {
187 if (QApplication::mouseButtons() & Qt::LeftButton) {
188 delete tip;
189 tip = 0;
190 // m_emptyRenderedKToolTipItem is an alias for tip.
191 m_emptyRenderedKToolTipItem = 0;
192 return;
193 }
194
195 KStyleOptionToolTip option;
196 // TODO: get option content from KToolTip or add KToolTip::sizeHint() method
197 option.direction = QApplication::layoutDirection();
198 option.fontMetrics = QFontMetrics(QToolTip::font());
199 option.activeCorner = KStyleOptionToolTip::TopLeftCorner;
200 option.palette = QToolTip::palette();
201 option.font = QToolTip::font();
202 option.rect = QRect();
203 option.state = QStyle::State_None;
204 option.decorationSize = QSize(32, 32);
205
206 QSize size;
207 if (m_previewIsLate) {
208 QPixmap paddedImage(QSize(PREVIEW_WIDTH, PREVIEW_HEIGHT));
209 KToolTipItem* maxiTip = new KToolTipItem(paddedImage, m_item.getToolTipText());
210 size = g_delegate->sizeHint(&option, maxiTip);
211 delete maxiTip;
212 maxiTip = 0;
213 }
214 else {
215 size = g_delegate->sizeHint(&option, tip);
216 }
217 const QRect desktop = QApplication::desktop()->screenGeometry(m_itemRect.bottomRight());
218
219 // m_itemRect defines the area of the item, where the tooltip should be
220 // shown. Per default the tooltip is shown in the bottom right corner.
221 // If the tooltip content exceeds the desktop borders, it must be assured that:
222 // - the content is fully visible
223 // - the content is not drawn inside m_itemRect
224 const bool hasRoomToLeft = (m_itemRect.left() - size.width() >= desktop.left());
225 const bool hasRoomToRight = (m_itemRect.right() + size.width() <= desktop.right());
226 const bool hasRoomAbove = (m_itemRect.top() - size.height() >= desktop.top());
227 const bool hasRoomBelow = (m_itemRect.bottom() + size.height() <= desktop.bottom());
228 if (!hasRoomAbove && !hasRoomBelow && !hasRoomToLeft && !hasRoomToRight) {
229 delete tip;
230 tip = 0;
231 return;
232 }
233
234 int x = 0;
235 int y = 0;
236 if (hasRoomBelow || hasRoomAbove) {
237 x = QCursor::pos().x() + 16; // TODO: use mouse pointer width instead of the magic value of 16
238 if (x + size.width() >= desktop.right()) {
239 x = desktop.right() - size.width();
240 }
241 y = hasRoomBelow ? m_itemRect.bottom() : m_itemRect.top() - size.height();
242 } else {
243 Q_ASSERT(hasRoomToLeft || hasRoomToRight);
244 x = hasRoomToRight ? m_itemRect.right() : m_itemRect.left() - size.width();
245
246 // Put the tooltip at the bottom of the screen. The x-coordinate has already
247 // been adjusted, so that no overlapping with m_itemRect occurs.
248 y = desktop.bottom() - size.height();
249 }
250
251 KToolTip::showTip(QPoint(x, y), tip);
252 }
253
254
255
256 void ToolTipManager::startPreviewJob()
257 {
258 m_generatingPreview = true;
259 KIO::PreviewJob* job = KIO::filePreview(KUrl::List() << m_item.url(),
260 PREVIEW_WIDTH,
261 PREVIEW_HEIGHT);
262 job->setIgnoreMaximumSize(true);
263
264 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
265 this, SLOT(setPreviewPix(const KFileItem&, const QPixmap&)));
266 connect(job, SIGNAL(failed(const KFileItem&)),
267 this, SLOT(previewFailed(const KFileItem&)));
268 }
269
270
271 void ToolTipManager::setPreviewPix(const KFileItem& item,
272 const QPixmap& pixmap)
273 {
274 if (m_item.url() != item.url()) {
275 m_generatingPreview = false;
276 return;
277 }
278
279 if (m_previewIsLate) {
280 // always use the maximal width
281 QPixmap paddedImage(QSize(PREVIEW_WIDTH, pixmap.height()));
282 paddedImage.fill(Qt::transparent);
283 QPainter painter(&paddedImage);
284 painter.drawPixmap((PREVIEW_WIDTH - pixmap.width()) / 2, 0, pixmap);
285 m_pix = paddedImage;
286 } else {
287 m_pix = pixmap;
288 }
289 m_preview = true;
290 m_generatingPreview = false;
291 }
292
293 void ToolTipManager::previewFailed(const KFileItem& item)
294 {
295 Q_UNUSED(item);
296 m_generatingPreview = false;
297 }
298
299 #include "tooltipmanager.moc"