]> cloud.milkyroute.net Git - dolphin.git/blob - src/infosidebarpage.cpp
information sidebar fixes:
[dolphin.git] / src / infosidebarpage.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include <config-kmetadata.h>
21
22 #include "infosidebarpage.h"
23
24 #include <QDir>
25 #include <QEvent>
26 #include <QFontMetrics>
27 #include <QInputDialog>
28 #include <QLabel>
29 #include <QLayout>
30 #include <QMenu>
31 #include <QPainter>
32 #include <QPixmap>
33 #include <QPushButton>
34 #include <QResizeEvent>
35 #include <QTimer>
36
37 #include <kfileplacesmodel.h>
38 #include <klocale.h>
39 #include <kstandarddirs.h>
40 #include <kio/previewjob.h>
41 #include <kfileitem.h>
42 #include <kdialog.h>
43 #include <kglobalsettings.h>
44 #include <kfilemetainfo.h>
45 #include <kvbox.h>
46 #include <kseparator.h>
47 #include <kiconloader.h>
48
49 #include "pixmapviewer.h"
50 #include "dolphinsettings.h"
51 #include "metadatawidget.h"
52
53 InfoSidebarPage::InfoSidebarPage(QWidget* parent) :
54 SidebarPage(parent),
55 m_multipleSelection(false), //TODO: check if I'm needed
56 m_pendingPreview(false),
57 m_timer(0),
58 m_preview(0),
59 m_name(0),
60 m_infos(0),
61 m_metadataWidget(0)
62 {
63 const int spacing = KDialog::spacingHint();
64
65 m_timer = new QTimer(this);
66 connect(m_timer, SIGNAL(timeout()),
67 this, SLOT(slotTimeout()));
68
69 QVBoxLayout* layout = new QVBoxLayout;
70 layout->setSpacing(spacing);
71
72 // preview
73 m_preview = new PixmapViewer(this);
74 m_preview->setMinimumWidth(K3Icon::SizeEnormous);
75 m_preview->setFixedHeight(K3Icon::SizeEnormous);
76
77 // name
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);
82
83 // general information
84 m_infos = new QLabel(this);
85 m_infos->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
86 m_infos->setTextFormat(Qt::RichText);
87
88 if (MetaDataWidget::metaDataAvailable()) {
89 m_metadataWidget = new MetaDataWidget(this);
90 }
91
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));
101 }
102 // ensure that widgets in the information side bar are aligned towards the top
103 layout->addStretch(1);
104 setLayout(layout);
105 }
106
107 InfoSidebarPage::~InfoSidebarPage()
108 {
109 }
110
111 void InfoSidebarPage::setUrl(const KUrl& url)
112 {
113 if (!m_shownUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
114 cancelRequest();
115 m_shownUrl = url;
116 showItemInfo();
117 }
118 }
119
120 void InfoSidebarPage::setSelection(const KFileItemList& selection)
121 {
122 cancelRequest();
123 SidebarPage::setSelection(selection);
124 m_multipleSelection = (selection.size() > 1);
125 showItemInfo();
126 }
127
128 void InfoSidebarPage::requestDelayedItemInfo(const KUrl& url)
129 {
130 cancelRequest();
131
132 if (!url.isEmpty() && !m_multipleSelection) {
133 m_urlCandidate = url;
134 m_timer->setSingleShot(true);
135 m_timer->start(300);
136 }
137 }
138
139 void InfoSidebarPage::showEvent(QShowEvent* event)
140 {
141 SidebarPage::showEvent(event);
142 showItemInfo();
143 }
144
145 void InfoSidebarPage::resizeEvent(QResizeEvent* event)
146 {
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);
153 }
154
155 void InfoSidebarPage::showItemInfo()
156 {
157 if (!isVisible()) {
158 return;
159 }
160
161 cancelRequest();
162
163 const KFileItemList& selectedItems = selection();
164
165 KUrl file;
166 if (selectedItems.count() == 0) {
167 file = m_shownUrl;
168 } else {
169 file = selectedItems[0]->url();
170 }
171 if (m_multipleSelection) {
172 KIconLoader iconLoader;
173 QPixmap icon = iconLoader.loadIcon("exec",
174 K3Icon::NoGroup,
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...
180 KUrl::List list;
181 list.append(file);
182
183 m_pendingPreview = true;
184 m_preview->setPixmap(QPixmap());
185
186 KIO::PreviewJob* job = KIO::filePreview(list,
187 m_preview->width(),
188 K3Icon::SizeEnormous,
189 0,
190 0,
191 true,
192 false);
193 job->setIgnoreMaximumSize(true);
194
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&)));
199
200 QString text("<b>");
201 text.append(file.fileName());
202 text.append("</b>");
203 m_name->setText(text);
204 }
205
206 createMetaInfo();
207 }
208
209 void InfoSidebarPage::slotTimeout()
210 {
211 m_shownUrl = m_urlCandidate;
212 showItemInfo();
213 }
214
215 void InfoSidebarPage::showIcon(const KFileItem& item)
216 {
217 m_pendingPreview = false;
218 if (!applyBookmark(item.url())) {
219 m_preview->setPixmap(item.pixmap(K3Icon::SizeEnormous));
220 }
221 }
222
223 void InfoSidebarPage::showPreview(const KFileItem& item,
224 const QPixmap& pixmap)
225 {
226 Q_UNUSED(item);
227 if (m_pendingPreview) {
228 m_preview->setPixmap(pixmap);
229 m_pendingPreview = false;
230 }
231 }
232
233 bool InfoSidebarPage::applyBookmark(const KUrl& url)
234 {
235 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
236 int count = placesModel->rowCount();
237
238 for (int i = 0; i < count; ++i) {
239 QModelIndex index = placesModel->index(i, 0);
240
241 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
242 QString text("<b>");
243 text.append(placesModel->text(index));
244 text.append("</b>");
245 m_name->setText(text);
246
247 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
248 return true;
249 }
250 }
251
252 return false;
253 }
254
255 void InfoSidebarPage::cancelRequest()
256 {
257 m_timer->stop();
258 m_pendingPreview = false;
259 }
260
261 void InfoSidebarPage::createMetaInfo()
262 {
263 beginInfoLines();
264 const KFileItemList& selectedItems = selection();
265 if (selectedItems.size() == 0) {
266 KFileItem fileItem(S_IFDIR, KFileItem::Unknown, m_shownUrl);
267 fileItem.refresh();
268
269 if (fileItem.isDir()) {
270 addInfoLine(i18n("Type:"), i18n("Directory"));
271 }
272 if (MetaDataWidget::metaDataAvailable()) {
273 m_metadataWidget->setFile(fileItem.url());
274 }
275 } else if (selectedItems.count() == 1) {
276 KFileItem* fileItem = selectedItems.at(0);
277 addInfoLine(i18n("Type:"), fileItem->mimeComment());
278
279 QString sizeText(KIO::convertSize(fileItem->size()));
280 addInfoLine(i18n("Size:"), sizeText);
281 addInfoLine(i18n("Modified:"), fileItem->timeString());
282
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());
290 }
291 }
292 }
293 if (MetaDataWidget::metaDataAvailable()) {
294 m_metadataWidget->setFile(fileItem->url());
295 }
296 } else {
297 if (MetaDataWidget::metaDataAvailable()) {
298 m_metadataWidget->setFiles(selectedItems.urlList());
299 }
300
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
304 }
305 addInfoLine(i18n("Total size:"), KIO::convertSize(totSize));
306 }
307 endInfoLines();
308 }
309
310 void InfoSidebarPage::beginInfoLines()
311 {
312 m_infoLines = QString();
313 }
314
315 void InfoSidebarPage::endInfoLines()
316 {
317 m_infos->setText(m_infoLines);
318 }
319
320 bool InfoSidebarPage::showMetaInfo(const QString& key) const
321 {
322 // sorted list of keys, where it's data should be shown
323 static const char* keys[] = {
324 "Album",
325 "Artist",
326 "Author",
327 "Bitrate",
328 "Date",
329 "Dimensions",
330 "Genre",
331 "Length",
332 "Lines",
333 "Pages",
334 "Title",
335 "Words"
336 };
337
338 // do a binary search for the key...
339 int top = 0;
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]);
344 if (result < 0) {
345 bottom = middle - 1;
346 } else if (result > 0) {
347 top = middle + 1;
348 } else {
349 return true;
350 }
351 }
352
353 return false;
354 }
355
356 void InfoSidebarPage::addInfoLine(const QString& labelText, const QString& infoText)
357 {
358 if (!m_infoLines.isEmpty()) {
359 m_infoLines += "<br/>";
360 }
361 m_infoLines += QString("<b>%1</b> %2").arg(labelText).arg(infoText);
362 }
363
364 #include "infosidebarpage.moc"