Akonadi Contacts

customfieldsmodel.cpp
1 /*
2  This file is part of Contact Editor.
3 
4  Copyright (c) 2010 Tobias Koenig <[email protected]>
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 
22 #include "customfieldsmodel.h"
23 
24 #include <KLocalizedString>
25 
26 #include <QDateTime>
27 #include <QLocale>
28 
29 Q_DECLARE_METATYPE(Qt::CheckState)
30 
31 CustomFieldsModel::CustomFieldsModel(QObject *parent)
32  : QAbstractItemModel(parent)
33 {
34 }
35 
36 CustomFieldsModel::~CustomFieldsModel()
37 {
38 }
39 
40 void CustomFieldsModel::setCustomFields(const CustomField::List &customFields)
41 {
42  Q_EMIT layoutAboutToBeChanged();
43 
44  mCustomFields = customFields;
45 
46  Q_EMIT layoutChanged();
47 }
48 
49 CustomField::List CustomFieldsModel::customFields() const
50 {
51  return mCustomFields;
52 }
53 
54 QModelIndex CustomFieldsModel::index(int row, int column, const QModelIndex &parent) const
55 {
56  Q_UNUSED(parent);
57  return createIndex(row, column);
58 }
59 
61 {
62  Q_UNUSED(child);
63  return QModelIndex();
64 }
65 
66 QVariant CustomFieldsModel::data(const QModelIndex &index, int role) const
67 {
68  if (!index.isValid()) {
69  return QVariant();
70  }
71 
72  if (index.row() < 0 || index.row() >= mCustomFields.count()) {
73  return QVariant();
74  }
75 
76  if (index.column() < 0 || index.column() > 2) {
77  return QVariant();
78  }
79 
80  const CustomField &customField = mCustomFields[index.row()];
81 
82  if (role == Qt::DisplayRole) {
83  if (index.column() == 0) {
84  return customField.title();
85  } else if (index.column() == 1) {
86  switch (customField.type()) {
87  case CustomField::TextType:
88  case CustomField::NumericType:
89  case CustomField::UrlType:
90  return customField.value();
91  case CustomField::BooleanType:
92  return QString();
93  case CustomField::DateType:
94  {
95  const QDate value = QDate::fromString(customField.value(), Qt::ISODate);
96  return QLocale().toString(value, QLocale::ShortFormat);
97  }
98  case CustomField::TimeType:
99  {
100  const QTime value = QTime::fromString(customField.value(), Qt::ISODate);
101  return QLocale().toString(value);
102  }
103  case CustomField::DateTimeType:
104  {
105  const QDateTime value = QDateTime::fromString(customField.value(), Qt::ISODate);
106  return QLocale().toString(value);
107  }
108  }
109  return customField.value();
110  } else {
111  return customField.key();
112  }
113  }
114 
115  if (role == Qt::CheckStateRole) {
116  if (index.column() == 1) {
117  if (customField.type() == CustomField::BooleanType) {
118  return customField.value() == QLatin1String("true") ? Qt::Checked : Qt::Unchecked;
119  }
120  }
121  }
122 
123  if (role == Qt::EditRole) {
124  if (index.column() == 0) {
125  return customField.title();
126  } else if (index.column() == 1) {
127  return customField.value();
128  } else {
129  return customField.key();
130  }
131  }
132 
133  if (role == TypeRole) {
134  return customField.type();
135  }
136 
137  if (role == ScopeRole) {
138  return customField.scope();
139  }
140 
141  return QVariant();
142 }
143 
144 bool CustomFieldsModel::setData(const QModelIndex &index, const QVariant &value, int role)
145 {
146  if (!index.isValid()) {
147  return false;
148  }
149 
150  if (index.row() < 0 || index.row() >= mCustomFields.count()) {
151  return false;
152  }
153 
154  if (index.column() < 0 || index.column() > 2) {
155  return false;
156  }
157 
158  CustomField &customField = mCustomFields[index.row()];
159 
160  if (role == Qt::EditRole) {
161  if (index.column() == 0) {
162  customField.setTitle(value.toString());
163  } else if (index.column() == 1) {
164  customField.setValue(value.toString());
165  } else {
166  customField.setKey(value.toString());
167  }
168 
169  Q_EMIT dataChanged(index, index);
170  return true;
171  }
172 
173  if (role == Qt::CheckStateRole) {
174  if (index.column() == 1) {
175  if (customField.type() == CustomField::BooleanType) {
176  customField.setValue(static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked
177  ? QStringLiteral("true") : QStringLiteral("false"));
178  Q_EMIT dataChanged(index, index);
179  return true;
180  }
181  }
182  }
183 
184  if (role == TypeRole) {
185  customField.setType((CustomField::Type)value.toInt());
186  Q_EMIT dataChanged(index, index);
187  return true;
188  }
189 
190  if (role == ScopeRole) {
191  customField.setScope((CustomField::Scope)value.toInt());
192  Q_EMIT dataChanged(index, index);
193  return true;
194  }
195 
196  return false;
197 }
198 
199 QVariant CustomFieldsModel::headerData(int section, Qt::Orientation orientation, int role) const
200 {
201  if (section < 0 || section > 1) {
202  return QVariant();
203  }
204 
205  if (orientation != Qt::Horizontal) {
206  return QVariant();
207  }
208 
209  if (role != Qt::DisplayRole) {
210  return QVariant();
211  }
212 
213  if (section == 0) {
214  return i18nc("custom field title", "Title");
215  } else {
216  return i18nc("custom field value", "Value");
217  }
218 }
219 
220 Qt::ItemFlags CustomFieldsModel::flags(const QModelIndex &index) const
221 {
222  if (!index.isValid() || index.row() < 0 || index.row() >= mCustomFields.count()) {
223  return QAbstractItemModel::flags(index);
224  }
225 
226  const CustomField &customField = mCustomFields[index.row()];
227 
228  const Qt::ItemFlags parentFlags = QAbstractItemModel::flags(index);
229  if ((customField.type() == CustomField::BooleanType) && (index.column() == 1)) {
231  } else {
232  return parentFlags | Qt::ItemIsEnabled | Qt::ItemIsEditable;
233  }
234 }
235 
236 int CustomFieldsModel::columnCount(const QModelIndex &parent) const
237 {
238  if (!parent.isValid()) {
239  return 3;
240  } else {
241  return 0;
242  }
243 }
244 
245 int CustomFieldsModel::rowCount(const QModelIndex &parent) const
246 {
247  if (!parent.isValid()) {
248  return mCustomFields.count();
249  } else {
250  return 0;
251  }
252 }
253 
254 bool CustomFieldsModel::insertRows(int row, int count, const QModelIndex &parent)
255 {
256  if (parent.isValid()) {
257  return false;
258  }
259 
260  beginInsertRows(parent, row, row + count - 1);
261  for (int i = 0; i < count; ++i) {
262  mCustomFields.insert(row, CustomField());
263  }
264  endInsertRows();
265 
266  return true;
267 }
268 
269 bool CustomFieldsModel::removeRows(int row, int count, const QModelIndex &parent)
270 {
271  if (parent.isValid()) {
272  return false;
273  }
274 
275  beginRemoveRows(parent, row, row + count - 1);
276  for (int i = 0; i < count; ++i) {
277  mCustomFields.remove(row);
278  }
279  endRemoveRows();
280 
281  return true;
282 }
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
QObject * parent() const const
QString toString() const const
typedef ItemFlags
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Aug 5 2020 23:05:57 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.