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 | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 40 | default: |
| 41 | return QVariant(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | bool 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: |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 52 | _notebook->_cells[index.row()]->setCode(value.toString()); |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 53 | break; |
| 54 | case ResultRole: |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 55 | _notebook->_cells[index.row()]->setResult(value.toString()); |
| 56 | break; |
| 57 | case StatusRole: |
| 58 | _notebook->_cells[index.row()]->setStatus(value.toInt()); |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 59 | 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 | |
| 72 | Qt::ItemFlags CellModel::flags(const QModelIndex &index) const |
| 73 | { |
| 74 | if (!index.isValid()) |
| 75 | return Qt::NoItemFlags; |
| 76 | |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 77 | return Qt::ItemIsEditable; |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | bool 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++) |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 85 | { |
| 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 | } |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 105 | |
| 106 | endInsertRows(); |
| 107 | |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | bool 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++) |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 116 | { |
| 117 | delete _notebook->_cells[row]; |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 118 | _notebook->_cells.removeAt(row); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 119 | } |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 120 | |
| 121 | endRemoveRows(); |
| 122 | |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | QHash<int, QByteArray> CellModel::roleNames() const |
| 127 | { |
| 128 | return { |
| 129 | {CodeRole, "code"}, |
| 130 | {ResultRole, "result"}, |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 131 | {UuidRole, "uuid"}, |
| 132 | {StatusRole, "status"}, |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 133 | }; |
| 134 | } |
| 135 | |
| 136 | void CellModel::addCell(const Cell &cell) |
| 137 | { |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 138 | int i = _notebook->_cells.size(); |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 139 | |
| 140 | insertRows(i, 1, QModelIndex()); |
| 141 | |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 142 | *_notebook->_cells[i] = cell; |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 143 | emit dataChanged(index(i), index(i), {CodeRole, ResultRole}); |
| 144 | } |
| 145 | |
| 146 | void CellModel::addCell(QString code, QString result) |
| 147 | { |
| 148 | addCell(Cell(code, result)); |
| 149 | } |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 150 | |
swissChili | 5d3e556 | 2022-02-24 16:49:19 -0800 | [diff] [blame] | 151 | void CellModel::insertCellBefore(int index) |
| 152 | { |
| 153 | insertRow(index); |
| 154 | } |
| 155 | |
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 156 | void CellModel::deleteCellAt(int index) |
| 157 | { |
| 158 | removeRow(index); |
| 159 | } |
| 160 | |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 161 | void 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 | } |