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 "dolphinfilemetadatawidget.h"
24 #include <KIO/JobUiDelegate>
25 #include <KIO/PreviewJob>
26 #include <KJobWidgets>
27 #include <KToolTipWidget>
29 #include <QApplication>
30 #include <QDesktopWidget>
37 ToolTipManager::ToolTipManager(QWidget
* parent
) :
39 m_showToolTipTimer(nullptr),
40 m_contentRetrievalTimer(nullptr),
41 m_transientParent(nullptr),
42 m_fileMetaDataWidget(nullptr),
43 m_toolTipRequested(false),
44 m_metaDataRequested(false),
45 m_appliedWaitCursor(false),
51 m_margin
= qMax(m_margin
, parent
->style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth
));
54 m_showToolTipTimer
= new QTimer(this);
55 m_showToolTipTimer
->setSingleShot(true);
56 m_showToolTipTimer
->setInterval(500);
57 connect(m_showToolTipTimer
, &QTimer::timeout
, this, static_cast<void(ToolTipManager::*)()>(&ToolTipManager::showToolTip
));
59 m_contentRetrievalTimer
= new QTimer(this);
60 m_contentRetrievalTimer
->setSingleShot(true);
61 m_contentRetrievalTimer
->setInterval(200);
62 connect(m_contentRetrievalTimer
, &QTimer::timeout
, this, &ToolTipManager::startContentRetrieval
);
64 Q_ASSERT(m_contentRetrievalTimer
->interval() < m_showToolTipTimer
->interval());
67 ToolTipManager::~ToolTipManager()
71 void ToolTipManager::showToolTip(const KFileItem
& item
, const QRectF
& itemRect
, QWindow
*transientParent
)
75 m_itemRect
= itemRect
.toRect();
77 m_itemRect
.adjust(-m_margin
, -m_margin
, m_margin
, m_margin
);
80 m_transientParent
= transientParent
;
82 // Only start the retrieving of the content, when the mouse has been over this
83 // item for 200 milliseconds. This prevents a lot of useless preview jobs and
84 // meta data retrieval, when passing rapidly over a lot of items.
85 delete m_fileMetaDataWidget
;
86 m_fileMetaDataWidget
= new DolphinFileMetaDataWidget();
87 connect(m_fileMetaDataWidget
, &DolphinFileMetaDataWidget::metaDataRequestFinished
,
88 this, &ToolTipManager::slotMetaDataRequestFinished
);
89 connect(m_fileMetaDataWidget
, &DolphinFileMetaDataWidget::urlActivated
,
90 this, &ToolTipManager::urlActivated
);
92 m_contentRetrievalTimer
->start();
93 m_showToolTipTimer
->start();
94 m_toolTipRequested
= true;
95 Q_ASSERT(!m_metaDataRequested
);
98 void ToolTipManager::hideToolTip()
100 if (m_appliedWaitCursor
) {
101 QApplication::restoreOverrideCursor();
102 m_appliedWaitCursor
= false;
105 m_toolTipRequested
= false;
106 m_metaDataRequested
= false;
107 m_showToolTipTimer
->stop();
108 m_contentRetrievalTimer
->stop();
109 if (m_tooltipWidget
) {
110 m_tooltipWidget
->hideLater();
114 void ToolTipManager::startContentRetrieval()
116 if (!m_toolTipRequested
) {
120 m_fileMetaDataWidget
->setName(m_item
.text());
122 // Request the retrieval of meta-data. The slot
123 // slotMetaDataRequestFinished() is invoked after the
124 // meta-data have been received.
125 m_metaDataRequested
= true;
126 m_fileMetaDataWidget
->setItems(KFileItemList() << m_item
);
127 m_fileMetaDataWidget
->adjustSize();
129 // Request a preview of the item
130 m_fileMetaDataWidget
->setPreview(QPixmap());
132 QStringList plugins
= KIO::PreviewJob::availablePlugins();
133 KIO::PreviewJob
* job
= new KIO::PreviewJob(KFileItemList() << m_item
,
136 job
->setIgnoreMaximumSize(m_item
.isLocalFile());
137 if (job
->uiDelegate()) {
138 KJobWidgets::setWindow(job
, qApp
->activeWindow());
141 connect(job
, &KIO::PreviewJob::gotPreview
,
142 this, &ToolTipManager::setPreviewPix
);
143 connect(job
, &KIO::PreviewJob::failed
,
144 this, &ToolTipManager::previewFailed
);
148 void ToolTipManager::setPreviewPix(const KFileItem
& item
,
149 const QPixmap
& pixmap
)
151 if (!m_toolTipRequested
|| (m_item
.url() != item
.url())) {
152 // No tooltip is requested anymore or an old preview has been received
156 if (pixmap
.isNull()) {
159 m_fileMetaDataWidget
->setPreview(pixmap
);
160 if (!m_showToolTipTimer
->isActive()) {
166 void ToolTipManager::previewFailed()
168 if (!m_toolTipRequested
) {
172 const QPixmap pixmap
= QIcon::fromTheme(m_item
.iconName()).pixmap(128, 128);
173 m_fileMetaDataWidget
->setPreview(pixmap
);
174 if (!m_showToolTipTimer
->isActive()) {
179 void ToolTipManager::slotMetaDataRequestFinished()
181 if (!m_toolTipRequested
) {
185 m_metaDataRequested
= false;
187 if (!m_showToolTipTimer
->isActive()) {
192 void ToolTipManager::showToolTip()
194 Q_ASSERT(m_toolTipRequested
);
195 if (m_appliedWaitCursor
) {
196 QApplication::restoreOverrideCursor();
197 m_appliedWaitCursor
= false;
200 if (m_fileMetaDataWidget
->preview().isNull() || m_metaDataRequested
) {
201 Q_ASSERT(!m_appliedWaitCursor
);
202 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor
));
203 m_appliedWaitCursor
= true;
207 // Adjust the size to get a proper sizeHint()
208 m_fileMetaDataWidget
->adjustSize();
209 if (!m_tooltipWidget
) {
210 m_tooltipWidget
.reset(new KToolTipWidget());
212 m_tooltipWidget
->showBelow(m_itemRect
, m_fileMetaDataWidget
, m_transientParent
);
213 m_toolTipRequested
= false;