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 delete m_fileMetaDataToolTip
;
108 m_fileMetaDataToolTip
= 0;
112 void ToolTipManager::startContentRetrieval()
114 if (!m_toolTipRequested
) {
118 m_fileMetaDataToolTip
->setName(m_item
.text());
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_fileMetaDataToolTip
->setItems(KFileItemList() << m_item
);
125 m_fileMetaDataToolTip
->adjustSize();
127 // Request a preview of the item
128 m_fileMetaDataToolTip
->setPreview(QPixmap());
130 KIO::PreviewJob
* job
= new KIO::PreviewJob(KFileItemList() << m_item
, QSize(256, 256));
131 job
->setIgnoreMaximumSize(m_item
.isLocalFile());
133 job
->ui()->setWindow(qApp
->activeWindow());
136 connect(job
, SIGNAL(gotPreview(KFileItem
,QPixmap
)),
137 this, SLOT(setPreviewPix(KFileItem
,QPixmap
)));
138 connect(job
, SIGNAL(failed(KFileItem
)),
139 this, SLOT(previewFailed()));
143 void ToolTipManager::setPreviewPix(const KFileItem
& item
,
144 const QPixmap
& pixmap
)
146 if (!m_toolTipRequested
|| (m_item
.url() != item
.url())) {
147 // No tooltip is requested anymore or an old preview has been received
151 if (pixmap
.isNull()) {
154 m_fileMetaDataToolTip
->setPreview(pixmap
);
155 if (!m_showToolTipTimer
->isActive()) {
161 void ToolTipManager::previewFailed()
163 if (!m_toolTipRequested
) {
167 const QPixmap pixmap
= KIcon(m_item
.iconName()).pixmap(128, 128);
168 m_fileMetaDataToolTip
->setPreview(pixmap
);
169 if (!m_showToolTipTimer
->isActive()) {
174 void ToolTipManager::slotMetaDataRequestFinished()
176 if (!m_toolTipRequested
) {
180 m_metaDataRequested
= false;
182 if (!m_showToolTipTimer
->isActive()) {
187 void ToolTipManager::showToolTip()
189 Q_ASSERT(m_toolTipRequested
);
190 if (m_appliedWaitCursor
) {
191 QApplication::restoreOverrideCursor();
192 m_appliedWaitCursor
= false;
195 if (m_fileMetaDataToolTip
->preview().isNull() || m_metaDataRequested
) {
196 Q_ASSERT(!m_appliedWaitCursor
);
197 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor
));
198 m_appliedWaitCursor
= true;
202 const QRect screen
= QApplication::desktop()->screenGeometry(QCursor::pos());
204 // Restrict tooltip size to current screen size when needed.
205 // Because layout controlling widget doesn't respect widget's maximumSize property
206 // (correct me if I'm wrong), we need to let layout do its work, then manually change
207 // geometry if resulting widget doesn't fit the screen.
209 // Step #1 - make sizeHint return calculated tooltip size
210 m_fileMetaDataToolTip
->layout()->setSizeConstraint(QLayout::SetFixedSize
);
211 m_fileMetaDataToolTip
->adjustSize();
212 QSize size
= m_fileMetaDataToolTip
->sizeHint();
214 // Step #2 - correct tooltip size when needed
215 if (size
.width() > screen
.width()) {
216 size
.setWidth(screen
.width());
218 if (size
.height() > screen
.height()) {
219 size
.setHeight(screen
.height());
222 // m_itemRect defines the area of the item, where the tooltip should be
223 // shown. Per default the tooltip is shown centered at the bottom.
224 // It must be assured that:
225 // - the content is fully visible
226 // - the content is not drawn inside m_itemRect
227 const bool hasRoomToLeft
= (m_itemRect
.left() - size
.width() - m_margin
>= screen
.left());
228 const bool hasRoomToRight
= (m_itemRect
.right() + size
.width() + m_margin
<= screen
.right());
229 const bool hasRoomAbove
= (m_itemRect
.top() - size
.height() - m_margin
>= screen
.top());
230 const bool hasRoomBelow
= (m_itemRect
.bottom() + size
.height() + m_margin
<= screen
.bottom());
231 if (!hasRoomAbove
&& !hasRoomBelow
&& !hasRoomToLeft
&& !hasRoomToRight
) {
236 if (hasRoomBelow
|| hasRoomAbove
) {
237 x
= qMax(screen
.left(), m_itemRect
.center().x() - size
.width() / 2);
238 if (x
+ size
.width() >= screen
.right()) {
239 x
= screen
.right() - size
.width() + 1;
242 y
= m_itemRect
.bottom() + m_margin
;
244 y
= m_itemRect
.top() - size
.height() - m_margin
;
247 Q_ASSERT(hasRoomToLeft
|| hasRoomToRight
);
248 if (hasRoomToRight
) {
249 x
= m_itemRect
.right() + m_margin
;
251 x
= m_itemRect
.left() - size
.width() - m_margin
;
253 // Put the tooltip at the bottom of the screen. The x-coordinate has already
254 // been adjusted, so that no overlapping with m_itemRect occurs.
255 y
= screen
.bottom() - size
.height() + 1;
258 // Step #3 - Alter tooltip geometry
259 m_fileMetaDataToolTip
->setFixedSize(size
);
260 m_fileMetaDataToolTip
->layout()->setSizeConstraint(QLayout::SetNoConstraint
);
261 m_fileMetaDataToolTip
->move(QPoint(x
, y
));
262 m_fileMetaDataToolTip
->show();
264 m_toolTipRequested
= false;
267 #include "tooltipmanager.moc"