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"
26 #include <QFontMetrics>
27 #include <QInputDialog>
33 #include <QPushButton>
34 #include <QResizeEvent>
37 #include <kfileplacesmodel.h>
39 #include <kstandarddirs.h>
40 #include <kio/previewjob.h>
41 #include <kfileitem.h>
43 #include <kglobalsettings.h>
44 #include <kfilemetainfo.h>
46 #include <kseparator.h>
47 #include <kiconloader.h>
49 #include "pixmapviewer.h"
50 #include "dolphinsettings.h"
51 #include "metadatawidget.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
);
145 void InfoSidebarPage::resizeEvent(QResizeEvent
* event
)
147 // If the item name cannot get wrapped, the maximum width of
148 // the label is increased, so that the width of the information sidebar
149 // gets increased. To prevent this, the maximum width is adjusted to
150 // the current width of the sidebar.
151 m_name
->setMaximumWidth(event
->size().width() - KDialog::spacingHint() * 4);
152 SidebarPage::resizeEvent(event
);
155 void InfoSidebarPage::showItemInfo()
163 const KFileItemList
& selectedItems
= selection();
166 if (selectedItems
.count() == 0) {
169 file
= selectedItems
[0]->url();
171 if (m_multipleSelection
) {
172 KIconLoader iconLoader
;
173 QPixmap icon
= iconLoader
.loadIcon("exec",
175 K3Icon::SizeEnormous
);
176 m_preview
->setPixmap(icon
);
177 m_name
->setText(i18n("%1 items selected", selectedItems
.count()));
178 } else if (!applyBookmark(file
)) {
179 // try to get a preview pixmap from the item...
183 m_pendingPreview
= true;
184 m_preview
->setPixmap(QPixmap());
186 KIO::PreviewJob
* job
= KIO::filePreview(list
,
188 K3Icon::SizeEnormous
,
193 job
->setIgnoreMaximumSize(true);
195 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
196 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
197 connect(job
, SIGNAL(failed(const KFileItem
&)),
198 this, SLOT(showIcon(const KFileItem
&)));
201 text
.append(file
.fileName());
203 m_name
->setText(text
);
209 void InfoSidebarPage::slotTimeout()
211 m_shownUrl
= m_urlCandidate
;
215 void InfoSidebarPage::showIcon(const KFileItem
& item
)
217 m_pendingPreview
= false;
218 if (!applyBookmark(item
.url())) {
219 m_preview
->setPixmap(item
.pixmap(K3Icon::SizeEnormous
));
223 void InfoSidebarPage::showPreview(const KFileItem
& item
,
224 const QPixmap
& pixmap
)
227 if (m_pendingPreview
) {
228 m_preview
->setPixmap(pixmap
);
229 m_pendingPreview
= false;
233 bool InfoSidebarPage::applyBookmark(const KUrl
& url
)
235 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
236 int count
= placesModel
->rowCount();
238 for (int i
= 0; i
< count
; ++i
) {
239 QModelIndex index
= placesModel
->index(i
, 0);
241 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
243 text
.append(placesModel
->text(index
));
245 m_name
->setText(text
);
247 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
255 void InfoSidebarPage::cancelRequest()
258 m_pendingPreview
= false;
261 void InfoSidebarPage::createMetaInfo()
264 const KFileItemList
& selectedItems
= selection();
265 if (selectedItems
.size() == 0) {
266 KFileItem
fileItem(S_IFDIR
, KFileItem::Unknown
, m_shownUrl
);
269 if (fileItem
.isDir()) {
270 addInfoLine(i18n("Type:"), i18n("Directory"));
272 if (MetaDataWidget::metaDataAvailable()) {
273 m_metadataWidget
->setFile(fileItem
.url());
275 } else if (selectedItems
.count() == 1) {
276 KFileItem
* fileItem
= selectedItems
.at(0);
277 addInfoLine(i18n("Type:"), fileItem
->mimeComment());
279 QString
sizeText(KIO::convertSize(fileItem
->size()));
280 addInfoLine(i18n("Size:"), sizeText
);
281 addInfoLine(i18n("Modified:"), fileItem
->timeString());
283 const KFileMetaInfo
& metaInfo
= fileItem
->metaInfo();
284 if (metaInfo
.isValid()) {
285 QStringList keys
= metaInfo
.supportedKeys();
286 for (QStringList::Iterator it
= keys
.begin(); it
!= keys
.end(); ++it
) {
287 if (showMetaInfo(*it
)) {
288 KFileMetaInfoItem metaInfoItem
= metaInfo
.item(*it
);
289 addInfoLine(*it
, metaInfoItem
.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 totSize
= 0;
302 foreach(KFileItem
* item
, selectedItems
) {
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/>";
361 m_infoLines
+= QString("<b>%1</b> %2").arg(labelText
).arg(infoText
);
364 #include "infosidebarpage.moc"