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>
42 #include <QStyleOptionMenuItem>
44 #include <QVBoxLayout>
46 #include "dolphinsettings.h"
47 #include "metadatawidget.h"
48 #include "metatextlabel.h"
49 #include "pixmapviewer.h"
51 class InfoSeparator
: public QWidget
54 InfoSeparator(QWidget
* parent
);
55 virtual ~InfoSeparator();
58 virtual void paintEvent(QPaintEvent
* event
);
61 InfoSeparator::InfoSeparator(QWidget
* parent
) :
67 InfoSeparator::~InfoSeparator()
71 void InfoSeparator::paintEvent(QPaintEvent
* event
)
74 QPainter
painter(this);
76 QStyleOptionMenuItem option
;
77 option
.initFrom(this);
78 option
.menuItemType
= QStyleOptionMenuItem::Separator
;
79 style()->drawControl(QStyle::CE_MenuItem
, &option
, &painter
, this);
82 InfoSidebarPage::InfoSidebarPage(QWidget
* parent
) :
84 m_pendingPreview(false),
93 const int spacing
= KDialog::spacingHint();
95 m_timer
= new QTimer(this);
96 m_timer
->setSingleShot(true);
97 connect(m_timer
, SIGNAL(timeout()),
98 this, SLOT(slotTimeout()));
100 QVBoxLayout
* layout
= new QVBoxLayout
;
101 layout
->setSpacing(spacing
);
104 m_nameLabel
= new QLabel(this);
105 QFont font
= m_nameLabel
->font();
107 m_nameLabel
->setFont(font
);
108 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
109 m_nameLabel
->setWordWrap(true);
112 m_preview
= new PixmapViewer(this);
113 m_preview
->setMinimumWidth(KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
);
114 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
116 if (MetaDataWidget::metaDataAvailable()) {
117 // rating, comment and tags
118 m_metaDataWidget
= new MetaDataWidget(this);
121 // general meta text information
122 m_metaTextLabel
= new MetaTextLabel(this);
123 m_metaTextLabel
->setMinimumWidth(spacing
);
125 layout
->addWidget(m_nameLabel
);
126 layout
->addWidget(new InfoSeparator(this));
127 layout
->addWidget(m_preview
);
128 layout
->addWidget(new InfoSeparator(this));
129 if (m_metaDataWidget
!= 0) {
130 layout
->addWidget(m_metaDataWidget
);
131 layout
->addWidget(new InfoSeparator(this));
133 layout
->addWidget(m_metaTextLabel
);
135 // ensure that widgets in the information side bar are aligned towards the top
136 layout
->addStretch(1);
140 InfoSidebarPage::~InfoSidebarPage()
144 QSize
InfoSidebarPage::sizeHint() const
146 QSize size
= SidebarPage::sizeHint();
147 size
.setWidth(minimumSizeHint().width());
151 void InfoSidebarPage::setUrl(const KUrl
& url
)
153 SidebarPage::setUrl(url
);
154 if (url
.isValid() && !m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
161 void InfoSidebarPage::setSelection(const KFileItemList
& selection
)
163 SidebarPage::setSelection(selection
);
165 const int count
= selection
.count();
170 if ((count
== 1) && !selection
.first().url().isEmpty()) {
171 m_urlCandidate
= selection
.first().url();
173 m_timer
->start(TimerDelay
);
177 void InfoSidebarPage::requestDelayedItemInfo(const KFileItem
& item
)
181 m_fileItem
= KFileItem();
183 // The cursor is above the viewport. If files are selected,
184 // show information regarding the selection.
185 if (selection().size() > 0) {
186 m_timer
->start(TimerDelay
);
188 } else if (!item
.url().isEmpty()) {
189 m_urlCandidate
= item
.url();
191 m_timer
->start(TimerDelay
);
195 void InfoSidebarPage::showEvent(QShowEvent
* event
)
197 SidebarPage::showEvent(event
);
198 if (!event
->spontaneous()) {
203 void InfoSidebarPage::resizeEvent(QResizeEvent
* event
)
205 // If the text inside the name label or the info label cannot
206 // get wrapped, then the maximum width of the label is increased
207 // so that the width of the information sidebar gets increased.
208 // To prevent this, the maximum width is adjusted to
209 // the current width of the sidebar.
210 const int maxWidth
= event
->size().width() - KDialog::spacingHint() * 4;
211 m_nameLabel
->setMaximumWidth(maxWidth
);
213 // try to increase the preview as large as possible
214 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
215 m_urlCandidate
= m_shownUrl
; // reset the URL candidate if a resizing is done
216 m_timer
->start(TimerDelay
);
218 SidebarPage::resizeEvent(event
);
221 void InfoSidebarPage::showItemInfo()
229 const KFileItemList
& selectedItems
= selection();
230 const KUrl file
= (!m_fileItem
.isNull() || selectedItems
.isEmpty()) ? m_shownUrl
: selectedItems
[0].url();
231 if (!file
.isValid()) {
235 const int selectionCount
= selectedItems
.count();
236 if (m_fileItem
.isNull() && (selectionCount
> 1)) {
237 KIconLoader iconLoader
;
238 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
239 KIconLoader::NoGroup
,
240 KIconLoader::SizeEnormous
);
241 m_preview
->setPixmap(icon
);
242 m_nameLabel
->setText(i18ncp("@info", "%1 item selected", "%1 items selected", selectionCount
));
243 } else if (!applyPlace(file
)) {
244 // try to get a preview pixmap from the item...
248 m_pendingPreview
= true;
250 KIconEffect iconEffect
;
251 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(), KIconLoader::Desktop
, KIconLoader::DisabledState
);
252 m_preview
->setPixmap(disabledPixmap
);
254 KIO::PreviewJob
* job
= KIO::filePreview(list
,
261 job
->setIgnoreMaximumSize(true);
263 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
264 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
265 connect(job
, SIGNAL(failed(const KFileItem
&)),
266 this, SLOT(showIcon(const KFileItem
&)));
268 m_nameLabel
->setText(file
.fileName());
274 void InfoSidebarPage::slotTimeout()
276 m_shownUrl
= m_urlCandidate
;
280 void InfoSidebarPage::showIcon(const KFileItem
& item
)
282 m_pendingPreview
= false;
283 if (!applyPlace(item
.url())) {
284 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
288 void InfoSidebarPage::showPreview(const KFileItem
& item
,
289 const QPixmap
& pixmap
)
292 if (m_pendingPreview
) {
293 m_preview
->setPixmap(pixmap
);
294 m_pendingPreview
= false;
298 bool InfoSidebarPage::applyPlace(const KUrl
& url
)
300 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
301 int count
= placesModel
->rowCount();
303 for (int i
= 0; i
< count
; ++i
) {
304 QModelIndex index
= placesModel
->index(i
, 0);
306 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
307 m_nameLabel
->setText(placesModel
->text(index
));
308 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
316 void InfoSidebarPage::cancelRequest()
321 void InfoSidebarPage::showMetaInfo()
323 m_metaTextLabel
->clear();
325 const KFileItemList
& selectedItems
= selection();
326 if ((selectedItems
.size() <= 1) || !m_fileItem
.isNull()) {
328 if (m_fileItem
.isNull()) {
329 // no pending request is ongoing
330 const KUrl url
= (selectedItems
.size() == 1) ? selectedItems
.first().url() : m_shownUrl
;
331 fileItem
= KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, url
);
334 fileItem
= m_fileItem
;
337 if (fileItem
.isDir()) {
338 m_metaTextLabel
->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
339 m_metaTextLabel
->add(i18nc("@label", "Modified:"), fileItem
.timeString());
341 m_metaTextLabel
->add(i18nc("@label", "Type:"), fileItem
.mimeComment());
343 m_metaTextLabel
->add(i18nc("@label", "Size:"), KIO::convertSize(fileItem
.size()));
344 m_metaTextLabel
->add(i18nc("@label", "Modified:"), fileItem
.timeString());
346 // TODO: See convertMetaInfo below, find a way to display only interesting information
348 const KFileMetaInfo::WhatFlags flags
= KFileMetaInfo::Fastest
|
349 KFileMetaInfo::TechnicalInfo
|
350 KFileMetaInfo::ContentInfo
|
351 KFileMetaInfo::Thumbnail
;
352 const QString path
= fileItem
.url().url();
353 const KFileMetaInfo
fileMetaInfo(path
, QString(), flags
);
354 if (fileMetaInfo
.isValid()) {
355 const QHash
<QString
, KFileMetaInfoItem
>& items
= fileMetaInfo
.items();
356 QHash
<QString
, KFileMetaInfoItem
>::const_iterator it
= items
.constBegin();
357 const QHash
<QString
, KFileMetaInfoItem
>::const_iterator end
= items
.constEnd();
360 const KFileMetaInfoItem
& metaInfoItem
= it
.value();
361 const QVariant
& value
= metaInfoItem
.value();
362 if (value
.isValid() && convertMetaInfo(metaInfoItem
.name(), labelText
)) {
363 m_metaTextLabel
->add(labelText
, value
.toString());
370 if (m_metaDataWidget
!= 0) {
371 m_metaDataWidget
->setFile(fileItem
.targetUrl());
374 if (m_metaDataWidget
!= 0) {
376 foreach (const KFileItem
& item
, selectedItems
) {
377 urls
.append(item
.targetUrl());
379 m_metaDataWidget
->setFiles(urls
);
382 unsigned long int totalSize
= 0;
383 foreach (const KFileItem
& item
, selectedItems
) {
384 // Only count the size of files, not dirs to match what
385 // DolphinViewContainer::selectionStatusBarText() does.
386 if (!item
.isDir() && !item
.isLink()) {
387 totalSize
+= item
.size();
390 m_metaTextLabel
->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
394 bool InfoSidebarPage::convertMetaInfo(const QString
& key
, QString
& text
) const
396 // TODO: This code prevents that interesting meta information might be hidden
397 // and only bypasses the current problem that not all the meta information should
398 // be shown to the user. Check whether it's possible with Nepomuk to show
399 // all "user relevant" information in a readable way...
406 // sorted list of keys, where its data should be shown
407 static const MetaKey keys
[] = {
408 { "audio.album", "Album:" },
409 { "audio.artist", "Artist:" },
410 { "audio.title", "Title:" },
413 // do a binary search for the key...
415 int bottom
= sizeof(keys
) / sizeof(MetaKey
) - 1;
416 while (top
<= bottom
) {
417 const int middle
= (top
+ bottom
) / 2;
418 const int result
= key
.compare(keys
[middle
].key
);
421 } else if (result
> 0) {
424 text
= keys
[middle
].text
;
432 #include "infosidebarpage.moc"