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