blob: 0aa29bbfcc878a54b240a12f2945dd2924941f02 [file] [log] [blame]
swissChili4b3105a2022-02-22 16:34:39 -08001#include "CellModel.h"
2
swissChili25620b02022-02-23 17:15:16 -08003CellModel::CellModel(Notebook *parent)
swissChili4b3105a2022-02-22 16:34:39 -08004 : QAbstractListModel(parent)
swissChili25620b02022-02-23 17:15:16 -08005 , _notebook(parent)
swissChili4b3105a2022-02-22 16:34:39 -08006{
7}
8
swissChili25620b02022-02-23 17:15:16 -08009CellModel::CellModel(const CellModel &other)
10 : CellModel(other._notebook)
swissChili4b3105a2022-02-22 16:34:39 -080011{
swissChili4b3105a2022-02-22 16:34:39 -080012}
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
swissChili25620b02022-02-23 17:15:16 -080021 return _notebook->_cells.size();
swissChili4b3105a2022-02-22 16:34:39 -080022}
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:
swissChilid85daa92022-02-24 15:29:02 -080032 return _notebook->_cells[index.row()]->code();
swissChili4b3105a2022-02-22 16:34:39 -080033 case ResultRole:
swissChilid85daa92022-02-24 15:29:02 -080034 return _notebook->_cells[index.row()]->result();
35 case UuidRole:
36 return _notebook->_cells[index.row()]->uuid();
37 case StatusRole:
38 return _notebook->_cells[index.row()]->status();
swissChili4b3105a2022-02-22 16:34:39 -080039 default:
40 return QVariant();
41 }
42}
43
44bool CellModel::setData(const QModelIndex &index, const QVariant &value, int role)
45{
46 if (data(index, role) != value)
47 {
48 switch (role)
49 {
50 case CodeRole:
swissChilid85daa92022-02-24 15:29:02 -080051 _notebook->_cells[index.row()]->setCode(value.toString());
swissChili4b3105a2022-02-22 16:34:39 -080052 break;
53 case ResultRole:
swissChilid85daa92022-02-24 15:29:02 -080054 _notebook->_cells[index.row()]->setResult(value.toString());
55 break;
56 case StatusRole:
57 _notebook->_cells[index.row()]->setStatus(value.toInt());
swissChili4b3105a2022-02-22 16:34:39 -080058 break;
59 default:
60 return false;
61 }
62
63 emit dataChanged(index, index, QVector<int>() << role);
64
65 return true;
66 }
67
68 return false;
69}
70
71Qt::ItemFlags CellModel::flags(const QModelIndex &index) const
72{
73 if (!index.isValid())
74 return Qt::NoItemFlags;
75
swissChilid85daa92022-02-24 15:29:02 -080076 return Qt::ItemIsEditable;
swissChili4b3105a2022-02-22 16:34:39 -080077}
78
79bool CellModel::insertRows(int row, int count, const QModelIndex &parent)
80{
81 beginInsertRows(parent, row, row + count - 1);
82
83 for (int i = 0; i < count; i++)
swissChilid85daa92022-02-24 15:29:02 -080084 {
85 Cell *cell = new Cell(this);
86
87 connect(cell, &Cell::codeChanged, this, [this, cell](QString)
88 {
89 announceCellChange(cell, CodeRole);
90 });
91
92 connect(cell, &Cell::resultChanged, this, [this, cell](QString)
93 {
94 announceCellChange(cell, ResultRole);
95 });
96
97 connect(cell, &Cell::statusChanged, this, [this, cell](int)
98 {
99 announceCellChange(cell, StatusRole);
100 });
101
102 _notebook->_cells.insert(row, cell);
103 }
swissChili4b3105a2022-02-22 16:34:39 -0800104
105 endInsertRows();
106
107 return false;
108}
109
110bool CellModel::removeRows(int row, int count, const QModelIndex &parent)
111{
112 beginRemoveRows(parent, row, row + count - 1);
113
114 for (int i = 0; i < count; i++)
swissChilid85daa92022-02-24 15:29:02 -0800115 {
116 delete _notebook->_cells[row];
swissChili25620b02022-02-23 17:15:16 -0800117 _notebook->_cells.removeAt(row);
swissChilid85daa92022-02-24 15:29:02 -0800118 }
swissChili4b3105a2022-02-22 16:34:39 -0800119
120 endRemoveRows();
121
122 return true;
123}
124
125QHash<int, QByteArray> CellModel::roleNames() const
126{
127 return {
128 {CodeRole, "code"},
129 {ResultRole, "result"},
swissChilid85daa92022-02-24 15:29:02 -0800130 {UuidRole, "uuid"},
131 {StatusRole, "status"},
swissChili4b3105a2022-02-22 16:34:39 -0800132 };
133}
134
135void CellModel::addCell(const Cell &cell)
136{
swissChili25620b02022-02-23 17:15:16 -0800137 int i = _notebook->_cells.size();
swissChili4b3105a2022-02-22 16:34:39 -0800138
139 insertRows(i, 1, QModelIndex());
140
swissChilid85daa92022-02-24 15:29:02 -0800141 *_notebook->_cells[i] = cell;
swissChili4b3105a2022-02-22 16:34:39 -0800142 emit dataChanged(index(i), index(i), {CodeRole, ResultRole});
143}
144
145void CellModel::addCell(QString code, QString result)
146{
147 addCell(Cell(code, result));
148}
swissChilid85daa92022-02-24 15:29:02 -0800149
swissChili5d3e5562022-02-24 16:49:19 -0800150void CellModel::insertCellBefore(int index)
151{
152 insertRow(index);
153}
154
swissChilid85daa92022-02-24 15:29:02 -0800155void CellModel::announceCellChange(Cell *cell, int role)
156{
157 // TODO: Optimize
158
159 for (int i = 0; i < rowCount(); i++)
160 {
161 if (_notebook->_cells[i] == cell)
162 {
163 emit dataChanged(index(i), index(i), QVector<int>() << role);
164 break;
165 }
166 }
167}