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