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