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