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

akonadi

  • sources
  • kde-4.12
  • 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& ) const
57 {
58  return createIndex( row, column );
59 }
60 
61 QModelIndex CustomFieldsModel::parent( const QModelIndex& ) const
62 {
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  break;
92  case CustomField::BooleanType:
93  return QString();
94  break;
95  case CustomField::DateType:
96  {
97  const QDate value = QDate::fromString( customField.value(), Qt::ISODate );
98  return KGlobal::locale()->formatDate( value, KLocale::ShortDate );
99  }
100  break;
101  case CustomField::TimeType:
102  {
103  const QTime value = QTime::fromString( customField.value(), Qt::ISODate );
104  return KGlobal::locale()->formatTime( value );
105  }
106  break;
107  case CustomField::DateTimeType:
108  {
109  const QDateTime value = QDateTime::fromString( customField.value(), Qt::ISODate );
110  return KGlobal::locale()->formatDateTime( value );
111  }
112  break;
113  }
114  return customField.value();
115  } else {
116  return customField.key();
117  }
118  }
119 
120  if ( role == Qt::CheckStateRole ) {
121  if ( index.column() == 1 ) {
122  if ( customField.type() == CustomField::BooleanType ) {
123  return ( customField.value() == QLatin1String( "true" ) ? Qt::Checked : Qt::Unchecked );
124  }
125  }
126  }
127 
128  if ( role == Qt::EditRole ) {
129  if ( index.column() == 0 ) {
130  return customField.title();
131  } else if ( index.column() == 1 ) {
132  return customField.value();
133  } else {
134  return customField.key();
135  }
136  }
137 
138  if ( role == TypeRole ) {
139  return customField.type();
140  }
141 
142  if ( role == ScopeRole ) {
143  return customField.scope();
144  }
145 
146  return QVariant();
147 }
148 
149 bool CustomFieldsModel::setData( const QModelIndex &index, const QVariant &value, int role )
150 {
151  if ( !index.isValid() ) {
152  return false;
153  }
154 
155  if ( index.row() < 0 || index.row() >= mCustomFields.count() ) {
156  return false;
157  }
158 
159  if ( index.column() < 0 || index.column() > 2 ) {
160  return false;
161  }
162 
163  CustomField &customField = mCustomFields[ index.row() ];
164 
165  if ( role == Qt::EditRole ) {
166  if ( index.column() == 0 ) {
167  customField.setTitle( value.toString() );
168  } else if ( index.column() == 1 ) {
169  customField.setValue( value.toString() );
170  } else {
171  customField.setKey( value.toString() );
172  }
173 
174  emit dataChanged( index, index );
175  return true;
176  }
177 
178  if ( role == Qt::CheckStateRole ) {
179  if ( index.column() == 1 ) {
180  if ( customField.type() == CustomField::BooleanType ) {
181  customField.setValue( static_cast<Qt::CheckState>( value.toInt() ) == Qt::Checked ?
182  QLatin1String( "true" ) : QLatin1String( "false" ) );
183  emit dataChanged( index, index );
184  return true;
185  }
186  }
187  }
188 
189  if ( role == TypeRole ) {
190  customField.setType( (CustomField::Type)value.toInt() );
191  emit dataChanged( index, index );
192  return true;
193  }
194 
195  if ( role == ScopeRole ) {
196  customField.setScope( (CustomField::Scope)value.toInt() );
197  emit dataChanged( index, index );
198  return true;
199  }
200 
201  return false;
202 }
203 
204 QVariant CustomFieldsModel::headerData( int section, Qt::Orientation orientation, int role ) const
205 {
206  if ( section < 0 || section > 1 ) {
207  return QVariant();
208  }
209 
210  if ( orientation != Qt::Horizontal ) {
211  return QVariant();
212  }
213 
214  if ( role != Qt::DisplayRole ) {
215  return QVariant();
216  }
217 
218  if ( section == 0 ) {
219  return i18nc( "custom field title", "Title" );
220  } else {
221  return i18nc( "custom field value", "Value" );
222  }
223 }
224 
225 Qt::ItemFlags CustomFieldsModel::flags( const QModelIndex &index ) const
226 {
227  if ( !index.isValid() || index.row() < 0 || index.row() >= mCustomFields.count() ) {
228  return QAbstractItemModel::flags( index );
229  }
230 
231  const CustomField &customField = mCustomFields[ index.row() ];
232 
233  const Qt::ItemFlags parentFlags = QAbstractItemModel::flags( index );
234  if ( ( customField.type() == CustomField::BooleanType ) && ( index.column() == 1 ) ) {
235  return ( parentFlags | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsUserCheckable );
236  } else {
237  return ( parentFlags | Qt::ItemIsEnabled | Qt::ItemIsEditable );
238  }
239 }
240 
241 int CustomFieldsModel::columnCount( const QModelIndex &parent ) const
242 {
243  if ( !parent.isValid() ) {
244  return 3;
245  } else {
246  return 0;
247  }
248 }
249 
250 int CustomFieldsModel::rowCount( const QModelIndex &parent ) const
251 {
252  if ( !parent.isValid() ) {
253  return mCustomFields.count();
254  } else {
255  return 0;
256  }
257 }
258 
259 bool CustomFieldsModel::insertRows( int row, int count, const QModelIndex &parent )
260 {
261  if ( parent.isValid() ) {
262  return false;
263  }
264 
265  beginInsertRows( parent, row, row + count - 1 );
266  for ( int i = 0; i < count; ++i ) {
267  mCustomFields.insert( row, CustomField() );
268  }
269  endInsertRows();
270 
271  return true;
272 }
273 
274 bool CustomFieldsModel::removeRows( int row, int count, const QModelIndex &parent )
275 {
276  if ( parent.isValid() ) {
277  return false;
278  }
279 
280  beginRemoveRows( parent, row, row + count - 1 );
281  for ( int i = 0; i < count; ++i ) {
282  mCustomFields.remove( row );
283  }
284  endRemoveRows();
285 
286  return true;
287 }
288 
CustomField
A class that represents non-standard contact fields.
Definition: customfields_p.h:47
CustomField::Scope
Scope
Definition: customfields_p.h:62
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

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