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