File indexing completed on 2024-05-12 16:42:19

0001 /*
0002     SPDX-FileCopyrightText: 2014-2015 Christian Dávid <christian-david@web.de>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "payeeidentifiercontainermodel.h"
0007 #include "payeeidentifier/payeeidentifier.h"
0008 #include "payeeidentifier/ibanbic/ibanbic.h"
0009 #include "payeeidentifier/nationalaccount/nationalaccount.h"
0010 
0011 #include <KLocalizedString>
0012 
0013 payeeIdentifierContainerModel::payeeIdentifierContainerModel(QObject* parent)
0014     : QAbstractListModel(parent),
0015       m_data(QSharedPointer<MyMoneyPayeeIdentifierContainer>())
0016 {
0017 }
0018 
0019 QVariant payeeIdentifierContainerModel::data(const QModelIndex& index, int role) const
0020 {
0021     // Needed for the selection box and it prevents a crash if index is out of range
0022     if (m_data.isNull() || index.row() >= rowCount(index.parent()) - 1)
0023         return QVariant();
0024 
0025     const ::payeeIdentifier ident = m_data->payeeIdentifiers().at(index.row());
0026 
0027     if (role == payeeIdentifier) {
0028         return QVariant::fromValue< ::payeeIdentifier >(ident);
0029     } else if (ident.isNull()) {
0030         return QVariant();
0031     } else if (role == payeeIdentifierType) {
0032         return ident.iid();
0033     } else if (role == Qt::DisplayRole) {
0034         // The custom delegates won't ask for this role
0035         return QVariant::fromValue(i18n("The plugin to show this information could not be found."));
0036     }
0037     return QVariant();
0038 }
0039 
0040 bool payeeIdentifierContainerModel::setData(const QModelIndex& index, const QVariant& value, int role)
0041 {
0042     if (!m_data.isNull() && role == payeeIdentifier) {
0043         ::payeeIdentifier ident = value.value< ::payeeIdentifier >();
0044         if (index.row() == rowCount(index.parent()) - 1) {
0045             // The new row will be the last but one
0046             beginInsertRows(index.parent(), index.row() - 1, index.row() - 1);
0047             m_data->addPayeeIdentifier(ident);
0048             endInsertRows();
0049         } else {
0050             m_data->modifyPayeeIdentifier(index.row(), ident);
0051             emit dataChanged(createIndex(index.row(), 0), createIndex(index.row(), 0));
0052         }
0053         return true;
0054     }
0055     return QAbstractItemModel::setData(index, value, role);
0056 }
0057 
0058 Qt::ItemFlags payeeIdentifierContainerModel::flags(const QModelIndex& index) const
0059 {
0060     static const QVector<QString> editableDelegates {
0061         payeeIdentifiers::ibanBic::staticPayeeIdentifierIid(),
0062         payeeIdentifiers::nationalAccount::staticPayeeIdentifierIid()
0063     };
0064     auto flags = QAbstractItemModel::flags(index) | Qt::ItemIsDragEnabled;
0065     const auto type = data(index, payeeIdentifierType).toString();
0066 
0067     // type.isEmpty() means the type selection can be shown
0068     if (!type.isEmpty() && editableDelegates.contains(type))
0069         flags |= Qt::ItemIsEditable;
0070     return flags;
0071 }
0072 
0073 int payeeIdentifierContainerModel::rowCount(const QModelIndex& parent) const
0074 {
0075     Q_UNUSED(parent);
0076     if (m_data.isNull())
0077         return 0;
0078     // Always a row more which creates new entries
0079     return m_data->payeeIdentifiers().count() + 1;
0080 }
0081 
0082 /** @brief unused at the moment */
0083 bool payeeIdentifierContainerModel::insertRows(int row, int count, const QModelIndex& parent)
0084 {
0085     Q_UNUSED(row);
0086     Q_UNUSED(count);
0087     Q_UNUSED(parent);
0088     return false;
0089 }
0090 
0091 bool payeeIdentifierContainerModel::removeRows(int row, int count, const QModelIndex& parent)
0092 {
0093     if (m_data.isNull())
0094         return false;
0095 
0096     if (count < 1 || row + count >= rowCount(parent))
0097         return false;
0098 
0099     beginRemoveRows(parent, row, row + count - 1);
0100     for (int i = row; i < row + count; ++i) {
0101         m_data->removePayeeIdentifier(i);
0102     }
0103     endRemoveRows();
0104     return true;
0105 }
0106 
0107 void payeeIdentifierContainerModel::setSource(const MyMoneyPayeeIdentifierContainer data)
0108 {
0109     beginResetModel();
0110     m_data = QSharedPointer<MyMoneyPayeeIdentifierContainer>(new MyMoneyPayeeIdentifierContainer(data));
0111     endResetModel();
0112 }
0113 
0114 void payeeIdentifierContainerModel::closeSource()
0115 {
0116     beginResetModel();
0117     m_data = QSharedPointer<MyMoneyPayeeIdentifierContainer>();
0118     endResetModel();
0119 }
0120 
0121 QList< ::payeeIdentifier > payeeIdentifierContainerModel::identifiers() const
0122 {
0123     if (m_data.isNull())
0124         return QList< ::payeeIdentifier >();
0125     return m_data->payeeIdentifiers();
0126 }