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