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 <kseparator.h>
33 #include <kiconloader.h>
36 #include <QInputDialog>
39 #include <QResizeEvent>
41 #include <QVBoxLayout>
43 #include "dolphinsettings.h"
44 #include "metadatawidget.h"
45 #include "metatextlabel.h"
46 #include "pixmapviewer.h"
48 InfoSidebarPage::InfoSidebarPage(QWidget
* parent
) :
50 m_pendingPreview(false),
59 const int spacing
= KDialog::spacingHint();
61 m_timer
= new QTimer(this);
62 m_timer
->setSingleShot(true);
63 connect(m_timer
, SIGNAL(timeout()),
64 this, SLOT(slotTimeout()));
66 QVBoxLayout
* layout
= new QVBoxLayout
;
67 layout
->setSpacing(spacing
);
70 m_nameLabel
= new QLabel(this);
71 QFont font
= m_nameLabel
->font();
73 m_nameLabel
->setFont(font
);
74 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
75 m_nameLabel
->setWordWrap(true);
78 m_preview
= new PixmapViewer(this);
79 m_preview
->setMinimumWidth(KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
);
80 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
82 if (MetaDataWidget::metaDataAvailable()) {
83 // rating, comment and tags
84 m_metaDataWidget
= new MetaDataWidget(this);
87 // general meta text information
88 m_metaTextLabel
= new MetaTextLabel(this);
89 m_metaTextLabel
->setMinimumWidth(spacing
);
91 layout
->addWidget(m_nameLabel
);
92 layout
->addWidget(m_preview
);
93 if (m_metaDataWidget
!= 0) {
94 layout
->addWidget(m_metaDataWidget
);
96 layout
->addWidget(m_metaTextLabel
);
98 // ensure that widgets in the information side bar are aligned towards the top
99 layout
->addStretch(1);
103 InfoSidebarPage::~InfoSidebarPage()
107 QSize
InfoSidebarPage::sizeHint() const
109 QSize size
= SidebarPage::sizeHint();
110 size
.setWidth(minimumSizeHint().width());
114 void InfoSidebarPage::setUrl(const KUrl
& url
)
116 if (url
.isValid() && !m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
123 void InfoSidebarPage::setSelection(const KFileItemList
& selection
)
125 SidebarPage::setSelection(selection
);
126 if (selection
.size() == 1) {
127 const KUrl url
= selection
.first().url();
128 if (!url
.isEmpty()) {
129 m_urlCandidate
= url
;
132 m_timer
->start(TimerDelay
);
135 void InfoSidebarPage::requestDelayedItemInfo(const KFileItem
& item
)
139 m_fileItem
= KFileItem();
141 if (!item
.isNull() && (selection().size() <= 1)) {
142 const KUrl url
= item
.url();
143 if (!url
.isEmpty()) {
144 m_urlCandidate
= url
;
146 m_timer
->start(TimerDelay
);
151 void InfoSidebarPage::showEvent(QShowEvent
* event
)
153 SidebarPage::showEvent(event
);
154 if (event
->spontaneous()) {
160 void InfoSidebarPage::resizeEvent(QResizeEvent
* event
)
162 // If the text inside the name label or the info label cannot
163 // get wrapped, then the maximum width of the label is increased
164 // so that the width of the information sidebar gets increased.
165 // To prevent this, the maximum width is adjusted to
166 // the current width of the sidebar.
167 const int maxWidth
= event
->size().width() - KDialog::spacingHint() * 4;
168 m_nameLabel
->setMaximumWidth(maxWidth
);
170 // try to increase the preview as large as possible
171 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
172 m_urlCandidate
= m_shownUrl
; // reset the URL candidate if a resizing is done
173 m_timer
->start(TimerDelay
);
175 SidebarPage::resizeEvent(event
);
178 void InfoSidebarPage::showItemInfo()
186 const KFileItemList
& selectedItems
= selection();
189 if (selectedItems
.isEmpty()) {
192 file
= selectedItems
[0].url();
194 if (!file
.isValid()) {
198 const int itemCount
= selectedItems
.count();
200 KIconLoader iconLoader
;
201 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
202 KIconLoader::NoGroup
,
203 KIconLoader::SizeEnormous
);
204 m_preview
->setPixmap(icon
);
205 m_nameLabel
->setText(i18ncp("@info", "%1 item selected", "%1 items selected", selectedItems
.count()));
206 } else if (!applyPlace(file
)) {
207 // try to get a preview pixmap from the item...
211 m_pendingPreview
= true;
212 m_preview
->setPixmap(QPixmap());
214 KIO::PreviewJob
* job
= KIO::filePreview(list
,
221 job
->setIgnoreMaximumSize(true);
223 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
224 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
225 connect(job
, SIGNAL(failed(const KFileItem
&)),
226 this, SLOT(showIcon(const KFileItem
&)));
228 m_nameLabel
->setText(file
.fileName());
234 void InfoSidebarPage::slotTimeout()
236 m_shownUrl
= m_urlCandidate
;
240 void InfoSidebarPage::showIcon(const KFileItem
& item
)
242 m_pendingPreview
= false;
243 if (!applyPlace(item
.url())) {
244 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
248 void InfoSidebarPage::showPreview(const KFileItem
& item
,
249 const QPixmap
& pixmap
)
252 if (m_pendingPreview
) {
253 m_preview
->setPixmap(pixmap
);
254 m_pendingPreview
= false;
258 bool InfoSidebarPage::applyPlace(const KUrl
& url
)
260 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
261 int count
= placesModel
->rowCount();
263 for (int i
= 0; i
< count
; ++i
) {
264 QModelIndex index
= placesModel
->index(i
, 0);
266 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
267 m_nameLabel
->setText(placesModel
->text(index
));
268 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
276 void InfoSidebarPage::cancelRequest()
281 void InfoSidebarPage::showMetaInfo()
283 m_metaTextLabel
->clear();
285 const KFileItemList
& selectedItems
= selection();
286 if (selectedItems
.size() <= 1) {
288 if (m_fileItem
.isNull()) {
289 // no pending request is ongoing
290 fileItem
= KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
293 fileItem
= m_fileItem
;
296 if (fileItem
.isDir()) {
297 m_metaTextLabel
->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
299 m_metaTextLabel
->add(i18nc("@label", "Type:"), fileItem
.mimeComment());
301 m_metaTextLabel
->add(i18nc("@label", "Size:"), KIO::convertSize(fileItem
.size()));
302 m_metaTextLabel
->add(i18nc("@label", "Modified:"), fileItem
.timeString());
304 // TODO: See convertMetaInfo below, find a way to display only interesting information
306 const KFileMetaInfo::WhatFlags flags
= KFileMetaInfo::Fastest
|
307 KFileMetaInfo::TechnicalInfo
|
308 KFileMetaInfo::ContentInfo
|
309 KFileMetaInfo::Thumbnail
;
310 const QString path
= fileItem
.url().url();
311 const KFileMetaInfo
fileMetaInfo(path
, QString(), flags
);
312 if (fileMetaInfo
.isValid()) {
313 const QHash
<QString
, KFileMetaInfoItem
>& items
= fileMetaInfo
.items();
314 QHash
<QString
, KFileMetaInfoItem
>::const_iterator it
= items
.constBegin();
315 const QHash
<QString
, KFileMetaInfoItem
>::const_iterator end
= items
.constEnd();
318 const KFileMetaInfoItem
& metaInfoItem
= it
.value();
319 const QVariant
& value
= metaInfoItem
.value();
320 if (value
.isValid() && convertMetaInfo(metaInfoItem
.name(), labelText
)) {
321 m_metaTextLabel
->add(labelText
, value
.toString());
328 if (m_metaDataWidget
!= 0) {
329 m_metaDataWidget
->setFile(fileItem
.url());
332 if (m_metaDataWidget
!= 0) {
334 foreach (const KFileItem
& item
, selectedItems
) {
335 urls
.append(item
.url());
337 m_metaDataWidget
->setFiles(urls
);
340 unsigned long int totalSize
= 0;
341 foreach (const KFileItem
& item
, selectedItems
) {
342 // Only count the size of files, not dirs to match what
343 // DolphinViewContainer::selectionStatusBarText() does.
344 if (!item
.isDir() && !item
.isLink()) {
345 totalSize
+= item
.size();
348 m_metaTextLabel
->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
352 bool InfoSidebarPage::convertMetaInfo(const QString
& key
, QString
& text
) const
354 // TODO: This code prevents that interesting meta information might be hidden
355 // and only bypasses the current problem that not all the meta information should
356 // be shown to the user. Check whether it's possible with Nepomuk to show
357 // all "user relevant" information in a readable way...
364 // sorted list of keys, where its data should be shown
365 static const MetaKey keys
[] = {
366 { "audio.album", "Album:" },
367 { "audio.artist", "Artist:" },
368 { "audio.title", "Title:" },
371 // do a binary search for the key...
373 int bottom
= sizeof(keys
) / sizeof(MetaKey
) - 1;
374 while (top
<= bottom
) {
375 const int middle
= (top
+ bottom
) / 2;
376 const int result
= key
.compare(keys
[middle
].key
);
379 } else if (result
> 0) {
382 text
= keys
[middle
].text
;
390 #include "infosidebarpage.moc"