]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanelcontent.cpp
Reactivate showing of general meta information. This is also done as part of the...
[dolphin.git] / src / panels / information / informationpanelcontent.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 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 "informationpanelcontent.h"
21
22 #include <config-nepomuk.h>
23
24 #include <kdialog.h>
25 #include <kfileitem.h>
26 #include <kfileplacesmodel.h>
27 #include <kio/previewjob.h>
28 #include <kiconeffect.h>
29 #include <kiconloader.h>
30 #include <klocale.h>
31 #include <kmenu.h>
32 #include <kseparator.h>
33
34 #include <Phonon/BackendCapabilities>
35 #include <Phonon/MediaObject>
36 #include <Phonon/SeekSlider>
37
38 #include <QEvent>
39 #include <QLabel>
40 #include <QPixmap>
41 #include <QResizeEvent>
42 #include <QScrollArea>
43 #include <QTextLayout>
44 #include <QTextLine>
45 #include <QTimer>
46 #include <QVBoxLayout>
47
48 #include "dolphin_informationpanelsettings.h"
49 #include "settings/dolphinsettings.h"
50 #include "metadatawidget.h"
51 #include "phononwidget.h"
52 #include "pixmapviewer.h"
53
54 /**
55 * Helper function for sorting items with qSort() in
56 * InformationPanelContent::contextMenu().
57 */
58 bool lessThan(const QAction* action1, const QAction* action2)
59 {
60 return action1->text() < action2->text();
61 }
62
63
64 InformationPanelContent::InformationPanelContent(QWidget* parent) :
65 Panel(parent),
66 m_item(),
67 m_pendingPreview(false),
68 m_outdatedPreviewTimer(0),
69 m_nameLabel(0),
70 m_preview(0),
71 m_previewSeparator(0),
72 m_phononWidget(0),
73 m_metaDataWidget(0),
74 m_metaDataArea(0)
75 {
76 parent->installEventFilter(this);
77
78 // Initialize timer for disabling an outdated preview with a small
79 // delay. This prevents flickering if the new preview can be generated
80 // within a very small timeframe.
81 m_outdatedPreviewTimer = new QTimer(this);
82 m_outdatedPreviewTimer->setInterval(300);
83 m_outdatedPreviewTimer->setSingleShot(true);
84 connect(m_outdatedPreviewTimer, SIGNAL(timeout()),
85 this, SLOT(markOutdatedPreview()));
86
87 QVBoxLayout* layout = new QVBoxLayout;
88 layout->setSpacing(KDialog::spacingHint());
89
90 // name
91 m_nameLabel = new QLabel(parent);
92 QFont font = m_nameLabel->font();
93 font.setBold(true);
94 m_nameLabel->setFont(font);
95 m_nameLabel->setAlignment(Qt::AlignHCenter);
96 m_nameLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
97 m_nameLabel->setMaximumWidth(KIconLoader::SizeEnormous);
98
99 // preview
100 const int minPreviewWidth = KIconLoader::SizeEnormous + KIconLoader::SizeMedium;
101
102 m_preview = new PixmapViewer(parent);
103 m_preview->setMinimumWidth(minPreviewWidth);
104 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
105
106 m_phononWidget = new PhononWidget(parent);
107 m_phononWidget->setMinimumWidth(minPreviewWidth);
108 connect(m_phononWidget, SIGNAL(playingStarted()),
109 this, SLOT(slotPlayingStarted()));
110 connect(m_phononWidget, SIGNAL(playingStopped()),
111 this, SLOT(slotPlayingStopped()));
112
113 m_previewSeparator = new KSeparator(parent);
114
115 const bool showPreview = InformationPanelSettings::showPreview();
116 m_preview->setVisible(showPreview);
117 m_previewSeparator->setVisible(showPreview);
118
119 m_metaDataWidget = new MetaDataWidget(parent);
120
121 m_metaDataArea = new QScrollArea(parent);
122 //m_metaDataArea->setWidget(m_metaDataArea);
123 m_metaDataArea->setWidgetResizable(true);
124 m_metaDataArea->setFrameShape(QFrame::NoFrame);
125
126 QWidget* viewport = m_metaDataArea->viewport();
127 viewport->installEventFilter(this);
128
129 QPalette palette = viewport->palette();
130 palette.setColor(viewport->backgroundRole(), QColor(Qt::transparent));
131 viewport->setPalette(palette);
132
133 layout->addWidget(m_nameLabel);
134 layout->addWidget(new KSeparator(this));
135 layout->addWidget(m_preview);
136 layout->addWidget(m_phononWidget);
137 layout->addWidget(m_previewSeparator);
138 if (m_metaDataWidget != 0) {
139 layout->addWidget(m_metaDataWidget);
140 }
141 layout->addWidget(m_metaDataArea);
142 parent->setLayout(layout);
143 }
144
145 InformationPanelContent::~InformationPanelContent()
146 {
147 InformationPanelSettings::self()->writeConfig();
148 }
149
150 void InformationPanelContent::showItem(const KFileItem& item)
151 {
152 m_pendingPreview = false;
153
154 const KUrl itemUrl = item.url();
155 if (!applyPlace(itemUrl)) {
156 // try to get a preview pixmap from the item...
157 m_pendingPreview = true;
158
159 // Mark the currently shown preview as outdated. This is done
160 // with a small delay to prevent a flickering when the next preview
161 // can be shown within a short timeframe. This timer is not started
162 // for directories, as directory previews might fail and return the
163 // same icon.
164 if (!item.isDir()) {
165 m_outdatedPreviewTimer->start();
166 }
167
168 KIO::PreviewJob* job = KIO::filePreview(KFileItemList() << item,
169 m_preview->width(),
170 m_preview->height(),
171 0,
172 0,
173 false,
174 true);
175
176 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
177 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
178 connect(job, SIGNAL(failed(const KFileItem&)),
179 this, SLOT(showIcon(const KFileItem&)));
180
181 setNameLabelText(itemUrl.fileName());
182 }
183
184 if (m_metaDataWidget != 0) {
185 m_metaDataWidget->setItem(item);
186 }
187
188 if (InformationPanelSettings::showPreview()) {
189 const QString mimeType = item.mimetype();
190 const bool usePhonon = Phonon::BackendCapabilities::isMimeTypeAvailable(mimeType) &&
191 (mimeType != "image/png"); // TODO: workaround, as Phonon
192 // thinks it supports PNG images
193 if (usePhonon) {
194 m_phononWidget->show();
195 PhononWidget::Mode mode = mimeType.startsWith(QLatin1String("video"))
196 ? PhononWidget::Video
197 : PhononWidget::Audio;
198 m_phononWidget->setMode(mode);
199 m_phononWidget->setUrl(item.url());
200 if ((mode == PhononWidget::Video) && m_preview->isVisible()) {
201 m_phononWidget->setVideoSize(m_preview->size());
202 }
203 } else {
204 m_phononWidget->hide();
205 m_preview->setVisible(true);
206 }
207 } else {
208 m_phononWidget->hide();
209 }
210
211 m_item = item;
212 }
213
214 void InformationPanelContent::showItems(const KFileItemList& items)
215 {
216 m_pendingPreview = false;
217
218 KIconLoader iconLoader;
219 QPixmap icon = iconLoader.loadIcon("dialog-information",
220 KIconLoader::NoGroup,
221 KIconLoader::SizeEnormous);
222 m_preview->setPixmap(icon);
223 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", items.count()));
224
225 if (m_metaDataWidget != 0) {
226 m_metaDataWidget->setItems(items);
227 }
228
229 m_phononWidget->hide();
230
231 m_item = KFileItem();
232 }
233
234 bool InformationPanelContent::eventFilter(QObject* obj, QEvent* event)
235 {
236 if (event->type() == QEvent::Resize) {
237 QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(event);
238 if (obj == m_metaDataArea->viewport()) {
239 // The size of the meta text area has changed. Adjust the fixed
240 // width in a way that no horizontal scrollbar needs to be shown.
241 //m_metaDataWidget->setFixedWidth(resizeEvent->size().width());
242 } else if (obj == parent()) {
243 // If the text inside the name label or the info label cannot
244 // get wrapped, then the maximum width of the label is increased
245 // so that the width of the information panel gets increased.
246 // To prevent this, the maximum width is adjusted to
247 // the current width of the panel.
248 const int maxWidth = resizeEvent->size().width() - KDialog::spacingHint() * 4;
249 m_nameLabel->setMaximumWidth(maxWidth);
250
251 // The metadata widget also contains a text widget which may return
252 // a large preferred width.
253 if (m_metaDataWidget != 0) {
254 //m_metaDataWidget->setMaximumWidth(maxWidth);
255 }
256
257 // try to increase the preview as large as possible
258 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
259
260 if (m_phononWidget->isVisible() && (m_phononWidget->mode() == PhononWidget::Video)) {
261 // assure that the size of the video player is the same as the preview size
262 m_phononWidget->setVideoSize(QSize(maxWidth, maxWidth));
263 }
264 }
265 }
266 return Panel::eventFilter(obj, event);
267 }
268
269 void InformationPanelContent::configureSettings()
270 {
271 if (m_item.isNull() ||
272 !m_item.nepomukUri().isValid()) {
273 return;
274 }
275
276 KMenu popup(this);
277
278 QAction* previewAction = popup.addAction(i18nc("@action:inmenu", "Preview"));
279 previewAction->setIcon(KIcon("view-preview"));
280 previewAction->setCheckable(true);
281 previewAction->setChecked(InformationPanelSettings::showPreview());
282
283 const bool metaDataAvailable = true; // MetaDataWidget::metaDataAvailable(); TODO
284
285 QAction* ratingAction = popup.addAction(i18nc("@action:inmenu", "Rating"));
286 ratingAction->setIcon(KIcon("rating"));
287 ratingAction->setCheckable(true);
288 ratingAction->setChecked(InformationPanelSettings::showRating());
289 ratingAction->setEnabled(metaDataAvailable);
290
291 QAction* commentAction = popup.addAction(i18nc("@action:inmenu", "Comment"));
292 commentAction->setIcon(KIcon("text-plain"));
293 commentAction->setCheckable(true);
294 commentAction->setChecked(InformationPanelSettings::showComment());
295 commentAction->setEnabled(metaDataAvailable);
296
297 QAction* tagsAction = popup.addAction(i18nc("@action:inmenu", "Tags"));
298 tagsAction->setCheckable(true);
299 tagsAction->setChecked(InformationPanelSettings::showTags());
300 tagsAction->setEnabled(metaDataAvailable);
301
302 KConfig config("kmetainformationrc", KConfig::NoGlobals);
303 KConfigGroup settings = config.group("Show");
304
305 QList<QAction*> actions;
306
307 // Get all meta information labels that are available for
308 // the currently shown file item and add them to the popup.
309 /*Nepomuk::Resource res(m_item.url());
310 QHash<QUrl, Nepomuk::Variant> properties = res.properties();
311 QHash<QUrl, Nepomuk::Variant>::const_iterator it = properties.constBegin();
312 while (it != properties.constEnd()) {
313 Nepomuk::Types::Property prop(it.key());
314 const QString key = prop.name();
315
316 // Meta information provided by Nepomuk that is already
317 // available from KFileItem should not be configurable.
318 bool skip = (key == "fileExtension") ||
319 (key == "name") ||
320 (key == "sourceModified") ||
321 (key == "size") ||
322 (key == "mime type");
323 if (!skip) {
324 // Check whether there is already a meta information
325 // having the same label. In this case don't show it
326 // twice in the menu.
327 foreach (const QAction* action, actions) {
328 if (action->data().toString() == key) {
329 skip = true;
330 break;
331 }
332 }
333 }
334
335 if (!skip) {
336 const QString label = tunedLabel(prop.label());
337 QAction* action = new QAction(label, &popup);
338 action->setCheckable(true);
339 action->setChecked(settings.readEntry(key, true));
340 action->setData(key);
341 actions.append(action);
342 }
343
344 ++it;
345 }*/
346
347 if (!actions.isEmpty()) {
348 popup.addSeparator();
349
350 // add all items alphabetically sorted to the popup
351 qSort(actions.begin(), actions.end(), lessThan);
352 foreach (QAction* action, actions) {
353 popup.addAction(action);
354 }
355 }
356
357 // Open the popup and adjust the settings for the
358 // selected action.
359 QAction* action = popup.exec(QCursor::pos());
360 if (action == 0) {
361 return;
362 }
363
364 const bool isChecked = action->isChecked();
365 if (action == previewAction) {
366 m_preview->setVisible(isChecked);
367 m_previewSeparator->setVisible(isChecked);
368 InformationPanelSettings::setShowPreview(isChecked);
369 } else if (action == ratingAction) {
370 //m_metaDataWidget->setRatingVisible(isChecked);
371 InformationPanelSettings::setShowRating(isChecked);
372 } else if (action == commentAction) {
373 //m_metaDataWidget->setCommentVisible(isChecked);
374 InformationPanelSettings::setShowComment(isChecked);
375 } else if (action == tagsAction) {
376 //m_metaDataWidget->setTagsVisible(isChecked);
377 InformationPanelSettings::setShowTags(isChecked);
378 } else {
379 settings.writeEntry(action->data().toString(), action->isChecked());
380 settings.sync();
381 }
382
383 /*if (m_metaDataWidget != 0) {
384 const bool visible = m_metaDataWidget->isRatingVisible() ||
385 m_metaDataWidget->isCommentVisible() ||
386 m_metaDataWidget->areTagsVisible();
387 m_metaDataSeparator->setVisible(visible);
388 }*/
389
390 showItem(m_item);
391 }
392
393 void InformationPanelContent::showIcon(const KFileItem& item)
394 {
395 m_outdatedPreviewTimer->stop();
396 m_pendingPreview = false;
397 if (!applyPlace(item.url())) {
398 m_preview->setPixmap(item.pixmap(KIconLoader::SizeEnormous));
399 }
400 }
401
402 void InformationPanelContent::showPreview(const KFileItem& item,
403 const QPixmap& pixmap)
404 {
405 m_outdatedPreviewTimer->stop();
406
407 Q_UNUSED(item);
408 if (m_pendingPreview) {
409 m_preview->setPixmap(pixmap);
410 m_pendingPreview = false;
411 }
412 }
413
414 void InformationPanelContent::markOutdatedPreview()
415 {
416 KIconEffect iconEffect;
417 QPixmap disabledPixmap = iconEffect.apply(m_preview->pixmap(),
418 KIconLoader::Desktop,
419 KIconLoader::DisabledState);
420 m_preview->setPixmap(disabledPixmap);
421 }
422
423 void InformationPanelContent::slotPlayingStarted()
424 {
425 m_preview->setVisible(m_phononWidget->mode() != PhononWidget::Video);
426 }
427
428 void InformationPanelContent::slotPlayingStopped()
429 {
430 m_preview->setVisible(true);
431 }
432
433 bool InformationPanelContent::applyPlace(const KUrl& url)
434 {
435 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
436 int count = placesModel->rowCount();
437
438 for (int i = 0; i < count; ++i) {
439 QModelIndex index = placesModel->index(i, 0);
440
441 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
442 setNameLabelText(placesModel->text(index));
443 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
444 return true;
445 }
446 }
447
448 return false;
449 }
450
451 void InformationPanelContent::setNameLabelText(const QString& text)
452 {
453 QTextOption textOption;
454 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
455
456 QTextLayout textLayout(text);
457 textLayout.setFont(m_nameLabel->font());
458 textLayout.setTextOption(textOption);
459
460 QString wrappedText;
461 wrappedText.reserve(text.length());
462
463 // wrap the text to fit into the width of m_nameLabel
464 textLayout.beginLayout();
465 QTextLine line = textLayout.createLine();
466 while (line.isValid()) {
467 line.setLineWidth(m_nameLabel->width());
468 wrappedText += text.mid(line.textStart(), line.textLength());
469
470 line = textLayout.createLine();
471 if (line.isValid()) {
472 wrappedText += QChar::LineSeparator;
473 }
474 }
475 textLayout.endLayout();
476
477 m_nameLabel->setText(wrappedText);
478 }
479
480 #include "informationpanelcontent.moc"