2 Copyright (c) 2008 Volker Krause <vkrause@kde.org>
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 #include "collectionattributespage.h"
22 #include <AkonadiCore/attributefactory.h>
23 #include <AkonadiCore/collection.h>
25 #include <KLocalizedString>
27 #include <QStandardItemModel>
29 using namespace Akonadi
;
31 CollectionAttributePage::CollectionAttributePage(QWidget
*parent
) :
32 CollectionPropertiesPage(parent
),
35 setPageTitle(i18n("Attributes"));
38 connect(ui
.addButton
, &QPushButton::clicked
, this, &CollectionAttributePage::addAttribute
);
39 connect(ui
.deleteButton
, &QPushButton::clicked
, this, &CollectionAttributePage::delAttribute
);
42 void CollectionAttributePage::load(const Collection
&col
)
44 Attribute::List list
= col
.attributes();
45 mModel
= new QStandardItemModel(list
.count(), 2);
47 labels
<< i18n("Attribute") << i18n("Value");
48 mModel
->setHorizontalHeaderLabels(labels
);
50 for (int i
= 0; i
< list
.count(); ++i
) {
51 QModelIndex index
= mModel
->index(i
, 0);
52 Q_ASSERT(index
.isValid());
53 mModel
->setData(index
, QString::fromLatin1(list
.at(i
)->type()));
54 mModel
->item(i
, 0)->setEditable(false);
55 index
= mModel
->index(i
, 1);
56 Q_ASSERT(index
.isValid());
57 mModel
->setData(index
, QString::fromLatin1(list
.at(i
)->serialized()));
58 mModel
->itemFromIndex(index
)->setFlags(Qt::ItemIsEditable
| mModel
->flags(index
));
60 ui
.attrView
->setModel(mModel
);
61 connect(mModel
, &QStandardItemModel::itemChanged
, this, &CollectionAttributePage::attributeChanged
);
64 void CollectionAttributePage::save(Collection
&col
)
66 foreach (const QString
&del
, mDeleted
) {
67 col
.removeAttribute(del
.toLatin1());
69 for (int i
= 0; i
< mModel
->rowCount(); ++i
) {
70 const QModelIndex typeIndex
= mModel
->index(i
, 0);
71 Q_ASSERT(typeIndex
.isValid());
72 if (!mChanged
.contains(typeIndex
.data().toString())) {
75 const QModelIndex valueIndex
= mModel
->index(i
, 1);
76 Q_ASSERT(valueIndex
.isValid());
77 Attribute
*attr
= AttributeFactory::createAttribute(mModel
->data(typeIndex
).toString().toLatin1());
79 attr
->deserialize(mModel
->data(valueIndex
).toString().toLatin1());
80 col
.addAttribute(attr
);
84 void CollectionAttributePage::addAttribute()
86 if (ui
.attrName
->text().isEmpty()) {
89 const QString attr
= ui
.attrName
->text();
90 mChanged
.insert(attr
);
91 mDeleted
.remove(attr
);
92 const int row
= mModel
->rowCount();
93 mModel
->insertRow(row
);
94 QModelIndex index
= mModel
->index(row
, 0);
95 Q_ASSERT(index
.isValid());
96 mModel
->setData(index
, attr
);
100 void CollectionAttributePage::delAttribute()
102 QModelIndexList selection
= ui
.attrView
->selectionModel()->selectedRows();
103 if (selection
.count() != 1) {
106 const QString attr
= selection
.first().data().toString();
107 mChanged
.remove(attr
);
108 mDeleted
.insert(attr
);
109 mModel
->removeRow(selection
.first().row());
112 void CollectionAttributePage::attributeChanged(QStandardItem
*item
)
114 const QString attr
= mModel
->data(mModel
->index(item
->row(), 0)).toString();
115 mDeleted
.remove(attr
);
116 mChanged
.insert(attr
);