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