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