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 | 505de41 | 2022-03-24 12:35:08 -0700 | [diff] [blame^] | 5 | #include <QJsonObject> |
| 6 | #include <QJsonArray> |
| 7 | #include <QFileDialog> |
| 8 | |
swissChili | 5d3e556 | 2022-02-24 16:49:19 -0800 | [diff] [blame] | 9 | // TODO: avoid potential race condition if Cell is deleted, pass by value with same UUID instead |
| 10 | |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 11 | Notebook::~Notebook() |
| 12 | { |
| 13 | _rtThread->quit(); |
| 14 | _rtThread->wait(); |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 15 | |
| 16 | delete _rt; |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 17 | } |
| 18 | |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 19 | Notebook::Notebook(QObject *parent) |
| 20 | : QObject(parent) |
| 21 | , _cellModel(new CellModel(this)) |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 22 | , _rtThread(new QThread(this)) |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 23 | , _rt(new NbRuntime) |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 24 | { |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 25 | connect(_rt, &NbRuntime::cellFailedToParse, this, &Notebook::cellFailedToParse); |
| 26 | connect(_rt, &NbRuntime::cellFinishedRunning, this, &Notebook::cellFinishedRunning); |
| 27 | connect(_rt, &NbRuntime::cellQuit, this, &Notebook::cellQuit); |
| 28 | connect(_rt, &NbRuntime::cellRunning, this, &Notebook::cellRunning); |
| 29 | connect(_rt, &NbRuntime::cellWaiting, this, &Notebook::cellWaiting); |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 30 | |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 31 | _rt->moveToThread(_rtThread); |
| 32 | _rtThread->start(); |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | Notebook::Notebook(const Notebook &other, QObject *parent) |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 36 | : Notebook(parent) |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 37 | { |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 38 | for (const Cell *cell : other._cells) |
| 39 | { |
| 40 | _cells.append(new Cell(*cell, this)); |
| 41 | } |
swissChili | 25620b0 | 2022-02-23 17:15:16 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | CellModel *Notebook::cellModel() |
| 45 | { |
| 46 | return _cellModel; |
| 47 | } |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 48 | |
| 49 | void Notebook::runCell(QUuid uuid) |
| 50 | { |
| 51 | qInfo() << "Running cell" << uuid; |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 52 | _rt->queueCell(Cell::cellFromUuid(uuid)); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void Notebook::quitCell(QUuid uuid) |
| 56 | { |
swissChili | 732628e | 2022-02-25 10:35:56 -0800 | [diff] [blame] | 57 | _rt->unqueueCell(Cell::cellFromUuid(uuid)); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 58 | } |
| 59 | |
swissChili | 505de41 | 2022-03-24 12:35:08 -0700 | [diff] [blame^] | 60 | QJsonDocument Notebook::toJson() const |
| 61 | { |
| 62 | QJsonObject nb; |
| 63 | QJsonArray cellArray; |
| 64 | |
| 65 | for (const Cell *cell : _cells) |
| 66 | { |
| 67 | cellArray.append(cell->toJson()); |
| 68 | } |
| 69 | |
| 70 | nb["cells"] = cellArray; |
| 71 | |
| 72 | return QJsonDocument(nb); |
| 73 | } |
| 74 | |
| 75 | void Notebook::save() |
| 76 | { |
| 77 | if (_savePath == "") |
| 78 | { |
| 79 | setSavePath(QFileDialog::getSaveFileName(nullptr, "Open Refal Notebook", "", "Refal Notebooks (*.refnb)")); |
| 80 | } |
| 81 | |
| 82 | QJsonDocument doc = toJson(); |
| 83 | QFile save(_savePath); |
| 84 | save.open(QFile::WriteOnly); |
| 85 | |
| 86 | if (!save.isOpen()) |
| 87 | { |
| 88 | emit saveError(save.errorString()); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | save.write(doc.toJson(QJsonDocument::Indented)); |
| 93 | save.close(); |
| 94 | } |
| 95 | |
| 96 | bool Notebook::savePathSet() |
| 97 | { |
| 98 | return _savePath != ""; |
| 99 | } |
| 100 | |
| 101 | QString Notebook::savePath() |
| 102 | { |
| 103 | return _savePath; |
| 104 | } |
| 105 | |
| 106 | void Notebook::setSavePath(QString savePath) |
| 107 | { |
| 108 | _savePath = savePath; |
| 109 | emit savePathChanged(savePath); |
| 110 | } |
| 111 | |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 112 | void Notebook::cellFinishedRunning(Cell *cell, RuntimeResult result) |
| 113 | { |
| 114 | qInfo() << "cellFinishedRunning" << cell->uuid() << pprint(result); |
| 115 | cell->setResult(pprint(result)); |
| 116 | cell->setStatus(Cell::IDLE); |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 117 | cell->setResultType(Cell::EXPRESSION); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 118 | } |
| 119 | |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 120 | void Notebook::cellFailedToParse(Cell *cell, ParseResult result, Parser parser) |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 121 | { |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 122 | qInfo() << "cellFailedToParse" << cell->uuid() << pprint(result, parser); |
| 123 | cell->setResult(pprint(result, parser, PPrint::HTML)); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 124 | cell->setStatus(Cell::IDLE); |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 125 | cell->setResultType(Cell::DIAGNOSTIC); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void Notebook::cellWaiting(Cell *cell) |
| 129 | { |
| 130 | qInfo() << "cellWaiting" << cell->uuid(); |
| 131 | cell->setStatus(Cell::WAITING); |
| 132 | } |
| 133 | |
| 134 | void Notebook::cellRunning(Cell *cell) |
| 135 | { |
| 136 | qInfo() << "cellRunning" << cell->uuid(); |
| 137 | cell->setStatus(Cell::RUNNING); |
| 138 | } |
| 139 | |
| 140 | void Notebook::cellQuit(Cell *cell) |
| 141 | { |
| 142 | qInfo() << "cellQuit" << cell->uuid(); |
| 143 | cell->setResult(""); |
| 144 | cell->setStatus(Cell::IDLE); |
| 145 | } |