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>
40 #include <QResizeEvent>
41 #include <QStyleOptionMenuItem>
43 #include <QVBoxLayout>
45 #include "dolphinsettings.h"
46 #include "metadatawidget.h"
47 #include "metatextlabel.h"
48 #include "pixmapviewer.h"
50 class InfoSeparator
: public QWidget
53 InfoSeparator(QWidget
* parent
);
54 virtual ~InfoSeparator();
57 virtual void paintEvent(QPaintEvent
* event
);
60 InfoSeparator::InfoSeparator(QWidget
* parent
) :
66 InfoSeparator::~InfoSeparator()
70 void InfoSeparator::paintEvent(QPaintEvent
* event
)
73 QPainter
painter(this);
75 QStyleOptionMenuItem option
;
76 option
.initFrom(this);
77 option
.menuItemType
= QStyleOptionMenuItem::Separator
;
78 style()->drawControl(QStyle::CE_MenuItem
, &option
, &painter
, this);
81 InfoSidebarPage::InfoSidebarPage(QWidget
* parent
) :
83 m_pendingPreview(false),
92 const int spacing
= KDialog::spacingHint();
94 m_timer
= new QTimer(this);
95 m_timer
->setSingleShot(true);
96 connect(m_timer
, SIGNAL(timeout()),
97 this, SLOT(slotTimeout()));
99 QVBoxLayout
* layout
= new QVBoxLayout
;
100 layout
->setSpacing(spacing
);
103 m_nameLabel
= new QLabel(this);
104 QFont font
= m_nameLabel
->font();
106 m_nameLabel
->setFont(font
);
107 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
108 m_nameLabel
->setWordWrap(true);
111 m_preview
= new PixmapViewer(this);
112 m_preview
->setMinimumWidth(KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
);
113 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
115 if (MetaDataWidget::metaDataAvailable()) {
116 // rating, comment and tags
117 m_metaDataWidget
= new MetaDataWidget(this);
120 // general meta text information
121 m_metaTextLabel
= new MetaTextLabel(this);
122 m_metaTextLabel
->setMinimumWidth(spacing
);
124 layout
->addWidget(m_nameLabel
);
125 layout
->addWidget(new InfoSeparator(this));
126 layout
->addWidget(m_preview
);
127 layout
->addWidget(new InfoSeparator(this));
128 if (m_metaDataWidget
!= 0) {
129 layout
->addWidget(m_metaDataWidget
);
130 layout
->addWidget(new InfoSeparator(this));
132 layout
->addWidget(m_metaTextLabel
);
134 // ensure that widgets in the information side bar are aligned towards the top
135 layout
->addStretch(1);
139 InfoSidebarPage::~InfoSidebarPage()
143 QSize
InfoSidebarPage::sizeHint() const
145 QSize size
= SidebarPage::sizeHint();
146 size
.setWidth(minimumSizeHint().width());
150 void InfoSidebarPage::setUrl(const KUrl
& url
)
152 SidebarPage::setUrl(url
);
153 if (url
.isValid() && !m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
160 void InfoSidebarPage::setSelection(const KFileItemList
& selection
)
162 SidebarPage::setSelection(selection
);
164 const int count
= selection
.count();
170 const KUrl url
= selection
.first().url();
171 if (!url
.isEmpty()) {
172 m_urlCandidate
= url
;
175 m_timer
->start(TimerDelay
);
179 void InfoSidebarPage::requestDelayedItemInfo(const KFileItem
& item
)
181 if (!selection().isEmpty()) {
182 // if items are selected, no item information may get requested
188 m_fileItem
= KFileItem();
190 if (!item
.isNull() && (selection().size() <= 1)) {
191 const KUrl url
= item
.url();
192 if (!url
.isEmpty()) {
193 m_urlCandidate
= url
;
195 m_timer
->start(TimerDelay
);
200 void InfoSidebarPage::showEvent(QShowEvent
* event
)
202 SidebarPage::showEvent(event
);
203 if (event
->spontaneous()) {
209 void InfoSidebarPage::resizeEvent(QResizeEvent
* event
)
211 // If the text inside the name label or the info label cannot
212 // get wrapped, then the maximum width of the label is increased
213 // so that the width of the information sidebar gets increased.
214 // To prevent this, the maximum width is adjusted to
215 // the current width of the sidebar.
216 const int maxWidth
= event
->size().width() - KDialog::spacingHint() * 4;
217 m_nameLabel
->setMaximumWidth(maxWidth
);
219 // try to increase the preview as large as possible
220 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
221 m_urlCandidate
= m_shownUrl
; // reset the URL candidate if a resizing is done
222 m_timer
->start(TimerDelay
);
224 SidebarPage::resizeEvent(event
);
227 void InfoSidebarPage::showItemInfo()
235 const KFileItemList
& selectedItems
= selection();
236 const KUrl file
= selectedItems
.isEmpty() ? m_shownUrl
: selectedItems
[0].url();
237 if (!file
.isValid()) {
241 const int itemCount
= selectedItems
.count();
243 KIconLoader iconLoader
;
244 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
245 KIconLoader::NoGroup
,
246 KIconLoader::SizeEnormous
);
247 m_preview
->setPixmap(icon
);
248 m_nameLabel
->setText(i18ncp("@info", "%1 item selected", "%1 items selected", selectedItems
.count()));
249 } else if (!applyPlace(file
)) {
250 // try to get a preview pixmap from the item...
254 m_pendingPreview
= true;
255 m_preview
->setPixmap(QPixmap());
257 KIO::PreviewJob
* job
= KIO::filePreview(list
,
264 job
->setIgnoreMaximumSize(true);
266 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
267 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
268 connect(job
, SIGNAL(failed(const KFileItem
&)),
269 this, SLOT(showIcon(const KFileItem
&)));
271 m_nameLabel
->setText(file
.fileName());
277 void InfoSidebarPage::slotTimeout()
279 m_shownUrl
= m_urlCandidate
;
283 void InfoSidebarPage::showIcon(const KFileItem
& item
)
285 m_pendingPreview
= false;
286 if (!applyPlace(item
.url())) {
287 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
291 void InfoSidebarPage::showPreview(const KFileItem
& item
,
292 const QPixmap
& pixmap
)
295 if (m_pendingPreview
) {
296 m_preview
->setPixmap(pixmap
);
297 m_pendingPreview
= false;
301 bool InfoSidebarPage::applyPlace(const KUrl
& url
)
303 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
304 int count
= placesModel
->rowCount();
306 for (int i
= 0; i
< count
; ++i
) {
307 QModelIndex index
= placesModel
->index(i
, 0);
309 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
310 m_nameLabel
->setText(placesModel
->text(index
));
311 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
319 void InfoSidebarPage::cancelRequest()
324 void InfoSidebarPage::showMetaInfo()
326 m_metaTextLabel
->clear();
328 const KFileItemList
& selectedItems
= selection();
329 if (selectedItems
.size() <= 1) {
331 if (m_fileItem
.isNull()) {
332 // no pending request is ongoing
333 fileItem
= KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
336 fileItem
= m_fileItem
;
339 if (fileItem
.isDir()) {
340 m_metaTextLabel
->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
342 m_metaTextLabel
->add(i18nc("@label", "Type:"), fileItem
.mimeComment());
344 m_metaTextLabel
->add(i18nc("@label", "Size:"), KIO::convertSize(fileItem
.size()));
345 m_metaTextLabel
->add(i18nc("@label", "Modified:"), fileItem
.timeString());
347 // TODO: See convertMetaInfo below, find a way to display only interesting information
349 const KFileMetaInfo::WhatFlags flags
= KFileMetaInfo::Fastest
|
350 KFileMetaInfo::TechnicalInfo
|
351 KFileMetaInfo::ContentInfo
|
352 KFileMetaInfo::Thumbnail
;
353 const QString path
= fileItem
.url().url();
354 const KFileMetaInfo
fileMetaInfo(path
, QString(), flags
);
355 if (fileMetaInfo
.isValid()) {
356 const QHash
<QString
, KFileMetaInfoItem
>& items
= fileMetaInfo
.items();
357 QHash
<QString
, KFileMetaInfoItem
>::const_iterator it
= items
.constBegin();
358 const QHash
<QString
, KFileMetaInfoItem
>::const_iterator end
= items
.constEnd();
361 const KFileMetaInfoItem
& metaInfoItem
= it
.value();
362 const QVariant
& value
= metaInfoItem
.value();
363 if (value
.isValid() && convertMetaInfo(metaInfoItem
.name(), labelText
)) {
364 m_metaTextLabel
->add(labelText
, value
.toString());
371 if (m_metaDataWidget
!= 0) {
372 m_metaDataWidget
->setFile(fileItem
.url());
375 if (m_metaDataWidget
!= 0) {
377 foreach (const KFileItem
& item
, selectedItems
) {
378 urls
.append(item
.url());
380 m_metaDataWidget
->setFiles(urls
);
383 unsigned long int totalSize
= 0;
384 foreach (const KFileItem
& item
, selectedItems
) {
385 // Only count the size of files, not dirs to match what
386 // DolphinViewContainer::selectionStatusBarText() does.
387 if (!item
.isDir() && !item
.isLink()) {
388 totalSize
+= item
.size();
391 m_metaTextLabel
->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
395 bool InfoSidebarPage::convertMetaInfo(const QString
& key
, QString
& text
) const
397 // TODO: This code prevents that interesting meta information might be hidden
398 // and only bypasses the current problem that not all the meta information should
399 // be shown to the user. Check whether it's possible with Nepomuk to show
400 // all "user relevant" information in a readable way...
407 // sorted list of keys, where its data should be shown
408 static const MetaKey keys
[] = {
409 { "audio.album", "Album:" },
410 { "audio.artist", "Artist:" },
411 { "audio.title", "Title:" },
414 // do a binary search for the key...
416 int bottom
= sizeof(keys
) / sizeof(MetaKey
) - 1;
417 while (top
<= bottom
) {
418 const int middle
= (top
+ bottom
) / 2;
419 const int result
= key
.compare(keys
[middle
].key
);
422 } else if (result
> 0) {
425 text
= keys
[middle
].text
;
433 #include "infosidebarpage.moc"