-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserExpensesModel.cpp
148 lines (130 loc) · 3.17 KB
/
UserExpensesModel.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
#include "UserExpensesModel.h"
UserExpensesModel::UserExpensesModel(QObject *parent)
: QAbstractTableModel(parent)
{
m_Expenses.append(new Expense("E1", 15.5f));
m_Expenses.append(new Expense("E2", 16.7f));
}
QVariant UserExpensesModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(orientation != Qt::Orientation::Horizontal)
{
return QVariant();
}
switch(role)
{
case Qt::DisplayRole:
{
switch(section)
{
case ColEnum::ExpenseName:
{
return "Expense Name";
}
case ColEnum::Amount:
{
return "Amount";
}
}
}
}
return QVariant();
}
bool UserExpensesModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
{
if (value != headerData(section, orientation, role)) {
// FIXME: Implement me!
emit headerDataChanged(orientation, section, section);
return true;
}
return false;
}
int UserExpensesModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return m_Expenses.length();
}
int UserExpensesModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return numCols;
}
QVariant UserExpensesModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
switch(role)
{
case Qt::DisplayRole:
{
switch(index.column())
{
case ColEnum::ExpenseName:
{
return QString(m_Expenses.at(index.row())->name);
}
case ColEnum::Amount:
{
return QString::number(m_Expenses.at(index.row())->amount, 'f', 2);
}
}
}
case Qt::BackgroundRole:
{
if(flags(index) & Qt::ItemIsEditable)
{
return m_tableBackgroundColor.lighter();
}
else
{
return m_tableBackgroundColor;
}
}
case Qt::TextAlignmentRole:
{
switch(index.column())
{
case ColEnum::ExpenseName:
{
return Qt::AlignmentFlag::AlignLeft;
}
case ColEnum::Amount:
{
return Qt::AlignmentFlag::AlignRight;
}
}
}
}
return QVariant();
}
bool UserExpensesModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (data(index, role) != value) {
// FIXME: Implement me!
emit dataChanged(index, index, {role});
return true;
}
return false;
}
Qt::ItemFlags UserExpensesModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}
bool UserExpensesModel::insertRows(int row, int count, const QModelIndex &parent)
{
beginInsertRows(parent, row, row + count - 1);
// FIXME: Implement me!
endInsertRows();
return true;
}
bool UserExpensesModel::removeRows(int row, int count, const QModelIndex &parent)
{
beginRemoveRows(parent, row, row + count - 1);
// FIXME: Implement me!
endRemoveRows();
return true;
}