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"
23 #include <KConfigGroup>
25 #include <KIO/PreviewJob>
26 #include <KSharedConfig>
28 #include <QApplication>
29 #include <QDesktopWidget>
30 #include <QScrollArea>
34 #include <views/dolphinmodel.h>
35 #include <views/dolphinsortfilterproxymodel.h>
37 ToolTipManager::ToolTipManager(QAbstractItemView
* parent
,
38 DolphinSortFilterProxyModel
* 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),
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
60 m_fileMetaDataToolTip
= sharedToolTip
;
61 connect(m_fileMetaDataToolTip
, SIGNAL(metaDataRequestFinished(KFileItemList
)),
62 this, SLOT(slotMetaDataRequestFinished()));
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()));
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()));
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()));
81 Q_ASSERT(m_contentRetrievalTimer
->interval() < m_showToolTipTimer
->interval());
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()));
91 m_view
->viewport()->installEventFilter(this);
92 m_view
->installEventFilter(this);
95 ToolTipManager::~ToolTipManager()
99 void ToolTipManager::hideToolTip()
101 if (m_appliedWaitCursor
) {
102 QApplication::restoreOverrideCursor();
103 m_appliedWaitCursor
= false;
106 m_toolTipRequested
= false;
107 m_metaDataRequested
= false;
108 m_showToolTipTimer
->stop();
109 m_contentRetrievalTimer
->stop();
111 m_fileMetaDataToolTip
->setItems(KFileItemList());
112 m_fileMetaDataToolTip
->hide();
115 bool ToolTipManager::eventFilter(QObject
* watched
, QEvent
* event
)
117 if (watched
== m_view
->viewport()) {
118 switch (event
->type()) {
120 case QEvent::MouseButtonPress
:
126 } else if ((watched
== m_view
) && (event
->type() == QEvent::KeyPress
)) {
130 return QObject::eventFilter(watched
, event
);
133 void ToolTipManager::requestToolTip(const QModelIndex
& index
)
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
);
144 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
145 m_item
= m_dolphinModel
->itemForIndex(dirIndex
);
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
);
157 void ToolTipManager::startContentRetrieval()
159 if (!m_toolTipRequested
) {
163 m_fileMetaDataToolTip
->setName(m_item
.text());
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
);
171 // Request a preview of the item
172 m_fileMetaDataToolTip
->setPreview(QPixmap());
174 if (m_enabledPlugins
.isEmpty()) {
175 const KConfigGroup
globalConfig(KGlobal::config(), "PreviewSettings");
176 m_enabledPlugins
= globalConfig
.readEntry("Plugins", QStringList()
177 << "directorythumbnail"
181 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << m_item
, 256, 256, true, true, &m_enabledPlugins
);
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()));
190 void ToolTipManager::setPreviewPix(const KFileItem
& item
,
191 const QPixmap
& pixmap
)
193 if (!m_toolTipRequested
|| (m_item
.url() != item
.url())) {
194 // No tooltip is requested anymore or an old preview has been received
198 if (pixmap
.isNull()) {
201 m_fileMetaDataToolTip
->setPreview(pixmap
);
202 if (!m_showToolTipTimer
->isActive()) {
208 void ToolTipManager::previewFailed()
210 if (!m_toolTipRequested
) {
214 const QPixmap pixmap
= KIcon(m_item
.iconName()).pixmap(128, 128);
215 m_fileMetaDataToolTip
->setPreview(pixmap
);
216 if (!m_showToolTipTimer
->isActive()) {
221 void ToolTipManager::slotMetaDataRequestFinished()
223 if (!m_toolTipRequested
) {
227 m_metaDataRequested
= false;
228 if (!m_showToolTipTimer
->isActive()) {
233 void ToolTipManager::showToolTip()
235 Q_ASSERT(m_toolTipRequested
);
236 if (m_appliedWaitCursor
) {
237 QApplication::restoreOverrideCursor();
238 m_appliedWaitCursor
= false;
241 if (QApplication::mouseButtons() & Qt::LeftButton
) {
245 if (m_fileMetaDataToolTip
->preview().isNull() || m_metaDataRequested
) {
246 Q_ASSERT(!m_appliedWaitCursor
);
247 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor
));
248 m_appliedWaitCursor
= true;
252 const QRect screen
= QApplication::desktop()->screenGeometry(QCursor::pos());
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.
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();
264 // Step #2 - correct tooltip size when needed
265 if (size
.width() > screen
.width()) {
266 size
.setWidth(screen
.width());
268 if (size
.height() > screen
.height()) {
269 size
.setHeight(screen
.height());
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
) {
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;
292 y
= m_itemRect
.bottom() + 1;
294 y
= m_itemRect
.top() - size
.height();
297 Q_ASSERT(hasRoomToLeft
|| hasRoomToRight
);
298 if (hasRoomToRight
) {
299 x
= m_itemRect
.right() + 1;
301 x
= m_itemRect
.left() - size
.width();
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;
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();
314 m_toolTipRequested
= false;
317 #include "tooltipmanager.moc"