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