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