Akonadi Contacts

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

KDE's Doxygen guidelines are available online.