1 /*******************************************************************************
2 * Copyright (C) 2008 by Konstantin Heil <konst.heil@stud.uni-heidelberg.de> *
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. *
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. *
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 *******************************************************************************/
20 #include "tooltipmanager.h"
22 #include "filemetadatatooltip.h"
24 #include <KIO/PreviewJob>
25 #include <KSharedConfig>
27 #include <QApplication>
28 #include <QDesktopWidget>
29 #include <QScrollArea>
33 #include <views/dolphinmodel.h>
34 #include <views/dolphinsortfilterproxymodel.h>
36 ToolTipManager::ToolTipManager(QAbstractItemView
* parent
,
37 DolphinSortFilterProxyModel
* 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),
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()));
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()));
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()));
68 Q_ASSERT(m_contentRetrievalTimer
->interval() < m_showToolTipTimer
->interval());
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()));
79 m_view
->viewport()->installEventFilter(this);
80 m_view
->installEventFilter(this);
83 ToolTipManager::~ToolTipManager()
87 void ToolTipManager::hideToolTip()
89 if (m_appliedWaitCursor
) {
90 QApplication::restoreOverrideCursor();
91 m_appliedWaitCursor
= false;
94 m_toolTipRequested
= false;
95 m_metaDataRequested
= false;
96 m_showToolTipTimer
->stop();
97 m_contentRetrievalTimer
->stop();
99 delete m_fileMetaDataToolTip
;
100 m_fileMetaDataToolTip
= 0;
104 bool ToolTipManager::eventFilter(QObject
* watched
, QEvent
* event
)
106 if (watched
== m_view
->viewport()) {
107 switch (event
->type()) {
109 case QEvent::MouseButtonPress
:
115 } else if ((watched
== m_view
) && (event
->type() == QEvent::KeyPress
)) {
119 return QObject::eventFilter(watched
, event
);
122 void ToolTipManager::requestToolTip(const QModelIndex
& index
)
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
);
133 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
134 m_item
= m_dolphinModel
->itemForIndex(dirIndex
);
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()));
144 m_contentRetrievalTimer
->start();
145 m_showToolTipTimer
->start();
146 m_toolTipRequested
= true;
147 Q_ASSERT(!m_metaDataRequested
);
151 void ToolTipManager::startContentRetrieval()
153 if (!m_toolTipRequested
) {
157 m_fileMetaDataToolTip
->setName(m_item
.text());
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();
166 // Request a preview of the item
167 m_fileMetaDataToolTip
->setPreview(QPixmap());
169 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << m_item
, QSize(256, 256));
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()));
178 void ToolTipManager::setPreviewPix(const KFileItem
& item
,
179 const QPixmap
& pixmap
)
181 if (!m_toolTipRequested
|| (m_item
.url() != item
.url())) {
182 // No tooltip is requested anymore or an old preview has been received
186 if (pixmap
.isNull()) {
189 m_fileMetaDataToolTip
->setPreview(pixmap
);
190 if (!m_showToolTipTimer
->isActive()) {
196 void ToolTipManager::previewFailed()
198 if (!m_toolTipRequested
) {
202 const QPixmap pixmap
= KIcon(m_item
.iconName()).pixmap(128, 128);
203 m_fileMetaDataToolTip
->setPreview(pixmap
);
204 if (!m_showToolTipTimer
->isActive()) {
209 void ToolTipManager::slotMetaDataRequestFinished()
211 if (!m_toolTipRequested
) {
215 m_metaDataRequested
= false;
217 if (!m_showToolTipTimer
->isActive()) {
222 void ToolTipManager::showToolTip()
224 Q_ASSERT(m_toolTipRequested
);
225 if (m_appliedWaitCursor
) {
226 QApplication::restoreOverrideCursor();
227 m_appliedWaitCursor
= false;
230 if (QApplication::mouseButtons() & Qt::LeftButton
) {
234 if (m_fileMetaDataToolTip
->preview().isNull() || m_metaDataRequested
) {
235 Q_ASSERT(!m_appliedWaitCursor
);
236 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor
));
237 m_appliedWaitCursor
= true;
241 const QRect screen
= QApplication::desktop()->screenGeometry(QCursor::pos());
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.
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();
253 // Step #2 - correct tooltip size when needed
254 if (size
.width() > screen
.width()) {
255 size
.setWidth(screen
.width());
257 if (size
.height() > screen
.height()) {
258 size
.setHeight(screen
.height());
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
) {
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;
282 y
= m_itemRect
.bottom() + margin
;
284 y
= m_itemRect
.top() - size
.height() - margin
;
287 Q_ASSERT(hasRoomToLeft
|| hasRoomToRight
);
288 if (hasRoomToRight
) {
289 x
= m_itemRect
.right() + margin
;
291 x
= m_itemRect
.left() - size
.width() - margin
;
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;
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();
304 m_toolTipRequested
= false;
307 #include "tooltipmanager.moc"