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 SidebarPage::setUrl(url
);
117 if (url
.isValid() && !m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
124 void InfoSidebarPage::setSelection(const KFileItemList
& selection
)
126 SidebarPage::setSelection(selection
);
128 const int count
= selection
.count();
134 const KUrl url
= selection
.first().url();
135 if (!url
.isEmpty()) {
136 m_urlCandidate
= url
;
139 m_timer
->start(TimerDelay
);
143 void InfoSidebarPage::requestDelayedItemInfo(const KFileItem
& item
)
145 if (!selection().isEmpty()) {
146 // if items are selected, no item information may get requested
152 m_fileItem
= KFileItem();
154 if (!item
.isNull() && (selection().size() <= 1)) {
155 const KUrl url
= item
.url();
156 if (!url
.isEmpty()) {
157 m_urlCandidate
= url
;
159 m_timer
->start(TimerDelay
);
164 void InfoSidebarPage::showEvent(QShowEvent
* event
)
166 SidebarPage::showEvent(event
);
167 if (event
->spontaneous()) {
173 void InfoSidebarPage::resizeEvent(QResizeEvent
* event
)
175 // If the text inside the name label or the info label cannot
176 // get wrapped, then the maximum width of the label is increased
177 // so that the width of the information sidebar gets increased.
178 // To prevent this, the maximum width is adjusted to
179 // the current width of the sidebar.
180 const int maxWidth
= event
->size().width() - KDialog::spacingHint() * 4;
181 m_nameLabel
->setMaximumWidth(maxWidth
);
183 // try to increase the preview as large as possible
184 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
185 m_urlCandidate
= m_shownUrl
; // reset the URL candidate if a resizing is done
186 m_timer
->start(TimerDelay
);
188 SidebarPage::resizeEvent(event
);
191 void InfoSidebarPage::showItemInfo()
199 const KFileItemList
& selectedItems
= selection();
200 const KUrl file
= selectedItems
.isEmpty() ? m_shownUrl
: selectedItems
[0].url();
201 if (!file
.isValid()) {
205 const int itemCount
= selectedItems
.count();
207 KIconLoader iconLoader
;
208 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
209 KIconLoader::NoGroup
,
210 KIconLoader::SizeEnormous
);
211 m_preview
->setPixmap(icon
);
212 m_nameLabel
->setText(i18ncp("@info", "%1 item selected", "%1 items selected", selectedItems
.count()));
213 } else if (!applyPlace(file
)) {
214 // try to get a preview pixmap from the item...
218 m_pendingPreview
= true;
219 m_preview
->setPixmap(QPixmap());
221 KIO::PreviewJob
* job
= KIO::filePreview(list
,
228 job
->setIgnoreMaximumSize(true);
230 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
231 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
232 connect(job
, SIGNAL(failed(const KFileItem
&)),
233 this, SLOT(showIcon(const KFileItem
&)));
235 m_nameLabel
->setText(file
.fileName());
241 void InfoSidebarPage::slotTimeout()
243 m_shownUrl
= m_urlCandidate
;
247 void InfoSidebarPage::showIcon(const KFileItem
& item
)
249 m_pendingPreview
= false;
250 if (!applyPlace(item
.url())) {
251 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
255 void InfoSidebarPage::showPreview(const KFileItem
& item
,
256 const QPixmap
& pixmap
)
259 if (m_pendingPreview
) {
260 m_preview
->setPixmap(pixmap
);
261 m_pendingPreview
= false;
265 bool InfoSidebarPage::applyPlace(const KUrl
& url
)
267 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
268 int count
= placesModel
->rowCount();
270 for (int i
= 0; i
< count
; ++i
) {
271 QModelIndex index
= placesModel
->index(i
, 0);
273 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
274 m_nameLabel
->setText(placesModel
->text(index
));
275 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
283 void InfoSidebarPage::cancelRequest()
288 void InfoSidebarPage::showMetaInfo()
290 m_metaTextLabel
->clear();
292 const KFileItemList
& selectedItems
= selection();
293 if (selectedItems
.size() <= 1) {
295 if (m_fileItem
.isNull()) {
296 // no pending request is ongoing
297 fileItem
= KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
300 fileItem
= m_fileItem
;
303 if (fileItem
.isDir()) {
304 m_metaTextLabel
->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
306 m_metaTextLabel
->add(i18nc("@label", "Type:"), fileItem
.mimeComment());
308 m_metaTextLabel
->add(i18nc("@label", "Size:"), KIO::convertSize(fileItem
.size()));
309 m_metaTextLabel
->add(i18nc("@label", "Modified:"), fileItem
.timeString());
311 // TODO: See convertMetaInfo below, find a way to display only interesting information
313 const KFileMetaInfo::WhatFlags flags
= KFileMetaInfo::Fastest
|
314 KFileMetaInfo::TechnicalInfo
|
315 KFileMetaInfo::ContentInfo
|
316 KFileMetaInfo::Thumbnail
;
317 const QString path
= fileItem
.url().url();
318 const KFileMetaInfo
fileMetaInfo(path
, QString(), flags
);
319 if (fileMetaInfo
.isValid()) {
320 const QHash
<QString
, KFileMetaInfoItem
>& items
= fileMetaInfo
.items();
321 QHash
<QString
, KFileMetaInfoItem
>::const_iterator it
= items
.constBegin();
322 const QHash
<QString
, KFileMetaInfoItem
>::const_iterator end
= items
.constEnd();
325 const KFileMetaInfoItem
& metaInfoItem
= it
.value();
326 const QVariant
& value
= metaInfoItem
.value();
327 if (value
.isValid() && convertMetaInfo(metaInfoItem
.name(), labelText
)) {
328 m_metaTextLabel
->add(labelText
, value
.toString());
335 if (m_metaDataWidget
!= 0) {
336 m_metaDataWidget
->setFile(fileItem
.url());
339 if (m_metaDataWidget
!= 0) {
341 foreach (const KFileItem
& item
, selectedItems
) {
342 urls
.append(item
.url());
344 m_metaDataWidget
->setFiles(urls
);
347 unsigned long int totalSize
= 0;
348 foreach (const KFileItem
& item
, selectedItems
) {
349 // Only count the size of files, not dirs to match what
350 // DolphinViewContainer::selectionStatusBarText() does.
351 if (!item
.isDir() && !item
.isLink()) {
352 totalSize
+= item
.size();
355 m_metaTextLabel
->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
359 bool InfoSidebarPage::convertMetaInfo(const QString
& key
, QString
& text
) const
361 // TODO: This code prevents that interesting meta information might be hidden
362 // and only bypasses the current problem that not all the meta information should
363 // be shown to the user. Check whether it's possible with Nepomuk to show
364 // all "user relevant" information in a readable way...
371 // sorted list of keys, where its data should be shown
372 static const MetaKey keys
[] = {
373 { "audio.album", "Album:" },
374 { "audio.artist", "Artist:" },
375 { "audio.title", "Title:" },
378 // do a binary search for the key...
380 int bottom
= sizeof(keys
) / sizeof(MetaKey
) - 1;
381 while (top
<= bottom
) {
382 const int middle
= (top
+ bottom
) / 2;
383 const int result
= key
.compare(keys
[middle
].key
);
386 } else if (result
> 0) {
389 text
= keys
[middle
].text
;
397 #include "infosidebarpage.moc"