-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotebook_app.cpp
More file actions
109 lines (85 loc) · 2.82 KB
/
notebook_app.cpp
File metadata and controls
109 lines (85 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "notebook_app.hpp"
#include <QLayout>
#include "interrupt_flag.hpp"
void NotebookApp::initButtons() {
kernelControlButtons = new QHBoxLayout;
// Create the interpreter kernel control buttons
startBtn = new QPushButton("Start Kernel", this);
stopBtn = new QPushButton("Stop Kernel", this);
resetBtn = new QPushButton("Reset Kernel", this);
interruptBtn = new QPushButton("Interrupt", this);
// place them in the layout
kernelControlButtons->addWidget(startBtn);
kernelControlButtons->addWidget(stopBtn);
kernelControlButtons->addWidget(resetBtn);
kernelControlButtons->addWidget(interruptBtn);
// set the button names
startBtn->setObjectName("start");
stopBtn->setObjectName("stop");
resetBtn->setObjectName("reset");
interruptBtn->setObjectName("interrupt");
// connect the button to control functions of the interpreter
connect(startBtn, SIGNAL(clicked()), this, SLOT(startKernel()));
connect(stopBtn, SIGNAL(clicked()), this, SLOT(stopKernel()));
connect(resetBtn, SIGNAL(clicked()), this, SLOT(resetKernel()));
connect(interruptBtn, SIGNAL(clicked()), this, SLOT(interruptKernel()));
}
NotebookApp::NotebookApp(QWidget* parent): QWidget(parent), interp(&iq, &oq) {
input = new InputWidget(this);
input->setObjectName("input");
output = new OutputWidget(this);
output->setObjectName("output");
initButtons();
// Setup the layout
auto layout = new QVBoxLayout;
layout->addLayout(kernelControlButtons);
layout->addWidget(input);
layout->addWidget(output);
setLayout(layout);
setWindowTitle(tr("Plotscript Notebook"));
QTimer *outputTimer = new QTimer(this);
connect(outputTimer, SIGNAL(timeout()), this, SLOT(checkOutput()));
outputTimer->start();
// Whenever the user sends an input, we need to process that input
connect(input, &InputWidget::publish,
this, &NotebookApp::process);
}
void NotebookApp::checkOutput() {
OutputMessage msg;
if (oq.try_pop(msg)) {
// Output the message
if (msg.type == ErrorType) {
output->error(QString::fromStdString(msg.err));
} else if (msg.type == ExpressionType) {
output->processExpression(msg.exp);
}
// re-scale the output after the user's input gets processed
output->scale();
input->setDisabled(false);
// signal that the output has been processed (useful for tests)
emit outputProcessed();
}
}
void NotebookApp::process(const QString& str) {
interrupt_flag.store(false);
input->setDisabled(true);
// Clear the output widget then process the user's input
output->clear();
if (interp.isActive()) {
iq.push(str.toStdString());
} else {
oq.push(OutputMessage(ErrorType, "interpreter kernel not running"));
}
}
void NotebookApp::startKernel() {
interp.start();
}
void NotebookApp::stopKernel() {
interp.stop();
}
void NotebookApp::resetKernel() {
interp.reset();
}
void NotebookApp::interruptKernel() {
interrupt_flag.store(true);
}