]> cloud.milkyroute.net Git - dolphin.git/blob - src/tooltips/tooltipmanager.cpp
- The selection changed timer only needs to be created for a DolphinView instance.
[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_generatingPreview(false),
54 m_hasDefaultIcon(false),
55 m_previewPixmap()
56 {
57 KToolTip::setToolTipDelegate(g_delegate);
58
59 m_dolphinModel = static_cast<DolphinModel*>(m_proxyModel->sourceModel());
60 connect(parent, SIGNAL(entered(const QModelIndex&)),
61 this, SLOT(requestToolTip(const QModelIndex&)));
62 connect(parent, SIGNAL(viewportEntered()),
63 this, SLOT(hideToolTip()));
64
65 m_timer = new QTimer(this);
66 m_timer->setSingleShot(true);
67 connect(m_timer, SIGNAL(timeout()),
68 this, SLOT(prepareToolTip()));
69
70 m_previewTimer = new QTimer(this);
71 m_previewTimer->setSingleShot(true);
72 connect(m_previewTimer, SIGNAL(timeout()),
73 this, SLOT(startPreviewJob()));
74
75 m_waitOnPreviewTimer = new QTimer(this);
76 m_waitOnPreviewTimer->setSingleShot(true);
77 connect(m_waitOnPreviewTimer, SIGNAL(timeout()),
78 this, SLOT(prepareToolTip()));
79
80 // When the mousewheel is used, the items don't get a hovered indication
81 // (Qt-issue #200665). To assure that the tooltip still gets hidden,
82 // the scrollbars are observed.
83 connect(parent->horizontalScrollBar(), SIGNAL(valueChanged(int)),
84 this, SLOT(hideTip()));
85 connect(parent->verticalScrollBar(), SIGNAL(valueChanged(int)),
86 this, SLOT(hideTip()));
87
88 m_view->viewport()->installEventFilter(this);
89 m_view->installEventFilter(this);
90 }
91
92 ToolTipManager::~ToolTipManager()
93 {
94 }
95
96 void ToolTipManager::hideTip()
97 {
98 hideToolTip();
99 }
100
101 bool ToolTipManager::eventFilter(QObject* watched, QEvent* event)
102 {
103 if (watched == m_view->viewport()) {
104 switch (event->type()) {
105 case QEvent::Leave:
106 case QEvent::MouseButtonPress:
107 hideToolTip();
108 break;
109 default:
110 break;
111 }
112 } else if ((watched == m_view) && (event->type() == QEvent::KeyPress)) {
113 hideToolTip();
114 }
115
116 return QObject::eventFilter(watched, event);
117 }
118
119 void ToolTipManager::requestToolTip(const QModelIndex& index)
120 {
121 // only request a tooltip for the name column and when no selection or
122 // drag & drop operation is done (indicated by the left mouse button)
123 if ((index.column() == DolphinModel::Name) && !(QApplication::mouseButtons() & Qt::LeftButton)) {
124 m_waitOnPreviewTimer->stop();
125 KToolTip::hideTip();
126
127 m_itemRect = m_view->visualRect(index);
128 const QPoint pos = m_view->viewport()->mapToGlobal(m_itemRect.topLeft());
129 m_itemRect.moveTo(pos);
130
131 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
132 m_item = m_dolphinModel->itemForIndex(dirIndex);
133
134 // only start the previewJob when the mouse has been over this item for 200 milliseconds,
135 // this prevents a lot of useless preview jobs when passing rapidly over a lot of items
136 m_previewTimer->start(200);
137 m_previewPixmap = QPixmap();
138 m_hasDefaultIcon = false;
139
140 m_timer->start(500);
141 } else {
142 hideToolTip();
143 }
144 }
145
146 void ToolTipManager::hideToolTip()
147 {
148 m_timer->stop();
149 m_previewTimer->stop();
150 m_waitOnPreviewTimer->stop();
151 KToolTip::hideTip();
152 }
153
154 void ToolTipManager::prepareToolTip()
155 {
156 if (m_generatingPreview) {
157 m_waitOnPreviewTimer->start(250);
158 }
159
160 const QString text = m_item.getToolTipText();
161 if (!m_previewPixmap.isNull()) {
162 showToolTip(KIcon(m_previewPixmap), text);
163 } else if (!m_hasDefaultIcon) {
164 const QPixmap image(KIcon(m_item.iconName()).pixmap(ICON_WIDTH, ICON_HEIGHT));
165 showToolTip(image, text);
166 m_hasDefaultIcon = true;
167 }
168 }
169
170 void ToolTipManager::showToolTip(const QIcon& icon, const QString& text)
171 {
172 if (QApplication::mouseButtons() & Qt::LeftButton) {
173 return;
174 }
175
176 KToolTipItem* tip = new KToolTipItem(icon, text);
177
178 KStyleOptionToolTip option;
179 // TODO: get option content from KToolTip or add KToolTip::sizeHint() method
180 option.direction = QApplication::layoutDirection();
181 option.fontMetrics = QFontMetrics(QToolTip::font());
182 option.activeCorner = KStyleOptionToolTip::TopLeftCorner;
183 option.palette = QToolTip::palette();
184 option.font = QToolTip::font();
185 option.rect = QRect();
186 option.state = QStyle::State_None;
187 option.decorationSize = QSize(32, 32);
188
189 const QSize size = g_delegate->sizeHint(option, *tip);
190 const QRect desktop = QApplication::desktop()->screenGeometry(m_itemRect.bottomRight());
191
192 // m_itemRect defines the area of the item, where the tooltip should be
193 // shown. Per default the tooltip is shown in the bottom right corner.
194 // If the tooltip content exceeds the desktop borders, it must be assured that:
195 // - the content is fully visible
196 // - the content is not drawn inside m_itemRect
197 const bool hasRoomToLeft = (m_itemRect.left() - size.width() >= desktop.left());
198 const bool hasRoomToRight = (m_itemRect.right() + size.width() <= desktop.right());
199 const bool hasRoomAbove = (m_itemRect.top() - size.height() >= desktop.top());
200 const bool hasRoomBelow = (m_itemRect.bottom() + size.height() <= desktop.bottom());
201 if (!hasRoomAbove && !hasRoomBelow && !hasRoomToLeft && !hasRoomToRight) {
202 delete tip;
203 tip = 0;
204 return;
205 }
206
207 int x = 0;
208 int y = 0;
209 if (hasRoomBelow || hasRoomAbove) {
210 x = QCursor::pos().x() + 16; // TODO: use mouse pointer width instead of the magic value of 16
211 if (x + size.width() >= desktop.right()) {
212 x = desktop.right() - size.width();
213 }
214 y = hasRoomBelow ? m_itemRect.bottom() : m_itemRect.top() - size.height();
215 } else {
216 Q_ASSERT(hasRoomToLeft || hasRoomToRight);
217 x = hasRoomToRight ? m_itemRect.right() : m_itemRect.left() - size.width();
218
219 // Put the tooltip at the bottom of the screen. The x-coordinate has already
220 // been adjusted, so that no overlapping with m_itemRect occurs.
221 y = desktop.bottom() - size.height();
222 }
223
224 // the ownership of tip is transferred to KToolTip
225 KToolTip::showTip(QPoint(x, y), tip);
226 }
227
228
229
230 void ToolTipManager::startPreviewJob()
231 {
232 m_generatingPreview = true;
233 KIO::PreviewJob* job = KIO::filePreview(KFileItemList() << m_item,
234 PREVIEW_WIDTH,
235 PREVIEW_HEIGHT);
236
237 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
238 this, SLOT(setPreviewPix(const KFileItem&, const QPixmap&)));
239 connect(job, SIGNAL(failed(const KFileItem&)),
240 this, SLOT(previewFailed()));
241 }
242
243
244 void ToolTipManager::setPreviewPix(const KFileItem& item,
245 const QPixmap& pixmap)
246 {
247 if ((m_item.url() != item.url()) || pixmap.isNull()) {
248 // an old preview or an invalid preview has been received
249 previewFailed();
250 } else {
251 m_previewPixmap = pixmap;
252 m_generatingPreview = false;
253 }
254 }
255
256 void ToolTipManager::previewFailed()
257 {
258 m_generatingPreview = false;
259 }
260
261 #include "tooltipmanager.moc"