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