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(); |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 11 | |
| 12 | delete _rt; |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 13 | } |
| 14 | |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 15 | Notebook::Notebook(QObject *parent) |
| 16 | : QObject(parent) |
| 17 | , _cellModel(new CellModel(this)) |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 18 | , _rtThread(new QThread(this)) |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 19 | , _rt(new NbRuntime) |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 20 | { |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 21 | connect(_rt, &NbRuntime::cellFailedToParse, this, &Notebook::cellFailedToParse); |
| 22 | connect(_rt, &NbRuntime::cellFinishedRunning, this, &Notebook::cellFinishedRunning); |
| 23 | connect(_rt, &NbRuntime::cellQuit, this, &Notebook::cellQuit); |
| 24 | connect(_rt, &NbRuntime::cellRunning, this, &Notebook::cellRunning); |
| 25 | connect(_rt, &NbRuntime::cellWaiting, this, &Notebook::cellWaiting); |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 26 | |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 27 | _rt->moveToThread(_rtThread); |
| 28 | _rtThread->start(); |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | Notebook::Notebook(const Notebook &other, QObject *parent) |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 32 | : Notebook(parent) |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 33 | { |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 34 | for (const Cell *cell : other._cells) |
| 35 | { |
| 36 | _cells.append(new Cell(*cell, this)); |
| 37 | } |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | CellModel *Notebook::cellModel() |
| 41 | { |
| 42 | return _cellModel; |
| 43 | } |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 44 | |
| 45 | void Notebook::runCell(QUuid uuid) |
| 46 | { |
| 47 | qInfo() << "Running cell" << uuid; |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 48 | _rt->queueCell(Cell::cellFromUuid(uuid)); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void Notebook::quitCell(QUuid uuid) |
| 52 | { |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 53 | _rt->unqueueCell(Cell::cellFromUuid(uuid)); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void Notebook::cellFinishedRunning(Cell *cell, RuntimeResult result) |
| 57 | { |
| 58 | qInfo() << "cellFinishedRunning" << cell->uuid() << pprint(result); |
| 59 | cell->setResult(pprint(result)); |
| 60 | cell->setStatus(Cell::IDLE); |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 61 | cell->setResultType(Cell::EXPRESSION); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 62 | } |
| 63 | |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 64 | void Notebook::cellFailedToParse(Cell *cell, ParseResult result, Parser parser) |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 65 | { |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 66 | qInfo() << "cellFailedToParse" << cell->uuid() << pprint(result, parser); |
| 67 | cell->setResult(pprint(result, parser, PPrint::HTML)); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 68 | cell->setStatus(Cell::IDLE); |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 69 | cell->setResultType(Cell::DIAGNOSTIC); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void Notebook::cellWaiting(Cell *cell) |
| 73 | { |
| 74 | qInfo() << "cellWaiting" << cell->uuid(); |
| 75 | cell->setStatus(Cell::WAITING); |
| 76 | } |
| 77 | |
| 78 | void Notebook::cellRunning(Cell *cell) |
| 79 | { |
| 80 | qInfo() << "cellRunning" << cell->uuid(); |
| 81 | cell->setStatus(Cell::RUNNING); |
| 82 | } |
| 83 | |
| 84 | void Notebook::cellQuit(Cell *cell) |
| 85 | { |
| 86 | qInfo() << "cellQuit" << cell->uuid(); |
| 87 | cell->setResult(""); |
| 88 | cell->setStatus(Cell::IDLE); |
| 89 | } |