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