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