]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/tooltips/tooltipmanager.cpp
aae97458cfd6bbadfb606840366fec6b7765da7e
[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 "dolphinfilemetadatawidget.h"
23
24 #include <KIO/JobUiDelegate>
25 #include <KIO/PreviewJob>
26 #include <KJobWidgets>
27 #include <KToolTipWidget>
28 #include <KIconLoader>
29
30 #include <QApplication>
31 #include <QDesktopWidget>
32 #include <QIcon>
33 #include <QLayout>
34 #include <QStyle>
35 #include <QTimer>
36 #include <QWindow>
37
38 class IconLoaderSingleton {
39 public:
40 IconLoaderSingleton() = default;
41
42 KIconLoader self;
43 };
44
45 Q_GLOBAL_STATIC(IconLoaderSingleton, iconLoader)
46
47 ToolTipManager::ToolTipManager(QWidget* parent) :
48 QObject(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),
56 m_margin(4),
57 m_item(),
58 m_itemRect()
59 {
60 if (parent) {
61 m_margin = qMax(m_margin, parent->style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth));
62 }
63
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));
68
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);
73
74 Q_ASSERT(m_contentRetrievalTimer->interval() < m_showToolTipTimer->interval());
75 }
76
77 ToolTipManager::~ToolTipManager()
78 {
79 }
80
81 void ToolTipManager::showToolTip(const KFileItem& item, const QRectF& itemRect, QWindow *transientParent)
82 {
83 hideToolTip();
84
85 m_itemRect = itemRect.toRect();
86
87 m_itemRect.adjust(-m_margin, -m_margin, m_margin, m_margin);
88 m_item = item;
89
90 m_transientParent = transientParent;
91
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);
100
101 m_contentRetrievalTimer->start();
102 m_showToolTipTimer->start();
103 m_toolTipRequested = true;
104 Q_ASSERT(!m_metaDataRequested);
105 }
106
107 void ToolTipManager::hideToolTip()
108 {
109 if (m_appliedWaitCursor) {
110 QApplication::restoreOverrideCursor();
111 m_appliedWaitCursor = false;
112 }
113
114 m_toolTipRequested = false;
115 m_metaDataRequested = false;
116 m_showToolTipTimer->stop();
117 m_contentRetrievalTimer->stop();
118 if (m_tooltipWidget) {
119 m_tooltipWidget->hideLater();
120 }
121 }
122
123 void ToolTipManager::startContentRetrieval()
124 {
125 if (!m_toolTipRequested) {
126 return;
127 }
128
129 m_fileMetaDataWidget->setName(m_item.text());
130
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();
137
138 // Request a preview of the item
139 m_fileMetaDataWidget->setPreview(QPixmap());
140
141 QStringList plugins = KIO::PreviewJob::availablePlugins();
142 KIO::PreviewJob* job = new KIO::PreviewJob(KFileItemList() << m_item,
143 QSize(256, 256),
144 &plugins);
145 job->setIgnoreMaximumSize(m_item.isLocalFile());
146 if (job->uiDelegate()) {
147 KJobWidgets::setWindow(job, qApp->activeWindow());
148 }
149
150 connect(job, &KIO::PreviewJob::gotPreview,
151 this, &ToolTipManager::setPreviewPix);
152 connect(job, &KIO::PreviewJob::failed,
153 this, &ToolTipManager::previewFailed);
154 }
155
156
157 void ToolTipManager::setPreviewPix(const KFileItem& item,
158 const QPixmap& pixmap)
159 {
160 if (!m_toolTipRequested || (m_item.url() != item.url())) {
161 // No tooltip is requested anymore or an old preview has been received
162 return;
163 }
164
165 if (pixmap.isNull()) {
166 previewFailed();
167 } else {
168 m_fileMetaDataWidget->setPreview(pixmap);
169 if (!m_showToolTipTimer->isActive()) {
170 showToolTip();
171 }
172 }
173 }
174
175 void ToolTipManager::previewFailed()
176 {
177 if (!m_toolTipRequested) {
178 return;
179 }
180 QPalette pal;
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());
184 }
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()) {
189 showToolTip();
190 }
191 }
192
193 void ToolTipManager::slotMetaDataRequestFinished()
194 {
195 if (!m_toolTipRequested) {
196 return;
197 }
198
199 m_metaDataRequested = false;
200
201 if (!m_showToolTipTimer->isActive()) {
202 showToolTip();
203 }
204 }
205
206 void ToolTipManager::showToolTip()
207 {
208 Q_ASSERT(m_toolTipRequested);
209 if (m_appliedWaitCursor) {
210 QApplication::restoreOverrideCursor();
211 m_appliedWaitCursor = false;
212 }
213
214 if (m_fileMetaDataWidget->preview().isNull() || m_metaDataRequested) {
215 Q_ASSERT(!m_appliedWaitCursor);
216 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
217 m_appliedWaitCursor = true;
218 return;
219 }
220
221 // Adjust the size to get a proper sizeHint()
222 m_fileMetaDataWidget->adjustSize();
223 if (!m_tooltipWidget) {
224 m_tooltipWidget.reset(new KToolTipWidget());
225 }
226 m_tooltipWidget->showBelow(m_itemRect, m_fileMetaDataWidget.data(), m_transientParent);
227 m_toolTipRequested = false;
228 }
229