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