1 /*******************************************************************************
2 * Copyright (C) 2008 by Konstantin Heil <konst.heil@stud.uni-heidelberg.de> *
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. *
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. *
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 *******************************************************************************/
20 #include "tooltipmanager.h"
22 #include "filemetadatatooltip.h"
24 #include <KIO/JobUiDelegate>
25 #include <KIO/PreviewJob>
26 #include <KJobWidgets>
28 #include <QApplication>
29 #include <QDesktopWidget>
31 #include <QScrollArea>
36 ToolTipManager::ToolTipManager(QWidget
* parent
) :
38 m_showToolTipTimer(0),
39 m_contentRetrievalTimer(0),
40 m_fileMetaDataToolTip(0),
41 m_toolTipRequested(false),
42 m_metaDataRequested(false),
43 m_appliedWaitCursor(false),
49 m_margin
= qMax(m_margin
, parent
->style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth
));
52 m_showToolTipTimer
= new QTimer(this);
53 m_showToolTipTimer
->setSingleShot(true);
54 m_showToolTipTimer
->setInterval(500);
55 connect(m_showToolTipTimer
, &QTimer::timeout
, this, static_cast<void(ToolTipManager::*)()>(&ToolTipManager::showToolTip
));
57 m_contentRetrievalTimer
= new QTimer(this);
58 m_contentRetrievalTimer
->setSingleShot(true);
59 m_contentRetrievalTimer
->setInterval(200);
60 connect(m_contentRetrievalTimer
, &QTimer::timeout
, this, &ToolTipManager::startContentRetrieval
);
62 Q_ASSERT(m_contentRetrievalTimer
->interval() < m_showToolTipTimer
->interval());
65 ToolTipManager::~ToolTipManager()
67 delete m_fileMetaDataToolTip
;
68 m_fileMetaDataToolTip
= 0;
71 void ToolTipManager::showToolTip(const KFileItem
& item
, const QRectF
& itemRect
)
75 m_itemRect
= itemRect
.toRect();
77 m_itemRect
.adjust(-m_margin
, -m_margin
, m_margin
, m_margin
);
80 // Only start the retrieving of the content, when the mouse has been over this
81 // item for 200 milliseconds. This prevents a lot of useless preview jobs and
82 // meta data retrieval, when passing rapidly over a lot of items.
83 Q_ASSERT(!m_fileMetaDataToolTip
);
84 m_fileMetaDataToolTip
= new FileMetaDataToolTip();
85 connect(m_fileMetaDataToolTip
, &FileMetaDataToolTip::metaDataRequestFinished
,
86 this, &ToolTipManager::slotMetaDataRequestFinished
);
88 m_contentRetrievalTimer
->start();
89 m_showToolTipTimer
->start();
90 m_toolTipRequested
= true;
91 Q_ASSERT(!m_metaDataRequested
);
94 void ToolTipManager::hideToolTip()
96 if (m_appliedWaitCursor
) {
97 QApplication::restoreOverrideCursor();
98 m_appliedWaitCursor
= false;
101 m_toolTipRequested
= false;
102 m_metaDataRequested
= false;
103 m_showToolTipTimer
->stop();
104 m_contentRetrievalTimer
->stop();
106 if (m_fileMetaDataToolTip
) {
107 m_fileMetaDataToolTip
->hide();
108 // Do not delete the tool tip immediately to prevent crashes when
109 // QCoreApplication tries to deliver an 'Enter' event to it, see bug 310579.
110 m_fileMetaDataToolTip
->deleteLater();
111 m_fileMetaDataToolTip
= 0;
115 void ToolTipManager::startContentRetrieval()
117 if (!m_toolTipRequested
) {
121 m_fileMetaDataToolTip
->setName(m_item
.text());
123 // Request the retrieval of meta-data. The slot
124 // slotMetaDataRequestFinished() is invoked after the
125 // meta-data have been received.
126 m_metaDataRequested
= true;
127 m_fileMetaDataToolTip
->setItems(KFileItemList() << m_item
);
128 m_fileMetaDataToolTip
->adjustSize();
130 // Request a preview of the item
131 m_fileMetaDataToolTip
->setPreview(QPixmap());
133 KIO::PreviewJob
* job
= new KIO::PreviewJob(KFileItemList() << m_item
, QSize(256, 256));
134 job
->setIgnoreMaximumSize(m_item
.isLocalFile());
136 KJobWidgets::setWindow(job
, qApp
->activeWindow());
139 connect(job
, &KIO::PreviewJob::gotPreview
,
140 this, &ToolTipManager::setPreviewPix
);
141 connect(job
, &KIO::PreviewJob::failed
,
142 this, &ToolTipManager::previewFailed
);
146 void ToolTipManager::setPreviewPix(const KFileItem
& item
,
147 const QPixmap
& pixmap
)
149 if (!m_toolTipRequested
|| (m_item
.url() != item
.url())) {
150 // No tooltip is requested anymore or an old preview has been received
154 if (pixmap
.isNull()) {
157 m_fileMetaDataToolTip
->setPreview(pixmap
);
158 if (!m_showToolTipTimer
->isActive()) {
164 void ToolTipManager::previewFailed()
166 if (!m_toolTipRequested
) {
170 const QPixmap pixmap
= KIcon(m_item
.iconName()).pixmap(128, 128);
171 m_fileMetaDataToolTip
->setPreview(pixmap
);
172 if (!m_showToolTipTimer
->isActive()) {
177 void ToolTipManager::slotMetaDataRequestFinished()
179 if (!m_toolTipRequested
) {
183 m_metaDataRequested
= false;
185 if (!m_showToolTipTimer
->isActive()) {
190 void ToolTipManager::showToolTip()
192 Q_ASSERT(m_toolTipRequested
);
193 if (m_appliedWaitCursor
) {
194 QApplication::restoreOverrideCursor();
195 m_appliedWaitCursor
= false;
198 if (m_fileMetaDataToolTip
->preview().isNull() || m_metaDataRequested
) {
199 Q_ASSERT(!m_appliedWaitCursor
);
200 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor
));
201 m_appliedWaitCursor
= true;
205 const QRect screen
= QApplication::desktop()->screenGeometry(QCursor::pos());
207 // Restrict tooltip size to current screen size when needed.
208 // Because layout controlling widget doesn't respect widget's maximumSize property
209 // (correct me if I'm wrong), we need to let layout do its work, then manually change
210 // geometry if resulting widget doesn't fit the screen.
212 // Step #1 - make sizeHint return calculated tooltip size
213 m_fileMetaDataToolTip
->layout()->setSizeConstraint(QLayout::SetFixedSize
);
214 m_fileMetaDataToolTip
->adjustSize();
215 QSize size
= m_fileMetaDataToolTip
->sizeHint();
217 // Step #2 - correct tooltip size when needed
218 if (size
.width() > screen
.width()) {
219 size
.setWidth(screen
.width());
221 if (size
.height() > screen
.height()) {
222 size
.setHeight(screen
.height());
225 // m_itemRect defines the area of the item, where the tooltip should be
226 // shown. Per default the tooltip is shown centered at the bottom.
227 // It must be assured that:
228 // - the content is fully visible
229 // - the content is not drawn inside m_itemRect
230 const bool hasRoomToLeft
= (m_itemRect
.left() - size
.width() - m_margin
>= screen
.left());
231 const bool hasRoomToRight
= (m_itemRect
.right() + size
.width() + m_margin
<= screen
.right());
232 const bool hasRoomAbove
= (m_itemRect
.top() - size
.height() - m_margin
>= screen
.top());
233 const bool hasRoomBelow
= (m_itemRect
.bottom() + size
.height() + m_margin
<= screen
.bottom());
234 if (!hasRoomAbove
&& !hasRoomBelow
&& !hasRoomToLeft
&& !hasRoomToRight
) {
239 if (hasRoomBelow
|| hasRoomAbove
) {
240 x
= qMax(screen
.left(), m_itemRect
.center().x() - size
.width() / 2);
241 if (x
+ size
.width() >= screen
.right()) {
242 x
= screen
.right() - size
.width() + 1;
245 y
= m_itemRect
.bottom() + m_margin
;
247 y
= m_itemRect
.top() - size
.height() - m_margin
;
250 Q_ASSERT(hasRoomToLeft
|| hasRoomToRight
);
251 if (hasRoomToRight
) {
252 x
= m_itemRect
.right() + m_margin
;
254 x
= m_itemRect
.left() - size
.width() - m_margin
;
256 // Put the tooltip at the bottom of the screen. The x-coordinate has already
257 // been adjusted, so that no overlapping with m_itemRect occurs.
258 y
= screen
.bottom() - size
.height() + 1;
261 // Step #3 - Alter tooltip geometry
262 m_fileMetaDataToolTip
->setFixedSize(size
);
263 m_fileMetaDataToolTip
->layout()->setSizeConstraint(QLayout::SetNoConstraint
);
264 m_fileMetaDataToolTip
->move(QPoint(x
, y
));
265 m_fileMetaDataToolTip
->show();
267 m_toolTipRequested
= false;