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