]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanelcontent.cpp
Get rid of obsolete DolphinPlacesModel
[dolphin.git] / src / panels / information / informationpanelcontent.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz19@gmail.com> *
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>
23 #include <KFileItem>
24 #include <kfilemetadatawidget.h>
25 #include <KFilePlacesModel>
26 #include <KGlobalSettings>
27 #include <KIO/JobUiDelegate>
28 #include <KIO/PreviewJob>
29 #include <KIconEffect>
30 #include <KIconLoader>
31 #include <KLocale>
32 #include <KMenu>
33 #include <kseparator.h>
34 #include <KStringHandler>
35
36 #include <Phonon/BackendCapabilities>
37 #include <Phonon/MediaObject>
38 #include <Phonon/SeekSlider>
39
40 #include <QEvent>
41 #include <QLabel>
42 #include <QPixmap>
43 #include <QPointer>
44 #include <QResizeEvent>
45 #include <QScrollArea>
46 #include <QTextDocument>
47 #include <QTextLayout>
48 #include <QTextLine>
49 #include <QTimer>
50 #include <QVBoxLayout>
51
52 #include "dolphin_informationpanelsettings.h"
53 #include "filemetadataconfigurationdialog.h"
54 #include "phononwidget.h"
55 #include "pixmapviewer.h"
56
57 InformationPanelContent::InformationPanelContent(QWidget* parent) :
58 QWidget(parent),
59 m_item(),
60 m_pendingPreview(false),
61 m_outdatedPreviewTimer(0),
62 m_preview(0),
63 m_phononWidget(0),
64 m_nameLabel(0),
65 m_metaDataWidget(0),
66 m_metaDataArea(0),
67 m_placesModel(0)
68 {
69 parent->installEventFilter(this);
70
71 // Initialize timer for disabling an outdated preview with a small
72 // delay. This prevents flickering if the new preview can be generated
73 // within a very small timeframe.
74 m_outdatedPreviewTimer = new QTimer(this);
75 m_outdatedPreviewTimer->setInterval(300);
76 m_outdatedPreviewTimer->setSingleShot(true);
77 connect(m_outdatedPreviewTimer, SIGNAL(timeout()),
78 this, SLOT(markOutdatedPreview()));
79
80 QVBoxLayout* layout = new QVBoxLayout(this);
81 layout->setSpacing(KDialog::spacingHint());
82
83 // preview
84 const int minPreviewWidth = KIconLoader::SizeEnormous + KIconLoader::SizeMedium;
85
86 m_preview = new PixmapViewer(parent);
87 m_preview->setMinimumWidth(minPreviewWidth);
88 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
89
90 m_phononWidget = new PhononWidget(parent);
91 m_phononWidget->hide();
92 m_phononWidget->setMinimumWidth(minPreviewWidth);
93 connect(m_phononWidget, SIGNAL(hasVideoChanged(bool)),
94 this, SLOT(slotHasVideoChanged(bool)));
95
96 // name
97 m_nameLabel = new QLabel(parent);
98 QFont font = m_nameLabel->font();
99 font.setBold(true);
100 m_nameLabel->setFont(font);
101 m_nameLabel->setAlignment(Qt::AlignHCenter);
102 m_nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
103
104 const bool previewsShown = InformationPanelSettings::previewsShown();
105 m_preview->setVisible(previewsShown);
106
107 m_metaDataWidget = new KFileMetaDataWidget(parent);
108 m_metaDataWidget->setFont(KGlobalSettings::smallestReadableFont());
109 m_metaDataWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
110 connect(m_metaDataWidget, SIGNAL(urlActivated(KUrl)), this, SIGNAL(urlActivated(KUrl)));
111
112 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
113 // at the bottom. This prevents that the meta data widget gets vertically stretched
114 // in the case where the height of m_metaDataArea > m_metaDataWidget.
115 QWidget* metaDataWidgetContainer = new QWidget(parent);
116 QVBoxLayout* containerLayout = new QVBoxLayout(metaDataWidgetContainer);
117 containerLayout->setContentsMargins(0, 0, 0, 0);
118 containerLayout->setSpacing(0);
119 containerLayout->addWidget(m_metaDataWidget);
120 containerLayout->addStretch();
121
122 m_metaDataArea = new QScrollArea(parent);
123 m_metaDataArea->setWidget(metaDataWidgetContainer);
124 m_metaDataArea->setWidgetResizable(true);
125 m_metaDataArea->setFrameShape(QFrame::NoFrame);
126
127 QWidget* viewport = m_metaDataArea->viewport();
128 viewport->installEventFilter(this);
129
130 QPalette palette = viewport->palette();
131 palette.setColor(viewport->backgroundRole(), QColor(Qt::transparent));
132 viewport->setPalette(palette);
133
134 layout->addWidget(m_preview);
135 layout->addWidget(m_phononWidget);
136 layout->addWidget(m_nameLabel);
137 layout->addWidget(new KSeparator());
138 layout->addWidget(m_metaDataArea);
139
140 m_placesModel = new KFilePlacesModel(this);
141 }
142
143 InformationPanelContent::~InformationPanelContent()
144 {
145 InformationPanelSettings::self()->writeConfig();
146 }
147
148 void InformationPanelContent::showItem(const KFileItem& item)
149 {
150 m_pendingPreview = false;
151
152 const KUrl itemUrl = item.url();
153 const bool isSearchUrl = itemUrl.protocol().contains("search") && item.nepomukUri().isEmpty();
154 if (!applyPlace(itemUrl)) {
155 setNameLabelText(item.text());
156 if (isSearchUrl) {
157 // in the case of a search-URL the URL is not readable for humans
158 // (at least not useful to show in the Information Panel)
159 KIconLoader iconLoader;
160 QPixmap icon = iconLoader.loadIcon("nepomuk",
161 KIconLoader::NoGroup,
162 KIconLoader::SizeEnormous);
163 m_preview->setPixmap(icon);
164 } else {
165 // try to get a preview pixmap from the item...
166 m_pendingPreview = true;
167
168 // Mark the currently shown preview as outdated. This is done
169 // with a small delay to prevent a flickering when the next preview
170 // can be shown within a short timeframe. This timer is not started
171 // for directories, as directory previews might fail and return the
172 // same icon.
173 if (!item.isDir()) {
174 m_outdatedPreviewTimer->start();
175 }
176
177 KIO::PreviewJob* job = new KIO::PreviewJob(KFileItemList() << item, QSize(m_preview->width(), m_preview->height()));
178 job->setScaleType(KIO::PreviewJob::Unscaled);
179 job->setIgnoreMaximumSize(item.isLocalFile());
180 if (job->ui()) {
181 job->ui()->setWindow(this);
182 }
183
184 connect(job, SIGNAL(gotPreview(KFileItem,QPixmap)),
185 this, SLOT(showPreview(KFileItem,QPixmap)));
186 connect(job, SIGNAL(failed(KFileItem)),
187 this, SLOT(showIcon(KFileItem)));
188 }
189 }
190
191 if (m_metaDataWidget) {
192 m_metaDataWidget->show();
193 m_metaDataWidget->setItems(KFileItemList() << item);
194 }
195
196 if (InformationPanelSettings::previewsShown()) {
197 const QString mimeType = item.mimetype();
198 const bool usePhonon = Phonon::BackendCapabilities::isMimeTypeAvailable(mimeType) &&
199 (mimeType != "image/png"); // TODO: workaround, as Phonon
200 // thinks it supports PNG images
201 if (usePhonon) {
202 m_phononWidget->show();
203 m_phononWidget->setUrl(item.targetUrl());
204 if (m_preview->isVisible()) {
205 m_phononWidget->setVideoSize(m_preview->size());
206 }
207 } else {
208 m_phononWidget->hide();
209 m_preview->setVisible(true);
210 }
211 } else {
212 m_phononWidget->hide();
213 }
214
215 m_item = item;
216 }
217
218 void InformationPanelContent::showItems(const KFileItemList& items)
219 {
220 m_pendingPreview = false;
221
222 KIconLoader iconLoader;
223 QPixmap icon = iconLoader.loadIcon("dialog-information",
224 KIconLoader::NoGroup,
225 KIconLoader::SizeEnormous);
226 m_preview->setPixmap(icon);
227 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", items.count()));
228
229 if (m_metaDataWidget) {
230 m_metaDataWidget->setItems(items);
231 }
232
233 m_phononWidget->hide();
234
235 m_item = KFileItem();
236 }
237
238 bool InformationPanelContent::eventFilter(QObject* obj, QEvent* event)
239 {
240 switch (event->type()) {
241 case QEvent::Resize: {
242 QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(event);
243 if (obj == m_metaDataArea->viewport()) {
244 // The size of the meta text area has changed. Adjust the fixed
245 // width in a way that no horizontal scrollbar needs to be shown.
246 m_metaDataWidget->setFixedWidth(resizeEvent->size().width());
247 } else if (obj == parent()) {
248 adjustWidgetSizes(resizeEvent->size().width());
249 }
250 break;
251 }
252
253 case QEvent::Polish:
254 adjustWidgetSizes(parentWidget()->width());
255 break;
256
257 default:
258 break;
259 }
260
261 return QWidget::eventFilter(obj, event);
262 }
263
264 void InformationPanelContent::configureSettings(const QList<QAction*>& customContextMenuActions)
265 {
266 KMenu popup(this);
267
268 QAction* previewAction = popup.addAction(i18nc("@action:inmenu", "Preview"));
269 previewAction->setIcon(KIcon("view-preview"));
270 previewAction->setCheckable(true);
271 previewAction->setChecked(InformationPanelSettings::previewsShown());
272
273 QAction* configureAction = popup.addAction(i18nc("@action:inmenu", "Configure..."));
274 configureAction->setIcon(KIcon("configure"));
275
276 popup.addSeparator();
277 foreach (QAction* action, customContextMenuActions) {
278 popup.addAction(action);
279 }
280
281 // Open the popup and adjust the settings for the
282 // selected action.
283 QAction* action = popup.exec(QCursor::pos());
284 if (!action) {
285 return;
286 }
287
288 const bool isChecked = action->isChecked();
289 if (action == previewAction) {
290 m_preview->setVisible(isChecked);
291 InformationPanelSettings::setPreviewsShown(isChecked);
292 } else if (action == configureAction) {
293 FileMetaDataConfigurationDialog* dialog = new FileMetaDataConfigurationDialog();
294 dialog->setDescription(i18nc("@label::textbox",
295 "Select which data should be shown in the information panel:"));
296 dialog->setItems(m_metaDataWidget->items());
297 dialog->setAttribute(Qt::WA_DeleteOnClose);
298 dialog->show();
299 dialog->raise();
300 dialog->activateWindow();
301 connect(dialog, SIGNAL(destroyed()), this, SLOT(refreshMetaData()));
302 }
303 }
304
305 void InformationPanelContent::showIcon(const KFileItem& item)
306 {
307 m_outdatedPreviewTimer->stop();
308 m_pendingPreview = false;
309 if (!applyPlace(item.targetUrl())) {
310 KIcon icon(item.iconName(), KIconLoader::global(), item.overlays());
311 m_preview->setPixmap(icon.pixmap(KIconLoader::SizeEnormous));
312 }
313 }
314
315 void InformationPanelContent::showPreview(const KFileItem& item,
316 const QPixmap& pixmap)
317 {
318 m_outdatedPreviewTimer->stop();
319 Q_UNUSED(item);
320 if (m_pendingPreview) {
321 QPixmap p = pixmap;
322 KIconLoader::global()->drawOverlays(item.overlays(), p, KIconLoader::Desktop);
323 m_preview->setPixmap(p);
324 m_pendingPreview = false;
325 }
326 }
327
328 void InformationPanelContent::markOutdatedPreview()
329 {
330 KIconEffect *iconEffect = KIconLoader::global()->iconEffect();
331 QPixmap disabledPixmap = iconEffect->apply(m_preview->pixmap(),
332 KIconLoader::Desktop,
333 KIconLoader::DisabledState);
334 m_preview->setPixmap(disabledPixmap);
335 }
336
337 void InformationPanelContent::slotHasVideoChanged(bool hasVideo)
338 {
339 m_preview->setVisible(!hasVideo);
340 }
341
342 void InformationPanelContent::refreshMetaData()
343 {
344 if (!m_item.isNull() && m_item.nepomukUri().isValid()) {
345 showItem(m_item);
346 }
347 }
348
349 bool InformationPanelContent::applyPlace(const KUrl& url)
350 {
351 const int count = m_placesModel->rowCount();
352 for (int i = 0; i < count; ++i) {
353 QModelIndex index = m_placesModel->index(i, 0);
354 if (url.equals(m_placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
355 setNameLabelText(m_placesModel->text(index));
356 m_preview->setPixmap(m_placesModel->icon(index).pixmap(128, 128));
357 return true;
358 }
359 }
360
361 return false;
362 }
363
364 void InformationPanelContent::setNameLabelText(const QString& text)
365 {
366 QTextOption textOption;
367 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
368
369 const QString processedText = Qt::mightBeRichText(text) ? text : KStringHandler::preProcessWrap(text);
370
371 QTextLayout textLayout(processedText);
372 textLayout.setFont(m_nameLabel->font());
373 textLayout.setTextOption(textOption);
374
375 QString wrappedText;
376 wrappedText.reserve(processedText.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 += processedText.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 void InformationPanelContent::adjustWidgetSizes(int width)
396 {
397 // If the text inside the name label or the info label cannot
398 // get wrapped, then the maximum width of the label is increased
399 // so that the width of the information panel gets increased.
400 // To prevent this, the maximum width is adjusted to
401 // the current width of the panel.
402 const int maxWidth = width - KDialog::spacingHint() * 4;
403 m_nameLabel->setMaximumWidth(maxWidth);
404
405 // The metadata widget also contains a text widget which may return
406 // a large preferred width.
407 if (m_metaDataWidget) {
408 m_metaDataWidget->setMaximumWidth(maxWidth);
409 }
410
411 // try to increase the preview as large as possible
412 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
413
414 if (m_phononWidget->isVisible()) {
415 // assure that the size of the video player is the same as the preview size
416 m_phononWidget->setVideoSize(QSize(maxWidth, maxWidth));
417 }
418 }
419
420 #include "informationpanelcontent.moc"