blob: 8ed684f5f34019382a2a266fbbdbc62803881dcf [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();
swissChili4b3105a2022-02-22 16:34:39 -080040 default:
41 return QVariant();
42 }
43}
44
45bool CellModel::setData(const QModelIndex &index, const QVariant &value, int role)
46{
47 if (data(index, role) != value)
48 {
49 switch (role)
50 {
51 case CodeRole:
swissChilid85daa92022-02-24 15:29:02 -080052 _notebook->_cells[index.row()]->setCode(value.toString());
swissChili4b3105a2022-02-22 16:34:39 -080053 break;
54 case ResultRole:
swissChilid85daa92022-02-24 15:29:02 -080055 _notebook->_cells[index.row()]->setResult(value.toString());
56 break;
57 case StatusRole:
58 _notebook->_cells[index.row()]->setStatus(value.toInt());
swissChili4b3105a2022-02-22 16:34:39 -080059 break;
60 default:
61 return false;
62 }
63
64 emit dataChanged(index, index, QVector<int>() << role);
65
66 return true;
67 }
68
69 return false;
70}
71
72Qt::ItemFlags CellModel::flags(const QModelIndex &index) const
73{
74 if (!index.isValid())
75 return Qt::NoItemFlags;
76
swissChilid85daa92022-02-24 15:29:02 -080077 return Qt::ItemIsEditable;
swissChili4b3105a2022-02-22 16:34:39 -080078}
79
80bool CellModel::insertRows(int row, int count, const QModelIndex &parent)
81{
82 beginInsertRows(parent, row, row + count - 1);
83
84 for (int i = 0; i < count; i++)
swissChilid85daa92022-02-24 15:29:02 -080085 {
86 Cell *cell = new Cell(this);
87
88 connect(cell, &Cell::codeChanged, this, [this, cell](QString)
89 {
90 announceCellChange(cell, CodeRole);
91 });
92
93 connect(cell, &Cell::resultChanged, this, [this, cell](QString)
94 {
95 announceCellChange(cell, ResultRole);
96 });
97
98 connect(cell, &Cell::statusChanged, this, [this, cell](int)
99 {
100 announceCellChange(cell, StatusRole);
101 });
102
103 _notebook->_cells.insert(row, cell);
104 }
swissChili4b3105a2022-02-22 16:34:39 -0800105
106 endInsertRows();
107
108 return false;
109}
110
111bool CellModel::removeRows(int row, int count, const QModelIndex &parent)
112{
113 beginRemoveRows(parent, row, row + count - 1);
114
115 for (int i = 0; i < count; i++)
swissChilid85daa92022-02-24 15:29:02 -0800116 {
117 delete _notebook->_cells[row];
swissChili25620b02022-02-23 17:15:16 -0800118 _notebook->_cells.removeAt(row);
swissChilid85daa92022-02-24 15:29:02 -0800119 }
swissChili4b3105a2022-02-22 16:34:39 -0800120
121 endRemoveRows();
122
123 return true;
124}
125
126QHash<int, QByteArray> CellModel::roleNames() const
127{
128 return {
129 {CodeRole, "code"},
130 {ResultRole, "result"},
swissChilid85daa92022-02-24 15:29:02 -0800131 {UuidRole, "uuid"},
132 {StatusRole, "status"},
swissChili4b3105a2022-02-22 16:34:39 -0800133 };
134}
135
136void CellModel::addCell(const Cell &cell)
137{
swissChili25620b02022-02-23 17:15:16 -0800138 int i = _notebook->_cells.size();
swissChili4b3105a2022-02-22 16:34:39 -0800139
140 insertRows(i, 1, QModelIndex());
141
swissChilid85daa92022-02-24 15:29:02 -0800142 *_notebook->_cells[i] = cell;
swissChili4b3105a2022-02-22 16:34:39 -0800143 emit dataChanged(index(i), index(i), {CodeRole, ResultRole});
144}
145
146void CellModel::addCell(QString code, QString result)
147{
148 addCell(Cell(code, result));
149}
swissChilid85daa92022-02-24 15:29:02 -0800150
swissChili5d3e5562022-02-24 16:49:19 -0800151void CellModel::insertCellBefore(int index)
152{
153 insertRow(index);
154}
155
swissChili06cec4e2022-02-24 19:04:32 -0800156void CellModel::deleteCellAt(int index)
157{
158 removeRow(index);
159}
160
swissChilid85daa92022-02-24 15:29:02 -0800161void CellModel::announceCellChange(Cell *cell, int role)
162{
163 // TODO: Optimize
164
165 for (int i = 0; i < rowCount(); i++)
166 {
167 if (_notebook->_cells[i] == cell)
168 {
169 emit dataChanged(index(i), index(i), QVector<int>() << role);
170 break;
171 }
172 }
173}