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