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 "filemetadatatooltip.h"
24 #include <KIO/JobUiDelegate>
25 #include <KIO/PreviewJob>
27 #include <QApplication>
28 #include <QDesktopWidget>
30 #include <QScrollArea>
35 ToolTipManager::ToolTipManager(QWidget
* parent
) :
37 m_showToolTipTimer(0),
38 m_contentRetrievalTimer(0),
39 m_fileMetaDataToolTip(0),
40 m_toolTipRequested(false),
41 m_metaDataRequested(false),
42 m_appliedWaitCursor(false),
48 m_margin
= qMax(m_margin
, parent
->style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth
));
51 m_showToolTipTimer
= new QTimer(this);
52 m_showToolTipTimer
->setSingleShot(true);
53 m_showToolTipTimer
->setInterval(500);
54 connect(m_showToolTipTimer
, SIGNAL(timeout()), this, SLOT(showToolTip()));
56 m_contentRetrievalTimer
= new QTimer(this);
57 m_contentRetrievalTimer
->setSingleShot(true);
58 m_contentRetrievalTimer
->setInterval(200);
59 connect(m_contentRetrievalTimer
, SIGNAL(timeout()), this, SLOT(startContentRetrieval()));
61 Q_ASSERT(m_contentRetrievalTimer
->interval() < m_showToolTipTimer
->interval());
64 ToolTipManager::~ToolTipManager()
66 delete m_fileMetaDataToolTip
;
67 m_fileMetaDataToolTip
= 0;
70 void ToolTipManager::showToolTip(const KFileItem
& item
, const QRectF
& itemRect
)
74 m_itemRect
= itemRect
.toRect();
76 m_itemRect
.adjust(-m_margin
, -m_margin
, m_margin
, m_margin
);
79 // Only start the retrieving of the content, when the mouse has been over this
80 // item for 200 milliseconds. This prevents a lot of useless preview jobs and
81 // meta data retrieval, when passing rapidly over a lot of items.
82 Q_ASSERT(!m_fileMetaDataToolTip
);
83 m_fileMetaDataToolTip
= new FileMetaDataToolTip();
84 connect(m_fileMetaDataToolTip
, SIGNAL(metaDataRequestFinished(KFileItemList
)),
85 this, SLOT(slotMetaDataRequestFinished()));
87 m_contentRetrievalTimer
->start();
88 m_showToolTipTimer
->start();
89 m_toolTipRequested
= true;
90 Q_ASSERT(!m_metaDataRequested
);
93 void ToolTipManager::hideToolTip()
95 if (m_appliedWaitCursor
) {
96 QApplication::restoreOverrideCursor();
97 m_appliedWaitCursor
= false;
100 m_toolTipRequested
= false;
101 m_metaDataRequested
= false;
102 m_showToolTipTimer
->stop();
103 m_contentRetrievalTimer
->stop();
105 if (m_fileMetaDataToolTip
) {
106 m_fileMetaDataToolTip
->hide();
107 // Do not delete the tool tip immediately to prevent crashes when
108 // QCoreApplication tries to deliver an 'Enter' event to it, see bug 310579.
109 m_fileMetaDataToolTip
->deleteLater();
110 m_fileMetaDataToolTip
= 0;
114 void ToolTipManager::startContentRetrieval()
116 if (!m_toolTipRequested
) {
120 m_fileMetaDataToolTip
->setName(m_item
.text());
122 // Request the retrieval of meta-data. The slot
123 // slotMetaDataRequestFinished() is invoked after the
124 // meta-data have been received.
125 m_metaDataRequested
= true;
126 m_fileMetaDataToolTip
->setItems(KFileItemList() << m_item
);
127 m_fileMetaDataToolTip
->adjustSize();
129 // Request a preview of the item
130 m_fileMetaDataToolTip
->setPreview(QPixmap());
132 KIO::PreviewJob
* job
= new KIO::PreviewJob(KFileItemList() << m_item
, QSize(256, 256));
133 job
->setIgnoreMaximumSize(m_item
.isLocalFile());
135 job
->ui()->setWindow(qApp
->activeWindow());
138 connect(job
, SIGNAL(gotPreview(KFileItem
,QPixmap
)),
139 this, SLOT(setPreviewPix(KFileItem
,QPixmap
)));
140 connect(job
, SIGNAL(failed(KFileItem
)),
141 this, SLOT(previewFailed()));
145 void ToolTipManager::setPreviewPix(const KFileItem
& item
,
146 const QPixmap
& pixmap
)
148 if (!m_toolTipRequested
|| (m_item
.url() != item
.url())) {
149 // No tooltip is requested anymore or an old preview has been received
153 if (pixmap
.isNull()) {
156 m_fileMetaDataToolTip
->setPreview(pixmap
);
157 if (!m_showToolTipTimer
->isActive()) {
163 void ToolTipManager::previewFailed()
165 if (!m_toolTipRequested
) {
169 const QPixmap pixmap
= KIcon(m_item
.iconName()).pixmap(128, 128);
170 m_fileMetaDataToolTip
->setPreview(pixmap
);
171 if (!m_showToolTipTimer
->isActive()) {
176 void ToolTipManager::slotMetaDataRequestFinished()
178 if (!m_toolTipRequested
) {
182 m_metaDataRequested
= false;
184 if (!m_showToolTipTimer
->isActive()) {
189 void ToolTipManager::showToolTip()
191 Q_ASSERT(m_toolTipRequested
);
192 if (m_appliedWaitCursor
) {
193 QApplication::restoreOverrideCursor();
194 m_appliedWaitCursor
= false;
197 if (m_fileMetaDataToolTip
->preview().isNull() || m_metaDataRequested
) {
198 Q_ASSERT(!m_appliedWaitCursor
);
199 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor
));
200 m_appliedWaitCursor
= true;
204 const QRect screen
= QApplication::desktop()->screenGeometry(QCursor::pos());
206 // Restrict tooltip size to current screen size when needed.
207 // Because layout controlling widget doesn't respect widget's maximumSize property
208 // (correct me if I'm wrong), we need to let layout do its work, then manually change
209 // geometry if resulting widget doesn't fit the screen.
211 // Step #1 - make sizeHint return calculated tooltip size
212 m_fileMetaDataToolTip
->layout()->setSizeConstraint(QLayout::SetFixedSize
);
213 m_fileMetaDataToolTip
->adjustSize();
214 QSize size
= m_fileMetaDataToolTip
->sizeHint();
216 // Step #2 - correct tooltip size when needed
217 if (size
.width() > screen
.width()) {
218 size
.setWidth(screen
.width());
220 if (size
.height() > screen
.height()) {
221 size
.setHeight(screen
.height());
224 // m_itemRect defines the area of the item, where the tooltip should be
225 // shown. Per default the tooltip is shown centered at the bottom.
226 // It must be assured that:
227 // - the content is fully visible
228 // - the content is not drawn inside m_itemRect
229 const bool hasRoomToLeft
= (m_itemRect
.left() - size
.width() - m_margin
>= screen
.left());
230 const bool hasRoomToRight
= (m_itemRect
.right() + size
.width() + m_margin
<= screen
.right());
231 const bool hasRoomAbove
= (m_itemRect
.top() - size
.height() - m_margin
>= screen
.top());
232 const bool hasRoomBelow
= (m_itemRect
.bottom() + size
.height() + m_margin
<= screen
.bottom());
233 if (!hasRoomAbove
&& !hasRoomBelow
&& !hasRoomToLeft
&& !hasRoomToRight
) {
238 if (hasRoomBelow
|| hasRoomAbove
) {
239 x
= qMax(screen
.left(), m_itemRect
.center().x() - size
.width() / 2);
240 if (x
+ size
.width() >= screen
.right()) {
241 x
= screen
.right() - size
.width() + 1;
244 y
= m_itemRect
.bottom() + m_margin
;
246 y
= m_itemRect
.top() - size
.height() - m_margin
;
249 Q_ASSERT(hasRoomToLeft
|| hasRoomToRight
);
250 if (hasRoomToRight
) {
251 x
= m_itemRect
.right() + m_margin
;
253 x
= m_itemRect
.left() - size
.width() - m_margin
;
255 // Put the tooltip at the bottom of the screen. The x-coordinate has already
256 // been adjusted, so that no overlapping with m_itemRect occurs.
257 y
= screen
.bottom() - size
.height() + 1;
260 // Step #3 - Alter tooltip geometry
261 m_fileMetaDataToolTip
->setFixedSize(size
);
262 m_fileMetaDataToolTip
->layout()->setSizeConstraint(QLayout::SetNoConstraint
);
263 m_fileMetaDataToolTip
->move(QPoint(x
, y
));
264 m_fileMetaDataToolTip
->show();
266 m_toolTipRequested
= false;
269 #include "tooltipmanager.moc"