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>
33 #include <qfontmetrics.h>
35 #include <QInputDialog>
37 #include <kbookmarkmanager.h>
39 #include <kstandarddirs.h>
40 #include <kio/previewjob.h>
41 #include <kfileitem.h>
43 #include <kglobalsettings.h>
44 #include <kfilemetainfo.h>
46 #include <kseparator.h>
49 #include <kratingwidget.h>
52 #include "dolphinmainwindow.h"
53 #include "dolphinapplication.h"
54 #include "pixmapviewer.h"
55 #include "dolphinsettings.h"
56 #include "metadatawidget.h"
58 InfoSidebarPage::InfoSidebarPage(DolphinMainWindow
* mainWindow
, QWidget
* parent
) :
59 SidebarPage(mainWindow
, parent
),
60 m_multipleSelection(false),
61 m_pendingPreview(false),
67 const int spacing
= KDialog::spacingHint();
69 m_timer
= new QTimer(this);
70 connect(m_timer
, SIGNAL(timeout()),
71 this, SLOT(slotTimeout()));
73 QVBoxLayout
* layout
= new QVBoxLayout
;
74 layout
->setSpacing(spacing
);
77 m_preview
= new PixmapViewer(this);
78 m_preview
->setMinimumWidth(K3Icon::SizeEnormous
);
79 m_preview
->setFixedHeight(K3Icon::SizeEnormous
);
82 m_name
= new QLabel(this);
83 m_name
->setTextFormat(Qt::RichText
);
84 m_name
->setAlignment(m_name
->alignment() | Qt::AlignHCenter
);
85 QFontMetrics
fontMetrics(m_name
->font());
86 m_name
->setMinimumHeight(fontMetrics
.height() * 3);
87 m_name
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Maximum
);
89 KSeparator
* sep1
= new KSeparator(this);
91 // general information
92 m_infos
= new QLabel(this);
93 m_infos
->setSizePolicy(QSizePolicy::Minimum
, QSizePolicy::Fixed
);
94 m_infos
->setTextFormat(Qt::RichText
);
96 KSeparator
* sep2
= new KSeparator(this);
98 if ( MetaDataWidget::metaDataAvailable() )
99 m_metadataWidget
= new MetaDataWidget( this );
101 m_metadataWidget
= 0;
104 m_actionBox
= new KVBox(this);
105 m_actionBox
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
107 // Add a dummy widget with no restriction regarding a vertical resizing.
108 // This assures that information is always top aligned.
109 QWidget
* dummy
= new QWidget(this);
111 layout
->addItem(new QSpacerItem(spacing
, spacing
, QSizePolicy::Preferred
, QSizePolicy::Fixed
));
112 layout
->addWidget(m_preview
);
113 layout
->addWidget(m_name
);
114 layout
->addWidget(sep1
);
115 layout
->addWidget(m_infos
);
116 layout
->addWidget(sep2
);
117 if ( m_metadataWidget
) {
118 layout
->addWidget( m_metadataWidget
);
119 layout
->addWidget( new KSeparator( this ) );
121 layout
->addWidget(m_actionBox
);
122 layout
->addWidget(dummy
);
124 connect(mainWindow
, SIGNAL(selectionChanged()),
125 this, SLOT(showItemInfo()));
127 connectToActiveView();
130 InfoSidebarPage::~InfoSidebarPage()
134 void InfoSidebarPage::activeViewChanged()
136 connectToActiveView();
139 void InfoSidebarPage::requestDelayedItemInfo(const KUrl
& url
)
143 if (!url
.isEmpty() && !m_multipleSelection
) {
144 m_urlCandidate
= url
;
145 m_timer
->setSingleShot(true);
150 void InfoSidebarPage::requestItemInfo(const KUrl
& url
)
154 if (!url
.isEmpty() && !m_multipleSelection
) {
160 void InfoSidebarPage::showItemInfo()
164 m_multipleSelection
= false;
166 // show the preview...
167 DolphinView
* view
= mainWindow()->activeView();
168 const KFileItemList selectedItems
= view
->selectedItems();
170 if (selectedItems
.count() > 1) {
171 m_multipleSelection
= true;
172 } else if(selectedItems
.count() == 0) {
175 file
= selectedItems
[0]->url();
177 if (m_multipleSelection
) {
178 KIconLoader iconLoader
;
179 QPixmap icon
= iconLoader
.loadIcon("exec",
181 K3Icon::SizeEnormous
);
182 m_preview
->setPixmap(icon
);
183 m_name
->setText(i18n("%1 items selected",selectedItems
.count()));
185 else if (!applyBookmark(file
)) {
186 // try to get a preview pixmap from the item...
190 m_pendingPreview
= true;
191 m_preview
->setPixmap(QPixmap());
193 KIO::PreviewJob
* job
= KIO::filePreview(list
,
195 K3Icon::SizeEnormous
,
200 job
->setIgnoreMaximumSize(true);
202 connect(job
, SIGNAL(gotPreview(const KFileItem
*, const QPixmap
&)),
203 this, SLOT(gotPreview(const KFileItem
*, const QPixmap
&)));
204 connect(job
, SIGNAL(failed(const KFileItem
*)),
205 this, SLOT(slotPreviewFailed(const KFileItem
*)));
208 text
.append(file
.fileName());
210 m_name
->setText(text
);
217 void InfoSidebarPage::slotTimeout()
219 m_shownUrl
= m_urlCandidate
;
223 void InfoSidebarPage::slotPreviewFailed(const KFileItem
* item
)
225 m_pendingPreview
= false;
226 if (!applyBookmark(item
->url())) {
227 m_preview
->setPixmap(item
->pixmap(K3Icon::SizeEnormous
));
231 void InfoSidebarPage::gotPreview(const KFileItem
* /* item */,
232 const QPixmap
& pixmap
)
234 if (m_pendingPreview
) {
235 m_preview
->setPixmap(pixmap
);
236 m_pendingPreview
= false;
240 void InfoSidebarPage::startService(int index
)
242 DolphinView
* view
= mainWindow()->activeView();
243 if (view
->hasSelection()) {
244 KUrl::List selectedUrls
= view
->selectedUrls();
245 // TODO: Use "at()" as soon as executeService is fixed to take a const param (BIC)
246 KDEDesktopMimeType::executeService(selectedUrls
, m_actionsVector
[index
]);
250 KDEDesktopMimeType::executeService(m_shownUrl
, m_actionsVector
[index
]);
254 void InfoSidebarPage::connectToActiveView()
258 DolphinView
* view
= mainWindow()->activeView();
259 connect(view
, SIGNAL(requestItemInfo(const KUrl
&)),
260 this, SLOT(requestDelayedItemInfo(const KUrl
&)));
261 connect(view
, SIGNAL(urlChanged(const KUrl
&)),
262 this, SLOT(requestItemInfo(const KUrl
&)));
264 m_shownUrl
= view
->url();
268 bool InfoSidebarPage::applyBookmark(const KUrl
& url
)
270 KBookmarkGroup root
= DolphinSettings::instance().bookmarkManager()->root();
271 KBookmark bookmark
= root
.first();
272 while (!bookmark
.isNull()) {
273 if (url
.equals(bookmark
.url(), KUrl::CompareWithoutTrailingSlash
)) {
275 text
.append(bookmark
.text());
277 m_name
->setText(text
);
279 KIconLoader iconLoader
;
280 QPixmap icon
= iconLoader
.loadIcon(bookmark
.icon(),
282 K3Icon::SizeEnormous
);
283 m_preview
->setPixmap(icon
);
286 bookmark
= root
.next(bookmark
);
292 void InfoSidebarPage::cancelRequest()
295 m_pendingPreview
= false;
298 void InfoSidebarPage::createMetaInfo()
301 DolphinView
* view
= mainWindow()->activeView();
302 if (!view
->hasSelection()) {
303 KFileItem
fileItem(S_IFDIR
, KFileItem::Unknown
, m_shownUrl
);
306 if (fileItem
.isDir()) {
307 addInfoLine(i18n("Type:"), i18n("Directory"));
309 if ( MetaDataWidget::metaDataAvailable() )
310 m_metadataWidget
->setFile( fileItem
.url() );
312 else if (view
->selectedItems().count() == 1) {
313 KFileItem
* fileItem
= view
->selectedItems()[0];
314 addInfoLine(i18n("Type:"), fileItem
->mimeComment());
316 QString
sizeText(KIO::convertSize(fileItem
->size()));
317 addInfoLine(i18n("Size:"), sizeText
);
318 addInfoLine(i18n("Modified:"), fileItem
->timeString());
320 const KFileMetaInfo
& metaInfo
= fileItem
->metaInfo();
321 if (metaInfo
.isValid()) {
322 QStringList keys
= metaInfo
.supportedKeys();
323 for (QStringList::Iterator it
= keys
.begin(); it
!= keys
.end(); ++it
) {
324 if (showMetaInfo(*it
)) {
325 KFileMetaInfoItem metaInfoItem
= metaInfo
.item(*it
);
326 addInfoLine(*it
, metaInfoItem
.value().toString());
330 if ( MetaDataWidget::metaDataAvailable() )
331 m_metadataWidget
->setFile( fileItem
->url() );
334 if ( MetaDataWidget::metaDataAvailable() )
335 m_metadataWidget
->setFiles( 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 QListIterator
<QPushButton
*> deleteIter(m_actionBox
->findChildren
<QPushButton
*>());
404 while (deleteIter
.hasNext()) {
405 widget
= deleteIter
.next();
407 widget
->deleteLater();
410 m_actionsVector
.clear();
412 int actionsIndex
= 0;
414 // The algorithm for searching the available actions works on a list
415 // of KFileItems. If no selection is given, a temporary KFileItem
416 // by the given Url 'url' is created and added to the list.
417 KFileItem
fileItem(S_IFDIR
, KFileItem::Unknown
, m_shownUrl
);
418 KFileItemList itemList
= mainWindow()->activeView()->selectedItems();
419 if (itemList
.isEmpty()) {
421 itemList
.append(&fileItem
);
424 // 'itemList' contains now all KFileItems, where an item information should be shown.
425 // TODO: the following algorithm is quite equal to DolphinContextMenu::insertActionItems().
426 // It's open yet whether they should be merged or whether they have to work slightly different.
427 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
428 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
430 QStringList entries
= dir
.entryList(QStringList("*.desktop"), QDir::Files
);
432 for (QStringList::ConstIterator entryIt
= entries
.begin(); entryIt
!= entries
.end(); ++entryIt
) {
433 KConfigGroup
cfg(KSharedConfig::openConfig( *dirIt
+ *entryIt
, KConfig::OnlyLocal
), "Desktop Entry" );
434 if ((cfg
.hasKey("Actions") || cfg
.hasKey("X-KDE-GetActionMenu")) && cfg
.hasKey("ServiceTypes")) {
435 const QStringList types
= cfg
.readEntry("ServiceTypes", QStringList(), ',');
436 for (QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
) {
437 // check whether the mime type is equal or whether the
438 // mimegroup (e. g. image/*) is supported
441 if ((*it
) == "all/allfiles") {
442 // The service type is valid for all files, but not for directories.
443 // Check whether the selected items only consist of files...
444 QListIterator
<KFileItem
*> mimeIt(itemList
);
446 while (insert
&& mimeIt
.hasNext()) {
447 KFileItem
* item
= mimeIt
.next();
448 insert
= !item
->isDir();
453 // Check whether the MIME types of all selected files match
454 // to the mimetype of the service action. As soon as one MIME
455 // type does not match, no service menu is shown at all.
456 QListIterator
<KFileItem
*> mimeIt(itemList
);
458 while (insert
&& mimeIt
.hasNext()) {
459 KFileItem
* item
= mimeIt
.next();
460 const QString
mimeType(item
->mimetype());
461 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
463 insert
= (*it
== mimeType
) ||
464 ((*it
).right(1) == "*") &&
465 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
470 const QString submenuName
= cfg
.readEntry( "X-KDE-Submenu" );
472 if (!submenuName
.isEmpty()) {
473 // create a sub menu containing all actions
475 connect(popup
, SIGNAL(activated(int)),
476 this, SLOT(startService(int)));
478 QPushButton
* button
= new QPushButton(submenuName
, m_actionBox
);
479 button
->setFlat(true);
480 button
->setMenu(popup
);
484 QList
<KDEDesktopMimeType::Service
> userServices
=
485 KDEDesktopMimeType::userDefinedServices(*dirIt
+ *entryIt
, true);
487 // iterate through all actions and add them to a widget
488 QList
<KDEDesktopMimeType::Service
>::Iterator serviceIt
;
489 for (serviceIt
= userServices
.begin(); serviceIt
!= userServices
.end(); ++serviceIt
) {
490 KDEDesktopMimeType::Service service
= (*serviceIt
);
492 ServiceButton
* button
= new ServiceButton(KIcon(service
.m_strIcon
),
496 connect(button
, SIGNAL(requestServiceStart(int)),
497 this, SLOT(startService(int)));
501 popup
->insertItem(KIcon(service
.m_strIcon
), service
.m_strName
, actionsIndex
);
504 m_actionsVector
.append(service
);
516 ServiceButton::ServiceButton(const QIcon
& icon
,
520 QPushButton(icon
, text
, parent
),
524 setEraseColor(palette().brush(QPalette::Background
).color());
525 setFocusPolicy(Qt::NoFocus
);
526 connect(this, SIGNAL(released()),
527 this, SLOT(slotReleased()));
530 ServiceButton::~ServiceButton()
534 void ServiceButton::paintEvent(QPaintEvent
* event
)
537 QPainter
painter(this);
538 const int buttonWidth
= width();
539 const int buttonHeight
= height();
541 QColor backgroundColor
;
542 QColor foregroundColor
;
544 backgroundColor
= KGlobalSettings::highlightColor();
545 foregroundColor
= KGlobalSettings::highlightedTextColor();
548 backgroundColor
= palette().brush(QPalette::Background
).color();
549 foregroundColor
= KGlobalSettings::buttonTextColor();
552 // draw button background
553 painter
.setPen(Qt::NoPen
);
554 painter
.setBrush(backgroundColor
);
555 painter
.drawRect(0, 0, buttonWidth
, buttonHeight
);
557 const int spacing
= KDialog::spacingHint();
561 const int y
= (buttonHeight
- K3Icon::SizeSmall
) / 2;
562 const QIcon
&set
= icon();
564 painter
.drawPixmap(x
, y
, set
.pixmap(QIcon::Small
, QIcon::Normal
));
566 x
+= K3Icon::SizeSmall
+ spacing
;
569 painter
.setPen(foregroundColor
);
571 const int textWidth
= buttonWidth
- x
;
572 QFontMetrics
fontMetrics(font());
573 const bool clipped
= fontMetrics
.width(text()) >= textWidth
;
574 //const int align = clipped ? Qt::AlignVCenter : Qt::AlignCenter;
575 painter
.drawText(QRect(x
, 0, textWidth
, buttonHeight
), Qt::AlignVCenter
, text());
578 // Blend the right area of the text with the background, as the
580 // TODO #1: use alpha blending in Qt4 instead of drawing the text that often
581 // TODO #2: same code as in UrlNavigatorButton::drawButton() -> provide helper class?
582 const int blendSteps
= 16;
584 QColor
blendColor(backgroundColor
);
585 const int redInc
= (foregroundColor
.red() - backgroundColor
.red()) / blendSteps
;
586 const int greenInc
= (foregroundColor
.green() - backgroundColor
.green()) / blendSteps
;
587 const int blueInc
= (foregroundColor
.blue() - backgroundColor
.blue()) / blendSteps
;
588 for (int i
= 0; i
< blendSteps
; ++i
) {
589 painter
.setClipRect(QRect(x
+ textWidth
- i
, 0, 1, buttonHeight
));
590 painter
.setPen(blendColor
);
591 painter
.drawText(QRect(x
, 0, textWidth
, buttonHeight
), Qt::AlignVCenter
, text());
593 blendColor
.setRgb(blendColor
.red() + redInc
,
594 blendColor
.green() + greenInc
,
595 blendColor
.blue() + blueInc
);
600 void ServiceButton::enterEvent(QEvent
* event
)
602 QPushButton::enterEvent(event
);
607 void ServiceButton::leaveEvent(QEvent
* event
)
609 QPushButton::leaveEvent(event
);
614 void ServiceButton::slotReleased()
616 emit
requestServiceStart(m_index
);
619 #include "infosidebarpage.moc"