Add (and install) svg for the new krunner interface.
[kdebase/uwolfer.git] / apps / dolphin / src / infosidebarpage.cpp
blobf9a0b4a7ea2cf15950dfb9ff5e2f0eec60c903bf
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * *
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. *
8 * *
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. *
13 * *
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>
25 #include <klocale.h>
26 #include <kstandarddirs.h>
27 #include <kio/previewjob.h>
28 #include <kfileitem.h>
29 #include <kdialog.h>
30 #include <kglobalsettings.h>
31 #include <kfilemetainfo.h>
32 #include <kvbox.h>
33 #include <kseparator.h>
34 #include <kiconloader.h>
36 #include <QEvent>
37 #include <QInputDialog>
38 #include <QLabel>
39 #include <QPixmap>
40 #include <QResizeEvent>
41 #include <QTimer>
42 #include <QVBoxLayout>
44 #include "dolphinsettings.h"
45 #include "metadatawidget.h"
46 #include "pixmapviewer.h"
48 InfoSidebarPage::InfoSidebarPage(QWidget* parent) :
49 SidebarPage(parent),
50 m_pendingPreview(false),
51 m_shownUrl(),
52 m_urlCandidate(),
53 m_fileItem(),
54 m_preview(0),
55 m_nameLabel(0),
56 m_infoLabel(0),
57 m_metaDataWidget(0)
59 const int spacing = KDialog::spacingHint();
61 m_timer = new QTimer(this);
62 m_timer->setSingleShot(true);
63 connect(m_timer, SIGNAL(timeout()),
64 this, SLOT(slotTimeout()));
66 QVBoxLayout* layout = new QVBoxLayout;
67 layout->setSpacing(spacing);
69 // preview
70 m_preview = new PixmapViewer(this);
71 m_preview->setMinimumWidth(KIconLoader::SizeEnormous);
72 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
74 // name
75 m_nameLabel = new QLabel(this);
76 m_nameLabel->setTextFormat(Qt::RichText);
77 m_nameLabel->setAlignment(m_nameLabel->alignment() | Qt::AlignHCenter);
78 m_nameLabel->setWordWrap(true);
80 // general information
81 m_infoLabel = new QLabel(this);
82 m_infoLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
83 m_infoLabel->setTextFormat(Qt::RichText);
84 m_infoLabel->setWordWrap(true);
85 m_infoLabel->setFont(KGlobalSettings::smallestReadableFont());
87 if (MetaDataWidget::metaDataAvailable()) {
88 m_metaDataWidget = new MetaDataWidget(this);
91 layout->addItem(new QSpacerItem(spacing, spacing, QSizePolicy::Preferred, QSizePolicy::Fixed));
92 layout->addWidget(m_preview);
93 layout->addWidget(m_nameLabel);
94 layout->addWidget(new KSeparator(this));
95 layout->addWidget(m_infoLabel);
96 if (m_metaDataWidget != 0) {
97 layout->addWidget(new KSeparator(this));
98 layout->addWidget(m_metaDataWidget);
100 // ensure that widgets in the information side bar are aligned towards the top
101 layout->addStretch(1);
102 setLayout(layout);
105 InfoSidebarPage::~InfoSidebarPage()
109 QSize InfoSidebarPage::sizeHint() const
111 QSize size = SidebarPage::sizeHint();
112 size.setWidth(minimumSizeHint().width());
113 return size;
116 void InfoSidebarPage::setUrl(const KUrl& url)
118 if (url.isValid() && !m_shownUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
119 cancelRequest();
120 m_shownUrl = url;
121 showItemInfo();
125 void InfoSidebarPage::setSelection(const KFileItemList& selection)
127 SidebarPage::setSelection(selection);
128 if (selection.size() == 1) {
129 const KUrl url = selection.first().url();
130 if (!url.isEmpty()) {
131 m_urlCandidate = url;
134 m_timer->start(TimerDelay);
137 void InfoSidebarPage::requestDelayedItemInfo(const KFileItem& item)
139 cancelRequest();
141 m_fileItem = KFileItem();
143 if (!item.isNull() && (selection().size() <= 1)) {
144 const KUrl url = item.url();
145 if (!url.isEmpty()) {
146 m_urlCandidate = url;
147 m_fileItem = item;
148 m_timer->start(TimerDelay);
153 void InfoSidebarPage::showEvent(QShowEvent* event)
155 SidebarPage::showEvent(event);
156 if (event->spontaneous()) {
157 return;
159 showItemInfo();
162 void InfoSidebarPage::resizeEvent(QResizeEvent* event)
164 // If the text inside the name label or the info label cannot
165 // get wrapped, then the maximum width of the label is increased
166 // so that the width of the information sidebar gets increased.
167 // To prevent this, the maximum width is adjusted to
168 // the current width of the sidebar.
169 const int maxWidth = event->size().width() - KDialog::spacingHint() * 4;
170 m_nameLabel->setMaximumWidth(maxWidth);
171 m_infoLabel->setMaximumWidth(maxWidth);
173 // try to increase the preview as large as possible
174 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
175 m_urlCandidate = m_shownUrl; // reset the URL candidate if a resizing is done
176 m_timer->start(TimerDelay);
178 SidebarPage::resizeEvent(event);
181 void InfoSidebarPage::showItemInfo()
183 if (!isVisible()) {
184 return;
187 cancelRequest();
189 const KFileItemList& selectedItems = selection();
191 KUrl file;
192 if (selectedItems.isEmpty()) {
193 file = m_shownUrl;
194 } else {
195 file = selectedItems[0].url();
197 if (!file.isValid()) {
198 return;
200 const int itemCount = selectedItems.count();
201 if (itemCount > 1) {
202 KIconLoader iconLoader;
203 QPixmap icon = iconLoader.loadIcon("system-run",
204 KIconLoader::NoGroup,
205 m_preview->width());
206 m_preview->setPixmap(icon);
207 m_nameLabel->setText(i18ncp("@info", "%1 item selected", "%1 items selected", selectedItems.count()));
208 } else if (!applyPlace(file)) {
209 // try to get a preview pixmap from the item...
210 KUrl::List list;
211 list.append(file);
213 m_pendingPreview = true;
214 m_preview->setPixmap(QPixmap());
216 KIO::PreviewJob* job = KIO::filePreview(list,
217 m_preview->width(),
218 m_preview->height(),
221 true,
222 false);
223 job->setIgnoreMaximumSize(true);
225 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
226 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
227 connect(job, SIGNAL(failed(const KFileItem&)),
228 this, SLOT(showIcon(const KFileItem&)));
230 QString text("<b>");
231 text.append(file.fileName());
232 text.append("</b>");
233 m_nameLabel->setText(text);
236 showMetaInfo();
239 void InfoSidebarPage::slotTimeout()
241 m_shownUrl = m_urlCandidate;
242 showItemInfo();
245 void InfoSidebarPage::showIcon(const KFileItem& item)
247 m_pendingPreview = false;
248 if (!applyPlace(item.url())) {
249 m_preview->setPixmap(item.pixmap(KIconLoader::SizeEnormous));
253 void InfoSidebarPage::showPreview(const KFileItem& item,
254 const QPixmap& pixmap)
256 Q_UNUSED(item);
257 if (m_pendingPreview) {
258 m_preview->setPixmap(pixmap);
259 m_pendingPreview = false;
263 bool InfoSidebarPage::applyPlace(const KUrl& url)
265 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
266 int count = placesModel->rowCount();
268 for (int i = 0; i < count; ++i) {
269 QModelIndex index = placesModel->index(i, 0);
271 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
272 QString text("<b>");
273 text.append(placesModel->text(index));
274 text.append("</b>");
275 m_nameLabel->setText(text);
277 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
278 return true;
282 return false;
285 void InfoSidebarPage::cancelRequest()
287 m_timer->stop();
288 m_pendingPreview = false;
291 void InfoSidebarPage::showMetaInfo()
293 QString text;
295 const KFileItemList& selectedItems = selection();
296 if (selectedItems.size() <= 1) {
297 KFileItem fileItem;
298 if (m_fileItem.isNull()) {
299 // no pending request is ongoing
300 fileItem = KFileItem(KFileItem::Unknown, KFileItem::Unknown, m_shownUrl);
301 fileItem.refresh();
302 } else {
303 fileItem = m_fileItem;
306 if (fileItem.isDir()) {
307 addInfoLine(text, i18nc("@label", "Type:"), i18nc("@label", "Folder"));
308 } else {
309 addInfoLine(text, i18nc("@label", "Type:"), fileItem.mimeComment());
311 addInfoLine(text, i18nc("@label", "Size:"), KIO::convertSize(fileItem.size()));
312 addInfoLine(text, i18nc("@label", "Modified:"), fileItem.timeString());
314 // TODO: See convertMetaInfo below, find a way to display only interesting information
315 // in a readable way
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 metaInfo(path, QString(), flags);
322 if (metaInfo.isValid()) {
323 const QHash<QString, KFileMetaInfoItem>& items = metaInfo.items();
324 QHash<QString, KFileMetaInfoItem>::const_iterator it = items.constBegin();
325 const QHash<QString, KFileMetaInfoItem>::const_iterator end = items.constEnd();
326 QString labelText;
327 while (it != end) {
328 const KFileMetaInfoItem& metaInfo = it.value();
329 const QVariant& value = metaInfo.value();
330 if (value.isValid() && convertMetaInfo(metaInfo.name(), labelText)) {
331 addInfoLine(text, labelText, value.toString());
333 ++it;
338 if (m_metaDataWidget != 0) {
339 m_metaDataWidget->setFile(fileItem.url());
341 } else {
342 if (m_metaDataWidget != 0) {
343 KUrl::List urls;
344 foreach (const KFileItem& item, selectedItems) {
345 urls.append(item.url());
347 m_metaDataWidget->setFiles(urls);
350 unsigned long int totalSize = 0;
351 foreach (const KFileItem& item, selectedItems) {
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 addInfoLine(text, i18nc("@label", "Total size:"), KIO::convertSize(totalSize));
360 m_infoLabel->setText(text);
363 void InfoSidebarPage::addInfoLine(QString& text,
364 const QString& labelText,
365 const QString& infoText)
367 if (!text.isEmpty()) {
368 text += "<br/>";
370 text += QString("<b>%1</b> %2").arg(labelText).arg(infoText);
373 bool InfoSidebarPage::convertMetaInfo(const QString& key, QString& text) const
375 // TODO: This code prevents that interesting meta information might be hidden
376 // and only bypasses the current problem that not all the meta information should
377 // be shown to the user. Check whether it's possible with Nepomuk to show
378 // all "user relevant" information in a readable way...
380 struct MetaKey {
381 const char* key;
382 const char* text;
385 // sorted list of keys, where its data should be shown
386 static const MetaKey keys[] = {
387 { "audio.album", "Album:" },
388 { "audio.artist", "Artist:" },
389 { "audio.title", "Title:" },
392 // do a binary search for the key...
393 int top = 0;
394 int bottom = sizeof(keys) / sizeof(MetaKey) - 1;
395 while (top <= bottom) {
396 const int middle = (top + bottom) / 2;
397 const int result = key.compare(keys[middle].key);
398 if (result < 0) {
399 bottom = middle - 1;
400 } else if (result > 0) {
401 top = middle + 1;
402 } else {
403 text = keys[middle].text;
404 return true;
408 return false;
411 #include "infosidebarpage.moc"