blob: 911c9a9144337ca6dd6a3d25a9c04f932867cacd [file] [log] [blame]
swissChili4b3105a2022-02-22 16:34:39 -08001#include "CellModel.h"
2
3CellModel::CellModel(QObject *parent)
4 : QAbstractListModel(parent)
5{
6}
7
8CellModel::CellModel(const CellModel &model, QObject *parent)
9 : CellModel(parent)
10{
11 _cells = model._cells;
12}
13
14int CellModel::rowCount(const QModelIndex &parent) const
15{
16 // For list models only the root node (an invalid parent) should return the list's size. For all
17 // other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
18 if (parent.isValid())
19 return 0;
20
21 return _cells.size();
22}
23
24QVariant CellModel::data(const QModelIndex &index, int role) const
25{
26 if (!index.isValid())
27 return QVariant();
28
29 switch (role)
30 {
31 case CodeRole:
32 return _cells[index.row()].code();
33 case ResultRole:
34 return _cells[index.row()].result();
35 default:
36 return QVariant();
37 }
38}
39
40bool CellModel::setData(const QModelIndex &index, const QVariant &value, int role)
41{
42 if (data(index, role) != value)
43 {
44 switch (role)
45 {
46 case CodeRole:
47 _cells[index.row()].setCode(value.toString());
48 break;
49 case ResultRole:
50 _cells[index.row()].setResult(value.toString());
51 break;
52 default:
53 return false;
54 }
55
56 emit dataChanged(index, index, QVector<int>() << role);
57
58 return true;
59 }
60
61 return false;
62}
63
64Qt::ItemFlags CellModel::flags(const QModelIndex &index) const
65{
66 if (!index.isValid())
67 return Qt::NoItemFlags;
68
69 return Qt::ItemIsEditable; // FIXME: Implement me!
70}
71
72bool CellModel::insertRows(int row, int count, const QModelIndex &parent)
73{
74 beginInsertRows(parent, row, row + count - 1);
75
76 for (int i = 0; i < count; i++)
77 _cells.insert(row, Cell());
78
79 endInsertRows();
80
81 return false;
82}
83
84bool CellModel::removeRows(int row, int count, const QModelIndex &parent)
85{
86 beginRemoveRows(parent, row, row + count - 1);
87
88 for (int i = 0; i < count; i++)
89 _cells.removeAt(row);
90
91 endRemoveRows();
92
93 return true;
94}
95
96QHash<int, QByteArray> CellModel::roleNames() const
97{
98 return {
99 {CodeRole, "code"},
100 {ResultRole, "result"},
101 };
102}
103
104void CellModel::addCell(const Cell &cell)
105{
106 int i = _cells.size();
107
108 insertRows(i, 1, QModelIndex());
109
110 _cells[i] = cell;
111 emit dataChanged(index(i), index(i), {CodeRole, ResultRole});
112}
113
114void CellModel::addCell(QString code, QString result)
115{
116 addCell(Cell(code, result));
117}