]> cloud.milkyroute.net Git - dolphin.git/blob - src/infosidebarpage.cpp
performance optimization: don't retrieve the meta information immediately when changi...
[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_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 m_timer->setSingleShot(true);
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 (url.isValid() && !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 SidebarPage::setSelection(selection);
123 m_timer->start(TimerDelay);
124 }
125
126 void InfoSidebarPage::requestDelayedItemInfo(const KUrl& url)
127 {
128 cancelRequest();
129
130 if (!url.isEmpty() && (selection().size() <= 1)) {
131 m_urlCandidate = url;
132 m_timer->start(TimerDelay);
133 }
134 }
135
136 void InfoSidebarPage::showEvent(QShowEvent* event)
137 {
138 SidebarPage::showEvent(event);
139 if (event->spontaneous()) {
140 return;
141 }
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 const int itemCount = selectedItems.count();
167 if (selectedItems.count() == 0) {
168 file = m_shownUrl;
169 } else {
170 file = selectedItems[0]->url();
171 }
172 if (itemCount > 1) {
173 KIconLoader iconLoader;
174 QPixmap icon = iconLoader.loadIcon("exec",
175 K3Icon::NoGroup,
176 K3Icon::SizeEnormous);
177 m_preview->setPixmap(icon);
178 m_name->setText(i18n("%1 items selected", selectedItems.count()));
179 } else if (!applyBookmark(file)) {
180 // try to get a preview pixmap from the item...
181 KUrl::List list;
182 list.append(file);
183
184 m_pendingPreview = true;
185 m_preview->setPixmap(QPixmap());
186
187 KIO::PreviewJob* job = KIO::filePreview(list,
188 m_preview->width(),
189 K3Icon::SizeEnormous,
190 0,
191 0,
192 true,
193 false);
194 job->setIgnoreMaximumSize(true);
195
196 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
197 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
198 connect(job, SIGNAL(failed(const KFileItem&)),
199 this, SLOT(showIcon(const KFileItem&)));
200
201 QString text("<b>");
202 text.append(file.fileName());
203 text.append("</b>");
204 m_name->setText(text);
205 }
206
207 createMetaInfo();
208 }
209
210 void InfoSidebarPage::slotTimeout()
211 {
212 m_shownUrl = m_urlCandidate;
213 showItemInfo();
214 }
215
216 void InfoSidebarPage::showIcon(const KFileItem& item)
217 {
218 m_pendingPreview = false;
219 if (!applyBookmark(item.url())) {
220 m_preview->setPixmap(item.pixmap(K3Icon::SizeEnormous));
221 }
222 }
223
224 void InfoSidebarPage::showPreview(const KFileItem& item,
225 const QPixmap& pixmap)
226 {
227 Q_UNUSED(item);
228 if (m_pendingPreview) {
229 m_preview->setPixmap(pixmap);
230 m_pendingPreview = false;
231 }
232 }
233
234 bool InfoSidebarPage::applyBookmark(const KUrl& url)
235 {
236 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
237 int count = placesModel->rowCount();
238
239 for (int i = 0; i < count; ++i) {
240 QModelIndex index = placesModel->index(i, 0);
241
242 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
243 QString text("<b>");
244 text.append(placesModel->text(index));
245 text.append("</b>");
246 m_name->setText(text);
247
248 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
249 return true;
250 }
251 }
252
253 return false;
254 }
255
256 void InfoSidebarPage::cancelRequest()
257 {
258 m_timer->stop();
259 m_pendingPreview = false;
260 }
261
262 void InfoSidebarPage::createMetaInfo()
263 {
264 beginInfoLines();
265 const KFileItemList& selectedItems = selection();
266 if (selectedItems.size() == 0) {
267 KFileItem fileItem(S_IFDIR, KFileItem::Unknown, m_shownUrl);
268 fileItem.refresh();
269
270 if (fileItem.isDir()) {
271 addInfoLine(i18n("Type:"), i18n("Directory"));
272 }
273 if (MetaDataWidget::metaDataAvailable()) {
274 m_metadataWidget->setFile(fileItem.url());
275 }
276 } else if (selectedItems.count() == 1) {
277 KFileItem* fileItem = selectedItems.at(0);
278 addInfoLine(i18n("Type:"), fileItem->mimeComment());
279
280 QString sizeText(KIO::convertSize(fileItem->size()));
281 addInfoLine(i18n("Size:"), sizeText);
282 addInfoLine(i18n("Modified:"), fileItem->timeString());
283
284 const KFileMetaInfo& metaInfo = fileItem->metaInfo();
285 if (metaInfo.isValid()) {
286 QStringList keys = metaInfo.supportedKeys();
287 for (QStringList::Iterator it = keys.begin(); it != keys.end(); ++it) {
288 if (showMetaInfo(*it)) {
289 KFileMetaInfoItem metaInfoItem = metaInfo.item(*it);
290 addInfoLine(*it, metaInfoItem.value().toString());
291 }
292 }
293 }
294 if (MetaDataWidget::metaDataAvailable()) {
295 m_metadataWidget->setFile(fileItem->url());
296 }
297 } else {
298 if (MetaDataWidget::metaDataAvailable()) {
299 m_metadataWidget->setFiles(selectedItems.urlList());
300 }
301
302 unsigned long int totSize = 0;
303 foreach(KFileItem* item, selectedItems) {
304 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 }
306 addInfoLine(i18n("Total size:"), KIO::convertSize(totSize));
307 }
308 endInfoLines();
309 }
310
311 void InfoSidebarPage::beginInfoLines()
312 {
313 m_infoLines = QString();
314 }
315
316 void InfoSidebarPage::endInfoLines()
317 {
318 m_infos->setText(m_infoLines);
319 }
320
321 bool InfoSidebarPage::showMetaInfo(const QString& key) const
322 {
323 // sorted list of keys, where it's data should be shown
324 static const char* keys[] = {
325 "Album",
326 "Artist",
327 "Author",
328 "Bitrate",
329 "Date",
330 "Dimensions",
331 "Genre",
332 "Length",
333 "Lines",
334 "Pages",
335 "Title",
336 "Words"
337 };
338
339 // do a binary search for the key...
340 int top = 0;
341 int bottom = sizeof(keys) / sizeof(char*) - 1;
342 while (top < bottom) {
343 const int middle = (top + bottom) / 2;
344 const int result = key.compare(keys[middle]);
345 if (result < 0) {
346 bottom = middle - 1;
347 } else if (result > 0) {
348 top = middle + 1;
349 } else {
350 return true;
351 }
352 }
353
354 return false;
355 }
356
357 void InfoSidebarPage::addInfoLine(const QString& labelText, const QString& infoText)
358 {
359 if (!m_infoLines.isEmpty()) {
360 m_infoLines += "<br/>";
361 }
362 m_infoLines += QString("<b>%1</b> %2").arg(labelText).arg(infoText);
363 }
364
365 #include "infosidebarpage.moc"