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