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();
169 if ((count
== 1) && !selection
.first().url().isEmpty()) {
170 m_urlCandidate
= selection
.first().url();
172 m_timer
->start(TimerDelay
);
176 void InfoSidebarPage::requestDelayedItemInfo(const KFileItem
& item
)
180 m_fileItem
= KFileItem();
182 // The cursor is above the viewport. If files are selected,
183 // show information regarding the selection.
184 if (selection().size() > 0) {
185 m_timer
->start(TimerDelay
);
187 } else if (!item
.url().isEmpty()) {
188 m_urlCandidate
= item
.url();
190 m_timer
->start(TimerDelay
);
194 void InfoSidebarPage::showEvent(QShowEvent
* event
)
196 SidebarPage::showEvent(event
);
197 if (!event
->spontaneous()) {
202 void InfoSidebarPage::resizeEvent(QResizeEvent
* event
)
204 // If the text inside the name label or the info label cannot
205 // get wrapped, then the maximum width of the label is increased
206 // so that the width of the information sidebar gets increased.
207 // To prevent this, the maximum width is adjusted to
208 // the current width of the sidebar.
209 const int maxWidth
= event
->size().width() - KDialog::spacingHint() * 4;
210 m_nameLabel
->setMaximumWidth(maxWidth
);
212 // try to increase the preview as large as possible
213 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
214 m_urlCandidate
= m_shownUrl
; // reset the URL candidate if a resizing is done
215 m_timer
->start(TimerDelay
);
217 SidebarPage::resizeEvent(event
);
220 void InfoSidebarPage::showItemInfo()
228 const KFileItemList
& selectedItems
= selection();
229 const KUrl file
= (!m_fileItem
.isNull() || selectedItems
.isEmpty()) ? m_shownUrl
: selectedItems
[0].url();
230 if (!file
.isValid()) {
234 const int selectionCount
= selectedItems
.count();
235 if (m_fileItem
.isNull() && (selectionCount
> 1)) {
236 KIconLoader iconLoader
;
237 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
238 KIconLoader::NoGroup
,
239 KIconLoader::SizeEnormous
);
240 m_preview
->setPixmap(icon
);
241 m_nameLabel
->setText(i18ncp("@info", "%1 item selected", "%1 items selected", selectionCount
));
242 } else if (!applyPlace(file
)) {
243 // try to get a preview pixmap from the item...
247 m_pendingPreview
= true;
248 m_preview
->setPixmap(QPixmap());
250 KIO::PreviewJob
* job
= KIO::filePreview(list
,
257 job
->setIgnoreMaximumSize(true);
259 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
260 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
261 connect(job
, SIGNAL(failed(const KFileItem
&)),
262 this, SLOT(showIcon(const KFileItem
&)));
264 m_nameLabel
->setText(file
.fileName());
270 void InfoSidebarPage::slotTimeout()
272 m_shownUrl
= m_urlCandidate
;
276 void InfoSidebarPage::showIcon(const KFileItem
& item
)
278 m_pendingPreview
= false;
279 if (!applyPlace(item
.url())) {
280 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
284 void InfoSidebarPage::showPreview(const KFileItem
& item
,
285 const QPixmap
& pixmap
)
288 if (m_pendingPreview
) {
289 m_preview
->setPixmap(pixmap
);
290 m_pendingPreview
= false;
294 bool InfoSidebarPage::applyPlace(const KUrl
& url
)
296 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
297 int count
= placesModel
->rowCount();
299 for (int i
= 0; i
< count
; ++i
) {
300 QModelIndex index
= placesModel
->index(i
, 0);
302 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
303 m_nameLabel
->setText(placesModel
->text(index
));
304 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
312 void InfoSidebarPage::cancelRequest()
317 void InfoSidebarPage::showMetaInfo()
319 m_metaTextLabel
->clear();
321 const KFileItemList
& selectedItems
= selection();
322 if ((selectedItems
.size() <= 1) || !m_fileItem
.isNull()) {
324 if (m_fileItem
.isNull()) {
325 // no pending request is ongoing
326 const KUrl url
= (selectedItems
.size() == 1) ? selectedItems
.first().url() : m_shownUrl
;
327 fileItem
= KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, url
);
330 fileItem
= m_fileItem
;
333 if (fileItem
.isDir()) {
334 m_metaTextLabel
->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
335 m_metaTextLabel
->add(i18nc("@label", "Modified:"), fileItem
.timeString());
337 m_metaTextLabel
->add(i18nc("@label", "Type:"), fileItem
.mimeComment());
339 m_metaTextLabel
->add(i18nc("@label", "Size:"), KIO::convertSize(fileItem
.size()));
340 m_metaTextLabel
->add(i18nc("@label", "Modified:"), fileItem
.timeString());
342 // TODO: See convertMetaInfo below, find a way to display only interesting information
344 const KFileMetaInfo::WhatFlags flags
= KFileMetaInfo::Fastest
|
345 KFileMetaInfo::TechnicalInfo
|
346 KFileMetaInfo::ContentInfo
|
347 KFileMetaInfo::Thumbnail
;
348 const QString path
= fileItem
.url().url();
349 const KFileMetaInfo
fileMetaInfo(path
, QString(), flags
);
350 if (fileMetaInfo
.isValid()) {
351 const QHash
<QString
, KFileMetaInfoItem
>& items
= fileMetaInfo
.items();
352 QHash
<QString
, KFileMetaInfoItem
>::const_iterator it
= items
.constBegin();
353 const QHash
<QString
, KFileMetaInfoItem
>::const_iterator end
= items
.constEnd();
356 const KFileMetaInfoItem
& metaInfoItem
= it
.value();
357 const QVariant
& value
= metaInfoItem
.value();
358 if (value
.isValid() && convertMetaInfo(metaInfoItem
.name(), labelText
)) {
359 m_metaTextLabel
->add(labelText
, value
.toString());
366 if (m_metaDataWidget
!= 0) {
367 m_metaDataWidget
->setFile(fileItem
.targetUrl());
370 if (m_metaDataWidget
!= 0) {
372 foreach (const KFileItem
& item
, selectedItems
) {
373 urls
.append(item
.targetUrl());
375 m_metaDataWidget
->setFiles(urls
);
378 unsigned long int totalSize
= 0;
379 foreach (const KFileItem
& item
, selectedItems
) {
380 // Only count the size of files, not dirs to match what
381 // DolphinViewContainer::selectionStatusBarText() does.
382 if (!item
.isDir() && !item
.isLink()) {
383 totalSize
+= item
.size();
386 m_metaTextLabel
->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
390 bool InfoSidebarPage::convertMetaInfo(const QString
& key
, QString
& text
) const
392 // TODO: This code prevents that interesting meta information might be hidden
393 // and only bypasses the current problem that not all the meta information should
394 // be shown to the user. Check whether it's possible with Nepomuk to show
395 // all "user relevant" information in a readable way...
402 // sorted list of keys, where its data should be shown
403 static const MetaKey keys
[] = {
404 { "audio.album", "Album:" },
405 { "audio.artist", "Artist:" },
406 { "audio.title", "Title:" },
409 // do a binary search for the key...
411 int bottom
= sizeof(keys
) / sizeof(MetaKey
) - 1;
412 while (top
<= bottom
) {
413 const int middle
= (top
+ bottom
) / 2;
414 const int result
= key
.compare(keys
[middle
].key
);
417 } else if (result
> 0) {
420 text
= keys
[middle
].text
;
428 #include "infosidebarpage.moc"