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>
29 #include <QApplication>
30 #include <QDesktopWidget>
36 ToolTipManager::ToolTipManager(QWidget
* parent
) :
38 m_showToolTipTimer(0),
39 m_contentRetrievalTimer(0),
41 m_fileMetaDataWidget(0),
42 m_tooltipWidget(new KToolTipWidget()),
43 m_toolTipRequested(false),
44 m_metaDataRequested(false),
45 m_appliedWaitCursor(false),
51 m_margin
= qMax(m_margin
, parent
->style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth
));
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
));
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
);
64 Q_ASSERT(m_contentRetrievalTimer
->interval() < m_showToolTipTimer
->interval());
67 ToolTipManager::~ToolTipManager()
71 void ToolTipManager::showToolTip(const KFileItem
& item
, const QRectF
& itemRect
, QWindow
*transientParent
)
75 m_itemRect
= itemRect
.toRect();
77 m_itemRect
.adjust(-m_margin
, -m_margin
, m_margin
, m_margin
);
80 m_transientParent
= transientParent
;
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
);
90 m_contentRetrievalTimer
->start();
91 m_showToolTipTimer
->start();
92 m_toolTipRequested
= true;
93 Q_ASSERT(!m_metaDataRequested
);
96 void ToolTipManager::hideToolTip()
98 if (m_appliedWaitCursor
) {
99 QApplication::restoreOverrideCursor();
100 m_appliedWaitCursor
= false;
103 m_toolTipRequested
= false;
104 m_metaDataRequested
= false;
105 m_showToolTipTimer
->stop();
106 m_contentRetrievalTimer
->stop();
107 m_tooltipWidget
->hideLater();
110 void ToolTipManager::startContentRetrieval()
112 if (!m_toolTipRequested
) {
116 m_fileMetaDataWidget
->setName(m_item
.text());
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();
125 // Request a preview of the item
126 m_fileMetaDataWidget
->setPreview(QPixmap());
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());
134 connect(job
, &KIO::PreviewJob::gotPreview
,
135 this, &ToolTipManager::setPreviewPix
);
136 connect(job
, &KIO::PreviewJob::failed
,
137 this, &ToolTipManager::previewFailed
);
141 void ToolTipManager::setPreviewPix(const KFileItem
& item
,
142 const QPixmap
& pixmap
)
144 if (!m_toolTipRequested
|| (m_item
.url() != item
.url())) {
145 // No tooltip is requested anymore or an old preview has been received
149 if (pixmap
.isNull()) {
152 m_fileMetaDataWidget
->setPreview(pixmap
);
153 if (!m_showToolTipTimer
->isActive()) {
159 void ToolTipManager::previewFailed()
161 if (!m_toolTipRequested
) {
165 const QPixmap pixmap
= QIcon::fromTheme(m_item
.iconName()).pixmap(128, 128);
166 m_fileMetaDataWidget
->setPreview(pixmap
);
167 if (!m_showToolTipTimer
->isActive()) {
172 void ToolTipManager::slotMetaDataRequestFinished()
174 if (!m_toolTipRequested
) {
178 m_metaDataRequested
= false;
180 if (!m_showToolTipTimer
->isActive()) {
185 void ToolTipManager::showToolTip()
187 Q_ASSERT(m_toolTipRequested
);
188 if (m_appliedWaitCursor
) {
189 QApplication::restoreOverrideCursor();
190 m_appliedWaitCursor
= false;
193 if (m_fileMetaDataWidget
->preview().isNull() || m_metaDataRequested
) {
194 Q_ASSERT(!m_appliedWaitCursor
);
195 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor
));
196 m_appliedWaitCursor
= true;
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;