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.h>
25 #include <kwindowsystem.h>
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_prepareToolTipTimer(0),
43 m_startPreviewJobTimer(0),
44 m_waitOnPreviewTimer(0),
45 m_showToolTipDelayedTimer(0),
46 m_fileMetaDataToolTip(0),
49 m_generatingPreview(false),
50 m_hasDefaultIcon(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
;
62 m_dolphinModel
= static_cast<DolphinModel
*>(m_proxyModel
->sourceModel());
63 connect(parent
, SIGNAL(entered(const QModelIndex
&)),
64 this, SLOT(requestToolTip(const QModelIndex
&)));
65 connect(parent
, SIGNAL(viewportEntered()),
66 this, SLOT(hideToolTip()));
69 m_prepareToolTipTimer
= new QTimer(this);
70 m_prepareToolTipTimer
->setSingleShot(true);
71 m_prepareToolTipTimer
->setInterval(500);
72 connect(m_prepareToolTipTimer
, SIGNAL(timeout()), this, SLOT(prepareToolTip()));
74 m_startPreviewJobTimer
= new QTimer(this);
75 m_startPreviewJobTimer
->setSingleShot(true);
76 m_startPreviewJobTimer
->setInterval(200);
77 connect(m_startPreviewJobTimer
, SIGNAL(timeout()), this, SLOT(startPreviewJob()));
79 m_waitOnPreviewTimer
= new QTimer(this);
80 m_waitOnPreviewTimer
->setSingleShot(true);
81 m_waitOnPreviewTimer
->setInterval(250);
82 connect(m_waitOnPreviewTimer
, SIGNAL(timeout()), this, SLOT(prepareToolTip()));
84 m_showToolTipDelayedTimer
= new QTimer(this);
85 m_showToolTipDelayedTimer
->setSingleShot(true);
86 m_showToolTipDelayedTimer
->setInterval(100);
87 connect(m_showToolTipDelayedTimer
, SIGNAL(timeout()), this, SLOT(showToolTip()));
89 // When the mousewheel is used, the items don't get a hovered indication
90 // (Qt-issue #200665). To assure that the tooltip still gets hidden,
91 // the scrollbars are observed.
92 connect(parent
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
93 this, SLOT(hideTip()));
94 connect(parent
->verticalScrollBar(), SIGNAL(valueChanged(int)),
95 this, SLOT(hideTip()));
97 m_view
->viewport()->installEventFilter(this);
98 m_view
->installEventFilter(this);
101 ToolTipManager::~ToolTipManager()
105 void ToolTipManager::hideTip()
110 bool ToolTipManager::eventFilter(QObject
* watched
, QEvent
* event
)
112 if (watched
== m_view
->viewport()) {
113 switch (event
->type()) {
115 case QEvent::MouseButtonPress
:
121 } else if ((watched
== m_view
) && (event
->type() == QEvent::KeyPress
)) {
125 return QObject::eventFilter(watched
, event
);
128 void ToolTipManager::requestToolTip(const QModelIndex
& index
)
132 // Only request a tooltip for the name column and when no selection or
133 // drag & drop operation is done (indicated by the left mouse button)
134 if ((index
.column() == DolphinModel::Name
) && !(QApplication::mouseButtons() & Qt::LeftButton
)) {
135 m_itemRect
= m_view
->visualRect(index
);
136 const QPoint pos
= m_view
->viewport()->mapToGlobal(m_itemRect
.topLeft());
137 m_itemRect
.moveTo(pos
);
139 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
140 m_item
= m_dolphinModel
->itemForIndex(dirIndex
);
142 // Only start the previewJob when the mouse has been over this item for 200 milliseconds.
143 // This prevents a lot of useless preview jobs when passing rapidly over a lot of items.
144 m_startPreviewJobTimer
->start();
145 m_previewPixmap
= QPixmap();
146 m_hasDefaultIcon
= false;
148 m_prepareToolTipTimer
->start();
152 void ToolTipManager::hideToolTip()
154 m_prepareToolTipTimer
->stop();
155 m_startPreviewJobTimer
->stop();
156 m_waitOnPreviewTimer
->stop();
157 m_showToolTipDelayedTimer
->stop();
159 m_fileMetaDataToolTip
->hide();
162 void ToolTipManager::prepareToolTip()
164 if (m_generatingPreview
) {
165 m_waitOnPreviewTimer
->start();
168 if (!m_previewPixmap
.isNull()) {
169 showToolTipDelayed(m_previewPixmap
);
170 } else if (!m_hasDefaultIcon
) {
171 const QPixmap
image(KIcon(m_item
.iconName()).pixmap(128, 128));
172 showToolTipDelayed(image
);
173 m_hasDefaultIcon
= true;
177 void ToolTipManager::startPreviewJob()
179 m_generatingPreview
= true;
180 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << m_item
, 256, 256);
182 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
183 this, SLOT(setPreviewPix(const KFileItem
&, const QPixmap
&)));
184 connect(job
, SIGNAL(failed(const KFileItem
&)),
185 this, SLOT(previewFailed()));
189 void ToolTipManager::setPreviewPix(const KFileItem
& item
,
190 const QPixmap
& pixmap
)
192 if ((m_item
.url() != item
.url()) || pixmap
.isNull()) {
193 // An old preview or an invalid preview has been received
196 m_previewPixmap
= pixmap
;
197 m_generatingPreview
= false;
201 void ToolTipManager::previewFailed()
203 m_generatingPreview
= false;
206 void ToolTipManager::showToolTip()
208 const QRect desktop
= QApplication::desktop()->screenGeometry(m_itemRect
.bottomRight());
209 const QSize size
= m_fileMetaDataToolTip
->sizeHint();
211 // m_itemRect defines the area of the item, where the tooltip should be
212 // shown. Per default the tooltip is shown in the bottom right corner.
213 // If the tooltip content exceeds the desktop borders, it must be assured that:
214 // - the content is fully visible
215 // - the content is not drawn inside m_itemRect
216 const bool hasRoomToLeft
= (m_itemRect
.left() - size
.width() >= desktop
.left());
217 const bool hasRoomToRight
= (m_itemRect
.right() + size
.width() <= desktop
.right());
218 const bool hasRoomAbove
= (m_itemRect
.top() - size
.height() >= desktop
.top());
219 const bool hasRoomBelow
= (m_itemRect
.bottom() + size
.height() <= desktop
.bottom());
220 if (!hasRoomAbove
&& !hasRoomBelow
&& !hasRoomToLeft
&& !hasRoomToRight
) {
226 if (hasRoomBelow
|| hasRoomAbove
) {
227 x
= QCursor::pos().x() + 16; // TODO: use mouse pointer width instead of the magic value of 16
228 if (x
+ size
.width() >= desktop
.right()) {
229 x
= desktop
.right() - size
.width();
232 y
= m_itemRect
.bottom() + 1;
234 y
= m_itemRect
.top() - size
.height();
237 Q_ASSERT(hasRoomToLeft
|| hasRoomToRight
);
238 if (hasRoomToRight
) {
239 x
= m_itemRect
.right() + 1;
241 x
= m_itemRect
.left() - size
.width();
244 // Put the tooltip at the bottom of the screen. The x-coordinate has already
245 // been adjusted, so that no overlapping with m_itemRect occurs.
246 y
= desktop
.bottom() - size
.height();
249 m_fileMetaDataToolTip
->move(qMax(x
, 0), qMax(y
, 0));
250 m_fileMetaDataToolTip
->show();
253 void ToolTipManager::showToolTipDelayed(const QPixmap
& pixmap
)
255 if (QApplication::mouseButtons() & Qt::LeftButton
) {
259 m_fileMetaDataToolTip
->setPreview(pixmap
);
260 m_fileMetaDataToolTip
->setName(m_item
.text());
261 m_fileMetaDataToolTip
->setItems(KFileItemList() << m_item
);
263 // Because one tooltip instance is reused, the size hint always depends on the previous
264 // content (QWidgets don't update their layout geometry if they are invisible). To
265 // assure having a consistent size without relayout flickering, the tooltip is opened
266 // on an invisible position first. This gives the layout system some time to asynchronously
267 // update the content. Sadly this only works with compositing enabled.
268 if (KWindowSystem::compositingActive()) {
269 const QRect desktop
= QApplication::desktop()->screenGeometry(m_itemRect
.bottomRight());
270 m_fileMetaDataToolTip
->move(desktop
.bottomRight());
271 m_fileMetaDataToolTip
->show();
274 m_showToolTipDelayedTimer
->start(); // Calls ToolTipManager::showToolTip()
277 #include "tooltipmanager.moc"