1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
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 "infosidebarpage.h"
22 #include <config-nepomuk.h>
24 #include <kfileplacesmodel.h>
26 #include <kstandarddirs.h>
27 #include <kio/previewjob.h>
28 #include <kfileitem.h>
30 #include <kglobalsettings.h>
31 #include <kfilemetainfo.h>
32 #include <kiconeffect.h>
33 #include <kseparator.h>
34 #include <kiconloader.h>
37 #include <QInputDialog>
41 #include <QResizeEvent>
43 #include <QVBoxLayout>
45 #include "dolphinsettings.h"
46 #include "metadatawidget.h"
47 #include "metatextlabel.h"
48 #include "pixmapviewer.h"
52 InfoSidebarPage::InfoSidebarPage(QWidget
* parent
) :
54 m_pendingPreview(false),
64 const int spacing
= KDialog::spacingHint();
66 m_timer
= new QTimer(this);
67 m_timer
->setSingleShot(true);
68 connect(m_timer
, SIGNAL(timeout()),
69 this, SLOT(slotTimeout()));
71 QVBoxLayout
* layout
= new QVBoxLayout
;
72 layout
->setSpacing(spacing
);
75 m_nameLabel
= new QLabel(this);
76 QFont font
= m_nameLabel
->font();
78 m_nameLabel
->setFont(font
);
79 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
80 m_nameLabel
->setWordWrap(true);
81 m_nameLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
84 m_preview
= new PixmapViewer(this);
85 m_preview
->setMinimumWidth(KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
);
86 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
88 if (MetaDataWidget::metaDataAvailable()) {
89 // rating, comment and tags
90 m_metaDataWidget
= new MetaDataWidget(this);
91 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
94 // general meta text information
95 m_metaTextLabel
= new MetaTextLabel(this);
96 m_metaTextLabel
->setMinimumWidth(spacing
);
97 m_metaTextLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
99 layout
->addWidget(m_nameLabel
);
100 layout
->addWidget(new KSeparator(this));
101 layout
->addWidget(m_preview
);
102 layout
->addWidget(new KSeparator(this));
103 if (m_metaDataWidget
!= 0) {
104 layout
->addWidget(m_metaDataWidget
);
105 layout
->addWidget(new KSeparator(this));
107 layout
->addWidget(m_metaTextLabel
);
109 // ensure that widgets in the information side bar are aligned towards the top
110 layout
->addStretch(1);
114 InfoSidebarPage::~InfoSidebarPage()
118 QSize
InfoSidebarPage::sizeHint() const
120 QSize size
= SidebarPage::sizeHint();
121 size
.setWidth(minimumSizeHint().width());
125 void InfoSidebarPage::setUrl(const KUrl
& url
)
127 SidebarPage::setUrl(url
);
128 if (url
.isValid() && !m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
135 void InfoSidebarPage::setSelection(const KFileItemList
& selection
)
137 m_selection
= selection
;
139 const int count
= selection
.count();
144 if ((count
== 1) && !selection
.first().url().isEmpty()) {
145 m_urlCandidate
= selection
.first().url();
147 m_timer
->start(TimerDelay
);
151 void InfoSidebarPage::requestDelayedItemInfo(const KFileItem
& item
)
155 m_fileItem
= KFileItem();
157 // The cursor is above the viewport. If files are selected,
158 // show information regarding the selection.
159 if (m_selection
.size() > 0) {
160 m_pendingPreview
= false;
161 m_timer
->start(TimerDelay
);
163 } else if (!item
.url().isEmpty()) {
164 m_urlCandidate
= item
.url();
166 m_timer
->start(TimerDelay
);
170 void InfoSidebarPage::showEvent(QShowEvent
* event
)
172 SidebarPage::showEvent(event
);
173 if (!event
->spontaneous()) {
178 void InfoSidebarPage::resizeEvent(QResizeEvent
* event
)
180 // If the text inside the name label or the info label cannot
181 // get wrapped, then the maximum width of the label is increased
182 // so that the width of the information sidebar gets increased.
183 // To prevent this, the maximum width is adjusted to
184 // the current width of the sidebar.
185 const int maxWidth
= event
->size().width() - KDialog::spacingHint() * 4;
186 m_nameLabel
->setMaximumWidth(maxWidth
);
188 // try to increase the preview as large as possible
189 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
190 m_urlCandidate
= m_shownUrl
; // reset the URL candidate if a resizing is done
191 m_timer
->start(TimerDelay
);
193 SidebarPage::resizeEvent(event
);
196 void InfoSidebarPage::showItemInfo()
204 const KUrl file
= (!m_fileItem
.isNull() || m_selection
.isEmpty()) ? m_shownUrl
: m_selection
[0].url();
205 if (!file
.isValid()) {
209 const int selectionCount
= m_selection
.count();
210 if (m_fileItem
.isNull() && (selectionCount
> 1)) {
211 KIconLoader iconLoader
;
212 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
213 KIconLoader::NoGroup
,
214 KIconLoader::SizeEnormous
);
215 m_preview
->setPixmap(icon
);
216 m_nameLabel
->setText(i18ncp("@info", "%1 item selected", "%1 items selected", selectionCount
));
217 } else if (!applyPlace(file
)) {
218 // try to get a preview pixmap from the item...
222 m_pendingPreview
= true;
224 KIconEffect iconEffect
;
225 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(), KIconLoader::Desktop
, KIconLoader::DisabledState
);
226 m_preview
->setPixmap(disabledPixmap
);
228 KIO::PreviewJob
* job
= KIO::filePreview(list
,
235 job
->setIgnoreMaximumSize(true);
237 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
238 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
239 connect(job
, SIGNAL(failed(const KFileItem
&)),
240 this, SLOT(showIcon(const KFileItem
&)));
242 m_nameLabel
->setText(file
.fileName());
248 void InfoSidebarPage::slotTimeout()
250 m_shownUrl
= m_urlCandidate
;
254 void InfoSidebarPage::showIcon(const KFileItem
& item
)
256 m_pendingPreview
= false;
257 if (!applyPlace(item
.url())) {
258 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
262 void InfoSidebarPage::showPreview(const KFileItem
& item
,
263 const QPixmap
& pixmap
)
266 if (m_pendingPreview
) {
267 m_preview
->setPixmap(pixmap
);
268 m_pendingPreview
= false;
272 bool InfoSidebarPage::applyPlace(const KUrl
& url
)
274 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
275 int count
= placesModel
->rowCount();
277 for (int i
= 0; i
< count
; ++i
) {
278 QModelIndex index
= placesModel
->index(i
, 0);
280 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
281 m_nameLabel
->setText(placesModel
->text(index
));
282 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
290 void InfoSidebarPage::cancelRequest()
295 void InfoSidebarPage::showMetaInfo()
297 m_metaTextLabel
->clear();
299 if ((m_selection
.size() <= 1) || !m_fileItem
.isNull()) {
301 if (m_fileItem
.isNull()) {
302 // no pending request is ongoing
303 const KUrl url
= (m_selection
.size() == 1) ? m_selection
.first().url() : m_shownUrl
;
304 fileItem
= KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, url
);
307 fileItem
= m_fileItem
;
310 if (fileItem
.isDir()) {
311 m_metaTextLabel
->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
312 m_metaTextLabel
->add(i18nc("@label", "Modified:"), fileItem
.timeString());
314 m_metaTextLabel
->add(i18nc("@label", "Type:"), fileItem
.mimeComment());
316 m_metaTextLabel
->add(i18nc("@label", "Size:"), KIO::convertSize(fileItem
.size()));
317 m_metaTextLabel
->add(i18nc("@label", "Modified:"), fileItem
.timeString());
319 if (fileItem
.isLocalFile()) {
320 // TODO: See convertMetaInfo below, find a way to display only interesting information
322 const KFileMetaInfo::WhatFlags flags
= KFileMetaInfo::Fastest
|
323 KFileMetaInfo::TechnicalInfo
|
324 KFileMetaInfo::ContentInfo
;
325 const QString path
= fileItem
.url().path();
326 const KFileMetaInfo
fileMetaInfo(path
, QString(), flags
);
327 if (fileMetaInfo
.isValid()) {
328 const QHash
<QString
, KFileMetaInfoItem
>& items
= fileMetaInfo
.items();
329 QHash
<QString
, KFileMetaInfoItem
>::const_iterator it
= items
.constBegin();
330 const QHash
<QString
, KFileMetaInfoItem
>::const_iterator end
= items
.constEnd();
333 const KFileMetaInfoItem
& metaInfoItem
= it
.value();
334 const QVariant
& value
= metaInfoItem
.value();
335 if (value
.isValid() && convertMetaInfo(metaInfoItem
.name(), labelText
)) {
336 m_metaTextLabel
->add(labelText
, value
.toString());
344 if (m_metaDataWidget
!= 0) {
345 m_metaDataWidget
->setFile(fileItem
.targetUrl());
348 if (m_metaDataWidget
!= 0) {
350 foreach (const KFileItem
& item
, m_selection
) {
351 urls
.append(item
.targetUrl());
353 m_metaDataWidget
->setFiles(urls
);
356 quint64 totalSize
= 0;
357 foreach (const KFileItem
& item
, m_selection
) {
358 // Only count the size of files, not dirs to match what
359 // DolphinViewContainer::selectionStatusBarText() does.
360 if (!item
.isDir() && !item
.isLink()) {
361 totalSize
+= item
.size();
364 m_metaTextLabel
->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
368 bool InfoSidebarPage::convertMetaInfo(const QString
& key
, QString
& text
) const
370 // TODO: This code prevents that interesting meta information might be hidden
371 // and only bypasses the current problem that not all the meta information should
372 // be shown to the user. Check whether it's possible with Nepomuk to show
373 // all "user relevant" information in a readable way...
380 // sorted list of keys, where its data should be shown
381 static const MetaKey keys
[] = {
382 { "audio.album", "Album:" },
383 { "audio.artist", "Artist:" },
384 { "audio.title", "Title:" },
387 // do a binary search for the key...
389 int bottom
= sizeof(keys
) / sizeof(MetaKey
) - 1;
390 while (top
<= bottom
) {
391 const int middle
= (top
+ bottom
) / 2;
392 const int result
= key
.compare(keys
[middle
].key
);
395 } else if (result
> 0) {
398 text
= keys
[middle
].text
;
406 #include "infosidebarpage.moc"