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