]> cloud.milkyroute.net Git - dolphin.git/blob - src/infosidebarpage.cpp
adapt Dolphin to kdelibs coding style (http://techbase.kde.org/Policies/Kdelibs_Codin...
[dolphin.git] / src / infosidebarpage.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 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 <config-kmetadata.h>
21
22 #include "infosidebarpage.h"
23
24 #include <QLayout>
25 #include <QPixmap>
26 #include <QLabel>
27 #include <QTimer>
28 #include <QPushButton>
29 #include <QMenu>
30 #include <QPainter>
31 #include <QFontMetrics>
32 #include <QEvent>
33 #include <QInputDialog>
34 #include <QDir>
35
36 #include <kfileplacesmodel.h>
37 #include <klocale.h>
38 #include <kstandarddirs.h>
39 #include <kio/previewjob.h>
40 #include <kfileitem.h>
41 #include <kdialog.h>
42 #include <kglobalsettings.h>
43 #include <kfilemetainfo.h>
44 #include <kvbox.h>
45 #include <kseparator.h>
46 #include <kiconloader.h>
47
48 #ifdef HAVE_KMETADATA
49 #include <kratingwidget.h>
50 #endif
51
52 #include "pixmapviewer.h"
53 #include "dolphinsettings.h"
54 #include "metadatawidget.h"
55
56 InfoSidebarPage::InfoSidebarPage(QWidget* parent) :
57 SidebarPage(parent),
58 m_multipleSelection(false), //TODO:check if I'm needed
59 m_pendingPreview(false),
60 m_timer(0),
61 m_currentSelection(KFileItemList()),
62 m_preview(0),
63 m_name(0),
64 m_infos(0)
65 {
66 const int spacing = KDialog::spacingHint();
67
68 m_timer = new QTimer(this);
69 connect(m_timer, SIGNAL(timeout()),
70 this, SLOT(slotTimeout()));
71
72 QVBoxLayout* layout = new QVBoxLayout;
73 layout->setSpacing(spacing);
74
75 // preview
76 m_preview = new PixmapViewer(this);
77 m_preview->setMinimumWidth(K3Icon::SizeEnormous);
78 m_preview->setFixedHeight(K3Icon::SizeEnormous);
79
80 // name
81 m_name = new QLabel(this);
82 m_name->setTextFormat(Qt::RichText);
83 m_name->setAlignment(m_name->alignment() | Qt::AlignHCenter);
84 QFontMetrics fontMetrics(m_name->font());
85 m_name->setMinimumHeight(fontMetrics.height() * 3);
86 m_name->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum);
87
88 KSeparator* sep1 = new KSeparator(this);
89
90 // general information
91 m_infos = new QLabel(this);
92 m_infos->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
93 m_infos->setTextFormat(Qt::RichText);
94
95 KSeparator* sep2 = new KSeparator(this);
96
97 if (MetaDataWidget::metaDataAvailable())
98 m_metadataWidget = new MetaDataWidget(this);
99 else
100 m_metadataWidget = 0;
101
102 // actions
103 m_actionBox = new KVBox(this);
104 m_actionBox->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
105
106 layout->addItem(new QSpacerItem(spacing, spacing, QSizePolicy::Preferred, QSizePolicy::Fixed));
107 layout->addWidget(m_preview);
108 layout->addWidget(m_name);
109 layout->addWidget(sep1);
110 layout->addWidget(m_infos);
111 layout->addWidget(sep2);
112 if (m_metadataWidget) {
113 layout->addWidget(m_metadataWidget);
114 layout->addWidget(new KSeparator(this));
115 }
116 layout->addWidget(m_actionBox);
117 // ensure that widgets in the information side bar are aligned towards the top
118 layout->addStretch(1);
119 setLayout(layout);
120 }
121
122 InfoSidebarPage::~InfoSidebarPage()
123 {}
124
125 void InfoSidebarPage::setUrl(const KUrl& url)
126 {
127 if (!m_shownUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
128 cancelRequest();
129 m_shownUrl = url;
130 showItemInfo();
131 }
132 }
133
134 void InfoSidebarPage::setSelection(const KFileItemList& selection)
135 {
136 cancelRequest();
137 m_currentSelection = selection;
138 m_multipleSelection = (m_currentSelection.size() > 1);
139 showItemInfo();
140 }
141
142 void InfoSidebarPage::requestDelayedItemInfo(const KUrl& url)
143 {
144 cancelRequest();
145
146 if (!url.isEmpty() && !m_multipleSelection) {
147 m_urlCandidate = url;
148 m_timer->setSingleShot(true);
149 m_timer->start(300);
150 }
151 }
152
153 void InfoSidebarPage::showItemInfo()
154 {
155 cancelRequest();
156
157 KFileItemList selectedItems = m_currentSelection;
158 KUrl file;
159 if (selectedItems.count() == 0) {
160 file = m_shownUrl;
161 } else {
162 file = selectedItems[0]->url();
163 }
164 if (m_multipleSelection) {
165 KIconLoader iconLoader;
166 QPixmap icon = iconLoader.loadIcon("exec",
167 K3Icon::NoGroup,
168 K3Icon::SizeEnormous);
169 m_preview->setPixmap(icon);
170 m_name->setText(i18n("%1 items selected", selectedItems.count()));
171 } else if (!applyBookmark(file)) {
172 // try to get a preview pixmap from the item...
173 KUrl::List list;
174 list.append(file);
175
176 m_pendingPreview = true;
177 m_preview->setPixmap(QPixmap());
178
179 KIO::PreviewJob* job = KIO::filePreview(list,
180 m_preview->width(),
181 K3Icon::SizeEnormous,
182 0,
183 0,
184 true,
185 false);
186 job->setIgnoreMaximumSize(true);
187
188 connect(job, SIGNAL(gotPreview(const KFileItem*, const QPixmap&)),
189 this, SLOT(gotPreview(const KFileItem*, const QPixmap&)));
190 connect(job, SIGNAL(failed(const KFileItem*)),
191 this, SLOT(slotPreviewFailed(const KFileItem*)));
192
193 QString text("<b>");
194 text.append(file.fileName());
195 text.append("</b>");
196 m_name->setText(text);
197 }
198
199 createMetaInfo();
200 insertActions();
201 }
202
203 void InfoSidebarPage::slotTimeout()
204 {
205 m_shownUrl = m_urlCandidate;
206 showItemInfo();
207 }
208
209 void InfoSidebarPage::slotPreviewFailed(const KFileItem* item)
210 {
211 m_pendingPreview = false;
212 if (!applyBookmark(item->url())) {
213 m_preview->setPixmap(item->pixmap(K3Icon::SizeEnormous));
214 }
215 }
216
217 void InfoSidebarPage::gotPreview(const KFileItem* item,
218 const QPixmap& pixmap)
219 {
220 Q_UNUSED(item);
221 if (m_pendingPreview) {
222 m_preview->setPixmap(pixmap);
223 m_pendingPreview = false;
224 }
225 }
226
227 void InfoSidebarPage::startService(int index)
228 {
229 if (m_currentSelection.count() > 0) {
230 // TODO: Use "at()" as soon as executeService is fixed to take a const param (BIC)
231 KDesktopFileActions::executeService(m_currentSelection.urlList(), m_actionsVector[index]);
232 } else {
233 // TODO: likewise
234 KDesktopFileActions::executeService(m_shownUrl, m_actionsVector[index]);
235 }
236 }
237
238 bool InfoSidebarPage::applyBookmark(const KUrl& url)
239 {
240 KFilePlacesModel *placesModel = DolphinSettings::instance().placesModel();
241 int count = placesModel->rowCount();
242
243 for (int i = 0; i < count; ++i) {
244 QModelIndex index = placesModel->index(i, 0);
245
246 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
247 QString text("<b>");
248 text.append(placesModel->text(index));
249 text.append("</b>");
250 m_name->setText(text);
251
252 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
253 return true;
254 }
255 }
256
257 return false;
258 }
259
260 void InfoSidebarPage::cancelRequest()
261 {
262 m_timer->stop();
263 m_pendingPreview = false;
264 }
265
266 void InfoSidebarPage::createMetaInfo()
267 {
268 beginInfoLines();
269 if (m_currentSelection.size() == 0) {
270 KFileItem fileItem(S_IFDIR, KFileItem::Unknown, m_shownUrl);
271 fileItem.refresh();
272
273 if (fileItem.isDir()) {
274 addInfoLine(i18n("Type:"), i18n("Directory"));
275 }
276 if (MetaDataWidget::metaDataAvailable())
277 m_metadataWidget->setFile(fileItem.url());
278 } else if (m_currentSelection.count() == 1) {
279 KFileItem* fileItem = m_currentSelection.at(0);
280 addInfoLine(i18n("Type:"), fileItem->mimeComment());
281
282 QString sizeText(KIO::convertSize(fileItem->size()));
283 addInfoLine(i18n("Size:"), sizeText);
284 addInfoLine(i18n("Modified:"), fileItem->timeString());
285
286 const KFileMetaInfo& metaInfo = fileItem->metaInfo();
287 if (metaInfo.isValid()) {
288 QStringList keys = metaInfo.supportedKeys();
289 for (QStringList::Iterator it = keys.begin(); it != keys.end(); ++it) {
290 if (showMetaInfo(*it)) {
291 KFileMetaInfoItem metaInfoItem = metaInfo.item(*it);
292 addInfoLine(*it, metaInfoItem.value().toString());
293 }
294 }
295 }
296 if (MetaDataWidget::metaDataAvailable())
297 m_metadataWidget->setFile(fileItem->url());
298 } else {
299 if (MetaDataWidget::metaDataAvailable())
300 m_metadataWidget->setFiles(m_currentSelection.urlList());
301 unsigned long int totSize = 0;
302 foreach(KFileItem* item, m_currentSelection) {
303 totSize += item->size(); //FIXME what to do with directories ? (same with the one-item-selected-code), item->size() does not return the size of the content : not very instinctive for users
304 }
305 addInfoLine(i18n("Total size:"), KIO::convertSize(totSize));
306 }
307 endInfoLines();
308 }
309
310 void InfoSidebarPage::beginInfoLines()
311 {
312 m_infoLines = QString("");
313 }
314
315 void InfoSidebarPage::endInfoLines()
316 {
317 m_infos->setText(m_infoLines);
318 }
319
320 bool InfoSidebarPage::showMetaInfo(const QString& key) const
321 {
322 // sorted list of keys, where it's data should be shown
323 static const char* keys[] = {
324 "Album",
325 "Artist",
326 "Author",
327 "Bitrate",
328 "Date",
329 "Dimensions",
330 "Genre",
331 "Length",
332 "Lines",
333 "Pages",
334 "Title",
335 "Words"
336 };
337
338 // do a binary search for the key...
339 int top = 0;
340 int bottom = sizeof(keys) / sizeof(char*) - 1;
341 while (top < bottom) {
342 const int middle = (top + bottom) / 2;
343 const int result = key.compare(keys[middle]);
344 if (result < 0) {
345 bottom = middle - 1;
346 } else if (result > 0) {
347 top = middle + 1;
348 } else {
349 return true;
350 }
351 }
352
353 return false;
354 }
355
356 void InfoSidebarPage::addInfoLine(const QString& labelText, const QString& infoText)
357 {
358 if (!m_infoLines.isEmpty())
359 m_infoLines += "<br/>";
360 m_infoLines += QString("<b>%1</b> %2").arg(labelText).arg(infoText);
361 }
362
363 void InfoSidebarPage::insertActions()
364 {
365 QListIterator<QPushButton*> deleteIter(m_actionBox->findChildren<QPushButton*>());
366 QWidget* widget = 0;
367 while (deleteIter.hasNext()) {
368 widget = deleteIter.next();
369 widget->close();
370 widget->deleteLater();
371 }
372
373 m_actionsVector.clear();
374
375 int actionsIndex = 0;
376
377 // The algorithm for searching the available actions works on a list
378 // of KFileItems. If no selection is given, a temporary KFileItem
379 // by the given Url 'url' is created and added to the list.
380 KFileItem fileItem(S_IFDIR, KFileItem::Unknown, m_shownUrl);
381 KFileItemList itemList = m_currentSelection;
382 if (itemList.isEmpty()) {
383 fileItem.refresh();
384 itemList.append(&fileItem);
385 }
386
387 // 'itemList' contains now all KFileItems, where an item information should be shown.
388 // TODO: the following algorithm is quite equal to DolphinContextMenu::insertActionItems().
389 // It's open yet whether they should be merged or whether they have to work slightly different.
390 QStringList dirs = KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
391 for (QStringList::ConstIterator dirIt = dirs.begin(); dirIt != dirs.end(); ++dirIt) {
392 QDir dir(*dirIt);
393 QStringList entries = dir.entryList(QStringList("*.desktop"), QDir::Files);
394
395 for (QStringList::ConstIterator entryIt = entries.begin(); entryIt != entries.end(); ++entryIt) {
396 KConfigGroup cfg(KSharedConfig::openConfig(*dirIt + *entryIt, KConfig::OnlyLocal), "Desktop Entry");
397 if ((cfg.hasKey("Actions") || cfg.hasKey("X-KDE-GetActionMenu")) && cfg.hasKey("ServiceTypes")) {
398 const QStringList types = cfg.readEntry("ServiceTypes", QStringList(), ',');
399 for (QStringList::ConstIterator it = types.begin(); it != types.end(); ++it) {
400 // check whether the mime type is equal or whether the
401 // mimegroup (e. g. image/*) is supported
402
403 bool insert = false;
404 if ((*it) == "all/allfiles") {
405 // The service type is valid for all files, but not for directories.
406 // Check whether the selected items only consist of files...
407 QListIterator<KFileItem*> mimeIt(itemList);
408 insert = true;
409 while (insert && mimeIt.hasNext()) {
410 KFileItem* item = mimeIt.next();
411 insert = !item->isDir();
412 }
413 }
414
415 if (!insert) {
416 // Check whether the MIME types of all selected files match
417 // to the mimetype of the service action. As soon as one MIME
418 // type does not match, no service menu is shown at all.
419 QListIterator<KFileItem*> mimeIt(itemList);
420 insert = true;
421 while (insert && mimeIt.hasNext()) {
422 KFileItem* item = mimeIt.next();
423 const QString mimeType(item->mimetype());
424 const QString mimeGroup(mimeType.left(mimeType.indexOf('/')));
425
426 insert = (*it == mimeType) ||
427 ((*it).right(1) == "*") &&
428 ((*it).left((*it).indexOf('/')) == mimeGroup);
429 }
430 }
431
432 if (insert) {
433 const QString submenuName = cfg.readEntry("X-KDE-Submenu");
434 QMenu* popup = 0;
435 if (!submenuName.isEmpty()) {
436 // create a sub menu containing all actions
437 popup = new QMenu();
438 connect(popup, SIGNAL(activated(int)),
439 this, SLOT(startService(int)));
440
441 QPushButton* button = new QPushButton(submenuName, m_actionBox);
442 button->setFlat(true);
443 button->setMenu(popup);
444 button->show();
445 }
446
447 QList<KDesktopFileActions::Service> userServices =
448 KDesktopFileActions::userDefinedServices(*dirIt + *entryIt, true);
449
450 // iterate through all actions and add them to a widget
451 QList<KDesktopFileActions::Service>::Iterator serviceIt;
452 for (serviceIt = userServices.begin(); serviceIt != userServices.end(); ++serviceIt) {
453 KDesktopFileActions::Service service = (*serviceIt);
454 if (popup == 0) {
455 ServiceButton* button = new ServiceButton(KIcon(service.m_strIcon),
456 service.m_strName,
457 m_actionBox,
458 actionsIndex);
459 connect(button, SIGNAL(requestServiceStart(int)),
460 this, SLOT(startService(int)));
461 button->show();
462 } else {
463 popup->insertItem(KIcon(service.m_strIcon), service.m_strName, actionsIndex);
464 }
465
466 m_actionsVector.append(service);
467 ++actionsIndex;
468 }
469 }
470 }
471 }
472 }
473 }
474 }
475
476
477
478 ServiceButton::ServiceButton(const QIcon& icon,
479 const QString& text,
480 QWidget* parent,
481 int index) :
482 QPushButton(icon, text, parent),
483 m_hover(false),
484 m_index(index)
485 {
486 setEraseColor(palette().brush(QPalette::Background).color());
487 setFocusPolicy(Qt::NoFocus);
488 connect(this, SIGNAL(released()),
489 this, SLOT(slotReleased()));
490 }
491
492 ServiceButton::~ServiceButton()
493 {}
494
495 void ServiceButton::paintEvent(QPaintEvent* event)
496 {
497 Q_UNUSED(event);
498 QPainter painter(this);
499 const int buttonWidth = width();
500 const int buttonHeight = height();
501
502 QColor backgroundColor;
503 QColor foregroundColor;
504 if (m_hover) {
505 backgroundColor = KGlobalSettings::highlightColor();
506 foregroundColor = KGlobalSettings::highlightedTextColor();
507 } else {
508 backgroundColor = palette().brush(QPalette::Background).color();
509 foregroundColor = KGlobalSettings::buttonTextColor();
510 }
511
512 // draw button background
513 painter.setPen(Qt::NoPen);
514 painter.setBrush(backgroundColor);
515 painter.drawRect(0, 0, buttonWidth, buttonHeight);
516
517 const int spacing = KDialog::spacingHint();
518
519 // draw icon
520 int x = spacing;
521 const int y = (buttonHeight - K3Icon::SizeSmall) / 2;
522 const QIcon &set = icon();
523 if (!set.isNull()) {
524 painter.drawPixmap(x, y, set.pixmap(QIcon::Small, QIcon::Normal));
525 }
526 x += K3Icon::SizeSmall + spacing;
527
528 // draw text
529 painter.setPen(foregroundColor);
530
531 const int textWidth = buttonWidth - x;
532 QFontMetrics fontMetrics(font());
533 const bool clipped = fontMetrics.width(text()) >= textWidth;
534 //const int align = clipped ? Qt::AlignVCenter : Qt::AlignCenter;
535 painter.drawText(QRect(x, 0, textWidth, buttonHeight), Qt::AlignVCenter, text());
536
537 if (clipped) {
538 // Blend the right area of the text with the background, as the
539 // text is clipped.
540 // TODO #1: use alpha blending in Qt4 instead of drawing the text that often
541 // TODO #2: same code as in UrlNavigatorButton::drawButton() -> provide helper class?
542 const int blendSteps = 16;
543
544 QColor blendColor(backgroundColor);
545 const int redInc = (foregroundColor.red() - backgroundColor.red()) / blendSteps;
546 const int greenInc = (foregroundColor.green() - backgroundColor.green()) / blendSteps;
547 const int blueInc = (foregroundColor.blue() - backgroundColor.blue()) / blendSteps;
548 for (int i = 0; i < blendSteps; ++i) {
549 painter.setClipRect(QRect(x + textWidth - i, 0, 1, buttonHeight));
550 painter.setPen(blendColor);
551 painter.drawText(QRect(x, 0, textWidth, buttonHeight), Qt::AlignVCenter, text());
552
553 blendColor.setRgb(blendColor.red() + redInc,
554 blendColor.green() + greenInc,
555 blendColor.blue() + blueInc);
556 }
557 }
558 }
559
560 void ServiceButton::enterEvent(QEvent* event)
561 {
562 QPushButton::enterEvent(event);
563 m_hover = true;
564 update();
565 }
566
567 void ServiceButton::leaveEvent(QEvent* event)
568 {
569 QPushButton::leaveEvent(event);
570 m_hover = false;
571 update();
572 }
573
574 void ServiceButton::slotReleased()
575 {
576 emit requestServiceStart(m_index);
577 }
578
579 #include "infosidebarpage.moc"