]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/metadatawidget.cpp
Start of refactoring so that the metadata widget can be moved outside of Dolphin...
[dolphin.git] / src / panels / information / metadatawidget.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 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 "metadatawidget.h"
21
22 #include <kfileitem.h>
23 #include <klocale.h>
24
25 #include <config-nepomuk.h>
26 #ifdef HAVE_NEPOMUK
27 #include "commentwidget_p.h"
28 #include "nepomukmassupdatejob_p.h"
29 #include "taggingwidget_p.h"
30 #endif
31
32 #include <nepomuk/kratingwidget.h>
33
34 #include <QGridLayout>
35 #include <QLabel>
36 #include <QList>
37 #include <QString>
38
39 class MetaDataWidget::Private
40 {
41 public:
42 struct Row
43 {
44 QLabel* label;
45 QWidget* infoWidget;
46 };
47
48 Private(MetaDataWidget* parent);
49 ~Private();
50
51 void addRow(QLabel* label, QWidget* infoWidget);
52 void setRowVisible(QWidget* infoWidget, bool visible);
53
54 QList<Row> m_rows;
55
56 QGridLayout* m_gridLayout;
57
58 QLabel* m_typeInfo;
59 QLabel* m_sizeLabel;
60 QLabel* m_sizeInfo;
61 QLabel* m_modifiedInfo;
62 QLabel* m_ownerInfo;
63 QLabel* m_permissionsInfo;
64 #ifdef HAVE_NEPOMUK
65 KRatingWidget* m_ratingWidget;
66 TaggingWidget* m_taggingWidget;
67 CommentWidget* m_commentWidget;
68 #endif
69
70 private:
71 MetaDataWidget* const q;
72 };
73
74 MetaDataWidget::Private::Private(MetaDataWidget* parent) :
75 m_rows(),
76 m_gridLayout(0),
77 m_typeInfo(0),
78 m_sizeLabel(0),
79 m_sizeInfo(0),
80 m_modifiedInfo(0),
81 m_ownerInfo(0),
82 m_permissionsInfo(0),
83 #ifdef HAVE_NEPOMUK
84 m_ratingWidget(0),
85 m_taggingWidget(0),
86 m_commentWidget(0),
87 #endif
88 q(parent)
89 {
90 m_gridLayout = new QGridLayout(parent);
91
92 m_typeInfo = new QLabel(parent);
93 m_sizeLabel = new QLabel(parent);
94 m_sizeInfo = new QLabel(parent);
95 m_modifiedInfo = new QLabel(parent);
96 m_ownerInfo = new QLabel(parent);
97 m_permissionsInfo = new QLabel(parent);
98 #ifdef HAVE_NEPOMUK
99 m_ratingWidget = new KRatingWidget(parent);
100 m_taggingWidget = new TaggingWidget(parent);
101 m_commentWidget = new CommentWidget(parent);
102 #endif
103
104 addRow(new QLabel(i18nc("@label", "Type:"), parent), m_typeInfo);
105 addRow(m_sizeLabel, m_sizeInfo);
106 addRow(new QLabel(i18nc("@label", "Modified:"), parent), m_modifiedInfo);
107 addRow(new QLabel(i18nc("@label", "Owner:"), parent), m_ownerInfo);
108 addRow(new QLabel(i18nc("@label", "Permissions:"), parent), m_permissionsInfo);
109 #ifdef HAVE_NEPOMUK
110 addRow(new QLabel(i18nc("@label", "Rating:"), parent), m_ratingWidget);
111 addRow(new QLabel(i18nc("@label", "Tags:"), parent), m_taggingWidget);
112 addRow(new QLabel(i18nc("@label", "Comment:"), parent), m_commentWidget);
113 #endif
114 }
115
116 MetaDataWidget::Private::~Private()
117 {
118 }
119
120 void MetaDataWidget::Private::addRow(QLabel* label, QWidget* infoWidget)
121 {
122 Row row;
123 row.label = label;
124 row.infoWidget = infoWidget;
125 m_rows.append(row);
126
127 QPalette palette = label->palette();
128 QColor textColor = palette.color(QPalette::Text);
129 textColor.setAlpha(128);
130 palette.setColor(QPalette::WindowText, textColor);
131 label->setPalette(palette);
132
133 const int rowIndex = m_rows.count();
134 m_gridLayout->addWidget(label, rowIndex, 0, Qt::AlignLeft);
135 m_gridLayout->addWidget(infoWidget, rowIndex, 1, Qt::AlignRight);
136 }
137
138 void MetaDataWidget::Private::setRowVisible(QWidget* infoWidget, bool visible)
139 {
140 foreach (const Row& row, m_rows) {
141 if (row.infoWidget == infoWidget) {
142 row.label->setVisible(visible);
143 row.infoWidget->setVisible(visible);
144 return;
145 }
146 }
147 }
148
149
150 MetaDataWidget::MetaDataWidget(QWidget* parent) :
151 QWidget(parent),
152 d(new Private(this))
153 {
154 }
155
156 MetaDataWidget::~MetaDataWidget()
157 {
158 delete d;
159 }
160
161 void MetaDataWidget::setItem(const KFileItem& item)
162 {
163 // update values for "type", "size", "modified",
164 // "owner" and "permissions" synchronously
165 d->m_sizeLabel->setText(i18nc("@label", "Size:"));
166 if (item.isDir()) {
167 d->m_typeInfo->setText(i18nc("@label", "Folder"));
168 d->setRowVisible(d->m_sizeInfo, false);
169 } else {
170 d->m_typeInfo->setText(item.mimeComment());
171 d->m_sizeInfo->setText(KIO::convertSize(item.size()));
172 d->setRowVisible(d->m_sizeInfo, true);
173 }
174 d->m_modifiedInfo->setText(item.timeString());
175 d->m_ownerInfo->setText(item.user());
176 d->m_permissionsInfo->setText(item.permissionsString());
177
178 setItems(KFileItemList() << item);
179 }
180
181 void MetaDataWidget::setItems(const KFileItemList& items)
182 {
183 if (items.count() > 1) {
184 // calculate the size of all items and show this
185 // information to the user
186 d->m_sizeLabel->setText(i18nc("@label", "Total Size:"));
187 d->setRowVisible(d->m_sizeInfo, true);
188
189 quint64 totalSize = 0;
190 foreach (const KFileItem& item, items) {
191 if (!item.isDir() && !item.isLink()) {
192 totalSize += item.size();
193 }
194 }
195 d->m_sizeInfo->setText(KIO::convertSize(totalSize));
196 }
197 }
198
199 #include "metadatawidget.moc"