akonadi/contact
customfieldsmodel.cpp
00001 /* 00002 This file is part of Akonadi Contact. 00003 00004 Copyright (c) 2010 Tobias Koenig <tokoe@kde.org> 00005 00006 This library is free software; you can redistribute it and/or modify it 00007 under the terms of the GNU Library General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or (at your 00009 option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, but WITHOUT 00012 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00013 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00014 License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to the 00018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00019 02110-1301, USA. 00020 */ 00021 00022 #include "customfieldsmodel.h" 00023 00024 #include <kglobal.h> 00025 #include <kicon.h> 00026 #include <klocale.h> 00027 00028 #include <QtCore/QDateTime> 00029 00030 Q_DECLARE_METATYPE( Qt::CheckState ) 00031 00032 CustomFieldsModel::CustomFieldsModel( QObject *parent ) 00033 : QAbstractItemModel( parent ) 00034 { 00035 } 00036 00037 CustomFieldsModel::~CustomFieldsModel() 00038 { 00039 } 00040 00041 void CustomFieldsModel::setCustomFields( const CustomField::List &customFields ) 00042 { 00043 emit layoutAboutToBeChanged(); 00044 00045 mCustomFields = customFields; 00046 00047 emit layoutChanged(); 00048 } 00049 00050 CustomField::List CustomFieldsModel::customFields() const 00051 { 00052 return mCustomFields; 00053 } 00054 00055 QModelIndex CustomFieldsModel::index( int row, int column, const QModelIndex& ) const 00056 { 00057 return createIndex( row, column, 0 ); 00058 } 00059 00060 QModelIndex CustomFieldsModel::parent( const QModelIndex& ) const 00061 { 00062 return QModelIndex(); 00063 } 00064 00065 QVariant CustomFieldsModel::data( const QModelIndex &index, int role ) const 00066 { 00067 if ( !index.isValid() ) 00068 return QVariant(); 00069 00070 if ( index.row() < 0 || index.row() >= mCustomFields.count() ) 00071 return QVariant(); 00072 00073 if ( index.column() < 0 || index.column() > 2 ) 00074 return QVariant(); 00075 00076 const CustomField &customField = mCustomFields[ index.row() ]; 00077 00078 if ( role == Qt::DisplayRole ) { 00079 if ( index.column() == 0 ) 00080 return customField.title(); 00081 else if ( index.column() == 1 ) { 00082 switch ( customField.type() ) { 00083 case CustomField::TextType: 00084 case CustomField::NumericType: 00085 return customField.value(); 00086 break; 00087 case CustomField::BooleanType: 00088 return QString(); 00089 break; 00090 case CustomField::DateType: 00091 { 00092 const QDate value = QDate::fromString( customField.value(), Qt::ISODate ); 00093 return KGlobal::locale()->formatDate( value, KLocale::ShortDate ); 00094 } 00095 break; 00096 case CustomField::TimeType: 00097 { 00098 const QTime value = QTime::fromString( customField.value(), Qt::ISODate ); 00099 return KGlobal::locale()->formatTime( value ); 00100 } 00101 break; 00102 case CustomField::DateTimeType: 00103 { 00104 const QDateTime value = QDateTime::fromString( customField.value(), Qt::ISODate ); 00105 return KGlobal::locale()->formatDateTime( value ); 00106 } 00107 break; 00108 } 00109 return customField.value(); 00110 } else 00111 return customField.key(); 00112 } 00113 00114 if ( role == Qt::CheckStateRole ) { 00115 if ( index.column() == 1 ) { 00116 if ( customField.type() == CustomField::BooleanType ) { 00117 return (customField.value() == QLatin1String( "true" ) ? Qt::Checked : Qt::Unchecked); 00118 } 00119 } 00120 } 00121 00122 if ( role == Qt::EditRole ) { 00123 if ( index.column() == 0 ) 00124 return customField.title(); 00125 else if ( index.column() == 1 ) 00126 return customField.value(); 00127 else 00128 return customField.key(); 00129 } 00130 00131 if ( role == TypeRole ) 00132 return customField.type(); 00133 00134 if ( role == ScopeRole ) 00135 return customField.scope(); 00136 00137 return QVariant(); 00138 } 00139 00140 bool CustomFieldsModel::setData( const QModelIndex &index, const QVariant &value, int role ) 00141 { 00142 if ( !index.isValid() ) 00143 return false; 00144 00145 if ( index.row() < 0 || index.row() >= mCustomFields.count() ) 00146 return false; 00147 00148 if ( index.column() < 0 || index.column() > 2 ) 00149 return false; 00150 00151 CustomField &customField = mCustomFields[ index.row() ]; 00152 00153 if ( role == Qt::EditRole ) { 00154 if ( index.column() == 0 ) 00155 customField.setTitle( value.toString() ); 00156 else if ( index.column() == 1 ) 00157 customField.setValue( value.toString() ); 00158 else 00159 customField.setKey( value.toString() ); 00160 00161 emit dataChanged( index, index ); 00162 return true; 00163 } 00164 00165 if ( role == Qt::CheckStateRole ) { 00166 if ( index.column() == 1 ) { 00167 if ( customField.type() == CustomField::BooleanType ) { 00168 customField.setValue( static_cast<Qt::CheckState>( value.toInt() ) == Qt::Checked ? 00169 QLatin1String( "true" ) : QLatin1String( "false" ) ); 00170 emit dataChanged( index, index ); 00171 return true; 00172 } 00173 } 00174 } 00175 00176 if ( role == TypeRole ) { 00177 customField.setType( (CustomField::Type)value.toInt() ); 00178 emit dataChanged( index, index ); 00179 return true; 00180 } 00181 00182 if ( role == ScopeRole ) { 00183 customField.setScope( (CustomField::Scope)value.toInt() ); 00184 emit dataChanged( index, index ); 00185 return true; 00186 } 00187 00188 return false; 00189 } 00190 00191 QVariant CustomFieldsModel::headerData( int section, Qt::Orientation orientation, int role ) const 00192 { 00193 if ( section < 0 || section > 1 ) 00194 return QVariant(); 00195 00196 if ( orientation != Qt::Horizontal ) 00197 return QVariant(); 00198 00199 if ( role != Qt::DisplayRole ) 00200 return QVariant(); 00201 00202 if ( section == 0 ) 00203 return i18nc( "custom field title", "Title" ); 00204 else 00205 return i18nc( "custom field value", "Value" ); 00206 } 00207 00208 Qt::ItemFlags CustomFieldsModel::flags( const QModelIndex &index ) const 00209 { 00210 if ( !index.isValid() || index.row() < 0 || index.row() >= mCustomFields.count() ) 00211 return QAbstractItemModel::flags( index ); 00212 00213 const CustomField &customField = mCustomFields[ index.row() ]; 00214 00215 const Qt::ItemFlags parentFlags = QAbstractItemModel::flags( index ); 00216 if ( (customField.type() == CustomField::BooleanType) && (index.column() == 1) ) 00217 return (parentFlags | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsUserCheckable); 00218 else 00219 return (parentFlags | Qt::ItemIsEnabled | Qt::ItemIsEditable); 00220 } 00221 00222 int CustomFieldsModel::columnCount( const QModelIndex &parent ) const 00223 { 00224 if ( !parent.isValid() ) 00225 return 3; 00226 else 00227 return 0; 00228 } 00229 00230 int CustomFieldsModel::rowCount( const QModelIndex &parent ) const 00231 { 00232 if ( !parent.isValid() ) 00233 return mCustomFields.count(); 00234 else 00235 return 0; 00236 } 00237 00238 bool CustomFieldsModel::insertRows( int row, int count, const QModelIndex &parent ) 00239 { 00240 if ( parent.isValid() ) 00241 return false; 00242 00243 beginInsertRows( parent, row, row + count - 1 ); 00244 for ( int i = 0; i < count; ++i ) 00245 mCustomFields.insert( row, CustomField() ); 00246 endInsertRows(); 00247 00248 return true; 00249 } 00250 00251 bool CustomFieldsModel::removeRows( int row, int count, const QModelIndex &parent ) 00252 { 00253 if ( parent.isValid() ) 00254 return false; 00255 00256 beginRemoveRows( parent, row, row + count - 1 ); 00257 for ( int i = 0; i < count; ++i ) 00258 mCustomFields.remove( row ); 00259 endRemoveRows(); 00260 00261 return true; 00262 } 00263
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu Aug 2 2012 15:25:43 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu Aug 2 2012 15:25:43 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.