swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <QAbstractListModel> |
| 4 | #include <qqml.h> |
| 5 | |
| 6 | #include "Cell.h" |
| 7 | |
swissChili | e386bc7 | 2022-02-24 21:31:31 -0800 | [diff] [blame] | 8 | class Notebook; |
| 9 | |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 10 | class CellModel : public QAbstractListModel |
| 11 | { |
| 12 | Q_OBJECT |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 13 | |
| 14 | public: |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 15 | explicit CellModel(Notebook *parent = nullptr); |
| 16 | CellModel(const CellModel &other); |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 17 | |
| 18 | enum CellRoles |
| 19 | { |
| 20 | CodeRole = Qt::UserRole + 1, |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 21 | ResultRole, |
| 22 | UuidRole, |
| 23 | StatusRole, |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | // Basic functionality: |
| 27 | int rowCount(const QModelIndex &parent = QModelIndex()) const override; |
| 28 | |
| 29 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
| 30 | |
| 31 | // Editable: |
| 32 | Q_INVOKABLE bool setData(const QModelIndex &index, const QVariant &value, |
| 33 | int role = Qt::EditRole) override; |
| 34 | |
| 35 | Qt::ItemFlags flags(const QModelIndex& index) const override; |
| 36 | |
| 37 | // Add data: |
| 38 | Q_INVOKABLE bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; |
| 39 | |
| 40 | // Remove data: |
| 41 | Q_INVOKABLE bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; |
| 42 | |
| 43 | QHash<int, QByteArray> roleNames() const override; |
| 44 | |
| 45 | Q_INVOKABLE void addCell(const Cell &cell); |
| 46 | Q_INVOKABLE void addCell(QString code, QString result); |
| 47 | |
swissChili | 5d3e556 | 2022-02-24 16:49:19 -0800 | [diff] [blame] | 48 | Q_INVOKABLE void insertCellBefore(int index); |
swissChili | 06cec4e | 2022-02-24 19:04:32 -0800 | [diff] [blame] | 49 | Q_INVOKABLE void deleteCellAt(int index); |
swissChili | 5d3e556 | 2022-02-24 16:49:19 -0800 | [diff] [blame] | 50 | |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 51 | private: |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 52 | Notebook *_notebook; |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 53 | void announceCellChange(Cell *cell, int role); |
swissChili | 4b3105a | 2022-02-22 16:34:39 -0800 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | Q_DECLARE_METATYPE(CellModel) |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 57 | Q_DECLARE_METATYPE(CellModel *) |