File indexing completed on 2024-05-26 05:08:38

0001 /*
0002     SPDX-FileCopyrightText: 2016-2019 Thomas Baumgart <tbaumgart@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "budgetsmodel.h"
0007 
0008 // ----------------------------------------------------------------------------
0009 // QT Includes
0010 
0011 #include <QDebug>
0012 #include <QString>
0013 #include <QDate>
0014 #include <QRegularExpression>
0015 #include <QRegularExpressionMatch>
0016 
0017 // ----------------------------------------------------------------------------
0018 // KDE Includes
0019 
0020 #include <KLocalizedString>
0021 
0022 // ----------------------------------------------------------------------------
0023 // Project Includes
0024 
0025 struct BudgetsModel::Private
0026 {
0027     Private()
0028         : fiscalYearStartMonth(1)
0029         , fiscalYearStartDay(1)
0030     {}
0031 
0032     int fiscalYearStartMonth;
0033     int fiscalYearStartDay;
0034 };
0035 
0036 BudgetsModel::BudgetsModel(QObject* parent, QUndoStack* undoStack)
0037     : MyMoneyModel<MyMoneyBudget>(parent, QStringLiteral("B"), BudgetsModel::ID_SIZE, undoStack)
0038     , d(new Private)
0039 {
0040     setObjectName(QLatin1String("BudgetsModel"));
0041 }
0042 
0043 BudgetsModel::~BudgetsModel()
0044 {
0045 }
0046 
0047 int BudgetsModel::columnCount(const QModelIndex& parent) const
0048 {
0049     Q_UNUSED(parent);
0050     return 2;
0051 }
0052 
0053 Qt::ItemFlags BudgetsModel::flags(const QModelIndex &index) const
0054 {
0055     if (!index.isValid())
0056         return Qt::ItemFlags();
0057     if (index.row() < 0 || index.row() >= rowCount(index.parent()))
0058         return Qt::ItemFlags();
0059 
0060     return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
0061 }
0062 
0063 QVariant BudgetsModel::headerData(int section, Qt::Orientation orientation, int role) const
0064 {
0065     if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
0066         switch(section) {
0067         case 0:
0068             return i18n("Budget");
0069 
0070         case 1:
0071             return i18nc("Budget year", "Year");
0072         }
0073     }
0074     return MyMoneyModelBase::headerData(section, orientation, role);
0075 }
0076 
0077 QVariant BudgetsModel::data(const QModelIndex& index, int role) const
0078 {
0079     if (!index.isValid())
0080         return QVariant();
0081     if (index.row() < 0 || index.row() >= rowCount(index.parent()))
0082         return QVariant();
0083 
0084     const MyMoneyBudget& budget = static_cast<TreeItem<MyMoneyBudget>*>(index.internalPointer())->constDataRef();
0085     switch (role) {
0086     case Qt::DisplayRole:
0087     case Qt::EditRole:
0088         switch(index.column()) {
0089         case Columns::Name:
0090             return budget.name();
0091 
0092         case Columns::Year:
0093             return budget.budgetStart().year();
0094 
0095         }
0096         return QVariant();
0097 
0098     case Qt::TextAlignmentRole:
0099         return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
0100 
0101     case eMyMoney::Model::Roles::IdRole:
0102         return budget.id();
0103 
0104     case eMyMoney::Model::Roles::BudgetNameRole:
0105         return budget.name();
0106 
0107     default:
0108         break;
0109     }
0110     return QVariant();
0111 }
0112 
0113 bool BudgetsModel::setData(const QModelIndex& index, const QVariant& value, int role)
0114 {
0115     if (!index.isValid()) {
0116         return false;
0117     }
0118 
0119     if (index.row() < 0 || index.row() >= rowCount(index.parent()))
0120         return false;
0121 
0122     MyMoneyBudget& budget = static_cast<TreeItem<MyMoneyBudget>*>(index.internalPointer())->dataRef();
0123 
0124     switch(role) {
0125     case Qt::DisplayRole:
0126     case Qt::EditRole:
0127         switch(index.column()) {
0128         case Columns::Name:
0129             budget.setName(value.toString());
0130             break;
0131 
0132         case Columns::Year:
0133         {
0134             QDate date(value.toInt(), d->fiscalYearStartMonth, d->fiscalYearStartDay);
0135             if (date.isValid()) {
0136                 budget.setBudgetStart(QDate(value.toInt(), d->fiscalYearStartMonth, d->fiscalYearStartDay));
0137             } else {
0138                 return false;
0139             }
0140         }
0141         break;
0142 
0143         default:
0144             return false;
0145         }
0146         break;
0147 
0148     case eMyMoney::Model::Roles::BudgetNameRole:
0149         budget.setName(value.toString());
0150         break;
0151 
0152     default:
0153         return false;
0154     }
0155 
0156     setDirty();
0157     const auto topLeft = BudgetsModel::index(index.row(), 0);
0158     const auto bottomRight = BudgetsModel::index(index.row(), columnCount()-1);
0159     Q_EMIT dataChanged(topLeft, bottomRight);
0160 
0161     return true;
0162 }
0163 
0164 void BudgetsModel::setFiscalYearStart(int month, int day)
0165 {
0166     d->fiscalYearStartDay = day;
0167     d->fiscalYearStartMonth = month;
0168 }