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 "infosidebarpage.h"
22 #include <config-nepomuk.h>
24 #include <kfileplacesmodel.h>
26 #include <kstandarddirs.h>
27 #include <kio/previewjob.h>
28 #include <kfileitem.h>
30 #include <kglobalsettings.h>
31 #include <kfilemetainfo.h>
33 #include <kseparator.h>
34 #include <kiconloader.h>
37 #include <QInputDialog>
40 #include <QResizeEvent>
42 #include <QVBoxLayout>
44 #include "dolphinsettings.h"
45 #include "metadatawidget.h"
46 #include "pixmapviewer.h"
48 InfoSidebarPage::InfoSidebarPage(QWidget
* parent
) :
50 m_pendingPreview(false),
57 const int spacing
= KDialog::spacingHint();
59 m_timer
= new QTimer(this);
60 m_timer
->setSingleShot(true);
61 connect(m_timer
, SIGNAL(timeout()),
62 this, SLOT(slotTimeout()));
64 QVBoxLayout
* layout
= new QVBoxLayout
;
65 layout
->setSpacing(spacing
);
68 m_preview
= new PixmapViewer(this);
69 m_preview
->setMinimumWidth(KIconLoader::SizeEnormous
);
70 m_preview
->setFixedHeight(KIconLoader::SizeEnormous
);
73 m_nameLabel
= new QLabel(this);
74 m_nameLabel
->setTextFormat(Qt::RichText
);
75 m_nameLabel
->setAlignment(m_nameLabel
->alignment() | Qt::AlignHCenter
);
76 m_nameLabel
->setWordWrap(true);
78 // general information
79 m_infoLabel
= new QLabel(this);
80 m_infoLabel
->setSizePolicy(QSizePolicy::Minimum
, QSizePolicy::Minimum
);
81 m_infoLabel
->setTextFormat(Qt::RichText
);
82 m_infoLabel
->setWordWrap(true);
84 if (MetaDataWidget::metaDataAvailable()) {
85 m_metadataWidget
= new MetaDataWidget(this);
88 layout
->addItem(new QSpacerItem(spacing
, spacing
, QSizePolicy::Preferred
, QSizePolicy::Fixed
));
89 layout
->addWidget(m_preview
);
90 layout
->addWidget(m_nameLabel
);
91 layout
->addWidget(new KSeparator(this));
92 layout
->addWidget(m_infoLabel
);
93 layout
->addWidget(new KSeparator(this));
94 if (m_metadataWidget
) {
95 layout
->addWidget(m_metadataWidget
);
97 // ensure that widgets in the information side bar are aligned towards the top
98 layout
->addStretch(1);
102 InfoSidebarPage::~InfoSidebarPage()
106 QSize
InfoSidebarPage::sizeHint() const
108 QSize size
= SidebarPage::sizeHint();
109 size
.setWidth(minimumSizeHint().width());
113 void InfoSidebarPage::setUrl(const KUrl
& url
)
115 if (url
.isValid() && !m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
122 void InfoSidebarPage::setSelection(const KFileItemList
& selection
)
124 SidebarPage::setSelection(selection
);
125 if (selection
.size() == 1) {
126 const KUrl url
= selection
.first().url();
127 if (!url
.isEmpty()) {
128 m_urlCandidate
= url
;
131 m_timer
->start(TimerDelay
);
134 void InfoSidebarPage::requestDelayedItemInfo(const KFileItem
& item
)
138 if (!item
.isNull() && (selection().size() <= 1)) {
139 const KUrl url
= item
.url();
140 if (!url
.isEmpty()) {
141 m_urlCandidate
= url
;
142 m_timer
->start(TimerDelay
);
147 void InfoSidebarPage::showEvent(QShowEvent
* event
)
149 SidebarPage::showEvent(event
);
150 if (event
->spontaneous()) {
156 void InfoSidebarPage::resizeEvent(QResizeEvent
* event
)
158 // If the text inside the name label or the info label cannot
159 // get wrapped, then the maximum width of the label is increased
160 // so that the width of the information sidebar gets increased.
161 // To prevent this, the maximum width is adjusted to
162 // the current width of the sidebar.
163 const int maxWidth
= event
->size().width() - KDialog::spacingHint() * 4;
164 m_nameLabel
->setMaximumWidth(maxWidth
);
165 m_infoLabel
->setMaximumWidth(maxWidth
);
166 SidebarPage::resizeEvent(event
);
169 void InfoSidebarPage::showItemInfo()
177 const KFileItemList
& selectedItems
= selection();
180 if (selectedItems
.isEmpty()) {
183 file
= selectedItems
[0].url();
185 if (!file
.isValid()) {
188 const int itemCount
= selectedItems
.count();
190 KIconLoader iconLoader
;
191 QPixmap icon
= iconLoader
.loadIcon("exec",
192 KIconLoader::NoGroup
,
193 KIconLoader::SizeEnormous
);
194 m_preview
->setPixmap(icon
);
195 m_nameLabel
->setText(i18ncp("@info", "%1 item selected", "%1 items selected", selectedItems
.count()));
196 } else if (!applyPlace(file
)) {
197 // try to get a preview pixmap from the item...
201 m_pendingPreview
= true;
202 m_preview
->setPixmap(QPixmap());
204 KIO::PreviewJob
* job
= KIO::filePreview(list
,
206 KIconLoader::SizeEnormous
,
211 job
->setIgnoreMaximumSize(true);
213 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
214 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
215 connect(job
, SIGNAL(failed(const KFileItem
&)),
216 this, SLOT(showIcon(const KFileItem
&)));
219 text
.append(file
.fileName());
221 m_nameLabel
->setText(text
);
227 void InfoSidebarPage::slotTimeout()
229 m_shownUrl
= m_urlCandidate
;
233 void InfoSidebarPage::showIcon(const KFileItem
& item
)
235 m_pendingPreview
= false;
236 if (!applyPlace(item
.url())) {
237 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
241 void InfoSidebarPage::showPreview(const KFileItem
& item
,
242 const QPixmap
& pixmap
)
245 if (m_pendingPreview
) {
246 m_preview
->setPixmap(pixmap
);
247 m_pendingPreview
= false;
251 bool InfoSidebarPage::applyPlace(const KUrl
& url
)
253 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
254 int count
= placesModel
->rowCount();
256 for (int i
= 0; i
< count
; ++i
) {
257 QModelIndex index
= placesModel
->index(i
, 0);
259 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
261 text
.append(placesModel
->text(index
));
263 m_nameLabel
->setText(text
);
265 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
273 void InfoSidebarPage::cancelRequest()
276 m_pendingPreview
= false;
279 void InfoSidebarPage::showMetaInfo()
283 const KFileItemList
& selectedItems
= selection();
284 if (selectedItems
.size() <= 1) {
285 KFileItem
fileItem(S_IFDIR
, KFileItem::Unknown
, m_shownUrl
);
288 if (fileItem
.isDir()) {
289 addInfoLine(text
, i18nc("@label", "Type:"), i18nc("@label", "Folder"));
291 addInfoLine(text
, i18nc("@label", "Type:"), fileItem
.mimeComment());
293 QString
sizeText(KIO::convertSize(fileItem
.size()));
294 addInfoLine(text
, i18nc("@label", "Size:"), sizeText
);
295 addInfoLine(text
, i18nc("@label", "Modified:"), fileItem
.timeString());
297 // TODO: See convertMetaInfo below, find a way to display only interesting information
299 const KFileMetaInfo
metaInfo(fileItem
.url());
300 if (metaInfo
.isValid()) {
301 const QHash
<QString
, KFileMetaInfoItem
>& items
= metaInfo
.items();
302 QHash
<QString
, KFileMetaInfoItem
>::const_iterator it
= items
.constBegin();
303 const QHash
<QString
, KFileMetaInfoItem
>::const_iterator end
= items
.constEnd();
306 const KFileMetaInfoItem
& metaInfo
= it
.value();
307 const QVariant
& value
= metaInfo
.value();
308 if (value
.isValid() && convertMetaInfo(metaInfo
.name(), labelText
)) {
309 addInfoLine(text
, labelText
, value
.toString());
316 if (MetaDataWidget::metaDataAvailable()) {
317 m_metadataWidget
->setFile(fileItem
.url());
320 if (MetaDataWidget::metaDataAvailable()) {
322 foreach (const KFileItem
& item
, selectedItems
) {
323 urls
.append(item
.url());
325 m_metadataWidget
->setFiles(urls
);
328 unsigned long int totalSize
= 0;
329 foreach (const KFileItem
& item
, selectedItems
) {
330 // TODO: what to do with directories (same with the one-item-selected-code)?,
331 // item.size() does not return the size of the content : not very instinctive for users
332 totalSize
+= item
.size();
334 addInfoLine(text
, i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
336 m_infoLabel
->setText(text
);
339 void InfoSidebarPage::addInfoLine(QString
& text
,
340 const QString
& labelText
,
341 const QString
& infoText
)
343 if (!text
.isEmpty()) {
346 text
+= QString("<b>%1</b> %2").arg(labelText
).arg(infoText
);
349 bool InfoSidebarPage::convertMetaInfo(const QString
& key
, QString
& text
) const
351 // TODO: This code prevents that interesting meta information might be hidden
352 // and only bypasses the current problem that not all the meta information should
353 // be shown to the user. Check whether it's possible with Nepomuk to show
354 // all "user relevant" information in a readable way...
361 // sorted list of keys, where its data should be shown
362 static const MetaKey keys
[] = {
363 { "audio.album", "Album:" },
364 { "audio.artist", "Artist:" },
365 { "audio.title", "Title:" },
368 // do a binary search for the key...
370 int bottom
= sizeof(keys
) / sizeof(MetaKey
) - 1;
371 while (top
<= bottom
) {
372 const int middle
= (top
+ bottom
) / 2;
373 const int result
= key
.compare(keys
[middle
].key
);
376 } else if (result
> 0) {
379 text
= keys
[middle
].text
;
387 #include "infosidebarpage.moc"