]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanelcontent.cpp
the configuration menu should be shown also for multiple selections
[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 "metadataconfigurationdialog.h"
52 #include "phononwidget.h"
53 #include "pixmapviewer.h"
54
55 /**
56 * Helper function for sorting items with qSort() in
57 * InformationPanelContent::contextMenu().
58 */
59 bool lessThan(const QAction* action1, const QAction* action2)
60 {
61 return action1->text() < action2->text();
62 }
63
64
65 InformationPanelContent::InformationPanelContent(QWidget* parent) :
66 Panel(parent),
67 m_item(),
68 m_pendingPreview(false),
69 m_outdatedPreviewTimer(0),
70 m_nameLabel(0),
71 m_preview(0),
72 m_previewSeparator(0),
73 m_phononWidget(0),
74 m_metaDataWidget(0),
75 m_metaDataArea(0)
76 {
77 parent->installEventFilter(this);
78
79 // Initialize timer for disabling an outdated preview with a small
80 // delay. This prevents flickering if the new preview can be generated
81 // within a very small timeframe.
82 m_outdatedPreviewTimer = new QTimer(this);
83 m_outdatedPreviewTimer->setInterval(300);
84 m_outdatedPreviewTimer->setSingleShot(true);
85 connect(m_outdatedPreviewTimer, SIGNAL(timeout()),
86 this, SLOT(markOutdatedPreview()));
87
88 QVBoxLayout* layout = new QVBoxLayout;
89 layout->setSpacing(KDialog::spacingHint());
90
91 // name
92 m_nameLabel = new QLabel(parent);
93 QFont font = m_nameLabel->font();
94 font.setBold(true);
95 m_nameLabel->setFont(font);
96 m_nameLabel->setAlignment(Qt::AlignHCenter);
97 m_nameLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
98 m_nameLabel->setMaximumWidth(KIconLoader::SizeEnormous);
99
100 // preview
101 const int minPreviewWidth = KIconLoader::SizeEnormous + KIconLoader::SizeMedium;
102
103 m_preview = new PixmapViewer(parent);
104 m_preview->setMinimumWidth(minPreviewWidth);
105 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
106
107 m_phononWidget = new PhononWidget(parent);
108 m_phononWidget->setMinimumWidth(minPreviewWidth);
109 connect(m_phononWidget, SIGNAL(playingStarted()),
110 this, SLOT(slotPlayingStarted()));
111 connect(m_phononWidget, SIGNAL(playingStopped()),
112 this, SLOT(slotPlayingStopped()));
113
114 m_previewSeparator = new KSeparator(parent);
115
116 const bool showPreview = InformationPanelSettings::showPreview();
117 m_preview->setVisible(showPreview);
118 m_previewSeparator->setVisible(showPreview);
119
120 m_metaDataWidget = new MetaDataWidget(parent);
121 m_metaDataWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
122
123 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
124 // at the bottom. This prevents that the meta data widget gets vertically stretched
125 // in the case where the height of m_metaDataArea > m_metaDataWidget.
126 QWidget* metaDataWidgetContainer = new QWidget(parent);
127 QVBoxLayout* containerLayout = new QVBoxLayout(metaDataWidgetContainer);
128 containerLayout->setContentsMargins(0, 0, 0, 0);
129 containerLayout->setSpacing(0);
130 containerLayout->addWidget(m_metaDataWidget);
131 QWidget* stretchWidget = new QWidget(metaDataWidgetContainer);
132 stretchWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
133 containerLayout->addWidget(stretchWidget);
134
135 m_metaDataArea = new QScrollArea(parent);
136 m_metaDataArea->setWidget(metaDataWidgetContainer);
137 m_metaDataArea->setWidgetResizable(true);
138 m_metaDataArea->setFrameShape(QFrame::NoFrame);
139
140 QWidget* viewport = m_metaDataArea->viewport();
141 viewport->installEventFilter(this);
142
143 QPalette palette = viewport->palette();
144 palette.setColor(viewport->backgroundRole(), QColor(Qt::transparent));
145 viewport->setPalette(palette);
146
147 layout->addWidget(m_nameLabel);
148 layout->addWidget(new KSeparator(this));
149 layout->addWidget(m_preview);
150 layout->addWidget(m_phononWidget);
151 layout->addWidget(m_previewSeparator);
152 layout->addWidget(m_metaDataArea);
153 parent->setLayout(layout);
154 }
155
156 InformationPanelContent::~InformationPanelContent()
157 {
158 InformationPanelSettings::self()->writeConfig();
159 }
160
161 void InformationPanelContent::showItem(const KFileItem& item)
162 {
163 m_pendingPreview = false;
164
165 const KUrl itemUrl = item.url();
166 if (!applyPlace(itemUrl)) {
167 // try to get a preview pixmap from the item...
168 m_pendingPreview = true;
169
170 // Mark the currently shown preview as outdated. This is done
171 // with a small delay to prevent a flickering when the next preview
172 // can be shown within a short timeframe. This timer is not started
173 // for directories, as directory previews might fail and return the
174 // same icon.
175 if (!item.isDir()) {
176 m_outdatedPreviewTimer->start();
177 }
178
179 KIO::PreviewJob* job = KIO::filePreview(KFileItemList() << item,
180 m_preview->width(),
181 m_preview->height(),
182 0,
183 0,
184 false,
185 true);
186
187 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
188 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
189 connect(job, SIGNAL(failed(const KFileItem&)),
190 this, SLOT(showIcon(const KFileItem&)));
191
192 setNameLabelText(itemUrl.fileName());
193 }
194
195 if (m_metaDataWidget != 0) {
196 m_metaDataWidget->setItem(item);
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 m_previewSeparator->setVisible(isChecked);
303 InformationPanelSettings::setShowPreview(isChecked);
304 } else if (action == configureAction) {
305 MetaDataConfigurationDialog dialog(m_metaDataWidget, this, Qt::Dialog);
306 dialog.exec();
307 }
308
309 if (!m_item.isNull() && m_item.nepomukUri().isValid()) {
310 showItem(m_item);
311 }
312 }
313
314 void InformationPanelContent::showIcon(const KFileItem& item)
315 {
316 m_outdatedPreviewTimer->stop();
317 m_pendingPreview = false;
318 if (!applyPlace(item.url())) {
319 m_preview->setPixmap(item.pixmap(KIconLoader::SizeEnormous));
320 }
321 }
322
323 void InformationPanelContent::showPreview(const KFileItem& item,
324 const QPixmap& pixmap)
325 {
326 m_outdatedPreviewTimer->stop();
327
328 Q_UNUSED(item);
329 if (m_pendingPreview) {
330 m_preview->setPixmap(pixmap);
331 m_pendingPreview = false;
332 }
333 }
334
335 void InformationPanelContent::markOutdatedPreview()
336 {
337 KIconEffect iconEffect;
338 QPixmap disabledPixmap = iconEffect.apply(m_preview->pixmap(),
339 KIconLoader::Desktop,
340 KIconLoader::DisabledState);
341 m_preview->setPixmap(disabledPixmap);
342 }
343
344 void InformationPanelContent::slotPlayingStarted()
345 {
346 m_preview->setVisible(m_phononWidget->mode() != PhononWidget::Video);
347 }
348
349 void InformationPanelContent::slotPlayingStopped()
350 {
351 m_preview->setVisible(true);
352 }
353
354 bool InformationPanelContent::applyPlace(const KUrl& url)
355 {
356 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
357 int count = placesModel->rowCount();
358
359 for (int i = 0; i < count; ++i) {
360 QModelIndex index = placesModel->index(i, 0);
361
362 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
363 setNameLabelText(placesModel->text(index));
364 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
365 return true;
366 }
367 }
368
369 return false;
370 }
371
372 void InformationPanelContent::setNameLabelText(const QString& text)
373 {
374 QTextOption textOption;
375 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
376
377 QTextLayout textLayout(text);
378 textLayout.setFont(m_nameLabel->font());
379 textLayout.setTextOption(textOption);
380
381 QString wrappedText;
382 wrappedText.reserve(text.length());
383
384 // wrap the text to fit into the width of m_nameLabel
385 textLayout.beginLayout();
386 QTextLine line = textLayout.createLine();
387 while (line.isValid()) {
388 line.setLineWidth(m_nameLabel->width());
389 wrappedText += text.mid(line.textStart(), line.textLength());
390
391 line = textLayout.createLine();
392 if (line.isValid()) {
393 wrappedText += QChar::LineSeparator;
394 }
395 }
396 textLayout.endLayout();
397
398 m_nameLabel->setText(wrappedText);
399 }
400
401 #include "informationpanelcontent.moc"