File indexing completed on 2024-05-12 16:42:44
0001 /* 0002 SPDX-FileCopyrightText: 2015-2016 Christian Dávid <christian-david@web.de> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "payeeidentifiermodel.h" 0007 0008 #include <limits> 0009 0010 #include <KLocalizedString> 0011 0012 #include "mymoneyfile.h" 0013 #include "mymoneyexception.h" 0014 0015 /** 0016 * @brief create unique value for QModelIndex::internalId() to indicate "not set" 0017 */ 0018 static constexpr decltype(reinterpret_cast<QModelIndex*>(0)->internalId()) invalidParent = std::numeric_limits<decltype(reinterpret_cast<QModelIndex*>(0)->internalId())>::max(); 0019 0020 payeeIdentifierModel::payeeIdentifierModel(QObject* parent) 0021 : QAbstractItemModel(parent), 0022 m_payeeIdentifierIds(), 0023 m_typeFilter() 0024 { 0025 0026 } 0027 0028 void payeeIdentifierModel::setTypeFilter(QStringList filter) 0029 { 0030 m_typeFilter = filter; 0031 loadData(); 0032 } 0033 0034 void payeeIdentifierModel::setTypeFilter(QString type) 0035 { 0036 setTypeFilter(QStringList(type)); 0037 } 0038 0039 0040 void payeeIdentifierModel::loadData() 0041 { 0042 beginResetModel(); 0043 0044 const QList<MyMoneyPayee> payees = MyMoneyFile::instance()->payeeList(); 0045 m_payeeIdentifierIds.clear(); 0046 m_payeeIdentifierIds.reserve(payees.count()); 0047 Q_FOREACH(const MyMoneyPayee& payee, payees) { 0048 m_payeeIdentifierIds.append(payee.id()); 0049 } 0050 endResetModel(); 0051 } 0052 0053 MyMoneyPayee payeeIdentifierModel::payeeByIndex(const QModelIndex& index) const 0054 { 0055 if (index.isValid() && index.row() >= 0 && index.row() < m_payeeIdentifierIds.count()) { 0056 try { 0057 return MyMoneyFile::instance()->payee(m_payeeIdentifierIds.at(index.row())); 0058 } catch (const MyMoneyException &) { 0059 } 0060 } 0061 0062 return MyMoneyPayee(); 0063 } 0064 0065 QVariant payeeIdentifierModel::data(const QModelIndex& index, int role) const 0066 { 0067 if (!index.isValid()) 0068 return QVariant(); 0069 0070 const auto isPayeeIdentifierValid = index.parent().isValid(); 0071 if (role == payeeIdentifierModel::isPayeeIdentifier) 0072 return isPayeeIdentifierValid; 0073 0074 const MyMoneyPayee payee = (isPayeeIdentifierValid) ? payeeByIndex(index.parent()) : payeeByIndex(index); 0075 0076 0077 if (role == payeeName || (!isPayeeIdentifierValid && role == Qt::DisplayRole)) { 0078 // Return data for MyMoneyPayee 0079 return payee.name(); 0080 } else if (isPayeeIdentifierValid) { 0081 // Return data for payeeIdentifier 0082 if (index.row() >= 0 && static_cast<unsigned int>(index.row()) < payee.payeeIdentifierCount()) { 0083 ::payeeIdentifier ident = payee.payeeIdentifier(index.row()); 0084 0085 if (role == payeeIdentifier) { 0086 return QVariant::fromValue< ::payeeIdentifier >(ident); 0087 } else if (ident.isNull()) { 0088 return QVariant(); 0089 } else if (role == payeeIdentifierType) { 0090 return ident.iid(); 0091 } else if (role == Qt::DisplayRole) { 0092 // The custom delegates won't ask for this role 0093 return QVariant::fromValue(i18n("The plugin to show this information could not be found.")); 0094 } 0095 } 0096 } 0097 0098 return QVariant(); 0099 } 0100 0101 Qt::ItemFlags payeeIdentifierModel::flags(const QModelIndex &index) const 0102 { 0103 Q_UNUSED(index) 0104 #if 0 0105 if (!index.parent().isValid()) { 0106 if (payeeByIndex(index).payeeIdentifierCount() > 0) 0107 return Qt::ItemIsEnabled; 0108 } 0109 #endif 0110 0111 return (Qt::ItemIsEnabled | Qt::ItemIsSelectable); 0112 } 0113 0114 /** 0115 * @internal The internalId of QModelIndex is set to the row of the parent or invalidParent if there is no 0116 * parent. 0117 * 0118 * @todo Qt5: the type of the internal id changed! 0119 */ 0120 QModelIndex payeeIdentifierModel::index(int row, int column, const QModelIndex &parent) const 0121 { 0122 if (parent.isValid()) 0123 return createIndex(row, column, parent.row()); 0124 return createIndex(row, column, invalidParent); 0125 } 0126 0127 int payeeIdentifierModel::columnCount(const QModelIndex& parent) const 0128 { 0129 Q_UNUSED(parent); 0130 return 1; 0131 } 0132 0133 int payeeIdentifierModel::rowCount(const QModelIndex& parent) const 0134 { 0135 if (parent.isValid()) { 0136 if (parent.internalId() != invalidParent) 0137 return 0; 0138 return payeeByIndex(parent).payeeIdentifierCount(); 0139 } 0140 return m_payeeIdentifierIds.count(); 0141 } 0142 0143 QModelIndex payeeIdentifierModel::parent(const QModelIndex& child) const 0144 { 0145 if (child.internalId() != invalidParent) 0146 return createIndex(child.internalId(), 0, invalidParent); 0147 return QModelIndex(); 0148 }