]> cloud.milkyroute.net Git - dolphin.git/blob - src/infosidebarpage.cpp
allow to show hidden files in the Folders panel (treeview)
[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) {
170 const KUrl url = selection.first().url();
171 if (!url.isEmpty()) {
172 m_urlCandidate = url;
173 }
174 }
175 m_timer->start(TimerDelay);
176 }
177 }
178
179 void InfoSidebarPage::requestDelayedItemInfo(const KFileItem& item)
180 {
181 if (!selection().isEmpty()) {
182 // if items are selected, no item information may get requested
183 return;
184 }
185
186 cancelRequest();
187
188 m_fileItem = KFileItem();
189
190 if (!item.isNull() && (selection().size() <= 1)) {
191 const KUrl url = item.url();
192 if (!url.isEmpty()) {
193 m_urlCandidate = url;
194 m_fileItem = item;
195 m_timer->start(TimerDelay);
196 }
197 }
198 }
199
200 void InfoSidebarPage::showEvent(QShowEvent* event)
201 {
202 SidebarPage::showEvent(event);
203 if (event->spontaneous()) {
204 return;
205 }
206 showItemInfo();
207 }
208
209 void InfoSidebarPage::resizeEvent(QResizeEvent* event)
210 {
211 // If the text inside the name label or the info label cannot
212 // get wrapped, then the maximum width of the label is increased
213 // so that the width of the information sidebar gets increased.
214 // To prevent this, the maximum width is adjusted to
215 // the current width of the sidebar.
216 const int maxWidth = event->size().width() - KDialog::spacingHint() * 4;
217 m_nameLabel->setMaximumWidth(maxWidth);
218
219 // try to increase the preview as large as possible
220 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
221 m_urlCandidate = m_shownUrl; // reset the URL candidate if a resizing is done
222 m_timer->start(TimerDelay);
223
224 SidebarPage::resizeEvent(event);
225 }
226
227 void InfoSidebarPage::showItemInfo()
228 {
229 if (!isVisible()) {
230 return;
231 }
232
233 cancelRequest();
234
235 const KFileItemList& selectedItems = selection();
236 const KUrl file = selectedItems.isEmpty() ? m_shownUrl : selectedItems[0].url();
237 if (!file.isValid()) {
238 return;
239 }
240
241 const int itemCount = selectedItems.count();
242 if (itemCount > 1) {
243 KIconLoader iconLoader;
244 QPixmap icon = iconLoader.loadIcon("dialog-information",
245 KIconLoader::NoGroup,
246 KIconLoader::SizeEnormous);
247 m_preview->setPixmap(icon);
248 m_nameLabel->setText(i18ncp("@info", "%1 item selected", "%1 items selected", selectedItems.count()));
249 } else if (!applyPlace(file)) {
250 // try to get a preview pixmap from the item...
251 KUrl::List list;
252 list.append(file);
253
254 m_pendingPreview = true;
255 m_preview->setPixmap(QPixmap());
256
257 KIO::PreviewJob* job = KIO::filePreview(list,
258 m_preview->width(),
259 m_preview->height(),
260 0,
261 0,
262 true,
263 false);
264 job->setIgnoreMaximumSize(true);
265
266 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
267 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
268 connect(job, SIGNAL(failed(const KFileItem&)),
269 this, SLOT(showIcon(const KFileItem&)));
270
271 m_nameLabel->setText(file.fileName());
272 }
273
274 showMetaInfo();
275 }
276
277 void InfoSidebarPage::slotTimeout()
278 {
279 m_shownUrl = m_urlCandidate;
280 showItemInfo();
281 }
282
283 void InfoSidebarPage::showIcon(const KFileItem& item)
284 {
285 m_pendingPreview = false;
286 if (!applyPlace(item.url())) {
287 m_preview->setPixmap(item.pixmap(KIconLoader::SizeEnormous));
288 }
289 }
290
291 void InfoSidebarPage::showPreview(const KFileItem& item,
292 const QPixmap& pixmap)
293 {
294 Q_UNUSED(item);
295 if (m_pendingPreview) {
296 m_preview->setPixmap(pixmap);
297 m_pendingPreview = false;
298 }
299 }
300
301 bool InfoSidebarPage::applyPlace(const KUrl& url)
302 {
303 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
304 int count = placesModel->rowCount();
305
306 for (int i = 0; i < count; ++i) {
307 QModelIndex index = placesModel->index(i, 0);
308
309 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
310 m_nameLabel->setText(placesModel->text(index));
311 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
312 return true;
313 }
314 }
315
316 return false;
317 }
318
319 void InfoSidebarPage::cancelRequest()
320 {
321 m_timer->stop();
322 }
323
324 void InfoSidebarPage::showMetaInfo()
325 {
326 m_metaTextLabel->clear();
327
328 const KFileItemList& selectedItems = selection();
329 if (selectedItems.size() <= 1) {
330 KFileItem fileItem;
331 if (m_fileItem.isNull()) {
332 // no pending request is ongoing
333 fileItem = KFileItem(KFileItem::Unknown, KFileItem::Unknown, m_shownUrl);
334 fileItem.refresh();
335 } else {
336 fileItem = m_fileItem;
337 }
338
339 if (fileItem.isDir()) {
340 m_metaTextLabel->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
341 } else {
342 m_metaTextLabel->add(i18nc("@label", "Type:"), fileItem.mimeComment());
343
344 m_metaTextLabel->add(i18nc("@label", "Size:"), KIO::convertSize(fileItem.size()));
345 m_metaTextLabel->add(i18nc("@label", "Modified:"), fileItem.timeString());
346
347 // TODO: See convertMetaInfo below, find a way to display only interesting information
348 // in a readable way
349 const KFileMetaInfo::WhatFlags flags = KFileMetaInfo::Fastest |
350 KFileMetaInfo::TechnicalInfo |
351 KFileMetaInfo::ContentInfo |
352 KFileMetaInfo::Thumbnail;
353 const QString path = fileItem.url().url();
354 const KFileMetaInfo fileMetaInfo(path, QString(), flags);
355 if (fileMetaInfo.isValid()) {
356 const QHash<QString, KFileMetaInfoItem>& items = fileMetaInfo.items();
357 QHash<QString, KFileMetaInfoItem>::const_iterator it = items.constBegin();
358 const QHash<QString, KFileMetaInfoItem>::const_iterator end = items.constEnd();
359 QString labelText;
360 while (it != end) {
361 const KFileMetaInfoItem& metaInfoItem = it.value();
362 const QVariant& value = metaInfoItem.value();
363 if (value.isValid() && convertMetaInfo(metaInfoItem.name(), labelText)) {
364 m_metaTextLabel->add(labelText, value.toString());
365 }
366 ++it;
367 }
368 }
369 }
370
371 if (m_metaDataWidget != 0) {
372 m_metaDataWidget->setFile(fileItem.url());
373 }
374 } else {
375 if (m_metaDataWidget != 0) {
376 KUrl::List urls;
377 foreach (const KFileItem& item, selectedItems) {
378 urls.append(item.url());
379 }
380 m_metaDataWidget->setFiles(urls);
381 }
382
383 unsigned long int totalSize = 0;
384 foreach (const KFileItem& item, selectedItems) {
385 // Only count the size of files, not dirs to match what
386 // DolphinViewContainer::selectionStatusBarText() does.
387 if (!item.isDir() && !item.isLink()) {
388 totalSize += item.size();
389 }
390 }
391 m_metaTextLabel->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize));
392 }
393 }
394
395 bool InfoSidebarPage::convertMetaInfo(const QString& key, QString& text) const
396 {
397 // TODO: This code prevents that interesting meta information might be hidden
398 // and only bypasses the current problem that not all the meta information should
399 // be shown to the user. Check whether it's possible with Nepomuk to show
400 // all "user relevant" information in a readable way...
401
402 struct MetaKey {
403 const char* key;
404 const char* text;
405 };
406
407 // sorted list of keys, where its data should be shown
408 static const MetaKey keys[] = {
409 { "audio.album", "Album:" },
410 { "audio.artist", "Artist:" },
411 { "audio.title", "Title:" },
412 };
413
414 // do a binary search for the key...
415 int top = 0;
416 int bottom = sizeof(keys) / sizeof(MetaKey) - 1;
417 while (top <= bottom) {
418 const int middle = (top + bottom) / 2;
419 const int result = key.compare(keys[middle].key);
420 if (result < 0) {
421 bottom = middle - 1;
422 } else if (result > 0) {
423 top = middle + 1;
424 } else {
425 text = keys[middle].text;
426 return true;
427 }
428 }
429
430 return false;
431 }
432
433 #include "infosidebarpage.moc"