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

0001 /*
0002     SPDX-FileCopyrightText: 2009-2015 Cristian OneČ› <onet.cristian@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "models.h"
0007 
0008 // ----------------------------------------------------------------------------
0009 // QT Includes
0010 
0011 // ----------------------------------------------------------------------------
0012 // KDE Includes
0013 
0014 // ----------------------------------------------------------------------------
0015 // Project Includes
0016 
0017 #include "accountsmodel.h"
0018 #ifdef ENABLE_UNFINISHEDFEATURES
0019 #include "ledgermodel.h"
0020 #endif
0021 #include "costcentermodel.h"
0022 #include "payeesmodel.h"
0023 #include "equitiesmodel.h"
0024 #include "securitiesmodel.h"
0025 
0026 #ifdef KMM_MODELTEST
0027 #include "modeltest.h"
0028 #endif
0029 
0030 Q_GLOBAL_STATIC(Models, models);
0031 
0032 struct Models::Private {
0033     Private()
0034         : m_accountsModel(0)
0035         , m_institutionsModel(0)
0036 #ifdef ENABLE_UNFINISHEDFEATURES
0037         , m_ledgerModel(0)
0038 #endif
0039         , m_costCenterModel(0)
0040         , m_payeesModel(0)
0041         , m_equitiesModel(0)
0042         , m_securitiesModel(0)
0043     {}
0044 
0045     AccountsModel *m_accountsModel;
0046     InstitutionsModel *m_institutionsModel;
0047 #ifdef ENABLE_UNFINISHEDFEATURES
0048     LedgerModel *m_ledgerModel;
0049 #endif
0050     CostCenterModel *m_costCenterModel;
0051     PayeesModel *m_payeesModel;
0052     EquitiesModel *m_equitiesModel;
0053     SecuritiesModel *m_securitiesModel;
0054 };
0055 
0056 
0057 /**
0058   * This object is a singleton so it will be created very early in the application's life
0059   * so don't do anything on this constructor that relies on application data (even a i18n call).
0060   */
0061 Models::Models() : QObject(), d(new Private)
0062 {
0063 }
0064 
0065 Models::~Models()
0066 {
0067     delete d;
0068 }
0069 
0070 Models* Models::instance()
0071 {
0072     return models;
0073 }
0074 
0075 /**
0076   * This is the function to get a reference to the core @ref AccountsModel object.
0077   * The returned object is owned by this object so don't delete it. It creates the
0078   * model on the first access to it.
0079   */
0080 AccountsModel* Models::accountsModel()
0081 {
0082     if (!d->m_accountsModel) {
0083         d->m_accountsModel = new AccountsModel(this);
0084 #ifdef KMM_MODELTEST
0085         new ModelTest(d->m_accountsModel, Models::instance());
0086 #endif
0087     }
0088     return d->m_accountsModel;
0089 }
0090 
0091 /**
0092  * This is the function to get a reference to the core @ref InstitutionsModel.
0093  * The returned object is owned by this object so don't delete it. It creates the
0094  * model on the first access to it.
0095  */
0096 InstitutionsModel* Models::institutionsModel()
0097 {
0098     if (!d->m_institutionsModel) {
0099         d->m_institutionsModel = new InstitutionsModel(this);
0100 #ifdef KMM_MODELTEST
0101         new ModelTest(d->m_institutionsModel, Models::instance());
0102 #endif
0103     }
0104     return d->m_institutionsModel;
0105 }
0106 
0107 #ifdef ENABLE_UNFINISHEDFEATURES
0108 /**
0109  * This is the function to get a reference to the core @ref LedgerModel.
0110  * The returned object is owned by this object so don't delete it. It creates the
0111  * model on the first access to it.
0112  */
0113 LedgerModel* Models::ledgerModel()
0114 {
0115     if (!d->m_ledgerModel) {
0116         d->m_ledgerModel = new LedgerModel(this);
0117 #ifdef KMM_MODELTEST
0118         new ModelTest(d->m_ledgerModel, Models::instance());
0119 #endif
0120     }
0121     return d->m_ledgerModel;
0122 }
0123 #endif
0124 
0125 /**
0126  * This is the function to get a reference to the core @ref CostCenterModel.
0127  * The returned object is owned by this object so don't delete it. It creates the
0128  * model on the first access to it.
0129  */
0130 CostCenterModel* Models::costCenterModel()
0131 {
0132     if (!d->m_costCenterModel) {
0133         d->m_costCenterModel = new CostCenterModel(this);
0134 #ifdef KMM_MODELTEST
0135         new ModelTest(d->m_costCenterModel, Models::instance());
0136 #endif
0137     }
0138     return d->m_costCenterModel;
0139 }
0140 
0141 /**
0142  * This is the function to get a reference to the core @ref PayeesModel.
0143  * The returned object is owned by this object so don't delete it. It creates the
0144  * model on the first access to it.
0145  */
0146 PayeesModel* Models::payeesModel()
0147 {
0148     if (!d->m_payeesModel) {
0149         d->m_payeesModel = new PayeesModel(this);
0150 #ifdef KMM_MODELTEST
0151         new ModelTest(d->m_payeesModel, Models::instance());
0152 #endif
0153     }
0154     return d->m_payeesModel;
0155 }
0156 
0157 /**
0158  * This is the function to get a reference to the core @ref EquitiesModel.
0159  * The returned object is owned by this object so don't delete it. It creates the
0160  * model on the first access to it.
0161  */
0162 EquitiesModel* Models::equitiesModel()
0163 {
0164     if (!d->m_equitiesModel) {
0165         d->m_equitiesModel = new EquitiesModel(this);
0166 #ifdef KMM_MODELTEST
0167         new ModelTest(d->m_equitiesModel, Models::instance());
0168 #endif
0169     }
0170     return d->m_equitiesModel;
0171 }
0172 
0173 /**
0174  * This is the function to get a reference to the core @ref SecuritiesModel.
0175  * The returned object is owned by this object so don't delete it. It creates the
0176  * model on the first access to it.
0177  */
0178 SecuritiesModel* Models::securitiesModel()
0179 {
0180     if (!d->m_securitiesModel) {
0181         d->m_securitiesModel = new SecuritiesModel(this);
0182 #ifdef KMM_MODELTEST
0183         new ModelTest(d->m_securitiesModel, Models::instance());
0184 #endif
0185     }
0186     return d->m_securitiesModel;
0187 }
0188 
0189 QModelIndex Models::indexById(QAbstractItemModel* model, int role, const QString& id)
0190 {
0191     QModelIndexList indexList = model->match(model->index(0, 0),
0192                                 role,
0193                                 id,
0194                                 1,
0195                                 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive));
0196 
0197     if(indexList.count() == 1) {
0198         return indexList.first();
0199     }
0200     return QModelIndex();
0201 
0202 }
0203 
0204 void Models::fileOpened()
0205 {
0206     accountsModel()->AccountsModel::load();
0207     institutionsModel()->InstitutionsModel::load();
0208     costCenterModel()->load();
0209 #ifdef ENABLE_UNFINISHEDFEATURES
0210     ledgerModel()->load();
0211 #endif
0212     payeesModel()->load();
0213     equitiesModel()->load();
0214     securitiesModel()->load();
0215 
0216     emit modelsLoaded();
0217 }
0218 
0219 void Models::fileClosed()
0220 {
0221     // TODO: make this cleaner in the future, for now just clear the accounts model before the file is closed
0222     // to avoid any uncaught KMyMoneyExceptions while using the account objects from this model after the file has been closed
0223     accountsModel()->removeRows(0, accountsModel()->rowCount());
0224     institutionsModel()->removeRows(0, institutionsModel()->rowCount());
0225 #ifdef ENABLE_UNFINISHEDFEATURES
0226     ledgerModel()->unload();
0227 #endif
0228     costCenterModel()->unload();
0229     payeesModel()->unload();
0230     equitiesModel()->removeRows(0, equitiesModel()->rowCount());
0231     securitiesModel()->removeRows(0, securitiesModel()->rowCount());
0232 }