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