]> cloud.milkyroute.net Git - dolphin.git/blob - src/tooltipmanager.cpp
When common view properties are used for all folders, the internal properties should...
[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 <QScrollBar>
33 #include <QTimer>
34 #include <QToolTip>
35
36 const int PREVIEW_WIDTH = 256;
37 const int PREVIEW_HEIGHT = 256;
38 const int ICON_WIDTH = 128;
39 const int ICON_HEIGHT = 128;
40 const int PREVIEW_DELAY = 250;
41
42 K_GLOBAL_STATIC(DolphinBalloonTooltipDelegate, g_delegate)
43
44 ToolTipManager::ToolTipManager(QAbstractItemView* parent,
45 DolphinSortFilterProxyModel* model) :
46 QObject(parent),
47 m_view(parent),
48 m_dolphinModel(0),
49 m_proxyModel(model),
50 m_timer(0),
51 m_previewTimer(0),
52 m_waitOnPreviewTimer(0),
53 m_item(),
54 m_itemRect(),
55 m_preview(false),
56 m_generatingPreview(false),
57 m_previewIsLate(false),
58 m_previewPass(0),
59 m_emptyRenderedKToolTipItem(0),
60 m_pix()
61 {
62 KToolTip::setToolTipDelegate(g_delegate);
63
64 m_dolphinModel = static_cast<DolphinModel*>(m_proxyModel->sourceModel());
65 connect(parent, SIGNAL(entered(const QModelIndex&)),
66 this, SLOT(requestToolTip(const QModelIndex&)));
67 connect(parent, SIGNAL(viewportEntered()),
68 this, SLOT(hideToolTip()));
69
70 m_timer = new QTimer(this);
71 m_timer->setSingleShot(true);
72 connect(m_timer, SIGNAL(timeout()),
73 this, SLOT(prepareToolTip()));
74
75 m_previewTimer = new QTimer(this);
76 m_previewTimer->setSingleShot(true);
77 connect(m_previewTimer, SIGNAL(timeout()),
78 this, SLOT(startPreviewJob()));
79
80 m_waitOnPreviewTimer = new QTimer(this);
81 m_waitOnPreviewTimer->setSingleShot(true);
82 connect(m_waitOnPreviewTimer, SIGNAL(timeout()),
83 this, SLOT(prepareToolTip()));
84
85 // When the mousewheel is used, the items don't get a hovered indication
86 // (Qt-issue #200665). To assure that the tooltip still gets hidden,
87 // the scrollbars are observed.
88 connect(parent->horizontalScrollBar(), SIGNAL(valueChanged(int)),
89 this, SLOT(hideTip()));
90 connect(parent->verticalScrollBar(), SIGNAL(valueChanged(int)),
91 this, SLOT(hideTip()));
92
93 m_view->viewport()->installEventFilter(this);
94 }
95
96 ToolTipManager::~ToolTipManager()
97 {
98 }
99
100 void ToolTipManager::hideTip()
101 {
102 hideToolTip();
103 }
104
105 bool ToolTipManager::eventFilter(QObject* watched, QEvent* event)
106 {
107 if ((watched == m_view->viewport()) && (event->type() == QEvent::Leave)) {
108 hideToolTip();
109 }
110
111 return QObject::eventFilter(watched, event);
112 }
113
114 void ToolTipManager::requestToolTip(const QModelIndex& index)
115 {
116 // only request a tooltip for the name column and when no selection or
117 // drag & drop operation is done (indicated by the left mouse button)
118 if ((index.column() == DolphinModel::Name) && !(QApplication::mouseButtons() & Qt::LeftButton)) {
119 m_waitOnPreviewTimer->stop();
120 KToolTip::hideTip();
121
122 m_itemRect = m_view->visualRect(index);
123 const QPoint pos = m_view->viewport()->mapToGlobal(m_itemRect.topLeft());
124 m_itemRect.moveTo(pos);
125
126 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
127 m_item = m_dolphinModel->itemForIndex(dirIndex);
128
129 // only start the previewJob when the mouse has been over this item for 200 milliseconds,
130 // this prevents a lot of useless preview jobs when passing rapidly over a lot of items
131 m_previewTimer->start(200);
132 m_preview = false;
133 m_previewIsLate = false;
134 m_previewPass = 0;
135
136 m_timer->start(500);
137 } else {
138 hideToolTip();
139 }
140 }
141
142 void ToolTipManager::hideToolTip()
143 {
144 m_timer->stop();
145 m_previewTimer->stop();
146 m_waitOnPreviewTimer->stop();
147 m_previewIsLate = false;
148 KToolTip::hideTip();
149 }
150
151 void ToolTipManager::prepareToolTip()
152 {
153 if (m_generatingPreview) {
154 if (m_previewPass == 1) {
155 // We waited 250msec and the preview is still not finished,
156 // so show the toolTip with a transparent image of maximal width.
157 // When the preview finishes, m_previewIsLate will cause
158 // a direct update of the tooltip, via m_emptyRenderedKToolTipItem.
159 QPixmap paddedImage(QSize(PREVIEW_WIDTH, 32));
160 m_previewIsLate = true;
161 paddedImage.fill(Qt::transparent);
162 KToolTipItem* toolTip = new KToolTipItem(paddedImage, m_item.getToolTipText());
163 m_emptyRenderedKToolTipItem = toolTip; // make toolTip accessible everywhere
164 showToolTip(toolTip);
165 }
166
167 ++m_previewPass;
168 m_waitOnPreviewTimer->start(250);
169 } else {
170 // The preview generation has finished, find out under which circumstances.
171 if (m_preview && m_previewIsLate) {
172 // We got a preview, but it is late, the tooltip has already been shown.
173 // So update the tooltip directly.
174 if (m_emptyRenderedKToolTipItem != 0) {
175 m_emptyRenderedKToolTipItem->setData(Qt::DecorationRole, KIcon(m_pix));
176 }
177 return;
178 }
179
180 KIcon icon;
181 if (m_preview) {
182 // We got a preview.
183 icon = KIcon(m_pix);
184 } else {
185 // No preview, so use an icon.
186 // Force a 128x128 icon, a 256x256 one is far too big.
187 icon = KIcon(KIcon(m_item.iconName()).pixmap(ICON_WIDTH, ICON_HEIGHT));
188 }
189
190 KToolTipItem* toolTip = new KToolTipItem(icon, m_item.getToolTipText());
191 showToolTip(toolTip);
192 }
193 }
194
195 void ToolTipManager::showToolTip(KToolTipItem* tip)
196 {
197 if (QApplication::mouseButtons() & Qt::LeftButton) {
198 delete tip;
199 tip = 0;
200 // m_emptyRenderedKToolTipItem is an alias for tip.
201 m_emptyRenderedKToolTipItem = 0;
202 return;
203 }
204
205 KStyleOptionToolTip option;
206 // TODO: get option content from KToolTip or add KToolTip::sizeHint() method
207 option.direction = QApplication::layoutDirection();
208 option.fontMetrics = QFontMetrics(QToolTip::font());
209 option.activeCorner = KStyleOptionToolTip::TopLeftCorner;
210 option.palette = QToolTip::palette();
211 option.font = QToolTip::font();
212 option.rect = QRect();
213 option.state = QStyle::State_None;
214 option.decorationSize = QSize(32, 32);
215
216 QSize size;
217 if (m_previewIsLate) {
218 QPixmap paddedImage(QSize(PREVIEW_WIDTH, PREVIEW_HEIGHT));
219 KToolTipItem* maxiTip = new KToolTipItem(paddedImage, m_item.getToolTipText());
220 size = g_delegate->sizeHint(&option, maxiTip);
221 delete maxiTip;
222 maxiTip = 0;
223 }
224 else {
225 size = g_delegate->sizeHint(&option, tip);
226 }
227 const QRect desktop = QApplication::desktop()->screenGeometry(m_itemRect.bottomRight());
228
229 // m_itemRect defines the area of the item, where the tooltip should be
230 // shown. Per default the tooltip is shown in the bottom right corner.
231 // If the tooltip content exceeds the desktop borders, it must be assured that:
232 // - the content is fully visible
233 // - the content is not drawn inside m_itemRect
234 const bool hasRoomToLeft = (m_itemRect.left() - size.width() >= desktop.left());
235 const bool hasRoomToRight = (m_itemRect.right() + size.width() <= desktop.right());
236 const bool hasRoomAbove = (m_itemRect.top() - size.height() >= desktop.top());
237 const bool hasRoomBelow = (m_itemRect.bottom() + size.height() <= desktop.bottom());
238 if (!hasRoomAbove && !hasRoomBelow && !hasRoomToLeft && !hasRoomToRight) {
239 delete tip;
240 tip = 0;
241 return;
242 }
243
244 int x = 0;
245 int y = 0;
246 if (hasRoomBelow || hasRoomAbove) {
247 x = QCursor::pos().x() + 16; // TODO: use mouse pointer width instead of the magic value of 16
248 if (x + size.width() >= desktop.right()) {
249 x = desktop.right() - size.width();
250 }
251 y = hasRoomBelow ? m_itemRect.bottom() : m_itemRect.top() - size.height();
252 } else {
253 Q_ASSERT(hasRoomToLeft || hasRoomToRight);
254 x = hasRoomToRight ? m_itemRect.right() : m_itemRect.left() - size.width();
255
256 // Put the tooltip at the bottom of the screen. The x-coordinate has already
257 // been adjusted, so that no overlapping with m_itemRect occurs.
258 y = desktop.bottom() - size.height();
259 }
260
261 KToolTip::showTip(QPoint(x, y), tip);
262 }
263
264
265
266 void ToolTipManager::startPreviewJob()
267 {
268 m_generatingPreview = true;
269 KIO::PreviewJob* job = KIO::filePreview(KUrl::List() << m_item.url(),
270 PREVIEW_WIDTH,
271 PREVIEW_HEIGHT);
272 job->setIgnoreMaximumSize(true);
273
274 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
275 this, SLOT(setPreviewPix(const KFileItem&, const QPixmap&)));
276 connect(job, SIGNAL(failed(const KFileItem&)),
277 this, SLOT(previewFailed(const KFileItem&)));
278 }
279
280
281 void ToolTipManager::setPreviewPix(const KFileItem& item,
282 const QPixmap& pixmap)
283 {
284 if (m_item.url() != item.url()) {
285 m_generatingPreview = false;
286 return;
287 }
288
289 if (m_previewIsLate) {
290 // always use the maximal width
291 QPixmap paddedImage(QSize(PREVIEW_WIDTH, pixmap.height()));
292 paddedImage.fill(Qt::transparent);
293 QPainter painter(&paddedImage);
294 painter.drawPixmap((PREVIEW_WIDTH - pixmap.width()) / 2, 0, pixmap);
295 m_pix = paddedImage;
296 } else {
297 m_pix = pixmap;
298 }
299 m_preview = true;
300 m_generatingPreview = false;
301 }
302
303 void ToolTipManager::previewFailed(const KFileItem& item)
304 {
305 Q_UNUSED(item);
306 m_generatingPreview = false;
307 }
308
309 #include "tooltipmanager.moc"