File indexing completed on 2025-02-16 04:45:52
0001 /* 0002 This file is part of Contact Editor. 0003 0004 SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.0-or-later 0007 */ 0008 0009 #include "customfieldsmodel.h" 0010 0011 #include <KLocalizedString> 0012 0013 #include <QDateTime> 0014 #include <QLocale> 0015 0016 Q_DECLARE_METATYPE(Qt::CheckState) 0017 using namespace Akonadi; 0018 CustomFieldsModel::CustomFieldsModel(QObject *parent) 0019 : QAbstractItemModel(parent) 0020 { 0021 } 0022 0023 CustomFieldsModel::~CustomFieldsModel() = default; 0024 0025 void CustomFieldsModel::setCustomFields(const CustomField::List &customFields) 0026 { 0027 Q_EMIT layoutAboutToBeChanged(); 0028 0029 mCustomFields = customFields; 0030 0031 Q_EMIT layoutChanged(); 0032 } 0033 0034 CustomField::List CustomFieldsModel::customFields() const 0035 { 0036 return mCustomFields; 0037 } 0038 0039 QModelIndex CustomFieldsModel::index(int row, int column, const QModelIndex &parent) const 0040 { 0041 Q_UNUSED(parent) 0042 return createIndex(row, column); 0043 } 0044 0045 QModelIndex CustomFieldsModel::parent(const QModelIndex &child) const 0046 { 0047 Q_UNUSED(child) 0048 return {}; 0049 } 0050 0051 QVariant CustomFieldsModel::data(const QModelIndex &index, int role) const 0052 { 0053 if (!index.isValid()) { 0054 return {}; 0055 } 0056 0057 if (index.row() < 0 || index.row() >= mCustomFields.count()) { 0058 return {}; 0059 } 0060 0061 if (index.column() < 0 || index.column() > 2) { 0062 return {}; 0063 } 0064 0065 const CustomField &customField = mCustomFields[index.row()]; 0066 0067 if (role == Qt::DisplayRole) { 0068 if (index.column() == 0) { 0069 return customField.title(); 0070 } else if (index.column() == 1) { 0071 switch (customField.type()) { 0072 case CustomField::TextType: 0073 case CustomField::NumericType: 0074 case CustomField::UrlType: 0075 return customField.value(); 0076 case CustomField::BooleanType: 0077 return QString(); 0078 case CustomField::DateType: { 0079 const QDate value = QDate::fromString(customField.value(), Qt::ISODate); 0080 return QLocale().toString(value, QLocale::ShortFormat); 0081 } 0082 case CustomField::TimeType: { 0083 const QTime value = QTime::fromString(customField.value(), Qt::ISODate); 0084 return QLocale().toString(value); 0085 } 0086 case CustomField::DateTimeType: { 0087 const QDateTime value = QDateTime::fromString(customField.value(), Qt::ISODate); 0088 return QLocale().toString(value); 0089 } 0090 } 0091 return customField.value(); 0092 } else { 0093 return customField.key(); 0094 } 0095 } 0096 0097 if (role == Qt::CheckStateRole) { 0098 if (index.column() == 1) { 0099 if (customField.type() == CustomField::BooleanType) { 0100 return customField.value() == QLatin1StringView("true") ? Qt::Checked : Qt::Unchecked; 0101 } 0102 } 0103 } 0104 0105 if (role == Qt::EditRole) { 0106 if (index.column() == 0) { 0107 return customField.title(); 0108 } else if (index.column() == 1) { 0109 return customField.value(); 0110 } else { 0111 return customField.key(); 0112 } 0113 } 0114 0115 if (role == TypeRole) { 0116 return customField.type(); 0117 } 0118 0119 if (role == ScopeRole) { 0120 return customField.scope(); 0121 } 0122 0123 return {}; 0124 } 0125 0126 bool CustomFieldsModel::setData(const QModelIndex &index, const QVariant &value, int role) 0127 { 0128 if (!index.isValid()) { 0129 return false; 0130 } 0131 0132 if (index.row() < 0 || index.row() >= mCustomFields.count()) { 0133 return false; 0134 } 0135 0136 if (index.column() < 0 || index.column() > 2) { 0137 return false; 0138 } 0139 0140 CustomField &customField = mCustomFields[index.row()]; 0141 0142 if (role == Qt::EditRole) { 0143 if (index.column() == 0) { 0144 customField.setTitle(value.toString()); 0145 } else if (index.column() == 1) { 0146 customField.setValue(value.toString()); 0147 } else { 0148 customField.setKey(value.toString()); 0149 } 0150 0151 Q_EMIT dataChanged(index, index); 0152 return true; 0153 } 0154 0155 if (role == Qt::CheckStateRole) { 0156 if (index.column() == 1) { 0157 if (customField.type() == CustomField::BooleanType) { 0158 customField.setValue(static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked ? QStringLiteral("true") : QStringLiteral("false")); 0159 Q_EMIT dataChanged(index, index); 0160 return true; 0161 } 0162 } 0163 } 0164 0165 if (role == TypeRole) { 0166 customField.setType((CustomField::Type)value.toInt()); 0167 Q_EMIT dataChanged(index, index); 0168 return true; 0169 } 0170 0171 if (role == ScopeRole) { 0172 customField.setScope((CustomField::Scope)value.toInt()); 0173 Q_EMIT dataChanged(index, index); 0174 return true; 0175 } 0176 0177 return false; 0178 } 0179 0180 QVariant CustomFieldsModel::headerData(int section, Qt::Orientation orientation, int role) const 0181 { 0182 if (section < 0 || section > 1) { 0183 return {}; 0184 } 0185 0186 if (orientation != Qt::Horizontal) { 0187 return {}; 0188 } 0189 0190 if (role != Qt::DisplayRole) { 0191 return {}; 0192 } 0193 0194 if (section == 0) { 0195 return i18nc("custom field title", "Title"); 0196 } else { 0197 return i18nc("custom field value", "Value"); 0198 } 0199 } 0200 0201 Qt::ItemFlags CustomFieldsModel::flags(const QModelIndex &index) const 0202 { 0203 if (!index.isValid() || index.row() < 0 || index.row() >= mCustomFields.count()) { 0204 return QAbstractItemModel::flags(index); 0205 } 0206 0207 const CustomField &customField = mCustomFields[index.row()]; 0208 0209 const Qt::ItemFlags parentFlags = QAbstractItemModel::flags(index); 0210 if ((customField.type() == CustomField::BooleanType) && (index.column() == 1)) { 0211 return parentFlags | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsUserCheckable; 0212 } else { 0213 return parentFlags | Qt::ItemIsEnabled | Qt::ItemIsEditable; 0214 } 0215 } 0216 0217 int CustomFieldsModel::columnCount(const QModelIndex &parent) const 0218 { 0219 if (!parent.isValid()) { 0220 return 3; 0221 } else { 0222 return 0; 0223 } 0224 } 0225 0226 int CustomFieldsModel::rowCount(const QModelIndex &parent) const 0227 { 0228 if (!parent.isValid()) { 0229 return mCustomFields.count(); 0230 } else { 0231 return 0; 0232 } 0233 } 0234 0235 bool CustomFieldsModel::insertRows(int row, int count, const QModelIndex &parent) 0236 { 0237 if (parent.isValid()) { 0238 return false; 0239 } 0240 0241 beginInsertRows(parent, row, row + count - 1); 0242 for (int i = 0; i < count; ++i) { 0243 mCustomFields.insert(row, CustomField()); 0244 } 0245 endInsertRows(); 0246 0247 return true; 0248 } 0249 0250 bool CustomFieldsModel::removeRows(int row, int count, const QModelIndex &parent) 0251 { 0252 if (parent.isValid()) { 0253 return false; 0254 } 0255 0256 beginRemoveRows(parent, row, row + count - 1); 0257 for (int i = 0; i < count; ++i) { 0258 mCustomFields.remove(row); 0259 } 0260 endRemoveRows(); 0261 0262 return true; 0263 } 0264 0265 #include "moc_customfieldsmodel.cpp"