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>
33 #include <kseparator.h>
34 #include <kiconloader.h>
37 #include <QInputDialog>
40 #include <QResizeEvent>
42 #include <QVBoxLayout>
44 #include "dolphinsettings.h"
45 #include "metadatawidget.h"
46 #include "pixmapviewer.h"
48 InfoSidebarPage::InfoSidebarPage(QWidget
* parent
) :
50 m_pendingPreview(false),
57 const int spacing
= KDialog::spacingHint();
59 m_timer
= new QTimer(this);
60 m_timer
->setSingleShot(true);
61 connect(m_timer
, SIGNAL(timeout()),
62 this, SLOT(slotTimeout()));
64 QVBoxLayout
* layout
= new QVBoxLayout
;
65 layout
->setSpacing(spacing
);
68 m_preview
= new PixmapViewer(this);
69 m_preview
->setMinimumWidth(KIconLoader::SizeEnormous
);
70 m_preview
->setFixedHeight(KIconLoader::SizeEnormous
);
73 m_nameLabel
= new QLabel(this);
74 m_nameLabel
->setTextFormat(Qt::RichText
);
75 m_nameLabel
->setAlignment(m_nameLabel
->alignment() | Qt::AlignHCenter
);
76 m_nameLabel
->setWordWrap(true);
78 // general information
79 m_infoLabel
= new QLabel(this);
80 m_infoLabel
->setSizePolicy(QSizePolicy::Minimum
, QSizePolicy::Fixed
);
81 m_infoLabel
->setTextFormat(Qt::RichText
);
83 if (MetaDataWidget::metaDataAvailable()) {
84 m_metadataWidget
= new MetaDataWidget(this);
87 layout
->addItem(new QSpacerItem(spacing
, spacing
, QSizePolicy::Preferred
, QSizePolicy::Fixed
));
88 layout
->addWidget(m_preview
);
89 layout
->addWidget(m_nameLabel
);
90 layout
->addWidget(new KSeparator(this));
91 layout
->addWidget(m_infoLabel
);
92 layout
->addWidget(new KSeparator(this));
93 if (m_metadataWidget
) {
94 layout
->addWidget(m_metadataWidget
);
96 // ensure that widgets in the information side bar are aligned towards the top
97 layout
->addStretch(1);
101 InfoSidebarPage::~InfoSidebarPage()
105 QSize
InfoSidebarPage::sizeHint() const
107 QSize size
= SidebarPage::sizeHint();
108 size
.setWidth(minimumSizeHint().width());
112 void InfoSidebarPage::setUrl(const KUrl
& url
)
114 if (url
.isValid() && !m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
121 void InfoSidebarPage::setSelection(const KFileItemList
& selection
)
123 SidebarPage::setSelection(selection
);
124 if (selection
.size() == 1) {
125 const KUrl url
= selection
.first().url();
126 if (!url
.isEmpty()) {
127 m_urlCandidate
= url
;
130 m_timer
->start(TimerDelay
);
133 void InfoSidebarPage::requestDelayedItemInfo(const KFileItem
& item
)
137 if (!item
.isNull() && (selection().size() <= 1)) {
138 const KUrl url
= item
.url();
139 if (!url
.isEmpty()) {
140 m_urlCandidate
= url
;
141 m_timer
->start(TimerDelay
);
146 void InfoSidebarPage::showEvent(QShowEvent
* event
)
148 SidebarPage::showEvent(event
);
149 if (event
->spontaneous()) {
155 void InfoSidebarPage::resizeEvent(QResizeEvent
* event
)
157 // If the text inside the name label or the info label cannot
158 // get wrapped, then the maximum width of the label is increased
159 // so that the width of the information sidebar gets increased.
160 // To prevent this, the maximum width is adjusted to
161 // the current width of the sidebar.
162 const int maxWidth
= event
->size().width() - KDialog::spacingHint() * 4;
163 m_nameLabel
->setMaximumWidth(maxWidth
);
164 m_infoLabel
->setMaximumWidth(maxWidth
);
165 SidebarPage::resizeEvent(event
);
168 void InfoSidebarPage::showItemInfo()
176 const KFileItemList
& selectedItems
= selection();
179 if (selectedItems
.isEmpty()) {
182 file
= selectedItems
[0].url();
184 if (!file
.isValid()) {
187 const int itemCount
= selectedItems
.count();
189 KIconLoader iconLoader
;
190 QPixmap icon
= iconLoader
.loadIcon("exec",
191 KIconLoader::NoGroup
,
192 KIconLoader::SizeEnormous
);
193 m_preview
->setPixmap(icon
);
194 m_nameLabel
->setText(i18ncp("@info", "%1 item selected", "%1 items selected", selectedItems
.count()));
195 } else if (!applyPlace(file
)) {
196 // try to get a preview pixmap from the item...
200 m_pendingPreview
= true;
201 m_preview
->setPixmap(QPixmap());
203 KIO::PreviewJob
* job
= KIO::filePreview(list
,
205 KIconLoader::SizeEnormous
,
210 job
->setIgnoreMaximumSize(true);
212 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
213 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
214 connect(job
, SIGNAL(failed(const KFileItem
&)),
215 this, SLOT(showIcon(const KFileItem
&)));
218 text
.append(file
.fileName());
220 m_nameLabel
->setText(text
);
226 void InfoSidebarPage::slotTimeout()
228 m_shownUrl
= m_urlCandidate
;
232 void InfoSidebarPage::showIcon(const KFileItem
& item
)
234 m_pendingPreview
= false;
235 if (!applyPlace(item
.url())) {
236 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
240 void InfoSidebarPage::showPreview(const KFileItem
& item
,
241 const QPixmap
& pixmap
)
244 if (m_pendingPreview
) {
245 m_preview
->setPixmap(pixmap
);
246 m_pendingPreview
= false;
250 bool InfoSidebarPage::applyPlace(const KUrl
& url
)
252 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
253 int count
= placesModel
->rowCount();
255 for (int i
= 0; i
< count
; ++i
) {
256 QModelIndex index
= placesModel
->index(i
, 0);
258 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
260 text
.append(placesModel
->text(index
));
262 m_nameLabel
->setText(text
);
264 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
272 void InfoSidebarPage::cancelRequest()
275 m_pendingPreview
= false;
278 void InfoSidebarPage::showMetaInfo()
282 const KFileItemList
& selectedItems
= selection();
283 if (selectedItems
.size() <= 1) {
284 KFileItem
fileItem(S_IFDIR
, KFileItem::Unknown
, m_shownUrl
);
287 if (fileItem
.isDir()) {
288 addInfoLine(text
, i18nc("@label", "Type:"), i18nc("@label", "Folder"));
290 addInfoLine(text
, i18nc("@label", "Type:"), fileItem
.mimeComment());
292 QString
sizeText(KIO::convertSize(fileItem
.size()));
293 addInfoLine(text
, i18nc("@label", "Size:"), sizeText
);
294 addInfoLine(text
, i18nc("@label", "Modified:"), fileItem
.timeString());
296 // TODO: See convertMetaInfo below, find a way to display only interesting information
298 const KFileMetaInfo
metaInfo(fileItem
.url());
299 if (metaInfo
.isValid()) {
300 const QHash
<QString
, KFileMetaInfoItem
>& items
= metaInfo
.items();
301 QHash
<QString
, KFileMetaInfoItem
>::const_iterator it
= items
.constBegin();
302 const QHash
<QString
, KFileMetaInfoItem
>::const_iterator end
= items
.constEnd();
305 const KFileMetaInfoItem
& metaInfo
= it
.value();
306 const QVariant
& value
= metaInfo
.value();
307 if (value
.isValid() && convertMetaInfo(metaInfo
.name(), labelText
)) {
308 addInfoLine(text
, labelText
, value
.toString());
315 if (MetaDataWidget::metaDataAvailable()) {
316 m_metadataWidget
->setFile(fileItem
.url());
319 if (MetaDataWidget::metaDataAvailable()) {
321 foreach (const KFileItem
& item
, selectedItems
) {
322 urls
.append(item
.url());
324 m_metadataWidget
->setFiles(urls
);
327 unsigned long int totalSize
= 0;
328 foreach (const KFileItem
& item
, selectedItems
) {
329 // TODO: what to do with directories (same with the one-item-selected-code)?,
330 // item.size() does not return the size of the content : not very instinctive for users
331 totalSize
+= item
.size();
333 addInfoLine(text
, i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
335 m_infoLabel
->setText(text
);
338 void InfoSidebarPage::addInfoLine(QString
& text
,
339 const QString
& labelText
,
340 const QString
& infoText
)
342 if (!text
.isEmpty()) {
345 text
+= QString("<b>%1</b> %2").arg(labelText
).arg(infoText
);
348 bool InfoSidebarPage::convertMetaInfo(const QString
& key
, QString
& text
) const
350 // TODO: This code prevents that interesting meta information might be hidden
351 // and only bypasses the current problem that not all the meta information should
352 // be shown to the user. Check whether it's possible with Nepomuk to show
353 // all "user relevant" information in a readable way...
360 // sorted list of keys, where its data should be shown
361 static const MetaKey keys
[] = {
362 { "audio.album", "Album:" },
363 { "audio.artist", "Artist:" },
364 { "audio.title", "Title:" },
367 // do a binary search for the key...
369 int bottom
= sizeof(keys
) / sizeof(MetaKey
) - 1;
370 while (top
<= bottom
) {
371 const int middle
= (top
+ bottom
) / 2;
372 const int result
= key
.compare(keys
[middle
].key
);
375 } else if (result
> 0) {
378 text
= keys
[middle
].text
;
386 #include "infosidebarpage.moc"