blob: 3396779b5225f0a734a56f85968686318f8c8c51 [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();
11}
12
swissChili25620b02022-02-23 17:15:16 -080013Notebook::Notebook(QObject *parent)
14 : QObject(parent)
15 , _cellModel(new CellModel(this))
swissChili732628e2022-02-25 10:35:56 -080016 , _rt(new NbRuntime(this))
17 , _rtThread(new QThread(this))
swissChili25620b02022-02-23 17:15:16 -080018{
swissChili732628e2022-02-25 10:35:56 -080019 connect(_rt, &NbRuntime::cellFailedToParse, this, &Notebook::cellFailedToParse);
20 connect(_rt, &NbRuntime::cellFinishedRunning, this, &Notebook::cellFinishedRunning);
21 connect(_rt, &NbRuntime::cellQuit, this, &Notebook::cellQuit);
22 connect(_rt, &NbRuntime::cellRunning, this, &Notebook::cellRunning);
23 connect(_rt, &NbRuntime::cellWaiting, this, &Notebook::cellWaiting);
swissChili25620b02022-02-23 17:15:16 -080024
swissChili732628e2022-02-25 10:35:56 -080025 _rt->moveToThread(_rtThread);
26 _rtThread->start();
swissChili25620b02022-02-23 17:15:16 -080027}
28
29Notebook::Notebook(const Notebook &other, QObject *parent)
swissChilid85daa92022-02-24 15:29:02 -080030 : Notebook(parent)
swissChili25620b02022-02-23 17:15:16 -080031{
swissChilid85daa92022-02-24 15:29:02 -080032 for (const Cell *cell : other._cells)
33 {
34 _cells.append(new Cell(*cell, this));
35 }
swissChili25620b02022-02-23 17:15:16 -080036}
37
38CellModel *Notebook::cellModel()
39{
40 return _cellModel;
41}
swissChilid85daa92022-02-24 15:29:02 -080042
43void Notebook::runCell(QUuid uuid)
44{
45 qInfo() << "Running cell" << uuid;
swissChili732628e2022-02-25 10:35:56 -080046 _rt->queueCell(Cell::cellFromUuid(uuid));
swissChilid85daa92022-02-24 15:29:02 -080047}
48
49void Notebook::quitCell(QUuid uuid)
50{
swissChili732628e2022-02-25 10:35:56 -080051 _rt->unqueueCell(Cell::cellFromUuid(uuid));
swissChilid85daa92022-02-24 15:29:02 -080052}
53
54void Notebook::cellFinishedRunning(Cell *cell, RuntimeResult result)
55{
56 qInfo() << "cellFinishedRunning" << cell->uuid() << pprint(result);
57 cell->setResult(pprint(result));
58 cell->setStatus(Cell::IDLE);
59}
60
61void Notebook::cellFailedToParse(Cell *cell, ParseResult result)
62{
63 qInfo() << "cellFailedToParse" << cell->uuid() << pprint(result);
64 cell->setResult(pprint(result));
65 cell->setStatus(Cell::IDLE);
66}
67
68void Notebook::cellWaiting(Cell *cell)
69{
70 qInfo() << "cellWaiting" << cell->uuid();
71 cell->setStatus(Cell::WAITING);
72}
73
74void Notebook::cellRunning(Cell *cell)
75{
76 qInfo() << "cellRunning" << cell->uuid();
77 cell->setStatus(Cell::RUNNING);
78}
79
80void Notebook::cellQuit(Cell *cell)
81{
82 qInfo() << "cellQuit" << cell->uuid();
83 cell->setResult("");
84 cell->setStatus(Cell::IDLE);
85}