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