swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 1 | #include "Notebook.h" |
| 2 | #include "CellModel.h" |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 3 | #include "../PPrint.h" |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 4 | |
swissChili | 5d3e556 | 2022-02-24 16:49:19 -0800 | [diff] [blame] | 5 | // TODO: avoid potential race condition if Cell is deleted, pass by value with same UUID instead |
| 6 | |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame^] | 7 | Notebook::~Notebook() |
| 8 | { |
| 9 | _rtThread->quit(); |
| 10 | _rtThread->wait(); |
| 11 | } |
| 12 | |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 13 | Notebook::Notebook(QObject *parent) |
| 14 | : QObject(parent) |
| 15 | , _cellModel(new CellModel(this)) |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame^] | 16 | , _rt(new NbRuntime(this)) |
| 17 | , _rtThread(new QThread(this)) |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 18 | { |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame^] | 19 | connect(_rt, &NbRuntime::cellFailedToParse, this, &Notebook::cellFailedToParse); |
| 20 | connect(_rt, &NbRuntime::cellFinishedRunning, this, &Notebook::cellFinishedRunning); |
| 21 | connect(_rt, &NbRuntime::cellQuit, this, &Notebook::cellQuit); |
| 22 | connect(_rt, &NbRuntime::cellRunning, this, &Notebook::cellRunning); |
| 23 | connect(_rt, &NbRuntime::cellWaiting, this, &Notebook::cellWaiting); |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 24 | |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame^] | 25 | _rt->moveToThread(_rtThread); |
| 26 | _rtThread->start(); |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | Notebook::Notebook(const Notebook &other, QObject *parent) |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 30 | : Notebook(parent) |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 31 | { |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 32 | for (const Cell *cell : other._cells) |
| 33 | { |
| 34 | _cells.append(new Cell(*cell, this)); |
| 35 | } |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | CellModel *Notebook::cellModel() |
| 39 | { |
| 40 | return _cellModel; |
| 41 | } |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 42 | |
| 43 | void Notebook::runCell(QUuid uuid) |
| 44 | { |
| 45 | qInfo() << "Running cell" << uuid; |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame^] | 46 | _rt->queueCell(Cell::cellFromUuid(uuid)); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void Notebook::quitCell(QUuid uuid) |
| 50 | { |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame^] | 51 | _rt->unqueueCell(Cell::cellFromUuid(uuid)); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void Notebook::cellFinishedRunning(Cell *cell, RuntimeResult result) |
| 55 | { |
| 56 | qInfo() << "cellFinishedRunning" << cell->uuid() << pprint(result); |
| 57 | cell->setResult(pprint(result)); |
| 58 | cell->setStatus(Cell::IDLE); |
| 59 | } |
| 60 | |
| 61 | void Notebook::cellFailedToParse(Cell *cell, ParseResult result) |
| 62 | { |
| 63 | qInfo() << "cellFailedToParse" << cell->uuid() << pprint(result); |
| 64 | cell->setResult(pprint(result)); |
| 65 | cell->setStatus(Cell::IDLE); |
| 66 | } |
| 67 | |
| 68 | void Notebook::cellWaiting(Cell *cell) |
| 69 | { |
| 70 | qInfo() << "cellWaiting" << cell->uuid(); |
| 71 | cell->setStatus(Cell::WAITING); |
| 72 | } |
| 73 | |
| 74 | void Notebook::cellRunning(Cell *cell) |
| 75 | { |
| 76 | qInfo() << "cellRunning" << cell->uuid(); |
| 77 | cell->setStatus(Cell::RUNNING); |
| 78 | } |
| 79 | |
| 80 | void Notebook::cellQuit(Cell *cell) |
| 81 | { |
| 82 | qInfo() << "cellQuit" << cell->uuid(); |
| 83 | cell->setResult(""); |
| 84 | cell->setStatus(Cell::IDLE); |
| 85 | } |