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