22 #include "customfieldsmodel.h"
27 #include <klocalizedstring.h>
29 #include <QtCore/QDateTime>
31 Q_DECLARE_METATYPE( Qt::CheckState )
33 CustomFieldsModel::CustomFieldsModel( QObject *parent )
34 : QAbstractItemModel( parent )
38 CustomFieldsModel::~CustomFieldsModel()
42 void CustomFieldsModel::setCustomFields(
const CustomField::List &customFields )
44 emit layoutAboutToBeChanged();
46 mCustomFields = customFields;
51 CustomField::List CustomFieldsModel::customFields()
const
56 QModelIndex CustomFieldsModel::index(
int row,
int column,
const QModelIndex& )
const
58 return createIndex( row, column );
61 QModelIndex CustomFieldsModel::parent(
const QModelIndex& )
const
66 QVariant CustomFieldsModel::data(
const QModelIndex &index,
int role )
const
68 if ( !index.isValid() ) {
72 if ( index.row() < 0 || index.row() >= mCustomFields.count() ) {
76 if ( index.column() < 0 || index.column() > 2 ) {
80 const CustomField &customField = mCustomFields[ index.row() ];
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();
92 case CustomField::BooleanType:
95 case CustomField::DateType:
97 const QDate value = QDate::fromString( customField.value(), Qt::ISODate );
98 return KGlobal::locale()->formatDate( value, KLocale::ShortDate );
101 case CustomField::TimeType:
103 const QTime value = QTime::fromString( customField.value(), Qt::ISODate );
104 return KGlobal::locale()->formatTime( value );
107 case CustomField::DateTimeType:
109 const QDateTime value = QDateTime::fromString( customField.value(), Qt::ISODate );
110 return KGlobal::locale()->formatDateTime( value );
114 return customField.value();
116 return customField.key();
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 );
128 if ( role == Qt::EditRole ) {
129 if ( index.column() == 0 ) {
130 return customField.title();
131 }
else if ( index.column() == 1 ) {
132 return customField.value();
134 return customField.key();
138 if ( role == TypeRole ) {
139 return customField.type();
142 if ( role == ScopeRole ) {
143 return customField.scope();
149 bool CustomFieldsModel::setData(
const QModelIndex &index,
const QVariant &value,
int role )
151 if ( !index.isValid() ) {
155 if ( index.row() < 0 || index.row() >= mCustomFields.count() ) {
159 if ( index.column() < 0 || index.column() > 2 ) {
163 CustomField &customField = mCustomFields[ index.row() ];
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() );
171 customField.setKey( value.toString() );
174 emit dataChanged( index, index );
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 );
189 if ( role == TypeRole ) {
190 customField.setType( (CustomField::Type)value.toInt() );
191 emit dataChanged( index, index );
195 if ( role == ScopeRole ) {
197 emit dataChanged( index, index );
204 QVariant CustomFieldsModel::headerData(
int section, Qt::Orientation orientation,
int role )
const
206 if ( section < 0 || section > 1 ) {
210 if ( orientation != Qt::Horizontal ) {
214 if ( role != Qt::DisplayRole ) {
218 if ( section == 0 ) {
219 return i18nc(
"custom field title",
"Title" );
221 return i18nc(
"custom field value",
"Value" );
225 Qt::ItemFlags CustomFieldsModel::flags(
const QModelIndex &index )
const
227 if ( !index.isValid() || index.row() < 0 || index.row() >= mCustomFields.count() ) {
228 return QAbstractItemModel::flags( index );
231 const CustomField &customField = mCustomFields[ index.row() ];
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 );
237 return ( parentFlags | Qt::ItemIsEnabled | Qt::ItemIsEditable );
241 int CustomFieldsModel::columnCount(
const QModelIndex &parent )
const
243 if ( !parent.isValid() ) {
250 int CustomFieldsModel::rowCount(
const QModelIndex &parent )
const
252 if ( !parent.isValid() ) {
253 return mCustomFields.count();
259 bool CustomFieldsModel::insertRows(
int row,
int count,
const QModelIndex &parent )
261 if ( parent.isValid() ) {
265 beginInsertRows( parent, row, row + count - 1 );
266 for (
int i = 0; i < count; ++i ) {
274 bool CustomFieldsModel::removeRows(
int row,
int count,
const QModelIndex &parent )
276 if ( parent.isValid() ) {
280 beginRemoveRows( parent, row, row + count - 1 );
281 for (
int i = 0; i < count; ++i ) {
282 mCustomFields.remove( row );
A class that represents non-standard contact fields.