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