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"
28 #include <kio/previewjob.h>
30 #include <QApplication>
31 #include <QDesktopWidget>
36 const int PREVIEW_WIDTH
= 256;
37 const int PREVIEW_HEIGHT
= 256;
38 const int ICON_WIDTH
= 128;
39 const int ICON_HEIGHT
= 128;
40 const int PREVIEW_DELAY
= 250;
42 K_GLOBAL_STATIC(DolphinBalloonTooltipDelegate
, g_delegate
)
44 ToolTipManager::ToolTipManager(QAbstractItemView
* parent
,
45 DolphinSortFilterProxyModel
* model
) :
52 m_waitOnPreviewTimer(0),
56 m_generatingPreview(false),
57 m_previewIsLate(false),
59 m_emptyRenderedKToolTipItem(0),
62 KToolTip::setToolTipDelegate(g_delegate
);
64 m_dolphinModel
= static_cast<DolphinModel
*>(m_proxyModel
->sourceModel());
65 connect(parent
, SIGNAL(entered(const QModelIndex
&)),
66 this, SLOT(requestToolTip(const QModelIndex
&)));
67 connect(parent
, SIGNAL(viewportEntered()),
68 this, SLOT(hideToolTip()));
70 m_timer
= new QTimer(this);
71 m_timer
->setSingleShot(true);
72 connect(m_timer
, SIGNAL(timeout()),
73 this, SLOT(prepareToolTip()));
75 m_previewTimer
= new QTimer(this);
76 m_previewTimer
->setSingleShot(true);
77 connect(m_previewTimer
, SIGNAL(timeout()),
78 this, SLOT(startPreviewJob()));
80 m_waitOnPreviewTimer
= new QTimer(this);
81 m_waitOnPreviewTimer
->setSingleShot(true);
82 connect(m_waitOnPreviewTimer
, SIGNAL(timeout()),
83 this, SLOT(prepareToolTip()));
85 // When the mousewheel is used, the items don't get a hovered indication
86 // (Qt-issue #200665). To assure that the tooltip still gets hidden,
87 // the scrollbars are observed.
88 connect(parent
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
89 this, SLOT(hideTip()));
90 connect(parent
->verticalScrollBar(), SIGNAL(valueChanged(int)),
91 this, SLOT(hideTip()));
93 m_view
->viewport()->installEventFilter(this);
96 ToolTipManager::~ToolTipManager()
100 void ToolTipManager::hideTip()
105 bool ToolTipManager::eventFilter(QObject
* watched
, QEvent
* event
)
107 if ((watched
== m_view
->viewport()) && (event
->type() == QEvent::Leave
)) {
111 return QObject::eventFilter(watched
, event
);
114 void ToolTipManager::requestToolTip(const QModelIndex
& index
)
116 // only request a tooltip for the name column and when no selection or
117 // drag & drop operation is done (indicated by the left mouse button)
118 if ((index
.column() == DolphinModel::Name
) && !(QApplication::mouseButtons() & Qt::LeftButton
)) {
119 m_waitOnPreviewTimer
->stop();
122 m_itemRect
= m_view
->visualRect(index
);
123 const QPoint pos
= m_view
->viewport()->mapToGlobal(m_itemRect
.topLeft());
124 m_itemRect
.moveTo(pos
);
126 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
127 m_item
= m_dolphinModel
->itemForIndex(dirIndex
);
129 // only start the previewJob when the mouse has been over this item for 200 milliseconds,
130 // this prevents a lot of useless preview jobs when passing rapidly over a lot of items
131 m_previewTimer
->start(200);
133 m_previewIsLate
= false;
142 void ToolTipManager::hideToolTip()
145 m_previewTimer
->stop();
146 m_waitOnPreviewTimer
->stop();
147 m_previewIsLate
= false;
151 void ToolTipManager::prepareToolTip()
153 if (m_generatingPreview
) {
154 if (m_previewPass
== 1) {
155 // We waited 250msec and the preview is still not finished,
156 // so show the toolTip with a transparent image of maximal width.
157 // When the preview finishes, m_previewIsLate will cause
158 // a direct update of the tooltip, via m_emptyRenderedKToolTipItem.
159 QPixmap
paddedImage(QSize(PREVIEW_WIDTH
, 32));
160 m_previewIsLate
= true;
161 paddedImage
.fill(Qt::transparent
);
162 KToolTipItem
* toolTip
= new KToolTipItem(paddedImage
, m_item
.getToolTipText());
163 m_emptyRenderedKToolTipItem
= toolTip
; // make toolTip accessible everywhere
164 showToolTip(toolTip
);
168 m_waitOnPreviewTimer
->start(250);
170 // The preview generation has finished, find out under which circumstances.
171 if (m_preview
&& m_previewIsLate
) {
172 // We got a preview, but it is late, the tooltip has already been shown.
173 // So update the tooltip directly.
174 if (m_emptyRenderedKToolTipItem
!= 0) {
175 m_emptyRenderedKToolTipItem
->setData(Qt::DecorationRole
, KIcon(m_pix
));
185 // No preview, so use an icon.
186 // Force a 128x128 icon, a 256x256 one is far too big.
187 icon
= KIcon(KIcon(m_item
.iconName()).pixmap(ICON_WIDTH
, ICON_HEIGHT
));
190 KToolTipItem
* toolTip
= new KToolTipItem(icon
, m_item
.getToolTipText());
191 showToolTip(toolTip
);
195 void ToolTipManager::showToolTip(KToolTipItem
* tip
)
197 if (QApplication::mouseButtons() & Qt::LeftButton
) {
200 // m_emptyRenderedKToolTipItem is an alias for tip.
201 m_emptyRenderedKToolTipItem
= 0;
205 KStyleOptionToolTip option
;
206 // TODO: get option content from KToolTip or add KToolTip::sizeHint() method
207 option
.direction
= QApplication::layoutDirection();
208 option
.fontMetrics
= QFontMetrics(QToolTip::font());
209 option
.activeCorner
= KStyleOptionToolTip::TopLeftCorner
;
210 option
.palette
= QToolTip::palette();
211 option
.font
= QToolTip::font();
212 option
.rect
= QRect();
213 option
.state
= QStyle::State_None
;
214 option
.decorationSize
= QSize(32, 32);
217 if (m_previewIsLate
) {
218 QPixmap
paddedImage(QSize(PREVIEW_WIDTH
, PREVIEW_HEIGHT
));
219 KToolTipItem
* maxiTip
= new KToolTipItem(paddedImage
, m_item
.getToolTipText());
220 size
= g_delegate
->sizeHint(&option
, maxiTip
);
225 size
= g_delegate
->sizeHint(&option
, tip
);
227 const QRect desktop
= QApplication::desktop()->screenGeometry(m_itemRect
.bottomRight());
229 // m_itemRect defines the area of the item, where the tooltip should be
230 // shown. Per default the tooltip is shown in the bottom right corner.
231 // If the tooltip content exceeds the desktop borders, it must be assured that:
232 // - the content is fully visible
233 // - the content is not drawn inside m_itemRect
234 const bool hasRoomToLeft
= (m_itemRect
.left() - size
.width() >= desktop
.left());
235 const bool hasRoomToRight
= (m_itemRect
.right() + size
.width() <= desktop
.right());
236 const bool hasRoomAbove
= (m_itemRect
.top() - size
.height() >= desktop
.top());
237 const bool hasRoomBelow
= (m_itemRect
.bottom() + size
.height() <= desktop
.bottom());
238 if (!hasRoomAbove
&& !hasRoomBelow
&& !hasRoomToLeft
&& !hasRoomToRight
) {
246 if (hasRoomBelow
|| hasRoomAbove
) {
247 x
= QCursor::pos().x() + 16; // TODO: use mouse pointer width instead of the magic value of 16
248 if (x
+ size
.width() >= desktop
.right()) {
249 x
= desktop
.right() - size
.width();
251 y
= hasRoomBelow
? m_itemRect
.bottom() : m_itemRect
.top() - size
.height();
253 Q_ASSERT(hasRoomToLeft
|| hasRoomToRight
);
254 x
= hasRoomToRight
? m_itemRect
.right() : m_itemRect
.left() - size
.width();
256 // Put the tooltip at the bottom of the screen. The x-coordinate has already
257 // been adjusted, so that no overlapping with m_itemRect occurs.
258 y
= desktop
.bottom() - size
.height();
261 KToolTip::showTip(QPoint(x
, y
), tip
);
266 void ToolTipManager::startPreviewJob()
268 m_generatingPreview
= true;
269 KIO::PreviewJob
* job
= KIO::filePreview(KUrl::List() << m_item
.url(),
272 job
->setIgnoreMaximumSize(true);
274 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
275 this, SLOT(setPreviewPix(const KFileItem
&, const QPixmap
&)));
276 connect(job
, SIGNAL(failed(const KFileItem
&)),
277 this, SLOT(previewFailed(const KFileItem
&)));
281 void ToolTipManager::setPreviewPix(const KFileItem
& item
,
282 const QPixmap
& pixmap
)
284 if (m_item
.url() != item
.url()) {
285 m_generatingPreview
= false;
289 if (m_previewIsLate
) {
290 // always use the maximal width
291 QPixmap
paddedImage(QSize(PREVIEW_WIDTH
, pixmap
.height()));
292 paddedImage
.fill(Qt::transparent
);
293 QPainter
painter(&paddedImage
);
294 painter
.drawPixmap((PREVIEW_WIDTH
- pixmap
.width()) / 2, 0, pixmap
);
300 m_generatingPreview
= false;
303 void ToolTipManager::previewFailed(const KFileItem
& item
)
306 m_generatingPreview
= false;
309 #include "tooltipmanager.moc"