]> cloud.milkyroute.net Git - dolphin.git/blob - src/infosidebarpage.cpp
Move the pasteIntoFolder() method from the contextmenu into DolphinView. This allows...
[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 fileItem = KFileItem(KFileItem::Unknown, KFileItem::Unknown, m_shownUrl);
327 fileItem.refresh();
328 } else {
329 fileItem = m_fileItem;
330 }
331
332 if (fileItem.isDir()) {
333 m_metaTextLabel->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
334 } else {
335 m_metaTextLabel->add(i18nc("@label", "Type:"), fileItem.mimeComment());
336
337 m_metaTextLabel->add(i18nc("@label", "Size:"), KIO::convertSize(fileItem.size()));
338 m_metaTextLabel->add(i18nc("@label", "Modified:"), fileItem.timeString());
339
340 // TODO: See convertMetaInfo below, find a way to display only interesting information
341 // in a readable way
342 const KFileMetaInfo::WhatFlags flags = KFileMetaInfo::Fastest |
343 KFileMetaInfo::TechnicalInfo |
344 KFileMetaInfo::ContentInfo |
345 KFileMetaInfo::Thumbnail;
346 const QString path = fileItem.url().url();
347 const KFileMetaInfo fileMetaInfo(path, QString(), flags);
348 if (fileMetaInfo.isValid()) {
349 const QHash<QString, KFileMetaInfoItem>& items = fileMetaInfo.items();
350 QHash<QString, KFileMetaInfoItem>::const_iterator it = items.constBegin();
351 const QHash<QString, KFileMetaInfoItem>::const_iterator end = items.constEnd();
352 QString labelText;
353 while (it != end) {
354 const KFileMetaInfoItem& metaInfoItem = it.value();
355 const QVariant& value = metaInfoItem.value();
356 if (value.isValid() && convertMetaInfo(metaInfoItem.name(), labelText)) {
357 m_metaTextLabel->add(labelText, value.toString());
358 }
359 ++it;
360 }
361 }
362 }
363
364 if (m_metaDataWidget != 0) {
365 m_metaDataWidget->setFile(fileItem.url());
366 }
367 } else {
368 if (m_metaDataWidget != 0) {
369 KUrl::List urls;
370 foreach (const KFileItem& item, selectedItems) {
371 urls.append(item.url());
372 }
373 m_metaDataWidget->setFiles(urls);
374 }
375
376 unsigned long int totalSize = 0;
377 foreach (const KFileItem& item, selectedItems) {
378 // Only count the size of files, not dirs to match what
379 // DolphinViewContainer::selectionStatusBarText() does.
380 if (!item.isDir() && !item.isLink()) {
381 totalSize += item.size();
382 }
383 }
384 m_metaTextLabel->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize));
385 }
386 }
387
388 bool InfoSidebarPage::convertMetaInfo(const QString& key, QString& text) const
389 {
390 // TODO: This code prevents that interesting meta information might be hidden
391 // and only bypasses the current problem that not all the meta information should
392 // be shown to the user. Check whether it's possible with Nepomuk to show
393 // all "user relevant" information in a readable way...
394
395 struct MetaKey {
396 const char* key;
397 const char* text;
398 };
399
400 // sorted list of keys, where its data should be shown
401 static const MetaKey keys[] = {
402 { "audio.album", "Album:" },
403 { "audio.artist", "Artist:" },
404 { "audio.title", "Title:" },
405 };
406
407 // do a binary search for the key...
408 int top = 0;
409 int bottom = sizeof(keys) / sizeof(MetaKey) - 1;
410 while (top <= bottom) {
411 const int middle = (top + bottom) / 2;
412 const int result = key.compare(keys[middle].key);
413 if (result < 0) {
414 bottom = middle - 1;
415 } else if (result > 0) {
416 top = middle + 1;
417 } else {
418 text = keys[middle].text;
419 return true;
420 }
421 }
422
423 return false;
424 }
425
426 #include "infosidebarpage.moc"