]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/tooltips/tooltipmanager.cpp
Fix directory navigation in Dolphin::Terminal.
[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 bool ToolTipManager::eventFilter(QObject* watched, QEvent* event)
104 {
105 if (watched == m_view->viewport()) {
106 switch (event->type()) {
107 case QEvent::Leave:
108 case QEvent::MouseButtonPress:
109 hideToolTip();
110 break;
111 default:
112 break;
113 }
114 } else if ((watched == m_view) && (event->type() == QEvent::KeyPress)) {
115 hideToolTip();
116 }
117
118 return QObject::eventFilter(watched, event);
119 }
120
121 void ToolTipManager::requestToolTip(const QModelIndex& index)
122 {
123 hideToolTip();
124
125 // Only request a tooltip for the name column and when no selection or
126 // drag & drop operation is done (indicated by the left mouse button)
127 if ((index.column() == DolphinModel::Name) && !(QApplication::mouseButtons() & Qt::LeftButton)) {
128 m_itemRect = m_view->visualRect(index);
129 const QPoint pos = m_view->viewport()->mapToGlobal(m_itemRect.topLeft());
130 m_itemRect.moveTo(pos);
131
132 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
133 m_item = m_dolphinModel->itemForIndex(dirIndex);
134
135 // Only start the retrieving of the content, when the mouse has been over this
136 // item for 200 milliseconds. This prevents a lot of useless preview jobs and
137 // meta data retrieval, when passing rapidly over a lot of items.
138 Q_ASSERT(!m_fileMetaDataToolTip);
139 m_fileMetaDataToolTip = new FileMetaDataToolTip(m_view);
140 connect(m_fileMetaDataToolTip, SIGNAL(metaDataRequestFinished(KFileItemList)),
141 this, SLOT(slotMetaDataRequestFinished()));
142
143 m_contentRetrievalTimer->start();
144 m_showToolTipTimer->start();
145 m_toolTipRequested = true;
146 Q_ASSERT(!m_metaDataRequested);
147 }
148 }
149
150 void ToolTipManager::startContentRetrieval()
151 {
152 if (!m_toolTipRequested) {
153 return;
154 }
155
156 m_fileMetaDataToolTip->setName(m_item.text());
157
158 // Request the retrieval of meta-data. The slot
159 // slotMetaDataRequestFinished() is invoked after the
160 // meta-data have been received.
161 m_metaDataRequested = true;
162 m_fileMetaDataToolTip->setItems(KFileItemList() << m_item);
163
164 // Request a preview of the item
165 m_fileMetaDataToolTip->setPreview(QPixmap());
166
167 KIO::PreviewJob* job = KIO::filePreview(KFileItemList() << m_item, QSize(256, 256));
168
169 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
170 this, SLOT(setPreviewPix(const KFileItem&, const QPixmap&)));
171 connect(job, SIGNAL(failed(const KFileItem&)),
172 this, SLOT(previewFailed()));
173 }
174
175
176 void ToolTipManager::setPreviewPix(const KFileItem& item,
177 const QPixmap& pixmap)
178 {
179 if (!m_toolTipRequested || (m_item.url() != item.url())) {
180 // No tooltip is requested anymore or an old preview has been received
181 return;
182 }
183
184 if (pixmap.isNull()) {
185 previewFailed();
186 } else {
187 m_fileMetaDataToolTip->setPreview(pixmap);
188 if (!m_showToolTipTimer->isActive()) {
189 showToolTip();
190 }
191 }
192 }
193
194 void ToolTipManager::previewFailed()
195 {
196 if (!m_toolTipRequested) {
197 return;
198 }
199
200 const QPixmap pixmap = KIcon(m_item.iconName()).pixmap(128, 128);
201 m_fileMetaDataToolTip->setPreview(pixmap);
202 if (!m_showToolTipTimer->isActive()) {
203 showToolTip();
204 }
205 }
206
207 void ToolTipManager::slotMetaDataRequestFinished()
208 {
209 if (!m_toolTipRequested) {
210 return;
211 }
212
213 m_metaDataRequested = false;
214 if (!m_showToolTipTimer->isActive()) {
215 showToolTip();
216 }
217 }
218
219 void ToolTipManager::showToolTip()
220 {
221 Q_ASSERT(m_toolTipRequested);
222 if (m_appliedWaitCursor) {
223 QApplication::restoreOverrideCursor();
224 m_appliedWaitCursor = false;
225 }
226
227 if (QApplication::mouseButtons() & Qt::LeftButton) {
228 return;
229 }
230
231 if (m_fileMetaDataToolTip->preview().isNull() || m_metaDataRequested) {
232 Q_ASSERT(!m_appliedWaitCursor);
233 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
234 m_appliedWaitCursor = true;
235 return;
236 }
237
238 const QRect screen = QApplication::desktop()->screenGeometry(QCursor::pos());
239
240 // Restrict tooltip size to current screen size when needed.
241 // Because layout controlling widget doesn't respect widget's maximumSize property
242 // (correct me if I'm wrong), we need to let layout do its work, then manually change
243 // geometry if resulting widget doesn't fit the screen.
244
245 // Step #1 - make sizeHint return calculated tooltip size
246 m_fileMetaDataToolTip->layout()->setSizeConstraint(QLayout::SetFixedSize);
247 m_fileMetaDataToolTip->adjustSize();
248 QSize size = m_fileMetaDataToolTip->sizeHint();
249
250 // Step #2 - correct tooltip size when needed
251 if (size.width() > screen.width()) {
252 size.setWidth(screen.width());
253 }
254 if (size.height() > screen.height()) {
255 size.setHeight(screen.height());
256 }
257
258 // m_itemRect defines the area of the item, where the tooltip should be
259 // shown. Per default the tooltip is shown in the bottom right corner.
260 // It must be assured that:
261 // - the content is fully visible
262 // - the content is not drawn inside m_itemRect
263 const bool hasRoomToLeft = (m_itemRect.left() - size.width() >= screen.left());
264 const bool hasRoomToRight = (m_itemRect.right() + size.width() <= screen.right());
265 const bool hasRoomAbove = (m_itemRect.top() - size.height() >= screen.top());
266 const bool hasRoomBelow = (m_itemRect.bottom() + size.height() <= screen.bottom());
267 if (!hasRoomAbove && !hasRoomBelow && !hasRoomToLeft && !hasRoomToRight) {
268 return;
269 }
270
271 int x, y;
272 if (hasRoomBelow || hasRoomAbove) {
273 x = QCursor::pos().x() + 16; // TODO: use mouse pointer width instead of the magic value of 16
274 if (x + size.width() >= screen.right()) {
275 x = screen.right() - size.width() + 1;
276 }
277 if (hasRoomBelow) {
278 y = m_itemRect.bottom() + 1;
279 } else {
280 y = m_itemRect.top() - size.height();
281 }
282 } else {
283 Q_ASSERT(hasRoomToLeft || hasRoomToRight);
284 if (hasRoomToRight) {
285 x = m_itemRect.right() + 1;
286 } else {
287 x = m_itemRect.left() - size.width();
288 }
289 // Put the tooltip at the bottom of the screen. The x-coordinate has already
290 // been adjusted, so that no overlapping with m_itemRect occurs.
291 y = screen.bottom() - size.height() + 1;
292 }
293
294 // Step #3 - Alter tooltip geometry
295 m_fileMetaDataToolTip->setFixedSize(size);
296 m_fileMetaDataToolTip->layout()->setSizeConstraint(QLayout::SetNoConstraint);
297 m_fileMetaDataToolTip->setGeometry(x, y, size.width(), size.height());
298 m_fileMetaDataToolTip->show();
299
300 m_toolTipRequested = false;
301 }
302
303 #include "tooltipmanager.moc"