22 #include "customfieldsmodel.h"
27 #include <klocalizedstring.h>
29 #include <QtCore/QDateTime>
31 Q_DECLARE_METATYPE(Qt::CheckState)
33 CustomFieldsModel::CustomFieldsModel(
QObject *parent)
38 CustomFieldsModel::~CustomFieldsModel()
44 emit layoutAboutToBeChanged();
46 mCustomFields = customFields;
59 return createIndex(row, column);
74 if (index.
row() < 0 || index.
row() >= mCustomFields.count()) {
84 if (role == Qt::DisplayRole) {
86 return customField.title();
87 }
else if (index.
column() == 1) {
88 switch (customField.type()) {
89 case CustomField::TextType:
90 case CustomField::NumericType:
91 case CustomField::UrlType:
92 return customField.value();
94 case CustomField::BooleanType:
97 case CustomField::DateType: {
99 return KGlobal::locale()->formatDate(value, KLocale::ShortDate);
102 case CustomField::TimeType: {
104 return KGlobal::locale()->formatTime(value);
107 case CustomField::DateTimeType: {
109 return KGlobal::locale()->formatDateTime(value);
113 return customField.value();
115 return customField.key();
119 if (role == Qt::CheckStateRole) {
120 if (index.
column() == 1) {
121 if (customField.type() == CustomField::BooleanType) {
122 return (customField.value() ==
QLatin1String(
"true") ? Qt::Checked : Qt::Unchecked);
127 if (role == Qt::EditRole) {
128 if (index.
column() == 0) {
129 return customField.title();
130 }
else if (index.
column() == 1) {
131 return customField.value();
133 return customField.key();
137 if (role == TypeRole) {
138 return customField.type();
141 if (role == ScopeRole) {
142 return customField.scope();
154 if (index.
row() < 0 || index.
row() >= mCustomFields.count()) {
164 if (role == Qt::EditRole) {
165 if (index.
column() == 0) {
166 customField.setTitle(value.
toString());
167 }
else if (index.
column() == 1) {
168 customField.setValue(value.
toString());
170 customField.setKey(value.
toString());
173 emit dataChanged(index, index);
177 if (role == Qt::CheckStateRole) {
178 if (index.
column() == 1) {
179 if (customField.type() == CustomField::BooleanType) {
180 customField.setValue(static_cast<Qt::CheckState>(value.
toInt()) == Qt::Checked ?
182 emit dataChanged(index, index);
188 if (role == TypeRole) {
189 customField.setType((CustomField::Type)value.
toInt());
190 emit dataChanged(index, index);
194 if (role == ScopeRole) {
196 emit dataChanged(index, index);
203 QVariant CustomFieldsModel::headerData(
int section, Qt::Orientation orientation,
int role)
const
205 if (section < 0 || section > 1) {
209 if (orientation != Qt::Horizontal) {
213 if (role != Qt::DisplayRole) {
218 return i18nc(
"custom field title",
"Title");
220 return i18nc(
"custom field value",
"Value");
226 if (!index.
isValid() || index.
row() < 0 || index.
row() >= mCustomFields.count()) {
233 if ((customField.type() == CustomField::BooleanType) && (index.
column() == 1)) {
234 return (parentFlags | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsUserCheckable);
236 return (parentFlags | Qt::ItemIsEnabled | Qt::ItemIsEditable);
240 int CustomFieldsModel::columnCount(
const QModelIndex &parent)
const
249 int CustomFieldsModel::rowCount(
const QModelIndex &parent)
const
252 return mCustomFields.count();
258 bool CustomFieldsModel::insertRows(
int row,
int count,
const QModelIndex &parent)
264 beginInsertRows(parent, row, row + count - 1);
265 for (
int i = 0; i < count; ++i) {
273 bool CustomFieldsModel::removeRows(
int row,
int count,
const QModelIndex &parent)
279 beginRemoveRows(parent, row, row + count - 1);
280 for (
int i = 0; i < count; ++i) {
281 mCustomFields.remove(row);
A class that represents non-standard contact fields.
QTime fromString(const QString &string, Qt::DateFormat format)
QDate fromString(const QString &string, Qt::DateFormat format)
int toInt(bool *ok) const
QDateTime fromString(const QString &string, Qt::DateFormat format)
virtual Qt::ItemFlags flags(const QModelIndex &index) const