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