9#include "customfieldsmodel.h"
11#include <KLocalizedString>
18CustomFieldsModel::CustomFieldsModel(
QObject *parent)
23CustomFieldsModel::~CustomFieldsModel() =
default;
25void CustomFieldsModel::setCustomFields(
const CustomField::List &customFields)
29 mCustomFields = customFields;
34CustomField::List CustomFieldsModel::customFields()
const
39QModelIndex CustomFieldsModel::index(
int row,
int column,
const QModelIndex &parent)
const
51QVariant CustomFieldsModel::data(
const QModelIndex &index,
int role)
const
53 if (!index.isValid()) {
57 if (index.row() < 0 || index.row() >= mCustomFields.count()) {
61 if (index.column() < 0 || index.column() > 2) {
65 const CustomField &customField = mCustomFields[index.row()];
68 if (index.column() == 0) {
69 return customField.title();
70 }
else if (index.column() == 1) {
71 switch (customField.type()) {
72 case CustomField::TextType:
73 case CustomField::NumericType:
74 case CustomField::UrlType:
75 return customField.value();
76 case CustomField::BooleanType:
78 case CustomField::DateType: {
82 case CustomField::TimeType: {
84 return QLocale().toString(value);
86 case CustomField::DateTimeType: {
88 return QLocale().toString(value);
91 return customField.value();
93 return customField.key();
98 if (index.column() == 1) {
99 if (customField.type() == CustomField::BooleanType) {
106 if (index.column() == 0) {
107 return customField.title();
108 }
else if (index.column() == 1) {
109 return customField.value();
111 return customField.key();
115 if (role == TypeRole) {
116 return customField.type();
119 if (role == ScopeRole) {
120 return customField.scope();
126bool CustomFieldsModel::setData(
const QModelIndex &index,
const QVariant &value,
int role)
128 if (!index.isValid()) {
132 if (index.row() < 0 || index.row() >= mCustomFields.count()) {
136 if (index.column() < 0 || index.column() > 2) {
140 CustomField &customField = mCustomFields[index.row()];
143 if (index.column() == 0) {
144 customField.setTitle(value.
toString());
145 }
else if (index.column() == 1) {
146 customField.setValue(value.
toString());
148 customField.setKey(value.
toString());
156 if (index.column() == 1) {
157 if (customField.type() == CustomField::BooleanType) {
165 if (role == TypeRole) {
166 customField.setType((CustomField::Type)value.
toInt());
171 if (role == ScopeRole) {
172 customField.setScope((CustomField::Scope)value.
toInt());
180QVariant CustomFieldsModel::headerData(
int section,
Qt::Orientation orientation,
int role)
const
182 if (section < 0 || section > 1) {
195 return i18nc(
"custom field title",
"Title");
197 return i18nc(
"custom field value",
"Value");
201Qt::ItemFlags CustomFieldsModel::flags(
const QModelIndex &index)
const
203 if (!index.isValid() || index.row() < 0 || index.row() >= mCustomFields.count()) {
207 const CustomField &customField = mCustomFields[index.row()];
210 if ((customField.type() == CustomField::BooleanType) && (index.column() == 1)) {
217int CustomFieldsModel::columnCount(
const QModelIndex &parent)
const
219 if (!parent.isValid()) {
226int CustomFieldsModel::rowCount(
const QModelIndex &parent)
const
228 if (!parent.isValid()) {
229 return mCustomFields.count();
235bool CustomFieldsModel::insertRows(
int row,
int count,
const QModelIndex &parent)
237 if (parent.isValid()) {
242 for (
int i = 0; i < count; ++i) {
243 mCustomFields.insert(row, CustomField());
250bool CustomFieldsModel::removeRows(
int row,
int count,
const QModelIndex &parent)
252 if (parent.isValid()) {
257 for (
int i = 0; i < count; ++i) {
258 mCustomFields.remove(row);
265#include "moc_customfieldsmodel.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
A widget for editing the display name of a contact.
void beginInsertRows(const QModelIndex &parent, int first, int last)
void beginRemoveRows(const QModelIndex &parent, int first, int last)
QModelIndex createIndex(int row, int column, const void *ptr) const const
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual Qt::ItemFlags flags(const QModelIndex &index) const const
void layoutAboutToBeChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
void layoutChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
QDate fromString(QStringView string, QStringView format, QCalendar cal)
QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
QObject * parent() const const
QTime fromString(QStringView string, QStringView format)
int toInt(bool *ok) const const
QString toString() const const