]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanelcontent.cpp
Use KFileMetaDataWidget from kdelibs. Still open: Provide dialog which wraps KFileMet...
[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 <kio/previewjob.h>
27 #include <kiconeffect.h>
28 #include <kiconloader.h>
29 #include <klocale.h>
30 #include <kmenu.h>
31 #include <kseparator.h>
32
33 #include <Phonon/BackendCapabilities>
34 #include <Phonon/MediaObject>
35 #include <Phonon/SeekSlider>
36
37 #include <QEvent>
38 #include <QLabel>
39 #include <QPixmap>
40 #include <QPointer>
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 "phononwidget.h"
51 #include "pixmapviewer.h"
52
53 InformationPanelContent::InformationPanelContent(QWidget* parent) :
54 Panel(parent),
55 m_item(),
56 m_pendingPreview(false),
57 m_outdatedPreviewTimer(0),
58 m_preview(0),
59 m_phononWidget(0),
60 m_nameLabel(0),
61 m_metaDataWidget(0),
62 m_metaDataArea(0)
63 {
64 parent->installEventFilter(this);
65
66 // Initialize timer for disabling an outdated preview with a small
67 // delay. This prevents flickering if the new preview can be generated
68 // within a very small timeframe.
69 m_outdatedPreviewTimer = new QTimer(this);
70 m_outdatedPreviewTimer->setInterval(300);
71 m_outdatedPreviewTimer->setSingleShot(true);
72 connect(m_outdatedPreviewTimer, SIGNAL(timeout()),
73 this, SLOT(markOutdatedPreview()));
74
75 QVBoxLayout* layout = new QVBoxLayout;
76 layout->setSpacing(KDialog::spacingHint());
77
78 // preview
79 const int minPreviewWidth = KIconLoader::SizeEnormous + KIconLoader::SizeMedium;
80
81 m_preview = new PixmapViewer(parent);
82 m_preview->setMinimumWidth(minPreviewWidth);
83 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
84
85 m_phononWidget = new PhononWidget(parent);
86 m_phononWidget->setMinimumWidth(minPreviewWidth);
87 connect(m_phononWidget, SIGNAL(playingStarted()),
88 this, SLOT(slotPlayingStarted()));
89 connect(m_phononWidget, SIGNAL(playingStopped()),
90 this, SLOT(slotPlayingStopped()));
91
92 // name
93 m_nameLabel = new QLabel(parent);
94 QFont font = m_nameLabel->font();
95 font.setBold(true);
96 m_nameLabel->setFont(font);
97 m_nameLabel->setAlignment(Qt::AlignHCenter);
98 m_nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
99
100 const bool showPreview = InformationPanelSettings::showPreview();
101 m_preview->setVisible(showPreview);
102
103 m_metaDataWidget = new KFileMetaDataWidget(parent);
104 m_metaDataWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
105 connect(m_metaDataWidget, SIGNAL(urlActivated(KUrl)), this, SIGNAL(urlActivated(KUrl)));
106
107 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
108 // at the bottom. This prevents that the meta data widget gets vertically stretched
109 // in the case where the height of m_metaDataArea > m_metaDataWidget.
110 QWidget* metaDataWidgetContainer = new QWidget(parent);
111 QVBoxLayout* containerLayout = new QVBoxLayout(metaDataWidgetContainer);
112 containerLayout->setContentsMargins(0, 0, 0, 0);
113 containerLayout->setSpacing(0);
114 containerLayout->addWidget(m_metaDataWidget);
115 QWidget* stretchWidget = new QWidget(metaDataWidgetContainer);
116 stretchWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
117 containerLayout->addWidget(stretchWidget);
118
119 m_metaDataArea = new QScrollArea(parent);
120 m_metaDataArea->setWidget(metaDataWidgetContainer);
121 m_metaDataArea->setWidgetResizable(true);
122 m_metaDataArea->setFrameShape(QFrame::NoFrame);
123
124 QWidget* viewport = m_metaDataArea->viewport();
125 viewport->installEventFilter(this);
126
127 QPalette palette = viewport->palette();
128 palette.setColor(viewport->backgroundRole(), QColor(Qt::transparent));
129 viewport->setPalette(palette);
130
131 layout->addWidget(m_preview);
132 layout->addWidget(m_phononWidget);
133 layout->addWidget(m_nameLabel);
134 layout->addWidget(new KSeparator());
135 layout->addWidget(m_metaDataArea);
136 parent->setLayout(layout);
137 }
138
139 InformationPanelContent::~InformationPanelContent()
140 {
141 InformationPanelSettings::self()->writeConfig();
142 }
143
144 void InformationPanelContent::showItem(const KFileItem& item)
145 {
146 m_pendingPreview = false;
147
148 const KUrl itemUrl = item.url();
149 const bool isNepomukSearchUrl = itemUrl.protocol().startsWith("nepomuk") && item.nepomukUri().isEmpty();
150 if (!applyPlace(itemUrl)) {
151 if (isNepomukSearchUrl) {
152 // in the case of a Nepomuk query-URL the URL is not readable for humans
153 // (at least not useful to show in the Information Panel)
154 KIconLoader iconLoader;
155 QPixmap icon = iconLoader.loadIcon("nepomuk",
156 KIconLoader::NoGroup,
157 KIconLoader::SizeEnormous);
158 m_preview->setPixmap(icon);
159 setNameLabelText(QString());
160 } else {
161 // try to get a preview pixmap from the item...
162 m_pendingPreview = true;
163
164 // Mark the currently shown preview as outdated. This is done
165 // with a small delay to prevent a flickering when the next preview
166 // can be shown within a short timeframe. This timer is not started
167 // for directories, as directory previews might fail and return the
168 // same icon.
169 if (!item.isDir()) {
170 m_outdatedPreviewTimer->start();
171 }
172
173 KIO::PreviewJob* job = KIO::filePreview(KFileItemList() << item,
174 m_preview->width(),
175 m_preview->height(),
176 0,
177 0,
178 false,
179 true);
180
181 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
182 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
183 connect(job, SIGNAL(failed(const KFileItem&)),
184 this, SLOT(showIcon(const KFileItem&)));
185
186 setNameLabelText(item.text());
187 }
188 }
189
190 if (m_metaDataWidget != 0) {
191 if (isNepomukSearchUrl) {
192 m_metaDataWidget->hide();
193 } else {
194 m_metaDataWidget->show();
195 m_metaDataWidget->setItems(KFileItemList() << item);
196 }
197 }
198
199 if (InformationPanelSettings::showPreview()) {
200 const QString mimeType = item.mimetype();
201 const bool usePhonon = Phonon::BackendCapabilities::isMimeTypeAvailable(mimeType) &&
202 (mimeType != "image/png"); // TODO: workaround, as Phonon
203 // thinks it supports PNG images
204 if (usePhonon) {
205 m_phononWidget->show();
206 PhononWidget::Mode mode = mimeType.startsWith(QLatin1String("video"))
207 ? PhononWidget::Video
208 : PhononWidget::Audio;
209 m_phononWidget->setMode(mode);
210 m_phononWidget->setUrl(item.url());
211 if ((mode == PhononWidget::Video) && m_preview->isVisible()) {
212 m_phononWidget->setVideoSize(m_preview->size());
213 }
214 } else {
215 m_phononWidget->hide();
216 m_preview->setVisible(true);
217 }
218 } else {
219 m_phononWidget->hide();
220 }
221
222 m_item = item;
223 }
224
225 void InformationPanelContent::showItems(const KFileItemList& items)
226 {
227 m_pendingPreview = false;
228
229 KIconLoader iconLoader;
230 QPixmap icon = iconLoader.loadIcon("dialog-information",
231 KIconLoader::NoGroup,
232 KIconLoader::SizeEnormous);
233 m_preview->setPixmap(icon);
234 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", items.count()));
235
236 if (m_metaDataWidget != 0) {
237 m_metaDataWidget->setItems(items);
238 }
239
240 m_phononWidget->hide();
241
242 m_item = KFileItem();
243 }
244
245 bool InformationPanelContent::eventFilter(QObject* obj, QEvent* event)
246 {
247 switch (event->type()) {
248 case QEvent::Resize: {
249 QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(event);
250 if (obj == m_metaDataArea->viewport()) {
251 // The size of the meta text area has changed. Adjust the fixed
252 // width in a way that no horizontal scrollbar needs to be shown.
253 m_metaDataWidget->setFixedWidth(resizeEvent->size().width());
254 } else if (obj == parent()) {
255 adjustWidgetSizes(resizeEvent->size().width());
256 }
257 break;
258 }
259
260 case QEvent::Polish:
261 adjustWidgetSizes(parentWidget()->width());
262 break;
263
264 default:
265 break;
266 }
267
268 return Panel::eventFilter(obj, event);
269 }
270
271 void InformationPanelContent::configureSettings()
272 {
273 KMenu popup(this);
274
275 QAction* previewAction = popup.addAction(i18nc("@action:inmenu", "Preview"));
276 previewAction->setIcon(KIcon("view-preview"));
277 previewAction->setCheckable(true);
278 previewAction->setChecked(InformationPanelSettings::showPreview());
279
280 QAction* configureAction = popup.addAction(i18nc("@action:inmenu", "Configure..."));
281 configureAction->setIcon(KIcon("configure"));
282
283 // Open the popup and adjust the settings for the
284 // selected action.
285 QAction* action = popup.exec(QCursor::pos());
286 if (action == 0) {
287 return;
288 }
289
290 const bool isChecked = action->isChecked();
291 if (action == previewAction) {
292 m_preview->setVisible(isChecked);
293 InformationPanelSettings::setShowPreview(isChecked);
294 } else if (action == configureAction) {
295 // TODO:
296 //QPointer<KFileMetaDataConfigurationDialog> dialog =
297 // new KFileMetaDataConfigurationDialog(this);
298 //dialog->setDescription(i18nc("@label::textbox",
299 // "Configure which data should be shown in the Information Panel."));
300 //dialog->setItems(m_metaDataWidget->items());
301 //dialog->exec();
302 //delete dialog;
303 }
304
305 if (!m_item.isNull() && m_item.nepomukUri().isValid()) {
306 showItem(m_item);
307 }
308 }
309
310 void InformationPanelContent::showIcon(const KFileItem& item)
311 {
312 m_outdatedPreviewTimer->stop();
313 m_pendingPreview = false;
314 if (!applyPlace(item.url())) {
315 m_preview->setPixmap(item.pixmap(KIconLoader::SizeEnormous));
316 }
317 }
318
319 void InformationPanelContent::showPreview(const KFileItem& item,
320 const QPixmap& pixmap)
321 {
322 m_outdatedPreviewTimer->stop();
323
324 Q_UNUSED(item);
325 if (m_pendingPreview) {
326 m_preview->setPixmap(pixmap);
327 m_pendingPreview = false;
328 }
329 }
330
331 void InformationPanelContent::markOutdatedPreview()
332 {
333 KIconEffect iconEffect;
334 QPixmap disabledPixmap = iconEffect.apply(m_preview->pixmap(),
335 KIconLoader::Desktop,
336 KIconLoader::DisabledState);
337 m_preview->setPixmap(disabledPixmap);
338 }
339
340 void InformationPanelContent::slotPlayingStarted()
341 {
342 m_preview->setVisible(m_phononWidget->mode() != PhononWidget::Video);
343 }
344
345 void InformationPanelContent::slotPlayingStopped()
346 {
347 m_preview->setVisible(true);
348 }
349
350 bool InformationPanelContent::applyPlace(const KUrl& url)
351 {
352 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
353 int count = placesModel->rowCount();
354
355 for (int i = 0; i < count; ++i) {
356 QModelIndex index = placesModel->index(i, 0);
357
358 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
359 setNameLabelText(placesModel->text(index));
360 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
361 return true;
362 }
363 }
364
365 return false;
366 }
367
368 void InformationPanelContent::setNameLabelText(const QString& text)
369 {
370 QTextOption textOption;
371 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
372
373 QTextLayout textLayout(text);
374 textLayout.setFont(m_nameLabel->font());
375 textLayout.setTextOption(textOption);
376
377 QString wrappedText;
378 wrappedText.reserve(text.length());
379
380 // wrap the text to fit into the width of m_nameLabel
381 textLayout.beginLayout();
382 QTextLine line = textLayout.createLine();
383 while (line.isValid()) {
384 line.setLineWidth(m_nameLabel->width());
385 wrappedText += text.mid(line.textStart(), line.textLength());
386
387 line = textLayout.createLine();
388 if (line.isValid()) {
389 wrappedText += QChar::LineSeparator;
390 }
391 }
392 textLayout.endLayout();
393
394 m_nameLabel->setText(wrappedText);
395 }
396
397 void InformationPanelContent::adjustWidgetSizes(int width)
398 {
399 // If the text inside the name label or the info label cannot
400 // get wrapped, then the maximum width of the label is increased
401 // so that the width of the information panel gets increased.
402 // To prevent this, the maximum width is adjusted to
403 // the current width of the panel.
404 const int maxWidth = width - KDialog::spacingHint() * 4;
405 m_nameLabel->setMaximumWidth(maxWidth);
406
407 // The metadata widget also contains a text widget which may return
408 // a large preferred width.
409 if (m_metaDataWidget != 0) {
410 m_metaDataWidget->setMaximumWidth(maxWidth);
411 }
412
413 // try to increase the preview as large as possible
414 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
415
416 if (m_phononWidget->isVisible() && (m_phononWidget->mode() == PhononWidget::Video)) {
417 // assure that the size of the video player is the same as the preview size
418 m_phononWidget->setVideoSize(QSize(maxWidth, maxWidth));
419 }
420 }
421
422 #include "informationpanelcontent.moc"