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 <config-kmetadata.h>
22 #include "infosidebarpage.h"
29 #include <qpushbutton.h>
31 #include <q3popupmenu.h>
33 #include <qfontmetrics.h>
34 #include <Q3ValueList>
36 #include <QInputDialog>
38 #include <kbookmarkmanager.h>
40 #include <kstandarddirs.h>
41 #include <kio/previewjob.h>
42 #include <kfileitem.h>
44 #include <kglobalsettings.h>
45 #include <kfilemetainfo.h>
47 #include <kseparator.h>
50 #include <kratingwidget.h>
53 #include "dolphinmainwindow.h"
54 #include "dolphinapplication.h"
55 #include "pixmapviewer.h"
56 #include "dolphinsettings.h"
57 #include "metadatawidget.h"
59 InfoSidebarPage::InfoSidebarPage(DolphinMainWindow
* mainWindow
, QWidget
* parent
) :
60 SidebarPage(mainWindow
, parent
),
61 m_multipleSelection(false),
62 m_pendingPreview(false),
68 const int spacing
= KDialog::spacingHint();
70 m_timer
= new QTimer(this);
71 connect(m_timer
, SIGNAL(timeout()),
72 this, SLOT(slotTimeout()));
74 QVBoxLayout
* layout
= new QVBoxLayout
;
75 layout
->setSpacing(spacing
);
78 m_preview
= new PixmapViewer(this);
79 m_preview
->setMinimumWidth(K3Icon::SizeEnormous
);
80 m_preview
->setFixedHeight(K3Icon::SizeEnormous
);
83 m_name
= new QLabel(this);
84 m_name
->setTextFormat(Qt::RichText
);
85 m_name
->setAlignment(m_name
->alignment() | Qt::AlignHCenter
);
86 QFontMetrics
fontMetrics(m_name
->font());
87 m_name
->setMinimumHeight(fontMetrics
.height() * 3);
88 m_name
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Maximum
);
90 KSeparator
* sep1
= new KSeparator(this);
92 // general information
93 m_infos
= new QLabel(this);
94 m_infos
->setSizePolicy(QSizePolicy::Minimum
, QSizePolicy::Fixed
);
95 m_infos
->setTextFormat(Qt::RichText
);
97 KSeparator
* sep2
= new KSeparator(this);
99 if ( MetaDataWidget::metaDataAvailable() )
100 m_metadataWidget
= new MetaDataWidget( this );
102 m_metadataWidget
= 0;
105 m_actionBox
= new KVBox(this);
106 m_actionBox
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
108 // Add a dummy widget with no restriction regarding a vertical resizing.
109 // This assures that information is always top aligned.
110 QWidget
* dummy
= new QWidget(this);
112 layout
->addItem(new QSpacerItem(spacing
, spacing
, QSizePolicy::Preferred
, QSizePolicy::Fixed
));
113 layout
->addWidget(m_preview
);
114 layout
->addWidget(m_name
);
115 layout
->addWidget(sep1
);
116 layout
->addWidget(m_infos
);
117 layout
->addWidget(sep2
);
118 if ( m_metadataWidget
) {
119 layout
->addWidget( m_metadataWidget
);
120 layout
->addWidget( new KSeparator( this ) );
122 layout
->addWidget(m_actionBox
);
123 layout
->addWidget(dummy
);
125 connect(mainWindow
, SIGNAL(selectionChanged()),
126 this, SLOT(showItemInfo()));
128 connectToActiveView();
131 InfoSidebarPage::~InfoSidebarPage()
135 void InfoSidebarPage::activeViewChanged()
137 connectToActiveView();
140 void InfoSidebarPage::requestDelayedItemInfo(const KUrl
& url
)
144 if (!url
.isEmpty() && !m_multipleSelection
) {
145 m_urlCandidate
= url
;
146 m_timer
->setSingleShot(true);
151 void InfoSidebarPage::requestItemInfo(const KUrl
& url
)
155 if (!url
.isEmpty() && !m_multipleSelection
) {
161 void InfoSidebarPage::showItemInfo()
165 m_multipleSelection
= false;
167 // show the preview...
168 DolphinView
* view
= mainWindow()->activeView();
169 const KFileItemList selectedItems
= view
->selectedItems();
171 if (selectedItems
.count() > 1) {
172 m_multipleSelection
= true;
173 } else if(selectedItems
.count() == 0) {
176 file
= selectedItems
[0]->url();
178 if (m_multipleSelection
) {
179 KIconLoader iconLoader
;
180 QPixmap icon
= iconLoader
.loadIcon("exec",
182 K3Icon::SizeEnormous
);
183 m_preview
->setPixmap(icon
);
184 m_name
->setText(i18n("%1 items selected",selectedItems
.count()));
186 else if (!applyBookmark(file
)) {
187 // try to get a preview pixmap from the item...
191 m_pendingPreview
= true;
192 m_preview
->setPixmap(QPixmap());
194 KIO::PreviewJob
* job
= KIO::filePreview(list
,
196 K3Icon::SizeEnormous
,
201 job
->setIgnoreMaximumSize(true);
203 connect(job
, SIGNAL(gotPreview(const KFileItem
*, const QPixmap
&)),
204 this, SLOT(gotPreview(const KFileItem
*, const QPixmap
&)));
205 connect(job
, SIGNAL(failed(const KFileItem
*)),
206 this, SLOT(slotPreviewFailed(const KFileItem
*)));
209 text
.append(file
.fileName());
211 m_name
->setText(text
);
218 void InfoSidebarPage::slotTimeout()
220 m_shownUrl
= m_urlCandidate
;
224 void InfoSidebarPage::slotPreviewFailed(const KFileItem
* item
)
226 m_pendingPreview
= false;
227 if (!applyBookmark(item
->url())) {
228 m_preview
->setPixmap(item
->pixmap(K3Icon::SizeEnormous
));
232 void InfoSidebarPage::gotPreview(const KFileItem
* /* item */,
233 const QPixmap
& pixmap
)
235 if (m_pendingPreview
) {
236 m_preview
->setPixmap(pixmap
);
237 m_pendingPreview
= false;
241 void InfoSidebarPage::startService(int index
)
243 DolphinView
* view
= mainWindow()->activeView();
244 if (view
->hasSelection()) {
245 KUrl::List selectedUrls
= view
->selectedUrls();
246 KDEDesktopMimeType::executeService(selectedUrls
, m_actionsVector
[index
]);
249 KDEDesktopMimeType::executeService(m_shownUrl
, m_actionsVector
[index
]);
253 void InfoSidebarPage::connectToActiveView()
257 DolphinView
* view
= mainWindow()->activeView();
258 connect(view
, SIGNAL(requestItemInfo(const KUrl
&)),
259 this, SLOT(requestDelayedItemInfo(const KUrl
&)));
260 connect(view
, SIGNAL(urlChanged(const KUrl
&)),
261 this, SLOT(requestItemInfo(const KUrl
&)));
263 m_shownUrl
= view
->url();
267 bool InfoSidebarPage::applyBookmark(const KUrl
& url
)
269 KBookmarkGroup root
= DolphinSettings::instance().bookmarkManager()->root();
270 KBookmark bookmark
= root
.first();
271 while (!bookmark
.isNull()) {
272 if (url
.equals(bookmark
.url(), KUrl::CompareWithoutTrailingSlash
)) {
274 text
.append(bookmark
.text());
276 m_name
->setText(text
);
278 KIconLoader iconLoader
;
279 QPixmap icon
= iconLoader
.loadIcon(bookmark
.icon(),
281 K3Icon::SizeEnormous
);
282 m_preview
->setPixmap(icon
);
285 bookmark
= root
.next(bookmark
);
291 void InfoSidebarPage::cancelRequest()
294 m_pendingPreview
= false;
297 void InfoSidebarPage::createMetaInfo()
300 DolphinView
* view
= mainWindow()->activeView();
301 if (!view
->hasSelection()) {
302 KFileItem
fileItem(S_IFDIR
, KFileItem::Unknown
, m_shownUrl
);
305 if (fileItem
.isDir()) {
306 addInfoLine(i18n("Type:"), i18n("Directory"));
308 if ( MetaDataWidget::metaDataAvailable() )
309 m_metadataWidget
->setFile( fileItem
.url() );
311 else if (view
->selectedItems().count() == 1) {
312 KFileItem
* fileItem
= view
->selectedItems()[0];
313 addInfoLine(i18n("Type:"), fileItem
->mimeComment());
315 QString
sizeText(KIO::convertSize(fileItem
->size()));
316 addInfoLine(i18n("Size:"), sizeText
);
317 addInfoLine(i18n("Modified:"), fileItem
->timeString());
319 const KFileMetaInfo
& metaInfo
= fileItem
->metaInfo();
320 if (metaInfo
.isValid()) {
321 QStringList keys
= metaInfo
.supportedKeys();
322 for (QStringList::Iterator it
= keys
.begin(); it
!= keys
.end(); ++it
) {
323 if (showMetaInfo(*it
)) {
324 KFileMetaInfoItem metaInfoItem
= metaInfo
.item(*it
);
325 addInfoLine(*it
, metaInfoItem
.value().toString());
329 if ( MetaDataWidget::metaDataAvailable() )
330 m_metadataWidget
->setFile( fileItem
->url() );
333 m_metadataWidget
->setFiles( view
->selectedItems().urlList() );
334 unsigned long int totSize
= 0;
335 foreach(KFileItem
* item
, view
->selectedItems()) {
336 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
338 addInfoLine(i18n("Total size:"), KIO::convertSize(totSize
));
343 void InfoSidebarPage::beginInfoLines()
345 m_infoLines
= QString("");
348 void InfoSidebarPage::endInfoLines()
350 m_infos
->setText(m_infoLines
);
353 bool InfoSidebarPage::showMetaInfo(const QString
& key
) const
355 // sorted list of keys, where it's data should be shown
356 static const char* keys
[] = {
371 // do a binary search for the key...
373 int bottom
= sizeof(keys
) / sizeof(char*) - 1;
374 while (top
< bottom
) {
375 const int middle
= (top
+ bottom
) / 2;
376 const int result
= key
.compare(keys
[middle
]);
380 else if (result
> 0) {
391 void InfoSidebarPage::addInfoLine(const QString
& labelText
, const QString
& infoText
)
393 if (!m_infoLines
.isEmpty())
394 m_infoLines
+= "<br/>";
395 m_infoLines
+= QString("<b>%1</b> %2").arg(labelText
).arg(infoText
);
398 void InfoSidebarPage::insertActions()
400 // delete all existing action widgets
401 // TODO: just use children() from QObject...
402 Q3PtrListIterator
<QWidget
> deleteIter(m_actionWidgets
);
404 while ((widget
= deleteIter
.current()) != 0) {
406 widget
->deleteLater();
410 m_actionWidgets
.clear();
411 m_actionsVector
.clear();
413 int actionsIndex
= 0;
415 // The algorithm for searching the available actions works on a list
416 // of KFileItems. If no selection is given, a temporary KFileItem
417 // by the given Url 'url' is created and added to the list.
418 KFileItem
fileItem(S_IFDIR
, KFileItem::Unknown
, m_shownUrl
);
419 KFileItemList itemList
= mainWindow()->activeView()->selectedItems();
420 if (itemList
.isEmpty()) {
422 itemList
.append(&fileItem
);
425 // 'itemList' contains now all KFileItems, where an item information should be shown.
426 // TODO: the following algorithm is quite equal to DolphinContextMenu::insertActionItems().
427 // It's open yet whether they should be merged or whether they have to work slightly different.
428 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
429 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
431 QStringList entries
= dir
.entryList(QStringList("*.desktop"), QDir::Files
);
433 for (QStringList::ConstIterator entryIt
= entries
.begin(); entryIt
!= entries
.end(); ++entryIt
) {
434 KConfigGroup
cfg(KSharedConfig::openConfig( *dirIt
+ *entryIt
, KConfig::OnlyLocal
), "Desktop Entry" );
435 if ((cfg
.hasKey("Actions") || cfg
.hasKey("X-KDE-GetActionMenu")) && cfg
.hasKey("ServiceTypes")) {
436 const QStringList types
= cfg
.readEntry("ServiceTypes", QStringList(), ',');
437 for (QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
) {
438 // check whether the mime type is equal or whether the
439 // mimegroup (e. g. image/*) is supported
442 if ((*it
) == "all/allfiles") {
443 // The service type is valid for all files, but not for directories.
444 // Check whether the selected items only consist of files...
445 QListIterator
<KFileItem
*> mimeIt(itemList
);
447 while (insert
&& mimeIt
.hasNext()) {
448 KFileItem
* item
= mimeIt
.next();
449 insert
= !item
->isDir();
454 // Check whether the MIME types of all selected files match
455 // to the mimetype of the service action. As soon as one MIME
456 // type does not match, no service menu is shown at all.
457 QListIterator
<KFileItem
*> mimeIt(itemList
);
459 while (insert
&& mimeIt
.hasNext()) {
460 KFileItem
* item
= mimeIt
.next();
461 const QString
mimeType(item
->mimetype());
462 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
464 insert
= (*it
== mimeType
) ||
465 ((*it
).right(1) == "*") &&
466 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
471 const QString submenuName
= cfg
.readEntry( "X-KDE-Submenu" );
472 Q3PopupMenu
* popup
= 0;
473 if (!submenuName
.isEmpty()) {
474 // create a sub menu containing all actions
475 popup
= new Q3PopupMenu();
476 connect(popup
, SIGNAL(activated(int)),
477 this, SLOT(startService(int)));
479 QPushButton
* button
= new QPushButton(submenuName
, m_actionBox
);
480 button
->setFlat(true);
481 button
->setMenu(popup
);
483 m_actionWidgets
.append(button
);
486 Q3ValueList
<KDEDesktopMimeType::Service
> userServices
=
487 KDEDesktopMimeType::userDefinedServices(*dirIt
+ *entryIt
, true);
489 // iterate through all actions and add them to a widget
490 Q3ValueList
<KDEDesktopMimeType::Service
>::Iterator serviceIt
;
491 for (serviceIt
= userServices
.begin(); serviceIt
!= userServices
.end(); ++serviceIt
) {
492 KDEDesktopMimeType::Service service
= (*serviceIt
);
494 ServiceButton
* button
= new ServiceButton(KIcon(service
.m_strIcon
),
498 connect(button
, SIGNAL(requestServiceStart(int)),
499 this, SLOT(startService(int)));
500 m_actionWidgets
.append(button
);
504 popup
->insertItem(KIcon(service
.m_strIcon
), service
.m_strName
, actionsIndex
);
507 m_actionsVector
.append(service
);
519 ServiceButton::ServiceButton(const QIcon
& icon
,
523 QPushButton(icon
, text
, parent
),
527 setEraseColor(palette().brush(QPalette::Background
).color());
528 setFocusPolicy(Qt::NoFocus
);
529 connect(this, SIGNAL(released()),
530 this, SLOT(slotReleased()));
533 ServiceButton::~ServiceButton()
537 void ServiceButton::paintEvent(QPaintEvent
* event
)
540 QPainter
painter(this);
541 const int buttonWidth
= width();
542 const int buttonHeight
= height();
544 QColor backgroundColor
;
545 QColor foregroundColor
;
547 backgroundColor
= KGlobalSettings::highlightColor();
548 foregroundColor
= KGlobalSettings::highlightedTextColor();
551 backgroundColor
= palette().brush(QPalette::Background
).color();
552 foregroundColor
= KGlobalSettings::buttonTextColor();
555 // draw button background
556 painter
.setPen(Qt::NoPen
);
557 painter
.setBrush(backgroundColor
);
558 painter
.drawRect(0, 0, buttonWidth
, buttonHeight
);
560 const int spacing
= KDialog::spacingHint();
564 const int y
= (buttonHeight
- K3Icon::SizeSmall
) / 2;
565 const QIcon
&set
= icon();
567 painter
.drawPixmap(x
, y
, set
.pixmap(QIcon::Small
, QIcon::Normal
));
569 x
+= K3Icon::SizeSmall
+ spacing
;
572 painter
.setPen(foregroundColor
);
574 const int textWidth
= buttonWidth
- x
;
575 QFontMetrics
fontMetrics(font());
576 const bool clipped
= fontMetrics
.width(text()) >= textWidth
;
577 //const int align = clipped ? Qt::AlignVCenter : Qt::AlignCenter;
578 painter
.drawText(QRect(x
, 0, textWidth
, buttonHeight
), Qt::AlignVCenter
, text());
581 // Blend the right area of the text with the background, as the
583 // TODO #1: use alpha blending in Qt4 instead of drawing the text that often
584 // TODO #2: same code as in UrlNavigatorButton::drawButton() -> provide helper class?
585 const int blendSteps
= 16;
587 QColor
blendColor(backgroundColor
);
588 const int redInc
= (foregroundColor
.red() - backgroundColor
.red()) / blendSteps
;
589 const int greenInc
= (foregroundColor
.green() - backgroundColor
.green()) / blendSteps
;
590 const int blueInc
= (foregroundColor
.blue() - backgroundColor
.blue()) / blendSteps
;
591 for (int i
= 0; i
< blendSteps
; ++i
) {
592 painter
.setClipRect(QRect(x
+ textWidth
- i
, 0, 1, buttonHeight
));
593 painter
.setPen(blendColor
);
594 painter
.drawText(QRect(x
, 0, textWidth
, buttonHeight
), Qt::AlignVCenter
, text());
596 blendColor
.setRgb(blendColor
.red() + redInc
,
597 blendColor
.green() + greenInc
,
598 blendColor
.blue() + blueInc
);
603 void ServiceButton::enterEvent(QEvent
* event
)
605 QPushButton::enterEvent(event
);
610 void ServiceButton::leaveEvent(QEvent
* event
)
612 QPushButton::leaveEvent(event
);
617 void ServiceButton::slotReleased()
619 emit
requestServiceStart(m_index
);
622 #include "infosidebarpage.moc"