]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/tooltips/tooltipmanager.cpp
Ignore maximum size for local files when creating previews
[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 = new KIO::PreviewJob(KFileItemList() << m_item, QSize(256, 256));
130 job->setIgnoreMaximumSize(m_item.isLocalFile());
131
132 connect(job, SIGNAL(gotPreview(KFileItem,QPixmap)),
133 this, SLOT(setPreviewPix(KFileItem,QPixmap)));
134 connect(job, SIGNAL(failed(KFileItem)),
135 this, SLOT(previewFailed()));
136 }
137
138
139 void ToolTipManager::setPreviewPix(const KFileItem& item,
140 const QPixmap& pixmap)
141 {
142 if (!m_toolTipRequested || (m_item.url() != item.url())) {
143 // No tooltip is requested anymore or an old preview has been received
144 return;
145 }
146
147 if (pixmap.isNull()) {
148 previewFailed();
149 } else {
150 m_fileMetaDataToolTip->setPreview(pixmap);
151 if (!m_showToolTipTimer->isActive()) {
152 showToolTip();
153 }
154 }
155 }
156
157 void ToolTipManager::previewFailed()
158 {
159 if (!m_toolTipRequested) {
160 return;
161 }
162
163 const QPixmap pixmap = KIcon(m_item.iconName()).pixmap(128, 128);
164 m_fileMetaDataToolTip->setPreview(pixmap);
165 if (!m_showToolTipTimer->isActive()) {
166 showToolTip();
167 }
168 }
169
170 void ToolTipManager::slotMetaDataRequestFinished()
171 {
172 if (!m_toolTipRequested) {
173 return;
174 }
175
176 m_metaDataRequested = false;
177
178 if (!m_showToolTipTimer->isActive()) {
179 showToolTip();
180 }
181 }
182
183 void ToolTipManager::showToolTip()
184 {
185 Q_ASSERT(m_toolTipRequested);
186 if (m_appliedWaitCursor) {
187 QApplication::restoreOverrideCursor();
188 m_appliedWaitCursor = false;
189 }
190
191 if (m_fileMetaDataToolTip->preview().isNull() || m_metaDataRequested) {
192 Q_ASSERT(!m_appliedWaitCursor);
193 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
194 m_appliedWaitCursor = true;
195 return;
196 }
197
198 const QRect screen = QApplication::desktop()->screenGeometry(QCursor::pos());
199
200 // Restrict tooltip size to current screen size when needed.
201 // Because layout controlling widget doesn't respect widget's maximumSize property
202 // (correct me if I'm wrong), we need to let layout do its work, then manually change
203 // geometry if resulting widget doesn't fit the screen.
204
205 // Step #1 - make sizeHint return calculated tooltip size
206 m_fileMetaDataToolTip->layout()->setSizeConstraint(QLayout::SetFixedSize);
207 m_fileMetaDataToolTip->adjustSize();
208 QSize size = m_fileMetaDataToolTip->sizeHint();
209
210 // Step #2 - correct tooltip size when needed
211 if (size.width() > screen.width()) {
212 size.setWidth(screen.width());
213 }
214 if (size.height() > screen.height()) {
215 size.setHeight(screen.height());
216 }
217
218 // m_itemRect defines the area of the item, where the tooltip should be
219 // shown. Per default the tooltip is shown centered at the bottom.
220 // It must be assured that:
221 // - the content is fully visible
222 // - the content is not drawn inside m_itemRect
223 const bool hasRoomToLeft = (m_itemRect.left() - size.width() - m_margin >= screen.left());
224 const bool hasRoomToRight = (m_itemRect.right() + size.width() + m_margin <= screen.right());
225 const bool hasRoomAbove = (m_itemRect.top() - size.height() - m_margin >= screen.top());
226 const bool hasRoomBelow = (m_itemRect.bottom() + size.height() + m_margin <= screen.bottom());
227 if (!hasRoomAbove && !hasRoomBelow && !hasRoomToLeft && !hasRoomToRight) {
228 return;
229 }
230
231 int x, y;
232 if (hasRoomBelow || hasRoomAbove) {
233 x = qMax(screen.left(), m_itemRect.center().x() - size.width() / 2);
234 if (x + size.width() >= screen.right()) {
235 x = screen.right() - size.width() + 1;
236 }
237 if (hasRoomBelow) {
238 y = m_itemRect.bottom() + m_margin;
239 } else {
240 y = m_itemRect.top() - size.height() - m_margin;
241 }
242 } else {
243 Q_ASSERT(hasRoomToLeft || hasRoomToRight);
244 if (hasRoomToRight) {
245 x = m_itemRect.right() + m_margin;
246 } else {
247 x = m_itemRect.left() - size.width() - m_margin;
248 }
249 // Put the tooltip at the bottom of the screen. The x-coordinate has already
250 // been adjusted, so that no overlapping with m_itemRect occurs.
251 y = screen.bottom() - size.height() + 1;
252 }
253
254 // Step #3 - Alter tooltip geometry
255 m_fileMetaDataToolTip->setFixedSize(size);
256 m_fileMetaDataToolTip->layout()->setSizeConstraint(QLayout::SetNoConstraint);
257 m_fileMetaDataToolTip->move(QPoint(x, y));
258 m_fileMetaDataToolTip->show();
259
260 m_toolTipRequested = false;
261 }
262
263 #include "tooltipmanager.moc"