]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/tooltips/tooltipmanager.cpp
4a9f91359897c03c2565878bbe1d9b666d68a029
[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 #include <QIcon>
24 #include <KIO/JobUiDelegate>
25 #include <KIO/PreviewJob>
26 #include <KJobWidgets>
27 #include <KToolTipWidget>
28
29 #include <QApplication>
30 #include <QDesktopWidget>
31 #include <QLayout>
32 #include <QStyle>
33 #include <QTimer>
34 #include <QWindow>
35
36 ToolTipManager::ToolTipManager(QWidget* parent) :
37 QObject(parent),
38 m_showToolTipTimer(0),
39 m_contentRetrievalTimer(0),
40 m_transientParent(0),
41 m_fileMetaDataWidget(0),
42 m_tooltipWidget(new KToolTipWidget()),
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 delete m_fileMetaDataWidget;
86 m_fileMetaDataWidget = new DolphinFileMetaDataWidget();
87 connect(m_fileMetaDataWidget, &DolphinFileMetaDataWidget::metaDataRequestFinished,
88 this, &ToolTipManager::slotMetaDataRequestFinished);
89
90 m_contentRetrievalTimer->start();
91 m_showToolTipTimer->start();
92 m_toolTipRequested = true;
93 Q_ASSERT(!m_metaDataRequested);
94 }
95
96 void ToolTipManager::hideToolTip()
97 {
98 if (m_appliedWaitCursor) {
99 QApplication::restoreOverrideCursor();
100 m_appliedWaitCursor = false;
101 }
102
103 m_toolTipRequested = false;
104 m_metaDataRequested = false;
105 m_showToolTipTimer->stop();
106 m_contentRetrievalTimer->stop();
107 m_tooltipWidget->hideLater();
108 }
109
110 void ToolTipManager::startContentRetrieval()
111 {
112 if (!m_toolTipRequested) {
113 return;
114 }
115
116 m_fileMetaDataWidget->setName(m_item.text());
117
118 // Request the retrieval of meta-data. The slot
119 // slotMetaDataRequestFinished() is invoked after the
120 // meta-data have been received.
121 m_metaDataRequested = true;
122 m_fileMetaDataWidget->setItems(KFileItemList() << m_item);
123 m_fileMetaDataWidget->adjustSize();
124
125 // Request a preview of the item
126 m_fileMetaDataWidget->setPreview(QPixmap());
127
128 KIO::PreviewJob* job = new KIO::PreviewJob(KFileItemList() << m_item, QSize(256, 256));
129 job->setIgnoreMaximumSize(m_item.isLocalFile());
130 if (job->uiDelegate()) {
131 KJobWidgets::setWindow(job, qApp->activeWindow());
132 }
133
134 connect(job, &KIO::PreviewJob::gotPreview,
135 this, &ToolTipManager::setPreviewPix);
136 connect(job, &KIO::PreviewJob::failed,
137 this, &ToolTipManager::previewFailed);
138 }
139
140
141 void ToolTipManager::setPreviewPix(const KFileItem& item,
142 const QPixmap& pixmap)
143 {
144 if (!m_toolTipRequested || (m_item.url() != item.url())) {
145 // No tooltip is requested anymore or an old preview has been received
146 return;
147 }
148
149 if (pixmap.isNull()) {
150 previewFailed();
151 } else {
152 m_fileMetaDataWidget->setPreview(pixmap);
153 if (!m_showToolTipTimer->isActive()) {
154 showToolTip();
155 }
156 }
157 }
158
159 void ToolTipManager::previewFailed()
160 {
161 if (!m_toolTipRequested) {
162 return;
163 }
164
165 const QPixmap pixmap = QIcon::fromTheme(m_item.iconName()).pixmap(128, 128);
166 m_fileMetaDataWidget->setPreview(pixmap);
167 if (!m_showToolTipTimer->isActive()) {
168 showToolTip();
169 }
170 }
171
172 void ToolTipManager::slotMetaDataRequestFinished()
173 {
174 if (!m_toolTipRequested) {
175 return;
176 }
177
178 m_metaDataRequested = false;
179
180 if (!m_showToolTipTimer->isActive()) {
181 showToolTip();
182 }
183 }
184
185 void ToolTipManager::showToolTip()
186 {
187 Q_ASSERT(m_toolTipRequested);
188 if (m_appliedWaitCursor) {
189 QApplication::restoreOverrideCursor();
190 m_appliedWaitCursor = false;
191 }
192
193 if (m_fileMetaDataWidget->preview().isNull() || m_metaDataRequested) {
194 Q_ASSERT(!m_appliedWaitCursor);
195 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
196 m_appliedWaitCursor = true;
197 return;
198 }
199
200 // Adjust the size to get a proper sizeHint()
201 m_fileMetaDataWidget->adjustSize();
202 m_tooltipWidget->showBelow(m_itemRect, m_fileMetaDataWidget, m_transientParent);
203 m_toolTipRequested = false;
204 }
205