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