]> cloud.milkyroute.net Git - dolphin.git/blob - src/infosidebarpage.cpp
icon renaming:
[dolphin.git] / src / infosidebarpage.cpp
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 ***************************************************************************/
19
20 #include "infosidebarpage.h"
21
22 #include <config-nepomuk.h>
23
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>
35
36 #include <QEvent>
37 #include <QInputDialog>
38 #include <QLabel>
39 #include <QPixmap>
40 #include <QResizeEvent>
41 #include <QTimer>
42 #include <QVBoxLayout>
43
44 #include "dolphinsettings.h"
45 #include "metadatawidget.h"
46 #include "pixmapviewer.h"
47
48 InfoSidebarPage::InfoSidebarPage(QWidget* parent) :
49 SidebarPage(parent),
50 m_pendingPreview(false),
51 m_timer(0),
52 m_preview(0),
53 m_nameLabel(0),
54 m_infoLabel(0),
55 m_metadataWidget(0)
56 {
57 const int spacing = KDialog::spacingHint();
58
59 m_timer = new QTimer(this);
60 m_timer->setSingleShot(true);
61 connect(m_timer, SIGNAL(timeout()),
62 this, SLOT(slotTimeout()));
63
64 QVBoxLayout* layout = new QVBoxLayout;
65 layout->setSpacing(spacing);
66
67 // preview
68 m_preview = new PixmapViewer(this);
69 m_preview->setMinimumWidth(KIconLoader::SizeEnormous);
70 m_preview->setFixedHeight(KIconLoader::SizeEnormous);
71
72 // name
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);
77
78 // general information
79 m_infoLabel = new QLabel(this);
80 m_infoLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
81 m_infoLabel->setTextFormat(Qt::RichText);
82 m_infoLabel->setWordWrap(true);
83 m_infoLabel->setFont(KGlobalSettings::smallestReadableFont());
84
85 if (MetaDataWidget::metaDataAvailable()) {
86 m_metadataWidget = new MetaDataWidget(this);
87 }
88
89 layout->addItem(new QSpacerItem(spacing, spacing, QSizePolicy::Preferred, QSizePolicy::Fixed));
90 layout->addWidget(m_preview);
91 layout->addWidget(m_nameLabel);
92 layout->addWidget(new KSeparator(this));
93 layout->addWidget(m_infoLabel);
94 if (m_metadataWidget != 0) {
95 layout->addWidget(new KSeparator(this));
96 layout->addWidget(m_metadataWidget);
97 }
98 // ensure that widgets in the information side bar are aligned towards the top
99 layout->addStretch(1);
100 setLayout(layout);
101 }
102
103 InfoSidebarPage::~InfoSidebarPage()
104 {
105 }
106
107 QSize InfoSidebarPage::sizeHint() const
108 {
109 QSize size = SidebarPage::sizeHint();
110 size.setWidth(minimumSizeHint().width());
111 return size;
112 }
113
114 void InfoSidebarPage::setUrl(const KUrl& url)
115 {
116 if (url.isValid() && !m_shownUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
117 cancelRequest();
118 m_shownUrl = url;
119 showItemInfo();
120 }
121 }
122
123 void InfoSidebarPage::setSelection(const KFileItemList& selection)
124 {
125 SidebarPage::setSelection(selection);
126 if (selection.size() == 1) {
127 const KUrl url = selection.first().url();
128 if (!url.isEmpty()) {
129 m_urlCandidate = url;
130 }
131 }
132 m_timer->start(TimerDelay);
133 }
134
135 void InfoSidebarPage::requestDelayedItemInfo(const KFileItem& item)
136 {
137 cancelRequest();
138
139 if (!item.isNull() && (selection().size() <= 1)) {
140 const KUrl url = item.url();
141 if (!url.isEmpty()) {
142 m_urlCandidate = url;
143 m_timer->start(TimerDelay);
144 }
145 }
146 }
147
148 void InfoSidebarPage::showEvent(QShowEvent* event)
149 {
150 SidebarPage::showEvent(event);
151 if (event->spontaneous()) {
152 return;
153 }
154 showItemInfo();
155 }
156
157 void InfoSidebarPage::resizeEvent(QResizeEvent* event)
158 {
159 // If the text inside the name label or the info label cannot
160 // get wrapped, then the maximum width of the label is increased
161 // so that the width of the information sidebar gets increased.
162 // To prevent this, the maximum width is adjusted to
163 // the current width of the sidebar.
164 const int maxWidth = event->size().width() - KDialog::spacingHint() * 4;
165 m_nameLabel->setMaximumWidth(maxWidth);
166 m_infoLabel->setMaximumWidth(maxWidth);
167 SidebarPage::resizeEvent(event);
168 }
169
170 void InfoSidebarPage::showItemInfo()
171 {
172 if (!isVisible()) {
173 return;
174 }
175
176 cancelRequest();
177
178 const KFileItemList& selectedItems = selection();
179
180 KUrl file;
181 if (selectedItems.isEmpty()) {
182 file = m_shownUrl;
183 } else {
184 file = selectedItems[0].url();
185 }
186 if (!file.isValid()) {
187 return;
188 }
189 const int itemCount = selectedItems.count();
190 if (itemCount > 1) {
191 KIconLoader iconLoader;
192 QPixmap icon = iconLoader.loadIcon("system-run",
193 KIconLoader::NoGroup,
194 KIconLoader::SizeEnormous);
195 m_preview->setPixmap(icon);
196 m_nameLabel->setText(i18ncp("@info", "%1 item selected", "%1 items selected", selectedItems.count()));
197 } else if (!applyPlace(file)) {
198 // try to get a preview pixmap from the item...
199 KUrl::List list;
200 list.append(file);
201
202 m_pendingPreview = true;
203 m_preview->setPixmap(QPixmap());
204
205 KIO::PreviewJob* job = KIO::filePreview(list,
206 m_preview->width(),
207 KIconLoader::SizeEnormous,
208 0,
209 0,
210 true,
211 false);
212 job->setIgnoreMaximumSize(true);
213
214 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
215 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
216 connect(job, SIGNAL(failed(const KFileItem&)),
217 this, SLOT(showIcon(const KFileItem&)));
218
219 QString text("<b>");
220 text.append(file.fileName());
221 text.append("</b>");
222 m_nameLabel->setText(text);
223 }
224
225 showMetaInfo();
226 }
227
228 void InfoSidebarPage::slotTimeout()
229 {
230 m_shownUrl = m_urlCandidate;
231 showItemInfo();
232 }
233
234 void InfoSidebarPage::showIcon(const KFileItem& item)
235 {
236 m_pendingPreview = false;
237 if (!applyPlace(item.url())) {
238 m_preview->setPixmap(item.pixmap(KIconLoader::SizeEnormous));
239 }
240 }
241
242 void InfoSidebarPage::showPreview(const KFileItem& item,
243 const QPixmap& pixmap)
244 {
245 Q_UNUSED(item);
246 if (m_pendingPreview) {
247 m_preview->setPixmap(pixmap);
248 m_pendingPreview = false;
249 }
250 }
251
252 bool InfoSidebarPage::applyPlace(const KUrl& url)
253 {
254 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
255 int count = placesModel->rowCount();
256
257 for (int i = 0; i < count; ++i) {
258 QModelIndex index = placesModel->index(i, 0);
259
260 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
261 QString text("<b>");
262 text.append(placesModel->text(index));
263 text.append("</b>");
264 m_nameLabel->setText(text);
265
266 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
267 return true;
268 }
269 }
270
271 return false;
272 }
273
274 void InfoSidebarPage::cancelRequest()
275 {
276 m_timer->stop();
277 m_pendingPreview = false;
278 }
279
280 void InfoSidebarPage::showMetaInfo()
281 {
282 QString text;
283
284 const KFileItemList& selectedItems = selection();
285 if (selectedItems.size() <= 1) {
286 KFileItem fileItem(S_IFDIR, KFileItem::Unknown, m_shownUrl);
287 fileItem.refresh();
288
289 if (fileItem.isDir()) {
290 addInfoLine(text, i18nc("@label", "Type:"), i18nc("@label", "Folder"));
291 } else {
292 addInfoLine(text, i18nc("@label", "Type:"), fileItem.mimeComment());
293
294 QString sizeText(KIO::convertSize(fileItem.size()));
295 addInfoLine(text, i18nc("@label", "Size:"), sizeText);
296 addInfoLine(text, i18nc("@label", "Modified:"), fileItem.timeString());
297
298 // TODO: See convertMetaInfo below, find a way to display only interesting information
299 // in a readable way
300 const KFileMetaInfo::WhatFlags flags = KFileMetaInfo::Fastest |
301 KFileMetaInfo::TechnicalInfo |
302 KFileMetaInfo::ContentInfo |
303 KFileMetaInfo::Thumbnail;
304 const QString path = fileItem.url().url();
305 const KFileMetaInfo metaInfo(path, QString(), flags);
306 if (metaInfo.isValid()) {
307 const QHash<QString, KFileMetaInfoItem>& items = metaInfo.items();
308 QHash<QString, KFileMetaInfoItem>::const_iterator it = items.constBegin();
309 const QHash<QString, KFileMetaInfoItem>::const_iterator end = items.constEnd();
310 QString labelText;
311 while (it != end) {
312 const KFileMetaInfoItem& metaInfo = it.value();
313 const QVariant& value = metaInfo.value();
314 if (value.isValid() && convertMetaInfo(metaInfo.name(), labelText)) {
315 addInfoLine(text, labelText, value.toString());
316 }
317 ++it;
318 }
319 }
320 }
321
322 if (MetaDataWidget::metaDataAvailable()) {
323 m_metadataWidget->setFile(fileItem.url());
324 }
325 } else {
326 if (MetaDataWidget::metaDataAvailable()) {
327 KUrl::List urls;
328 foreach (const KFileItem& item, selectedItems) {
329 urls.append(item.url());
330 }
331 m_metadataWidget->setFiles(urls);
332 }
333
334 unsigned long int totalSize = 0;
335 foreach (const KFileItem& item, selectedItems) {
336 // TODO: what to do with directories (same with the one-item-selected-code)?,
337 // item.size() does not return the size of the content : not very instinctive for users
338 totalSize += item.size();
339 }
340 addInfoLine(text, i18nc("@label", "Total size:"), KIO::convertSize(totalSize));
341 }
342 m_infoLabel->setText(text);
343 }
344
345 void InfoSidebarPage::addInfoLine(QString& text,
346 const QString& labelText,
347 const QString& infoText)
348 {
349 if (!text.isEmpty()) {
350 text += "<br/>";
351 }
352 text += QString("<b>%1</b> %2").arg(labelText).arg(infoText);
353 }
354
355 bool InfoSidebarPage::convertMetaInfo(const QString& key, QString& text) const
356 {
357 // TODO: This code prevents that interesting meta information might be hidden
358 // and only bypasses the current problem that not all the meta information should
359 // be shown to the user. Check whether it's possible with Nepomuk to show
360 // all "user relevant" information in a readable way...
361
362 struct MetaKey {
363 const char* key;
364 const char* text;
365 };
366
367 // sorted list of keys, where its data should be shown
368 static const MetaKey keys[] = {
369 { "audio.album", "Album:" },
370 { "audio.artist", "Artist:" },
371 { "audio.title", "Title:" },
372 };
373
374 // do a binary search for the key...
375 int top = 0;
376 int bottom = sizeof(keys) / sizeof(MetaKey) - 1;
377 while (top <= bottom) {
378 const int middle = (top + bottom) / 2;
379 const int result = key.compare(keys[middle].key);
380 if (result < 0) {
381 bottom = middle - 1;
382 } else if (result > 0) {
383 top = middle + 1;
384 } else {
385 text = keys[middle].text;
386 return true;
387 }
388 }
389
390 return false;
391 }
392
393 #include "infosidebarpage.moc"