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