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