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 if (index
.column() == DolphinModel::Name
) {
108 m_waitOnPreviewTimer
->stop();
111 m_itemRect
= m_view
->visualRect(index
);
112 const QPoint pos
= m_view
->viewport()->mapToGlobal(m_itemRect
.topLeft());
113 m_itemRect
.moveTo(pos
);
115 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
116 m_item
= m_dolphinModel
->itemForIndex(dirIndex
);
118 // Only start the previewJob when the mouse has been over this item for 200msec,
119 // this prevents a lot of useless previewJobs (when passing rapidly over a lot of items).
120 m_previewTimer
->start(200);
121 // reset these variables
123 m_previewIsLate
= false;
132 void ToolTipManager::hideToolTip()
135 m_previewTimer
->stop();
136 m_waitOnPreviewTimer
->stop();
137 m_previewIsLate
= false;
141 void ToolTipManager::prepareToolTip()
143 if (m_generatingPreview
) {
144 if (m_previewPass
== 1) {
145 // We waited 250msec and the preview is still not finished,
146 // so show the toolTip with a transparent image of maximal width.
147 // When the preview finishes, m_previewIsLate will cause
148 // a direct update of the tooltip, via m_emptyRenderedKToolTipItem.
149 QPixmap
paddedImage(QSize(PREVIEW_WIDTH
, 32));
150 m_previewIsLate
= true;
151 paddedImage
.fill(Qt::transparent
);
152 KToolTipItem
* toolTip
= new KToolTipItem(paddedImage
, m_item
.getToolTipText());
153 m_emptyRenderedKToolTipItem
= toolTip
; // make toolTip accessible everywhere
154 showToolTip(toolTip
);
158 m_waitOnPreviewTimer
->start(250);
160 // The preview generation has finished, find out under which circumstances.
161 if (m_preview
&& m_previewIsLate
) {
162 // We got a preview, but it is late, the tooltip has already been shown.
163 // So update the tooltip directly.
164 m_emptyRenderedKToolTipItem
->setData(Qt::DecorationRole
, KIcon(m_pix
));
173 // No preview, so use an icon.
174 // Force a 128x128 icon, a 256x256 one is far too big.
175 icon
= KIcon(KIcon(m_item
.iconName()).pixmap(ICON_WIDTH
, ICON_HEIGHT
));
178 KToolTipItem
* toolTip
= new KToolTipItem(icon
, m_item
.getToolTipText());
179 showToolTip(toolTip
);
183 void ToolTipManager::showToolTip(KToolTipItem
* tip
)
185 KStyleOptionToolTip option
;
186 // TODO: get option content from KToolTip or add KToolTip::sizeHint() method
187 option
.direction
= QApplication::layoutDirection();
188 option
.fontMetrics
= QFontMetrics(QToolTip::font());
189 option
.activeCorner
= KStyleOptionToolTip::TopLeftCorner
;
190 option
.palette
= QToolTip::palette();
191 option
.font
= QToolTip::font();
192 option
.rect
= QRect();
193 option
.state
= QStyle::State_None
;
194 option
.decorationSize
= QSize(32, 32);
197 if (m_previewIsLate
) {
198 QPixmap
paddedImage(QSize(PREVIEW_WIDTH
, PREVIEW_HEIGHT
));
199 KToolTipItem
* maxiTip
= new KToolTipItem(paddedImage
, m_item
.getToolTipText());
200 size
= g_delegate
->sizeHint(&option
, maxiTip
);
205 size
= g_delegate
->sizeHint(&option
, tip
);
207 const QRect desktop
= QApplication::desktop()->screenGeometry(m_itemRect
.bottomRight());
209 // m_itemRect defines the area of the item, where the tooltip should be
210 // shown. Per default the tooltip is shown in the bottom right corner.
211 // If the tooltip content exceeds the desktop borders, it must be assured that:
212 // - the content is fully visible
213 // - the content is not drawn inside m_itemRect
214 const bool hasRoomToLeft
= (m_itemRect
.left() - size
.width() >= desktop
.left());
215 const bool hasRoomToRight
= (m_itemRect
.right() + size
.width() <= desktop
.right());
216 const bool hasRoomAbove
= (m_itemRect
.top() - size
.height() >= desktop
.top());
217 const bool hasRoomBelow
= (m_itemRect
.bottom() + size
.height() <= desktop
.bottom());
218 if (!hasRoomAbove
&& !hasRoomBelow
&& !hasRoomToLeft
&& !hasRoomToRight
) {
226 if (hasRoomBelow
|| hasRoomAbove
) {
227 x
= QCursor::pos().x() + 16; // TODO: use mouse pointer width instead of the magic value of 16
228 if (x
+ size
.width() >= desktop
.right()) {
229 x
= desktop
.right() - size
.width();
231 y
= hasRoomBelow
? m_itemRect
.bottom() : m_itemRect
.top() - size
.height();
233 Q_ASSERT(hasRoomToLeft
|| hasRoomToRight
);
234 x
= hasRoomToRight
? m_itemRect
.right() : m_itemRect
.left() - size
.width();
236 // Put the tooltip at the bottom of the screen. The x-coordinate has already
237 // been adjusted, so that no overlapping with m_itemRect occurs.
238 y
= desktop
.bottom() - size
.height();
241 KToolTip::showTip(QPoint(x
, y
), tip
);
246 void ToolTipManager::startPreviewJob()
248 m_generatingPreview
= true;
249 KIO::PreviewJob
* job
= KIO::filePreview(KUrl::List() << m_item
.url(),
252 job
->setIgnoreMaximumSize(true);
254 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
255 this, SLOT(setPreviewPix(const KFileItem
&, const QPixmap
&)));
256 connect(job
, SIGNAL(failed(const KFileItem
&)),
257 this, SLOT(previewFailed(const KFileItem
&)));
261 void ToolTipManager::setPreviewPix(const KFileItem
& item
,
262 const QPixmap
& pixmap
)
264 if (m_item
.url() != item
.url()) {
265 m_generatingPreview
= false;
269 QPixmap icon
= pixmap
;
270 // only paint borders if the pixmap is opaque
271 if (!icon
.hasAlphaChannel()) {
272 // TODO: Make code from IconManager for drawing a border accessible for
273 // other classes (the following code has been adapted from IconManager).
274 // The frame is painted on top of the pixmap, this is needed to keep
275 // the text preview visually nice (the previewer adds ugly borders).
277 // make a buffer pixmap, tends to crash when 'icon' is directly painted ...
278 QPixmap
framedIcon(icon
.size().width(), pixmap
.size().height());
280 const int width
= framedIcon
.width() - 1;
281 const int height
= framedIcon
.height() - 1;
284 painter
.begin(&framedIcon
);
285 painter
.drawPixmap(0,0, icon
);
288 painter
.setRenderHint(QPainter::Antialiasing
, false);
289 painter
.setPen(QColor(0, 0, 0));
290 painter
.drawRect(0, 0, width
- 0, height
- 1);
291 painter
.drawRect(1, 1, width
- 2, height
- 2);
292 painter
.setPen(QColor(255, 255, 255));
293 painter
.drawRect(2, 2, width
- 4, height
- 4);
298 // provide an alpha channel for the frame
299 QPixmap
alphaChannel(icon
.size());
302 QPainter
alphaPainter(&alphaChannel
);
303 alphaPainter
.setBrush(Qt::NoBrush
);
304 alphaPainter
.setRenderHint(QPainter::Antialiasing
, false);
305 alphaPainter
.setPen(QColor(32, 32, 32));
306 alphaPainter
.drawRect(0, 0, width
, height
);
307 alphaPainter
.setPen(QColor(64, 64, 64));
308 alphaPainter
.drawRect(1, 1, width
- 2, height
- 2);
310 icon
.setAlphaChannel(alphaChannel
);
313 if (m_previewIsLate
) {
314 // always use the maximal width
315 QPixmap
paddedImage(QSize(PREVIEW_WIDTH
, icon
.height()));
316 paddedImage
.fill(Qt::transparent
);
317 QPainter
painter(&paddedImage
);
318 painter
.drawPixmap((PREVIEW_WIDTH
- icon
.width()) / 2, 0, icon
);
324 m_generatingPreview
= false;
327 void ToolTipManager::previewFailed(const KFileItem
& item
)
330 m_generatingPreview
= false;
333 #include "tooltipmanager.moc"