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