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