]> cloud.milkyroute.net Git - dolphin.git/blob - src/infosidebarpage.cpp
EBN fixes + minor coding guideline fixes (thanks to Max Blazejak!).
[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 "infosidebarpage.h"
21
22 #include <config-kmetadata.h>
23
24 #include <kfileplacesmodel.h>
25 #include <klocale.h>
26 #include <kstandarddirs.h>
27 #include <kio/previewjob.h>
28 #include <kfileitem.h>
29 #include <kdialog.h>
30 #include <kglobalsettings.h>
31 #include <kfilemetainfo.h>
32 #include <kvbox.h>
33 #include <kseparator.h>
34 #include <kiconloader.h>
35
36 #include <QDir>
37 #include <QEvent>
38 #include <QFontMetrics>
39 #include <QInputDialog>
40 #include <QLabel>
41 #include <QLayout>
42 #include <QMenu>
43 #include <QPainter>
44 #include <QPixmap>
45 #include <QPushButton>
46 #include <QResizeEvent>
47 #include <QTimer>
48
49 #include "dolphinsettings.h"
50 #include "metadatawidget.h"
51 #include "pixmapviewer.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 if (event->spontaneous()) {
143 return;
144 }
145 showItemInfo();
146 }
147
148 void InfoSidebarPage::resizeEvent(QResizeEvent* event)
149 {
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);
156 }
157
158 void InfoSidebarPage::showItemInfo()
159 {
160 if (!isVisible()) {
161 return;
162 }
163
164 cancelRequest();
165
166 const KFileItemList& selectedItems = selection();
167
168 KUrl file;
169 if (selectedItems.count() == 0) {
170 file = m_shownUrl;
171 } else {
172 file = selectedItems[0]->url();
173 }
174 if (m_multipleSelection) {
175 KIconLoader iconLoader;
176 QPixmap icon = iconLoader.loadIcon("exec",
177 K3Icon::NoGroup,
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...
183 KUrl::List list;
184 list.append(file);
185
186 m_pendingPreview = true;
187 m_preview->setPixmap(QPixmap());
188
189 KIO::PreviewJob* job = KIO::filePreview(list,
190 m_preview->width(),
191 K3Icon::SizeEnormous,
192 0,
193 0,
194 true,
195 false);
196 job->setIgnoreMaximumSize(true);
197
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&)));
202
203 QString text("<b>");
204 text.append(file.fileName());
205 text.append("</b>");
206 m_name->setText(text);
207 }
208
209 createMetaInfo();
210 }
211
212 void InfoSidebarPage::slotTimeout()
213 {
214 m_shownUrl = m_urlCandidate;
215 showItemInfo();
216 }
217
218 void InfoSidebarPage::showIcon(const KFileItem& item)
219 {
220 m_pendingPreview = false;
221 if (!applyBookmark(item.url())) {
222 m_preview->setPixmap(item.pixmap(K3Icon::SizeEnormous));
223 }
224 }
225
226 void InfoSidebarPage::showPreview(const KFileItem& item,
227 const QPixmap& pixmap)
228 {
229 Q_UNUSED(item);
230 if (m_pendingPreview) {
231 m_preview->setPixmap(pixmap);
232 m_pendingPreview = false;
233 }
234 }
235
236 bool InfoSidebarPage::applyBookmark(const KUrl& url)
237 {
238 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
239 int count = placesModel->rowCount();
240
241 for (int i = 0; i < count; ++i) {
242 QModelIndex index = placesModel->index(i, 0);
243
244 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
245 QString text("<b>");
246 text.append(placesModel->text(index));
247 text.append("</b>");
248 m_name->setText(text);
249
250 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
251 return true;
252 }
253 }
254
255 return false;
256 }
257
258 void InfoSidebarPage::cancelRequest()
259 {
260 m_timer->stop();
261 m_pendingPreview = false;
262 }
263
264 void InfoSidebarPage::createMetaInfo()
265 {
266 beginInfoLines();
267 const KFileItemList& selectedItems = selection();
268 if (selectedItems.size() == 0) {
269 KFileItem fileItem(S_IFDIR, KFileItem::Unknown, m_shownUrl);
270 fileItem.refresh();
271
272 if (fileItem.isDir()) {
273 addInfoLine(i18n("Type:"), i18n("Directory"));
274 }
275 if (MetaDataWidget::metaDataAvailable()) {
276 m_metadataWidget->setFile(fileItem.url());
277 }
278 } else if (selectedItems.count() == 1) {
279 KFileItem* fileItem = selectedItems.at(0);
280 addInfoLine(i18n("Type:"), fileItem->mimeComment());
281
282 QString sizeText(KIO::convertSize(fileItem->size()));
283 addInfoLine(i18n("Size:"), sizeText);
284 addInfoLine(i18n("Modified:"), fileItem->timeString());
285
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());
293 }
294 }
295 }
296 if (MetaDataWidget::metaDataAvailable()) {
297 m_metadataWidget->setFile(fileItem->url());
298 }
299 } else {
300 if (MetaDataWidget::metaDataAvailable()) {
301 m_metadataWidget->setFiles(selectedItems.urlList());
302 }
303
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
307 }
308 addInfoLine(i18n("Total size:"), KIO::convertSize(totSize));
309 }
310 endInfoLines();
311 }
312
313 void InfoSidebarPage::beginInfoLines()
314 {
315 m_infoLines = QString();
316 }
317
318 void InfoSidebarPage::endInfoLines()
319 {
320 m_infos->setText(m_infoLines);
321 }
322
323 bool InfoSidebarPage::showMetaInfo(const QString& key) const
324 {
325 // sorted list of keys, where it's data should be shown
326 static const char* keys[] = {
327 "Album",
328 "Artist",
329 "Author",
330 "Bitrate",
331 "Date",
332 "Dimensions",
333 "Genre",
334 "Length",
335 "Lines",
336 "Pages",
337 "Title",
338 "Words"
339 };
340
341 // do a binary search for the key...
342 int top = 0;
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]);
347 if (result < 0) {
348 bottom = middle - 1;
349 } else if (result > 0) {
350 top = middle + 1;
351 } else {
352 return true;
353 }
354 }
355
356 return false;
357 }
358
359 void InfoSidebarPage::addInfoLine(const QString& labelText, const QString& infoText)
360 {
361 if (!m_infoLines.isEmpty()) {
362 m_infoLines += "<br/>";
363 }
364 m_infoLines += QString("<b>%1</b> %2").arg(labelText).arg(infoText);
365 }
366
367 #include "infosidebarpage.moc"