blob: 167a2af66352a1ffc91649509e50a074a229e28a [file] [log] [blame]
swissChili4b3105a2022-02-22 16:34:39 -08001#include "CellModel.h"
swissChilie386bc72022-02-24 21:31:31 -08002#include "Notebook.h"
swissChili4b3105a2022-02-22 16:34:39 -08003
swissChili25620b02022-02-23 17:15:16 -08004CellModel::CellModel(Notebook *parent)
swissChili4b3105a2022-02-22 16:34:39 -08005 : QAbstractListModel(parent)
swissChili25620b02022-02-23 17:15:16 -08006 , _notebook(parent)
swissChili4b3105a2022-02-22 16:34:39 -08007{
8}
9
swissChili25620b02022-02-23 17:15:16 -080010CellModel::CellModel(const CellModel &other)
11 : CellModel(other._notebook)
swissChili4b3105a2022-02-22 16:34:39 -080012{
swissChili4b3105a2022-02-22 16:34:39 -080013}
14
15int CellModel::rowCount(const QModelIndex &parent) const
16{
17 // For list models only the root node (an invalid parent) should return the list's size. For all
18 // other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
19 if (parent.isValid())
20 return 0;
21
swissChili25620b02022-02-23 17:15:16 -080022 return _notebook->_cells.size();
swissChili4b3105a2022-02-22 16:34:39 -080023}
24
25QVariant CellModel::data(const QModelIndex &index, int role) const
26{
27 if (!index.isValid())
28 return QVariant();
29
30 switch (role)
31 {
32 case CodeRole:
swissChilid85daa92022-02-24 15:29:02 -080033 return _notebook->_cells[index.row()]->code();
swissChili4b3105a2022-02-22 16:34:39 -080034 case ResultRole:
swissChilid85daa92022-02-24 15:29:02 -080035 return _notebook->_cells[index.row()]->result();
36 case UuidRole:
37 return _notebook->_cells[index.row()]->uuid();
38 case StatusRole:
39 return _notebook->_cells[index.row()]->status();
swissChiliece1ac82022-02-25 11:20:42 -080040 case ResultTypeRole:
41 return _notebook->_cells[index.row()]->resultType();
swissChili4b3105a2022-02-22 16:34:39 -080042 default:
43 return QVariant();
44 }
45}
46
47bool CellModel::setData(const QModelIndex &index, const QVariant &value, int role)
48{
49 if (data(index, role) != value)
50 {
51 switch (role)
52 {
53 case CodeRole:
swissChilid85daa92022-02-24 15:29:02 -080054 _notebook->_cells[index.row()]->setCode(value.toString());
swissChili4b3105a2022-02-22 16:34:39 -080055 break;
56 case ResultRole:
swissChilid85daa92022-02-24 15:29:02 -080057 _notebook->_cells[index.row()]->setResult(value.toString());
58 break;
59 case StatusRole:
60 _notebook->_cells[index.row()]->setStatus(value.toInt());
swissChili4b3105a2022-02-22 16:34:39 -080061 break;
swissChiliece1ac82022-02-25 11:20:42 -080062 case ResultTypeRole:
63 _notebook->_cells[index.row()]->setResultType(value.toInt());
64 break;
swissChili4b3105a2022-02-22 16:34:39 -080065 default:
66 return false;
67 }
68
69 emit dataChanged(index, index, QVector<int>() << role);
70
71 return true;
72 }
73
74 return false;
75}
76
77Qt::ItemFlags CellModel::flags(const QModelIndex &index) const
78{
79 if (!index.isValid())
80 return Qt::NoItemFlags;
81
swissChilid85daa92022-02-24 15:29:02 -080082 return Qt::ItemIsEditable;
swissChili4b3105a2022-02-22 16:34:39 -080083}
84
85bool CellModel::insertRows(int row, int count, const QModelIndex &parent)
86{
87 beginInsertRows(parent, row, row + count - 1);
88
89 for (int i = 0; i < count; i++)
swissChilid85daa92022-02-24 15:29:02 -080090 {
91 Cell *cell = new Cell(this);
92
93 connect(cell, &Cell::codeChanged, this, [this, cell](QString)
94 {
95 announceCellChange(cell, CodeRole);
96 });
97
98 connect(cell, &Cell::resultChanged, this, [this, cell](QString)
99 {
100 announceCellChange(cell, ResultRole);
101 });
102
103 connect(cell, &Cell::statusChanged, this, [this, cell](int)
104 {
105 announceCellChange(cell, StatusRole);
106 });
107
swissChiliece1ac82022-02-25 11:20:42 -0800108 connect(cell, &Cell::resultTypeChanged, this, [this, cell](int)
109 {
110 announceCellChange(cell, ResultTypeRole);
111 });
112
swissChilid85daa92022-02-24 15:29:02 -0800113 _notebook->_cells.insert(row, cell);
114 }
swissChili4b3105a2022-02-22 16:34:39 -0800115
116 endInsertRows();
117
118 return false;
119}
120
121bool CellModel::removeRows(int row, int count, const QModelIndex &parent)
122{
123 beginRemoveRows(parent, row, row + count - 1);
124
125 for (int i = 0; i < count; i++)
swissChilid85daa92022-02-24 15:29:02 -0800126 {
127 delete _notebook->_cells[row];
swissChili25620b02022-02-23 17:15:16 -0800128 _notebook->_cells.removeAt(row);
swissChilid85daa92022-02-24 15:29:02 -0800129 }
swissChili4b3105a2022-02-22 16:34:39 -0800130
131 endRemoveRows();
132
133 return true;
134}
135
136QHash<int, QByteArray> CellModel::roleNames() const
137{
138 return {
139 {CodeRole, "code"},
140 {ResultRole, "result"},
swissChilid85daa92022-02-24 15:29:02 -0800141 {UuidRole, "uuid"},
142 {StatusRole, "status"},
swissChiliece1ac82022-02-25 11:20:42 -0800143 {ResultTypeRole, "resultType"},
swissChili4b3105a2022-02-22 16:34:39 -0800144 };
145}
146
147void CellModel::addCell(const Cell &cell)
148{
swissChili25620b02022-02-23 17:15:16 -0800149 int i = _notebook->_cells.size();
swissChili4b3105a2022-02-22 16:34:39 -0800150
151 insertRows(i, 1, QModelIndex());
152
swissChilid85daa92022-02-24 15:29:02 -0800153 *_notebook->_cells[i] = cell;
swissChili4b3105a2022-02-22 16:34:39 -0800154 emit dataChanged(index(i), index(i), {CodeRole, ResultRole});
155}
156
157void CellModel::addCell(QString code, QString result)
158{
159 addCell(Cell(code, result));
160}
swissChilid85daa92022-02-24 15:29:02 -0800161
swissChili5d3e5562022-02-24 16:49:19 -0800162void CellModel::insertCellBefore(int index)
163{
164 insertRow(index);
165}
166
swissChili06cec4e2022-02-24 19:04:32 -0800167void CellModel::deleteCellAt(int index)
168{
169 removeRow(index);
170}
171
swissChilid85daa92022-02-24 15:29:02 -0800172void CellModel::announceCellChange(Cell *cell, int role)
173{
174 // TODO: Optimize
175
176 for (int i = 0; i < rowCount(); i++)
177 {
178 if (_notebook->_cells[i] == cell)
179 {
180 emit dataChanged(index(i), index(i), QVector<int>() << role);
181 break;
182 }
183 }
184}