]> cloud.milkyroute.net Git - dolphin.git/blob - src/infosidebarpage.cpp
let the information sidebar react on selection changes
[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_currentSelection(KFileItemList()),
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 QFontMetrics fontMetrics(m_name->font());
82 m_name->setMinimumHeight(fontMetrics.height() * 3);
83 m_name->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum);
84
85 KSeparator* sep1 = new KSeparator(this);
86
87 // general information
88 m_infos = new QLabel(this);
89 m_infos->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
90 m_infos->setTextFormat(Qt::RichText);
91
92 KSeparator* sep2 = new KSeparator(this);
93
94 if (MetaDataWidget::metaDataAvailable()) {
95 m_metadataWidget = new MetaDataWidget(this);
96 }
97
98 layout->addItem(new QSpacerItem(spacing, spacing, QSizePolicy::Preferred, QSizePolicy::Fixed));
99 layout->addWidget(m_preview);
100 layout->addWidget(m_name);
101 layout->addWidget(sep1);
102 layout->addWidget(m_infos);
103 layout->addWidget(sep2);
104 if (m_metadataWidget) {
105 layout->addWidget(m_metadataWidget);
106 layout->addWidget(new KSeparator(this));
107 }
108 // ensure that widgets in the information side bar are aligned towards the top
109 layout->addStretch(1);
110 setLayout(layout);
111 }
112
113 InfoSidebarPage::~InfoSidebarPage()
114 {
115 }
116
117 void InfoSidebarPage::setUrl(const KUrl& url)
118 {
119 if (!m_shownUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
120 cancelRequest();
121 m_shownUrl = url;
122 showItemInfo();
123 }
124 }
125
126 void InfoSidebarPage::setSelection(const KFileItemList& selection)
127 {
128 cancelRequest();
129 m_currentSelection = selection;
130 m_multipleSelection = (m_currentSelection.size() > 1);
131 showItemInfo();
132 }
133
134 void InfoSidebarPage::requestDelayedItemInfo(const KUrl& url)
135 {
136 cancelRequest();
137
138 if (!url.isEmpty() && !m_multipleSelection) {
139 m_urlCandidate = url;
140 m_timer->setSingleShot(true);
141 m_timer->start(300);
142 }
143 }
144
145 void InfoSidebarPage::showItemInfo()
146 {
147 cancelRequest();
148
149 KFileItemList selectedItems = m_currentSelection;
150 KUrl file;
151 if (selectedItems.count() == 0) {
152 file = m_shownUrl;
153 } else {
154 file = selectedItems[0]->url();
155 }
156 if (m_multipleSelection) {
157 KIconLoader iconLoader;
158 QPixmap icon = iconLoader.loadIcon("exec",
159 K3Icon::NoGroup,
160 K3Icon::SizeEnormous);
161 m_preview->setPixmap(icon);
162 m_name->setText(i18n("%1 items selected", selectedItems.count()));
163 } else if (!applyBookmark(file)) {
164 // try to get a preview pixmap from the item...
165 KUrl::List list;
166 list.append(file);
167
168 m_pendingPreview = true;
169 m_preview->setPixmap(QPixmap());
170
171 KIO::PreviewJob* job = KIO::filePreview(list,
172 m_preview->width(),
173 K3Icon::SizeEnormous,
174 0,
175 0,
176 true,
177 false);
178 job->setIgnoreMaximumSize(true);
179
180 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
181 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
182 connect(job, SIGNAL(failed(const KFileItem&)),
183 this, SLOT(showIcon(const KFileItem&)));
184
185 QString text("<b>");
186 text.append(file.fileName());
187 text.append("</b>");
188 m_name->setText(text);
189 }
190
191 createMetaInfo();
192 }
193
194 void InfoSidebarPage::slotTimeout()
195 {
196 m_shownUrl = m_urlCandidate;
197 showItemInfo();
198 }
199
200 void InfoSidebarPage::showIcon(const KFileItem& item)
201 {
202 m_pendingPreview = false;
203 if (!applyBookmark(item.url())) {
204 m_preview->setPixmap(item.pixmap(K3Icon::SizeEnormous));
205 }
206 }
207
208 void InfoSidebarPage::showPreview(const KFileItem& item,
209 const QPixmap& pixmap)
210 {
211 Q_UNUSED(item);
212 if (m_pendingPreview) {
213 m_preview->setPixmap(pixmap);
214 m_pendingPreview = false;
215 }
216 }
217
218 bool InfoSidebarPage::applyBookmark(const KUrl& url)
219 {
220 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
221 int count = placesModel->rowCount();
222
223 for (int i = 0; i < count; ++i) {
224 QModelIndex index = placesModel->index(i, 0);
225
226 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
227 QString text("<b>");
228 text.append(placesModel->text(index));
229 text.append("</b>");
230 m_name->setText(text);
231
232 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
233 return true;
234 }
235 }
236
237 return false;
238 }
239
240 void InfoSidebarPage::cancelRequest()
241 {
242 m_timer->stop();
243 m_pendingPreview = false;
244 }
245
246 void InfoSidebarPage::createMetaInfo()
247 {
248 beginInfoLines();
249 if (m_currentSelection.size() == 0) {
250 KFileItem fileItem(S_IFDIR, KFileItem::Unknown, m_shownUrl);
251 fileItem.refresh();
252
253 if (fileItem.isDir()) {
254 addInfoLine(i18n("Type:"), i18n("Directory"));
255 }
256 if (MetaDataWidget::metaDataAvailable()) {
257 m_metadataWidget->setFile(fileItem.url());
258 }
259 } else if (m_currentSelection.count() == 1) {
260 KFileItem* fileItem = m_currentSelection.at(0);
261 addInfoLine(i18n("Type:"), fileItem->mimeComment());
262
263 QString sizeText(KIO::convertSize(fileItem->size()));
264 addInfoLine(i18n("Size:"), sizeText);
265 addInfoLine(i18n("Modified:"), fileItem->timeString());
266
267 const KFileMetaInfo& metaInfo = fileItem->metaInfo();
268 if (metaInfo.isValid()) {
269 QStringList keys = metaInfo.supportedKeys();
270 for (QStringList::Iterator it = keys.begin(); it != keys.end(); ++it) {
271 if (showMetaInfo(*it)) {
272 KFileMetaInfoItem metaInfoItem = metaInfo.item(*it);
273 addInfoLine(*it, metaInfoItem.value().toString());
274 }
275 }
276 }
277 if (MetaDataWidget::metaDataAvailable()) {
278 m_metadataWidget->setFile(fileItem->url());
279 }
280 } else {
281 if (MetaDataWidget::metaDataAvailable()) {
282 m_metadataWidget->setFiles(m_currentSelection.urlList());
283 }
284
285 unsigned long int totSize = 0;
286 foreach(KFileItem* item, m_currentSelection) {
287 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
288 }
289 addInfoLine(i18n("Total size:"), KIO::convertSize(totSize));
290 }
291 endInfoLines();
292 }
293
294 void InfoSidebarPage::beginInfoLines()
295 {
296 m_infoLines = QString();
297 }
298
299 void InfoSidebarPage::endInfoLines()
300 {
301 m_infos->setText(m_infoLines);
302 }
303
304 bool InfoSidebarPage::showMetaInfo(const QString& key) const
305 {
306 // sorted list of keys, where it's data should be shown
307 static const char* keys[] = {
308 "Album",
309 "Artist",
310 "Author",
311 "Bitrate",
312 "Date",
313 "Dimensions",
314 "Genre",
315 "Length",
316 "Lines",
317 "Pages",
318 "Title",
319 "Words"
320 };
321
322 // do a binary search for the key...
323 int top = 0;
324 int bottom = sizeof(keys) / sizeof(char*) - 1;
325 while (top < bottom) {
326 const int middle = (top + bottom) / 2;
327 const int result = key.compare(keys[middle]);
328 if (result < 0) {
329 bottom = middle - 1;
330 } else if (result > 0) {
331 top = middle + 1;
332 } else {
333 return true;
334 }
335 }
336
337 return false;
338 }
339
340 void InfoSidebarPage::addInfoLine(const QString& labelText, const QString& infoText)
341 {
342 if (!m_infoLines.isEmpty()) {
343 m_infoLines += "<br/>";
344 }
345 m_infoLines += QString("<b>%1</b> %2").arg(labelText).arg(infoText);
346 }
347
348 #include "infosidebarpage.moc"