Akonadi Contacts

customfieldsmodel.cpp
1/*
2 This file is part of Contact Editor.
3
4 SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "customfieldsmodel.h"
10
11#include <KLocalizedString>
12
13#include <QDateTime>
14#include <QLocale>
15
16Q_DECLARE_METATYPE(Qt::CheckState)
17using namespace Akonadi;
18CustomFieldsModel::CustomFieldsModel(QObject *parent)
19 : QAbstractItemModel(parent)
20{
21}
22
23CustomFieldsModel::~CustomFieldsModel() = default;
24
25void CustomFieldsModel::setCustomFields(const CustomField::List &customFields)
26{
28
29 mCustomFields = customFields;
30
32}
33
34CustomField::List CustomFieldsModel::customFields() const
35{
36 return mCustomFields;
37}
38
39QModelIndex CustomFieldsModel::index(int row, int column, const QModelIndex &parent) const
40{
41 Q_UNUSED(parent)
42 return createIndex(row, column);
43}
44
46{
47 Q_UNUSED(child)
48 return {};
49}
50
51QVariant CustomFieldsModel::data(const QModelIndex &index, int role) const
52{
53 if (!index.isValid()) {
54 return {};
55 }
56
57 if (index.row() < 0 || index.row() >= mCustomFields.count()) {
58 return {};
59 }
60
61 if (index.column() < 0 || index.column() > 2) {
62 return {};
63 }
64
65 const CustomField &customField = mCustomFields[index.row()];
66
67 if (role == Qt::DisplayRole) {
68 if (index.column() == 0) {
69 return customField.title();
70 } else if (index.column() == 1) {
71 switch (customField.type()) {
72 case CustomField::TextType:
73 case CustomField::NumericType:
74 case CustomField::UrlType:
75 return customField.value();
76 case CustomField::BooleanType:
77 return QString();
78 case CustomField::DateType: {
79 const QDate value = QDate::fromString(customField.value(), Qt::ISODate);
80 return QLocale().toString(value, QLocale::ShortFormat);
81 }
82 case CustomField::TimeType: {
83 const QTime value = QTime::fromString(customField.value(), Qt::ISODate);
84 return QLocale().toString(value);
85 }
86 case CustomField::DateTimeType: {
87 const QDateTime value = QDateTime::fromString(customField.value(), Qt::ISODate);
88 return QLocale().toString(value);
89 }
90 }
91 return customField.value();
92 } else {
93 return customField.key();
94 }
95 }
96
97 if (role == Qt::CheckStateRole) {
98 if (index.column() == 1) {
99 if (customField.type() == CustomField::BooleanType) {
100 return customField.value() == QLatin1StringView("true") ? Qt::Checked : Qt::Unchecked;
101 }
102 }
103 }
104
105 if (role == Qt::EditRole) {
106 if (index.column() == 0) {
107 return customField.title();
108 } else if (index.column() == 1) {
109 return customField.value();
110 } else {
111 return customField.key();
112 }
113 }
114
115 if (role == TypeRole) {
116 return customField.type();
117 }
118
119 if (role == ScopeRole) {
120 return customField.scope();
121 }
122
123 return {};
124}
125
126bool CustomFieldsModel::setData(const QModelIndex &index, const QVariant &value, int role)
127{
128 if (!index.isValid()) {
129 return false;
130 }
131
132 if (index.row() < 0 || index.row() >= mCustomFields.count()) {
133 return false;
134 }
135
136 if (index.column() < 0 || index.column() > 2) {
137 return false;
138 }
139
140 CustomField &customField = mCustomFields[index.row()];
141
142 if (role == Qt::EditRole) {
143 if (index.column() == 0) {
144 customField.setTitle(value.toString());
145 } else if (index.column() == 1) {
146 customField.setValue(value.toString());
147 } else {
148 customField.setKey(value.toString());
149 }
150
151 Q_EMIT dataChanged(index, index);
152 return true;
153 }
154
155 if (role == Qt::CheckStateRole) {
156 if (index.column() == 1) {
157 if (customField.type() == CustomField::BooleanType) {
158 customField.setValue(static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked ? QStringLiteral("true") : QStringLiteral("false"));
159 Q_EMIT dataChanged(index, index);
160 return true;
161 }
162 }
163 }
164
165 if (role == TypeRole) {
166 customField.setType((CustomField::Type)value.toInt());
167 Q_EMIT dataChanged(index, index);
168 return true;
169 }
170
171 if (role == ScopeRole) {
172 customField.setScope((CustomField::Scope)value.toInt());
173 Q_EMIT dataChanged(index, index);
174 return true;
175 }
176
177 return false;
178}
179
180QVariant CustomFieldsModel::headerData(int section, Qt::Orientation orientation, int role) const
181{
182 if (section < 0 || section > 1) {
183 return {};
184 }
185
186 if (orientation != Qt::Horizontal) {
187 return {};
188 }
189
190 if (role != Qt::DisplayRole) {
191 return {};
192 }
193
194 if (section == 0) {
195 return i18nc("custom field title", "Title");
196 } else {
197 return i18nc("custom field value", "Value");
198 }
199}
200
201Qt::ItemFlags CustomFieldsModel::flags(const QModelIndex &index) const
202{
203 if (!index.isValid() || index.row() < 0 || index.row() >= mCustomFields.count()) {
204 return QAbstractItemModel::flags(index);
205 }
206
207 const CustomField &customField = mCustomFields[index.row()];
208
209 const Qt::ItemFlags parentFlags = QAbstractItemModel::flags(index);
210 if ((customField.type() == CustomField::BooleanType) && (index.column() == 1)) {
212 } else {
213 return parentFlags | Qt::ItemIsEnabled | Qt::ItemIsEditable;
214 }
215}
216
217int CustomFieldsModel::columnCount(const QModelIndex &parent) const
218{
219 if (!parent.isValid()) {
220 return 3;
221 } else {
222 return 0;
223 }
224}
225
226int CustomFieldsModel::rowCount(const QModelIndex &parent) const
227{
228 if (!parent.isValid()) {
229 return mCustomFields.count();
230 } else {
231 return 0;
232 }
233}
234
235bool CustomFieldsModel::insertRows(int row, int count, const QModelIndex &parent)
236{
237 if (parent.isValid()) {
238 return false;
239 }
240
241 beginInsertRows(parent, row, row + count - 1);
242 for (int i = 0; i < count; ++i) {
243 mCustomFields.insert(row, CustomField());
244 }
246
247 return true;
248}
249
250bool CustomFieldsModel::removeRows(int row, int count, const QModelIndex &parent)
251{
252 if (parent.isValid()) {
253 return false;
254 }
255
256 beginRemoveRows(parent, row, row + count - 1);
257 for (int i = 0; i < count; ++i) {
258 mCustomFields.remove(row);
259 }
261
262 return true;
263}
264
265#include "moc_customfieldsmodel.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
A widget for editing the display name of a contact.
void beginInsertRows(const QModelIndex &parent, int first, int last)
void beginRemoveRows(const QModelIndex &parent, int first, int last)
QModelIndex createIndex(int row, int column, const void *ptr) const const
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual Qt::ItemFlags flags(const QModelIndex &index) const const
void layoutAboutToBeChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
void layoutChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
QDate fromString(QStringView string, QStringView format, QCalendar cal)
QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
QString toString(QDate date, FormatType format) const const
int column() const const
bool isValid() const const
int row() const const
Q_EMITQ_EMIT
QObject * parent() const const
CheckState
DisplayRole
typedef ItemFlags
Orientation
QTime fromString(QStringView string, QStringView format)
int toInt(bool *ok) const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:08 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.