• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

akonadi/contact

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

KDE's Doxygen guidelines are available online.

akonadi/contact

Skip menu "akonadi/contact"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal