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