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