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 <kratingwidget.h>
48 #include <kseparator.h>
50 #include "dolphinmainwindow.h"
51 #include "dolphinapplication.h"
52 #include "pixmapviewer.h"
53 #include "dolphinsettings.h"
54 #include "metadatawidget.h"
56 InfoSidebarPage::InfoSidebarPage(DolphinMainWindow
* mainWindow
, QWidget
* parent
) :
57 SidebarPage(mainWindow
, parent
),
58 m_multipleSelection(false),
59 m_pendingPreview(false),
65 const int spacing
= KDialog::spacingHint();
67 m_timer
= new QTimer(this);
68 connect(m_timer
, SIGNAL(timeout()),
69 this, SLOT(slotTimeout()));
71 QVBoxLayout
* layout
= new QVBoxLayout
;
72 layout
->setSpacing(spacing
);
75 m_preview
= new PixmapViewer(this);
76 m_preview
->setMinimumWidth(K3Icon::SizeEnormous
);
77 m_preview
->setFixedHeight(K3Icon::SizeEnormous
);
80 m_name
= new QLabel(this);
81 m_name
->setTextFormat(Qt::RichText
);
82 m_name
->setAlignment(m_name
->alignment() | Qt::AlignHCenter
);
83 QFontMetrics
fontMetrics(m_name
->font());
84 m_name
->setMinimumHeight(fontMetrics
.height() * 3);
85 m_name
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Maximum
);
87 KSeparator
* sep1
= new KSeparator(this);
89 // general information
90 m_infos
= new QLabel(this);
91 m_infos
->setSizePolicy(QSizePolicy::Minimum
, QSizePolicy::Fixed
);
92 m_infos
->setTextFormat(Qt::RichText
);
94 KSeparator
* sep2
= new KSeparator(this);
96 if ( MetaDataWidget::metaDataAvailable() )
97 m_metadataWidget
= new MetaDataWidget( this );
102 m_actionBox
= new KVBox(this);
103 m_actionBox
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
105 // Add a dummy widget with no restriction regarding a vertical resizing.
106 // This assures that information is always top aligned.
107 QWidget
* dummy
= new QWidget(this);
109 layout
->addItem(new QSpacerItem(spacing
, spacing
, QSizePolicy::Preferred
, QSizePolicy::Fixed
));
110 layout
->addWidget(m_preview
);
111 layout
->addWidget(m_name
);
112 layout
->addWidget(sep1
);
113 layout
->addWidget(m_infos
);
114 layout
->addWidget(sep2
);
115 if ( m_metadataWidget
) {
116 layout
->addWidget( m_metadataWidget
);
117 layout
->addWidget( new KSeparator( this ) );
119 layout
->addWidget(m_actionBox
);
120 layout
->addWidget(dummy
);
122 connect(mainWindow
, SIGNAL(selectionChanged()),
123 this, SLOT(showItemInfo()));
125 connectToActiveView();
128 InfoSidebarPage::~InfoSidebarPage()
132 void InfoSidebarPage::activeViewChanged()
134 connectToActiveView();
137 void InfoSidebarPage::requestDelayedItemInfo(const KUrl
& url
)
141 if (!url
.isEmpty() && !m_multipleSelection
) {
142 m_urlCandidate
= url
;
143 m_timer
->setSingleShot(true);
148 void InfoSidebarPage::requestItemInfo(const KUrl
& url
)
152 if (!url
.isEmpty() && !m_multipleSelection
) {
158 void InfoSidebarPage::showItemInfo()
162 m_multipleSelection
= false;
164 // show the preview...
165 DolphinView
* view
= mainWindow()->activeView();
166 const KFileItemList selectedItems
= view
->selectedItems();
168 if (selectedItems
.count() > 1) {
169 m_multipleSelection
= true;
170 } else if(selectedItems
.count() == 0) {
173 file
= selectedItems
[0]->url();
175 if (m_multipleSelection
) {
176 KIconLoader iconLoader
;
177 QPixmap icon
= iconLoader
.loadIcon("exec",
179 K3Icon::SizeEnormous
);
180 m_preview
->setPixmap(icon
);
181 m_name
->setText(i18n("%1 items selected",selectedItems
.count()));
183 else if (!applyBookmark(file
)) {
184 // try to get a preview pixmap from the item...
188 m_pendingPreview
= true;
189 m_preview
->setPixmap(QPixmap());
191 KIO::PreviewJob
* job
= KIO::filePreview(list
,
193 K3Icon::SizeEnormous
,
198 job
->setIgnoreMaximumSize(true);
200 connect(job
, SIGNAL(gotPreview(const KFileItem
*, const QPixmap
&)),
201 this, SLOT(gotPreview(const KFileItem
*, const QPixmap
&)));
202 connect(job
, SIGNAL(failed(const KFileItem
*)),
203 this, SLOT(slotPreviewFailed(const KFileItem
*)));
206 text
.append(file
.fileName());
208 m_name
->setText(text
);
215 void InfoSidebarPage::slotTimeout()
217 m_shownUrl
= m_urlCandidate
;
221 void InfoSidebarPage::slotPreviewFailed(const KFileItem
* item
)
223 m_pendingPreview
= false;
224 if (!applyBookmark(item
->url())) {
225 m_preview
->setPixmap(item
->pixmap(K3Icon::SizeEnormous
));
229 void InfoSidebarPage::gotPreview(const KFileItem
* /* item */,
230 const QPixmap
& pixmap
)
232 if (m_pendingPreview
) {
233 m_preview
->setPixmap(pixmap
);
234 m_pendingPreview
= false;
238 void InfoSidebarPage::startService(int index
)
240 DolphinView
* view
= mainWindow()->activeView();
241 if (view
->hasSelection()) {
242 KUrl::List selectedUrls
= view
->selectedUrls();
243 KDEDesktopMimeType::executeService(selectedUrls
, m_actionsVector
[index
]);
246 KDEDesktopMimeType::executeService(m_shownUrl
, m_actionsVector
[index
]);
250 void InfoSidebarPage::connectToActiveView()
254 DolphinView
* view
= mainWindow()->activeView();
255 connect(view
, SIGNAL(requestItemInfo(const KUrl
&)),
256 this, SLOT(requestDelayedItemInfo(const KUrl
&)));
257 connect(view
, SIGNAL(urlChanged(const KUrl
&)),
258 this, SLOT(requestItemInfo(const KUrl
&)));
260 m_shownUrl
= view
->url();
264 bool InfoSidebarPage::applyBookmark(const KUrl
& url
)
266 KBookmarkGroup root
= DolphinSettings::instance().bookmarkManager()->root();
267 KBookmark bookmark
= root
.first();
268 while (!bookmark
.isNull()) {
269 if (url
.equals(bookmark
.url(), KUrl::CompareWithoutTrailingSlash
)) {
271 text
.append(bookmark
.text());
273 m_name
->setText(text
);
275 KIconLoader iconLoader
;
276 QPixmap icon
= iconLoader
.loadIcon(bookmark
.icon(),
278 K3Icon::SizeEnormous
);
279 m_preview
->setPixmap(icon
);
282 bookmark
= root
.next(bookmark
);
288 void InfoSidebarPage::cancelRequest()
291 m_pendingPreview
= false;
294 void InfoSidebarPage::createMetaInfo()
297 DolphinView
* view
= mainWindow()->activeView();
298 if (!view
->hasSelection()) {
299 KFileItem
fileItem(S_IFDIR
, KFileItem::Unknown
, m_shownUrl
);
302 if (fileItem
.isDir()) {
303 addInfoLine(i18n("Type:"), i18n("Directory"));
305 if ( MetaDataWidget::metaDataAvailable() )
306 m_metadataWidget
->setFile( fileItem
.url() );
308 else if (view
->selectedItems().count() == 1) {
309 KFileItem
* fileItem
= view
->selectedItems()[0];
310 addInfoLine(i18n("Type:"), fileItem
->mimeComment());
312 QString
sizeText(KIO::convertSize(fileItem
->size()));
313 addInfoLine(i18n("Size:"), sizeText
);
314 addInfoLine(i18n("Modified:"), fileItem
->timeString());
316 const KFileMetaInfo
& metaInfo
= fileItem
->metaInfo();
317 if (metaInfo
.isValid()) {
318 QStringList keys
= metaInfo
.supportedKeys();
319 for (QStringList::Iterator it
= keys
.begin(); it
!= keys
.end(); ++it
) {
320 if (showMetaInfo(*it
)) {
321 KFileMetaInfoItem metaInfoItem
= metaInfo
.item(*it
);
322 addInfoLine(*it
, metaInfoItem
.value().toString());
326 if ( MetaDataWidget::metaDataAvailable() )
327 m_metadataWidget
->setFile( fileItem
->url() );
330 m_metadataWidget
->setFiles( view
->selectedItems().urlList() );
331 unsigned long int totSize
= 0;
332 foreach(KFileItem
* item
, view
->selectedItems()) {
333 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
335 addInfoLine(i18n("Total size:"), KIO::convertSize(totSize
));
340 void InfoSidebarPage::beginInfoLines()
342 m_infoLines
= QString("");
345 void InfoSidebarPage::endInfoLines()
347 m_infos
->setText(m_infoLines
);
350 bool InfoSidebarPage::showMetaInfo(const QString
& key
) const
352 // sorted list of keys, where it's data should be shown
353 static const char* keys
[] = {
368 // do a binary search for the key...
370 int bottom
= sizeof(keys
) / sizeof(char*) - 1;
371 while (top
< bottom
) {
372 const int middle
= (top
+ bottom
) / 2;
373 const int result
= key
.compare(keys
[middle
]);
377 else if (result
> 0) {
388 void InfoSidebarPage::addInfoLine(const QString
& labelText
, const QString
& infoText
)
390 if (!m_infoLines
.isEmpty())
391 m_infoLines
+= "<br/>";
392 m_infoLines
+= QString("<b>%1</b> %2").arg(labelText
).arg(infoText
);
395 void InfoSidebarPage::insertActions()
397 // delete all existing action widgets
398 // TODO: just use children() from QObject...
399 Q3PtrListIterator
<QWidget
> deleteIter(m_actionWidgets
);
401 while ((widget
= deleteIter
.current()) != 0) {
403 widget
->deleteLater();
407 m_actionWidgets
.clear();
408 m_actionsVector
.clear();
410 int actionsIndex
= 0;
412 // The algorithm for searching the available actions works on a list
413 // of KFileItems. If no selection is given, a temporary KFileItem
414 // by the given Url 'url' is created and added to the list.
415 KFileItem
fileItem(S_IFDIR
, KFileItem::Unknown
, m_shownUrl
);
416 KFileItemList itemList
= mainWindow()->activeView()->selectedItems();
417 if (itemList
.isEmpty()) {
419 itemList
.append(&fileItem
);
422 // 'itemList' contains now all KFileItems, where an item information should be shown.
423 // TODO: the following algorithm is quite equal to DolphinContextMenu::insertActionItems().
424 // It's open yet whether they should be merged or whether they have to work slightly different.
425 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
426 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
428 QStringList entries
= dir
.entryList(QStringList("*.desktop"), QDir::Files
);
430 for (QStringList::ConstIterator entryIt
= entries
.begin(); entryIt
!= entries
.end(); ++entryIt
) {
431 KConfigGroup
cfg(KSharedConfig::openConfig( *dirIt
+ *entryIt
, KConfig::OnlyLocal
), "Desktop Entry" );
432 if ((cfg
.hasKey("Actions") || cfg
.hasKey("X-KDE-GetActionMenu")) && cfg
.hasKey("ServiceTypes")) {
433 const QStringList types
= cfg
.readEntry("ServiceTypes", QStringList(), ',');
434 for (QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
) {
435 // check whether the mime type is equal or whether the
436 // mimegroup (e. g. image/*) is supported
439 if ((*it
) == "all/allfiles") {
440 // The service type is valid for all files, but not for directories.
441 // Check whether the selected items only consist of files...
442 QListIterator
<KFileItem
*> mimeIt(itemList
);
444 while (insert
&& mimeIt
.hasNext()) {
445 KFileItem
* item
= mimeIt
.next();
446 insert
= !item
->isDir();
451 // Check whether the MIME types of all selected files match
452 // to the mimetype of the service action. As soon as one MIME
453 // type does not match, no service menu is shown at all.
454 QListIterator
<KFileItem
*> mimeIt(itemList
);
456 while (insert
&& mimeIt
.hasNext()) {
457 KFileItem
* item
= mimeIt
.next();
458 const QString
mimeType(item
->mimetype());
459 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
461 insert
= (*it
== mimeType
) ||
462 ((*it
).right(1) == "*") &&
463 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
468 const QString submenuName
= cfg
.readEntry( "X-KDE-Submenu" );
469 Q3PopupMenu
* popup
= 0;
470 if (!submenuName
.isEmpty()) {
471 // create a sub menu containing all actions
472 popup
= new Q3PopupMenu();
473 connect(popup
, SIGNAL(activated(int)),
474 this, SLOT(startService(int)));
476 QPushButton
* button
= new QPushButton(submenuName
, m_actionBox
);
477 button
->setFlat(true);
478 button
->setMenu(popup
);
480 m_actionWidgets
.append(button
);
483 Q3ValueList
<KDEDesktopMimeType::Service
> userServices
=
484 KDEDesktopMimeType::userDefinedServices(*dirIt
+ *entryIt
, true);
486 // iterate through all actions and add them to a widget
487 Q3ValueList
<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)));
497 m_actionWidgets
.append(button
);
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"