]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/tooltips/tooltipmanager.cpp
Use a pointing-hand cursor when hovering items as discussed on http://lists.kde.org...
[dolphin.git] / src / views / tooltips / 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 "filemetadatatooltip.h"
23 #include <KIcon>
24 #include <KIO/PreviewJob>
25
26 #include <QApplication>
27 #include <QDesktopWidget>
28 #include <QScrollArea>
29 #include <QScrollBar>
30 #include <QTimer>
31
32 #include <views/dolphinmodel.h>
33 #include <views/dolphinsortfilterproxymodel.h>
34
35 ToolTipManager::ToolTipManager(QAbstractItemView* parent,
36 DolphinSortFilterProxyModel* model) :
37 QObject(parent),
38 m_view(parent),
39 m_dolphinModel(0),
40 m_proxyModel(model),
41 m_showToolTipTimer(0),
42 m_contentRetrievalTimer(0),
43 m_fileMetaDataToolTip(0),
44 m_toolTipRequested(false),
45 m_metaDataRequested(false),
46 m_appliedWaitCursor(false),
47 m_item(),
48 m_itemRect()
49 {
50 static FileMetaDataToolTip* sharedToolTip = 0;
51 if (sharedToolTip == 0) {
52 sharedToolTip = new FileMetaDataToolTip();
53 // TODO: Using K_GLOBAL_STATIC would be preferable to maintain the
54 // instance, but the cleanup of KFileMetaDataWidget at this stage does
55 // not work.
56 }
57 m_fileMetaDataToolTip = sharedToolTip;
58 connect(m_fileMetaDataToolTip, SIGNAL(metaDataRequestFinished(KFileItemList)),
59 this, SLOT(slotMetaDataRequestFinished()));
60
61 m_dolphinModel = static_cast<DolphinModel*>(m_proxyModel->sourceModel());
62 connect(parent, SIGNAL(entered(const QModelIndex&)),
63 this, SLOT(requestToolTip(const QModelIndex&)));
64 connect(parent, SIGNAL(viewportEntered()),
65 this, SLOT(hideToolTip()));
66
67 // Initialize timers
68 m_showToolTipTimer = new QTimer(this);
69 m_showToolTipTimer->setSingleShot(true);
70 m_showToolTipTimer->setInterval(500);
71 connect(m_showToolTipTimer, SIGNAL(timeout()), this, SLOT(showToolTip()));
72
73 m_contentRetrievalTimer = new QTimer(this);
74 m_contentRetrievalTimer->setSingleShot(true);
75 m_contentRetrievalTimer->setInterval(200);
76 connect(m_contentRetrievalTimer, SIGNAL(timeout()), this, SLOT(startContentRetrieval()));
77
78 Q_ASSERT(m_contentRetrievalTimer->interval() < m_showToolTipTimer->interval());
79
80 // When the mousewheel is used, the items don't get a hovered indication
81 // (Qt-issue #200665). To assure that the tooltip still gets hidden,
82 // the scrollbars are observed.
83 connect(parent->horizontalScrollBar(), SIGNAL(valueChanged(int)),
84 this, SLOT(hideToolTip()));
85 connect(parent->verticalScrollBar(), SIGNAL(valueChanged(int)),
86 this, SLOT(hideToolTip()));
87
88 m_view->viewport()->installEventFilter(this);
89 m_view->installEventFilter(this);
90 }
91
92 ToolTipManager::~ToolTipManager()
93 {
94 }
95
96 void ToolTipManager::hideToolTip()
97 {
98 if (m_appliedWaitCursor) {
99 QApplication::restoreOverrideCursor();
100 m_appliedWaitCursor = false;
101 }
102
103 m_toolTipRequested = false;
104 m_metaDataRequested = false;
105 m_showToolTipTimer->stop();
106 m_contentRetrievalTimer->stop();
107
108 m_fileMetaDataToolTip->setItems(KFileItemList());
109 m_fileMetaDataToolTip->hide();
110 }
111
112 bool ToolTipManager::eventFilter(QObject* watched, QEvent* event)
113 {
114 if (watched == m_view->viewport()) {
115 switch (event->type()) {
116 case QEvent::Leave:
117 case QEvent::MouseButtonPress:
118 hideToolTip();
119 break;
120 default:
121 break;
122 }
123 } else if ((watched == m_view) && (event->type() == QEvent::KeyPress)) {
124 hideToolTip();
125 }
126
127 return QObject::eventFilter(watched, event);
128 }
129
130 void ToolTipManager::requestToolTip(const QModelIndex& index)
131 {
132 hideToolTip();
133
134 // Only request a tooltip for the name column and when no selection or
135 // drag & drop operation is done (indicated by the left mouse button)
136 if ((index.column() == DolphinModel::Name) && !(QApplication::mouseButtons() & Qt::LeftButton)) {
137 m_itemRect = m_view->visualRect(index);
138 const QPoint pos = m_view->viewport()->mapToGlobal(m_itemRect.topLeft());
139 m_itemRect.moveTo(pos);
140
141 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
142 m_item = m_dolphinModel->itemForIndex(dirIndex);
143
144 // Only start the retrieving of the content, when the mouse has been over this
145 // item for 200 milliseconds. This prevents a lot of useless preview jobs and
146 // meta data retrieval, when passing rapidly over a lot of items.
147 m_contentRetrievalTimer->start();
148 m_showToolTipTimer->start();
149 m_toolTipRequested = true;
150 Q_ASSERT(!m_metaDataRequested);
151 }
152 }
153
154 void ToolTipManager::startContentRetrieval()
155 {
156 if (!m_toolTipRequested) {
157 return;
158 }
159
160 m_fileMetaDataToolTip->setName(m_item.text());
161
162 // Request the retrieval of meta-data. The slot
163 // slotMetaDataRequestFinished() is invoked after the
164 // meta-data have been received.
165 m_metaDataRequested = true;
166 m_fileMetaDataToolTip->setItems(KFileItemList() << m_item);
167
168 // Request a preview of the item
169 m_fileMetaDataToolTip->setPreview(QPixmap());
170
171 KIO::PreviewJob* job = KIO::filePreview(KFileItemList() << m_item, 256, 256);
172
173 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
174 this, SLOT(setPreviewPix(const KFileItem&, const QPixmap&)));
175 connect(job, SIGNAL(failed(const KFileItem&)),
176 this, SLOT(previewFailed()));
177 }
178
179
180 void ToolTipManager::setPreviewPix(const KFileItem& item,
181 const QPixmap& pixmap)
182 {
183 if (!m_toolTipRequested || (m_item.url() != item.url())) {
184 // No tooltip is requested anymore or an old preview has been received
185 return;
186 }
187
188 if (pixmap.isNull()) {
189 previewFailed();
190 } else {
191 m_fileMetaDataToolTip->setPreview(pixmap);
192 if (!m_showToolTipTimer->isActive()) {
193 showToolTip();
194 }
195 }
196 }
197
198 void ToolTipManager::previewFailed()
199 {
200 if (!m_toolTipRequested) {
201 return;
202 }
203
204 const QPixmap pixmap = KIcon(m_item.iconName()).pixmap(128, 128);
205 m_fileMetaDataToolTip->setPreview(pixmap);
206 if (!m_showToolTipTimer->isActive()) {
207 showToolTip();
208 }
209 }
210
211 void ToolTipManager::slotMetaDataRequestFinished()
212 {
213 if (!m_toolTipRequested) {
214 return;
215 }
216
217 m_metaDataRequested = false;
218 if (!m_showToolTipTimer->isActive()) {
219 showToolTip();
220 }
221 }
222
223 void ToolTipManager::showToolTip()
224 {
225 Q_ASSERT(m_toolTipRequested);
226 if (m_appliedWaitCursor) {
227 QApplication::restoreOverrideCursor();
228 m_appliedWaitCursor = false;
229 }
230
231 if (QApplication::mouseButtons() & Qt::LeftButton) {
232 return;
233 }
234
235 if (m_fileMetaDataToolTip->preview().isNull() || m_metaDataRequested) {
236 Q_ASSERT(!m_appliedWaitCursor);
237 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
238 m_appliedWaitCursor = true;
239 return;
240 }
241
242 const QRect screen = QApplication::desktop()->screenGeometry(QCursor::pos());
243
244 // Restrict tooltip size to current screen size when needed.
245 // Because layout controlling widget doesn't respect widget's maximumSize property
246 // (correct me if I'm wrong), we need to let layout do its work, then manually change
247 // geometry if resulting widget doesn't fit the screen.
248
249 // Step #1 - make sizeHint return calculated tooltip size
250 m_fileMetaDataToolTip->layout()->setSizeConstraint(QLayout::SetFixedSize);
251 m_fileMetaDataToolTip->adjustSize();
252 QSize size = m_fileMetaDataToolTip->sizeHint();
253
254 // Step #2 - correct tooltip size when needed
255 if (size.width() > screen.width()) {
256 size.setWidth(screen.width());
257 }
258 if (size.height() > screen.height()) {
259 size.setHeight(screen.height());
260 }
261
262 // m_itemRect defines the area of the item, where the tooltip should be
263 // shown. Per default the tooltip is shown in the bottom right corner.
264 // It must be assured that:
265 // - the content is fully visible
266 // - the content is not drawn inside m_itemRect
267 const bool hasRoomToLeft = (m_itemRect.left() - size.width() >= screen.left());
268 const bool hasRoomToRight = (m_itemRect.right() + size.width() <= screen.right());
269 const bool hasRoomAbove = (m_itemRect.top() - size.height() >= screen.top());
270 const bool hasRoomBelow = (m_itemRect.bottom() + size.height() <= screen.bottom());
271 if (!hasRoomAbove && !hasRoomBelow && !hasRoomToLeft && !hasRoomToRight) {
272 return;
273 }
274
275 int x, y;
276 if (hasRoomBelow || hasRoomAbove) {
277 x = QCursor::pos().x() + 16; // TODO: use mouse pointer width instead of the magic value of 16
278 if (x + size.width() >= screen.right()) {
279 x = screen.right() - size.width() + 1;
280 }
281 if (hasRoomBelow) {
282 y = m_itemRect.bottom() + 1;
283 } else {
284 y = m_itemRect.top() - size.height();
285 }
286 } else {
287 Q_ASSERT(hasRoomToLeft || hasRoomToRight);
288 if (hasRoomToRight) {
289 x = m_itemRect.right() + 1;
290 } else {
291 x = m_itemRect.left() - size.width();
292 }
293 // Put the tooltip at the bottom of the screen. The x-coordinate has already
294 // been adjusted, so that no overlapping with m_itemRect occurs.
295 y = screen.bottom() - size.height() + 1;
296 }
297
298 // Step #3 - Alter tooltip geometry
299 m_fileMetaDataToolTip->setFixedSize(size);
300 m_fileMetaDataToolTip->layout()->setSizeConstraint(QLayout::SetNoConstraint);
301 m_fileMetaDataToolTip->setGeometry(x, y, size.width(), size.height());
302 m_fileMetaDataToolTip->show();
303
304 m_toolTipRequested = false;
305 }
306
307 #include "tooltipmanager.moc"