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