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_parentWidget(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),
46 m_showToolTipTimer
= new QTimer(this);
47 m_showToolTipTimer
->setSingleShot(true);
48 m_showToolTipTimer
->setInterval(500);
49 connect(m_showToolTipTimer
, SIGNAL(timeout()), this, SLOT(showToolTip()));
51 m_contentRetrievalTimer
= new QTimer(this);
52 m_contentRetrievalTimer
->setSingleShot(true);
53 m_contentRetrievalTimer
->setInterval(200);
54 connect(m_contentRetrievalTimer
, SIGNAL(timeout()), this, SLOT(startContentRetrieval()));
56 Q_ASSERT(m_contentRetrievalTimer
->interval() < m_showToolTipTimer
->interval());
59 ToolTipManager::~ToolTipManager()
61 delete m_fileMetaDataToolTip
;
62 m_fileMetaDataToolTip
= 0;
65 void ToolTipManager::showToolTip(const KFileItem
& item
, const QRectF
& itemRect
)
69 m_itemRect
= itemRect
.toRect();
71 const int margin
= toolTipMargin();
72 m_itemRect
.adjust(-margin
, -margin
, margin
, margin
);
75 // Only start the retrieving of the content, when the mouse has been over this
76 // item for 200 milliseconds. This prevents a lot of useless preview jobs and
77 // meta data retrieval, when passing rapidly over a lot of items.
78 Q_ASSERT(!m_fileMetaDataToolTip
);
79 m_fileMetaDataToolTip
= new FileMetaDataToolTip(m_parentWidget
);
80 connect(m_fileMetaDataToolTip
, SIGNAL(metaDataRequestFinished(KFileItemList
)),
81 this, SLOT(slotMetaDataRequestFinished()));
83 m_contentRetrievalTimer
->start();
84 m_showToolTipTimer
->start();
85 m_toolTipRequested
= true;
86 Q_ASSERT(!m_metaDataRequested
);
89 void ToolTipManager::hideToolTip()
91 if (m_appliedWaitCursor
) {
92 QApplication::restoreOverrideCursor();
93 m_appliedWaitCursor
= false;
96 m_toolTipRequested
= false;
97 m_metaDataRequested
= false;
98 m_showToolTipTimer
->stop();
99 m_contentRetrievalTimer
->stop();
101 if (m_fileMetaDataToolTip
) {
102 m_fileMetaDataToolTip
->hide();
103 delete m_fileMetaDataToolTip
;
104 m_fileMetaDataToolTip
= 0;
108 void ToolTipManager::startContentRetrieval()
110 if (!m_toolTipRequested
) {
114 m_fileMetaDataToolTip
->setName(m_item
.text());
116 // Request the retrieval of meta-data. The slot
117 // slotMetaDataRequestFinished() is invoked after the
118 // meta-data have been received.
119 m_metaDataRequested
= true;
120 m_fileMetaDataToolTip
->setItems(KFileItemList() << m_item
);
121 m_fileMetaDataToolTip
->adjustSize();
123 // Request a preview of the item
124 m_fileMetaDataToolTip
->setPreview(QPixmap());
126 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << m_item
, QSize(256, 256));
128 connect(job
, SIGNAL(gotPreview(KFileItem
,QPixmap
)),
129 this, SLOT(setPreviewPix(KFileItem
,QPixmap
)));
130 connect(job
, SIGNAL(failed(KFileItem
)),
131 this, SLOT(previewFailed()));
135 void ToolTipManager::setPreviewPix(const KFileItem
& item
,
136 const QPixmap
& pixmap
)
138 if (!m_toolTipRequested
|| (m_item
.url() != item
.url())) {
139 // No tooltip is requested anymore or an old preview has been received
143 if (pixmap
.isNull()) {
146 m_fileMetaDataToolTip
->setPreview(pixmap
);
147 if (!m_showToolTipTimer
->isActive()) {
153 void ToolTipManager::previewFailed()
155 if (!m_toolTipRequested
) {
159 const QPixmap pixmap
= KIcon(m_item
.iconName()).pixmap(128, 128);
160 m_fileMetaDataToolTip
->setPreview(pixmap
);
161 if (!m_showToolTipTimer
->isActive()) {
166 void ToolTipManager::slotMetaDataRequestFinished()
168 if (!m_toolTipRequested
) {
172 m_metaDataRequested
= false;
174 if (!m_showToolTipTimer
->isActive()) {
179 void ToolTipManager::showToolTip()
181 Q_ASSERT(m_toolTipRequested
);
182 if (m_appliedWaitCursor
) {
183 QApplication::restoreOverrideCursor();
184 m_appliedWaitCursor
= false;
187 if (m_fileMetaDataToolTip
->preview().isNull() || m_metaDataRequested
) {
188 Q_ASSERT(!m_appliedWaitCursor
);
189 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor
));
190 m_appliedWaitCursor
= true;
194 const QRect screen
= QApplication::desktop()->screenGeometry(QCursor::pos());
196 // Restrict tooltip size to current screen size when needed.
197 // Because layout controlling widget doesn't respect widget's maximumSize property
198 // (correct me if I'm wrong), we need to let layout do its work, then manually change
199 // geometry if resulting widget doesn't fit the screen.
201 // Step #1 - make sizeHint return calculated tooltip size
202 m_fileMetaDataToolTip
->layout()->setSizeConstraint(QLayout::SetFixedSize
);
203 m_fileMetaDataToolTip
->adjustSize();
204 QSize size
= m_fileMetaDataToolTip
->sizeHint();
206 // Step #2 - correct tooltip size when needed
207 if (size
.width() > screen
.width()) {
208 size
.setWidth(screen
.width());
210 if (size
.height() > screen
.height()) {
211 size
.setHeight(screen
.height());
214 // m_itemRect defines the area of the item, where the tooltip should be
215 // shown. Per default the tooltip is shown centered at the bottom.
216 // It must be assured that:
217 // - the content is fully visible
218 // - the content is not drawn inside m_itemRect
219 const int margin
= toolTipMargin();
220 const bool hasRoomToLeft
= (m_itemRect
.left() - size
.width() - margin
>= screen
.left());
221 const bool hasRoomToRight
= (m_itemRect
.right() + size
.width() + margin
<= screen
.right());
222 const bool hasRoomAbove
= (m_itemRect
.top() - size
.height() - margin
>= screen
.top());
223 const bool hasRoomBelow
= (m_itemRect
.bottom() + size
.height() + margin
<= screen
.bottom());
224 if (!hasRoomAbove
&& !hasRoomBelow
&& !hasRoomToLeft
&& !hasRoomToRight
) {
229 if (hasRoomBelow
|| hasRoomAbove
) {
230 x
= qMax(screen
.left(), m_itemRect
.center().x() - size
.width() / 2);
231 if (x
+ size
.width() >= screen
.right()) {
232 x
= screen
.right() - size
.width() + 1;
235 y
= m_itemRect
.bottom() + margin
;
237 y
= m_itemRect
.top() - size
.height() - margin
;
240 Q_ASSERT(hasRoomToLeft
|| hasRoomToRight
);
241 if (hasRoomToRight
) {
242 x
= m_itemRect
.right() + margin
;
244 x
= m_itemRect
.left() - size
.width() - margin
;
246 // Put the tooltip at the bottom of the screen. The x-coordinate has already
247 // been adjusted, so that no overlapping with m_itemRect occurs.
248 y
= screen
.bottom() - size
.height() + 1;
251 // Step #3 - Alter tooltip geometry
252 m_fileMetaDataToolTip
->setFixedSize(size
);
253 m_fileMetaDataToolTip
->layout()->setSizeConstraint(QLayout::SetNoConstraint
);
254 m_fileMetaDataToolTip
->move(QPoint(x
, y
));
255 m_fileMetaDataToolTip
->show();
257 m_toolTipRequested
= false;
260 int ToolTipManager::toolTipMargin() const
262 const int margin
= m_parentWidget
->style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth
);
263 return qMax(4, margin
);
266 #include "tooltipmanager.moc"