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>
43 #include <QVBoxLayout>
45 #include "dolphinsettings.h"
46 #include "metadatawidget.h"
47 #include "metatextlabel.h"
48 #include "pixmapviewer.h"
50 InfoSidebarPage::InfoSidebarPage(QWidget
* parent
) :
52 m_pendingPreview(false),
62 const int spacing
= KDialog::spacingHint();
64 m_timer
= new QTimer(this);
65 m_timer
->setSingleShot(true);
66 connect(m_timer
, SIGNAL(timeout()),
67 this, SLOT(slotTimeout()));
69 QVBoxLayout
* layout
= new QVBoxLayout
;
70 layout
->setSpacing(spacing
);
73 m_nameLabel
= new QLabel(this);
74 QFont font
= m_nameLabel
->font();
76 m_nameLabel
->setFont(font
);
77 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
78 m_nameLabel
->setWordWrap(true);
81 m_preview
= new PixmapViewer(this);
82 m_preview
->setMinimumWidth(KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
);
83 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
85 if (MetaDataWidget::metaDataAvailable()) {
86 // rating, comment and tags
87 m_metaDataWidget
= new MetaDataWidget(this);
90 // general meta text information
91 m_metaTextLabel
= new MetaTextLabel(this);
92 m_metaTextLabel
->setMinimumWidth(spacing
);
94 layout
->addWidget(m_nameLabel
);
95 layout
->addWidget(new KSeparator(this));
96 layout
->addWidget(m_preview
);
97 layout
->addWidget(new KSeparator(this));
98 if (m_metaDataWidget
!= 0) {
99 layout
->addWidget(m_metaDataWidget
);
100 layout
->addWidget(new KSeparator(this));
102 layout
->addWidget(m_metaTextLabel
);
104 // ensure that widgets in the information side bar are aligned towards the top
105 layout
->addStretch(1);
109 InfoSidebarPage::~InfoSidebarPage()
113 QSize
InfoSidebarPage::sizeHint() const
115 QSize size
= SidebarPage::sizeHint();
116 size
.setWidth(minimumSizeHint().width());
120 void InfoSidebarPage::setUrl(const KUrl
& url
)
122 SidebarPage::setUrl(url
);
123 if (url
.isValid() && !m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
130 void InfoSidebarPage::setSelection(const KFileItemList
& selection
)
132 m_selection
= selection
;
134 const int count
= selection
.count();
139 if ((count
== 1) && !selection
.first().url().isEmpty()) {
140 m_urlCandidate
= selection
.first().url();
142 m_timer
->start(TimerDelay
);
146 void InfoSidebarPage::requestDelayedItemInfo(const KFileItem
& item
)
150 m_fileItem
= KFileItem();
152 // The cursor is above the viewport. If files are selected,
153 // show information regarding the selection.
154 if (m_selection
.size() > 0) {
155 m_pendingPreview
= false;
156 m_timer
->start(TimerDelay
);
158 } else if (!item
.url().isEmpty()) {
159 m_urlCandidate
= item
.url();
161 m_timer
->start(TimerDelay
);
165 void InfoSidebarPage::showEvent(QShowEvent
* event
)
167 SidebarPage::showEvent(event
);
168 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 KUrl file
= (!m_fileItem
.isNull() || m_selection
.isEmpty()) ? m_shownUrl
: m_selection
[0].url();
200 if (!file
.isValid()) {
204 const int selectionCount
= m_selection
.count();
205 if (m_fileItem
.isNull() && (selectionCount
> 1)) {
206 KIconLoader iconLoader
;
207 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
208 KIconLoader::NoGroup
,
209 KIconLoader::SizeEnormous
);
210 m_preview
->setPixmap(icon
);
211 m_nameLabel
->setText(i18ncp("@info", "%1 item selected", "%1 items selected", selectionCount
));
212 } else if (!applyPlace(file
)) {
213 // try to get a preview pixmap from the item...
217 m_pendingPreview
= true;
219 KIconEffect iconEffect
;
220 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(), KIconLoader::Desktop
, KIconLoader::DisabledState
);
221 m_preview
->setPixmap(disabledPixmap
);
223 KIO::PreviewJob
* job
= KIO::filePreview(list
,
230 job
->setIgnoreMaximumSize(true);
232 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
233 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
234 connect(job
, SIGNAL(failed(const KFileItem
&)),
235 this, SLOT(showIcon(const KFileItem
&)));
237 m_nameLabel
->setText(file
.fileName());
243 void InfoSidebarPage::slotTimeout()
245 m_shownUrl
= m_urlCandidate
;
249 void InfoSidebarPage::showIcon(const KFileItem
& item
)
251 m_pendingPreview
= false;
252 if (!applyPlace(item
.url())) {
253 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
257 void InfoSidebarPage::showPreview(const KFileItem
& item
,
258 const QPixmap
& pixmap
)
261 if (m_pendingPreview
) {
262 m_preview
->setPixmap(pixmap
);
263 m_pendingPreview
= false;
267 bool InfoSidebarPage::applyPlace(const KUrl
& url
)
269 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
270 int count
= placesModel
->rowCount();
272 for (int i
= 0; i
< count
; ++i
) {
273 QModelIndex index
= placesModel
->index(i
, 0);
275 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
276 m_nameLabel
->setText(placesModel
->text(index
));
277 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
285 void InfoSidebarPage::cancelRequest()
290 void InfoSidebarPage::showMetaInfo()
292 m_metaTextLabel
->clear();
294 if ((m_selection
.size() <= 1) || !m_fileItem
.isNull()) {
296 if (m_fileItem
.isNull()) {
297 // no pending request is ongoing
298 const KUrl url
= (m_selection
.size() == 1) ? m_selection
.first().url() : m_shownUrl
;
299 fileItem
= KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, url
);
302 fileItem
= m_fileItem
;
305 if (fileItem
.isDir()) {
306 m_metaTextLabel
->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
307 m_metaTextLabel
->add(i18nc("@label", "Modified:"), fileItem
.timeString());
309 m_metaTextLabel
->add(i18nc("@label", "Type:"), fileItem
.mimeComment());
311 m_metaTextLabel
->add(i18nc("@label", "Size:"), KIO::convertSize(fileItem
.size()));
312 m_metaTextLabel
->add(i18nc("@label", "Modified:"), fileItem
.timeString());
314 // TODO: See convertMetaInfo below, find a way to display only interesting information
316 const KFileMetaInfo::WhatFlags flags
= KFileMetaInfo::Fastest
|
317 KFileMetaInfo::TechnicalInfo
|
318 KFileMetaInfo::ContentInfo
|
319 KFileMetaInfo::Thumbnail
;
320 const QString path
= fileItem
.url().url();
321 const KFileMetaInfo
fileMetaInfo(path
, QString(), flags
);
322 if (fileMetaInfo
.isValid()) {
323 const QHash
<QString
, KFileMetaInfoItem
>& items
= fileMetaInfo
.items();
324 QHash
<QString
, KFileMetaInfoItem
>::const_iterator it
= items
.constBegin();
325 const QHash
<QString
, KFileMetaInfoItem
>::const_iterator end
= items
.constEnd();
328 const KFileMetaInfoItem
& metaInfoItem
= it
.value();
329 const QVariant
& value
= metaInfoItem
.value();
330 if (value
.isValid() && convertMetaInfo(metaInfoItem
.name(), labelText
)) {
331 m_metaTextLabel
->add(labelText
, value
.toString());
338 if (m_metaDataWidget
!= 0) {
339 m_metaDataWidget
->setFile(fileItem
.targetUrl());
342 if (m_metaDataWidget
!= 0) {
344 foreach (const KFileItem
& item
, m_selection
) {
345 urls
.append(item
.targetUrl());
347 m_metaDataWidget
->setFiles(urls
);
350 quint64 totalSize
= 0;
351 foreach (const KFileItem
& item
, m_selection
) {
352 // Only count the size of files, not dirs to match what
353 // DolphinViewContainer::selectionStatusBarText() does.
354 if (!item
.isDir() && !item
.isLink()) {
355 totalSize
+= item
.size();
358 m_metaTextLabel
->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
362 bool InfoSidebarPage::convertMetaInfo(const QString
& key
, QString
& text
) const
364 // TODO: This code prevents that interesting meta information might be hidden
365 // and only bypasses the current problem that not all the meta information should
366 // be shown to the user. Check whether it's possible with Nepomuk to show
367 // all "user relevant" information in a readable way...
374 // sorted list of keys, where its data should be shown
375 static const MetaKey keys
[] = {
376 { "audio.album", "Album:" },
377 { "audio.artist", "Artist:" },
378 { "audio.title", "Title:" },
381 // do a binary search for the key...
383 int bottom
= sizeof(keys
) / sizeof(MetaKey
) - 1;
384 while (top
<= bottom
) {
385 const int middle
= (top
+ bottom
) / 2;
386 const int result
= key
.compare(keys
[middle
].key
);
389 } else if (result
> 0) {
392 text
= keys
[middle
].text
;
400 #include "infosidebarpage.moc"