delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / apps / dolphin / src / tooltips / tooltipmanager.cpp
blob36a008f338777db76c5f23dd1cbc977b7513ff1a
1 /*******************************************************************************
2 * Copyright (C) 2008 by Konstantin Heil <konst.heil@stud.uni-heidelberg.de> *
3 * *
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. *
8 * *
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. *
13 * *
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"
26 #include <kicon.h>
27 #include <tooltips/ktooltip.h>
28 #include <kio/previewjob.h>
30 #include <QApplication>
31 #include <QDesktopWidget>
32 #include <QScrollBar>
33 #include <QTimer>
34 #include <QToolTip>
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) :
44 QObject(parent),
45 m_view(parent),
46 m_dolphinModel(0),
47 m_proxyModel(model),
48 m_timer(0),
49 m_previewTimer(0),
50 m_waitOnPreviewTimer(0),
51 m_item(),
52 m_itemRect(),
53 m_preview(false),
54 m_generatingPreview(false),
55 m_previewIsLate(false),
56 m_previewPass(0),
57 m_emptyRenderedKToolTipItem(0),
58 m_pix()
60 KToolTip::setToolTipDelegate(g_delegate);
62 m_dolphinModel = static_cast<DolphinModel*>(m_proxyModel->sourceModel());
63 connect(parent, SIGNAL(entered(const QModelIndex&)),
64 this, SLOT(requestToolTip(const QModelIndex&)));
65 connect(parent, SIGNAL(viewportEntered()),
66 this, SLOT(hideToolTip()));
68 m_timer = new QTimer(this);
69 m_timer->setSingleShot(true);
70 connect(m_timer, SIGNAL(timeout()),
71 this, SLOT(prepareToolTip()));
73 m_previewTimer = new QTimer(this);
74 m_previewTimer->setSingleShot(true);
75 connect(m_previewTimer, SIGNAL(timeout()),
76 this, SLOT(startPreviewJob()));
78 m_waitOnPreviewTimer = new QTimer(this);
79 m_waitOnPreviewTimer->setSingleShot(true);
80 connect(m_waitOnPreviewTimer, SIGNAL(timeout()),
81 this, SLOT(prepareToolTip()));
83 // When the mousewheel is used, the items don't get a hovered indication
84 // (Qt-issue #200665). To assure that the tooltip still gets hidden,
85 // the scrollbars are observed.
86 connect(parent->horizontalScrollBar(), SIGNAL(valueChanged(int)),
87 this, SLOT(hideTip()));
88 connect(parent->verticalScrollBar(), SIGNAL(valueChanged(int)),
89 this, SLOT(hideTip()));
91 m_view->viewport()->installEventFilter(this);
94 ToolTipManager::~ToolTipManager()
98 void ToolTipManager::hideTip()
100 hideToolTip();
103 bool ToolTipManager::eventFilter(QObject* watched, QEvent* event)
105 if ((watched == m_view->viewport()) && (event->type() == QEvent::Leave)) {
106 hideToolTip();
109 return QObject::eventFilter(watched, event);
112 void ToolTipManager::requestToolTip(const QModelIndex& index)
114 // only request a tooltip for the name column and when no selection or
115 // drag & drop operation is done (indicated by the left mouse button)
116 if ((index.column() == DolphinModel::Name) && !(QApplication::mouseButtons() & Qt::LeftButton)) {
117 m_waitOnPreviewTimer->stop();
118 KToolTip::hideTip();
120 m_itemRect = m_view->visualRect(index);
121 const QPoint pos = m_view->viewport()->mapToGlobal(m_itemRect.topLeft());
122 m_itemRect.moveTo(pos);
124 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
125 m_item = m_dolphinModel->itemForIndex(dirIndex);
127 // only start the previewJob when the mouse has been over this item for 200 milliseconds,
128 // this prevents a lot of useless preview jobs when passing rapidly over a lot of items
129 m_previewTimer->start(200);
130 m_preview = false;
131 m_previewIsLate = false;
132 m_previewPass = 0;
134 m_timer->start(500);
135 } else {
136 hideToolTip();
140 void ToolTipManager::hideToolTip()
142 m_timer->stop();
143 m_previewTimer->stop();
144 m_waitOnPreviewTimer->stop();
145 m_previewIsLate = false;
146 KToolTip::hideTip();
149 void ToolTipManager::prepareToolTip()
151 if (m_generatingPreview) {
152 if (m_previewPass == 1) {
153 // We waited 250msec and the preview is still not finished,
154 // so show the toolTip with a transparent image of maximal width.
155 // When the preview finishes, m_previewIsLate will cause
156 // a direct update of the tooltip, via m_emptyRenderedKToolTipItem.
157 QPixmap paddedImage(QSize(PREVIEW_WIDTH, 32));
158 m_previewIsLate = true;
159 paddedImage.fill(Qt::transparent);
160 KToolTipItem* toolTip = new KToolTipItem(paddedImage, m_item.getToolTipText());
161 m_emptyRenderedKToolTipItem = toolTip; // make toolTip accessible everywhere
162 showToolTip(toolTip);
165 ++m_previewPass;
166 m_waitOnPreviewTimer->start(250);
167 } else {
168 // The preview generation has finished, find out under which circumstances.
169 if (m_preview && m_previewIsLate) {
170 // We got a preview, but it is late, the tooltip has already been shown.
171 // So update the tooltip directly.
172 if (m_emptyRenderedKToolTipItem != 0) {
173 m_emptyRenderedKToolTipItem->setData(Qt::DecorationRole, KIcon(m_pix));
175 return;
178 KIcon icon;
179 if (m_preview) {
180 // We got a preview.
181 icon = KIcon(m_pix);
182 } else {
183 // No preview, so use an icon.
184 // Force a 128x128 icon, a 256x256 one is far too big.
185 icon = KIcon(KIcon(m_item.iconName()).pixmap(ICON_WIDTH, ICON_HEIGHT));
188 KToolTipItem* toolTip = new KToolTipItem(icon, m_item.getToolTipText());
189 showToolTip(toolTip);
193 void ToolTipManager::showToolTip(KToolTipItem* tip)
195 if (QApplication::mouseButtons() & Qt::LeftButton) {
196 delete tip;
197 tip = 0;
198 // m_emptyRenderedKToolTipItem is an alias for tip.
199 m_emptyRenderedKToolTipItem = 0;
200 return;
203 KStyleOptionToolTip option;
204 // TODO: get option content from KToolTip or add KToolTip::sizeHint() method
205 option.direction = QApplication::layoutDirection();
206 option.fontMetrics = QFontMetrics(QToolTip::font());
207 option.activeCorner = KStyleOptionToolTip::TopLeftCorner;
208 option.palette = QToolTip::palette();
209 option.font = QToolTip::font();
210 option.rect = QRect();
211 option.state = QStyle::State_None;
212 option.decorationSize = QSize(32, 32);
214 QSize size;
215 if (m_previewIsLate) {
216 QPixmap paddedImage(QSize(PREVIEW_WIDTH, PREVIEW_HEIGHT));
217 KToolTipItem* maxiTip = new KToolTipItem(paddedImage, m_item.getToolTipText());
218 size = g_delegate->sizeHint(&option, maxiTip);
219 delete maxiTip;
220 maxiTip = 0;
222 else {
223 size = g_delegate->sizeHint(&option, tip);
225 const QRect desktop = QApplication::desktop()->screenGeometry(m_itemRect.bottomRight());
227 // m_itemRect defines the area of the item, where the tooltip should be
228 // shown. Per default the tooltip is shown in the bottom right corner.
229 // If the tooltip content exceeds the desktop borders, it must be assured that:
230 // - the content is fully visible
231 // - the content is not drawn inside m_itemRect
232 const bool hasRoomToLeft = (m_itemRect.left() - size.width() >= desktop.left());
233 const bool hasRoomToRight = (m_itemRect.right() + size.width() <= desktop.right());
234 const bool hasRoomAbove = (m_itemRect.top() - size.height() >= desktop.top());
235 const bool hasRoomBelow = (m_itemRect.bottom() + size.height() <= desktop.bottom());
236 if (!hasRoomAbove && !hasRoomBelow && !hasRoomToLeft && !hasRoomToRight) {
237 delete tip;
238 tip = 0;
239 return;
242 int x = 0;
243 int y = 0;
244 if (hasRoomBelow || hasRoomAbove) {
245 x = QCursor::pos().x() + 16; // TODO: use mouse pointer width instead of the magic value of 16
246 if (x + size.width() >= desktop.right()) {
247 x = desktop.right() - size.width();
249 y = hasRoomBelow ? m_itemRect.bottom() : m_itemRect.top() - size.height();
250 } else {
251 Q_ASSERT(hasRoomToLeft || hasRoomToRight);
252 x = hasRoomToRight ? m_itemRect.right() : m_itemRect.left() - size.width();
254 // Put the tooltip at the bottom of the screen. The x-coordinate has already
255 // been adjusted, so that no overlapping with m_itemRect occurs.
256 y = desktop.bottom() - size.height();
259 KToolTip::showTip(QPoint(x, y), tip);
264 void ToolTipManager::startPreviewJob()
266 m_generatingPreview = true;
267 KIO::PreviewJob* job = KIO::filePreview(KFileItemList() << m_item,
268 PREVIEW_WIDTH,
269 PREVIEW_HEIGHT);
270 job->setIgnoreMaximumSize(true);
272 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
273 this, SLOT(setPreviewPix(const KFileItem&, const QPixmap&)));
274 connect(job, SIGNAL(failed(const KFileItem&)),
275 this, SLOT(previewFailed(const KFileItem&)));
279 void ToolTipManager::setPreviewPix(const KFileItem& item,
280 const QPixmap& pixmap)
282 if (m_item.url() != item.url()) {
283 m_generatingPreview = false;
284 return;
287 if (m_previewIsLate) {
288 // always use the maximal width
289 QPixmap paddedImage(QSize(PREVIEW_WIDTH, pixmap.height()));
290 paddedImage.fill(Qt::transparent);
291 QPainter painter(&paddedImage);
292 painter.drawPixmap((PREVIEW_WIDTH - pixmap.width()) / 2, 0, pixmap);
293 m_pix = paddedImage;
294 } else {
295 m_pix = pixmap;
297 m_preview = true;
298 m_generatingPreview = false;
301 void ToolTipManager::previewFailed(const KFileItem& item)
303 Q_UNUSED(item);
304 m_generatingPreview = false;
307 #include "tooltipmanager.moc"