swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 1 | #include "CellModel.h" |
swissChili | e386bc7 | 2022-02-24 21:31:31 -0800 | [diff] [blame] | 2 | #include "Notebook.h" |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 3 | |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 4 | CellModel::CellModel(Notebook *parent) |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 5 | : QAbstractListModel(parent) |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 6 | , _notebook(parent) |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 7 | { |
| 8 | } |
| 9 | |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 10 | CellModel::CellModel(const CellModel &other) |
| 11 | : CellModel(other._notebook) |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 12 | { |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 13 | } |
| 14 | |
| 15 | int 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 | |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 22 | return _notebook->_cells.size(); |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | QVariant CellModel::data(const QModelIndex &index, int role) const |
| 26 | { |
| 27 | if (!index.isValid()) |
| 28 | return QVariant(); |
| 29 | |
| 30 | switch (role) |
| 31 | { |
| 32 | case CodeRole: |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 33 | return _notebook->_cells[index.row()]->code(); |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 34 | case ResultRole: |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 35 | 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(); |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 40 | case ResultTypeRole: |
| 41 | return _notebook->_cells[index.row()]->resultType(); |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 42 | default: |
| 43 | return QVariant(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | bool 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: |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 54 | _notebook->_cells[index.row()]->setCode(value.toString()); |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 55 | break; |
| 56 | case ResultRole: |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 57 | _notebook->_cells[index.row()]->setResult(value.toString()); |
| 58 | break; |
| 59 | case StatusRole: |
| 60 | _notebook->_cells[index.row()]->setStatus(value.toInt()); |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 61 | break; |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 62 | case ResultTypeRole: |
| 63 | _notebook->_cells[index.row()]->setResultType(value.toInt()); |
| 64 | break; |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 65 | 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 | |
| 77 | Qt::ItemFlags CellModel::flags(const QModelIndex &index) const |
| 78 | { |
| 79 | if (!index.isValid()) |
| 80 | return Qt::NoItemFlags; |
| 81 | |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 82 | return Qt::ItemIsEditable; |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | bool 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++) |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 90 | { |
| 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 | |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 108 | connect(cell, &Cell::resultTypeChanged, this, [this, cell](int) |
| 109 | { |
| 110 | announceCellChange(cell, ResultTypeRole); |
| 111 | }); |
| 112 | |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 113 | _notebook->_cells.insert(row, cell); |
| 114 | } |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 115 | |
| 116 | endInsertRows(); |
| 117 | |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | bool 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++) |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 126 | { |
| 127 | delete _notebook->_cells[row]; |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 128 | _notebook->_cells.removeAt(row); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 129 | } |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 130 | |
| 131 | endRemoveRows(); |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | QHash<int, QByteArray> CellModel::roleNames() const |
| 137 | { |
| 138 | return { |
| 139 | {CodeRole, "code"}, |
| 140 | {ResultRole, "result"}, |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 141 | {UuidRole, "uuid"}, |
| 142 | {StatusRole, "status"}, |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 143 | {ResultTypeRole, "resultType"}, |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 144 | }; |
| 145 | } |
| 146 | |
| 147 | void CellModel::addCell(const Cell &cell) |
| 148 | { |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 149 | int i = _notebook->_cells.size(); |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 150 | |
| 151 | insertRows(i, 1, QModelIndex()); |
| 152 | |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 153 | *_notebook->_cells[i] = cell; |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 154 | emit dataChanged(index(i), index(i), {CodeRole, ResultRole}); |
| 155 | } |
| 156 | |
| 157 | void CellModel::addCell(QString code, QString result) |
| 158 | { |
| 159 | addCell(Cell(code, result)); |
| 160 | } |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 161 | |
swissChili | 5d3e556 | 2022-02-24 16:49:19 -0800 | [diff] [blame] | 162 | void CellModel::insertCellBefore(int index) |
| 163 | { |
| 164 | insertRow(index); |
| 165 | } |
| 166 | |
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 167 | void CellModel::deleteCellAt(int index) |
| 168 | { |
| 169 | removeRow(index); |
| 170 | } |
| 171 | |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 172 | void 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 | } |