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