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