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>
34 ToolTipManager::ToolTipManager(QWidget
* 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),
47 m_margin
= qMax(m_margin
, parent
->style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth
));
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
));
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
);
60 Q_ASSERT(m_contentRetrievalTimer
->interval() < m_showToolTipTimer
->interval());
63 ToolTipManager::~ToolTipManager()
65 delete m_fileMetaDataToolTip
;
66 m_fileMetaDataToolTip
= 0;
69 void ToolTipManager::showToolTip(const KFileItem
& item
, const QRectF
& itemRect
)
73 m_itemRect
= itemRect
.toRect();
75 m_itemRect
.adjust(-m_margin
, -m_margin
, m_margin
, m_margin
);
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
);
86 m_contentRetrievalTimer
->start();
87 m_showToolTipTimer
->start();
88 m_toolTipRequested
= true;
89 Q_ASSERT(!m_metaDataRequested
);
92 void ToolTipManager::hideToolTip()
94 if (m_appliedWaitCursor
) {
95 QApplication::restoreOverrideCursor();
96 m_appliedWaitCursor
= false;
99 m_toolTipRequested
= false;
100 m_metaDataRequested
= false;
101 m_showToolTipTimer
->stop();
102 m_contentRetrievalTimer
->stop();
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;
113 void ToolTipManager::startContentRetrieval()
115 if (!m_toolTipRequested
) {
119 m_fileMetaDataToolTip
->setName(m_item
.text());
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();
128 // Request a preview of the item
129 m_fileMetaDataToolTip
->setPreview(QPixmap());
131 KIO::PreviewJob
* job
= new KIO::PreviewJob(KFileItemList() << m_item
, QSize(256, 256));
132 job
->setIgnoreMaximumSize(m_item
.isLocalFile());
134 KJobWidgets::setWindow(job
, qApp
->activeWindow());
137 connect(job
, &KIO::PreviewJob::gotPreview
,
138 this, &ToolTipManager::setPreviewPix
);
139 connect(job
, &KIO::PreviewJob::failed
,
140 this, &ToolTipManager::previewFailed
);
144 void ToolTipManager::setPreviewPix(const KFileItem
& item
,
145 const QPixmap
& pixmap
)
147 if (!m_toolTipRequested
|| (m_item
.url() != item
.url())) {
148 // No tooltip is requested anymore or an old preview has been received
152 if (pixmap
.isNull()) {
155 m_fileMetaDataToolTip
->setPreview(pixmap
);
156 if (!m_showToolTipTimer
->isActive()) {
162 void ToolTipManager::previewFailed()
164 if (!m_toolTipRequested
) {
168 const QPixmap pixmap
= QIcon::fromTheme(m_item
.iconName()).pixmap(128, 128);
169 m_fileMetaDataToolTip
->setPreview(pixmap
);
170 if (!m_showToolTipTimer
->isActive()) {
175 void ToolTipManager::slotMetaDataRequestFinished()
177 if (!m_toolTipRequested
) {
181 m_metaDataRequested
= false;
183 if (!m_showToolTipTimer
->isActive()) {
188 void ToolTipManager::showToolTip()
190 Q_ASSERT(m_toolTipRequested
);
191 if (m_appliedWaitCursor
) {
192 QApplication::restoreOverrideCursor();
193 m_appliedWaitCursor
= false;
196 if (m_fileMetaDataToolTip
->preview().isNull() || m_metaDataRequested
) {
197 Q_ASSERT(!m_appliedWaitCursor
);
198 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor
));
199 m_appliedWaitCursor
= true;
203 const QRect screen
= QApplication::desktop()->screenGeometry(QCursor::pos());
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.
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();
215 // Step #2 - correct tooltip size when needed
216 if (size
.width() > screen
.width()) {
217 size
.setWidth(screen
.width());
219 if (size
.height() > screen
.height()) {
220 size
.setHeight(screen
.height());
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
) {
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;
243 y
= m_itemRect
.bottom() + m_margin
;
245 y
= m_itemRect
.top() - size
.height() - m_margin
;
248 Q_ASSERT(hasRoomToLeft
|| hasRoomToRight
);
249 if (hasRoomToRight
) {
250 x
= m_itemRect
.right() + m_margin
;
252 x
= m_itemRect
.left() - size
.width() - m_margin
;
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;
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();
265 m_toolTipRequested
= false;