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 <kfileplacesmodel.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()
125 void InfoSidebarPage::setUrl(const KUrl
& url
)
127 if (!m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
134 void InfoSidebarPage::setSelection(const KFileItemList
& selection
)
137 m_currentSelection
= selection
;
138 m_multipleSelection
= (m_currentSelection
.size() > 1);
142 void InfoSidebarPage::requestDelayedItemInfo(const KUrl
& url
)
146 if (!url
.isEmpty() && !m_multipleSelection
) {
147 m_urlCandidate
= url
;
148 m_timer
->setSingleShot(true);
153 void InfoSidebarPage::showItemInfo()
157 KFileItemList selectedItems
= m_currentSelection
;
159 if (selectedItems
.count() == 0) {
162 file
= selectedItems
[0]->url();
164 if (m_multipleSelection
) {
165 KIconLoader iconLoader
;
166 QPixmap icon
= iconLoader
.loadIcon("exec",
168 K3Icon::SizeEnormous
);
169 m_preview
->setPixmap(icon
);
170 m_name
->setText(i18n("%1 items selected", selectedItems
.count()));
171 } else if (!applyBookmark(file
)) {
172 // try to get a preview pixmap from the item...
176 m_pendingPreview
= true;
177 m_preview
->setPixmap(QPixmap());
179 KIO::PreviewJob
* job
= KIO::filePreview(list
,
181 K3Icon::SizeEnormous
,
186 job
->setIgnoreMaximumSize(true);
188 connect(job
, SIGNAL(gotPreview(const KFileItem
*, const QPixmap
&)),
189 this, SLOT(gotPreview(const KFileItem
*, const QPixmap
&)));
190 connect(job
, SIGNAL(failed(const KFileItem
*)),
191 this, SLOT(slotPreviewFailed(const KFileItem
*)));
194 text
.append(file
.fileName());
196 m_name
->setText(text
);
203 void InfoSidebarPage::slotTimeout()
205 m_shownUrl
= m_urlCandidate
;
209 void InfoSidebarPage::slotPreviewFailed(const KFileItem
* item
)
211 m_pendingPreview
= false;
212 if (!applyBookmark(item
->url())) {
213 m_preview
->setPixmap(item
->pixmap(K3Icon::SizeEnormous
));
217 void InfoSidebarPage::gotPreview(const KFileItem
* item
,
218 const QPixmap
& pixmap
)
221 if (m_pendingPreview
) {
222 m_preview
->setPixmap(pixmap
);
223 m_pendingPreview
= false;
227 void InfoSidebarPage::startService(int index
)
229 if (m_currentSelection
.count() > 0) {
230 // TODO: Use "at()" as soon as executeService is fixed to take a const param (BIC)
231 KDesktopFileActions::executeService(m_currentSelection
.urlList(), m_actionsVector
[index
]);
234 KDesktopFileActions::executeService(m_shownUrl
, m_actionsVector
[index
]);
238 bool InfoSidebarPage::applyBookmark(const KUrl
& url
)
240 KFilePlacesModel
*placesModel
= DolphinSettings::instance().placesModel();
241 int count
= placesModel
->rowCount();
243 for (int i
= 0; i
< count
; ++i
) {
244 QModelIndex index
= placesModel
->index(i
, 0);
246 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
248 text
.append(placesModel
->text(index
));
250 m_name
->setText(text
);
252 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
260 void InfoSidebarPage::cancelRequest()
263 m_pendingPreview
= false;
266 void InfoSidebarPage::createMetaInfo()
269 if (m_currentSelection
.size() == 0) {
270 KFileItem
fileItem(S_IFDIR
, KFileItem::Unknown
, m_shownUrl
);
273 if (fileItem
.isDir()) {
274 addInfoLine(i18n("Type:"), i18n("Directory"));
276 if (MetaDataWidget::metaDataAvailable())
277 m_metadataWidget
->setFile(fileItem
.url());
278 } else if (m_currentSelection
.count() == 1) {
279 KFileItem
* fileItem
= m_currentSelection
.at(0);
280 addInfoLine(i18n("Type:"), fileItem
->mimeComment());
282 QString
sizeText(KIO::convertSize(fileItem
->size()));
283 addInfoLine(i18n("Size:"), sizeText
);
284 addInfoLine(i18n("Modified:"), fileItem
->timeString());
286 const KFileMetaInfo
& metaInfo
= fileItem
->metaInfo();
287 if (metaInfo
.isValid()) {
288 QStringList keys
= metaInfo
.supportedKeys();
289 for (QStringList::Iterator it
= keys
.begin(); it
!= keys
.end(); ++it
) {
290 if (showMetaInfo(*it
)) {
291 KFileMetaInfoItem metaInfoItem
= metaInfo
.item(*it
);
292 addInfoLine(*it
, metaInfoItem
.value().toString());
296 if (MetaDataWidget::metaDataAvailable())
297 m_metadataWidget
->setFile(fileItem
->url());
299 if (MetaDataWidget::metaDataAvailable())
300 m_metadataWidget
->setFiles(m_currentSelection
.urlList());
301 unsigned long int totSize
= 0;
302 foreach(KFileItem
* item
, m_currentSelection
) {
303 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
305 addInfoLine(i18n("Total size:"), KIO::convertSize(totSize
));
310 void InfoSidebarPage::beginInfoLines()
312 m_infoLines
= QString("");
315 void InfoSidebarPage::endInfoLines()
317 m_infos
->setText(m_infoLines
);
320 bool InfoSidebarPage::showMetaInfo(const QString
& key
) const
322 // sorted list of keys, where it's data should be shown
323 static const char* keys
[] = {
338 // do a binary search for the key...
340 int bottom
= sizeof(keys
) / sizeof(char*) - 1;
341 while (top
< bottom
) {
342 const int middle
= (top
+ bottom
) / 2;
343 const int result
= key
.compare(keys
[middle
]);
346 } else if (result
> 0) {
356 void InfoSidebarPage::addInfoLine(const QString
& labelText
, const QString
& infoText
)
358 if (!m_infoLines
.isEmpty())
359 m_infoLines
+= "<br/>";
360 m_infoLines
+= QString("<b>%1</b> %2").arg(labelText
).arg(infoText
);
363 void InfoSidebarPage::insertActions()
365 QListIterator
<QPushButton
*> deleteIter(m_actionBox
->findChildren
<QPushButton
*>());
367 while (deleteIter
.hasNext()) {
368 widget
= deleteIter
.next();
370 widget
->deleteLater();
373 m_actionsVector
.clear();
375 int actionsIndex
= 0;
377 // The algorithm for searching the available actions works on a list
378 // of KFileItems. If no selection is given, a temporary KFileItem
379 // by the given Url 'url' is created and added to the list.
380 KFileItem
fileItem(S_IFDIR
, KFileItem::Unknown
, m_shownUrl
);
381 KFileItemList itemList
= m_currentSelection
;
382 if (itemList
.isEmpty()) {
384 itemList
.append(&fileItem
);
387 // 'itemList' contains now all KFileItems, where an item information should be shown.
388 // TODO: the following algorithm is quite equal to DolphinContextMenu::insertActionItems().
389 // It's open yet whether they should be merged or whether they have to work slightly different.
390 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
391 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
393 QStringList entries
= dir
.entryList(QStringList("*.desktop"), QDir::Files
);
395 for (QStringList::ConstIterator entryIt
= entries
.begin(); entryIt
!= entries
.end(); ++entryIt
) {
396 KConfigGroup
cfg(KSharedConfig::openConfig(*dirIt
+ *entryIt
, KConfig::OnlyLocal
), "Desktop Entry");
397 if ((cfg
.hasKey("Actions") || cfg
.hasKey("X-KDE-GetActionMenu")) && cfg
.hasKey("ServiceTypes")) {
398 const QStringList types
= cfg
.readEntry("ServiceTypes", QStringList(), ',');
399 for (QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
) {
400 // check whether the mime type is equal or whether the
401 // mimegroup (e. g. image/*) is supported
404 if ((*it
) == "all/allfiles") {
405 // The service type is valid for all files, but not for directories.
406 // Check whether the selected items only consist of files...
407 QListIterator
<KFileItem
*> mimeIt(itemList
);
409 while (insert
&& mimeIt
.hasNext()) {
410 KFileItem
* item
= mimeIt
.next();
411 insert
= !item
->isDir();
416 // Check whether the MIME types of all selected files match
417 // to the mimetype of the service action. As soon as one MIME
418 // type does not match, no service menu is shown at all.
419 QListIterator
<KFileItem
*> mimeIt(itemList
);
421 while (insert
&& mimeIt
.hasNext()) {
422 KFileItem
* item
= mimeIt
.next();
423 const QString
mimeType(item
->mimetype());
424 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
426 insert
= (*it
== mimeType
) ||
427 ((*it
).right(1) == "*") &&
428 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
433 const QString submenuName
= cfg
.readEntry("X-KDE-Submenu");
435 if (!submenuName
.isEmpty()) {
436 // create a sub menu containing all actions
438 connect(popup
, SIGNAL(activated(int)),
439 this, SLOT(startService(int)));
441 QPushButton
* button
= new QPushButton(submenuName
, m_actionBox
);
442 button
->setFlat(true);
443 button
->setMenu(popup
);
447 QList
<KDesktopFileActions::Service
> userServices
=
448 KDesktopFileActions::userDefinedServices(*dirIt
+ *entryIt
, true);
450 // iterate through all actions and add them to a widget
451 QList
<KDesktopFileActions::Service
>::Iterator serviceIt
;
452 for (serviceIt
= userServices
.begin(); serviceIt
!= userServices
.end(); ++serviceIt
) {
453 KDesktopFileActions::Service service
= (*serviceIt
);
455 ServiceButton
* button
= new ServiceButton(KIcon(service
.m_strIcon
),
459 connect(button
, SIGNAL(requestServiceStart(int)),
460 this, SLOT(startService(int)));
463 popup
->insertItem(KIcon(service
.m_strIcon
), service
.m_strName
, actionsIndex
);
466 m_actionsVector
.append(service
);
478 ServiceButton::ServiceButton(const QIcon
& icon
,
482 QPushButton(icon
, text
, parent
),
486 setEraseColor(palette().brush(QPalette::Background
).color());
487 setFocusPolicy(Qt::NoFocus
);
488 connect(this, SIGNAL(released()),
489 this, SLOT(slotReleased()));
492 ServiceButton::~ServiceButton()
495 void ServiceButton::paintEvent(QPaintEvent
* event
)
498 QPainter
painter(this);
499 const int buttonWidth
= width();
500 const int buttonHeight
= height();
502 QColor backgroundColor
;
503 QColor foregroundColor
;
505 backgroundColor
= KGlobalSettings::highlightColor();
506 foregroundColor
= KGlobalSettings::highlightedTextColor();
508 backgroundColor
= palette().brush(QPalette::Background
).color();
509 foregroundColor
= KGlobalSettings::buttonTextColor();
512 // draw button background
513 painter
.setPen(Qt::NoPen
);
514 painter
.setBrush(backgroundColor
);
515 painter
.drawRect(0, 0, buttonWidth
, buttonHeight
);
517 const int spacing
= KDialog::spacingHint();
521 const int y
= (buttonHeight
- K3Icon::SizeSmall
) / 2;
522 const QIcon
&set
= icon();
524 painter
.drawPixmap(x
, y
, set
.pixmap(QIcon::Small
, QIcon::Normal
));
526 x
+= K3Icon::SizeSmall
+ spacing
;
529 painter
.setPen(foregroundColor
);
531 const int textWidth
= buttonWidth
- x
;
532 QFontMetrics
fontMetrics(font());
533 const bool clipped
= fontMetrics
.width(text()) >= textWidth
;
534 //const int align = clipped ? Qt::AlignVCenter : Qt::AlignCenter;
535 painter
.drawText(QRect(x
, 0, textWidth
, buttonHeight
), Qt::AlignVCenter
, text());
538 // Blend the right area of the text with the background, as the
540 // TODO #1: use alpha blending in Qt4 instead of drawing the text that often
541 // TODO #2: same code as in UrlNavigatorButton::drawButton() -> provide helper class?
542 const int blendSteps
= 16;
544 QColor
blendColor(backgroundColor
);
545 const int redInc
= (foregroundColor
.red() - backgroundColor
.red()) / blendSteps
;
546 const int greenInc
= (foregroundColor
.green() - backgroundColor
.green()) / blendSteps
;
547 const int blueInc
= (foregroundColor
.blue() - backgroundColor
.blue()) / blendSteps
;
548 for (int i
= 0; i
< blendSteps
; ++i
) {
549 painter
.setClipRect(QRect(x
+ textWidth
- i
, 0, 1, buttonHeight
));
550 painter
.setPen(blendColor
);
551 painter
.drawText(QRect(x
, 0, textWidth
, buttonHeight
), Qt::AlignVCenter
, text());
553 blendColor
.setRgb(blendColor
.red() + redInc
,
554 blendColor
.green() + greenInc
,
555 blendColor
.blue() + blueInc
);
560 void ServiceButton::enterEvent(QEvent
* event
)
562 QPushButton::enterEvent(event
);
567 void ServiceButton::leaveEvent(QEvent
* event
)
569 QPushButton::leaveEvent(event
);
574 void ServiceButton::slotReleased()
576 emit
requestServiceStart(m_index
);
579 #include "infosidebarpage.moc"