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),
54 m_generatingPreview(false),
55 m_previewIsLate(false),
59 KToolTip::setToolTipDelegate(g_delegate
);
61 m_dolphinModel
= static_cast<DolphinModel
*>(m_proxyModel
->sourceModel());
62 connect(parent
, SIGNAL(entered(const QModelIndex
&)),
63 this, SLOT(requestToolTip(const QModelIndex
&)));
64 connect(parent
, SIGNAL(viewportEntered()),
65 this, SLOT(hideToolTip()));
67 m_timer
= new QTimer(this);
68 m_timer
->setSingleShot(true);
69 connect(m_timer
, SIGNAL(timeout()),
70 this, SLOT(prepareToolTip()));
72 m_previewTimer
= new QTimer(this);
73 m_previewTimer
->setSingleShot(true);
74 connect(m_previewTimer
, SIGNAL(timeout()),
75 this, SLOT(startPreviewJob()));
77 m_waitOnPreviewTimer
= new QTimer(this);
78 m_waitOnPreviewTimer
->setSingleShot(true);
79 connect(m_waitOnPreviewTimer
, SIGNAL(timeout()),
80 this, SLOT(prepareToolTip()));
82 // When the mousewheel is used, the items don't get a hovered indication
83 // (Qt-issue #200665). To assure that the tooltip still gets hidden,
84 // the scrollbars are observed.
85 connect(parent
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
86 this, SLOT(hideTip()));
87 connect(parent
->verticalScrollBar(), SIGNAL(valueChanged(int)),
88 this, SLOT(hideTip()));
90 m_view
->viewport()->installEventFilter(this);
93 ToolTipManager::~ToolTipManager()
97 void ToolTipManager::hideTip()
102 bool ToolTipManager::eventFilter(QObject
* watched
, QEvent
* event
)
104 if ((watched
== m_view
->viewport()) && (event
->type() == QEvent::Leave
)) {
108 return QObject::eventFilter(watched
, event
);
111 void ToolTipManager::requestToolTip(const QModelIndex
& index
)
113 // only request a tooltip for the name column and when no selection or
114 // drag & drop operation is done (indicated by the left mouse button)
115 if ((index
.column() == DolphinModel::Name
) && !(QApplication::mouseButtons() & Qt::LeftButton
)) {
116 m_waitOnPreviewTimer
->stop();
119 m_itemRect
= m_view
->visualRect(index
);
120 const QPoint pos
= m_view
->viewport()->mapToGlobal(m_itemRect
.topLeft());
121 m_itemRect
.moveTo(pos
);
123 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(index
);
124 m_item
= m_dolphinModel
->itemForIndex(dirIndex
);
126 // only start the previewJob when the mouse has been over this item for 200 milliseconds,
127 // this prevents a lot of useless preview jobs when passing rapidly over a lot of items
128 m_previewTimer
->start(200);
130 m_previewIsLate
= false;
139 void ToolTipManager::hideToolTip()
142 m_previewTimer
->stop();
143 m_waitOnPreviewTimer
->stop();
144 m_previewIsLate
= false;
148 void ToolTipManager::prepareToolTip()
150 if (m_generatingPreview
) {
151 if (m_previewPass
== 1) {
152 // We waited 250msec and the preview is still not finished,
153 // so show the toolTip with a transparent image of maximal width.
154 QPixmap
paddedImage(QSize(PREVIEW_WIDTH
, 32));
155 m_previewIsLate
= true;
156 paddedImage
.fill(Qt::transparent
);
157 showToolTip(paddedImage
, m_item
.getToolTipText());
161 m_waitOnPreviewTimer
->start(250);
163 // The preview generation has finished, find out under which circumstances.
164 if (m_preview
&& m_previewIsLate
) {
165 // We got a preview, but it is late, the tooltip has already been shown.
166 // So update the tooltip directly.
167 showToolTip(m_pix
, m_item
.getToolTipText());
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 showToolTip(icon
, m_item
.getToolTipText());
185 void ToolTipManager::showToolTip(const QIcon
& icon
, const QString
& text
)
187 if (QApplication::mouseButtons() & Qt::LeftButton
) {
191 KToolTipItem
* tip
= new KToolTipItem(icon
, m_item
.getToolTipText());
193 KStyleOptionToolTip option
;
194 // TODO: get option content from KToolTip or add KToolTip::sizeHint() method
195 option
.direction
= QApplication::layoutDirection();
196 option
.fontMetrics
= QFontMetrics(QToolTip::font());
197 option
.activeCorner
= KStyleOptionToolTip::TopLeftCorner
;
198 option
.palette
= QToolTip::palette();
199 option
.font
= QToolTip::font();
200 option
.rect
= QRect();
201 option
.state
= QStyle::State_None
;
202 option
.decorationSize
= QSize(32, 32);
205 if (m_previewIsLate
) {
206 QPixmap
paddedImage(QSize(PREVIEW_WIDTH
, PREVIEW_HEIGHT
));
207 KToolTipItem
maxiTip(paddedImage
, m_item
.getToolTipText());
208 size
= g_delegate
->sizeHint(option
, maxiTip
);
211 size
= g_delegate
->sizeHint(option
, *tip
);
213 const QRect desktop
= QApplication::desktop()->screenGeometry(m_itemRect
.bottomRight());
215 // m_itemRect defines the area of the item, where the tooltip should be
216 // shown. Per default the tooltip is shown in the bottom right corner.
217 // If the tooltip content exceeds the desktop borders, it must be assured that:
218 // - the content is fully visible
219 // - the content is not drawn inside m_itemRect
220 const bool hasRoomToLeft
= (m_itemRect
.left() - size
.width() >= desktop
.left());
221 const bool hasRoomToRight
= (m_itemRect
.right() + size
.width() <= desktop
.right());
222 const bool hasRoomAbove
= (m_itemRect
.top() - size
.height() >= desktop
.top());
223 const bool hasRoomBelow
= (m_itemRect
.bottom() + size
.height() <= desktop
.bottom());
224 if (!hasRoomAbove
&& !hasRoomBelow
&& !hasRoomToLeft
&& !hasRoomToRight
) {
232 if (hasRoomBelow
|| hasRoomAbove
) {
233 x
= QCursor::pos().x() + 16; // TODO: use mouse pointer width instead of the magic value of 16
234 if (x
+ size
.width() >= desktop
.right()) {
235 x
= desktop
.right() - size
.width();
237 y
= hasRoomBelow
? m_itemRect
.bottom() : m_itemRect
.top() - size
.height();
239 Q_ASSERT(hasRoomToLeft
|| hasRoomToRight
);
240 x
= hasRoomToRight
? m_itemRect
.right() : m_itemRect
.left() - size
.width();
242 // Put the tooltip at the bottom of the screen. The x-coordinate has already
243 // been adjusted, so that no overlapping with m_itemRect occurs.
244 y
= desktop
.bottom() - size
.height();
247 // the ownership of tip is transferred to KToolTip
248 KToolTip::showTip(QPoint(x
, y
), tip
);
253 void ToolTipManager::startPreviewJob()
255 m_generatingPreview
= true;
256 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << m_item
,
260 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
261 this, SLOT(setPreviewPix(const KFileItem
&, const QPixmap
&)));
262 connect(job
, SIGNAL(failed(const KFileItem
&)),
263 this, SLOT(previewFailed(const KFileItem
&)));
267 void ToolTipManager::setPreviewPix(const KFileItem
& item
,
268 const QPixmap
& pixmap
)
270 if ((m_item
.url() != item
.url()) || pixmap
.isNull()) {
271 m_generatingPreview
= false;
275 if (m_previewIsLate
) {
276 // always use the maximal width
277 QPixmap
paddedImage(QSize(PREVIEW_WIDTH
, pixmap
.height()));
278 paddedImage
.fill(Qt::transparent
);
279 QPainter
painter(&paddedImage
);
280 painter
.drawPixmap((PREVIEW_WIDTH
- pixmap
.width()) / 2, 0, pixmap
);
286 m_generatingPreview
= false;
289 void ToolTipManager::previewFailed(const KFileItem
& item
)
292 m_generatingPreview
= false;
295 #include "tooltipmanager.moc"