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),
53 m_generatingPreview(false),
54 m_hasDefaultIcon(false),
57 KToolTip::setToolTipDelegate(g_delegate
);
59 m_dolphinModel
= static_cast<DolphinModel
*>(m_proxyModel
->sourceModel());
60 connect(parent
, SIGNAL(entered(const QModelIndex
&)),
61 this, SLOT(requestToolTip(const QModelIndex
&)));
62 connect(parent
, SIGNAL(viewportEntered()),
63 this, SLOT(hideToolTip()));
65 m_timer
= new QTimer(this);
66 m_timer
->setSingleShot(true);
67 connect(m_timer
, SIGNAL(timeout()),
68 this, SLOT(prepareToolTip()));
70 m_previewTimer
= new QTimer(this);
71 m_previewTimer
->setSingleShot(true);
72 connect(m_previewTimer
, SIGNAL(timeout()),
73 this, SLOT(startPreviewJob()));
75 m_waitOnPreviewTimer
= new QTimer(this);
76 m_waitOnPreviewTimer
->setSingleShot(true);
77 connect(m_waitOnPreviewTimer
, SIGNAL(timeout()),
78 this, SLOT(prepareToolTip()));
80 // When the mousewheel is used, the items don't get a hovered indication
81 // (Qt-issue #200665). To assure that the tooltip still gets hidden,
82 // the scrollbars are observed.
83 connect(parent
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
84 this, SLOT(hideTip()));
85 connect(parent
->verticalScrollBar(), SIGNAL(valueChanged(int)),
86 this, SLOT(hideTip()));
88 m_view
->viewport()->installEventFilter(this);
89 m_view
->installEventFilter(this);
92 ToolTipManager::~ToolTipManager()
96 void ToolTipManager::hideTip()
101 bool ToolTipManager::eventFilter(QObject
* watched
, QEvent
* event
)
103 if (watched
== m_view
->viewport()) {
104 switch (event
->type()) {
106 case QEvent::MouseButtonPress
:
112 } else if ((watched
== m_view
) && (event
->type() == QEvent::KeyPress
)) {
116 return QObject::eventFilter(watched
, event
);
119 void ToolTipManager::requestToolTip(const QModelIndex
& index
)
121 // only request a tooltip for the name column and when no selection or
122 // drag & drop operation is done (indicated by the left mouse button)
123 if ((index
.column() == DolphinModel::Name
) && !(QApplication::mouseButtons() & Qt::LeftButton
)) {
124 m_waitOnPreviewTimer
->stop();
127 m_itemRect
= m_view
->visualRect(index
);
128 const QPoint pos
= m_view
->viewport()->mapToGlobal(m_itemRect
.topLeft());
129 m_itemRect
.moveTo(pos
);
131 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
132 m_item
= m_dolphinModel
->itemForIndex(dirIndex
);
134 // only start the previewJob when the mouse has been over this item for 200 milliseconds,
135 // this prevents a lot of useless preview jobs when passing rapidly over a lot of items
136 m_previewTimer
->start(200);
137 m_previewPixmap
= QPixmap();
138 m_hasDefaultIcon
= false;
146 void ToolTipManager::hideToolTip()
149 m_previewTimer
->stop();
150 m_waitOnPreviewTimer
->stop();
154 void ToolTipManager::prepareToolTip()
156 if (m_generatingPreview
) {
157 m_waitOnPreviewTimer
->start(250);
160 const QString text
= m_item
.getToolTipText();
161 if (!m_previewPixmap
.isNull()) {
162 showToolTip(KIcon(m_previewPixmap
), text
);
163 } else if (!m_hasDefaultIcon
) {
164 const QPixmap
image(KIcon(m_item
.iconName()).pixmap(ICON_WIDTH
, ICON_HEIGHT
));
165 showToolTip(image
, text
);
166 m_hasDefaultIcon
= true;
170 void ToolTipManager::showToolTip(const QIcon
& icon
, const QString
& text
)
172 if (QApplication::mouseButtons() & Qt::LeftButton
) {
176 KToolTipItem
* tip
= new KToolTipItem(icon
, text
);
178 KStyleOptionToolTip option
;
179 // TODO: get option content from KToolTip or add KToolTip::sizeHint() method
180 option
.direction
= QApplication::layoutDirection();
181 option
.fontMetrics
= QFontMetrics(QToolTip::font());
182 option
.activeCorner
= KStyleOptionToolTip::TopLeftCorner
;
183 option
.palette
= QToolTip::palette();
184 option
.font
= QToolTip::font();
185 option
.rect
= QRect();
186 option
.state
= QStyle::State_None
;
187 option
.decorationSize
= QSize(32, 32);
189 const QSize size
= g_delegate
->sizeHint(option
, *tip
);
190 const QRect desktop
= QApplication::desktop()->screenGeometry(m_itemRect
.bottomRight());
192 // m_itemRect defines the area of the item, where the tooltip should be
193 // shown. Per default the tooltip is shown in the bottom right corner.
194 // If the tooltip content exceeds the desktop borders, it must be assured that:
195 // - the content is fully visible
196 // - the content is not drawn inside m_itemRect
197 const bool hasRoomToLeft
= (m_itemRect
.left() - size
.width() >= desktop
.left());
198 const bool hasRoomToRight
= (m_itemRect
.right() + size
.width() <= desktop
.right());
199 const bool hasRoomAbove
= (m_itemRect
.top() - size
.height() >= desktop
.top());
200 const bool hasRoomBelow
= (m_itemRect
.bottom() + size
.height() <= desktop
.bottom());
201 if (!hasRoomAbove
&& !hasRoomBelow
&& !hasRoomToLeft
&& !hasRoomToRight
) {
209 if (hasRoomBelow
|| hasRoomAbove
) {
210 x
= QCursor::pos().x() + 16; // TODO: use mouse pointer width instead of the magic value of 16
211 if (x
+ size
.width() >= desktop
.right()) {
212 x
= desktop
.right() - size
.width();
214 y
= hasRoomBelow
? m_itemRect
.bottom() : m_itemRect
.top() - size
.height();
216 Q_ASSERT(hasRoomToLeft
|| hasRoomToRight
);
217 x
= hasRoomToRight
? m_itemRect
.right() : m_itemRect
.left() - size
.width();
219 // Put the tooltip at the bottom of the screen. The x-coordinate has already
220 // been adjusted, so that no overlapping with m_itemRect occurs.
221 y
= desktop
.bottom() - size
.height();
224 // the ownership of tip is transferred to KToolTip
225 KToolTip::showTip(QPoint(x
, y
), tip
);
230 void ToolTipManager::startPreviewJob()
232 m_generatingPreview
= true;
233 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << m_item
,
237 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
238 this, SLOT(setPreviewPix(const KFileItem
&, const QPixmap
&)));
239 connect(job
, SIGNAL(failed(const KFileItem
&)),
240 this, SLOT(previewFailed()));
244 void ToolTipManager::setPreviewPix(const KFileItem
& item
,
245 const QPixmap
& pixmap
)
247 if ((m_item
.url() != item
.url()) || pixmap
.isNull()) {
248 // an old preview or an invalid preview has been received
251 m_previewPixmap
= pixmap
;
252 m_generatingPreview
= false;
256 void ToolTipManager::previewFailed()
258 m_generatingPreview
= false;
261 #include "tooltipmanager.moc"