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);
91 ToolTipManager::~ToolTipManager()
95 void ToolTipManager::hideTip()
100 bool ToolTipManager::eventFilter(QObject
* watched
, QEvent
* event
)
102 if ((watched
== m_view
->viewport()) && (event
->type() == QEvent::Leave
)) {
106 return QObject::eventFilter(watched
, event
);
109 void ToolTipManager::requestToolTip(const QModelIndex
& index
)
111 // only request a tooltip for the name column and when no selection or
112 // drag & drop operation is done (indicated by the left mouse button)
113 if ((index
.column() == DolphinModel::Name
) && !(QApplication::mouseButtons() & Qt::LeftButton
)) {
114 m_waitOnPreviewTimer
->stop();
117 m_itemRect
= m_view
->visualRect(index
);
118 const QPoint pos
= m_view
->viewport()->mapToGlobal(m_itemRect
.topLeft());
119 m_itemRect
.moveTo(pos
);
121 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
122 m_item
= m_dolphinModel
->itemForIndex(dirIndex
);
124 // only start the previewJob when the mouse has been over this item for 200 milliseconds,
125 // this prevents a lot of useless preview jobs when passing rapidly over a lot of items
126 m_previewTimer
->start(200);
127 m_previewPixmap
= QPixmap();
128 m_hasDefaultIcon
= false;
136 void ToolTipManager::hideToolTip()
139 m_previewTimer
->stop();
140 m_waitOnPreviewTimer
->stop();
144 void ToolTipManager::prepareToolTip()
146 if (m_generatingPreview
) {
147 m_waitOnPreviewTimer
->start(250);
150 const QString text
= m_item
.getToolTipText();
151 if (!m_previewPixmap
.isNull()) {
152 showToolTip(KIcon(m_previewPixmap
), text
);
153 } else if (!m_hasDefaultIcon
) {
154 const QPixmap
image(KIcon(m_item
.iconName()).pixmap(ICON_WIDTH
, ICON_HEIGHT
));
155 showToolTip(image
, text
);
156 m_hasDefaultIcon
= true;
160 void ToolTipManager::showToolTip(const QIcon
& icon
, const QString
& text
)
162 if (QApplication::mouseButtons() & Qt::LeftButton
) {
166 KToolTipItem
* tip
= new KToolTipItem(icon
, text
);
168 KStyleOptionToolTip option
;
169 // TODO: get option content from KToolTip or add KToolTip::sizeHint() method
170 option
.direction
= QApplication::layoutDirection();
171 option
.fontMetrics
= QFontMetrics(QToolTip::font());
172 option
.activeCorner
= KStyleOptionToolTip::TopLeftCorner
;
173 option
.palette
= QToolTip::palette();
174 option
.font
= QToolTip::font();
175 option
.rect
= QRect();
176 option
.state
= QStyle::State_None
;
177 option
.decorationSize
= QSize(32, 32);
179 const QSize size
= g_delegate
->sizeHint(option
, *tip
);
180 const QRect desktop
= QApplication::desktop()->screenGeometry(m_itemRect
.bottomRight());
182 // m_itemRect defines the area of the item, where the tooltip should be
183 // shown. Per default the tooltip is shown in the bottom right corner.
184 // If the tooltip content exceeds the desktop borders, it must be assured that:
185 // - the content is fully visible
186 // - the content is not drawn inside m_itemRect
187 const bool hasRoomToLeft
= (m_itemRect
.left() - size
.width() >= desktop
.left());
188 const bool hasRoomToRight
= (m_itemRect
.right() + size
.width() <= desktop
.right());
189 const bool hasRoomAbove
= (m_itemRect
.top() - size
.height() >= desktop
.top());
190 const bool hasRoomBelow
= (m_itemRect
.bottom() + size
.height() <= desktop
.bottom());
191 if (!hasRoomAbove
&& !hasRoomBelow
&& !hasRoomToLeft
&& !hasRoomToRight
) {
199 if (hasRoomBelow
|| hasRoomAbove
) {
200 x
= QCursor::pos().x() + 16; // TODO: use mouse pointer width instead of the magic value of 16
201 if (x
+ size
.width() >= desktop
.right()) {
202 x
= desktop
.right() - size
.width();
204 y
= hasRoomBelow
? m_itemRect
.bottom() : m_itemRect
.top() - size
.height();
206 Q_ASSERT(hasRoomToLeft
|| hasRoomToRight
);
207 x
= hasRoomToRight
? m_itemRect
.right() : m_itemRect
.left() - size
.width();
209 // Put the tooltip at the bottom of the screen. The x-coordinate has already
210 // been adjusted, so that no overlapping with m_itemRect occurs.
211 y
= desktop
.bottom() - size
.height();
214 // the ownership of tip is transferred to KToolTip
215 KToolTip::showTip(QPoint(x
, y
), tip
);
220 void ToolTipManager::startPreviewJob()
222 m_generatingPreview
= true;
223 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << m_item
,
227 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
228 this, SLOT(setPreviewPix(const KFileItem
&, const QPixmap
&)));
229 connect(job
, SIGNAL(failed(const KFileItem
&)),
230 this, SLOT(previewFailed()));
234 void ToolTipManager::setPreviewPix(const KFileItem
& item
,
235 const QPixmap
& pixmap
)
237 if ((m_item
.url() != item
.url()) || pixmap
.isNull()) {
238 // an old preview or an invalid preview has been received
241 m_previewPixmap
= pixmap
;
242 m_generatingPreview
= false;
246 void ToolTipManager::previewFailed()
248 m_generatingPreview
= false;
251 #include "tooltipmanager.moc"