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

akonadi/contact

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

Skip menu "kdepimlibs-4.10.5 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
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