]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanelcontent.cpp
Merge remote-tracking branch 'origin/Applications/16.12'
[dolphin.git] / src / panels / information / informationpanelcontent.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz19@gmail.com> *
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 <KFileItem>
23 #include <KIO/JobUiDelegate>
24 #include <KIO/PreviewJob>
25 #include <KJobWidgets>
26 #include <KIconEffect>
27 #include <KIconLoader>
28 #include <QIcon>
29 #include <KLocalizedString>
30 #include <QMenu>
31 #include <KSeparator>
32 #include <KStringHandler>
33 #include <QTextDocument>
34
35 #ifndef HAVE_BALOO
36 #include <KFileMetaDataWidget>
37 #else
38 #include <Baloo/FileMetaDataWidget>
39 #endif
40
41 #include <panels/places/placesitem.h>
42 #include <panels/places/placesitemmodel.h>
43
44 #include <Phonon/BackendCapabilities>
45 #include <Phonon/MediaObject>
46 #include <Phonon/SeekSlider>
47
48 #include <QEvent>
49 #include <QLabel>
50 #include <QPixmap>
51 #include <QResizeEvent>
52 #include <QScrollArea>
53 #include <QTextLayout>
54 #include <QTextLine>
55 #include <QTimer>
56 #include <QVBoxLayout>
57 #include <QFontDatabase>
58 #include <QStyle>
59
60 #include "dolphin_informationpanelsettings.h"
61 #include "filemetadataconfigurationdialog.h"
62 #include "phononwidget.h"
63 #include "pixmapviewer.h"
64
65 InformationPanelContent::InformationPanelContent(QWidget* parent) :
66 QWidget(parent),
67 m_item(),
68 m_previewJob(0),
69 m_outdatedPreviewTimer(0),
70 m_preview(0),
71 m_phononWidget(0),
72 m_nameLabel(0),
73 m_metaDataWidget(0),
74 m_metaDataArea(0),
75 m_placesItemModel(0)
76 {
77 parent->installEventFilter(this);
78
79 // Initialize timer for disabling an outdated preview with a small
80 // delay. This prevents flickering if the new preview can be generated
81 // within a very small timeframe.
82 m_outdatedPreviewTimer = new QTimer(this);
83 m_outdatedPreviewTimer->setInterval(300);
84 m_outdatedPreviewTimer->setSingleShot(true);
85 connect(m_outdatedPreviewTimer, &QTimer::timeout,
86 this, &InformationPanelContent::markOutdatedPreview);
87
88 QVBoxLayout* layout = new QVBoxLayout(this);
89
90 // preview
91 const int minPreviewWidth = KIconLoader::SizeEnormous + KIconLoader::SizeMedium;
92
93 m_preview = new PixmapViewer(parent);
94 m_preview->setMinimumWidth(minPreviewWidth);
95 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
96
97 m_phononWidget = new PhononWidget(parent);
98 m_phononWidget->hide();
99 m_phononWidget->setMinimumWidth(minPreviewWidth);
100 connect(m_phononWidget, &PhononWidget::hasVideoChanged,
101 this, &InformationPanelContent::slotHasVideoChanged);
102
103 // name
104 m_nameLabel = new QLabel(parent);
105 QFont font = m_nameLabel->font();
106 font.setBold(true);
107 m_nameLabel->setFont(font);
108 m_nameLabel->setTextFormat(Qt::PlainText);
109 m_nameLabel->setAlignment(Qt::AlignHCenter);
110 m_nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
111
112 const bool previewsShown = InformationPanelSettings::previewsShown();
113 m_preview->setVisible(previewsShown);
114
115 #ifndef HAVE_BALOO
116 m_metaDataWidget = new KFileMetaDataWidget(parent);
117 connect(m_metaDataWidget, &KFileMetaDataWidget::urlActivated,
118 this, &InformationPanelContent::urlActivated);
119 #else
120 m_metaDataWidget = new Baloo::FileMetaDataWidget(parent);
121 connect(m_metaDataWidget, &Baloo::FileMetaDataWidget::urlActivated,
122 this, &InformationPanelContent::urlActivated);
123 #endif
124 m_metaDataWidget->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
125 m_metaDataWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
126
127 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
128 // at the bottom. This prevents that the meta data widget gets vertically stretched
129 // in the case where the height of m_metaDataArea > m_metaDataWidget.
130 QWidget* metaDataWidgetContainer = new QWidget(parent);
131 QVBoxLayout* containerLayout = new QVBoxLayout(metaDataWidgetContainer);
132 containerLayout->setContentsMargins(0, 0, 0, 0);
133 containerLayout->setSpacing(0);
134 containerLayout->addWidget(m_metaDataWidget);
135 containerLayout->addStretch();
136
137 m_metaDataArea = new QScrollArea(parent);
138 m_metaDataArea->setWidget(metaDataWidgetContainer);
139 m_metaDataArea->setWidgetResizable(true);
140 m_metaDataArea->setFrameShape(QFrame::NoFrame);
141
142 QWidget* viewport = m_metaDataArea->viewport();
143 viewport->installEventFilter(this);
144
145 QPalette palette = viewport->palette();
146 palette.setColor(viewport->backgroundRole(), QColor(Qt::transparent));
147 viewport->setPalette(palette);
148
149 layout->addWidget(m_preview);
150 layout->addWidget(m_phononWidget);
151 layout->addWidget(m_nameLabel);
152 layout->addWidget(new KSeparator());
153 layout->addWidget(m_metaDataArea);
154
155 m_placesItemModel = new PlacesItemModel(this);
156 }
157
158 InformationPanelContent::~InformationPanelContent()
159 {
160 InformationPanelSettings::self()->save();
161 }
162
163 void InformationPanelContent::showItem(const KFileItem& item)
164 {
165 // If there is a preview job, kill it to prevent that we have jobs for
166 // multiple items running, and thus a race condition (bug 250787).
167 if (m_previewJob) {
168 m_previewJob->kill();
169 }
170
171 const QUrl itemUrl = item.url();
172 const bool isSearchUrl = itemUrl.scheme().contains(QStringLiteral("search")) && item.localPath().isEmpty();
173 if (!applyPlace(itemUrl)) {
174 setNameLabelText(item.text());
175 if (isSearchUrl) {
176 // in the case of a search-URL the URL is not readable for humans
177 // (at least not useful to show in the Information Panel)
178 KIconLoader iconLoader;
179 QPixmap icon = iconLoader.loadIcon(QStringLiteral("nepomuk"),
180 KIconLoader::NoGroup,
181 KIconLoader::SizeEnormous);
182 m_preview->setPixmap(icon);
183 } else {
184 // try to get a preview pixmap from the item...
185
186 // Mark the currently shown preview as outdated. This is done
187 // with a small delay to prevent a flickering when the next preview
188 // can be shown within a short timeframe. This timer is not started
189 // for directories, as directory previews might fail and return the
190 // same icon.
191 if (!item.isDir()) {
192 m_outdatedPreviewTimer->start();
193 }
194
195 m_previewJob = new KIO::PreviewJob(KFileItemList() << item, QSize(m_preview->width(), m_preview->height()));
196 m_previewJob->setScaleType(KIO::PreviewJob::Unscaled);
197 m_previewJob->setIgnoreMaximumSize(item.isLocalFile());
198 if (m_previewJob->uiDelegate()) {
199 KJobWidgets::setWindow(m_previewJob, this);
200 }
201
202 connect(m_previewJob.data(), &KIO::PreviewJob::gotPreview,
203 this, &InformationPanelContent::showPreview);
204 connect(m_previewJob.data(), &KIO::PreviewJob::failed,
205 this, &InformationPanelContent::showIcon);
206 }
207 }
208
209 if (m_metaDataWidget) {
210 m_metaDataWidget->show();
211 m_metaDataWidget->setItems(KFileItemList() << item);
212 }
213
214 if (InformationPanelSettings::previewsShown()) {
215 const QString mimeType = item.mimetype();
216 const bool usePhonon = mimeType.startsWith(QLatin1String("audio/")) || mimeType.startsWith(QLatin1String("video/"));
217 if (usePhonon) {
218 m_phononWidget->show();
219 m_phononWidget->setUrl(item.targetUrl());
220 if (m_preview->isVisible()) {
221 m_phononWidget->setVideoSize(m_preview->size());
222 }
223 } else {
224 m_phononWidget->hide();
225 m_preview->setVisible(true);
226 }
227 } else {
228 m_phononWidget->hide();
229 }
230
231 m_item = item;
232 }
233
234 void InformationPanelContent::showItems(const KFileItemList& items)
235 {
236 // If there is a preview job, kill it to prevent that we have jobs for
237 // multiple items running, and thus a race condition (bug 250787).
238 if (m_previewJob) {
239 m_previewJob->kill();
240 }
241
242 KIconLoader iconLoader;
243 QPixmap icon = iconLoader.loadIcon(QStringLiteral("dialog-information"),
244 KIconLoader::NoGroup,
245 KIconLoader::SizeEnormous);
246 m_preview->setPixmap(icon);
247 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items.count()));
248
249 if (m_metaDataWidget) {
250 m_metaDataWidget->setItems(items);
251 }
252
253 m_phononWidget->hide();
254
255 m_item = KFileItem();
256 }
257
258 bool InformationPanelContent::eventFilter(QObject* obj, QEvent* event)
259 {
260 switch (event->type()) {
261 case QEvent::Resize: {
262 QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(event);
263 if (obj == m_metaDataArea->viewport()) {
264 // The size of the meta text area has changed. Adjust the fixed
265 // width in a way that no horizontal scrollbar needs to be shown.
266 m_metaDataWidget->setFixedWidth(resizeEvent->size().width());
267 } else if (obj == parent()) {
268 adjustWidgetSizes(resizeEvent->size().width());
269 }
270 break;
271 }
272
273 case QEvent::Polish:
274 adjustWidgetSizes(parentWidget()->width());
275 break;
276
277 case QEvent::FontChange:
278 m_metaDataWidget->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
279 break;
280
281 default:
282 break;
283 }
284
285 return QWidget::eventFilter(obj, event);
286 }
287
288 void InformationPanelContent::configureSettings(const QList<QAction*>& customContextMenuActions)
289 {
290 QMenu popup(this);
291
292 QAction* previewAction = popup.addAction(i18nc("@action:inmenu", "Preview"));
293 previewAction->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
294 previewAction->setCheckable(true);
295 previewAction->setChecked(InformationPanelSettings::previewsShown());
296
297 QAction* configureAction = popup.addAction(i18nc("@action:inmenu", "Configure..."));
298 configureAction->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
299
300 popup.addSeparator();
301 foreach (QAction* action, customContextMenuActions) {
302 popup.addAction(action);
303 }
304
305 // Open the popup and adjust the settings for the
306 // selected action.
307 QAction* action = popup.exec(QCursor::pos());
308 if (!action) {
309 return;
310 }
311
312 const bool isChecked = action->isChecked();
313 if (action == previewAction) {
314 m_preview->setVisible(isChecked);
315 InformationPanelSettings::setPreviewsShown(isChecked);
316 } else if (action == configureAction) {
317 FileMetaDataConfigurationDialog* dialog = new FileMetaDataConfigurationDialog(this);
318 dialog->setDescription(i18nc("@label::textbox",
319 "Select which data should be shown in the information panel:"));
320 dialog->setItems(m_metaDataWidget->items());
321 dialog->setAttribute(Qt::WA_DeleteOnClose);
322 dialog->show();
323 connect(dialog, &FileMetaDataConfigurationDialog::destroyed, this, &InformationPanelContent::refreshMetaData);
324 }
325 }
326
327 void InformationPanelContent::showIcon(const KFileItem& item)
328 {
329 m_outdatedPreviewTimer->stop();
330 if (!applyPlace(item.targetUrl())) {
331 const QPixmap icon = KIconLoader::global()->loadIcon(item.iconName(), KIconLoader::Desktop,
332 KIconLoader::SizeEnormous, KIconLoader::DefaultState,
333 item.overlays());
334 m_preview->setPixmap(icon);
335 }
336 }
337
338 void InformationPanelContent::showPreview(const KFileItem& item,
339 const QPixmap& pixmap)
340 {
341 m_outdatedPreviewTimer->stop();
342 Q_UNUSED(item);
343
344 QPixmap p = pixmap;
345 KIconLoader::global()->drawOverlays(item.overlays(), p, KIconLoader::Desktop);
346 m_preview->setPixmap(p);
347 }
348
349 void InformationPanelContent::markOutdatedPreview()
350 {
351 KIconEffect *iconEffect = KIconLoader::global()->iconEffect();
352 QPixmap disabledPixmap = iconEffect->apply(m_preview->pixmap(),
353 KIconLoader::Desktop,
354 KIconLoader::DisabledState);
355 m_preview->setPixmap(disabledPixmap);
356 }
357
358 void InformationPanelContent::slotHasVideoChanged(bool hasVideo)
359 {
360 m_preview->setVisible(!hasVideo);
361 }
362
363 void InformationPanelContent::refreshMetaData()
364 {
365 if (!m_item.isNull()) {
366 showItem(m_item);
367 }
368 }
369
370 bool InformationPanelContent::applyPlace(const QUrl& url)
371 {
372 const int count = m_placesItemModel->count();
373 for (int i = 0; i < count; ++i) {
374 const PlacesItem* item = m_placesItemModel->placesItem(i);
375 if (item->url().matches(url, QUrl::StripTrailingSlash)) {
376 setNameLabelText(item->text());
377 m_preview->setPixmap(QIcon::fromTheme(item->icon()).pixmap(128, 128));
378 return true;
379 }
380 }
381
382 return false;
383 }
384
385 void InformationPanelContent::setNameLabelText(const QString& text)
386 {
387 QTextOption textOption;
388 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
389
390 const QString processedText = Qt::mightBeRichText(text) ? text : KStringHandler::preProcessWrap(text);
391
392 QTextLayout textLayout(processedText);
393 textLayout.setFont(m_nameLabel->font());
394 textLayout.setTextOption(textOption);
395
396 QString wrappedText;
397 wrappedText.reserve(processedText.length());
398
399 // wrap the text to fit into the width of m_nameLabel
400 textLayout.beginLayout();
401 QTextLine line = textLayout.createLine();
402 while (line.isValid()) {
403 line.setLineWidth(m_nameLabel->width());
404 wrappedText += processedText.midRef(line.textStart(), line.textLength());
405
406 line = textLayout.createLine();
407 if (line.isValid()) {
408 wrappedText += QChar::LineSeparator;
409 }
410 }
411 textLayout.endLayout();
412
413 m_nameLabel->setText(wrappedText);
414 }
415
416 void InformationPanelContent::adjustWidgetSizes(int width)
417 {
418 // If the text inside the name label or the info label cannot
419 // get wrapped, then the maximum width of the label is increased
420 // so that the width of the information panel gets increased.
421 // To prevent this, the maximum width is adjusted to
422 // the current width of the panel.
423 const int maxWidth = width - style()->layoutSpacing(QSizePolicy::DefaultType, QSizePolicy::DefaultType, Qt::Horizontal) * 4;
424 m_nameLabel->setMaximumWidth(maxWidth);
425
426 // The metadata widget also contains a text widget which may return
427 // a large preferred width.
428 if (m_metaDataWidget) {
429 m_metaDataWidget->setMaximumWidth(maxWidth);
430 }
431
432 // try to increase the preview as large as possible
433 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
434
435 if (m_phononWidget->isVisible()) {
436 // assure that the size of the video player is the same as the preview size
437 m_phononWidget->setVideoSize(QSize(maxWidth, maxWidth));
438 }
439 }
440