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-kmetadata.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_name
= new QLabel(this);
74 m_name
->setTextFormat(Qt::RichText
);
75 m_name
->setAlignment(m_name
->alignment() | Qt::AlignHCenter
);
76 m_name
->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_name
);
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 item name cannot get wrapped, the maximum width of
142 // the label is increased, so that the width of the information sidebar
143 // gets increased. To prevent this, the maximum width is adjusted to
144 // the current width of the sidebar.
145 m_name
->setMaximumWidth(event
->size().width() - KDialog::spacingHint() * 4);
146 SidebarPage::resizeEvent(event
);
149 void InfoSidebarPage::showItemInfo()
157 const KFileItemList
& selectedItems
= selection();
160 const int itemCount
= selectedItems
.count();
161 if (selectedItems
.count() == 0) {
164 file
= selectedItems
[0]->url();
167 KIconLoader iconLoader
;
168 QPixmap icon
= iconLoader
.loadIcon("exec",
170 K3Icon::SizeEnormous
);
171 m_preview
->setPixmap(icon
);
172 m_name
->setText(i18n("%1 items selected", selectedItems
.count()));
173 } else if (!applyBookmark(file
)) {
174 // try to get a preview pixmap from the item...
178 m_pendingPreview
= true;
179 m_preview
->setPixmap(QPixmap());
181 KIO::PreviewJob
* job
= KIO::filePreview(list
,
183 K3Icon::SizeEnormous
,
188 job
->setIgnoreMaximumSize(true);
190 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
191 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
192 connect(job
, SIGNAL(failed(const KFileItem
&)),
193 this, SLOT(showIcon(const KFileItem
&)));
196 text
.append(file
.fileName());
198 m_name
->setText(text
);
204 void InfoSidebarPage::slotTimeout()
206 m_shownUrl
= m_urlCandidate
;
210 void InfoSidebarPage::showIcon(const KFileItem
& item
)
212 m_pendingPreview
= false;
213 if (!applyBookmark(item
.url())) {
214 m_preview
->setPixmap(item
.pixmap(K3Icon::SizeEnormous
));
218 void InfoSidebarPage::showPreview(const KFileItem
& item
,
219 const QPixmap
& pixmap
)
222 if (m_pendingPreview
) {
223 m_preview
->setPixmap(pixmap
);
224 m_pendingPreview
= false;
228 bool InfoSidebarPage::applyBookmark(const KUrl
& url
)
230 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
231 int count
= placesModel
->rowCount();
233 for (int i
= 0; i
< count
; ++i
) {
234 QModelIndex index
= placesModel
->index(i
, 0);
236 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
238 text
.append(placesModel
->text(index
));
240 m_name
->setText(text
);
242 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
250 void InfoSidebarPage::cancelRequest()
253 m_pendingPreview
= false;
256 void InfoSidebarPage::showMetaInfo()
260 const KFileItemList
& selectedItems
= selection();
261 if (selectedItems
.size() <= 1) {
262 KFileItem
fileItem(S_IFDIR
, KFileItem::Unknown
, m_shownUrl
);
265 if (fileItem
.isDir()) {
266 addInfoLine(text
, i18n("Type:"), i18n("Folder"));
268 addInfoLine(text
, i18n("Type:"), fileItem
.mimeComment());
270 QString
sizeText(KIO::convertSize(fileItem
.size()));
271 addInfoLine(text
, i18n("Size:"), sizeText
);
272 addInfoLine(text
, i18n("Modified:"), fileItem
.timeString());
274 // TODO: deactivate showing additional meta information, as the system
275 // hangs when retrieving the meta information of a zipped file
276 /*const KFileMetaInfo metaInfo(fileItem.url());
277 if (metaInfo.isValid()) {
278 const QHash<QString, KFileMetaInfoItem>& items = metaInfo.items();
279 QHash<QString, KFileMetaInfoItem>::const_iterator it = items.constBegin();
280 const QHash<QString, KFileMetaInfoItem>::const_iterator end = items.constEnd();
283 const KFileMetaInfoItem& metaInfo = it.value();
284 const QVariant& value = metaInfo.value();
285 if (value.isValid() && convertMetaInfo(metaInfo.name(), labelText)) {
286 addInfoLine(text, labelText, value.toString());
293 if (MetaDataWidget::metaDataAvailable()) {
294 m_metadataWidget
->setFile(fileItem
.url());
297 if (MetaDataWidget::metaDataAvailable()) {
298 m_metadataWidget
->setFiles(selectedItems
.urlList());
301 unsigned long int totalSize
= 0;
302 foreach (KFileItem
* item
, selectedItems
) {
303 // TODO: what to do with directories (same with the one-item-selected-code)?,
304 // item->size() does not return the size of the content : not very instinctive for users
305 totalSize
+= item
->size();
307 addInfoLine(text
, i18n("Total size:"), KIO::convertSize(totalSize
));
309 m_infoLabel
->setText(text
);
312 void InfoSidebarPage::addInfoLine(QString
& text
,
313 const QString
& labelText
,
314 const QString
& infoText
)
316 if (!infoText
.isEmpty()) {
319 text
+= QString("<b>%1</b> %2").arg(labelText
).arg(infoText
);
322 bool InfoSidebarPage::convertMetaInfo(const QString
& key
, QString
& text
) const
324 // TODO: This code prevents that interesting meta information might be hidden
325 // and only bypasses the current problem that not all the meta information should
326 // be shown to the user. Check whether it's possible with Nepomuk to show
327 // all "user relevant" information in a readable way...
334 // sorted list of keys, where its data should be shown
335 static const MetaKey keys
[] = {
336 { "audio.album", "Album:" },
337 { "audio.artist", "Artist:" },
338 { "audio.title", "Title:" },
341 // do a binary search for the key...
343 int bottom
= sizeof(keys
) / sizeof(MetaKey
) - 1;
344 while (top
<= bottom
) {
345 const int middle
= (top
+ bottom
) / 2;
346 const int result
= key
.compare(keys
[middle
].key
);
349 } else if (result
> 0) {
352 text
= keys
[middle
].text
;
360 #include "infosidebarpage.moc"