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>
38 #include <QFontMetrics>
39 #include <QInputDialog>
45 #include <QPushButton>
46 #include <QResizeEvent>
49 #include "dolphinsettings.h"
50 #include "metadatawidget.h"
51 #include "pixmapviewer.h"
53 InfoSidebarPage::InfoSidebarPage(QWidget
* parent
) :
55 m_multipleSelection(false), //TODO: check if I'm needed
56 m_pendingPreview(false),
63 const int spacing
= KDialog::spacingHint();
65 m_timer
= new QTimer(this);
66 connect(m_timer
, SIGNAL(timeout()),
67 this, SLOT(slotTimeout()));
69 QVBoxLayout
* layout
= new QVBoxLayout
;
70 layout
->setSpacing(spacing
);
73 m_preview
= new PixmapViewer(this);
74 m_preview
->setMinimumWidth(K3Icon::SizeEnormous
);
75 m_preview
->setFixedHeight(K3Icon::SizeEnormous
);
78 m_name
= new QLabel(this);
79 m_name
->setTextFormat(Qt::RichText
);
80 m_name
->setAlignment(m_name
->alignment() | Qt::AlignHCenter
);
81 m_name
->setWordWrap(true);
83 // general information
84 m_infos
= new QLabel(this);
85 m_infos
->setSizePolicy(QSizePolicy::Minimum
, QSizePolicy::Fixed
);
86 m_infos
->setTextFormat(Qt::RichText
);
88 if (MetaDataWidget::metaDataAvailable()) {
89 m_metadataWidget
= new MetaDataWidget(this);
92 layout
->addItem(new QSpacerItem(spacing
, spacing
, QSizePolicy::Preferred
, QSizePolicy::Fixed
));
93 layout
->addWidget(m_preview
);
94 layout
->addWidget(m_name
);
95 layout
->addWidget(new KSeparator(this));
96 layout
->addWidget(m_infos
);
97 layout
->addWidget(new KSeparator(this));
98 if (m_metadataWidget
) {
99 layout
->addWidget(m_metadataWidget
);
100 layout
->addWidget(new KSeparator(this));
102 // ensure that widgets in the information side bar are aligned towards the top
103 layout
->addStretch(1);
107 InfoSidebarPage::~InfoSidebarPage()
111 void InfoSidebarPage::setUrl(const KUrl
& url
)
113 if (!m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
120 void InfoSidebarPage::setSelection(const KFileItemList
& selection
)
123 SidebarPage::setSelection(selection
);
124 m_multipleSelection
= (selection
.size() > 1);
128 void InfoSidebarPage::requestDelayedItemInfo(const KUrl
& url
)
132 if (!url
.isEmpty() && !m_multipleSelection
) {
133 m_urlCandidate
= url
;
134 m_timer
->setSingleShot(true);
139 void InfoSidebarPage::showEvent(QShowEvent
* event
)
141 SidebarPage::showEvent(event
);
142 if (event
->spontaneous()) {
148 void InfoSidebarPage::resizeEvent(QResizeEvent
* event
)
150 // If the item name cannot get wrapped, the maximum width of
151 // the label is increased, so that the width of the information sidebar
152 // gets increased. To prevent this, the maximum width is adjusted to
153 // the current width of the sidebar.
154 m_name
->setMaximumWidth(event
->size().width() - KDialog::spacingHint() * 4);
155 SidebarPage::resizeEvent(event
);
158 void InfoSidebarPage::showItemInfo()
166 const KFileItemList
& selectedItems
= selection();
169 if (selectedItems
.count() == 0) {
172 file
= selectedItems
[0]->url();
174 if (m_multipleSelection
) {
175 KIconLoader iconLoader
;
176 QPixmap icon
= iconLoader
.loadIcon("exec",
178 K3Icon::SizeEnormous
);
179 m_preview
->setPixmap(icon
);
180 m_name
->setText(i18n("%1 items selected", selectedItems
.count()));
181 } else if (!applyBookmark(file
)) {
182 // try to get a preview pixmap from the item...
186 m_pendingPreview
= true;
187 m_preview
->setPixmap(QPixmap());
189 KIO::PreviewJob
* job
= KIO::filePreview(list
,
191 K3Icon::SizeEnormous
,
196 job
->setIgnoreMaximumSize(true);
198 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
199 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
200 connect(job
, SIGNAL(failed(const KFileItem
&)),
201 this, SLOT(showIcon(const KFileItem
&)));
204 text
.append(file
.fileName());
206 m_name
->setText(text
);
212 void InfoSidebarPage::slotTimeout()
214 m_shownUrl
= m_urlCandidate
;
218 void InfoSidebarPage::showIcon(const KFileItem
& item
)
220 m_pendingPreview
= false;
221 if (!applyBookmark(item
.url())) {
222 m_preview
->setPixmap(item
.pixmap(K3Icon::SizeEnormous
));
226 void InfoSidebarPage::showPreview(const KFileItem
& item
,
227 const QPixmap
& pixmap
)
230 if (m_pendingPreview
) {
231 m_preview
->setPixmap(pixmap
);
232 m_pendingPreview
= false;
236 bool InfoSidebarPage::applyBookmark(const KUrl
& url
)
238 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
239 int count
= placesModel
->rowCount();
241 for (int i
= 0; i
< count
; ++i
) {
242 QModelIndex index
= placesModel
->index(i
, 0);
244 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
246 text
.append(placesModel
->text(index
));
248 m_name
->setText(text
);
250 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
258 void InfoSidebarPage::cancelRequest()
261 m_pendingPreview
= false;
264 void InfoSidebarPage::createMetaInfo()
267 const KFileItemList
& selectedItems
= selection();
268 if (selectedItems
.size() == 0) {
269 KFileItem
fileItem(S_IFDIR
, KFileItem::Unknown
, m_shownUrl
);
272 if (fileItem
.isDir()) {
273 addInfoLine(i18n("Type:"), i18n("Directory"));
275 if (MetaDataWidget::metaDataAvailable()) {
276 m_metadataWidget
->setFile(fileItem
.url());
278 } else if (selectedItems
.count() == 1) {
279 KFileItem
* fileItem
= selectedItems
.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());
300 if (MetaDataWidget::metaDataAvailable()) {
301 m_metadataWidget
->setFiles(selectedItems
.urlList());
304 unsigned long int totSize
= 0;
305 foreach(KFileItem
* item
, selectedItems
) {
306 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
308 addInfoLine(i18n("Total size:"), KIO::convertSize(totSize
));
313 void InfoSidebarPage::beginInfoLines()
315 m_infoLines
= QString();
318 void InfoSidebarPage::endInfoLines()
320 m_infos
->setText(m_infoLines
);
323 bool InfoSidebarPage::showMetaInfo(const QString
& key
) const
325 // sorted list of keys, where it's data should be shown
326 static const char* keys
[] = {
341 // do a binary search for the key...
343 int bottom
= sizeof(keys
) / sizeof(char*) - 1;
344 while (top
< bottom
) {
345 const int middle
= (top
+ bottom
) / 2;
346 const int result
= key
.compare(keys
[middle
]);
349 } else if (result
> 0) {
359 void InfoSidebarPage::addInfoLine(const QString
& labelText
, const QString
& infoText
)
361 if (!m_infoLines
.isEmpty()) {
362 m_infoLines
+= "<br/>";
364 m_infoLines
+= QString("<b>%1</b> %2").arg(labelText
).arg(infoText
);
367 #include "infosidebarpage.moc"