]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/tooltips/tooltipmanager.cpp
9e79a8f704a424b0e7e8a1c1eb02d9e055ac94da
[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
29 #include <QApplication>
30 #include <QDesktopWidget>
31 #include <QIcon>
32 #include <QLayout>
33 #include <QStyle>
34 #include <QTimer>
35 #include <QWindow>
36
37 ToolTipManager::ToolTipManager(QWidget* parent) :
38 QObject(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),
46 m_margin(4),
47 m_item(),
48 m_itemRect()
49 {
50 if (parent) {
51 m_margin = qMax(m_margin, parent->style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth));
52 }
53
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));
58
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);
63
64 Q_ASSERT(m_contentRetrievalTimer->interval() < m_showToolTipTimer->interval());
65 }
66
67 ToolTipManager::~ToolTipManager()
68 {
69 }
70
71 void ToolTipManager::showToolTip(const KFileItem& item, const QRectF& itemRect, QWindow *transientParent)
72 {
73 hideToolTip();
74
75 m_itemRect = itemRect.toRect();
76
77 m_itemRect.adjust(-m_margin, -m_margin, m_margin, m_margin);
78 m_item = item;
79
80 m_transientParent = transientParent;
81
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 m_fileMetaDataWidget.reset(new DolphinFileMetaDataWidget());
86 connect(m_fileMetaDataWidget.data(), &DolphinFileMetaDataWidget::metaDataRequestFinished,
87 this, &ToolTipManager::slotMetaDataRequestFinished);
88 connect(m_fileMetaDataWidget.data(), &DolphinFileMetaDataWidget::urlActivated,
89 this, &ToolTipManager::urlActivated);
90
91 m_contentRetrievalTimer->start();
92 m_showToolTipTimer->start();
93 m_toolTipRequested = true;
94 Q_ASSERT(!m_metaDataRequested);
95 }
96
97 void ToolTipManager::hideToolTip()
98 {
99 if (m_appliedWaitCursor) {
100 QApplication::restoreOverrideCursor();
101 m_appliedWaitCursor = false;
102 }
103
104 m_toolTipRequested = false;
105 m_metaDataRequested = false;
106 m_showToolTipTimer->stop();
107 m_contentRetrievalTimer->stop();
108 if (m_tooltipWidget) {
109 m_tooltipWidget->hideLater();
110 }
111 }
112
113 void ToolTipManager::startContentRetrieval()
114 {
115 if (!m_toolTipRequested) {
116 return;
117 }
118
119 m_fileMetaDataWidget->setName(m_item.text());
120
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_fileMetaDataWidget->setItems(KFileItemList() << m_item);
126 m_fileMetaDataWidget->adjustSize();
127
128 // Request a preview of the item
129 m_fileMetaDataWidget->setPreview(QPixmap());
130
131 QStringList plugins = KIO::PreviewJob::availablePlugins();
132 KIO::PreviewJob* job = new KIO::PreviewJob(KFileItemList() << m_item,
133 QSize(256, 256),
134 &plugins);
135 job->setIgnoreMaximumSize(m_item.isLocalFile());
136 if (job->uiDelegate()) {
137 KJobWidgets::setWindow(job, qApp->activeWindow());
138 }
139
140 connect(job, &KIO::PreviewJob::gotPreview,
141 this, &ToolTipManager::setPreviewPix);
142 connect(job, &KIO::PreviewJob::failed,
143 this, &ToolTipManager::previewFailed);
144 }
145
146
147 void ToolTipManager::setPreviewPix(const KFileItem& item,
148 const QPixmap& pixmap)
149 {
150 if (!m_toolTipRequested || (m_item.url() != item.url())) {
151 // No tooltip is requested anymore or an old preview has been received
152 return;
153 }
154
155 if (pixmap.isNull()) {
156 previewFailed();
157 } else {
158 m_fileMetaDataWidget->setPreview(pixmap);
159 if (!m_showToolTipTimer->isActive()) {
160 showToolTip();
161 }
162 }
163 }
164
165 void ToolTipManager::previewFailed()
166 {
167 if (!m_toolTipRequested) {
168 return;
169 }
170
171 const QPixmap pixmap = QIcon::fromTheme(m_item.iconName()).pixmap(128, 128);
172 m_fileMetaDataWidget->setPreview(pixmap);
173 if (!m_showToolTipTimer->isActive()) {
174 showToolTip();
175 }
176 }
177
178 void ToolTipManager::slotMetaDataRequestFinished()
179 {
180 if (!m_toolTipRequested) {
181 return;
182 }
183
184 m_metaDataRequested = false;
185
186 if (!m_showToolTipTimer->isActive()) {
187 showToolTip();
188 }
189 }
190
191 void ToolTipManager::showToolTip()
192 {
193 Q_ASSERT(m_toolTipRequested);
194 if (m_appliedWaitCursor) {
195 QApplication::restoreOverrideCursor();
196 m_appliedWaitCursor = false;
197 }
198
199 if (m_fileMetaDataWidget->preview().isNull() || m_metaDataRequested) {
200 Q_ASSERT(!m_appliedWaitCursor);
201 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
202 m_appliedWaitCursor = true;
203 return;
204 }
205
206 // Adjust the size to get a proper sizeHint()
207 m_fileMetaDataWidget->adjustSize();
208 if (!m_tooltipWidget) {
209 m_tooltipWidget.reset(new KToolTipWidget());
210 }
211 m_tooltipWidget->showBelow(m_itemRect, m_fileMetaDataWidget.data(), m_transientParent);
212 m_toolTipRequested = false;
213 }
214