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