Skip to content

Commit

Permalink
Use engine-tracked cell IDs in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
jonpalmisc committed Dec 18, 2021
1 parent 5e4c802 commit aea14d3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
27 changes: 21 additions & 6 deletions ui/Cell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
Cell::Cell(MainWindow* mainWindow, unsigned id, QWidget* parent)
: QWidget(parent)
, m_mainWindow(mainWindow)
, m_id(id)
, m_inputSubcell(new QWidget)
, m_outputSubcell(new QWidget)
, m_inputLabel(new QLabel)
Expand All @@ -39,9 +38,8 @@ Cell::Cell(MainWindow* mainWindow, unsigned id, QWidget* parent)
m_inputLabel->setEnabled(false);
m_outputLabel->setEnabled(false);

// Format the input and output labels with the cell ID.
m_inputLabel->setText(QString(" In[%1] := ").arg(m_id));
m_outputLabel->setText(QString("Out[%1] := ").arg(m_id));
// Set the cell's ID and populate the input/output labels.
setId(id);

// Shorthand for creating subcell layouts.
auto makeSubcellLayout = [](QWidget* label, QWidget* field) {
Expand Down Expand Up @@ -81,10 +79,27 @@ Cell::Cell(MainWindow* mainWindow, unsigned id, QWidget* parent)
connect(m_inputField, &QLineEdit::returnPressed, this, &Cell::evaluateCurrentInput);
}

void Cell::setId(unsigned int id)
{
m_id = id;

// Zero is used as the "not yet assigned" ID.
if (m_id == 0)
m_inputLabel->setText("In[$] := ");
else
m_inputLabel->setText(QString("In[%1] := ").arg(m_id));

m_outputLabel->setText(QString("Out[%1] = ").arg(m_id));
}

void Cell::evaluateCurrentInput()
{
auto output = m_mainWindow->engineEval(m_inputField->text());
m_outputField->setText(output);
auto engine = m_mainWindow->engine();

auto output = engine->eval(m_inputField->text().toStdString());
m_outputField->setText(QString::fromStdString(output));

setId(engine->lastOutputId());

m_outputSubcell->setVisible(true);
m_mainWindow->cellEvaluated(m_id);
Expand Down
5 changes: 4 additions & 1 deletion ui/Cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ class Cell : public QWidget {
QLabel* m_outputField;

public:
Cell(MainWindow* mainWindow, unsigned id, QWidget* parent = nullptr);
Cell(MainWindow* mainWindow, unsigned id = 0, QWidget* parent = nullptr);

/// Get the cell's ID.
unsigned id() const { return m_id; }

/// Set the cell's ID and update the input/output labels.
void setId(unsigned id);

/// Evaluate the cell's current input
void evaluateCurrentInput();
};
8 changes: 4 additions & 4 deletions ui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ constexpr auto kKernelPath = "/Applications/Wolfram\\ Engine.app/Contents/Resour

MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
, m_lastId(0)
, m_rootLayout(new QVBoxLayout)
{
auto* root = new QWidget;
Expand Down Expand Up @@ -48,14 +47,15 @@ MainWindow::~MainWindow()

void MainWindow::pushNewCell()
{
auto* cell = new Cell(this, ++m_lastId);
auto* cell = new Cell(this);

m_cells.append(cell);
m_rootLayout->insertWidget(m_rootLayout->count() - 1, cell);
}

QString MainWindow::engineEval(const QString& input) const
amu::Engine* MainWindow::engine()
{
return QString::fromStdString(m_engine.eval(input.toStdString()));
return &m_engine;
}

void MainWindow::cellEvaluated(unsigned id)
Expand Down
8 changes: 2 additions & 6 deletions ui/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
class MainWindow : public QMainWindow {
Q_OBJECT

unsigned m_lastId;

amu::Engine m_engine;

QList<Cell*> m_cells;
Expand All @@ -34,10 +32,8 @@ class MainWindow : public QMainWindow {
MainWindow(QWidget* parent = nullptr);
~MainWindow();

/// Evaluate a string under the connected engine.
///
/// \param input The input (Wolfram Language) to evaluate
QString engineEval(const QString& input) const;
/// Get a handle to the connected engine.
amu::Engine* engine();

public Q_SLOTS:

Expand Down

0 comments on commit aea14d3

Please sign in to comment.