File indexing completed on 2024-05-12 05:06:14
0001 /* 0002 SPDX-FileCopyrightText: 2021 Thomas Baumgart <tbaumgart@kde.org> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "comboboxmodels.h" 0007 0008 // ---------------------------------------------------------------------------- 0009 // QT Includes 0010 0011 #include <QVector> 0012 0013 // ---------------------------------------------------------------------------- 0014 // KDE Includes 0015 0016 #include <KLocalizedString> 0017 0018 // ---------------------------------------------------------------------------- 0019 // Project Includes 0020 0021 #include "mymoneyschedule.h" 0022 0023 using namespace eMyMoney::Schedule; 0024 using namespace eMyMoney::Model; 0025 0026 class OccurrencesModelPrivate 0027 { 0028 Q_DECLARE_PUBLIC(OccurrencesModel); 0029 OccurrencesModel* q_ptr; 0030 0031 public: 0032 explicit OccurrencesModelPrivate(OccurrencesModel* qq) 0033 : q_ptr(qq) 0034 { 0035 Q_Q(OccurrencesModel); 0036 const QVector<Occurrence> occurrences{Occurrence::Once, 0037 Occurrence::Daily, 0038 Occurrence::Weekly, 0039 Occurrence::EveryOtherWeek, 0040 Occurrence::EveryHalfMonth, 0041 Occurrence::EveryThreeWeeks, 0042 Occurrence::EveryThirtyDays, 0043 Occurrence::EveryFourWeeks, 0044 Occurrence::Monthly, 0045 Occurrence::EveryEightWeeks, 0046 Occurrence::EveryOtherMonth, 0047 Occurrence::EveryThreeMonths, 0048 Occurrence::EveryFourMonths, 0049 Occurrence::TwiceYearly, 0050 Occurrence::Yearly, 0051 Occurrence::EveryOtherYear}; 0052 q->insertRows(0, occurrences.count()); 0053 int row = 0; 0054 for (const auto& occurrence : occurrences) { 0055 const auto text = MyMoneySchedule::occurrenceToString(occurrence); 0056 rowToScheduleMap.insert(row, occurrence); 0057 scheduleToRowMap.insert(occurrence, row); 0058 scheduleToStringMap.insert(occurrence, text); 0059 stringToScheduleMap.insert(text, occurrence); 0060 q->setData(q->index(row++, 0), text, Qt::DisplayRole); 0061 } 0062 } 0063 0064 ~OccurrencesModelPrivate() 0065 { 0066 } 0067 0068 QHash<Occurrence, QString> scheduleToStringMap; 0069 QHash<QString, Occurrence> stringToScheduleMap; 0070 QHash<Occurrence, int> scheduleToRowMap; 0071 QHash<int, Occurrence> rowToScheduleMap; 0072 }; 0073 0074 OccurrencesModel::OccurrencesModel(QObject* parent) 0075 : QStringListModel(parent) 0076 , d_ptr(new OccurrencesModelPrivate(this)) 0077 { 0078 } 0079 0080 OccurrencesModel::~OccurrencesModel() 0081 { 0082 Q_D(OccurrencesModel); 0083 delete d; 0084 } 0085 0086 QVariant OccurrencesModel::data(const QModelIndex& idx, int role) const 0087 { 0088 if (!idx.isValid()) 0089 return {}; 0090 if (idx.row() < 0 || idx.row() > (rowCount() - 1)) 0091 return {}; 0092 0093 Q_D(const OccurrencesModel); 0094 switch (role) { 0095 case ScheduleFrequencyRole: 0096 return QVariant::fromValue(d->rowToScheduleMap.value(idx.row())); 0097 break; 0098 } 0099 return QStringListModel::data(idx, role); 0100 } 0101 0102 QModelIndex OccurrencesModel::indexByOccurrence(eMyMoney::Schedule::Occurrence occurrence) const 0103 { 0104 Q_D(const OccurrencesModel); 0105 if (d->scheduleToRowMap.contains(occurrence)) { 0106 return index(d->scheduleToRowMap.value(occurrence), 0); 0107 } 0108 return {}; 0109 } 0110 0111 class PaymentMethodModelPrivate 0112 { 0113 Q_DECLARE_PUBLIC(PaymentMethodModel); 0114 PaymentMethodModel* q_ptr; 0115 0116 public: 0117 explicit PaymentMethodModelPrivate(PaymentMethodModel* qq) 0118 : q_ptr(qq) 0119 { 0120 Q_Q(PaymentMethodModel); 0121 const QVector<PaymentType> paymentMethods{PaymentType::DirectDeposit, 0122 PaymentType::ManualDeposit, 0123 PaymentType::DirectDebit, 0124 PaymentType::StandingOrder, 0125 PaymentType::BankTransfer, 0126 PaymentType::WriteChecque, 0127 PaymentType::Other}; 0128 0129 q->insertRows(0, paymentMethods.count()); 0130 int row = 0; 0131 for (const auto& paymentMethod : paymentMethods) { 0132 const auto text = i18n(MyMoneySchedule::paymentMethodToString(paymentMethod)); 0133 paymentMethodToRowMap.insert(paymentMethod, row); 0134 rowToPaymentMethodMap.insert(row, paymentMethod); 0135 q->setData(q->index(row++, 0), text, Qt::DisplayRole); 0136 } 0137 } 0138 0139 ~PaymentMethodModelPrivate() 0140 { 0141 } 0142 0143 QHash<PaymentType, int> paymentMethodToRowMap; 0144 QHash<int, PaymentType> rowToPaymentMethodMap; 0145 }; 0146 0147 PaymentMethodModel::PaymentMethodModel(QObject* parent) 0148 : QStringListModel(parent) 0149 , d_ptr(new PaymentMethodModelPrivate(this)) 0150 { 0151 } 0152 0153 PaymentMethodModel::~PaymentMethodModel() 0154 { 0155 Q_D(PaymentMethodModel); 0156 delete d; 0157 } 0158 0159 QVariant PaymentMethodModel::data(const QModelIndex& idx, int role) const 0160 { 0161 if (!idx.isValid()) 0162 return {}; 0163 if (idx.row() < 0 || idx.row() > (rowCount() - 1)) 0164 return {}; 0165 0166 Q_D(const PaymentMethodModel); 0167 switch (role) { 0168 case SchedulePaymentTypeRole: 0169 return QVariant::fromValue(d->rowToPaymentMethodMap.value(idx.row())); 0170 break; 0171 } 0172 return QStringListModel::data(idx, role); 0173 } 0174 0175 QModelIndex PaymentMethodModel::indexByPaymentMethod(eMyMoney::Schedule::PaymentType type) const 0176 { 0177 Q_D(const PaymentMethodModel); 0178 if (d->paymentMethodToRowMap.contains(type)) { 0179 return index(d->paymentMethodToRowMap.value(type), 0); 0180 } 0181 return {}; 0182 }