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 "dolphintooltip.h"
23 #include "dolphinmodel.h"
24 #include "dolphinsortfilterproxymodel.h"
27 #include <tooltips/ktooltip.h>
28 #include <kio/previewjob.h>
30 #include <QApplication>
31 #include <QDesktopWidget>
36 const int ICON_WIDTH
= 128;
37 const int ICON_HEIGHT
= 128;
38 const int PREVIEW_DELAY
= 250;
40 K_GLOBAL_STATIC(DolphinBalloonTooltipDelegate
, g_delegate
)
42 ToolTipManager::ToolTipManager(QAbstractItemView
* parent
,
43 DolphinSortFilterProxyModel
* model
) :
50 m_waitOnPreviewTimer(0),
54 m_generatingPreview(false),
58 KToolTip::setToolTipDelegate(g_delegate
);
60 m_dolphinModel
= static_cast<DolphinModel
*>(m_proxyModel
->sourceModel());
61 connect(parent
, SIGNAL(entered(const QModelIndex
&)),
62 this, SLOT(requestToolTip(const QModelIndex
&)));
63 connect(parent
, SIGNAL(viewportEntered()),
64 this, SLOT(hideToolTip()));
66 m_timer
= new QTimer(this);
67 m_timer
->setSingleShot(true);
68 connect(m_timer
, SIGNAL(timeout()),
69 this, SLOT(prepareToolTip()));
71 m_previewTimer
= new QTimer(this);
72 m_previewTimer
->setSingleShot(true);
73 connect(m_previewTimer
, SIGNAL(timeout()),
74 this, SLOT(startPreviewJob()));
76 m_waitOnPreviewTimer
= new QTimer(this);
77 m_waitOnPreviewTimer
->setSingleShot(true);
78 connect(m_waitOnPreviewTimer
, SIGNAL(timeout()),
79 this, SLOT(prepareToolTip()));
81 // When the mousewheel is used, the items don't get a hovered indication
82 // (Qt-issue #200665). To assure that the tooltip still gets hidden,
83 // the scrollbars are observed.
84 connect(parent
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
85 this, SLOT(hideTip()));
86 connect(parent
->verticalScrollBar(), SIGNAL(valueChanged(int)),
87 this, SLOT(hideTip()));
89 m_view
->viewport()->installEventFilter(this);
92 ToolTipManager::~ToolTipManager()
96 void ToolTipManager::hideTip()
101 bool ToolTipManager::eventFilter(QObject
* watched
, QEvent
* event
)
103 if ((watched
== m_view
->viewport()) && (event
->type() == QEvent::Leave
)) {
107 return QObject::eventFilter(watched
, event
);
110 void ToolTipManager::requestToolTip(const QModelIndex
& index
)
112 // only request a tooltip for the name column and when no selection or
113 // drag & drop operation is done (indicated by the left mouse button)
114 if ((index
.column() == DolphinModel::Name
) && !(QApplication::mouseButtons() & Qt::LeftButton
)) {
115 m_waitOnPreviewTimer
->stop();
118 m_itemRect
= m_view
->visualRect(index
);
119 const QPoint pos
= m_view
->viewport()->mapToGlobal(m_itemRect
.topLeft());
120 m_itemRect
.moveTo(pos
);
122 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
123 m_item
= m_dolphinModel
->itemForIndex(dirIndex
);
125 // only start the previewJob when the mouse has been over this item for 200 milliseconds,
126 // this prevents a lot of useless preview jobs when passing rapidly over a lot of items
127 m_previewTimer
->start(200);
137 void ToolTipManager::hideToolTip()
140 m_previewTimer
->stop();
141 m_waitOnPreviewTimer
->stop();
145 void ToolTipManager::prepareToolTip()
147 if (m_generatingPreview
) {
148 if (m_previewPass
== 1) {
149 // We waited 250msec and the preview is still not finished,
150 // so show default icon as fallback.
151 QPixmap
image(KIcon(m_item
.iconName()).pixmap(ICON_WIDTH
, ICON_HEIGHT
));
152 showToolTip(image
, m_item
.getToolTipText());
156 m_waitOnPreviewTimer
->start(250);
163 // No preview, so use an icon.
164 // Force a 128x128 icon, a 256x256 one is far too big.
165 const QPixmap pixmap
= KIcon(m_item
.iconName()).pixmap(ICON_WIDTH
, ICON_HEIGHT
);
166 icon
= KIcon(pixmap
);
169 showToolTip(icon
, m_item
.getToolTipText());
173 void ToolTipManager::showToolTip(const QIcon
& icon
, const QString
& text
)
175 if (QApplication::mouseButtons() & Qt::LeftButton
) {
179 KToolTipItem
* tip
= new KToolTipItem(icon
, text
);
181 KStyleOptionToolTip option
;
182 // TODO: get option content from KToolTip or add KToolTip::sizeHint() method
183 option
.direction
= QApplication::layoutDirection();
184 option
.fontMetrics
= QFontMetrics(QToolTip::font());
185 option
.activeCorner
= KStyleOptionToolTip::TopLeftCorner
;
186 option
.palette
= QToolTip::palette();
187 option
.font
= QToolTip::font();
188 option
.rect
= QRect();
189 option
.state
= QStyle::State_None
;
190 option
.decorationSize
= QSize(32, 32);
192 const QSize size
= g_delegate
->sizeHint(option
, *tip
);
193 const QRect desktop
= QApplication::desktop()->screenGeometry(m_itemRect
.bottomRight());
195 // m_itemRect defines the area of the item, where the tooltip should be
196 // shown. Per default the tooltip is shown in the bottom right corner.
197 // If the tooltip content exceeds the desktop borders, it must be assured that:
198 // - the content is fully visible
199 // - the content is not drawn inside m_itemRect
200 const bool hasRoomToLeft
= (m_itemRect
.left() - size
.width() >= desktop
.left());
201 const bool hasRoomToRight
= (m_itemRect
.right() + size
.width() <= desktop
.right());
202 const bool hasRoomAbove
= (m_itemRect
.top() - size
.height() >= desktop
.top());
203 const bool hasRoomBelow
= (m_itemRect
.bottom() + size
.height() <= desktop
.bottom());
204 if (!hasRoomAbove
&& !hasRoomBelow
&& !hasRoomToLeft
&& !hasRoomToRight
) {
212 if (hasRoomBelow
|| hasRoomAbove
) {
213 x
= QCursor::pos().x() + 16; // TODO: use mouse pointer width instead of the magic value of 16
214 if (x
+ size
.width() >= desktop
.right()) {
215 x
= desktop
.right() - size
.width();
217 y
= hasRoomBelow
? m_itemRect
.bottom() : m_itemRect
.top() - size
.height();
219 Q_ASSERT(hasRoomToLeft
|| hasRoomToRight
);
220 x
= hasRoomToRight
? m_itemRect
.right() : m_itemRect
.left() - size
.width();
222 // Put the tooltip at the bottom of the screen. The x-coordinate has already
223 // been adjusted, so that no overlapping with m_itemRect occurs.
224 y
= desktop
.bottom() - size
.height();
227 // the ownership of tip is transferred to KToolTip
228 KToolTip::showTip(QPoint(x
, y
), tip
);
233 void ToolTipManager::startPreviewJob()
235 m_generatingPreview
= true;
236 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << m_item
,
240 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
241 this, SLOT(setPreviewPix(const KFileItem
&, const QPixmap
&)));
242 connect(job
, SIGNAL(failed(const KFileItem
&)),
243 this, SLOT(previewFailed(const KFileItem
&)));
247 void ToolTipManager::setPreviewPix(const KFileItem
& item
,
248 const QPixmap
& pixmap
)
250 if ((m_item
.url() != item
.url()) || pixmap
.isNull()) {
251 // an old preview or an invalid preview has been received
252 m_generatingPreview
= false;
258 m_generatingPreview
= false;
261 void ToolTipManager::previewFailed(const KFileItem
& item
)
264 m_generatingPreview
= false;
267 #include "tooltipmanager.moc"