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>
28 #include <KIconLoader>
30 #include <QApplication>
31 #include <QDesktopWidget>
38 class IconLoaderSingleton
{
40 IconLoaderSingleton() = default;
45 Q_GLOBAL_STATIC(IconLoaderSingleton
, iconLoader
)
47 ToolTipManager::ToolTipManager(QWidget
* parent
) :
49 m_showToolTipTimer(nullptr),
50 m_contentRetrievalTimer(nullptr),
51 m_transientParent(nullptr),
52 m_fileMetaDataWidget(nullptr),
53 m_toolTipRequested(false),
54 m_metaDataRequested(false),
55 m_appliedWaitCursor(false),
61 m_margin
= qMax(m_margin
, parent
->style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth
));
64 m_showToolTipTimer
= new QTimer(this);
65 m_showToolTipTimer
->setSingleShot(true);
66 m_showToolTipTimer
->setInterval(500);
67 connect(m_showToolTipTimer
, &QTimer::timeout
, this, QOverload
<>::of(&ToolTipManager::showToolTip
));
69 m_contentRetrievalTimer
= new QTimer(this);
70 m_contentRetrievalTimer
->setSingleShot(true);
71 m_contentRetrievalTimer
->setInterval(200);
72 connect(m_contentRetrievalTimer
, &QTimer::timeout
, this, &ToolTipManager::startContentRetrieval
);
74 Q_ASSERT(m_contentRetrievalTimer
->interval() < m_showToolTipTimer
->interval());
77 ToolTipManager::~ToolTipManager()
81 void ToolTipManager::showToolTip(const KFileItem
& item
, const QRectF
& itemRect
, QWindow
*transientParent
)
85 m_itemRect
= itemRect
.toRect();
87 m_itemRect
.adjust(-m_margin
, -m_margin
, m_margin
, m_margin
);
90 m_transientParent
= transientParent
;
92 // Only start the retrieving of the content, when the mouse has been over this
93 // item for 200 milliseconds. This prevents a lot of useless preview jobs and
94 // meta data retrieval, when passing rapidly over a lot of items.
95 m_fileMetaDataWidget
.reset(new DolphinFileMetaDataWidget());
96 connect(m_fileMetaDataWidget
.data(), &DolphinFileMetaDataWidget::metaDataRequestFinished
,
97 this, &ToolTipManager::slotMetaDataRequestFinished
);
98 connect(m_fileMetaDataWidget
.data(), &DolphinFileMetaDataWidget::urlActivated
,
99 this, &ToolTipManager::urlActivated
);
101 m_contentRetrievalTimer
->start();
102 m_showToolTipTimer
->start();
103 m_toolTipRequested
= true;
104 Q_ASSERT(!m_metaDataRequested
);
107 void ToolTipManager::hideToolTip()
109 if (m_appliedWaitCursor
) {
110 QApplication::restoreOverrideCursor();
111 m_appliedWaitCursor
= false;
114 m_toolTipRequested
= false;
115 m_metaDataRequested
= false;
116 m_showToolTipTimer
->stop();
117 m_contentRetrievalTimer
->stop();
118 if (m_tooltipWidget
) {
119 m_tooltipWidget
->hideLater();
123 void ToolTipManager::startContentRetrieval()
125 if (!m_toolTipRequested
) {
129 m_fileMetaDataWidget
->setName(m_item
.text());
131 // Request the retrieval of meta-data. The slot
132 // slotMetaDataRequestFinished() is invoked after the
133 // meta-data have been received.
134 m_metaDataRequested
= true;
135 m_fileMetaDataWidget
->setItems(KFileItemList() << m_item
);
136 m_fileMetaDataWidget
->adjustSize();
138 // Request a preview of the item
139 m_fileMetaDataWidget
->setPreview(QPixmap());
141 QStringList plugins
= KIO::PreviewJob::availablePlugins();
142 KIO::PreviewJob
* job
= new KIO::PreviewJob(KFileItemList() << m_item
,
145 job
->setIgnoreMaximumSize(m_item
.isLocalFile());
146 if (job
->uiDelegate()) {
147 KJobWidgets::setWindow(job
, qApp
->activeWindow());
150 connect(job
, &KIO::PreviewJob::gotPreview
,
151 this, &ToolTipManager::setPreviewPix
);
152 connect(job
, &KIO::PreviewJob::failed
,
153 this, &ToolTipManager::previewFailed
);
157 void ToolTipManager::setPreviewPix(const KFileItem
& item
,
158 const QPixmap
& pixmap
)
160 if (!m_toolTipRequested
|| (m_item
.url() != item
.url())) {
161 // No tooltip is requested anymore or an old preview has been received
165 if (pixmap
.isNull()) {
168 m_fileMetaDataWidget
->setPreview(pixmap
);
169 if (!m_showToolTipTimer
->isActive()) {
175 void ToolTipManager::previewFailed()
177 if (!m_toolTipRequested
) {
181 for (auto state
: { QPalette::Active
, QPalette::Inactive
, QPalette::Disabled
}) {
182 pal
.setBrush(state
, QPalette::WindowText
, pal
.toolTipText());
183 pal
.setBrush(state
, QPalette::Window
, pal
.toolTipBase());
185 iconLoader
->self
.setCustomPalette(pal
);
186 const QPixmap pixmap
= KDE::icon(m_item
.iconName(), &iconLoader
->self
).pixmap(128, 128);
187 m_fileMetaDataWidget
->setPreview(pixmap
);
188 if (!m_showToolTipTimer
->isActive()) {
193 void ToolTipManager::slotMetaDataRequestFinished()
195 if (!m_toolTipRequested
) {
199 m_metaDataRequested
= false;
201 if (!m_showToolTipTimer
->isActive()) {
206 void ToolTipManager::showToolTip()
208 Q_ASSERT(m_toolTipRequested
);
209 if (m_appliedWaitCursor
) {
210 QApplication::restoreOverrideCursor();
211 m_appliedWaitCursor
= false;
214 if (m_fileMetaDataWidget
->preview().isNull() || m_metaDataRequested
) {
215 Q_ASSERT(!m_appliedWaitCursor
);
216 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor
));
217 m_appliedWaitCursor
= true;
221 // Adjust the size to get a proper sizeHint()
222 m_fileMetaDataWidget
->adjustSize();
223 if (!m_tooltipWidget
) {
224 m_tooltipWidget
.reset(new KToolTipWidget());
226 m_tooltipWidget
->showBelow(m_itemRect
, m_fileMetaDataWidget
.data(), m_transientParent
);
227 m_toolTipRequested
= false;