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