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/PreviewJob>
26 #include <QApplication>
27 #include <QDesktopWidget>
29 #include <QScrollArea>
34 ToolTipManager::ToolTipManager(QWidget
* parent
) :
36 m_showToolTipTimer(0),
37 m_contentRetrievalTimer(0),
38 m_fileMetaDataToolTip(0),
39 m_toolTipRequested(false),
40 m_metaDataRequested(false),
41 m_appliedWaitCursor(false),
47 m_margin
= qMax(m_margin
, parent
->style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth
));
50 m_showToolTipTimer
= new QTimer(this);
51 m_showToolTipTimer
->setSingleShot(true);
52 m_showToolTipTimer
->setInterval(500);
53 connect(m_showToolTipTimer
, SIGNAL(timeout()), this, SLOT(showToolTip()));
55 m_contentRetrievalTimer
= new QTimer(this);
56 m_contentRetrievalTimer
->setSingleShot(true);
57 m_contentRetrievalTimer
->setInterval(200);
58 connect(m_contentRetrievalTimer
, SIGNAL(timeout()), this, SLOT(startContentRetrieval()));
60 Q_ASSERT(m_contentRetrievalTimer
->interval() < m_showToolTipTimer
->interval());
63 ToolTipManager::~ToolTipManager()
65 delete m_fileMetaDataToolTip
;
66 m_fileMetaDataToolTip
= 0;
69 void ToolTipManager::showToolTip(const KFileItem
& item
, const QRectF
& itemRect
)
73 m_itemRect
= itemRect
.toRect();
75 m_itemRect
.adjust(-m_margin
, -m_margin
, m_margin
, m_margin
);
78 // Only start the retrieving of the content, when the mouse has been over this
79 // item for 200 milliseconds. This prevents a lot of useless preview jobs and
80 // meta data retrieval, when passing rapidly over a lot of items.
81 Q_ASSERT(!m_fileMetaDataToolTip
);
82 m_fileMetaDataToolTip
= new FileMetaDataToolTip();
83 connect(m_fileMetaDataToolTip
, SIGNAL(metaDataRequestFinished(KFileItemList
)),
84 this, SLOT(slotMetaDataRequestFinished()));
86 m_contentRetrievalTimer
->start();
87 m_showToolTipTimer
->start();
88 m_toolTipRequested
= true;
89 Q_ASSERT(!m_metaDataRequested
);
92 void ToolTipManager::hideToolTip()
94 if (m_appliedWaitCursor
) {
95 QApplication::restoreOverrideCursor();
96 m_appliedWaitCursor
= false;
99 m_toolTipRequested
= false;
100 m_metaDataRequested
= false;
101 m_showToolTipTimer
->stop();
102 m_contentRetrievalTimer
->stop();
104 if (m_fileMetaDataToolTip
) {
105 m_fileMetaDataToolTip
->hide();
106 delete m_fileMetaDataToolTip
;
107 m_fileMetaDataToolTip
= 0;
111 void ToolTipManager::startContentRetrieval()
113 if (!m_toolTipRequested
) {
117 m_fileMetaDataToolTip
->setName(m_item
.text());
119 // Request the retrieval of meta-data. The slot
120 // slotMetaDataRequestFinished() is invoked after the
121 // meta-data have been received.
122 m_metaDataRequested
= true;
123 m_fileMetaDataToolTip
->setItems(KFileItemList() << m_item
);
124 m_fileMetaDataToolTip
->adjustSize();
126 // Request a preview of the item
127 m_fileMetaDataToolTip
->setPreview(QPixmap());
129 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << m_item
, QSize(256, 256));
131 connect(job
, SIGNAL(gotPreview(KFileItem
,QPixmap
)),
132 this, SLOT(setPreviewPix(KFileItem
,QPixmap
)));
133 connect(job
, SIGNAL(failed(KFileItem
)),
134 this, SLOT(previewFailed()));
138 void ToolTipManager::setPreviewPix(const KFileItem
& item
,
139 const QPixmap
& pixmap
)
141 if (!m_toolTipRequested
|| (m_item
.url() != item
.url())) {
142 // No tooltip is requested anymore or an old preview has been received
146 if (pixmap
.isNull()) {
149 m_fileMetaDataToolTip
->setPreview(pixmap
);
150 if (!m_showToolTipTimer
->isActive()) {
156 void ToolTipManager::previewFailed()
158 if (!m_toolTipRequested
) {
162 const QPixmap pixmap
= KIcon(m_item
.iconName()).pixmap(128, 128);
163 m_fileMetaDataToolTip
->setPreview(pixmap
);
164 if (!m_showToolTipTimer
->isActive()) {
169 void ToolTipManager::slotMetaDataRequestFinished()
171 if (!m_toolTipRequested
) {
175 m_metaDataRequested
= false;
177 if (!m_showToolTipTimer
->isActive()) {
182 void ToolTipManager::showToolTip()
184 Q_ASSERT(m_toolTipRequested
);
185 if (m_appliedWaitCursor
) {
186 QApplication::restoreOverrideCursor();
187 m_appliedWaitCursor
= false;
190 if (m_fileMetaDataToolTip
->preview().isNull() || m_metaDataRequested
) {
191 Q_ASSERT(!m_appliedWaitCursor
);
192 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor
));
193 m_appliedWaitCursor
= true;
197 const QRect screen
= QApplication::desktop()->screenGeometry(QCursor::pos());
199 // Restrict tooltip size to current screen size when needed.
200 // Because layout controlling widget doesn't respect widget's maximumSize property
201 // (correct me if I'm wrong), we need to let layout do its work, then manually change
202 // geometry if resulting widget doesn't fit the screen.
204 // Step #1 - make sizeHint return calculated tooltip size
205 m_fileMetaDataToolTip
->layout()->setSizeConstraint(QLayout::SetFixedSize
);
206 m_fileMetaDataToolTip
->adjustSize();
207 QSize size
= m_fileMetaDataToolTip
->sizeHint();
209 // Step #2 - correct tooltip size when needed
210 if (size
.width() > screen
.width()) {
211 size
.setWidth(screen
.width());
213 if (size
.height() > screen
.height()) {
214 size
.setHeight(screen
.height());
217 // m_itemRect defines the area of the item, where the tooltip should be
218 // shown. Per default the tooltip is shown centered at the bottom.
219 // It must be assured that:
220 // - the content is fully visible
221 // - the content is not drawn inside m_itemRect
222 const bool hasRoomToLeft
= (m_itemRect
.left() - size
.width() - m_margin
>= screen
.left());
223 const bool hasRoomToRight
= (m_itemRect
.right() + size
.width() + m_margin
<= screen
.right());
224 const bool hasRoomAbove
= (m_itemRect
.top() - size
.height() - m_margin
>= screen
.top());
225 const bool hasRoomBelow
= (m_itemRect
.bottom() + size
.height() + m_margin
<= screen
.bottom());
226 if (!hasRoomAbove
&& !hasRoomBelow
&& !hasRoomToLeft
&& !hasRoomToRight
) {
231 if (hasRoomBelow
|| hasRoomAbove
) {
232 x
= qMax(screen
.left(), m_itemRect
.center().x() - size
.width() / 2);
233 if (x
+ size
.width() >= screen
.right()) {
234 x
= screen
.right() - size
.width() + 1;
237 y
= m_itemRect
.bottom() + m_margin
;
239 y
= m_itemRect
.top() - size
.height() - m_margin
;
242 Q_ASSERT(hasRoomToLeft
|| hasRoomToRight
);
243 if (hasRoomToRight
) {
244 x
= m_itemRect
.right() + m_margin
;
246 x
= m_itemRect
.left() - size
.width() - m_margin
;
248 // Put the tooltip at the bottom of the screen. The x-coordinate has already
249 // been adjusted, so that no overlapping with m_itemRect occurs.
250 y
= screen
.bottom() - size
.height() + 1;
253 // Step #3 - Alter tooltip geometry
254 m_fileMetaDataToolTip
->setFixedSize(size
);
255 m_fileMetaDataToolTip
->layout()->setSizeConstraint(QLayout::SetNoConstraint
);
256 m_fileMetaDataToolTip
->move(QPoint(x
, y
));
257 m_fileMetaDataToolTip
->show();
259 m_toolTipRequested
= false;
262 #include "tooltipmanager.moc"