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