]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanelcontent.cpp
Open the meta data configuration dialog modeless
[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 <kio/previewjob.h>
27 #include <kiconeffect.h>
28 #include <kiconloader.h>
29 #include <klocale.h>
30 #include <kmenu.h>
31 #include <kseparator.h>
32
33 #include <Phonon/BackendCapabilities>
34 #include <Phonon/MediaObject>
35 #include <Phonon/SeekSlider>
36
37 #include <QEvent>
38 #include <QLabel>
39 #include <QPixmap>
40 #include <QPointer>
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 "settings/filemetadataconfigurationdialog.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 KFileMetaDataWidget(parent);
105 m_metaDataWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
106 connect(m_metaDataWidget, SIGNAL(urlActivated(KUrl)), this, SIGNAL(urlActivated(KUrl)));
107
108 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
109 // at the bottom. This prevents that the meta data widget gets vertically stretched
110 // in the case where the height of m_metaDataArea > m_metaDataWidget.
111 QWidget* metaDataWidgetContainer = new QWidget(parent);
112 QVBoxLayout* containerLayout = new QVBoxLayout(metaDataWidgetContainer);
113 containerLayout->setContentsMargins(0, 0, 0, 0);
114 containerLayout->setSpacing(0);
115 containerLayout->addWidget(m_metaDataWidget);
116 QWidget* stretchWidget = new QWidget(metaDataWidgetContainer);
117 stretchWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
118 containerLayout->addWidget(stretchWidget);
119
120 m_metaDataArea = new QScrollArea(parent);
121 m_metaDataArea->setWidget(metaDataWidgetContainer);
122 m_metaDataArea->setWidgetResizable(true);
123 m_metaDataArea->setFrameShape(QFrame::NoFrame);
124
125 QWidget* viewport = m_metaDataArea->viewport();
126 viewport->installEventFilter(this);
127
128 QPalette palette = viewport->palette();
129 palette.setColor(viewport->backgroundRole(), QColor(Qt::transparent));
130 viewport->setPalette(palette);
131
132 layout->addWidget(m_preview);
133 layout->addWidget(m_phononWidget);
134 layout->addWidget(m_nameLabel);
135 layout->addWidget(new KSeparator());
136 layout->addWidget(m_metaDataArea);
137 parent->setLayout(layout);
138 }
139
140 InformationPanelContent::~InformationPanelContent()
141 {
142 InformationPanelSettings::self()->writeConfig();
143 }
144
145 void InformationPanelContent::showItem(const KFileItem& item)
146 {
147 m_pendingPreview = false;
148
149 const KUrl itemUrl = item.url();
150 const bool isNepomukSearchUrl = itemUrl.protocol().startsWith("nepomuk") && item.nepomukUri().isEmpty();
151 if (!applyPlace(itemUrl)) {
152 if (isNepomukSearchUrl) {
153 // in the case of a Nepomuk query-URL the URL is not readable for humans
154 // (at least not useful to show in the Information Panel)
155 KIconLoader iconLoader;
156 QPixmap icon = iconLoader.loadIcon("nepomuk",
157 KIconLoader::NoGroup,
158 KIconLoader::SizeEnormous);
159 m_preview->setPixmap(icon);
160 setNameLabelText(QString());
161 } else {
162 // try to get a preview pixmap from the item...
163 m_pendingPreview = true;
164
165 // Mark the currently shown preview as outdated. This is done
166 // with a small delay to prevent a flickering when the next preview
167 // can be shown within a short timeframe. This timer is not started
168 // for directories, as directory previews might fail and return the
169 // same icon.
170 if (!item.isDir()) {
171 m_outdatedPreviewTimer->start();
172 }
173
174 KIO::PreviewJob* job = KIO::filePreview(KFileItemList() << item,
175 m_preview->width(),
176 m_preview->height(),
177 0,
178 0,
179 false,
180 true);
181
182 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
183 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
184 connect(job, SIGNAL(failed(const KFileItem&)),
185 this, SLOT(showIcon(const KFileItem&)));
186
187 setNameLabelText(item.text());
188 }
189 }
190
191 if (m_metaDataWidget != 0) {
192 if (isNepomukSearchUrl) {
193 m_metaDataWidget->hide();
194 } else {
195 m_metaDataWidget->show();
196 m_metaDataWidget->setItems(KFileItemList() << item);
197 }
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.url());
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 Panel::eventFilter(obj, event);
270 }
271
272 void InformationPanelContent::configureSettings()
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 // Open the popup and adjust the settings for the
285 // selected action.
286 QAction* action = popup.exec(QCursor::pos());
287 if (action == 0) {
288 return;
289 }
290
291 const bool isChecked = action->isChecked();
292 if (action == previewAction) {
293 m_preview->setVisible(isChecked);
294 InformationPanelSettings::setShowPreview(isChecked);
295 } else if (action == configureAction) {
296 FileMetaDataConfigurationDialog* dialog = new FileMetaDataConfigurationDialog();
297 dialog->setDescription(i18nc("@label::textbox",
298 "Configure which data should be shown in the tooltip."));
299 dialog->setItems(m_metaDataWidget->items());
300 dialog->setAttribute(Qt::WA_DeleteOnClose);
301 dialog->show();
302 dialog->raise();
303 dialog->activateWindow();
304 connect(dialog, SIGNAL(destroyed()), this, SLOT(refreshMetaData()));
305 }
306 }
307
308 void InformationPanelContent::showIcon(const KFileItem& item)
309 {
310 m_outdatedPreviewTimer->stop();
311 m_pendingPreview = false;
312 if (!applyPlace(item.url())) {
313 m_preview->setPixmap(item.pixmap(KIconLoader::SizeEnormous));
314 }
315 }
316
317 void InformationPanelContent::showPreview(const KFileItem& item,
318 const QPixmap& pixmap)
319 {
320 m_outdatedPreviewTimer->stop();
321
322 Q_UNUSED(item);
323 if (m_pendingPreview) {
324 m_preview->setPixmap(pixmap);
325 m_pendingPreview = false;
326 }
327 }
328
329 void InformationPanelContent::markOutdatedPreview()
330 {
331 KIconEffect iconEffect;
332 QPixmap disabledPixmap = iconEffect.apply(m_preview->pixmap(),
333 KIconLoader::Desktop,
334 KIconLoader::DisabledState);
335 m_preview->setPixmap(disabledPixmap);
336 }
337
338 void InformationPanelContent::slotPlayingStarted()
339 {
340 m_preview->setVisible(m_phononWidget->mode() != PhononWidget::Video);
341 }
342
343 void InformationPanelContent::slotPlayingStopped()
344 {
345 m_preview->setVisible(true);
346 }
347
348 void InformationPanelContent::refreshMetaData()
349 {
350 if (!m_item.isNull() && m_item.nepomukUri().isValid()) {
351 showItem(m_item);
352 }
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 void InformationPanelContent::adjustWidgetSizes(int width)
403 {
404 // If the text inside the name label or the info label cannot
405 // get wrapped, then the maximum width of the label is increased
406 // so that the width of the information panel gets increased.
407 // To prevent this, the maximum width is adjusted to
408 // the current width of the panel.
409 const int maxWidth = width - KDialog::spacingHint() * 4;
410 m_nameLabel->setMaximumWidth(maxWidth);
411
412 // The metadata widget also contains a text widget which may return
413 // a large preferred width.
414 if (m_metaDataWidget != 0) {
415 m_metaDataWidget->setMaximumWidth(maxWidth);
416 }
417
418 // try to increase the preview as large as possible
419 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
420
421 if (m_phononWidget->isVisible() && (m_phononWidget->mode() == PhononWidget::Video)) {
422 // assure that the size of the video player is the same as the preview size
423 m_phononWidget->setVideoSize(QSize(maxWidth, maxWidth));
424 }
425 }
426
427 #include "informationpanelcontent.moc"