]> cloud.milkyroute.net Git - dolphin.git/blob - src/infosidebarpage.cpp
porting
[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 "infosidebarpage.h"
21 #include <assert.h>
22
23 #include <qlayout.h>
24 #include <qpixmap.h>
25 #include <qlabel.h>
26 #include <qtimer.h>
27 #include <qpushbutton.h>
28
29 #include <q3vgroupbox.h>
30 #include <q3popupmenu.h>
31 #include <qpainter.h>
32 #include <qfontmetrics.h>
33 #include <q3grid.h>
34 #include <q3hgroupbox.h>
35 //Added by qt3to4:
36 #include <Q3ValueList>
37 #include <QEvent>
38 #include <Q3VBoxLayout>
39
40 #include <kbookmarkmanager.h>
41 #include <klocale.h>
42 #include <kstandarddirs.h>
43 #include <kio/previewjob.h>
44 #include <kfileitem.h>
45 #include <kdialog.h>
46 #include <kglobalsettings.h>
47 #include <kfilemetainfo.h>
48 #include <kvbox.h>
49
50 #include "dolphinmainwindow.h"
51 #include "pixmapviewer.h"
52 #include "dolphinsettings.h"
53
54 InfoSidebarPage::InfoSidebarPage(DolphinMainWindow* mainWindow, QWidget* parent) :
55 SidebarPage(mainWindow, parent),
56 m_multipleSelection(false),
57 m_pendingPreview(false),
58 m_timer(0),
59 m_preview(0),
60 m_name(0),
61 m_currInfoLineIdx(0),
62 m_infoGrid(0),
63 m_actionBox(0)
64 {
65 const int spacing = KDialog::spacingHint();
66
67 m_timer = new QTimer(this);
68 connect(m_timer, SIGNAL(timeout()),
69 this, SLOT(slotTimeout()));
70
71 Q3VBoxLayout* layout = new Q3VBoxLayout(this);
72 layout->setSpacing(spacing);
73
74 // preview
75 m_preview = new PixmapViewer(this);
76 m_preview->setMinimumWidth(K3Icon::SizeEnormous);
77 m_preview->setFixedHeight(K3Icon::SizeEnormous);
78
79 // name
80 m_name = new QLabel(this);
81 m_name->setTextFormat(Qt::RichText);
82 m_name->setAlignment(m_name->alignment() | Qt::AlignHCenter);
83 QFontMetrics fontMetrics(m_name->font());
84 m_name->setMinimumHeight(fontMetrics.height() * 3);
85 m_name->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum);
86
87 QWidget* sep1 = new Q3HGroupBox(this); // TODO: check whether default widget exist for this?
88 sep1->setFixedHeight(1);
89
90 // general information
91 m_infoGrid = new Q3Grid(2, this);
92 m_infoGrid->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
93
94 QWidget* sep2 = new Q3HGroupBox(this); // TODO: check whether default widget exist for this?
95 sep2->setFixedHeight(1);
96
97 // actions
98 m_actionBox = new KVBox(this);
99 m_actionBox->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
100
101 // Add a dummy widget with no restriction regarding a vertical resizing.
102 // This assures that information is always top aligned.
103 QWidget* dummy = new QWidget(this);
104
105 layout->addItem(new QSpacerItem(spacing, spacing, QSizePolicy::Preferred, QSizePolicy::Fixed));
106 layout->addWidget(m_preview);
107 layout->addWidget(m_name);
108 layout->addWidget(sep1);
109 layout->addWidget(m_infoGrid);
110 layout->addWidget(sep2);
111 layout->addWidget(m_actionBox);
112 layout->addWidget(dummy);
113
114 connect(mainWindow, SIGNAL(selectionChanged()),
115 this, SLOT(showItemInfo()));
116
117 connectToActiveView();
118 }
119
120 InfoSidebarPage::~InfoSidebarPage()
121 {
122 }
123
124 void InfoSidebarPage::activeViewChanged()
125 {
126 connectToActiveView();
127 }
128
129 void InfoSidebarPage::requestDelayedItemInfo(const KUrl& url)
130 {
131 cancelRequest();
132
133 if (!url.isEmpty() && !m_multipleSelection) {
134 m_urlCandidate = url;
135 m_timer->setSingleShot(true);
136 m_timer->start(300);
137 }
138 }
139
140 void InfoSidebarPage::requestItemInfo(const KUrl& url)
141 {
142 cancelRequest();
143
144 if (!url.isEmpty() && !m_multipleSelection) {
145 m_shownUrl = url;
146 showItemInfo();
147 }
148 }
149
150 void InfoSidebarPage::showItemInfo()
151 {
152 cancelRequest();
153
154 m_multipleSelection = false;
155
156 // show the preview...
157 DolphinView* view = mainWindow()->activeView();
158 const KFileItemList selectedItems = view->selectedItems();
159 if (selectedItems.count() > 1) {
160 m_multipleSelection = true;
161 }
162
163 if (m_multipleSelection) {
164 KIconLoader iconLoader;
165 QPixmap icon = iconLoader.loadIcon("exec",
166 K3Icon::NoGroup,
167 K3Icon::SizeEnormous);
168 m_preview->setPixmap(icon);
169 m_name->setText(i18n("%1 items selected",selectedItems.count()));
170 }
171 else if (!applyBookmark()) {
172 // try to get a preview pixmap from the item...
173 KUrl::List list;
174 list.append(m_shownUrl);
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(m_shownUrl.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()) {
213 m_preview->setPixmap(item->pixmap(K3Icon::SizeEnormous));
214 }
215 }
216
217 void InfoSidebarPage::gotPreview(const KFileItem* /* item */,
218 const QPixmap& pixmap)
219 {
220 if (m_pendingPreview) {
221 m_preview->setPixmap(pixmap);
222 m_pendingPreview = false;
223 }
224 }
225
226 void InfoSidebarPage::startService(int index)
227 {
228 DolphinView* view = mainWindow()->activeView();
229 if (view->hasSelection()) {
230 KUrl::List selectedUrls = view->selectedUrls();
231 KDEDesktopMimeType::executeService(selectedUrls, m_actionsVector[index]);
232 }
233 else {
234 KDEDesktopMimeType::executeService(m_shownUrl, m_actionsVector[index]);
235 }
236 }
237
238 void InfoSidebarPage::connectToActiveView()
239 {
240 cancelRequest();
241
242 DolphinView* view = mainWindow()->activeView();
243 connect(view, SIGNAL(requestItemInfo(const KUrl&)),
244 this, SLOT(requestDelayedItemInfo(const KUrl&)));
245 connect(view, SIGNAL(urlChanged(const KUrl&)),
246 this, SLOT(requestItemInfo(const KUrl&)));
247
248 m_shownUrl = view->url();
249 showItemInfo();
250 }
251
252 bool InfoSidebarPage::applyBookmark()
253 {
254 KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
255 KBookmark bookmark = root.first();
256 while (!bookmark.isNull()) {
257 if (m_shownUrl.equals(bookmark.url(), KUrl::CompareWithoutTrailingSlash)) {
258 QString text("<b>");
259 text.append(bookmark.text());
260 text.append("</b>");
261 m_name->setText(text);
262
263 KIconLoader iconLoader;
264 QPixmap icon = iconLoader.loadIcon(bookmark.icon(),
265 K3Icon::NoGroup,
266 K3Icon::SizeEnormous);
267 m_preview->setPixmap(icon);
268 return true;
269 }
270 bookmark = root.next(bookmark);
271 }
272
273 return false;
274 }
275
276 void InfoSidebarPage::cancelRequest()
277 {
278 m_timer->stop();
279 m_pendingPreview = false;
280 }
281
282 void InfoSidebarPage::createMetaInfo()
283 {
284 // To prevent a flickering it's important to reuse available
285 // labels instead of deleting them and recreate them afterwards.
286 // The methods beginInfoLines(), addInfoLine() and endInfoLines()
287 // take care of this.
288 beginInfoLines();
289 DolphinView* view = mainWindow()->activeView();
290 if (!view->hasSelection()) {
291 KFileItem fileItem(S_IFDIR, KFileItem::Unknown, m_shownUrl);
292 fileItem.refresh();
293
294 if (fileItem.isDir()) {
295 addInfoLine(i18n("Type:"), i18n("Directory"));
296 }
297 else {
298 addInfoLine(i18n("Type:"), fileItem.mimeComment());
299
300 QString sizeText(KIO::convertSize(fileItem.size()));
301 addInfoLine(i18n("Size:"), sizeText);
302 addInfoLine(i18n("Modified:"), fileItem.timeString());
303
304 const KFileMetaInfo& metaInfo = fileItem.metaInfo();
305 if (metaInfo.isValid()) {
306 QStringList keys = metaInfo.supportedKeys();
307 for (QStringList::Iterator it = keys.begin(); it != keys.end(); ++it) {
308 if (showMetaInfo(*it)) {
309 KFileMetaInfoItem metaInfoItem = metaInfo.item(*it);
310 addInfoLine(*it, metaInfoItem.string());
311 }
312 }
313 }
314 }
315 }
316 endInfoLines();
317 }
318
319 void InfoSidebarPage::beginInfoLines()
320 {
321 m_currInfoLineIdx = 0;
322 }
323
324 void InfoSidebarPage::endInfoLines()
325 {
326 if (m_currInfoLineIdx <= 0) {
327 return;
328 }
329
330 // remove labels which have not been used
331 if (m_currInfoLineIdx < static_cast<int>(m_infoWidgets.count())) {
332 Q3PtrListIterator<QLabel> deleteIter(m_infoWidgets);
333 deleteIter += m_currInfoLineIdx;
334
335 QWidget* widget = 0;
336 int removeCount = 0;
337 while ((widget = deleteIter.current()) != 0) {
338 widget->close();
339 widget->deleteLater();
340 ++deleteIter;
341 ++removeCount;
342 }
343 for (int i = 0; i < removeCount; ++i) {
344 m_infoWidgets.removeLast();
345 }
346 }
347 }
348
349 bool InfoSidebarPage::showMetaInfo(const QString& key) const
350 {
351 // sorted list of keys, where it's data should be shown
352 static const char* keys[] = {
353 "Album",
354 "Artist",
355 "Author",
356 "Bitrate",
357 "Date",
358 "Dimensions",
359 "Genre",
360 "Length",
361 "Lines",
362 "Pages",
363 "Title",
364 "Words"
365 };
366
367 // do a binary search for the key...
368 int top = 0;
369 int bottom = sizeof(keys) / sizeof(char*) - 1;
370 while (top < bottom) {
371 const int middle = (top + bottom) / 2;
372 const int result = key.compare(keys[middle]);
373 if (result < 0) {
374 bottom = middle - 1;
375 }
376 else if (result > 0) {
377 top = middle + 1;
378 }
379 else {
380 return true;
381 }
382 }
383
384 return false;
385 }
386
387 void InfoSidebarPage::addInfoLine(const QString& labelText, const QString& infoText)
388 {
389 QString labelStr("<b>");
390 labelStr.append(labelText);
391 labelStr.append("</b>&nbsp;");
392
393 const int count = m_infoWidgets.count();
394 if (m_currInfoLineIdx < count - 1) {
395 // reuse available labels
396 m_infoWidgets.at(m_currInfoLineIdx++)->setText(labelStr);
397 m_infoWidgets.at(m_currInfoLineIdx++)->setText(infoText);
398 }
399 else {
400 // no labels are available anymore, hence create 2 new ones
401 QLabel* label = new QLabel(labelStr, m_infoGrid);
402 label->setTextFormat(Qt::RichText);
403 label->setAlignment(Qt::AlignRight |
404 Qt::AlignTop);
405 label->show();
406 m_infoWidgets.append(label);
407
408 QLabel* info = new QLabel(infoText, m_infoGrid);
409 info->setTextFormat(Qt::RichText);
410 info->setAlignment(Qt::AlignTop);
411 info->setWordWrap(true);
412 info->show();
413 m_infoWidgets.append(info);
414
415 m_currInfoLineIdx += 2;
416 }
417 }
418
419 void InfoSidebarPage::insertActions()
420 {
421 // delete all existing action widgets
422 // TODO: just use children() from QObject...
423 Q3PtrListIterator<QWidget> deleteIter(m_actionWidgets);
424 QWidget* widget = 0;
425 while ((widget = deleteIter.current()) != 0) {
426 widget->close();
427 widget->deleteLater();
428 ++deleteIter;
429 }
430
431 m_actionWidgets.clear();
432 m_actionsVector.clear();
433
434 int actionsIndex = 0;
435
436 // The algorithm for searching the available actions works on a list
437 // of KFileItems. If no selection is given, a temporary KFileItem
438 // by the given Url 'url' is created and added to the list.
439 KFileItem fileItem(S_IFDIR, KFileItem::Unknown, m_shownUrl);
440 KFileItemList itemList = mainWindow()->activeView()->selectedItems();
441 if (itemList.isEmpty()) {
442 fileItem.refresh();
443 itemList.append(&fileItem);
444 }
445
446 // 'itemList' contains now all KFileItems, where an item information should be shown.
447 // TODO: the following algorithm is quite equal to DolphinContextMenu::insertActionItems().
448 // It's open yet whether they should be merged or whether they have to work slightly different.
449 QStringList dirs = KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
450 for (QStringList::ConstIterator dirIt = dirs.begin(); dirIt != dirs.end(); ++dirIt) {
451 QDir dir(*dirIt);
452 QStringList entries = dir.entryList(QStringList("*.desktop"), QDir::Files);
453
454 for (QStringList::ConstIterator entryIt = entries.begin(); entryIt != entries.end(); ++entryIt) {
455 KConfigGroup cfg(KSharedConfig::openConfig( *dirIt + *entryIt, KConfig::OnlyLocal ), "Desktop Entry" );
456 if ((cfg.hasKey("Actions") || cfg.hasKey("X-KDE-GetActionMenu")) && cfg.hasKey("ServiceTypes")) {
457 const QStringList types = cfg.readEntry("ServiceTypes", QStringList(), ',');
458 for (QStringList::ConstIterator it = types.begin(); it != types.end(); ++it) {
459 // check whether the mime type is equal or whether the
460 // mimegroup (e. g. image/*) is supported
461
462 bool insert = false;
463 if ((*it) == "all/allfiles") {
464 // The service type is valid for all files, but not for directories.
465 // Check whether the selected items only consist of files...
466 QListIterator<KFileItem*> mimeIt(itemList);
467 insert = true;
468 while (insert && mimeIt.hasNext()) {
469 KFileItem* item = mimeIt.next();
470 insert = !item->isDir();
471 }
472 }
473
474 if (!insert) {
475 // Check whether the MIME types of all selected files match
476 // to the mimetype of the service action. As soon as one MIME
477 // type does not match, no service menu is shown at all.
478 QListIterator<KFileItem*> mimeIt(itemList);
479 insert = true;
480 while (insert && mimeIt.hasNext()) {
481 KFileItem* item = mimeIt.next();
482 const QString mimeType(item->mimetype());
483 const QString mimeGroup(mimeType.left(mimeType.indexOf('/')));
484
485 insert = (*it == mimeType) ||
486 ((*it).right(1) == "*") &&
487 ((*it).left((*it).indexOf('/')) == mimeGroup);
488 }
489 }
490
491 if (insert) {
492 const QString submenuName = cfg.readEntry( "X-KDE-Submenu" );
493 Q3PopupMenu* popup = 0;
494 if (!submenuName.isEmpty()) {
495 // create a sub menu containing all actions
496 popup = new Q3PopupMenu();
497 connect(popup, SIGNAL(activated(int)),
498 this, SLOT(startService(int)));
499
500 QPushButton* button = new QPushButton(submenuName, m_actionBox);
501 button->setFlat(true);
502 button->setMenu(popup);
503 button->show();
504 m_actionWidgets.append(button);
505 }
506
507 Q3ValueList<KDEDesktopMimeType::Service> userServices =
508 KDEDesktopMimeType::userDefinedServices(*dirIt + *entryIt, true);
509
510 // iterate through all actions and add them to a widget
511 Q3ValueList<KDEDesktopMimeType::Service>::Iterator serviceIt;
512 for (serviceIt = userServices.begin(); serviceIt != userServices.end(); ++serviceIt) {
513 KDEDesktopMimeType::Service service = (*serviceIt);
514 if (popup == 0) {
515 ServiceButton* button = new ServiceButton(KIcon(service.m_strIcon),
516 service.m_strName,
517 m_actionBox,
518 actionsIndex);
519 connect(button, SIGNAL(requestServiceStart(int)),
520 this, SLOT(startService(int)));
521 m_actionWidgets.append(button);
522 button->show();
523 }
524 else {
525 popup->insertItem(KIcon(service.m_strIcon), service.m_strName, actionsIndex);
526 }
527
528 m_actionsVector.append(service);
529 ++actionsIndex;
530 }
531 }
532 }
533 }
534 }
535 }
536 }
537
538 ServiceButton::ServiceButton(const QIcon& icon,
539 const QString& text,
540 QWidget* parent,
541 int index) :
542 QPushButton(icon, text, parent),
543 m_hover(false),
544 m_index(index)
545 {
546 setEraseColor(palette().brush(QPalette::Background).color());
547 setFocusPolicy(Qt::NoFocus);
548 connect(this, SIGNAL(released()),
549 this, SLOT(slotReleased()));
550 }
551
552 ServiceButton::~ServiceButton()
553 {
554 }
555
556 void ServiceButton::paintEvent(QPaintEvent* event)
557 {
558 QPainter painter(this);
559 const int buttonWidth = width();
560 const int buttonHeight = height();
561
562 QColor backgroundColor;
563 QColor foregroundColor;
564 if (m_hover) {
565 backgroundColor = KGlobalSettings::highlightColor();
566 foregroundColor = KGlobalSettings::highlightedTextColor();
567 }
568 else {
569 backgroundColor = palette().brush(QPalette::Background).color();
570 foregroundColor = KGlobalSettings::buttonTextColor();
571 }
572
573 // draw button background
574 painter.setPen(Qt::NoPen);
575 painter.setBrush(backgroundColor);
576 painter.drawRect(0, 0, buttonWidth, buttonHeight);
577
578 const int spacing = KDialog::spacingHint();
579
580 // draw icon
581 int x = spacing;
582 const int y = (buttonHeight - K3Icon::SizeSmall) / 2;
583 const QIcon &set = icon();
584 if (!set.isNull()) {
585 painter.drawPixmap(x, y, set.pixmap(QIcon::Small, QIcon::Normal));
586 }
587 x += K3Icon::SizeSmall + spacing;
588
589 // draw text
590 painter.setPen(foregroundColor);
591
592 const int textWidth = buttonWidth - x;
593 QFontMetrics fontMetrics(font());
594 const bool clipped = fontMetrics.width(text()) >= textWidth;
595 //const int align = clipped ? Qt::AlignVCenter : Qt::AlignCenter;
596 painter.drawText(QRect(x, 0, textWidth, buttonHeight), Qt::AlignVCenter, text());
597
598 if (clipped) {
599 // Blend the right area of the text with the background, as the
600 // text is clipped.
601 // TODO #1: use alpha blending in Qt4 instead of drawing the text that often
602 // TODO #2: same code as in UrlNavigatorButton::drawButton() -> provide helper class?
603 const int blendSteps = 16;
604
605 QColor blendColor(backgroundColor);
606 const int redInc = (foregroundColor.red() - backgroundColor.red()) / blendSteps;
607 const int greenInc = (foregroundColor.green() - backgroundColor.green()) / blendSteps;
608 const int blueInc = (foregroundColor.blue() - backgroundColor.blue()) / blendSteps;
609 for (int i = 0; i < blendSteps; ++i) {
610 painter.setClipRect(QRect(x + textWidth - i, 0, 1, buttonHeight));
611 painter.setPen(blendColor);
612 painter.drawText(QRect(x, 0, textWidth, buttonHeight), Qt::AlignVCenter, text());
613
614 blendColor.setRgb(blendColor.red() + redInc,
615 blendColor.green() + greenInc,
616 blendColor.blue() + blueInc);
617 }
618 }
619 }
620
621 void ServiceButton::enterEvent(QEvent* event)
622 {
623 QPushButton::enterEvent(event);
624 m_hover = true;
625 update();
626 }
627
628 void ServiceButton::leaveEvent(QEvent* event)
629 {
630 QPushButton::leaveEvent(event);
631 m_hover = false;
632 update();
633 }
634
635 void ServiceButton::slotReleased()
636 {
637 emit requestServiceStart(m_index);
638 }
639
640 #include "infosidebarpage.moc"