]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanelcontent.cpp
Port away from kdelibs4support
[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 m_previewJob = new KIO::PreviewJob(KFileItemList() << item, QSize(m_preview->width(), m_preview->height()));
174 m_previewJob->setScaleType(KIO::PreviewJob::Unscaled);
175 m_previewJob->setIgnoreMaximumSize(item.isLocalFile());
176 if (m_previewJob->uiDelegate()) {
177 KJobWidgets::setWindow(m_previewJob, this);
178 }
179
180 connect(m_previewJob.data(), &KIO::PreviewJob::gotPreview,
181 this, &InformationPanelContent::showPreview);
182 connect(m_previewJob.data(), &KIO::PreviewJob::failed,
183 this, &InformationPanelContent::showIcon);
184 }
185
186 if (m_metaDataWidget) {
187 m_metaDataWidget->setDateFormat(static_cast<Baloo::DateFormats>(InformationPanelSettings::dateFormat()));
188 m_metaDataWidget->show();
189 m_metaDataWidget->setItems(KFileItemList() << item);
190 }
191
192 if (InformationPanelSettings::previewsShown()) {
193 const QString mimeType = item.mimetype();
194 const bool usePhonon = mimeType.startsWith(QLatin1String("audio/")) || mimeType.startsWith(QLatin1String("video/"));
195 if (usePhonon) {
196 m_phononWidget->show();
197 m_phononWidget->setUrl(item.targetUrl());
198 if (m_preview->isVisible()) {
199 m_phononWidget->setVideoSize(m_preview->size());
200 }
201 } else {
202 m_phononWidget->hide();
203 m_preview->setVisible(true);
204 }
205 } else {
206 m_phononWidget->hide();
207 }
208
209 m_item = item;
210 }
211
212 void InformationPanelContent::showItems(const KFileItemList& items)
213 {
214 // If there is a preview job, kill it to prevent that we have jobs for
215 // multiple items running, and thus a race condition (bug 250787).
216 if (m_previewJob) {
217 m_previewJob->kill();
218 }
219
220 m_preview->setPixmap(
221 QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(KIconLoader::SizeEnormous, KIconLoader::SizeEnormous)
222 );
223 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items.count()));
224
225 if (m_metaDataWidget) {
226 m_metaDataWidget->setItems(items);
227 }
228
229 m_phononWidget->hide();
230
231 m_item = KFileItem();
232 }
233
234 bool InformationPanelContent::eventFilter(QObject* obj, QEvent* event)
235 {
236 switch (event->type()) {
237 case QEvent::Resize: {
238 QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(event);
239 if (obj == m_metaDataArea->viewport()) {
240 // The size of the meta text area has changed. Adjust the fixed
241 // width in a way that no horizontal scrollbar needs to be shown.
242 m_metaDataWidget->setFixedWidth(resizeEvent->size().width());
243 } else if (obj == parent()) {
244 adjustWidgetSizes(resizeEvent->size().width());
245 }
246 break;
247 }
248
249 case QEvent::Polish:
250 adjustWidgetSizes(parentWidget()->width());
251 break;
252
253 case QEvent::FontChange:
254 m_metaDataWidget->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
255 break;
256
257 default:
258 break;
259 }
260
261 return QWidget::eventFilter(obj, event);
262 }
263
264 void InformationPanelContent::configureSettings(const QList<QAction*>& customContextMenuActions)
265 {
266 QMenu popup(this);
267
268 QAction* previewAction = popup.addAction(i18nc("@action:inmenu", "Preview"));
269 previewAction->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
270 previewAction->setCheckable(true);
271 previewAction->setChecked(InformationPanelSettings::previewsShown());
272
273 QAction* configureAction = popup.addAction(i18nc("@action:inmenu", "Configure..."));
274 configureAction->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
275
276 QAction* dateformatAction = popup.addAction(i18nc("@action:inmenu", "Condensed Date"));
277 dateformatAction->setIcon(QIcon::fromTheme(QStringLiteral("change-date-symbolic")));
278 dateformatAction->setCheckable(true);
279 dateformatAction->setChecked(InformationPanelSettings::dateFormat() == static_cast<int>(Baloo::DateFormats::ShortFormat));
280
281 popup.addSeparator();
282 foreach (QAction* action, customContextMenuActions) {
283 popup.addAction(action);
284 }
285
286 // Open the popup and adjust the settings for the
287 // selected action.
288 QAction* action = popup.exec(QCursor::pos());
289 if (!action) {
290 return;
291 }
292
293 const bool isChecked = action->isChecked();
294 if (action == previewAction) {
295 m_preview->setVisible(isChecked);
296 InformationPanelSettings::setPreviewsShown(isChecked);
297 } else if (action == configureAction) {
298 FileMetaDataConfigurationDialog* dialog = new FileMetaDataConfigurationDialog(this);
299 dialog->setDescription(i18nc("@label::textbox",
300 "Select which data should be shown in the information panel:"));
301 dialog->setItems(m_metaDataWidget->items());
302 dialog->setAttribute(Qt::WA_DeleteOnClose);
303 dialog->show();
304 connect(dialog, &FileMetaDataConfigurationDialog::destroyed, this, &InformationPanelContent::refreshMetaData);
305 }
306 if (action == dateformatAction) {
307 int dateFormat = static_cast<int>(isChecked ? Baloo::DateFormats::ShortFormat : Baloo::DateFormats::LongFormat);
308
309 InformationPanelSettings::setDateFormat(dateFormat);
310 refreshMetaData();
311 }
312 }
313
314 void InformationPanelContent::showIcon(const KFileItem& item)
315 {
316 m_outdatedPreviewTimer->stop();
317 QPixmap pixmap = QIcon::fromTheme(item.iconName()).pixmap(KIconLoader::SizeEnormous, KIconLoader::SizeEnormous);
318 KIconLoader::global()->drawOverlays(item.overlays(), pixmap, KIconLoader::Desktop);
319 m_preview->setPixmap(pixmap);
320 }
321
322 void InformationPanelContent::showPreview(const KFileItem& item,
323 const QPixmap& pixmap)
324 {
325 m_outdatedPreviewTimer->stop();
326 Q_UNUSED(item);
327
328 QPixmap p = pixmap;
329 KIconLoader::global()->drawOverlays(item.overlays(), p, KIconLoader::Desktop);
330 m_preview->setPixmap(p);
331 }
332
333 void InformationPanelContent::markOutdatedPreview()
334 {
335 KIconEffect *iconEffect = KIconLoader::global()->iconEffect();
336 QPixmap disabledPixmap = iconEffect->apply(m_preview->pixmap(),
337 KIconLoader::Desktop,
338 KIconLoader::DisabledState);
339 m_preview->setPixmap(disabledPixmap);
340 }
341
342 void InformationPanelContent::slotHasVideoChanged(bool hasVideo)
343 {
344 m_preview->setVisible(!hasVideo);
345 }
346
347 void InformationPanelContent::refreshMetaData()
348 {
349 if (!m_item.isNull()) {
350 showItem(m_item);
351 }
352 }
353
354 void InformationPanelContent::setNameLabelText(const QString& text)
355 {
356 QTextOption textOption;
357 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
358
359 const QString processedText = Qt::mightBeRichText(text) ? text : KStringHandler::preProcessWrap(text);
360
361 QTextLayout textLayout(processedText);
362 textLayout.setFont(m_nameLabel->font());
363 textLayout.setTextOption(textOption);
364
365 QString wrappedText;
366 wrappedText.reserve(processedText.length());
367
368 // wrap the text to fit into the width of m_nameLabel
369 textLayout.beginLayout();
370 QTextLine line = textLayout.createLine();
371 while (line.isValid()) {
372 line.setLineWidth(m_nameLabel->width());
373 wrappedText += processedText.midRef(line.textStart(), line.textLength());
374
375 line = textLayout.createLine();
376 if (line.isValid()) {
377 wrappedText += QChar::LineSeparator;
378 }
379 }
380 textLayout.endLayout();
381
382 m_nameLabel->setText(wrappedText);
383 }
384
385 void InformationPanelContent::adjustWidgetSizes(int width)
386 {
387 // If the text inside the name label or the info label cannot
388 // get wrapped, then the maximum width of the label is increased
389 // so that the width of the information panel gets increased.
390 // To prevent this, the maximum width is adjusted to
391 // the current width of the panel.
392 const int maxWidth = width - style()->layoutSpacing(QSizePolicy::DefaultType, QSizePolicy::DefaultType, Qt::Horizontal) * 4;
393 m_nameLabel->setMaximumWidth(maxWidth);
394
395 // The metadata widget also contains a text widget which may return
396 // a large preferred width.
397 if (m_metaDataWidget) {
398 m_metaDataWidget->setMaximumWidth(maxWidth);
399 }
400
401 // try to increase the preview as large as possible
402 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
403
404 if (m_phononWidget->isVisible()) {
405 // assure that the size of the video player is the same as the preview size
406 m_phononWidget->setVideoSize(QSize(maxWidth, maxWidth));
407 }
408 }
409