]> cloud.milkyroute.net Git - dolphin.git/blob - src/infosidebarpage.cpp
some cleanups for the sidebar pages (move protected members to private section etc.)
[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::requestDelayedItemInfo(const KUrl& url)
134 {
135 cancelRequest();
136
137 if (!url.isEmpty() && !m_multipleSelection) {
138 m_urlCandidate = url;
139 m_timer->setSingleShot(true);
140 m_timer->start(300);
141 }
142 }
143
144 void InfoSidebarPage::showItemInfo()
145 {
146 cancelRequest();
147
148 const KFileItemList& selectedItems = selection();
149
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 const KFileItemList& selectedItems = selection();
250 if (selectedItems.size() == 0) {
251 KFileItem fileItem(S_IFDIR, KFileItem::Unknown, m_shownUrl);
252 fileItem.refresh();
253
254 if (fileItem.isDir()) {
255 addInfoLine(i18n("Type:"), i18n("Directory"));
256 }
257 if (MetaDataWidget::metaDataAvailable()) {
258 m_metadataWidget->setFile(fileItem.url());
259 }
260 } else if (selectedItems.count() == 1) {
261 KFileItem* fileItem = selectedItems.at(0);
262 addInfoLine(i18n("Type:"), fileItem->mimeComment());
263
264 QString sizeText(KIO::convertSize(fileItem->size()));
265 addInfoLine(i18n("Size:"), sizeText);
266 addInfoLine(i18n("Modified:"), fileItem->timeString());
267
268 const KFileMetaInfo& metaInfo = fileItem->metaInfo();
269 if (metaInfo.isValid()) {
270 QStringList keys = metaInfo.supportedKeys();
271 for (QStringList::Iterator it = keys.begin(); it != keys.end(); ++it) {
272 if (showMetaInfo(*it)) {
273 KFileMetaInfoItem metaInfoItem = metaInfo.item(*it);
274 addInfoLine(*it, metaInfoItem.value().toString());
275 }
276 }
277 }
278 if (MetaDataWidget::metaDataAvailable()) {
279 m_metadataWidget->setFile(fileItem->url());
280 }
281 } else {
282 if (MetaDataWidget::metaDataAvailable()) {
283 m_metadataWidget->setFiles(selectedItems.urlList());
284 }
285
286 unsigned long int totSize = 0;
287 foreach(KFileItem* item, selectedItems) {
288 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
289 }
290 addInfoLine(i18n("Total size:"), KIO::convertSize(totSize));
291 }
292 endInfoLines();
293 }
294
295 void InfoSidebarPage::beginInfoLines()
296 {
297 m_infoLines = QString();
298 }
299
300 void InfoSidebarPage::endInfoLines()
301 {
302 m_infos->setText(m_infoLines);
303 }
304
305 bool InfoSidebarPage::showMetaInfo(const QString& key) const
306 {
307 // sorted list of keys, where it's data should be shown
308 static const char* keys[] = {
309 "Album",
310 "Artist",
311 "Author",
312 "Bitrate",
313 "Date",
314 "Dimensions",
315 "Genre",
316 "Length",
317 "Lines",
318 "Pages",
319 "Title",
320 "Words"
321 };
322
323 // do a binary search for the key...
324 int top = 0;
325 int bottom = sizeof(keys) / sizeof(char*) - 1;
326 while (top < bottom) {
327 const int middle = (top + bottom) / 2;
328 const int result = key.compare(keys[middle]);
329 if (result < 0) {
330 bottom = middle - 1;
331 } else if (result > 0) {
332 top = middle + 1;
333 } else {
334 return true;
335 }
336 }
337
338 return false;
339 }
340
341 void InfoSidebarPage::addInfoLine(const QString& labelText, const QString& infoText)
342 {
343 if (!m_infoLines.isEmpty()) {
344 m_infoLines += "<br/>";
345 }
346 m_infoLines += QString("<b>%1</b> %2").arg(labelText).arg(infoText);
347 }
348
349 #include "infosidebarpage.moc"