]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/tooltips/tooltipmanager.cpp
Merge branch 'master' into frameworks
[dolphin.git] / src / views / 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 "filemetadatatooltip.h"
23 #include <KIcon>
24 #include <KIO/JobUiDelegate>
25 #include <KIO/PreviewJob>
26 #include <KJobWidgets>
27
28 #include <QApplication>
29 #include <QDesktopWidget>
30 #include <QLayout>
31 #include <QScrollArea>
32 #include <QScrollBar>
33 #include <QStyle>
34 #include <QTimer>
35
36 ToolTipManager::ToolTipManager(QWidget* parent) :
37 QObject(parent),
38 m_showToolTipTimer(0),
39 m_contentRetrievalTimer(0),
40 m_fileMetaDataToolTip(0),
41 m_toolTipRequested(false),
42 m_metaDataRequested(false),
43 m_appliedWaitCursor(false),
44 m_margin(4),
45 m_item(),
46 m_itemRect()
47 {
48 if (parent) {
49 m_margin = qMax(m_margin, parent->style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth));
50 }
51
52 m_showToolTipTimer = new QTimer(this);
53 m_showToolTipTimer->setSingleShot(true);
54 m_showToolTipTimer->setInterval(500);
55 connect(m_showToolTipTimer, &QTimer::timeout, this, static_cast<void(ToolTipManager::*)()>(&ToolTipManager::showToolTip));
56
57 m_contentRetrievalTimer = new QTimer(this);
58 m_contentRetrievalTimer->setSingleShot(true);
59 m_contentRetrievalTimer->setInterval(200);
60 connect(m_contentRetrievalTimer, &QTimer::timeout, this, &ToolTipManager::startContentRetrieval);
61
62 Q_ASSERT(m_contentRetrievalTimer->interval() < m_showToolTipTimer->interval());
63 }
64
65 ToolTipManager::~ToolTipManager()
66 {
67 delete m_fileMetaDataToolTip;
68 m_fileMetaDataToolTip = 0;
69 }
70
71 void ToolTipManager::showToolTip(const KFileItem& item, const QRectF& itemRect)
72 {
73 hideToolTip();
74
75 m_itemRect = itemRect.toRect();
76
77 m_itemRect.adjust(-m_margin, -m_margin, m_margin, m_margin);
78 m_item = item;
79
80 // Only start the retrieving of the content, when the mouse has been over this
81 // item for 200 milliseconds. This prevents a lot of useless preview jobs and
82 // meta data retrieval, when passing rapidly over a lot of items.
83 Q_ASSERT(!m_fileMetaDataToolTip);
84 m_fileMetaDataToolTip = new FileMetaDataToolTip();
85 connect(m_fileMetaDataToolTip, &FileMetaDataToolTip::metaDataRequestFinished,
86 this, &ToolTipManager::slotMetaDataRequestFinished);
87
88 m_contentRetrievalTimer->start();
89 m_showToolTipTimer->start();
90 m_toolTipRequested = true;
91 Q_ASSERT(!m_metaDataRequested);
92 }
93
94 void ToolTipManager::hideToolTip()
95 {
96 if (m_appliedWaitCursor) {
97 QApplication::restoreOverrideCursor();
98 m_appliedWaitCursor = false;
99 }
100
101 m_toolTipRequested = false;
102 m_metaDataRequested = false;
103 m_showToolTipTimer->stop();
104 m_contentRetrievalTimer->stop();
105
106 if (m_fileMetaDataToolTip) {
107 m_fileMetaDataToolTip->hide();
108 // Do not delete the tool tip immediately to prevent crashes when
109 // QCoreApplication tries to deliver an 'Enter' event to it, see bug 310579.
110 m_fileMetaDataToolTip->deleteLater();
111 m_fileMetaDataToolTip = 0;
112 }
113 }
114
115 void ToolTipManager::startContentRetrieval()
116 {
117 if (!m_toolTipRequested) {
118 return;
119 }
120
121 m_fileMetaDataToolTip->setName(m_item.text());
122
123 // Request the retrieval of meta-data. The slot
124 // slotMetaDataRequestFinished() is invoked after the
125 // meta-data have been received.
126 m_metaDataRequested = true;
127 m_fileMetaDataToolTip->setItems(KFileItemList() << m_item);
128 m_fileMetaDataToolTip->adjustSize();
129
130 // Request a preview of the item
131 m_fileMetaDataToolTip->setPreview(QPixmap());
132
133 KIO::PreviewJob* job = new KIO::PreviewJob(KFileItemList() << m_item, QSize(256, 256));
134 job->setIgnoreMaximumSize(m_item.isLocalFile());
135 if (job->ui()) {
136 KJobWidgets::setWindow(job, qApp->activeWindow());
137 }
138
139 connect(job, &KIO::PreviewJob::gotPreview,
140 this, &ToolTipManager::setPreviewPix);
141 connect(job, &KIO::PreviewJob::failed,
142 this, &ToolTipManager::previewFailed);
143 }
144
145
146 void ToolTipManager::setPreviewPix(const KFileItem& item,
147 const QPixmap& pixmap)
148 {
149 if (!m_toolTipRequested || (m_item.url() != item.url())) {
150 // No tooltip is requested anymore or an old preview has been received
151 return;
152 }
153
154 if (pixmap.isNull()) {
155 previewFailed();
156 } else {
157 m_fileMetaDataToolTip->setPreview(pixmap);
158 if (!m_showToolTipTimer->isActive()) {
159 showToolTip();
160 }
161 }
162 }
163
164 void ToolTipManager::previewFailed()
165 {
166 if (!m_toolTipRequested) {
167 return;
168 }
169
170 const QPixmap pixmap = KIcon(m_item.iconName()).pixmap(128, 128);
171 m_fileMetaDataToolTip->setPreview(pixmap);
172 if (!m_showToolTipTimer->isActive()) {
173 showToolTip();
174 }
175 }
176
177 void ToolTipManager::slotMetaDataRequestFinished()
178 {
179 if (!m_toolTipRequested) {
180 return;
181 }
182
183 m_metaDataRequested = false;
184
185 if (!m_showToolTipTimer->isActive()) {
186 showToolTip();
187 }
188 }
189
190 void ToolTipManager::showToolTip()
191 {
192 Q_ASSERT(m_toolTipRequested);
193 if (m_appliedWaitCursor) {
194 QApplication::restoreOverrideCursor();
195 m_appliedWaitCursor = false;
196 }
197
198 if (m_fileMetaDataToolTip->preview().isNull() || m_metaDataRequested) {
199 Q_ASSERT(!m_appliedWaitCursor);
200 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
201 m_appliedWaitCursor = true;
202 return;
203 }
204
205 const QRect screen = QApplication::desktop()->screenGeometry(QCursor::pos());
206
207 // Restrict tooltip size to current screen size when needed.
208 // Because layout controlling widget doesn't respect widget's maximumSize property
209 // (correct me if I'm wrong), we need to let layout do its work, then manually change
210 // geometry if resulting widget doesn't fit the screen.
211
212 // Step #1 - make sizeHint return calculated tooltip size
213 m_fileMetaDataToolTip->layout()->setSizeConstraint(QLayout::SetFixedSize);
214 m_fileMetaDataToolTip->adjustSize();
215 QSize size = m_fileMetaDataToolTip->sizeHint();
216
217 // Step #2 - correct tooltip size when needed
218 if (size.width() > screen.width()) {
219 size.setWidth(screen.width());
220 }
221 if (size.height() > screen.height()) {
222 size.setHeight(screen.height());
223 }
224
225 // m_itemRect defines the area of the item, where the tooltip should be
226 // shown. Per default the tooltip is shown centered at the bottom.
227 // It must be assured that:
228 // - the content is fully visible
229 // - the content is not drawn inside m_itemRect
230 const bool hasRoomToLeft = (m_itemRect.left() - size.width() - m_margin >= screen.left());
231 const bool hasRoomToRight = (m_itemRect.right() + size.width() + m_margin <= screen.right());
232 const bool hasRoomAbove = (m_itemRect.top() - size.height() - m_margin >= screen.top());
233 const bool hasRoomBelow = (m_itemRect.bottom() + size.height() + m_margin <= screen.bottom());
234 if (!hasRoomAbove && !hasRoomBelow && !hasRoomToLeft && !hasRoomToRight) {
235 return;
236 }
237
238 int x, y;
239 if (hasRoomBelow || hasRoomAbove) {
240 x = qMax(screen.left(), m_itemRect.center().x() - size.width() / 2);
241 if (x + size.width() >= screen.right()) {
242 x = screen.right() - size.width() + 1;
243 }
244 if (hasRoomBelow) {
245 y = m_itemRect.bottom() + m_margin;
246 } else {
247 y = m_itemRect.top() - size.height() - m_margin;
248 }
249 } else {
250 Q_ASSERT(hasRoomToLeft || hasRoomToRight);
251 if (hasRoomToRight) {
252 x = m_itemRect.right() + m_margin;
253 } else {
254 x = m_itemRect.left() - size.width() - m_margin;
255 }
256 // Put the tooltip at the bottom of the screen. The x-coordinate has already
257 // been adjusted, so that no overlapping with m_itemRect occurs.
258 y = screen.bottom() - size.height() + 1;
259 }
260
261 // Step #3 - Alter tooltip geometry
262 m_fileMetaDataToolTip->setFixedSize(size);
263 m_fileMetaDataToolTip->layout()->setSizeConstraint(QLayout::SetNoConstraint);
264 m_fileMetaDataToolTip->move(QPoint(x, y));
265 m_fileMetaDataToolTip->show();
266
267 m_toolTipRequested = false;
268 }
269