1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
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. *
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. *
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 ***************************************************************************/
20 #include "infosidebarpage.h"
27 #include <qpushbutton.h>
29 #include <q3popupmenu.h>
31 #include <qfontmetrics.h>
32 #include <Q3ValueList>
34 #include <QInputDialog>
36 #include <kbookmarkmanager.h>
38 #include <kstandarddirs.h>
39 #include <kio/previewjob.h>
40 #include <kfileitem.h>
42 #include <kglobalsettings.h>
43 #include <kfilemetainfo.h>
45 #include <kseparator.h>
47 #include "dolphinmainwindow.h"
48 #include "dolphinapplication.h"
49 #include "pixmapviewer.h"
50 #include "dolphinsettings.h"
51 #include "metadataloader.h"
53 InfoSidebarPage::InfoSidebarPage(DolphinMainWindow
* mainWindow
, QWidget
* parent
) :
54 SidebarPage(mainWindow
, parent
),
55 m_multipleSelection(false),
56 m_pendingPreview(false),
61 m_metadata(DolphinApplication::app()->metadataLoader())
63 const int spacing
= KDialog::spacingHint();
65 m_timer
= new QTimer(this);
66 connect(m_timer
, SIGNAL(timeout()),
67 this, SLOT(slotTimeout()));
69 QVBoxLayout
* layout
= new QVBoxLayout
;
70 layout
->setSpacing(spacing
);
73 m_preview
= new PixmapViewer(this);
74 m_preview
->setMinimumWidth(K3Icon::SizeEnormous
);
75 m_preview
->setFixedHeight(K3Icon::SizeEnormous
);
78 m_name
= new QLabel(this);
79 m_name
->setTextFormat(Qt::RichText
);
80 m_name
->setAlignment(m_name
->alignment() | Qt::AlignHCenter
);
81 QFontMetrics
fontMetrics(m_name
->font());
82 m_name
->setMinimumHeight(fontMetrics
.height() * 3);
83 m_name
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Maximum
);
85 KSeparator
* sep1
= new KSeparator(this);
87 // general information
88 m_infos
= new QLabel(this);
89 m_infos
->setSizePolicy(QSizePolicy::Minimum
, QSizePolicy::Fixed
);
90 m_infos
->setTextFormat(Qt::RichText
);
92 KSeparator
* sep2
= new KSeparator(this);
96 if (m_metadata
->storageUp()) {
97 m_annotationLabel
= new QLabel(this);
98 m_annotationLabel
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
99 m_annotationLabel
->setTextFormat(Qt::RichText
);
100 m_annotationLabel
->setWordWrap(true);
101 m_annotationButton
= new QPushButton("", this);
102 m_annotationButton
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
103 connect(m_annotationButton
, SIGNAL(released()), this, SLOT(changeAnnotation()));
104 sep3
= new KSeparator(this);
108 m_actionBox
= new KVBox(this);
109 m_actionBox
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
111 // Add a dummy widget with no restriction regarding a vertical resizing.
112 // This assures that information is always top aligned.
113 QWidget
* dummy
= new QWidget(this);
115 layout
->addItem(new QSpacerItem(spacing
, spacing
, QSizePolicy::Preferred
, QSizePolicy::Fixed
));
116 layout
->addWidget(m_preview
);
117 layout
->addWidget(m_name
);
118 layout
->addWidget(sep1
);
119 layout
->addWidget(m_infos
);
120 layout
->addWidget(sep2
);
121 if (m_metadata
->storageUp()) {
122 layout
->addWidget(m_annotationLabel
);
123 layout
->addWidget(m_annotationButton
);
124 layout
->addWidget(sep3
);
126 layout
->addWidget(m_actionBox
);
127 layout
->addWidget(dummy
);
129 connect(mainWindow
, SIGNAL(selectionChanged()),
130 this, SLOT(showItemInfo()));
132 connectToActiveView();
135 InfoSidebarPage::~InfoSidebarPage()
139 void InfoSidebarPage::activeViewChanged()
141 connectToActiveView();
144 void InfoSidebarPage::requestDelayedItemInfo(const KUrl
& url
)
148 if (!url
.isEmpty() && !m_multipleSelection
) {
149 m_urlCandidate
= url
;
150 m_timer
->setSingleShot(true);
155 void InfoSidebarPage::requestItemInfo(const KUrl
& url
)
159 if (!url
.isEmpty() && !m_multipleSelection
) {
165 void InfoSidebarPage::showItemInfo()
169 m_multipleSelection
= false;
171 // show the preview...
172 DolphinView
* view
= mainWindow()->activeView();
173 const KFileItemList selectedItems
= view
->selectedItems();
175 if (selectedItems
.count() > 1) {
176 m_multipleSelection
= true;
177 } else if(selectedItems
.count() == 0) {
180 file
= selectedItems
[0]->url();
182 if (m_multipleSelection
) {
183 KIconLoader iconLoader
;
184 QPixmap icon
= iconLoader
.loadIcon("exec",
186 K3Icon::SizeEnormous
);
187 m_preview
->setPixmap(icon
);
188 m_name
->setText(i18n("%1 items selected",selectedItems
.count()));
190 else if (!applyBookmark(file
)) {
191 // try to get a preview pixmap from the item...
195 m_pendingPreview
= true;
196 m_preview
->setPixmap(QPixmap());
198 KIO::PreviewJob
* job
= KIO::filePreview(list
,
200 K3Icon::SizeEnormous
,
205 job
->setIgnoreMaximumSize(true);
207 connect(job
, SIGNAL(gotPreview(const KFileItem
*, const QPixmap
&)),
208 this, SLOT(gotPreview(const KFileItem
*, const QPixmap
&)));
209 connect(job
, SIGNAL(failed(const KFileItem
*)),
210 this, SLOT(slotPreviewFailed(const KFileItem
*)));
213 text
.append(file
.fileName());
215 m_name
->setText(text
);
222 void InfoSidebarPage::slotTimeout()
224 m_shownUrl
= m_urlCandidate
;
228 void InfoSidebarPage::slotPreviewFailed(const KFileItem
* item
)
230 m_pendingPreview
= false;
231 if (!applyBookmark(item
->url())) {
232 m_preview
->setPixmap(item
->pixmap(K3Icon::SizeEnormous
));
236 void InfoSidebarPage::gotPreview(const KFileItem
* /* item */,
237 const QPixmap
& pixmap
)
239 if (m_pendingPreview
) {
240 m_preview
->setPixmap(pixmap
);
241 m_pendingPreview
= false;
245 void InfoSidebarPage::startService(int index
)
247 DolphinView
* view
= mainWindow()->activeView();
248 if (view
->hasSelection()) {
249 KUrl::List selectedUrls
= view
->selectedUrls();
250 KDEDesktopMimeType::executeService(selectedUrls
, m_actionsVector
[index
]);
253 KDEDesktopMimeType::executeService(m_shownUrl
, m_actionsVector
[index
]);
257 void InfoSidebarPage::connectToActiveView()
261 DolphinView
* view
= mainWindow()->activeView();
262 connect(view
, SIGNAL(requestItemInfo(const KUrl
&)),
263 this, SLOT(requestDelayedItemInfo(const KUrl
&)));
264 connect(view
, SIGNAL(urlChanged(const KUrl
&)),
265 this, SLOT(requestItemInfo(const KUrl
&)));
267 m_shownUrl
= view
->url();
271 bool InfoSidebarPage::applyBookmark(const KUrl
& url
)
273 KBookmarkGroup root
= DolphinSettings::instance().bookmarkManager()->root();
274 KBookmark bookmark
= root
.first();
275 while (!bookmark
.isNull()) {
276 if (url
.equals(bookmark
.url(), KUrl::CompareWithoutTrailingSlash
)) {
278 text
.append(bookmark
.text());
280 m_name
->setText(text
);
282 KIconLoader iconLoader
;
283 QPixmap icon
= iconLoader
.loadIcon(bookmark
.icon(),
285 K3Icon::SizeEnormous
);
286 m_preview
->setPixmap(icon
);
289 bookmark
= root
.next(bookmark
);
295 void InfoSidebarPage::cancelRequest()
298 m_pendingPreview
= false;
301 void InfoSidebarPage::createMetaInfo()
304 DolphinView
* view
= mainWindow()->activeView();
305 if (!view
->hasSelection()) {
306 KFileItem
fileItem(S_IFDIR
, KFileItem::Unknown
, m_shownUrl
);
309 if (fileItem
.isDir()) {
310 addInfoLine(i18n("Type:"), i18n("Directory"));
312 showAnnotation(m_shownUrl
);
314 else if (view
->selectedItems().count() == 1) {
315 KFileItem
* fileItem
= view
->selectedItems()[0];
316 addInfoLine(i18n("Type:"), fileItem
->mimeComment());
318 QString
sizeText(KIO::convertSize(fileItem
->size()));
319 addInfoLine(i18n("Size:"), sizeText
);
320 addInfoLine(i18n("Modified:"), fileItem
->timeString());
322 const KFileMetaInfo
& metaInfo
= fileItem
->metaInfo();
323 if (metaInfo
.isValid()) {
324 QStringList keys
= metaInfo
.supportedKeys();
325 for (QStringList::Iterator it
= keys
.begin(); it
!= keys
.end(); ++it
) {
326 if (showMetaInfo(*it
)) {
327 KFileMetaInfoItem metaInfoItem
= metaInfo
.item(*it
);
328 addInfoLine(*it
, metaInfoItem
.string());
332 showAnnotation(fileItem
->url());
335 showAnnotations(view
->selectedItems().urlList());
336 unsigned long int totSize
= 0;
337 foreach(KFileItem
* item
, view
->selectedItems()) {
338 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
340 addInfoLine(i18n("Total size:"), KIO::convertSize(totSize
));
345 void InfoSidebarPage::beginInfoLines()
347 m_infoLines
= QString("");
350 void InfoSidebarPage::endInfoLines()
352 m_infos
->setText(m_infoLines
);
355 bool InfoSidebarPage::showMetaInfo(const QString
& key
) const
357 // sorted list of keys, where it's data should be shown
358 static const char* keys
[] = {
373 // do a binary search for the key...
375 int bottom
= sizeof(keys
) / sizeof(char*) - 1;
376 while (top
< bottom
) {
377 const int middle
= (top
+ bottom
) / 2;
378 const int result
= key
.compare(keys
[middle
]);
382 else if (result
> 0) {
393 void InfoSidebarPage::addInfoLine(const QString
& labelText
, const QString
& infoText
)
395 if (!m_infoLines
.isEmpty())
396 m_infoLines
+= "<br/>";
397 m_infoLines
+= QString("<b>%1</b> %2").arg(labelText
).arg(infoText
);
400 void InfoSidebarPage::insertActions()
402 // delete all existing action widgets
403 // TODO: just use children() from QObject...
404 Q3PtrListIterator
<QWidget
> deleteIter(m_actionWidgets
);
406 while ((widget
= deleteIter
.current()) != 0) {
408 widget
->deleteLater();
412 m_actionWidgets
.clear();
413 m_actionsVector
.clear();
415 int actionsIndex
= 0;
417 // The algorithm for searching the available actions works on a list
418 // of KFileItems. If no selection is given, a temporary KFileItem
419 // by the given Url 'url' is created and added to the list.
420 KFileItem
fileItem(S_IFDIR
, KFileItem::Unknown
, m_shownUrl
);
421 KFileItemList itemList
= mainWindow()->activeView()->selectedItems();
422 if (itemList
.isEmpty()) {
424 itemList
.append(&fileItem
);
427 // 'itemList' contains now all KFileItems, where an item information should be shown.
428 // TODO: the following algorithm is quite equal to DolphinContextMenu::insertActionItems().
429 // It's open yet whether they should be merged or whether they have to work slightly different.
430 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
431 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
433 QStringList entries
= dir
.entryList(QStringList("*.desktop"), QDir::Files
);
435 for (QStringList::ConstIterator entryIt
= entries
.begin(); entryIt
!= entries
.end(); ++entryIt
) {
436 KConfigGroup
cfg(KSharedConfig::openConfig( *dirIt
+ *entryIt
, KConfig::OnlyLocal
), "Desktop Entry" );
437 if ((cfg
.hasKey("Actions") || cfg
.hasKey("X-KDE-GetActionMenu")) && cfg
.hasKey("ServiceTypes")) {
438 const QStringList types
= cfg
.readEntry("ServiceTypes", QStringList(), ',');
439 for (QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
) {
440 // check whether the mime type is equal or whether the
441 // mimegroup (e. g. image/*) is supported
444 if ((*it
) == "all/allfiles") {
445 // The service type is valid for all files, but not for directories.
446 // Check whether the selected items only consist of files...
447 QListIterator
<KFileItem
*> mimeIt(itemList
);
449 while (insert
&& mimeIt
.hasNext()) {
450 KFileItem
* item
= mimeIt
.next();
451 insert
= !item
->isDir();
456 // Check whether the MIME types of all selected files match
457 // to the mimetype of the service action. As soon as one MIME
458 // type does not match, no service menu is shown at all.
459 QListIterator
<KFileItem
*> mimeIt(itemList
);
461 while (insert
&& mimeIt
.hasNext()) {
462 KFileItem
* item
= mimeIt
.next();
463 const QString
mimeType(item
->mimetype());
464 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
466 insert
= (*it
== mimeType
) ||
467 ((*it
).right(1) == "*") &&
468 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
473 const QString submenuName
= cfg
.readEntry( "X-KDE-Submenu" );
474 Q3PopupMenu
* popup
= 0;
475 if (!submenuName
.isEmpty()) {
476 // create a sub menu containing all actions
477 popup
= new Q3PopupMenu();
478 connect(popup
, SIGNAL(activated(int)),
479 this, SLOT(startService(int)));
481 QPushButton
* button
= new QPushButton(submenuName
, m_actionBox
);
482 button
->setFlat(true);
483 button
->setMenu(popup
);
485 m_actionWidgets
.append(button
);
488 Q3ValueList
<KDEDesktopMimeType::Service
> userServices
=
489 KDEDesktopMimeType::userDefinedServices(*dirIt
+ *entryIt
, true);
491 // iterate through all actions and add them to a widget
492 Q3ValueList
<KDEDesktopMimeType::Service
>::Iterator serviceIt
;
493 for (serviceIt
= userServices
.begin(); serviceIt
!= userServices
.end(); ++serviceIt
) {
494 KDEDesktopMimeType::Service service
= (*serviceIt
);
496 ServiceButton
* button
= new ServiceButton(KIcon(service
.m_strIcon
),
500 connect(button
, SIGNAL(requestServiceStart(int)),
501 this, SLOT(startService(int)));
502 m_actionWidgets
.append(button
);
506 popup
->insertItem(KIcon(service
.m_strIcon
), service
.m_strName
, actionsIndex
);
509 m_actionsVector
.append(service
);
519 void InfoSidebarPage::showAnnotation(const KUrl
& file
)
521 if(m_metadata
->storageUp()) {
522 QString text
= m_metadata
->getAnnotation(file
);
523 if (!text
.isEmpty()) {
524 m_annotationLabel
->show();
525 m_annotationLabel
->setText(QString("<b>%1</b> :<br/>%2").arg(i18n("Annotation")).arg(text
));
526 m_annotationButton
->setText(i18n("Change annotation"));
528 m_annotationLabel
->hide();
529 m_annotationButton
->setText(i18n("Annotate file"));
534 void InfoSidebarPage::showAnnotations(const KUrl::List
& files
)
536 static unsigned int maxShownAnnot
= 3; //The maximum number of show annotations when selecting multiple files
537 if (m_metadata
->storageUp()) {
538 bool hasAnnotation
= false;
539 unsigned int annotateNum
= 0;
540 QString firsts
= QString("<b>%1 :</b><br/>").arg(i18n("Annotations"));
541 foreach (KUrl file
, files
) {
542 QString annotation
= m_metadata
->getAnnotation(file
);
543 if (!annotation
.isEmpty()) {
544 hasAnnotation
= true;
545 if(annotateNum
< maxShownAnnot
) {
546 firsts
+= m_annotationLabel
->fontMetrics().elidedText(QString("<b>%1</b> : %2<br/>").arg(file
.fileName()).arg(annotation
), Qt::ElideRight
, width());//FIXME not really the good method, does not handle resizing ...
552 m_annotationLabel
->show();
553 m_annotationLabel
->setText(firsts
);
554 } else m_annotationLabel
->hide();
555 m_annotationButton
->setText(hasAnnotation
? i18n("Change annotations") : i18n("Annotate files"));
559 void InfoSidebarPage::changeAnnotation()
562 KUrl::List
files(mainWindow()->activeView()->selectedItems().urlList());
564 if (files
.isEmpty()) {
567 else if (files
.count() == 1) {
568 name
= files
[0].url();
569 old
= m_metadata
->getAnnotation(files
[0]);
572 name
= QString("%1 files").arg(files
.count());
575 QString text
= QInputDialog::getText(this, "Annotate", QString("Set annotation for %1").arg(name
), QLineEdit::Normal
, old
, &ok
);//FIXME temporary, must move to a real dialog
577 foreach(KUrl file
, files
) {
578 m_metadata
->setAnnotation(file
, text
);
580 showAnnotation(files
[0]);
584 ServiceButton::ServiceButton(const QIcon
& icon
,
588 QPushButton(icon
, text
, parent
),
592 setEraseColor(palette().brush(QPalette::Background
).color());
593 setFocusPolicy(Qt::NoFocus
);
594 connect(this, SIGNAL(released()),
595 this, SLOT(slotReleased()));
598 ServiceButton::~ServiceButton()
602 void ServiceButton::paintEvent(QPaintEvent
* event
)
605 QPainter
painter(this);
606 const int buttonWidth
= width();
607 const int buttonHeight
= height();
609 QColor backgroundColor
;
610 QColor foregroundColor
;
612 backgroundColor
= KGlobalSettings::highlightColor();
613 foregroundColor
= KGlobalSettings::highlightedTextColor();
616 backgroundColor
= palette().brush(QPalette::Background
).color();
617 foregroundColor
= KGlobalSettings::buttonTextColor();
620 // draw button background
621 painter
.setPen(Qt::NoPen
);
622 painter
.setBrush(backgroundColor
);
623 painter
.drawRect(0, 0, buttonWidth
, buttonHeight
);
625 const int spacing
= KDialog::spacingHint();
629 const int y
= (buttonHeight
- K3Icon::SizeSmall
) / 2;
630 const QIcon
&set
= icon();
632 painter
.drawPixmap(x
, y
, set
.pixmap(QIcon::Small
, QIcon::Normal
));
634 x
+= K3Icon::SizeSmall
+ spacing
;
637 painter
.setPen(foregroundColor
);
639 const int textWidth
= buttonWidth
- x
;
640 QFontMetrics
fontMetrics(font());
641 const bool clipped
= fontMetrics
.width(text()) >= textWidth
;
642 //const int align = clipped ? Qt::AlignVCenter : Qt::AlignCenter;
643 painter
.drawText(QRect(x
, 0, textWidth
, buttonHeight
), Qt::AlignVCenter
, text());
646 // Blend the right area of the text with the background, as the
648 // TODO #1: use alpha blending in Qt4 instead of drawing the text that often
649 // TODO #2: same code as in UrlNavigatorButton::drawButton() -> provide helper class?
650 const int blendSteps
= 16;
652 QColor
blendColor(backgroundColor
);
653 const int redInc
= (foregroundColor
.red() - backgroundColor
.red()) / blendSteps
;
654 const int greenInc
= (foregroundColor
.green() - backgroundColor
.green()) / blendSteps
;
655 const int blueInc
= (foregroundColor
.blue() - backgroundColor
.blue()) / blendSteps
;
656 for (int i
= 0; i
< blendSteps
; ++i
) {
657 painter
.setClipRect(QRect(x
+ textWidth
- i
, 0, 1, buttonHeight
));
658 painter
.setPen(blendColor
);
659 painter
.drawText(QRect(x
, 0, textWidth
, buttonHeight
), Qt::AlignVCenter
, text());
661 blendColor
.setRgb(blendColor
.red() + redInc
,
662 blendColor
.green() + greenInc
,
663 blendColor
.blue() + blueInc
);
668 void ServiceButton::enterEvent(QEvent
* event
)
670 QPushButton::enterEvent(event
);
675 void ServiceButton::leaveEvent(QEvent
* event
)
677 QPushButton::leaveEvent(event
);
682 void ServiceButton::slotReleased()
684 emit
requestServiceStart(m_index
);
687 #include "infosidebarpage.moc"