File indexing completed on 2024-05-12 16:42:17
0001 /* 0002 SPDX-FileCopyrightText: 2016-2017 Thomas Baumgart <tbaumgart@kde.org> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "costcentermodel.h" 0007 0008 // ---------------------------------------------------------------------------- 0009 // QT Includes 0010 0011 #include <QDebug> 0012 #include <QString> 0013 0014 // ---------------------------------------------------------------------------- 0015 // KDE Includes 0016 0017 #include <KLocalizedString> 0018 0019 // ---------------------------------------------------------------------------- 0020 // Project Includes 0021 0022 #include "mymoneyfile.h" 0023 #include "mymoneycostcenter.h" 0024 0025 struct CostCenterModel::Private 0026 { 0027 Private() {} 0028 0029 QVector<MyMoneyCostCenter*> m_costCenterItems; 0030 }; 0031 0032 CostCenterModel::CostCenterModel(QObject* parent) 0033 : QAbstractListModel(parent) 0034 , d(new Private) 0035 { 0036 qDebug() << "Cost center model created with items" << d->m_costCenterItems.count(); 0037 d->m_costCenterItems.clear(); 0038 } 0039 0040 CostCenterModel::~CostCenterModel() 0041 { 0042 } 0043 0044 int CostCenterModel::rowCount(const QModelIndex& parent) const 0045 { 0046 // since the ledger model is a simple table model, we only 0047 // return the rowCount for the hiddenRootItem. and zero otherwise 0048 if(parent.isValid()) { 0049 return 0; 0050 } 0051 0052 return d->m_costCenterItems.count(); 0053 } 0054 0055 int CostCenterModel::columnCount(const QModelIndex& parent) const 0056 { 0057 Q_UNUSED(parent); 0058 return 1; 0059 } 0060 0061 Qt::ItemFlags CostCenterModel::flags(const QModelIndex& index) const 0062 { 0063 Qt::ItemFlags flags; 0064 0065 if(!index.isValid()) 0066 return flags; 0067 if(index.row() < 0 || index.row() >= d->m_costCenterItems.count()) 0068 return flags; 0069 0070 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable; 0071 } 0072 0073 0074 QVariant CostCenterModel::headerData(int section, Qt::Orientation orientation, int role) const 0075 { 0076 if(orientation == Qt::Horizontal && role == Qt::DisplayRole) { 0077 switch(section) { 0078 case 0: 0079 return i18n("Cost center"); 0080 break; 0081 } 0082 } 0083 return QAbstractItemModel::headerData(section, orientation, role); 0084 } 0085 0086 QVariant CostCenterModel::data(const QModelIndex& index, int role) const 0087 { 0088 if(!index.isValid()) 0089 return QVariant(); 0090 if(index.row() < 0 || index.row() >= d->m_costCenterItems.count()) 0091 return QVariant(); 0092 0093 QVariant rc; 0094 switch(role) { 0095 case Qt::DisplayRole: 0096 case Qt::EditRole: 0097 // make sure to never return any displayable text for the dummy entry 0098 if(!d->m_costCenterItems[index.row()]->id().isEmpty()) { 0099 rc = d->m_costCenterItems[index.row()]->name(); 0100 } else { 0101 rc = QString(); 0102 } 0103 break; 0104 0105 case Qt::TextAlignmentRole: 0106 rc = QVariant(Qt::AlignLeft | Qt::AlignTop); 0107 break; 0108 0109 case ShortNameRole: 0110 rc = d->m_costCenterItems[index.row()]->shortName(); 0111 break; 0112 0113 case CostCenterIdRole: 0114 rc = d->m_costCenterItems[index.row()]->id(); 0115 break; 0116 } 0117 return rc; 0118 } 0119 0120 bool CostCenterModel::setData(const QModelIndex& index, const QVariant& value, int role) 0121 { 0122 if(!index.isValid()) { 0123 return false; 0124 } 0125 0126 qDebug() << "setData(" << index.row() << index.column() << ")" << value << role; 0127 return QAbstractItemModel::setData(index, value, role); 0128 } 0129 0130 0131 0132 void CostCenterModel::unload() 0133 { 0134 if(rowCount() > 0) { 0135 beginRemoveRows(QModelIndex(), 0, rowCount() - 1); 0136 for(int i = 0; i < rowCount(); ++i) { 0137 delete d->m_costCenterItems[i]; 0138 } 0139 d->m_costCenterItems.clear(); 0140 endRemoveRows(); 0141 } 0142 } 0143 0144 void CostCenterModel::load() 0145 { 0146 QList<MyMoneyCostCenter> list; 0147 MyMoneyFile::instance()->costCenterList(list); 0148 0149 if(list.count() > 0) { 0150 beginInsertRows(QModelIndex(), rowCount(), rowCount() + list.count()); 0151 // create an empty entry for those items that do not reference a cost center 0152 d->m_costCenterItems.append((new MyMoneyCostCenter)); 0153 QList< MyMoneyCostCenter >::const_iterator it; 0154 for(it = list.constBegin(); it != list.constEnd(); ++it) { 0155 d->m_costCenterItems.append(new MyMoneyCostCenter(*it)); 0156 } 0157 endInsertRows(); 0158 } 0159 }