File indexing completed on 2024-05-19 05:07:26
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 #if defined(__GNUC__) 0019 #pragma GCC diagnostic push 0020 #pragma GCC diagnostic ignored "-Wnonnull" 0021 #endif 0022 static constexpr decltype(reinterpret_cast<QModelIndex*>(0)->internalId()) invalidParent = std::numeric_limits<decltype(reinterpret_cast<QModelIndex*>(0)->internalId())>::max(); 0023 #if defined(__GNUC__) 0024 #pragma GCC diagnostic pop 0025 #endif 0026 0027 payeeIdentifierModel::payeeIdentifierModel(QObject* parent) 0028 : QAbstractItemModel(parent), 0029 m_payeeIdentifierIds(), 0030 m_typeFilter() 0031 { 0032 0033 } 0034 0035 void payeeIdentifierModel::setTypeFilter(QStringList filter) 0036 { 0037 m_typeFilter = filter; 0038 loadData(); 0039 } 0040 0041 void payeeIdentifierModel::setTypeFilter(QString type) 0042 { 0043 setTypeFilter(QStringList(type)); 0044 } 0045 0046 0047 void payeeIdentifierModel::loadData() 0048 { 0049 beginResetModel(); 0050 0051 const QList<MyMoneyPayee> payees = MyMoneyFile::instance()->payeeList(); 0052 m_payeeIdentifierIds.clear(); 0053 m_payeeIdentifierIds.reserve(payees.count()); 0054 for (const MyMoneyPayee& payee : payees) { 0055 m_payeeIdentifierIds.append(payee.id()); 0056 } 0057 endResetModel(); 0058 } 0059 0060 MyMoneyPayee payeeIdentifierModel::payeeByIndex(const QModelIndex& index) const 0061 { 0062 if (index.isValid() && index.row() >= 0 && index.row() < m_payeeIdentifierIds.count()) { 0063 try { 0064 return MyMoneyFile::instance()->payee(m_payeeIdentifierIds.at(index.row())); 0065 } catch (const MyMoneyException &) { 0066 } 0067 } 0068 0069 return MyMoneyPayee(); 0070 } 0071 0072 QVariant payeeIdentifierModel::data(const QModelIndex& index, int role) const 0073 { 0074 if (!index.isValid()) 0075 return QVariant(); 0076 0077 const auto isPayeeIdentifierValid = index.parent().isValid(); 0078 if (role == payeeIdentifierModel::isPayeeIdentifier) 0079 return isPayeeIdentifierValid; 0080 0081 const MyMoneyPayee payee = (isPayeeIdentifierValid) ? payeeByIndex(index.parent()) : payeeByIndex(index); 0082 0083 0084 if (role == payeeName || (!isPayeeIdentifierValid && role == Qt::DisplayRole)) { 0085 // Return data for MyMoneyPayee 0086 return payee.name(); 0087 } else if (isPayeeIdentifierValid) { 0088 // Return data for payeeIdentifier 0089 if (index.row() >= 0 && static_cast<unsigned int>(index.row()) < payee.payeeIdentifierCount()) { 0090 ::payeeIdentifier ident = payee.payeeIdentifier(index.row()); 0091 0092 if (role == payeeIdentifier) { 0093 return QVariant::fromValue< ::payeeIdentifier >(ident); 0094 } else if (ident.isNull()) { 0095 return QVariant(); 0096 } else if (role == payeeIdentifierType) { 0097 return ident.iid(); 0098 } else if (role == Qt::DisplayRole) { 0099 // The custom delegates won't ask for this role 0100 return QVariant::fromValue(i18n("The plugin to show this information could not be found.")); 0101 } 0102 } 0103 } 0104 0105 return QVariant(); 0106 } 0107 0108 Qt::ItemFlags payeeIdentifierModel::flags(const QModelIndex &index) const 0109 { 0110 Q_UNUSED(index) 0111 #if 0 0112 if (!index.parent().isValid()) { 0113 if (payeeByIndex(index).payeeIdentifierCount() > 0) 0114 return Qt::ItemIsEnabled; 0115 } 0116 #endif 0117 if (!index.isValid()) { 0118 return Qt::NoItemFlags; 0119 } 0120 return (Qt::ItemIsEnabled | Qt::ItemIsSelectable); 0121 } 0122 0123 /** 0124 * @internal The internalId of QModelIndex is set to the row of the parent or invalidParent if there is no 0125 * parent. 0126 * 0127 * @todo Qt5: the type of the internal id changed! 0128 */ 0129 QModelIndex payeeIdentifierModel::index(int row, int column, const QModelIndex &parent) const 0130 { 0131 if (parent.isValid()) 0132 return createIndex(row, column, parent.row()); 0133 return createIndex(row, column, invalidParent); 0134 } 0135 0136 int payeeIdentifierModel::columnCount(const QModelIndex& parent) const 0137 { 0138 Q_UNUSED(parent); 0139 return 1; 0140 } 0141 0142 int payeeIdentifierModel::rowCount(const QModelIndex& parent) const 0143 { 0144 if (parent.isValid()) { 0145 if (parent.internalId() != invalidParent) 0146 return 0; 0147 return payeeByIndex(parent).payeeIdentifierCount(); 0148 } 0149 return m_payeeIdentifierIds.count(); 0150 } 0151 0152 QModelIndex payeeIdentifierModel::parent(const QModelIndex& child) const 0153 { 0154 if (child.internalId() != invalidParent) 0155 return createIndex(child.internalId(), 0, invalidParent); 0156 return QModelIndex(); 0157 }