blob: 97df18162039dd235b04d6189c6f17a18220a9cf [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
swissChili5d3e5562022-02-24 16:49:19 -08005// TODO: avoid potential race condition if Cell is deleted, pass by value with same UUID instead
6
swissChili732628e2022-02-25 10:35:56 -08007Notebook::~Notebook()
8{
9 _rtThread->quit();
10 _rtThread->wait();
swissChiliece1ac82022-02-25 11:20:42 -080011
12 delete _rt;
swissChili732628e2022-02-25 10:35:56 -080013}
14
swissChili25620b02022-02-23 17:15:16 -080015Notebook::Notebook(QObject *parent)
16 : QObject(parent)
17 , _cellModel(new CellModel(this))
swissChili732628e2022-02-25 10:35:56 -080018 , _rtThread(new QThread(this))
swissChiliece1ac82022-02-25 11:20:42 -080019 , _rt(new NbRuntime)
swissChili25620b02022-02-23 17:15:16 -080020{
swissChili732628e2022-02-25 10:35:56 -080021 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);
swissChili25620b02022-02-23 17:15:16 -080026
swissChili732628e2022-02-25 10:35:56 -080027 _rt->moveToThread(_rtThread);
28 _rtThread->start();
swissChili25620b02022-02-23 17:15:16 -080029}
30
31Notebook::Notebook(const Notebook &other, QObject *parent)
swissChilid85daa92022-02-24 15:29:02 -080032 : Notebook(parent)
swissChili25620b02022-02-23 17:15:16 -080033{
swissChilid85daa92022-02-24 15:29:02 -080034 for (const Cell *cell : other._cells)
35 {
36 _cells.append(new Cell(*cell, this));
37 }
swissChili25620b02022-02-23 17:15:16 -080038}
39
40CellModel *Notebook::cellModel()
41{
42 return _cellModel;
43}
swissChilid85daa92022-02-24 15:29:02 -080044
45void Notebook::runCell(QUuid uuid)
46{
47 qInfo() << "Running cell" << uuid;
swissChili732628e2022-02-25 10:35:56 -080048 _rt->queueCell(Cell::cellFromUuid(uuid));
swissChilid85daa92022-02-24 15:29:02 -080049}
50
51void Notebook::quitCell(QUuid uuid)
52{
swissChili732628e2022-02-25 10:35:56 -080053 _rt->unqueueCell(Cell::cellFromUuid(uuid));
swissChilid85daa92022-02-24 15:29:02 -080054}
55
56void Notebook::cellFinishedRunning(Cell *cell, RuntimeResult result)
57{
58 qInfo() << "cellFinishedRunning" << cell->uuid() << pprint(result);
59 cell->setResult(pprint(result));
60 cell->setStatus(Cell::IDLE);
swissChiliece1ac82022-02-25 11:20:42 -080061 cell->setResultType(Cell::EXPRESSION);
swissChilid85daa92022-02-24 15:29:02 -080062}
63
swissChiliece1ac82022-02-25 11:20:42 -080064void Notebook::cellFailedToParse(Cell *cell, ParseResult result, Parser parser)
swissChilid85daa92022-02-24 15:29:02 -080065{
swissChiliece1ac82022-02-25 11:20:42 -080066 qInfo() << "cellFailedToParse" << cell->uuid() << pprint(result, parser);
67 cell->setResult(pprint(result, parser, PPrint::HTML));
swissChilid85daa92022-02-24 15:29:02 -080068 cell->setStatus(Cell::IDLE);
swissChiliece1ac82022-02-25 11:20:42 -080069 cell->setResultType(Cell::DIAGNOSTIC);
swissChilid85daa92022-02-24 15:29:02 -080070}
71
72void Notebook::cellWaiting(Cell *cell)
73{
74 qInfo() << "cellWaiting" << cell->uuid();
75 cell->setStatus(Cell::WAITING);
76}
77
78void Notebook::cellRunning(Cell *cell)
79{
80 qInfo() << "cellRunning" << cell->uuid();
81 cell->setStatus(Cell::RUNNING);
82}
83
84void Notebook::cellQuit(Cell *cell)
85{
86 qInfo() << "cellQuit" << cell->uuid();
87 cell->setResult("");
88 cell->setStatus(Cell::IDLE);
89}