blob: 41acbe750204bd73a430668f342b3a45c317d615 [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:
swissChili25620b02022-02-23 17:15:16 -080032 return _notebook->_cells[index.row()].code();
swissChili4b3105a2022-02-22 16:34:39 -080033 case ResultRole:
swissChili25620b02022-02-23 17:15:16 -080034 return _notebook->_cells[index.row()].result();
swissChili4b3105a2022-02-22 16:34:39 -080035 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:
swissChili25620b02022-02-23 17:15:16 -080047 _notebook->_cells[index.row()].setCode(value.toString());
swissChili4b3105a2022-02-22 16:34:39 -080048 break;
49 case ResultRole:
swissChili25620b02022-02-23 17:15:16 -080050 _notebook->_cells[index.row()].setResult(value.toString());
swissChili4b3105a2022-02-22 16:34:39 -080051 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++)
swissChili25620b02022-02-23 17:15:16 -080077 _notebook->_cells.insert(row, Cell());
swissChili4b3105a2022-02-22 16:34:39 -080078
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++)
swissChili25620b02022-02-23 17:15:16 -080089 _notebook->_cells.removeAt(row);
swissChili4b3105a2022-02-22 16:34:39 -080090
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{
swissChili25620b02022-02-23 17:15:16 -0800106 int i = _notebook->_cells.size();
swissChili4b3105a2022-02-22 16:34:39 -0800107
108 insertRows(i, 1, QModelIndex());
109
swissChili25620b02022-02-23 17:15:16 -0800110 _notebook->_cells[i] = cell;
swissChili4b3105a2022-02-22 16:34:39 -0800111 emit dataChanged(index(i), index(i), {CodeRole, ResultRole});
112}
113
114void CellModel::addCell(QString code, QString result)
115{
116 addCell(Cell(code, result));
117}