]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanelcontent.cpp
Merge branch 'Applications/18.04'
[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 <KIO/JobUiDelegate>
23 #include <KIO/PreviewJob>
24 #include <KIconEffect>
25 #include <KIconLoader>
26 #include <KJobWidgets>
27 #include <KLocalizedString>
28 #include <KSeparator>
29 #include <KStringHandler>
30
31 #include <QIcon>
32 #include <QMenu>
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
47 #include <QLabel>
48 #include <QScrollArea>
49 #include <QTextLayout>
50 #include <QTimer>
51 #include <QVBoxLayout>
52 #include <QStyle>
53
54 #include "dolphin_informationpanelsettings.h"
55 #include "filemetadataconfigurationdialog.h"
56 #include "phononwidget.h"
57 #include "pixmapviewer.h"
58
59 InformationPanelContent::InformationPanelContent(QWidget* parent) :
60 QWidget(parent),
61 m_item(),
62 m_previewJob(nullptr),
63 m_outdatedPreviewTimer(nullptr),
64 m_preview(nullptr),
65 m_phononWidget(nullptr),
66 m_nameLabel(nullptr),
67 m_metaDataWidget(nullptr),
68 m_metaDataArea(nullptr),
69 m_placesItemModel(nullptr)
70 {
71 parent->installEventFilter(this);
72
73 // Initialize timer for disabling an outdated preview with a small
74 // delay. This prevents flickering if the new preview can be generated
75 // within a very small timeframe.
76 m_outdatedPreviewTimer = new QTimer(this);
77 m_outdatedPreviewTimer->setInterval(300);
78 m_outdatedPreviewTimer->setSingleShot(true);
79 connect(m_outdatedPreviewTimer, &QTimer::timeout,
80 this, &InformationPanelContent::markOutdatedPreview);
81
82 QVBoxLayout* layout = new QVBoxLayout(this);
83
84 // preview
85 const int minPreviewWidth = KIconLoader::SizeEnormous + KIconLoader::SizeMedium;
86
87 m_preview = new PixmapViewer(parent);
88 m_preview->setMinimumWidth(minPreviewWidth);
89 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
90
91 m_phononWidget = new PhononWidget(parent);
92 m_phononWidget->hide();
93 m_phononWidget->setMinimumWidth(minPreviewWidth);
94 connect(m_phononWidget, &PhononWidget::hasVideoChanged,
95 this, &InformationPanelContent::slotHasVideoChanged);
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->setTextFormat(Qt::PlainText);
103 m_nameLabel->setAlignment(Qt::AlignHCenter);
104 m_nameLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
105
106 const bool previewsShown = InformationPanelSettings::previewsShown();
107 m_preview->setVisible(previewsShown);
108
109 #ifndef HAVE_BALOO
110 m_metaDataWidget = new KFileMetaDataWidget(parent);
111 connect(m_metaDataWidget, &KFileMetaDataWidget::urlActivated,
112 this, &InformationPanelContent::urlActivated);
113 #else
114 m_metaDataWidget = new Baloo::FileMetaDataWidget(parent);
115 m_metaDataWidget->setDateFormat(static_cast<Baloo::DateFormats>(InformationPanelSettings::dateFormat()));
116 connect(m_metaDataWidget, &Baloo::FileMetaDataWidget::urlActivated,
117 this, &InformationPanelContent::urlActivated);
118 #endif
119 m_metaDataWidget->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
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->addStretch();
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 layout->addWidget(m_preview);
141 layout->addWidget(m_phononWidget);
142 layout->addWidget(m_nameLabel);
143 layout->addWidget(new KSeparator());
144 layout->addWidget(m_metaDataArea);
145
146 m_placesItemModel = new PlacesItemModel(this);
147 }
148
149 InformationPanelContent::~InformationPanelContent()
150 {
151 InformationPanelSettings::self()->save();
152 }
153
154 void InformationPanelContent::showItem(const KFileItem& item)
155 {
156 // If there is a preview job, kill it to prevent that we have jobs for
157 // multiple items running, and thus a race condition (bug 250787).
158 if (m_previewJob) {
159 m_previewJob->kill();
160 }
161
162 const QUrl itemUrl = item.url();
163 const bool isSearchUrl = itemUrl.scheme().contains(QStringLiteral("search")) && item.localPath().isEmpty();
164 setNameLabelText(item.text());
165 if (isSearchUrl) {
166 // in the case of a search-URL the URL is not readable for humans
167 // (at least not useful to show in the Information Panel)
168 m_preview->setPixmap(
169 QIcon::fromTheme(QStringLiteral("nepomuk")).pixmap(KIconLoader::SizeEnormous, KIconLoader::SizeEnormous)
170 );
171 } else {
172 // try to get a preview pixmap from the item...
173
174 // Mark the currently shown preview as outdated. This is done
175 // with a small delay to prevent a flickering when the next preview
176 // can be shown within a short timeframe. This timer is not started
177 // for directories, as directory previews might fail and return the
178 // same icon.
179 if (!item.isDir()) {
180 m_outdatedPreviewTimer->start();
181 }
182
183 m_previewJob = new KIO::PreviewJob(KFileItemList() << item, QSize(m_preview->width(), m_preview->height()));
184 m_previewJob->setScaleType(KIO::PreviewJob::Unscaled);
185 m_previewJob->setIgnoreMaximumSize(item.isLocalFile());
186 if (m_previewJob->uiDelegate()) {
187 KJobWidgets::setWindow(m_previewJob, this);
188 }
189
190 connect(m_previewJob.data(), &KIO::PreviewJob::gotPreview,
191 this, &InformationPanelContent::showPreview);
192 connect(m_previewJob.data(), &KIO::PreviewJob::failed,
193 this, &InformationPanelContent::showIcon);
194 }
195
196 if (m_metaDataWidget) {
197 #ifdef HAVE_BALOO
198 m_metaDataWidget->setDateFormat(static_cast<Baloo::DateFormats>(InformationPanelSettings::dateFormat()));
199 #endif
200 m_metaDataWidget->show();
201 m_metaDataWidget->setItems(KFileItemList() << item);
202 }
203
204 if (InformationPanelSettings::previewsShown()) {
205 const QString mimeType = item.mimetype();
206 const bool usePhonon = mimeType.startsWith(QLatin1String("audio/")) || mimeType.startsWith(QLatin1String("video/"));
207 if (usePhonon) {
208 m_phononWidget->show();
209 m_phononWidget->setUrl(item.targetUrl());
210 if (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 // If there is a preview job, kill it to prevent that we have jobs for
227 // multiple items running, and thus a race condition (bug 250787).
228 if (m_previewJob) {
229 m_previewJob->kill();
230 }
231
232 m_preview->setPixmap(
233 QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(KIconLoader::SizeEnormous, KIconLoader::SizeEnormous)
234 );
235 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items.count()));
236
237 if (m_metaDataWidget) {
238 m_metaDataWidget->setItems(items);
239 }
240
241 m_phononWidget->hide();
242
243 m_item = KFileItem();
244 }
245
246 bool InformationPanelContent::eventFilter(QObject* obj, QEvent* event)
247 {
248 switch (event->type()) {
249 case QEvent::Resize: {
250 QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(event);
251 if (obj == m_metaDataArea->viewport()) {
252 // The size of the meta text area has changed. Adjust the fixed
253 // width in a way that no horizontal scrollbar needs to be shown.
254 m_metaDataWidget->setFixedWidth(resizeEvent->size().width());
255 } else if (obj == parent()) {
256 adjustWidgetSizes(resizeEvent->size().width());
257 }
258 break;
259 }
260
261 case QEvent::Polish:
262 adjustWidgetSizes(parentWidget()->width());
263 break;
264
265 case QEvent::FontChange:
266 m_metaDataWidget->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
267 break;
268
269 default:
270 break;
271 }
272
273 return QWidget::eventFilter(obj, event);
274 }
275
276 void InformationPanelContent::configureSettings(const QList<QAction*>& customContextMenuActions)
277 {
278 QMenu popup(this);
279
280 QAction* previewAction = popup.addAction(i18nc("@action:inmenu", "Preview"));
281 previewAction->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
282 previewAction->setCheckable(true);
283 previewAction->setChecked(InformationPanelSettings::previewsShown());
284
285 QAction* configureAction = popup.addAction(i18nc("@action:inmenu", "Configure..."));
286 configureAction->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
287
288 #ifdef HAVE_BALOO
289 QAction* dateformatAction = popup.addAction(i18nc("@action:inmenu", "Condensed Date"));
290 dateformatAction->setIcon(QIcon::fromTheme(QStringLiteral("change-date-symbolic")));
291 dateformatAction->setCheckable(true);
292 dateformatAction->setChecked(InformationPanelSettings::dateFormat() == static_cast<int>(Baloo::DateFormats::ShortFormat));
293 #endif
294 popup.addSeparator();
295 foreach (QAction* action, customContextMenuActions) {
296 popup.addAction(action);
297 }
298
299 // Open the popup and adjust the settings for the
300 // selected action.
301 QAction* action = popup.exec(QCursor::pos());
302 if (!action) {
303 return;
304 }
305
306 const bool isChecked = action->isChecked();
307 if (action == previewAction) {
308 m_preview->setVisible(isChecked);
309 InformationPanelSettings::setPreviewsShown(isChecked);
310 } else if (action == configureAction) {
311 FileMetaDataConfigurationDialog* dialog = new FileMetaDataConfigurationDialog(this);
312 dialog->setDescription(i18nc("@label::textbox",
313 "Select which data should be shown in the information panel:"));
314 dialog->setItems(m_metaDataWidget->items());
315 dialog->setAttribute(Qt::WA_DeleteOnClose);
316 dialog->show();
317 connect(dialog, &FileMetaDataConfigurationDialog::destroyed, this, &InformationPanelContent::refreshMetaData);
318 }
319 #ifdef HAVE_BALOO
320 if (action == dateformatAction) {
321 int dateFormat = static_cast<int>(isChecked ? Baloo::DateFormats::ShortFormat : Baloo::DateFormats::LongFormat);
322
323 InformationPanelSettings::setDateFormat(dateFormat);
324 refreshMetaData();
325 }
326 #endif
327 }
328
329 void InformationPanelContent::showIcon(const KFileItem& item)
330 {
331 m_outdatedPreviewTimer->stop();
332 QPixmap pixmap = QIcon::fromTheme(item.iconName()).pixmap(KIconLoader::SizeEnormous, KIconLoader::SizeEnormous);
333 KIconLoader::global()->drawOverlays(item.overlays(), pixmap, KIconLoader::Desktop);
334 m_preview->setPixmap(pixmap);
335 }
336
337 void InformationPanelContent::showPreview(const KFileItem& item,
338 const QPixmap& pixmap)
339 {
340 m_outdatedPreviewTimer->stop();
341 Q_UNUSED(item);
342
343 QPixmap p = pixmap;
344 KIconLoader::global()->drawOverlays(item.overlays(), p, KIconLoader::Desktop);
345 m_preview->setPixmap(p);
346 }
347
348 void InformationPanelContent::markOutdatedPreview()
349 {
350 KIconEffect *iconEffect = KIconLoader::global()->iconEffect();
351 QPixmap disabledPixmap = iconEffect->apply(m_preview->pixmap(),
352 KIconLoader::Desktop,
353 KIconLoader::DisabledState);
354 m_preview->setPixmap(disabledPixmap);
355 }
356
357 void InformationPanelContent::slotHasVideoChanged(bool hasVideo)
358 {
359 m_preview->setVisible(!hasVideo);
360 }
361
362 void InformationPanelContent::refreshMetaData()
363 {
364 if (!m_item.isNull()) {
365 showItem(m_item);
366 }
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.midRef(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 - style()->layoutSpacing(QSizePolicy::DefaultType, QSizePolicy::DefaultType, Qt::Horizontal) * 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) {
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()) {
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