swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 1 | #include "CellModel.h" |
| 2 | |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 3 | CellModel::CellModel(Notebook *parent) |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 4 | : QAbstractListModel(parent) |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 5 | , _notebook(parent) |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 6 | { |
| 7 | } |
| 8 | |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 9 | CellModel::CellModel(const CellModel &other) |
| 10 | : CellModel(other._notebook) |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 11 | { |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 12 | } |
| 13 | |
| 14 | int 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 | |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 21 | return _notebook->_cells.size(); |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | QVariant CellModel::data(const QModelIndex &index, int role) const |
| 25 | { |
| 26 | if (!index.isValid()) |
| 27 | return QVariant(); |
| 28 | |
| 29 | switch (role) |
| 30 | { |
| 31 | case CodeRole: |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 32 | return _notebook->_cells[index.row()]->code(); |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 33 | case ResultRole: |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 34 | 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(); |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 39 | default: |
| 40 | return QVariant(); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | bool 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: |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 51 | _notebook->_cells[index.row()]->setCode(value.toString()); |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 52 | break; |
| 53 | case ResultRole: |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 54 | _notebook->_cells[index.row()]->setResult(value.toString()); |
| 55 | break; |
| 56 | case StatusRole: |
| 57 | _notebook->_cells[index.row()]->setStatus(value.toInt()); |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 58 | 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 | |
| 71 | Qt::ItemFlags CellModel::flags(const QModelIndex &index) const |
| 72 | { |
| 73 | if (!index.isValid()) |
| 74 | return Qt::NoItemFlags; |
| 75 | |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 76 | return Qt::ItemIsEditable; |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | bool 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++) |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 84 | { |
| 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 | } |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 104 | |
| 105 | endInsertRows(); |
| 106 | |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | bool 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++) |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 115 | { |
| 116 | delete _notebook->_cells[row]; |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 117 | _notebook->_cells.removeAt(row); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 118 | } |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 119 | |
| 120 | endRemoveRows(); |
| 121 | |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | QHash<int, QByteArray> CellModel::roleNames() const |
| 126 | { |
| 127 | return { |
| 128 | {CodeRole, "code"}, |
| 129 | {ResultRole, "result"}, |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 130 | {UuidRole, "uuid"}, |
| 131 | {StatusRole, "status"}, |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 132 | }; |
| 133 | } |
| 134 | |
| 135 | void CellModel::addCell(const Cell &cell) |
| 136 | { |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 137 | int i = _notebook->_cells.size(); |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 138 | |
| 139 | insertRows(i, 1, QModelIndex()); |
| 140 | |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 141 | *_notebook->_cells[i] = cell; |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 142 | emit dataChanged(index(i), index(i), {CodeRole, ResultRole}); |
| 143 | } |
| 144 | |
| 145 | void CellModel::addCell(QString code, QString result) |
| 146 | { |
| 147 | addCell(Cell(code, result)); |
| 148 | } |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 149 | |
swissChili | 5d3e556 | 2022-02-24 16:49:19 -0800 | [diff] [blame] | 150 | void CellModel::insertCellBefore(int index) |
| 151 | { |
| 152 | insertRow(index); |
| 153 | } |
| 154 | |
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame^] | 155 | void CellModel::deleteCellAt(int index) |
| 156 | { |
| 157 | removeRow(index); |
| 158 | } |
| 159 | |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 160 | void CellModel::announceCellChange(Cell *cell, int role) |
| 161 | { |
| 162 | // TODO: Optimize |
| 163 | |
| 164 | for (int i = 0; i < rowCount(); i++) |
| 165 | { |
| 166 | if (_notebook->_cells[i] == cell) |
| 167 | { |
| 168 | emit dataChanged(index(i), index(i), QVector<int>() << role); |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | } |