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