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