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