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