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