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(K3Icon::SizeEnormous
);
70 m_preview
->setFixedHeight(K3Icon::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::Fixed
);
81 m_infoLabel
->setTextFormat(Qt::RichText
);
83 if (MetaDataWidget::metaDataAvailable()) {
84 m_metadataWidget
= new MetaDataWidget(this);
87 layout
->addItem(new QSpacerItem(spacing
, spacing
, QSizePolicy::Preferred
, QSizePolicy::Fixed
));
88 layout
->addWidget(m_preview
);
89 layout
->addWidget(m_nameLabel
);
90 layout
->addWidget(new KSeparator(this));
91 layout
->addWidget(m_infoLabel
);
92 layout
->addWidget(new KSeparator(this));
93 if (m_metadataWidget
) {
94 layout
->addWidget(m_metadataWidget
);
96 // ensure that widgets in the information side bar are aligned towards the top
97 layout
->addStretch(1);
101 InfoSidebarPage::~InfoSidebarPage()
105 void InfoSidebarPage::setUrl(const KUrl
& url
)
107 if (url
.isValid() && !m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
114 void InfoSidebarPage::setSelection(const KFileItemList
& selection
)
116 SidebarPage::setSelection(selection
);
117 m_timer
->start(TimerDelay
);
120 void InfoSidebarPage::requestDelayedItemInfo(const KUrl
& url
)
124 if (!url
.isEmpty() && (selection().size() <= 1)) {
125 m_urlCandidate
= url
;
126 m_timer
->start(TimerDelay
);
130 void InfoSidebarPage::showEvent(QShowEvent
* event
)
132 SidebarPage::showEvent(event
);
133 if (event
->spontaneous()) {
139 void InfoSidebarPage::resizeEvent(QResizeEvent
* event
)
141 // If the text inside the name label or the info label cannot
142 // get wrapped, then the maximum width of the label is increased
143 // so that the width of the information sidebar gets increased.
144 // To prevent this, the maximum width is adjusted to
145 // the current width of the sidebar.
146 const int maxWidth
= event
->size().width() - KDialog::spacingHint() * 4;
147 m_nameLabel
->setMaximumWidth(maxWidth
);
148 m_infoLabel
->setMaximumWidth(maxWidth
);
149 SidebarPage::resizeEvent(event
);
152 void InfoSidebarPage::showItemInfo()
160 const KFileItemList
& selectedItems
= selection();
163 const int itemCount
= selectedItems
.count();
164 if (selectedItems
.count() == 0) {
167 file
= selectedItems
[0]->url();
170 KIconLoader iconLoader
;
171 QPixmap icon
= iconLoader
.loadIcon("exec",
173 K3Icon::SizeEnormous
);
174 m_preview
->setPixmap(icon
);
175 m_nameLabel
->setText(i18np("%1 item selected", "%1 items selected", selectedItems
.count()));
176 } else if (!applyBookmark(file
)) {
177 // try to get a preview pixmap from the item...
181 m_pendingPreview
= true;
182 m_preview
->setPixmap(QPixmap());
184 KIO::PreviewJob
* job
= KIO::filePreview(list
,
186 K3Icon::SizeEnormous
,
191 job
->setIgnoreMaximumSize(true);
193 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
194 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
195 connect(job
, SIGNAL(failed(const KFileItem
&)),
196 this, SLOT(showIcon(const KFileItem
&)));
199 text
.append(file
.fileName());
201 m_nameLabel
->setText(text
);
207 void InfoSidebarPage::slotTimeout()
209 m_shownUrl
= m_urlCandidate
;
213 void InfoSidebarPage::showIcon(const KFileItem
& item
)
215 m_pendingPreview
= false;
216 if (!applyBookmark(item
.url())) {
217 m_preview
->setPixmap(item
.pixmap(K3Icon::SizeEnormous
));
221 void InfoSidebarPage::showPreview(const KFileItem
& item
,
222 const QPixmap
& pixmap
)
225 if (m_pendingPreview
) {
226 m_preview
->setPixmap(pixmap
);
227 m_pendingPreview
= false;
231 bool InfoSidebarPage::applyBookmark(const KUrl
& url
)
233 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
234 int count
= placesModel
->rowCount();
236 for (int i
= 0; i
< count
; ++i
) {
237 QModelIndex index
= placesModel
->index(i
, 0);
239 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
241 text
.append(placesModel
->text(index
));
243 m_nameLabel
->setText(text
);
245 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
253 void InfoSidebarPage::cancelRequest()
256 m_pendingPreview
= false;
259 void InfoSidebarPage::showMetaInfo()
263 const KFileItemList
& selectedItems
= selection();
264 if (selectedItems
.size() <= 1) {
265 KFileItem
fileItem(S_IFDIR
, KFileItem::Unknown
, m_shownUrl
);
268 if (fileItem
.isDir()) {
269 addInfoLine(text
, i18n("Type:"), i18n("Folder"));
271 addInfoLine(text
, i18n("Type:"), fileItem
.mimeComment());
273 QString
sizeText(KIO::convertSize(fileItem
.size()));
274 addInfoLine(text
, i18n("Size:"), sizeText
);
275 addInfoLine(text
, i18n("Modified:"), fileItem
.timeString());
277 // TODO: deactivate showing additional meta information, as the system
278 // hangs when retrieving the meta information of a zipped file
279 /*const KFileMetaInfo metaInfo(fileItem.url());
280 if (metaInfo.isValid()) {
281 const QHash<QString, KFileMetaInfoItem>& items = metaInfo.items();
282 QHash<QString, KFileMetaInfoItem>::const_iterator it = items.constBegin();
283 const QHash<QString, KFileMetaInfoItem>::const_iterator end = items.constEnd();
286 const KFileMetaInfoItem& metaInfo = it.value();
287 const QVariant& value = metaInfo.value();
288 if (value.isValid() && convertMetaInfo(metaInfo.name(), labelText)) {
289 addInfoLine(text, labelText, value.toString());
296 if (MetaDataWidget::metaDataAvailable()) {
297 m_metadataWidget
->setFile(fileItem
.url());
300 if (MetaDataWidget::metaDataAvailable()) {
301 m_metadataWidget
->setFiles(selectedItems
.urlList());
304 unsigned long int totalSize
= 0;
305 foreach (KFileItem
* item
, selectedItems
) {
306 // TODO: what to do with directories (same with the one-item-selected-code)?,
307 // item->size() does not return the size of the content : not very instinctive for users
308 totalSize
+= item
->size();
310 addInfoLine(text
, i18n("Total size:"), KIO::convertSize(totalSize
));
312 m_infoLabel
->setText(text
);
315 void InfoSidebarPage::addInfoLine(QString
& text
,
316 const QString
& labelText
,
317 const QString
& infoText
)
319 if (!infoText
.isEmpty()) {
322 text
+= QString("<b>%1</b> %2").arg(labelText
).arg(infoText
);
325 bool InfoSidebarPage::convertMetaInfo(const QString
& key
, QString
& text
) const
327 // TODO: This code prevents that interesting meta information might be hidden
328 // and only bypasses the current problem that not all the meta information should
329 // be shown to the user. Check whether it's possible with Nepomuk to show
330 // all "user relevant" information in a readable way...
337 // sorted list of keys, where its data should be shown
338 static const MetaKey keys
[] = {
339 { "audio.album", "Album:" },
340 { "audio.artist", "Artist:" },
341 { "audio.title", "Title:" },
344 // do a binary search for the key...
346 int bottom
= sizeof(keys
) / sizeof(MetaKey
) - 1;
347 while (top
<= bottom
) {
348 const int middle
= (top
+ bottom
) / 2;
349 const int result
= key
.compare(keys
[middle
].key
);
352 } else if (result
> 0) {
355 text
= keys
[middle
].text
;
363 #include "infosidebarpage.moc"