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 | a44bf72 | 2022-04-16 18:41:54 -0700 | [diff] [blame^] | 59 | _runningAll = false; |
| 60 | } |
| 61 | |
| 62 | void Notebook::runAll() |
| 63 | { |
| 64 | if (_cells.size() > 0) |
| 65 | { |
| 66 | _rt->queueCell(_cells.first()); |
| 67 | _runningAll = true; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void Notebook::reset() |
| 72 | { |
| 73 | _rt->reset(); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 74 | } |
| 75 | |
swissChili | d2af6ad | 2022-04-16 14:42:17 -0700 | [diff] [blame] | 76 | void Notebook::fromJson(QJsonDocument doc) |
| 77 | { |
| 78 | QJsonObject nb = doc.object(); |
| 79 | QJsonArray cellArray = nb["cells"].toArray(); |
| 80 | |
| 81 | for (const QJsonValueRef &cell : cellArray) |
| 82 | { |
| 83 | cellModel()->insertCellBefore(cellModel()->rowCount()); |
| 84 | _cells.last()->fromJson(cell.toObject()); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void Notebook::open(QString path) |
| 89 | { |
| 90 | QFile file(path); |
| 91 | |
| 92 | if (file.exists()) |
| 93 | { |
| 94 | file.open(QFile::ReadOnly); |
| 95 | |
| 96 | QJsonParseError error; |
| 97 | QJsonDocument doc = QJsonDocument::fromJson(file.readAll(), &error); |
| 98 | |
| 99 | if (error.error == QJsonParseError::NoError) |
| 100 | { |
| 101 | fromJson(doc); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | qWarning() << error.errorString(); |
| 106 | } |
| 107 | |
| 108 | file.close(); |
| 109 | setSavePath(path); |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | qWarning() << "File does not exist" << path; |
| 114 | } |
| 115 | } |
| 116 | |
swissChili | 505de41 | 2022-03-24 12:35:08 -0700 | [diff] [blame] | 117 | QJsonDocument Notebook::toJson() const |
| 118 | { |
| 119 | QJsonObject nb; |
| 120 | QJsonArray cellArray; |
| 121 | |
| 122 | for (const Cell *cell : _cells) |
| 123 | { |
| 124 | cellArray.append(cell->toJson()); |
| 125 | } |
| 126 | |
| 127 | nb["cells"] = cellArray; |
| 128 | |
| 129 | return QJsonDocument(nb); |
| 130 | } |
| 131 | |
| 132 | void Notebook::save() |
| 133 | { |
| 134 | if (_savePath == "") |
| 135 | { |
swissChili | a44bf72 | 2022-04-16 18:41:54 -0700 | [diff] [blame^] | 136 | setSavePath(QFileDialog::getSaveFileName(nullptr, "Open REFAL Notebook", "", "REFAL Notebook (*.refnb)")); |
swissChili | 505de41 | 2022-03-24 12:35:08 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | QJsonDocument doc = toJson(); |
| 140 | QFile save(_savePath); |
| 141 | save.open(QFile::WriteOnly); |
| 142 | |
| 143 | if (!save.isOpen()) |
| 144 | { |
| 145 | emit saveError(save.errorString()); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | save.write(doc.toJson(QJsonDocument::Indented)); |
| 150 | save.close(); |
swissChili | a44bf72 | 2022-04-16 18:41:54 -0700 | [diff] [blame^] | 151 | |
| 152 | emit saved(); |
swissChili | 505de41 | 2022-03-24 12:35:08 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | bool Notebook::savePathSet() |
| 156 | { |
| 157 | return _savePath != ""; |
| 158 | } |
| 159 | |
| 160 | QString Notebook::savePath() |
| 161 | { |
| 162 | return _savePath; |
| 163 | } |
| 164 | |
| 165 | void Notebook::setSavePath(QString savePath) |
| 166 | { |
| 167 | _savePath = savePath; |
| 168 | emit savePathChanged(savePath); |
| 169 | } |
| 170 | |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 171 | void Notebook::cellFinishedRunning(Cell *cell, RuntimeResult result) |
| 172 | { |
| 173 | qInfo() << "cellFinishedRunning" << cell->uuid() << pprint(result); |
| 174 | cell->setResult(pprint(result)); |
| 175 | cell->setStatus(Cell::IDLE); |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 176 | cell->setResultType(Cell::EXPRESSION); |
swissChili | a44bf72 | 2022-04-16 18:41:54 -0700 | [diff] [blame^] | 177 | |
| 178 | if (_runningAll) |
| 179 | { |
| 180 | int index = _cells.indexOf(cell); |
| 181 | |
| 182 | // not last |
| 183 | if (index < _cells.size() - 1) |
| 184 | { |
| 185 | _rt->queueCell(_cells[index + 1]); |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | _runningAll = false; |
| 190 | } |
| 191 | } |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 192 | } |
| 193 | |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 194 | void Notebook::cellFailedToParse(Cell *cell, ParseResult result, Parser parser) |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 195 | { |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 196 | qInfo() << "cellFailedToParse" << cell->uuid() << pprint(result, parser); |
| 197 | cell->setResult(pprint(result, parser, PPrint::HTML)); |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 198 | cell->setStatus(Cell::IDLE); |
swissChili | ece1ac8 | 2022-02-25 11:20:42 -0800 | [diff] [blame] | 199 | cell->setResultType(Cell::DIAGNOSTIC); |
swissChili | a44bf72 | 2022-04-16 18:41:54 -0700 | [diff] [blame^] | 200 | |
| 201 | _runningAll = false; |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void Notebook::cellWaiting(Cell *cell) |
| 205 | { |
| 206 | qInfo() << "cellWaiting" << cell->uuid(); |
| 207 | cell->setStatus(Cell::WAITING); |
| 208 | } |
| 209 | |
| 210 | void Notebook::cellRunning(Cell *cell) |
| 211 | { |
| 212 | qInfo() << "cellRunning" << cell->uuid(); |
| 213 | cell->setStatus(Cell::RUNNING); |
| 214 | } |
| 215 | |
| 216 | void Notebook::cellQuit(Cell *cell) |
| 217 | { |
| 218 | qInfo() << "cellQuit" << cell->uuid(); |
| 219 | cell->setResult(""); |
| 220 | cell->setStatus(Cell::IDLE); |
swissChili | a44bf72 | 2022-04-16 18:41:54 -0700 | [diff] [blame^] | 221 | |
| 222 | _runningAll = false; |
swissChili | d85daa9 | 2022-02-24 15:29:02 -0800 | [diff] [blame] | 223 | } |