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 m_metadataWidget
->setFiles( view
->selectedItems().urlList() );
335 unsigned long int totSize
= 0;
336 foreach(KFileItem
* item
, view
->selectedItems()) {
337 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
339 addInfoLine(i18n("Total size:"), KIO::convertSize(totSize
));
344 void InfoSidebarPage::beginInfoLines()
346 m_infoLines
= QString("");
349 void InfoSidebarPage::endInfoLines()
351 m_infos
->setText(m_infoLines
);
354 bool InfoSidebarPage::showMetaInfo(const QString
& key
) const
356 // sorted list of keys, where it's data should be shown
357 static const char* keys
[] = {
372 // do a binary search for the key...
374 int bottom
= sizeof(keys
) / sizeof(char*) - 1;
375 while (top
< bottom
) {
376 const int middle
= (top
+ bottom
) / 2;
377 const int result
= key
.compare(keys
[middle
]);
381 else if (result
> 0) {
392 void InfoSidebarPage::addInfoLine(const QString
& labelText
, const QString
& infoText
)
394 if (!m_infoLines
.isEmpty())
395 m_infoLines
+= "<br/>";
396 m_infoLines
+= QString("<b>%1</b> %2").arg(labelText
).arg(infoText
);
399 void InfoSidebarPage::insertActions()
401 QListIterator
<QPushButton
*> deleteIter(m_actionBox
->findChildren
<QPushButton
*>());
403 while (deleteIter
.hasNext()) {
404 widget
= deleteIter
.next();
406 widget
->deleteLater();
409 m_actionsVector
.clear();
411 int actionsIndex
= 0;
413 // The algorithm for searching the available actions works on a list
414 // of KFileItems. If no selection is given, a temporary KFileItem
415 // by the given Url 'url' is created and added to the list.
416 KFileItem
fileItem(S_IFDIR
, KFileItem::Unknown
, m_shownUrl
);
417 KFileItemList itemList
= mainWindow()->activeView()->selectedItems();
418 if (itemList
.isEmpty()) {
420 itemList
.append(&fileItem
);
423 // 'itemList' contains now all KFileItems, where an item information should be shown.
424 // TODO: the following algorithm is quite equal to DolphinContextMenu::insertActionItems().
425 // It's open yet whether they should be merged or whether they have to work slightly different.
426 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
427 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
429 QStringList entries
= dir
.entryList(QStringList("*.desktop"), QDir::Files
);
431 for (QStringList::ConstIterator entryIt
= entries
.begin(); entryIt
!= entries
.end(); ++entryIt
) {
432 KConfigGroup
cfg(KSharedConfig::openConfig( *dirIt
+ *entryIt
, KConfig::OnlyLocal
), "Desktop Entry" );
433 if ((cfg
.hasKey("Actions") || cfg
.hasKey("X-KDE-GetActionMenu")) && cfg
.hasKey("ServiceTypes")) {
434 const QStringList types
= cfg
.readEntry("ServiceTypes", QStringList(), ',');
435 for (QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
) {
436 // check whether the mime type is equal or whether the
437 // mimegroup (e. g. image/*) is supported
440 if ((*it
) == "all/allfiles") {
441 // The service type is valid for all files, but not for directories.
442 // Check whether the selected items only consist of files...
443 QListIterator
<KFileItem
*> mimeIt(itemList
);
445 while (insert
&& mimeIt
.hasNext()) {
446 KFileItem
* item
= mimeIt
.next();
447 insert
= !item
->isDir();
452 // Check whether the MIME types of all selected files match
453 // to the mimetype of the service action. As soon as one MIME
454 // type does not match, no service menu is shown at all.
455 QListIterator
<KFileItem
*> mimeIt(itemList
);
457 while (insert
&& mimeIt
.hasNext()) {
458 KFileItem
* item
= mimeIt
.next();
459 const QString
mimeType(item
->mimetype());
460 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
462 insert
= (*it
== mimeType
) ||
463 ((*it
).right(1) == "*") &&
464 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
469 const QString submenuName
= cfg
.readEntry( "X-KDE-Submenu" );
471 if (!submenuName
.isEmpty()) {
472 // create a sub menu containing all actions
474 connect(popup
, SIGNAL(activated(int)),
475 this, SLOT(startService(int)));
477 QPushButton
* button
= new QPushButton(submenuName
, m_actionBox
);
478 button
->setFlat(true);
479 button
->setMenu(popup
);
483 QList
<KDEDesktopMimeType::Service
> userServices
=
484 KDEDesktopMimeType::userDefinedServices(*dirIt
+ *entryIt
, true);
486 // iterate through all actions and add them to a widget
487 QList
<KDEDesktopMimeType::Service
>::Iterator serviceIt
;
488 for (serviceIt
= userServices
.begin(); serviceIt
!= userServices
.end(); ++serviceIt
) {
489 KDEDesktopMimeType::Service service
= (*serviceIt
);
491 ServiceButton
* button
= new ServiceButton(KIcon(service
.m_strIcon
),
495 connect(button
, SIGNAL(requestServiceStart(int)),
496 this, SLOT(startService(int)));
500 popup
->insertItem(KIcon(service
.m_strIcon
), service
.m_strName
, actionsIndex
);
503 m_actionsVector
.append(service
);
515 ServiceButton::ServiceButton(const QIcon
& icon
,
519 QPushButton(icon
, text
, parent
),
523 setEraseColor(palette().brush(QPalette::Background
).color());
524 setFocusPolicy(Qt::NoFocus
);
525 connect(this, SIGNAL(released()),
526 this, SLOT(slotReleased()));
529 ServiceButton::~ServiceButton()
533 void ServiceButton::paintEvent(QPaintEvent
* event
)
536 QPainter
painter(this);
537 const int buttonWidth
= width();
538 const int buttonHeight
= height();
540 QColor backgroundColor
;
541 QColor foregroundColor
;
543 backgroundColor
= KGlobalSettings::highlightColor();
544 foregroundColor
= KGlobalSettings::highlightedTextColor();
547 backgroundColor
= palette().brush(QPalette::Background
).color();
548 foregroundColor
= KGlobalSettings::buttonTextColor();
551 // draw button background
552 painter
.setPen(Qt::NoPen
);
553 painter
.setBrush(backgroundColor
);
554 painter
.drawRect(0, 0, buttonWidth
, buttonHeight
);
556 const int spacing
= KDialog::spacingHint();
560 const int y
= (buttonHeight
- K3Icon::SizeSmall
) / 2;
561 const QIcon
&set
= icon();
563 painter
.drawPixmap(x
, y
, set
.pixmap(QIcon::Small
, QIcon::Normal
));
565 x
+= K3Icon::SizeSmall
+ spacing
;
568 painter
.setPen(foregroundColor
);
570 const int textWidth
= buttonWidth
- x
;
571 QFontMetrics
fontMetrics(font());
572 const bool clipped
= fontMetrics
.width(text()) >= textWidth
;
573 //const int align = clipped ? Qt::AlignVCenter : Qt::AlignCenter;
574 painter
.drawText(QRect(x
, 0, textWidth
, buttonHeight
), Qt::AlignVCenter
, text());
577 // Blend the right area of the text with the background, as the
579 // TODO #1: use alpha blending in Qt4 instead of drawing the text that often
580 // TODO #2: same code as in UrlNavigatorButton::drawButton() -> provide helper class?
581 const int blendSteps
= 16;
583 QColor
blendColor(backgroundColor
);
584 const int redInc
= (foregroundColor
.red() - backgroundColor
.red()) / blendSteps
;
585 const int greenInc
= (foregroundColor
.green() - backgroundColor
.green()) / blendSteps
;
586 const int blueInc
= (foregroundColor
.blue() - backgroundColor
.blue()) / blendSteps
;
587 for (int i
= 0; i
< blendSteps
; ++i
) {
588 painter
.setClipRect(QRect(x
+ textWidth
- i
, 0, 1, buttonHeight
));
589 painter
.setPen(blendColor
);
590 painter
.drawText(QRect(x
, 0, textWidth
, buttonHeight
), Qt::AlignVCenter
, text());
592 blendColor
.setRgb(blendColor
.red() + redInc
,
593 blendColor
.green() + greenInc
,
594 blendColor
.blue() + blueInc
);
599 void ServiceButton::enterEvent(QEvent
* event
)
601 QPushButton::enterEvent(event
);
606 void ServiceButton::leaveEvent(QEvent
* event
)
608 QPushButton::leaveEvent(event
);
613 void ServiceButton::slotReleased()
615 emit
requestServiceStart(m_index
);
618 #include "infosidebarpage.moc"