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