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"
28 #include <QPushButton>
31 #include <QFontMetrics>
33 #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>
46 #include <kiconloader.h>
49 #include <kratingwidget.h>
52 #include "pixmapviewer.h"
53 #include "dolphinsettings.h"
54 #include "metadatawidget.h"
56 InfoSidebarPage::InfoSidebarPage(QWidget
* parent
) :
58 m_multipleSelection(false), //TODO:check if I'm needed
59 m_pendingPreview(false),
61 m_currentSelection(KFileItemList()),
66 const int spacing
= KDialog::spacingHint();
68 m_timer
= new QTimer(this);
69 connect(m_timer
, SIGNAL(timeout()),
70 this, SLOT(slotTimeout()));
72 QVBoxLayout
* layout
= new QVBoxLayout
;
73 layout
->setSpacing(spacing
);
76 m_preview
= new PixmapViewer(this);
77 m_preview
->setMinimumWidth(K3Icon::SizeEnormous
);
78 m_preview
->setFixedHeight(K3Icon::SizeEnormous
);
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
);
88 KSeparator
* sep1
= new KSeparator(this);
90 // general information
91 m_infos
= new QLabel(this);
92 m_infos
->setSizePolicy(QSizePolicy::Minimum
, QSizePolicy::Fixed
);
93 m_infos
->setTextFormat(Qt::RichText
);
95 KSeparator
* sep2
= new KSeparator(this);
97 if ( MetaDataWidget::metaDataAvailable() )
98 m_metadataWidget
= new MetaDataWidget( this );
100 m_metadataWidget
= 0;
103 m_actionBox
= new KVBox(this);
104 m_actionBox
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
106 layout
->addItem(new QSpacerItem(spacing
, spacing
, QSizePolicy::Preferred
, QSizePolicy::Fixed
));
107 layout
->addWidget(m_preview
);
108 layout
->addWidget(m_name
);
109 layout
->addWidget(sep1
);
110 layout
->addWidget(m_infos
);
111 layout
->addWidget(sep2
);
112 if ( m_metadataWidget
) {
113 layout
->addWidget( m_metadataWidget
);
114 layout
->addWidget( new KSeparator( this ) );
116 layout
->addWidget(m_actionBox
);
117 // ensure that widgets in the information side bar are aligned towards the top
118 layout
->addStretch(1);
122 InfoSidebarPage::~InfoSidebarPage()
126 void InfoSidebarPage::setUrl(const KUrl
& url
)
128 if (!m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
135 void InfoSidebarPage::setSelection(const KFileItemList
& selection
)
138 m_currentSelection
= selection
;
139 m_multipleSelection
= (m_currentSelection
.size() > 1);
143 void InfoSidebarPage::requestDelayedItemInfo(const KUrl
& url
)
147 if (!url
.isEmpty() && !m_multipleSelection
) {
148 m_urlCandidate
= url
;
149 m_timer
->setSingleShot(true);
154 void InfoSidebarPage::showItemInfo()
158 KFileItemList selectedItems
= m_currentSelection
;
160 if(selectedItems
.count() == 0) {
163 file
= selectedItems
[0]->url();
165 if (m_multipleSelection
) {
166 KIconLoader iconLoader
;
167 QPixmap icon
= iconLoader
.loadIcon("exec",
169 K3Icon::SizeEnormous
);
170 m_preview
->setPixmap(icon
);
171 m_name
->setText(i18n("%1 items selected",selectedItems
.count()));
173 else if (!applyBookmark(file
)) {
174 // try to get a preview pixmap from the item...
178 m_pendingPreview
= true;
179 m_preview
->setPixmap(QPixmap());
181 KIO::PreviewJob
* job
= KIO::filePreview(list
,
183 K3Icon::SizeEnormous
,
188 job
->setIgnoreMaximumSize(true);
190 connect(job
, SIGNAL(gotPreview(const KFileItem
*, const QPixmap
&)),
191 this, SLOT(gotPreview(const KFileItem
*, const QPixmap
&)));
192 connect(job
, SIGNAL(failed(const KFileItem
*)),
193 this, SLOT(slotPreviewFailed(const KFileItem
*)));
196 text
.append(file
.fileName());
198 m_name
->setText(text
);
205 void InfoSidebarPage::slotTimeout()
207 m_shownUrl
= m_urlCandidate
;
211 void InfoSidebarPage::slotPreviewFailed(const KFileItem
* item
)
213 m_pendingPreview
= false;
214 if (!applyBookmark(item
->url())) {
215 m_preview
->setPixmap(item
->pixmap(K3Icon::SizeEnormous
));
219 void InfoSidebarPage::gotPreview(const KFileItem
* item
,
220 const QPixmap
& pixmap
)
223 if (m_pendingPreview
) {
224 m_preview
->setPixmap(pixmap
);
225 m_pendingPreview
= false;
229 void InfoSidebarPage::startService(int index
)
231 if (m_currentSelection
.count() > 0) {
232 // TODO: Use "at()" as soon as executeService is fixed to take a const param (BIC)
233 KDesktopFileActions::executeService(m_currentSelection
.urlList(), m_actionsVector
[index
]);
237 KDesktopFileActions::executeService(m_shownUrl
, m_actionsVector
[index
]);
241 bool InfoSidebarPage::applyBookmark(const KUrl
& url
)
243 KBookmarkGroup root
= DolphinSettings::instance().bookmarkManager()->root();
244 KBookmark bookmark
= root
.first();
245 while (!bookmark
.isNull()) {
246 if (url
.equals(bookmark
.url(), KUrl::CompareWithoutTrailingSlash
)) {
248 text
.append(bookmark
.text());
250 m_name
->setText(text
);
252 KIconLoader iconLoader
;
253 QPixmap icon
= iconLoader
.loadIcon(bookmark
.icon(),
255 K3Icon::SizeEnormous
);
256 m_preview
->setPixmap(icon
);
259 bookmark
= root
.next(bookmark
);
265 void InfoSidebarPage::cancelRequest()
268 m_pendingPreview
= false;
271 void InfoSidebarPage::createMetaInfo()
274 if(m_currentSelection
.size() == 0) {
275 KFileItem
fileItem(S_IFDIR
, KFileItem::Unknown
, m_shownUrl
);
278 if (fileItem
.isDir()) {
279 addInfoLine(i18n("Type:"), i18n("Directory"));
281 if ( MetaDataWidget::metaDataAvailable() )
282 m_metadataWidget
->setFile( fileItem
.url() );
284 else if (m_currentSelection
.count() == 1) {
285 KFileItem
* fileItem
= m_currentSelection
.at(0);
286 addInfoLine(i18n("Type:"), fileItem
->mimeComment());
288 QString
sizeText(KIO::convertSize(fileItem
->size()));
289 addInfoLine(i18n("Size:"), sizeText
);
290 addInfoLine(i18n("Modified:"), fileItem
->timeString());
292 const KFileMetaInfo
& metaInfo
= fileItem
->metaInfo();
293 if (metaInfo
.isValid()) {
294 QStringList keys
= metaInfo
.supportedKeys();
295 for (QStringList::Iterator it
= keys
.begin(); it
!= keys
.end(); ++it
) {
296 if (showMetaInfo(*it
)) {
297 KFileMetaInfoItem metaInfoItem
= metaInfo
.item(*it
);
298 addInfoLine(*it
, metaInfoItem
.value().toString());
302 if ( MetaDataWidget::metaDataAvailable() )
303 m_metadataWidget
->setFile( fileItem
->url() );
306 if ( MetaDataWidget::metaDataAvailable() )
307 m_metadataWidget
->setFiles( m_currentSelection
.urlList() );
308 unsigned long int totSize
= 0;
309 foreach(KFileItem
* item
, m_currentSelection
) {
310 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
312 addInfoLine(i18n("Total size:"), KIO::convertSize(totSize
));
317 void InfoSidebarPage::beginInfoLines()
319 m_infoLines
= QString("");
322 void InfoSidebarPage::endInfoLines()
324 m_infos
->setText(m_infoLines
);
327 bool InfoSidebarPage::showMetaInfo(const QString
& key
) const
329 // sorted list of keys, where it's data should be shown
330 static const char* keys
[] = {
345 // do a binary search for the key...
347 int bottom
= sizeof(keys
) / sizeof(char*) - 1;
348 while (top
< bottom
) {
349 const int middle
= (top
+ bottom
) / 2;
350 const int result
= key
.compare(keys
[middle
]);
354 else if (result
> 0) {
365 void InfoSidebarPage::addInfoLine(const QString
& labelText
, const QString
& infoText
)
367 if (!m_infoLines
.isEmpty())
368 m_infoLines
+= "<br/>";
369 m_infoLines
+= QString("<b>%1</b> %2").arg(labelText
).arg(infoText
);
372 void InfoSidebarPage::insertActions()
374 QListIterator
<QPushButton
*> deleteIter(m_actionBox
->findChildren
<QPushButton
*>());
376 while (deleteIter
.hasNext()) {
377 widget
= deleteIter
.next();
379 widget
->deleteLater();
382 m_actionsVector
.clear();
384 int actionsIndex
= 0;
386 // The algorithm for searching the available actions works on a list
387 // of KFileItems. If no selection is given, a temporary KFileItem
388 // by the given Url 'url' is created and added to the list.
389 KFileItem
fileItem(S_IFDIR
, KFileItem::Unknown
, m_shownUrl
);
390 KFileItemList itemList
= m_currentSelection
;
391 if (itemList
.isEmpty()) {
393 itemList
.append(&fileItem
);
396 // 'itemList' contains now all KFileItems, where an item information should be shown.
397 // TODO: the following algorithm is quite equal to DolphinContextMenu::insertActionItems().
398 // It's open yet whether they should be merged or whether they have to work slightly different.
399 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
400 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
402 QStringList entries
= dir
.entryList(QStringList("*.desktop"), QDir::Files
);
404 for (QStringList::ConstIterator entryIt
= entries
.begin(); entryIt
!= entries
.end(); ++entryIt
) {
405 KConfigGroup
cfg(KSharedConfig::openConfig( *dirIt
+ *entryIt
, KConfig::OnlyLocal
), "Desktop Entry" );
406 if ((cfg
.hasKey("Actions") || cfg
.hasKey("X-KDE-GetActionMenu")) && cfg
.hasKey("ServiceTypes")) {
407 const QStringList types
= cfg
.readEntry("ServiceTypes", QStringList(), ',');
408 for (QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
) {
409 // check whether the mime type is equal or whether the
410 // mimegroup (e. g. image/*) is supported
413 if ((*it
) == "all/allfiles") {
414 // The service type is valid for all files, but not for directories.
415 // Check whether the selected items only consist of files...
416 QListIterator
<KFileItem
*> mimeIt(itemList
);
418 while (insert
&& mimeIt
.hasNext()) {
419 KFileItem
* item
= mimeIt
.next();
420 insert
= !item
->isDir();
425 // Check whether the MIME types of all selected files match
426 // to the mimetype of the service action. As soon as one MIME
427 // type does not match, no service menu is shown at all.
428 QListIterator
<KFileItem
*> mimeIt(itemList
);
430 while (insert
&& mimeIt
.hasNext()) {
431 KFileItem
* item
= mimeIt
.next();
432 const QString
mimeType(item
->mimetype());
433 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
435 insert
= (*it
== mimeType
) ||
436 ((*it
).right(1) == "*") &&
437 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
442 const QString submenuName
= cfg
.readEntry( "X-KDE-Submenu" );
444 if (!submenuName
.isEmpty()) {
445 // create a sub menu containing all actions
447 connect(popup
, SIGNAL(activated(int)),
448 this, SLOT(startService(int)));
450 QPushButton
* button
= new QPushButton(submenuName
, m_actionBox
);
451 button
->setFlat(true);
452 button
->setMenu(popup
);
456 QList
<KDesktopFileActions::Service
> userServices
=
457 KDesktopFileActions::userDefinedServices(*dirIt
+ *entryIt
, true);
459 // iterate through all actions and add them to a widget
460 QList
<KDesktopFileActions::Service
>::Iterator serviceIt
;
461 for (serviceIt
= userServices
.begin(); serviceIt
!= userServices
.end(); ++serviceIt
) {
462 KDesktopFileActions::Service service
= (*serviceIt
);
464 ServiceButton
* button
= new ServiceButton(KIcon(service
.m_strIcon
),
468 connect(button
, SIGNAL(requestServiceStart(int)),
469 this, SLOT(startService(int)));
473 popup
->insertItem(KIcon(service
.m_strIcon
), service
.m_strName
, actionsIndex
);
476 m_actionsVector
.append(service
);
488 ServiceButton::ServiceButton(const QIcon
& icon
,
492 QPushButton(icon
, text
, parent
),
496 setEraseColor(palette().brush(QPalette::Background
).color());
497 setFocusPolicy(Qt::NoFocus
);
498 connect(this, SIGNAL(released()),
499 this, SLOT(slotReleased()));
502 ServiceButton::~ServiceButton()
506 void ServiceButton::paintEvent(QPaintEvent
* event
)
509 QPainter
painter(this);
510 const int buttonWidth
= width();
511 const int buttonHeight
= height();
513 QColor backgroundColor
;
514 QColor foregroundColor
;
516 backgroundColor
= KGlobalSettings::highlightColor();
517 foregroundColor
= KGlobalSettings::highlightedTextColor();
520 backgroundColor
= palette().brush(QPalette::Background
).color();
521 foregroundColor
= KGlobalSettings::buttonTextColor();
524 // draw button background
525 painter
.setPen(Qt::NoPen
);
526 painter
.setBrush(backgroundColor
);
527 painter
.drawRect(0, 0, buttonWidth
, buttonHeight
);
529 const int spacing
= KDialog::spacingHint();
533 const int y
= (buttonHeight
- K3Icon::SizeSmall
) / 2;
534 const QIcon
&set
= icon();
536 painter
.drawPixmap(x
, y
, set
.pixmap(QIcon::Small
, QIcon::Normal
));
538 x
+= K3Icon::SizeSmall
+ spacing
;
541 painter
.setPen(foregroundColor
);
543 const int textWidth
= buttonWidth
- x
;
544 QFontMetrics
fontMetrics(font());
545 const bool clipped
= fontMetrics
.width(text()) >= textWidth
;
546 //const int align = clipped ? Qt::AlignVCenter : Qt::AlignCenter;
547 painter
.drawText(QRect(x
, 0, textWidth
, buttonHeight
), Qt::AlignVCenter
, text());
550 // Blend the right area of the text with the background, as the
552 // TODO #1: use alpha blending in Qt4 instead of drawing the text that often
553 // TODO #2: same code as in UrlNavigatorButton::drawButton() -> provide helper class?
554 const int blendSteps
= 16;
556 QColor
blendColor(backgroundColor
);
557 const int redInc
= (foregroundColor
.red() - backgroundColor
.red()) / blendSteps
;
558 const int greenInc
= (foregroundColor
.green() - backgroundColor
.green()) / blendSteps
;
559 const int blueInc
= (foregroundColor
.blue() - backgroundColor
.blue()) / blendSteps
;
560 for (int i
= 0; i
< blendSteps
; ++i
) {
561 painter
.setClipRect(QRect(x
+ textWidth
- i
, 0, 1, buttonHeight
));
562 painter
.setPen(blendColor
);
563 painter
.drawText(QRect(x
, 0, textWidth
, buttonHeight
), Qt::AlignVCenter
, text());
565 blendColor
.setRgb(blendColor
.red() + redInc
,
566 blendColor
.green() + greenInc
,
567 blendColor
.blue() + blueInc
);
572 void ServiceButton::enterEvent(QEvent
* event
)
574 QPushButton::enterEvent(event
);
579 void ServiceButton::leaveEvent(QEvent
* event
)
581 QPushButton::leaveEvent(event
);
586 void ServiceButton::slotReleased()
588 emit
requestServiceStart(m_index
);
591 #include "infosidebarpage.moc"