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