]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanelcontent.cpp
Hide the Phonon-widget per default, otherwise it will temporary be shown during 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 <kdialog.h>
23 #include <kfileitem.h>
24 #include <kfilemetadatawidget.h>
25 #include <kfileplacesmodel.h>
26 #include <kglobalsettings.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 #include <kstringhandler.h>
34
35 #include <Phonon/BackendCapabilities>
36 #include <Phonon/MediaObject>
37 #include <Phonon/SeekSlider>
38
39 #include <QEvent>
40 #include <QLabel>
41 #include <QPixmap>
42 #include <QPointer>
43 #include <QResizeEvent>
44 #include <QScrollArea>
45 #include <QTextDocument>
46 #include <QTextLayout>
47 #include <QTextLine>
48 #include <QTimer>
49 #include <QVBoxLayout>
50
51 #include "dolphin_informationpanelsettings.h"
52 #include "settings/dolphinsettings.h"
53 #include "settings/filemetadataconfigurationdialog.h"
54 #include "phononwidget.h"
55 #include "pixmapviewer.h"
56
57 InformationPanelContent::InformationPanelContent(QWidget* parent) :
58 Panel(parent),
59 m_item(),
60 m_pendingPreview(false),
61 m_outdatedPreviewTimer(0),
62 m_preview(0),
63 m_phononWidget(0),
64 m_nameLabel(0),
65 m_metaDataWidget(0),
66 m_metaDataArea(0)
67 {
68 parent->installEventFilter(this);
69
70 // Initialize timer for disabling an outdated preview with a small
71 // delay. This prevents flickering if the new preview can be generated
72 // within a very small timeframe.
73 m_outdatedPreviewTimer = new QTimer(this);
74 m_outdatedPreviewTimer->setInterval(300);
75 m_outdatedPreviewTimer->setSingleShot(true);
76 connect(m_outdatedPreviewTimer, SIGNAL(timeout()),
77 this, SLOT(markOutdatedPreview()));
78
79 QVBoxLayout* layout = new QVBoxLayout(this);
80 layout->setSpacing(KDialog::spacingHint());
81
82 // preview
83 const int minPreviewWidth = KIconLoader::SizeEnormous + KIconLoader::SizeMedium;
84
85 m_preview = new PixmapViewer(parent);
86 m_preview->setMinimumWidth(minPreviewWidth);
87 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
88
89 m_phononWidget = new PhononWidget(parent);
90 m_phononWidget->hide();
91 m_phononWidget->setMinimumWidth(minPreviewWidth);
92 connect(m_phononWidget, SIGNAL(playingStarted()),
93 this, SLOT(slotPlayingStarted()));
94 connect(m_phononWidget, SIGNAL(playingStopped()),
95 this, SLOT(slotPlayingStopped()));
96
97 // name
98 m_nameLabel = new QLabel(parent);
99 QFont font = m_nameLabel->font();
100 font.setBold(true);
101 m_nameLabel->setFont(font);
102 m_nameLabel->setAlignment(Qt::AlignHCenter);
103 m_nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
104
105 const bool showPreview = InformationPanelSettings::showPreview();
106 m_preview->setVisible(showPreview);
107
108 m_metaDataWidget = new KFileMetaDataWidget(parent);
109 m_metaDataWidget->setFont(KGlobalSettings::smallestReadableFont());
110 m_metaDataWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
111 connect(m_metaDataWidget, SIGNAL(urlActivated(KUrl)), this, SIGNAL(urlActivated(KUrl)));
112
113 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
114 // at the bottom. This prevents that the meta data widget gets vertically stretched
115 // in the case where the height of m_metaDataArea > m_metaDataWidget.
116 QWidget* metaDataWidgetContainer = new QWidget(parent);
117 QVBoxLayout* containerLayout = new QVBoxLayout(metaDataWidgetContainer);
118 containerLayout->setContentsMargins(0, 0, 0, 0);
119 containerLayout->setSpacing(0);
120 containerLayout->addWidget(m_metaDataWidget);
121 containerLayout->addStretch();
122
123 m_metaDataArea = new QScrollArea(parent);
124 m_metaDataArea->setWidget(metaDataWidgetContainer);
125 m_metaDataArea->setWidgetResizable(true);
126 m_metaDataArea->setFrameShape(QFrame::NoFrame);
127
128 QWidget* viewport = m_metaDataArea->viewport();
129 viewport->installEventFilter(this);
130
131 QPalette palette = viewport->palette();
132 palette.setColor(viewport->backgroundRole(), QColor(Qt::transparent));
133 viewport->setPalette(palette);
134
135 layout->addWidget(m_preview);
136 layout->addWidget(m_phononWidget);
137 layout->addWidget(m_nameLabel);
138 layout->addWidget(new KSeparator());
139 layout->addWidget(m_metaDataArea);
140 }
141
142 InformationPanelContent::~InformationPanelContent()
143 {
144 InformationPanelSettings::self()->writeConfig();
145 }
146
147 void InformationPanelContent::showItem(const KFileItem& item)
148 {
149 m_pendingPreview = false;
150
151 const KUrl itemUrl = item.url();
152 const bool isSearchUrl = itemUrl.protocol().contains("search") && item.nepomukUri().isEmpty();
153 if (!applyPlace(itemUrl)) {
154 setNameLabelText(item.text());
155 if (isSearchUrl) {
156 // in the case of a search-URL the URL is not readable for humans
157 // (at least not useful to show in the Information Panel)
158 KIconLoader iconLoader;
159 QPixmap icon = iconLoader.loadIcon("nepomuk",
160 KIconLoader::NoGroup,
161 KIconLoader::SizeEnormous);
162 m_preview->setPixmap(icon);
163 } else {
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 }
190
191 if (m_metaDataWidget != 0) {
192 m_metaDataWidget->show();
193 m_metaDataWidget->setItems(KFileItemList() << 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 switch (event->type()) {
245 case QEvent::Resize: {
246 QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(event);
247 if (obj == m_metaDataArea->viewport()) {
248 // The size of the meta text area has changed. Adjust the fixed
249 // width in a way that no horizontal scrollbar needs to be shown.
250 m_metaDataWidget->setFixedWidth(resizeEvent->size().width());
251 } else if (obj == parent()) {
252 adjustWidgetSizes(resizeEvent->size().width());
253 }
254 break;
255 }
256
257 case QEvent::Polish:
258 adjustWidgetSizes(parentWidget()->width());
259 break;
260
261 default:
262 break;
263 }
264
265 return Panel::eventFilter(obj, event);
266 }
267
268 void InformationPanelContent::configureSettings()
269 {
270 KMenu popup(this);
271
272 QAction* previewAction = popup.addAction(i18nc("@action:inmenu", "Preview"));
273 previewAction->setIcon(KIcon("view-preview"));
274 previewAction->setCheckable(true);
275 previewAction->setChecked(InformationPanelSettings::showPreview());
276
277 QAction* configureAction = popup.addAction(i18nc("@action:inmenu", "Configure..."));
278 configureAction->setIcon(KIcon("configure"));
279
280 // Open the popup and adjust the settings for the
281 // selected action.
282 QAction* action = popup.exec(QCursor::pos());
283 if (action == 0) {
284 return;
285 }
286
287 const bool isChecked = action->isChecked();
288 if (action == previewAction) {
289 m_preview->setVisible(isChecked);
290 InformationPanelSettings::setShowPreview(isChecked);
291 } else if (action == configureAction) {
292 FileMetaDataConfigurationDialog* dialog = new FileMetaDataConfigurationDialog();
293 dialog->setDescription(i18nc("@label::textbox",
294 "Select which data should be shown in the information panel."));
295 dialog->setItems(m_metaDataWidget->items());
296 dialog->setAttribute(Qt::WA_DeleteOnClose);
297 dialog->show();
298 dialog->raise();
299 dialog->activateWindow();
300 connect(dialog, SIGNAL(destroyed()), this, SLOT(refreshMetaData()));
301 }
302 }
303
304 void InformationPanelContent::showIcon(const KFileItem& item)
305 {
306 m_outdatedPreviewTimer->stop();
307 m_pendingPreview = false;
308 if (!applyPlace(item.url())) {
309 m_preview->setPixmap(item.pixmap(KIconLoader::SizeEnormous));
310 }
311 }
312
313 void InformationPanelContent::showPreview(const KFileItem& item,
314 const QPixmap& pixmap)
315 {
316 m_outdatedPreviewTimer->stop();
317
318 Q_UNUSED(item);
319 if (m_pendingPreview) {
320 m_preview->setPixmap(pixmap);
321 m_pendingPreview = false;
322 }
323 }
324
325 void InformationPanelContent::markOutdatedPreview()
326 {
327 KIconEffect iconEffect;
328 QPixmap disabledPixmap = iconEffect.apply(m_preview->pixmap(),
329 KIconLoader::Desktop,
330 KIconLoader::DisabledState);
331 m_preview->setPixmap(disabledPixmap);
332 }
333
334 void InformationPanelContent::slotPlayingStarted()
335 {
336 m_preview->setVisible(m_phononWidget->mode() != PhononWidget::Video);
337 }
338
339 void InformationPanelContent::slotPlayingStopped()
340 {
341 m_preview->setVisible(true);
342 }
343
344 void InformationPanelContent::refreshMetaData()
345 {
346 if (!m_item.isNull() && m_item.nepomukUri().isValid()) {
347 showItem(m_item);
348 }
349 }
350
351 bool InformationPanelContent::applyPlace(const KUrl& url)
352 {
353 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
354 int count = placesModel->rowCount();
355
356 for (int i = 0; i < count; ++i) {
357 QModelIndex index = placesModel->index(i, 0);
358
359 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
360 setNameLabelText(placesModel->text(index));
361 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
362 return true;
363 }
364 }
365
366 return false;
367 }
368
369 void InformationPanelContent::setNameLabelText(const QString& text)
370 {
371 QTextOption textOption;
372 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
373
374 const QString processedText = Qt::mightBeRichText(text) ? text : KStringHandler::preProcessWrap(text);
375
376 QTextLayout textLayout(processedText);
377 textLayout.setFont(m_nameLabel->font());
378 textLayout.setTextOption(textOption);
379
380 QString wrappedText;
381 wrappedText.reserve(processedText.length());
382
383 // wrap the text to fit into the width of m_nameLabel
384 textLayout.beginLayout();
385 QTextLine line = textLayout.createLine();
386 while (line.isValid()) {
387 line.setLineWidth(m_nameLabel->width());
388 wrappedText += processedText.mid(line.textStart(), line.textLength());
389
390 line = textLayout.createLine();
391 if (line.isValid()) {
392 wrappedText += QChar::LineSeparator;
393 }
394 }
395 textLayout.endLayout();
396
397 m_nameLabel->setText(wrappedText);
398 }
399
400 void InformationPanelContent::adjustWidgetSizes(int width)
401 {
402 // If the text inside the name label or the info label cannot
403 // get wrapped, then the maximum width of the label is increased
404 // so that the width of the information panel gets increased.
405 // To prevent this, the maximum width is adjusted to
406 // the current width of the panel.
407 const int maxWidth = width - KDialog::spacingHint() * 4;
408 m_nameLabel->setMaximumWidth(maxWidth);
409
410 // The metadata widget also contains a text widget which may return
411 // a large preferred width.
412 if (m_metaDataWidget != 0) {
413 m_metaDataWidget->setMaximumWidth(maxWidth);
414 }
415
416 // try to increase the preview as large as possible
417 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
418
419 if (m_phononWidget->isVisible() && (m_phononWidget->mode() == PhononWidget::Video)) {
420 // assure that the size of the video player is the same as the preview size
421 m_phononWidget->setVideoSize(QSize(maxWidth, maxWidth));
422 }
423 }
424
425 #include "informationpanelcontent.moc"