• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.3 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  if ( index.row() < 0 || index.row() >= mCustomFields.count() )
71  return QVariant();
72 
73  if ( index.column() < 0 || index.column() > 2 )
74  return QVariant();
75 
76  const CustomField &customField = mCustomFields[ index.row() ];
77 
78  if ( role == Qt::DisplayRole ) {
79  if ( index.column() == 0 )
80  return customField.title();
81  else if ( index.column() == 1 ) {
82  switch ( customField.type() ) {
83  case CustomField::TextType:
84  case CustomField::NumericType:
85  return customField.value();
86  break;
87  case CustomField::BooleanType:
88  return QString();
89  break;
90  case CustomField::DateType:
91  {
92  const QDate value = QDate::fromString( customField.value(), Qt::ISODate );
93  return KGlobal::locale()->formatDate( value, KLocale::ShortDate );
94  }
95  break;
96  case CustomField::TimeType:
97  {
98  const QTime value = QTime::fromString( customField.value(), Qt::ISODate );
99  return KGlobal::locale()->formatTime( value );
100  }
101  break;
102  case CustomField::DateTimeType:
103  {
104  const QDateTime value = QDateTime::fromString( customField.value(), Qt::ISODate );
105  return KGlobal::locale()->formatDateTime( value );
106  }
107  break;
108  }
109  return customField.value();
110  } else
111  return customField.key();
112  }
113 
114  if ( role == Qt::CheckStateRole ) {
115  if ( index.column() == 1 ) {
116  if ( customField.type() == CustomField::BooleanType ) {
117  return (customField.value() == QLatin1String( "true" ) ? Qt::Checked : Qt::Unchecked);
118  }
119  }
120  }
121 
122  if ( role == Qt::EditRole ) {
123  if ( index.column() == 0 )
124  return customField.title();
125  else if ( index.column() == 1 )
126  return customField.value();
127  else
128  return customField.key();
129  }
130 
131  if ( role == TypeRole )
132  return customField.type();
133 
134  if ( role == ScopeRole )
135  return customField.scope();
136 
137  return QVariant();
138 }
139 
140 bool CustomFieldsModel::setData( const QModelIndex &index, const QVariant &value, int role )
141 {
142  if ( !index.isValid() )
143  return false;
144 
145  if ( index.row() < 0 || index.row() >= mCustomFields.count() )
146  return false;
147 
148  if ( index.column() < 0 || index.column() > 2 )
149  return false;
150 
151  CustomField &customField = mCustomFields[ index.row() ];
152 
153  if ( role == Qt::EditRole ) {
154  if ( index.column() == 0 )
155  customField.setTitle( value.toString() );
156  else if ( index.column() == 1 )
157  customField.setValue( value.toString() );
158  else
159  customField.setKey( value.toString() );
160 
161  emit dataChanged( index, index );
162  return true;
163  }
164 
165  if ( role == Qt::CheckStateRole ) {
166  if ( index.column() == 1 ) {
167  if ( customField.type() == CustomField::BooleanType ) {
168  customField.setValue( static_cast<Qt::CheckState>( value.toInt() ) == Qt::Checked ?
169  QLatin1String( "true" ) : QLatin1String( "false" ) );
170  emit dataChanged( index, index );
171  return true;
172  }
173  }
174  }
175 
176  if ( role == TypeRole ) {
177  customField.setType( (CustomField::Type)value.toInt() );
178  emit dataChanged( index, index );
179  return true;
180  }
181 
182  if ( role == ScopeRole ) {
183  customField.setScope( (CustomField::Scope)value.toInt() );
184  emit dataChanged( index, index );
185  return true;
186  }
187 
188  return false;
189 }
190 
191 QVariant CustomFieldsModel::headerData( int section, Qt::Orientation orientation, int role ) const
192 {
193  if ( section < 0 || section > 1 )
194  return QVariant();
195 
196  if ( orientation != Qt::Horizontal )
197  return QVariant();
198 
199  if ( role != Qt::DisplayRole )
200  return QVariant();
201 
202  if ( section == 0 )
203  return i18nc( "custom field title", "Title" );
204  else
205  return i18nc( "custom field value", "Value" );
206 }
207 
208 Qt::ItemFlags CustomFieldsModel::flags( const QModelIndex &index ) const
209 {
210  if ( !index.isValid() || index.row() < 0 || index.row() >= mCustomFields.count() )
211  return QAbstractItemModel::flags( index );
212 
213  const CustomField &customField = mCustomFields[ index.row() ];
214 
215  const Qt::ItemFlags parentFlags = QAbstractItemModel::flags( index );
216  if ( (customField.type() == CustomField::BooleanType) && (index.column() == 1) )
217  return (parentFlags | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsUserCheckable);
218  else
219  return (parentFlags | Qt::ItemIsEnabled | Qt::ItemIsEditable);
220 }
221 
222 int CustomFieldsModel::columnCount( const QModelIndex &parent ) const
223 {
224  if ( !parent.isValid() )
225  return 3;
226  else
227  return 0;
228 }
229 
230 int CustomFieldsModel::rowCount( const QModelIndex &parent ) const
231 {
232  if ( !parent.isValid() )
233  return mCustomFields.count();
234  else
235  return 0;
236 }
237 
238 bool CustomFieldsModel::insertRows( int row, int count, const QModelIndex &parent )
239 {
240  if ( parent.isValid() )
241  return false;
242 
243  beginInsertRows( parent, row, row + count - 1 );
244  for ( int i = 0; i < count; ++i )
245  mCustomFields.insert( row, CustomField() );
246  endInsertRows();
247 
248  return true;
249 }
250 
251 bool CustomFieldsModel::removeRows( int row, int count, const QModelIndex &parent )
252 {
253  if ( parent.isValid() )
254  return false;
255 
256  beginRemoveRows( parent, row, row + count - 1 );
257  for ( int i = 0; i < count; ++i )
258  mCustomFields.remove( row );
259  endRemoveRows();
260 
261  return true;
262 }
263 
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Nov 26 2012 16:49:09 by doxygen 1.8.1.2 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.9.3 API Reference

Skip menu "kdepimlibs-4.9.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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