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"
26 #include <kio/previewjob.h>
27 #include <kseparator.h>
29 #include "panels/information/kmetadatawidget.h"
30 #include "tooltips/ktooltip.h"
32 #include <QApplication>
33 #include <QDesktopWidget>
34 #include <QHBoxLayout>
36 #include <QScrollArea>
39 #include <QVBoxLayout>
41 ToolTipManager::ToolTipManager(QAbstractItemView
* parent
,
42 DolphinSortFilterProxyModel
* model
) :
49 m_waitOnPreviewTimer(0),
52 m_generatingPreview(false),
53 m_hasDefaultIcon(false),
56 m_dolphinModel
= static_cast<DolphinModel
*>(m_proxyModel
->sourceModel());
57 connect(parent
, SIGNAL(entered(const QModelIndex
&)),
58 this, SLOT(requestToolTip(const QModelIndex
&)));
59 connect(parent
, SIGNAL(viewportEntered()),
60 this, SLOT(hideToolTip()));
62 m_timer
= new QTimer(this);
63 m_timer
->setSingleShot(true);
64 connect(m_timer
, SIGNAL(timeout()),
65 this, SLOT(prepareToolTip()));
67 m_previewTimer
= new QTimer(this);
68 m_previewTimer
->setSingleShot(true);
69 connect(m_previewTimer
, SIGNAL(timeout()),
70 this, SLOT(startPreviewJob()));
72 m_waitOnPreviewTimer
= new QTimer(this);
73 m_waitOnPreviewTimer
->setSingleShot(true);
74 connect(m_waitOnPreviewTimer
, SIGNAL(timeout()),
75 this, SLOT(prepareToolTip()));
77 // When the mousewheel is used, the items don't get a hovered indication
78 // (Qt-issue #200665). To assure that the tooltip still gets hidden,
79 // the scrollbars are observed.
80 connect(parent
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
81 this, SLOT(hideTip()));
82 connect(parent
->verticalScrollBar(), SIGNAL(valueChanged(int)),
83 this, SLOT(hideTip()));
85 m_view
->viewport()->installEventFilter(this);
86 m_view
->installEventFilter(this);
89 ToolTipManager::~ToolTipManager()
93 void ToolTipManager::hideTip()
98 bool ToolTipManager::eventFilter(QObject
* watched
, QEvent
* event
)
100 if (watched
== m_view
->viewport()) {
101 switch (event
->type()) {
103 case QEvent::MouseButtonPress
:
109 } else if ((watched
== m_view
) && (event
->type() == QEvent::KeyPress
)) {
113 return QObject::eventFilter(watched
, event
);
116 void ToolTipManager::requestToolTip(const QModelIndex
& index
)
118 // only request a tooltip for the name column and when no selection or
119 // drag & drop operation is done (indicated by the left mouse button)
120 if ((index
.column() == DolphinModel::Name
) && !(QApplication::mouseButtons() & Qt::LeftButton
)) {
121 m_waitOnPreviewTimer
->stop();
124 m_itemRect
= m_view
->visualRect(index
);
125 const QPoint pos
= m_view
->viewport()->mapToGlobal(m_itemRect
.topLeft());
126 m_itemRect
.moveTo(pos
);
128 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
129 m_item
= m_dolphinModel
->itemForIndex(dirIndex
);
131 // only start the previewJob when the mouse has been over this item for 200 milliseconds,
132 // this prevents a lot of useless preview jobs when passing rapidly over a lot of items
133 m_previewTimer
->start(200);
134 m_previewPixmap
= QPixmap();
135 m_hasDefaultIcon
= false;
143 void ToolTipManager::hideToolTip()
146 m_previewTimer
->stop();
147 m_waitOnPreviewTimer
->stop();
151 void ToolTipManager::prepareToolTip()
153 if (m_generatingPreview
) {
154 m_waitOnPreviewTimer
->start(250);
157 if (!m_previewPixmap
.isNull()) {
158 showToolTip(m_previewPixmap
);
159 } else if (!m_hasDefaultIcon
) {
160 const QPixmap
image(KIcon(m_item
.iconName()).pixmap(128, 128));
162 m_hasDefaultIcon
= true;
166 void ToolTipManager::startPreviewJob()
168 m_generatingPreview
= true;
169 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << m_item
, 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_item
.url() != item
.url()) || pixmap
.isNull()) {
182 // an old preview or an invalid preview has been received
185 m_previewPixmap
= pixmap
;
186 m_generatingPreview
= false;
190 void ToolTipManager::previewFailed()
192 m_generatingPreview
= false;
196 void ToolTipManager::showToolTip(const QPixmap
& pixmap
)
198 if (QApplication::mouseButtons() & Qt::LeftButton
) {
202 QWidget
* tip
= createTipContent(pixmap
);
204 // calculate the x- and y-position of the tooltip
205 const QSize size
= tip
->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
) {
225 if (hasRoomBelow
|| hasRoomAbove
) {
226 x
= QCursor::pos().x() + 16; // TODO: use mouse pointer width instead of the magic value of 16
227 if (x
+ size
.width() >= desktop
.right()) {
228 x
= desktop
.right() - size
.width();
230 y
= hasRoomBelow
? m_itemRect
.bottom() : m_itemRect
.top() - size
.height();
232 Q_ASSERT(hasRoomToLeft
|| hasRoomToRight
);
233 x
= hasRoomToRight
? m_itemRect
.right() : m_itemRect
.left() - size
.width();
235 // Put the tooltip at the bottom of the screen. The x-coordinate has already
236 // been adjusted, so that no overlapping with m_itemRect occurs.
237 y
= desktop
.bottom() - size
.height();
240 // the ownership of tip is transferred to KToolTip
241 KToolTip::showTip(QPoint(x
, y
), tip
);
244 QWidget
* ToolTipManager::createTipContent(const QPixmap
& pixmap
) const
246 QWidget
* tipContent
= new QWidget();
249 QLabel
* pixmapLabel
= new QLabel(tipContent
);
250 pixmapLabel
->setPixmap(pixmap
);
251 pixmapLabel
->setFixedSize(pixmap
.size());
254 QLabel
* nameLabel
= new QLabel(tipContent
);
255 nameLabel
->setText(m_item
.name());
256 nameLabel
->setWordWrap(true);
257 QFont font
= nameLabel
->font();
259 nameLabel
->setFont(font
);
260 nameLabel
->setAlignment(Qt::AlignHCenter
);
261 nameLabel
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
264 KMetaDataWidget
* metaDataWidget
= new KMetaDataWidget(tipContent
);
265 metaDataWidget
->setForegroundRole(QPalette::ToolTipText
);
266 metaDataWidget
->setItem(m_item
);
267 metaDataWidget
->setSizePolicy(QSizePolicy::Fixed
, QSizePolicy::Fixed
);
268 metaDataWidget
->setReadOnly(true);
270 // the stretchwidget allows the metadata widget to be top aligned and fills
271 // the remaining vertical space
272 QWidget
* stretchWidget
= new QWidget(tipContent
);
273 stretchWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::MinimumExpanding
);
275 QWidget
* textContainer
= new QWidget(tipContent
);
276 QVBoxLayout
* textLayout
= new QVBoxLayout(textContainer
);
277 textLayout
->addWidget(nameLabel
);
278 textLayout
->addWidget(new KSeparator());
279 textLayout
->addWidget(metaDataWidget
);
280 textLayout
->addWidget(stretchWidget
);
282 QHBoxLayout
* tipLayout
= new QHBoxLayout(tipContent
);
283 tipLayout
->setMargin(0);
284 tipLayout
->addWidget(pixmapLabel
);
285 tipLayout
->addWidget(textContainer
);
290 #include "tooltipmanager.moc"