blob: 8d0e8ee35c6b871c0390654f18739d3766e5565d [file] [log] [blame]
swissChili25620b02022-02-23 17:15:16 -08001#include "Notebook.h"
2#include "CellModel.h"
swissChilid85daa92022-02-24 15:29:02 -08003#include "../PPrint.h"
swissChili25620b02022-02-23 17:15:16 -08004
swissChili505de412022-03-24 12:35:08 -07005#include <QJsonObject>
6#include <QJsonArray>
7#include <QFileDialog>
8
swissChili5d3e5562022-02-24 16:49:19 -08009// TODO: avoid potential race condition if Cell is deleted, pass by value with same UUID instead
10
swissChili732628e2022-02-25 10:35:56 -080011Notebook::~Notebook()
12{
13 _rtThread->quit();
14 _rtThread->wait();
swissChiliece1ac82022-02-25 11:20:42 -080015
16 delete _rt;
swissChili732628e2022-02-25 10:35:56 -080017}
18
swissChili25620b02022-02-23 17:15:16 -080019Notebook::Notebook(QObject *parent)
20 : QObject(parent)
21 , _cellModel(new CellModel(this))
swissChili732628e2022-02-25 10:35:56 -080022 , _rtThread(new QThread(this))
swissChiliece1ac82022-02-25 11:20:42 -080023 , _rt(new NbRuntime)
swissChili25620b02022-02-23 17:15:16 -080024{
swissChili732628e2022-02-25 10:35:56 -080025 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);
swissChili25620b02022-02-23 17:15:16 -080030
swissChili732628e2022-02-25 10:35:56 -080031 _rt->moveToThread(_rtThread);
32 _rtThread->start();
swissChili25620b02022-02-23 17:15:16 -080033}
34
35Notebook::Notebook(const Notebook &other, QObject *parent)
swissChilid85daa92022-02-24 15:29:02 -080036 : Notebook(parent)
swissChili25620b02022-02-23 17:15:16 -080037{
swissChilid85daa92022-02-24 15:29:02 -080038 for (const Cell *cell : other._cells)
39 {
40 _cells.append(new Cell(*cell, this));
41 }
swissChili25620b02022-02-23 17:15:16 -080042}
43
44CellModel *Notebook::cellModel()
45{
46 return _cellModel;
47}
swissChilid85daa92022-02-24 15:29:02 -080048
49void Notebook::runCell(QUuid uuid)
50{
51 qInfo() << "Running cell" << uuid;
swissChili732628e2022-02-25 10:35:56 -080052 _rt->queueCell(Cell::cellFromUuid(uuid));
swissChilid85daa92022-02-24 15:29:02 -080053}
54
55void Notebook::quitCell(QUuid uuid)
56{
swissChili732628e2022-02-25 10:35:56 -080057 _rt->unqueueCell(Cell::cellFromUuid(uuid));
swissChilid85daa92022-02-24 15:29:02 -080058}
59
swissChili505de412022-03-24 12:35:08 -070060QJsonDocument 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
75void 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
96bool Notebook::savePathSet()
97{
98 return _savePath != "";
99}
100
101QString Notebook::savePath()
102{
103 return _savePath;
104}
105
106void Notebook::setSavePath(QString savePath)
107{
108 _savePath = savePath;
109 emit savePathChanged(savePath);
110}
111
swissChilid85daa92022-02-24 15:29:02 -0800112void Notebook::cellFinishedRunning(Cell *cell, RuntimeResult result)
113{
114 qInfo() << "cellFinishedRunning" << cell->uuid() << pprint(result);
115 cell->setResult(pprint(result));
116 cell->setStatus(Cell::IDLE);
swissChiliece1ac82022-02-25 11:20:42 -0800117 cell->setResultType(Cell::EXPRESSION);
swissChilid85daa92022-02-24 15:29:02 -0800118}
119
swissChiliece1ac82022-02-25 11:20:42 -0800120void Notebook::cellFailedToParse(Cell *cell, ParseResult result, Parser parser)
swissChilid85daa92022-02-24 15:29:02 -0800121{
swissChiliece1ac82022-02-25 11:20:42 -0800122 qInfo() << "cellFailedToParse" << cell->uuid() << pprint(result, parser);
123 cell->setResult(pprint(result, parser, PPrint::HTML));
swissChilid85daa92022-02-24 15:29:02 -0800124 cell->setStatus(Cell::IDLE);
swissChiliece1ac82022-02-25 11:20:42 -0800125 cell->setResultType(Cell::DIAGNOSTIC);
swissChilid85daa92022-02-24 15:29:02 -0800126}
127
128void Notebook::cellWaiting(Cell *cell)
129{
130 qInfo() << "cellWaiting" << cell->uuid();
131 cell->setStatus(Cell::WAITING);
132}
133
134void Notebook::cellRunning(Cell *cell)
135{
136 qInfo() << "cellRunning" << cell->uuid();
137 cell->setStatus(Cell::RUNNING);
138}
139
140void Notebook::cellQuit(Cell *cell)
141{
142 qInfo() << "cellQuit" << cell->uuid();
143 cell->setResult("");
144 cell->setStatus(Cell::IDLE);
145}