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