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