]> cloud.milkyroute.net Git - dolphin.git/blob - src/tooltips/tooltipmanager.cpp
- Don't keep pointers to objects that are obtained and deleted by other objects.
[dolphin.git] / src / tooltips / tooltipmanager.cpp
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 *******************************************************************************/
19
20 #include "tooltipmanager.h"
21
22 #include "dolphintooltip.h"
23 #include "dolphinmodel.h"
24 #include "dolphinsortfilterproxymodel.h"
25
26 #include <kicon.h>
27 #include <tooltips/ktooltip.h>
28 #include <kio/previewjob.h>
29
30 #include <QApplication>
31 #include <QDesktopWidget>
32 #include <QScrollBar>
33 #include <QTimer>
34 #include <QToolTip>
35
36 const int ICON_WIDTH = 128;
37 const int ICON_HEIGHT = 128;
38 const int PREVIEW_DELAY = 250;
39
40 K_GLOBAL_STATIC(DolphinBalloonTooltipDelegate, g_delegate)
41
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_pix()
58 {
59 KToolTip::setToolTipDelegate(g_delegate);
60
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()));
66
67 m_timer = new QTimer(this);
68 m_timer->setSingleShot(true);
69 connect(m_timer, SIGNAL(timeout()),
70 this, SLOT(prepareToolTip()));
71
72 m_previewTimer = new QTimer(this);
73 m_previewTimer->setSingleShot(true);
74 connect(m_previewTimer, SIGNAL(timeout()),
75 this, SLOT(startPreviewJob()));
76
77 m_waitOnPreviewTimer = new QTimer(this);
78 m_waitOnPreviewTimer->setSingleShot(true);
79 connect(m_waitOnPreviewTimer, SIGNAL(timeout()),
80 this, SLOT(prepareToolTip()));
81
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()));
89
90 m_view->viewport()->installEventFilter(this);
91 }
92
93 ToolTipManager::~ToolTipManager()
94 {
95 }
96
97 void ToolTipManager::hideTip()
98 {
99 hideToolTip();
100 }
101
102 bool ToolTipManager::eventFilter(QObject* watched, QEvent* event)
103 {
104 if ((watched == m_view->viewport()) && (event->type() == QEvent::Leave)) {
105 hideToolTip();
106 }
107
108 return QObject::eventFilter(watched, event);
109 }
110
111 void ToolTipManager::requestToolTip(const QModelIndex& index)
112 {
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();
117 KToolTip::hideTip();
118
119 m_itemRect = m_view->visualRect(index);
120 const QPoint pos = m_view->viewport()->mapToGlobal(m_itemRect.topLeft());
121 m_itemRect.moveTo(pos);
122
123 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
124 m_item = m_dolphinModel->itemForIndex(dirIndex);
125
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);
129 m_preview = false;
130 m_previewIsLate = false;
131 m_previewPass = 0;
132
133 m_timer->start(500);
134 } else {
135 hideToolTip();
136 }
137 }
138
139 void ToolTipManager::hideToolTip()
140 {
141 m_timer->stop();
142 m_previewTimer->stop();
143 m_waitOnPreviewTimer->stop();
144 m_previewIsLate = false;
145 KToolTip::hideTip();
146 }
147
148 void ToolTipManager::prepareToolTip()
149 {
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());
158 }
159
160 ++m_previewPass;
161 m_waitOnPreviewTimer->start(250);
162 } else {
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());
168 return;
169 }
170
171 KIcon icon;
172 if (m_preview) {
173 // We got a preview.
174 icon = KIcon(m_pix);
175 } else {
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));
179 }
180
181 showToolTip(icon, m_item.getToolTipText());
182 }
183 }
184
185 void ToolTipManager::showToolTip(const QIcon& icon, const QString& text)
186 {
187 if (QApplication::mouseButtons() & Qt::LeftButton) {
188 return;
189 }
190
191 KToolTipItem* tip = new KToolTipItem(icon, m_item.getToolTipText());
192
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);
203
204 QSize size;
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);
209 }
210 else if (tip != 0) {
211 size = g_delegate->sizeHint(option, *tip);
212 }
213 const QRect desktop = QApplication::desktop()->screenGeometry(m_itemRect.bottomRight());
214
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) {
225 delete tip;
226 tip = 0;
227 return;
228 }
229
230 int x = 0;
231 int y = 0;
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();
236 }
237 y = hasRoomBelow ? m_itemRect.bottom() : m_itemRect.top() - size.height();
238 } else {
239 Q_ASSERT(hasRoomToLeft || hasRoomToRight);
240 x = hasRoomToRight ? m_itemRect.right() : m_itemRect.left() - size.width();
241
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();
245 }
246
247 // the ownership of tip is transferred to KToolTip
248 KToolTip::showTip(QPoint(x, y), tip);
249 }
250
251
252
253 void ToolTipManager::startPreviewJob()
254 {
255 m_generatingPreview = true;
256 KIO::PreviewJob* job = KIO::filePreview(KFileItemList() << m_item,
257 PREVIEW_WIDTH,
258 PREVIEW_HEIGHT);
259
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&)));
264 }
265
266
267 void ToolTipManager::setPreviewPix(const KFileItem& item,
268 const QPixmap& pixmap)
269 {
270 if ((m_item.url() != item.url()) || pixmap.isNull()) {
271 m_generatingPreview = false;
272 return;
273 }
274
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);
281 m_pix = paddedImage;
282 } else {
283 m_pix = pixmap;
284 }
285 m_preview = true;
286 m_generatingPreview = false;
287 }
288
289 void ToolTipManager::previewFailed(const KFileItem& item)
290 {
291 Q_UNUSED(item);
292 m_generatingPreview = false;
293 }
294
295 #include "tooltipmanager.moc"