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 "dolphinmodel.h"
23 #include "dolphinsortfilterproxymodel.h"
25 #include "filemetadatatooltip.h"
27 #include <kio/previewjob.h>
29 #include <QApplication>
30 #include <QDesktopWidget>
31 #include <QScrollArea>
35 ToolTipManager::ToolTipManager(QAbstractItemView
* parent
,
36 DolphinSortFilterProxyModel
* model
) :
43 m_waitOnPreviewTimer(0),
44 m_fileMetaDataToolTip(0),
47 m_generatingPreview(false),
48 m_hasDefaultIcon(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()));
57 m_timer
= new QTimer(this);
58 m_timer
->setSingleShot(true);
59 connect(m_timer
, SIGNAL(timeout()),
60 this, SLOT(prepareToolTip()));
62 m_previewTimer
= new QTimer(this);
63 m_previewTimer
->setSingleShot(true);
64 connect(m_previewTimer
, SIGNAL(timeout()),
65 this, SLOT(startPreviewJob()));
67 m_waitOnPreviewTimer
= new QTimer(this);
68 m_waitOnPreviewTimer
->setSingleShot(true);
69 connect(m_waitOnPreviewTimer
, SIGNAL(timeout()),
70 this, SLOT(prepareToolTip()));
72 // When the mousewheel is used, the items don't get a hovered indication
73 // (Qt-issue #200665). To assure that the tooltip still gets hidden,
74 // the scrollbars are observed.
75 connect(parent
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
76 this, SLOT(hideTip()));
77 connect(parent
->verticalScrollBar(), SIGNAL(valueChanged(int)),
78 this, SLOT(hideTip()));
80 m_view
->viewport()->installEventFilter(this);
81 m_view
->installEventFilter(this);
83 m_fileMetaDataToolTip
= new FileMetaDataToolTip(parent
);
86 ToolTipManager::~ToolTipManager()
90 void ToolTipManager::hideTip()
95 bool ToolTipManager::eventFilter(QObject
* watched
, QEvent
* event
)
97 if (watched
== m_view
->viewport()) {
98 switch (event
->type()) {
100 case QEvent::MouseButtonPress
:
106 } else if ((watched
== m_view
) && (event
->type() == QEvent::KeyPress
)) {
110 return QObject::eventFilter(watched
, event
);
113 void ToolTipManager::requestToolTip(const QModelIndex
& index
)
115 // Only request a tooltip for the name column and when no selection or
116 // drag & drop operation is done (indicated by the left mouse button)
117 if ((index
.column() == DolphinModel::Name
) && !(QApplication::mouseButtons() & Qt::LeftButton
)) {
118 m_waitOnPreviewTimer
->stop();
119 m_fileMetaDataToolTip
->hide();
121 m_itemRect
= m_view
->visualRect(index
);
122 const QPoint pos
= m_view
->viewport()->mapToGlobal(m_itemRect
.topLeft());
123 m_itemRect
.moveTo(pos
);
125 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
126 m_item
= m_dolphinModel
->itemForIndex(dirIndex
);
128 // Only start the previewJob when the mouse has been over this item for 200 milliseconds.
129 // This prevents a lot of useless preview jobs when passing rapidly over a lot of items.
130 m_previewTimer
->start(200);
131 m_previewPixmap
= QPixmap();
132 m_hasDefaultIcon
= false;
140 void ToolTipManager::hideToolTip()
143 m_previewTimer
->stop();
144 m_waitOnPreviewTimer
->stop();
146 m_fileMetaDataToolTip
->hide();
149 void ToolTipManager::prepareToolTip()
151 if (m_generatingPreview
) {
152 m_waitOnPreviewTimer
->start(250);
155 if (!m_previewPixmap
.isNull()) {
156 showToolTip(m_previewPixmap
);
157 } else if (!m_hasDefaultIcon
) {
158 const QPixmap
image(KIcon(m_item
.iconName()).pixmap(128, 128));
160 m_hasDefaultIcon
= true;
164 void ToolTipManager::startPreviewJob()
166 m_generatingPreview
= true;
167 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << m_item
, 256, 256);
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()));
176 void ToolTipManager::setPreviewPix(const KFileItem
& item
,
177 const QPixmap
& pixmap
)
179 if ((m_item
.url() != item
.url()) || pixmap
.isNull()) {
180 // An old preview or an invalid preview has been received
183 m_previewPixmap
= pixmap
;
184 m_generatingPreview
= false;
188 void ToolTipManager::previewFailed()
190 m_generatingPreview
= false;
194 void ToolTipManager::showToolTip(const QPixmap
& pixmap
)
196 if (QApplication::mouseButtons() & Qt::LeftButton
) {
200 m_fileMetaDataToolTip
->setPreview(pixmap
);
201 m_fileMetaDataToolTip
->setName(m_item
.text());
202 m_fileMetaDataToolTip
->setItems(KFileItemList() << m_item
);
204 // Calculate the x- and y-position of the tooltip
205 const QSize size
= m_fileMetaDataToolTip
->sizeHint();
206 const QRect desktop
= QApplication::desktop()->screenGeometry(m_itemRect
.bottomRight());
208 // m_itemRect defines the area of the item, where the tooltip should be
209 // shown. Per default the tooltip is shown in the bottom right corner.
210 // If the tooltip content exceeds the desktop borders, it must be assured that:
211 // - the content is fully visible
212 // - the content is not drawn inside m_itemRect
213 const bool hasRoomToLeft
= (m_itemRect
.left() - size
.width() >= desktop
.left());
214 const bool hasRoomToRight
= (m_itemRect
.right() + size
.width() <= desktop
.right());
215 const bool hasRoomAbove
= (m_itemRect
.top() - size
.height() >= desktop
.top());
216 const bool hasRoomBelow
= (m_itemRect
.bottom() + size
.height() <= desktop
.bottom());
217 if (!hasRoomAbove
&& !hasRoomBelow
&& !hasRoomToLeft
&& !hasRoomToRight
) {
223 if (hasRoomBelow
|| hasRoomAbove
) {
224 x
= QCursor::pos().x() + 16; // TODO: use mouse pointer width instead of the magic value of 16
225 if (x
+ size
.width() >= desktop
.right()) {
226 x
= desktop
.right() - size
.width();
228 y
= hasRoomBelow
? m_itemRect
.bottom() : m_itemRect
.top() - size
.height();
230 Q_ASSERT(hasRoomToLeft
|| hasRoomToRight
);
231 x
= hasRoomToRight
? m_itemRect
.right() : m_itemRect
.left() - size
.width();
233 // Put the tooltip at the bottom of the screen. The x-coordinate has already
234 // been adjusted, so that no overlapping with m_itemRect occurs.
235 y
= desktop
.bottom() - size
.height();
238 m_fileMetaDataToolTip
->move(x
, y
);
239 m_fileMetaDataToolTip
->show();
242 #include "tooltipmanager.moc"