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