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>
35 const int PREVIEW_WIDTH
= 256;
36 const int PREVIEW_HEIGHT
= 256;
37 const int ICON_WIDTH
= 128;
38 const int ICON_HEIGHT
= 128;
39 const int PREVIEW_DELAY
= 250;
41 K_GLOBAL_STATIC(DolphinBalloonTooltipDelegate
, g_delegate
)
43 ToolTipManager::ToolTipManager(QAbstractItemView
* parent
,
44 DolphinSortFilterProxyModel
* model
) :
51 m_waitOnPreviewTimer(0),
55 m_generatingPreview(false),
56 m_previewIsLate(false),
58 m_emptyRenderedKToolTipItem(0),
61 KToolTip::setToolTipDelegate(g_delegate
);
63 m_dolphinModel
= static_cast<DolphinModel
*>(m_proxyModel
->sourceModel());
64 connect(parent
, SIGNAL(entered(const QModelIndex
&)),
65 this, SLOT(requestToolTip(const QModelIndex
&)));
66 connect(parent
, SIGNAL(viewportEntered()),
67 this, SLOT(hideToolTip()));
69 m_timer
= new QTimer(this);
70 m_timer
->setSingleShot(true);
71 connect(m_timer
, SIGNAL(timeout()),
72 this, SLOT(prepareToolTip()));
74 m_previewTimer
= new QTimer(this);
75 m_previewTimer
->setSingleShot(true);
76 connect(m_previewTimer
, SIGNAL(timeout()),
77 this, SLOT(startPreviewJob()));
79 m_waitOnPreviewTimer
= new QTimer(this);
80 m_waitOnPreviewTimer
->setSingleShot(true);
81 connect(m_waitOnPreviewTimer
, SIGNAL(timeout()),
82 this, SLOT(prepareToolTip()));
84 m_view
->viewport()->installEventFilter(this);
87 ToolTipManager::~ToolTipManager()
91 void ToolTipManager::hideTip()
96 bool ToolTipManager::eventFilter(QObject
* watched
, QEvent
* event
)
98 if ((watched
== m_view
->viewport()) && (event
->type() == QEvent::Leave
)) {
102 return QObject::eventFilter(watched
, event
);
105 void ToolTipManager::requestToolTip(const QModelIndex
& index
)
107 // only request a tooltip for the name column and when no selection or
108 // drag & drop operation is done (indicated by the left mouse button)
109 if ((index
.column() == DolphinModel::Name
) && !(QApplication::mouseButtons() & Qt::LeftButton
)) {
110 m_waitOnPreviewTimer
->stop();
113 m_itemRect
= m_view
->visualRect(index
);
114 const QPoint pos
= m_view
->viewport()->mapToGlobal(m_itemRect
.topLeft());
115 m_itemRect
.moveTo(pos
);
117 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
118 m_item
= m_dolphinModel
->itemForIndex(dirIndex
);
120 // only start the previewJob when the mouse has been over this item for 200 milliseconds,
121 // this prevents a lot of useless preview jobs when passing rapidly over a lot of items
122 m_previewTimer
->start(200);
124 m_previewIsLate
= false;
133 void ToolTipManager::hideToolTip()
136 m_previewTimer
->stop();
137 m_waitOnPreviewTimer
->stop();
138 m_previewIsLate
= false;
142 void ToolTipManager::prepareToolTip()
144 if (m_generatingPreview
) {
145 if (m_previewPass
== 1) {
146 // We waited 250msec and the preview is still not finished,
147 // so show the toolTip with a transparent image of maximal width.
148 // When the preview finishes, m_previewIsLate will cause
149 // a direct update of the tooltip, via m_emptyRenderedKToolTipItem.
150 QPixmap
paddedImage(QSize(PREVIEW_WIDTH
, 32));
151 m_previewIsLate
= true;
152 paddedImage
.fill(Qt::transparent
);
153 KToolTipItem
* toolTip
= new KToolTipItem(paddedImage
, m_item
.getToolTipText());
154 m_emptyRenderedKToolTipItem
= toolTip
; // make toolTip accessible everywhere
155 showToolTip(toolTip
);
159 m_waitOnPreviewTimer
->start(250);
161 // The preview generation has finished, find out under which circumstances.
162 if (m_preview
&& m_previewIsLate
) {
163 // We got a preview, but it is late, the tooltip has already been shown.
164 // So update the tooltip directly.
165 if (m_emptyRenderedKToolTipItem
!= 0) {
166 m_emptyRenderedKToolTipItem
->setData(Qt::DecorationRole
, KIcon(m_pix
));
176 // No preview, so use an icon.
177 // Force a 128x128 icon, a 256x256 one is far too big.
178 icon
= KIcon(KIcon(m_item
.iconName()).pixmap(ICON_WIDTH
, ICON_HEIGHT
));
181 KToolTipItem
* toolTip
= new KToolTipItem(icon
, m_item
.getToolTipText());
182 showToolTip(toolTip
);
186 void ToolTipManager::showToolTip(KToolTipItem
* tip
)
188 if (QApplication::mouseButtons() & Qt::LeftButton
) {
191 // m_emptyRenderedKToolTipItem is an alias for tip.
192 m_emptyRenderedKToolTipItem
= 0;
196 KStyleOptionToolTip option
;
197 // TODO: get option content from KToolTip or add KToolTip::sizeHint() method
198 option
.direction
= QApplication::layoutDirection();
199 option
.fontMetrics
= QFontMetrics(QToolTip::font());
200 option
.activeCorner
= KStyleOptionToolTip::TopLeftCorner
;
201 option
.palette
= QToolTip::palette();
202 option
.font
= QToolTip::font();
203 option
.rect
= QRect();
204 option
.state
= QStyle::State_None
;
205 option
.decorationSize
= QSize(32, 32);
208 if (m_previewIsLate
) {
209 QPixmap
paddedImage(QSize(PREVIEW_WIDTH
, PREVIEW_HEIGHT
));
210 KToolTipItem
* maxiTip
= new KToolTipItem(paddedImage
, m_item
.getToolTipText());
211 size
= g_delegate
->sizeHint(&option
, maxiTip
);
216 size
= g_delegate
->sizeHint(&option
, tip
);
218 const QRect desktop
= QApplication::desktop()->screenGeometry(m_itemRect
.bottomRight());
220 // m_itemRect defines the area of the item, where the tooltip should be
221 // shown. Per default the tooltip is shown in the bottom right corner.
222 // If the tooltip content exceeds the desktop borders, it must be assured that:
223 // - the content is fully visible
224 // - the content is not drawn inside m_itemRect
225 const bool hasRoomToLeft
= (m_itemRect
.left() - size
.width() >= desktop
.left());
226 const bool hasRoomToRight
= (m_itemRect
.right() + size
.width() <= desktop
.right());
227 const bool hasRoomAbove
= (m_itemRect
.top() - size
.height() >= desktop
.top());
228 const bool hasRoomBelow
= (m_itemRect
.bottom() + size
.height() <= desktop
.bottom());
229 if (!hasRoomAbove
&& !hasRoomBelow
&& !hasRoomToLeft
&& !hasRoomToRight
) {
237 if (hasRoomBelow
|| hasRoomAbove
) {
238 x
= QCursor::pos().x() + 16; // TODO: use mouse pointer width instead of the magic value of 16
239 if (x
+ size
.width() >= desktop
.right()) {
240 x
= desktop
.right() - size
.width();
242 y
= hasRoomBelow
? m_itemRect
.bottom() : m_itemRect
.top() - size
.height();
244 Q_ASSERT(hasRoomToLeft
|| hasRoomToRight
);
245 x
= hasRoomToRight
? m_itemRect
.right() : m_itemRect
.left() - size
.width();
247 // Put the tooltip at the bottom of the screen. The x-coordinate has already
248 // been adjusted, so that no overlapping with m_itemRect occurs.
249 y
= desktop
.bottom() - size
.height();
252 KToolTip::showTip(QPoint(x
, y
), tip
);
257 void ToolTipManager::startPreviewJob()
259 m_generatingPreview
= true;
260 KIO::PreviewJob
* job
= KIO::filePreview(KUrl::List() << m_item
.url(),
263 job
->setIgnoreMaximumSize(true);
265 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
266 this, SLOT(setPreviewPix(const KFileItem
&, const QPixmap
&)));
267 connect(job
, SIGNAL(failed(const KFileItem
&)),
268 this, SLOT(previewFailed(const KFileItem
&)));
272 void ToolTipManager::setPreviewPix(const KFileItem
& item
,
273 const QPixmap
& pixmap
)
275 if (m_item
.url() != item
.url()) {
276 m_generatingPreview
= false;
280 if (m_previewIsLate
) {
281 // always use the maximal width
282 QPixmap
paddedImage(QSize(PREVIEW_WIDTH
, pixmap
.height()));
283 paddedImage
.fill(Qt::transparent
);
284 QPainter
painter(&paddedImage
);
285 painter
.drawPixmap((PREVIEW_WIDTH
- pixmap
.width()) / 2, 0, pixmap
);
291 m_generatingPreview
= false;
294 void ToolTipManager::previewFailed(const KFileItem
& item
)
297 m_generatingPreview
= false;
300 #include "tooltipmanager.moc"