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