-
Notifications
You must be signed in to change notification settings - Fork 0
/
BudgetDataModel.cpp
271 lines (242 loc) · 7.23 KB
/
BudgetDataModel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "BudgetDataModel.h"
BudgetDataModel::BudgetDataModel(QObject *parent)
: QAbstractTableModel(parent)
{
// Set default values
m_BudgetData.insert(BudgetDataModel::RowEnum::HourlyPay, 18.0f);
m_BudgetData.insert(BudgetDataModel::RowEnum::HoursPerWeek, 40.0f);
m_BudgetData.insert(BudgetDataModel::RowEnum::WeeksPerYear, 52.0f);
m_BudgetData.insert(BudgetDataModel::RowEnum::Contrib401kPercent, 3.0f);
m_BudgetData.insert(BudgetDataModel::RowEnum::AnnualHealthInsurance, 5000.0f);
m_BudgetData.insert(BudgetDataModel::RowEnum::AnnualTaxPercent, 12.0f);
// Recalculate on startup
recalculateValues();
}
QVariant BudgetDataModel::headerData(int section, Qt::Orientation orientation, int role) const
{
switch(role)
{
case Qt::DisplayRole:
{
switch(orientation)
{
case Qt::Orientation::Horizontal:
{
switch(section)
{
case ColEnum::Amount:
{
return "Amount";
}
default:
return QVariant();
}
}
case Qt::Orientation::Vertical:
{
switch(section)
{
case RowEnum::HourlyPay:
{
return "Hourly Pay";
}
case RowEnum::HoursPerWeek:
{
return "Hours Per Week";
}
case RowEnum::WeeksPerYear:
{
return "Weeks Per Year";
}
case RowEnum::GrossSalary:
{
return "Gross Salary";
}
case RowEnum::Contrib401kPercent:
{
return "401K Contribution (%)";
}
case RowEnum::Contrib401k:
{
return "401K Contribution";
}
case RowEnum::AnnualHealthInsurance:
{
return "Annual Health Insurance";
}
case RowEnum::TaxableIncome:
{
return "Taxable Income";
}
case RowEnum::AnnualTaxPercent:
{
return "Annual Tax (%)";
}
case RowEnum::AnnualTax:
{
return "Annual Tax";
}
case RowEnum::AnnualTakeHome:
{
return "Annual Take Home";
}
case RowEnum::MonthlyTakeHome:
{
return "Monthly Take Home";
}
default:
return QVariant();
}
}
}
}
case Qt::BackgroundRole:
{
return m_tableBackgroundColor;
}
default:
return QVariant();
}
}
bool BudgetDataModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
{
if (value != headerData(section, orientation, role)) {
emit headerDataChanged(orientation, section, section);
return true;
}
return false;
}
int BudgetDataModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return RowEnum::numRows;
}
int BudgetDataModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return ColEnum::numColumns;
}
QVariant BudgetDataModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
{
return QVariant();
}
switch(role)
{
case Qt::DisplayRole:
{
switch(index.column())
{
case ColEnum::Amount:
{
switch(index.row())
{
// Fallthrough to handle displaying percentages
case RowEnum::Contrib401kPercent:
case RowEnum::AnnualTaxPercent:
{
BudgetDataModel::RowEnum row = static_cast<BudgetDataModel::RowEnum>(index.row());
return QString::number(m_BudgetData.value(row), 'f', 2) + '%';
}
default:
{
BudgetDataModel::RowEnum row = static_cast<BudgetDataModel::RowEnum>(index.row());
return QString::number(m_BudgetData.value(row), 'f', 2);
}
}
}
}
}
case Qt::BackgroundRole:
{
if(flags(index) & Qt::ItemIsEditable)
{
return m_tableBackgroundColor.lighter();
}
else
{
return m_tableBackgroundColor;
}
}
case Qt::TextAlignmentRole:
return Qt::AlignmentFlag::AlignRight;
}
return QVariant();
}
bool BudgetDataModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
switch(role)
{
case Qt::EditRole:
{
switch(index.column())
{
case BudgetDataModel::ColEnum::Amount:
{
m_BudgetData.insert(static_cast<BudgetDataModel::RowEnum>(index.row()), value.toFloat());
}
}
}
}
if (data(index, role) != value)
{
recalculateValues();
emit dataChanged(index, index, {role});
return true;
}
return false;
}
Qt::ItemFlags BudgetDataModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
switch(index.row())
{
// Fallthrough to the editables
case RowEnum::HourlyPay:
case RowEnum::HoursPerWeek:
case RowEnum::WeeksPerYear:
case RowEnum::Contrib401kPercent:
case RowEnum::AnnualHealthInsurance:
case RowEnum::AnnualTaxPercent:
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
// Fallthrough to the uneditables
case RowEnum::AnnualTax:
case RowEnum::GrossSalary:
case RowEnum::Contrib401k:
case RowEnum::TaxableIncome:
case RowEnum::AnnualTakeHome:
case RowEnum::MonthlyTakeHome:
return QAbstractItemModel::flags(index);
}
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}
float BudgetDataModel::getMonthlyTakeHome() const
{
return m_BudgetData.value(RowEnum::MonthlyTakeHome);
}
void BudgetDataModel::recalculateValues()
{
float grossSalary = m_BudgetData.value(RowEnum::HourlyPay)
* m_BudgetData.value(RowEnum::HoursPerWeek)
* m_BudgetData.value(RowEnum::WeeksPerYear);
m_BudgetData.insert(RowEnum::GrossSalary, grossSalary);
float contrib401kPerYear = m_BudgetData.value(RowEnum::GrossSalary)
* (m_BudgetData.value(RowEnum::Contrib401kPercent) / 100.0f);
m_BudgetData.insert(RowEnum::Contrib401k, contrib401kPerYear);
float taxableIncome = m_BudgetData.value(RowEnum::GrossSalary)
- m_BudgetData.value(RowEnum::Contrib401k)
- m_BudgetData.value(RowEnum::AnnualHealthInsurance);
m_BudgetData.insert(RowEnum::TaxableIncome, taxableIncome);
float annualTax = m_BudgetData.value(RowEnum::TaxableIncome)
* (m_BudgetData.value(RowEnum::AnnualTaxPercent) / 100.0f);
m_BudgetData.insert(RowEnum::AnnualTax, annualTax);
float annualTakeHome = m_BudgetData.value(RowEnum::TaxableIncome)
- m_BudgetData.value(RowEnum::AnnualTax);
m_BudgetData.insert(RowEnum::AnnualTakeHome, annualTakeHome);
float monthlyTakeHome = m_BudgetData.value(RowEnum::AnnualTakeHome) / 12.0f;
m_BudgetData.insert(RowEnum::MonthlyTakeHome, monthlyTakeHome);
}