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