mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-19 00:37:25 +01:00
[apps/calculation] Create input and output layout according to display
mode set in preference Change-Id: If161958b2fcdad802fda5f8e0c44f8a5fc9d9ef1
This commit is contained in:
@@ -10,7 +10,7 @@ AppsContainer::AppsContainer() :
|
||||
m_homeApp(this),
|
||||
m_graphApp(this, &m_globalContext),
|
||||
m_probabilityApp(this),
|
||||
m_calculationApp(this, &m_globalContext),
|
||||
m_calculationApp(this, &m_globalContext, &m_preferences),
|
||||
m_regressionApp(this),
|
||||
m_settingsApp(this, &m_preferences),
|
||||
m_statisticsApp(this),
|
||||
|
||||
@@ -3,9 +3,10 @@
|
||||
|
||||
namespace Calculation {
|
||||
|
||||
App::App(Container * container, Context * context) :
|
||||
App::App(Container * container, Context * context, Preferences * preferences) :
|
||||
TextFieldDelegateApp(container, &m_editExpressionController, "Calculs", "CALCULS", ImageStore::CalculationIcon),
|
||||
m_localContext(LocalContext((GlobalContext *)context, &m_calculationStore)),
|
||||
m_preferences(preferences),
|
||||
m_calculationStore(CalculationStore()),
|
||||
m_historyController(HistoryController(&m_editExpressionController, &m_calculationStore)),
|
||||
m_editExpressionController(EditExpressionController(&m_modalViewController, &m_historyController, &m_calculationStore))
|
||||
@@ -16,4 +17,8 @@ Context * App::localContext() {
|
||||
return &m_localContext;
|
||||
}
|
||||
|
||||
Preferences * App::preferences() {
|
||||
return m_preferences;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,16 +5,19 @@
|
||||
#include "local_context.h"
|
||||
#include "history_controller.h"
|
||||
#include "../text_field_delegate_app.h"
|
||||
#include "../preferences.h"
|
||||
#include <escher.h>
|
||||
|
||||
namespace Calculation {
|
||||
|
||||
class App : public TextFieldDelegateApp {
|
||||
public:
|
||||
App(Container * container, Context * context);
|
||||
App(Container * container, Context * context, Preferences * preferences);
|
||||
Context * localContext() override;
|
||||
Preferences * preferences();
|
||||
private:
|
||||
LocalContext m_localContext;
|
||||
Preferences * m_preferences;
|
||||
CalculationStore m_calculationStore;
|
||||
HistoryController m_historyController;
|
||||
EditExpressionController m_editExpressionController;
|
||||
|
||||
@@ -11,59 +11,6 @@ Calculation::Calculation() :
|
||||
{
|
||||
}
|
||||
|
||||
Calculation & Calculation::operator= (const Calculation & other) {
|
||||
strlcpy(m_text, other.m_text, sizeof(m_text));
|
||||
if (m_input != nullptr) {
|
||||
delete m_input;
|
||||
}
|
||||
m_input = nullptr;
|
||||
if (other.m_input) {
|
||||
m_input = Expression::parse(m_text);
|
||||
}
|
||||
if (m_inputLayout != nullptr) {
|
||||
delete m_inputLayout;
|
||||
}
|
||||
m_inputLayout = nullptr;
|
||||
if (m_input && other.m_inputLayout) {
|
||||
m_inputLayout = m_input->createLayout();
|
||||
}
|
||||
if (m_output != nullptr) {
|
||||
delete m_output;
|
||||
}
|
||||
m_output = nullptr;
|
||||
if (other.m_output) {
|
||||
m_output = other.m_output->clone();
|
||||
}
|
||||
if (m_outputLayout != nullptr) {
|
||||
delete m_outputLayout;
|
||||
}
|
||||
m_outputLayout = nullptr;
|
||||
if (m_output && other.m_outputLayout) {
|
||||
m_outputLayout = m_output->createLayout();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Calculation::setContent(const char * c, Context * context) {
|
||||
strlcpy(m_text, c, sizeof(m_text));
|
||||
if (m_input != nullptr) {
|
||||
delete m_input;
|
||||
}
|
||||
m_input = Expression::parse(m_text);
|
||||
if (m_inputLayout != nullptr) {
|
||||
delete m_inputLayout;
|
||||
}
|
||||
m_inputLayout = m_input->createLayout();
|
||||
if (m_output != nullptr) {
|
||||
delete m_outputLayout;
|
||||
}
|
||||
m_output = m_input->evaluate(*context);
|
||||
if (m_outputLayout != nullptr) {
|
||||
delete m_outputLayout;
|
||||
}
|
||||
m_outputLayout = m_output->createLayout();
|
||||
}
|
||||
|
||||
Calculation::~Calculation() {
|
||||
if (m_inputLayout != nullptr) {
|
||||
delete m_inputLayout;
|
||||
@@ -79,6 +26,46 @@ Calculation::~Calculation() {
|
||||
}
|
||||
}
|
||||
|
||||
void Calculation::reset() {
|
||||
m_text[0] = 0;
|
||||
if (m_input != nullptr) {
|
||||
delete m_input;
|
||||
}
|
||||
m_input = nullptr;
|
||||
if (m_inputLayout != nullptr) {
|
||||
delete m_inputLayout;
|
||||
}
|
||||
m_inputLayout = nullptr;
|
||||
if (m_output != nullptr) {
|
||||
delete m_output;
|
||||
}
|
||||
m_output = nullptr;
|
||||
if (m_outputLayout != nullptr) {
|
||||
delete m_outputLayout;
|
||||
}
|
||||
m_outputLayout = nullptr;
|
||||
}
|
||||
|
||||
void Calculation::setContent(const char * c, Context * context, Preferences * preferences) {
|
||||
strlcpy(m_text, c, sizeof(m_text));
|
||||
if (m_input != nullptr) {
|
||||
delete m_input;
|
||||
}
|
||||
m_input = Expression::parse(m_text);
|
||||
if (m_inputLayout != nullptr) {
|
||||
delete m_inputLayout;
|
||||
}
|
||||
m_inputLayout = m_input->createLayout(preferences->displayMode());
|
||||
if (m_output != nullptr) {
|
||||
delete m_output;
|
||||
}
|
||||
m_output = m_input->evaluate(*context);
|
||||
if (m_outputLayout != nullptr) {
|
||||
delete m_outputLayout;
|
||||
}
|
||||
m_outputLayout = m_output->createLayout(preferences->displayMode());
|
||||
}
|
||||
|
||||
const char * Calculation::text() {
|
||||
return m_text;
|
||||
}
|
||||
@@ -100,7 +87,7 @@ ExpressionLayout * Calculation::outputLayout() {
|
||||
}
|
||||
|
||||
bool Calculation::isEmpty() {
|
||||
if (m_input == nullptr) {
|
||||
if (m_output == nullptr) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -2,20 +2,27 @@
|
||||
#define CALCULATION_CALCULATION_H
|
||||
|
||||
#include <poincare.h>
|
||||
#include "../preferences.h"
|
||||
|
||||
namespace Calculation {
|
||||
|
||||
class Calculation {
|
||||
public:
|
||||
Calculation();
|
||||
~Calculation(); // Delete expression and layout, if needed
|
||||
Calculation & operator= (const Calculation & other);
|
||||
~Calculation(); // Delete expression and layout, if needed
|
||||
/* The copy assignment operator is deleted as its default implementation does
|
||||
* not create new expressions. The new object thus become obsolete as soon
|
||||
* as the copy is deleted (because of our implementation of deletion). To
|
||||
* avoid any use of obsolete object, we prevent to copy and assign. */
|
||||
Calculation & operator= (const Calculation & other) = delete;
|
||||
/* c.reset() is the equivalent of c = Calculation() without copy assingment. */
|
||||
void reset();
|
||||
const char * text();
|
||||
Expression * input();
|
||||
ExpressionLayout * inputLayout();
|
||||
Expression * output();
|
||||
ExpressionLayout * outputLayout();
|
||||
void setContent(const char * c, Context * context);
|
||||
void setContent(const char * c, Context * context, Preferences * preferences);
|
||||
bool isEmpty();
|
||||
constexpr static int k_maximalExpressionTextLength = 255;
|
||||
private:
|
||||
|
||||
@@ -8,9 +8,9 @@ CalculationStore::CalculationStore() :
|
||||
{
|
||||
}
|
||||
|
||||
Calculation * CalculationStore::push(Calculation * c) {
|
||||
Calculation * CalculationStore::push(const char * text, Context * context, Preferences * preferences) {
|
||||
Calculation * result = m_start;
|
||||
*m_start++ = *c;
|
||||
m_start++->setContent(text, context, preferences);
|
||||
if (m_start >= m_calculations + k_maxNumberOfCalculations) {
|
||||
m_start = m_calculations;
|
||||
}
|
||||
@@ -45,7 +45,7 @@ int CalculationStore::numberOfCalculations() {
|
||||
}
|
||||
|
||||
void CalculationStore::deleteCalculationAtIndex(int i) {
|
||||
*calculationAtIndex(i) = Calculation();
|
||||
calculationAtIndex(i)->reset();
|
||||
}
|
||||
|
||||
void CalculationStore::deleteAll() {
|
||||
@@ -53,7 +53,7 @@ void CalculationStore::deleteAll() {
|
||||
Calculation * currentCalc= m_start;
|
||||
while (currentCalc < m_calculations + k_maxNumberOfCalculations) {
|
||||
if (!currentCalc->isEmpty()) {
|
||||
*currentCalc = Calculation();
|
||||
currentCalc->reset();
|
||||
}
|
||||
currentCalc++;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define CALCULATION_CALCULATION_STORE_H
|
||||
|
||||
#include "calculation.h"
|
||||
#include "../preferences.h"
|
||||
|
||||
namespace Calculation {
|
||||
|
||||
@@ -11,7 +12,7 @@ class CalculationStore {
|
||||
public:
|
||||
CalculationStore();
|
||||
Calculation * calculationAtIndex(int i);
|
||||
Calculation * push(Calculation * c);
|
||||
Calculation * push(const char * text, Context * context, Preferences * preferences);
|
||||
void deleteCalculationAtIndex(int i);
|
||||
void deleteAll();
|
||||
int numberOfCalculations();
|
||||
|
||||
@@ -80,10 +80,8 @@ bool EditExpressionController::textFieldDidReceiveEvent(::TextField * textField,
|
||||
}
|
||||
|
||||
bool EditExpressionController::textFieldDidFinishEditing(::TextField * textField, const char * text) {
|
||||
Calculation calculation = Calculation();
|
||||
App * calculationApp = (App *)app();
|
||||
calculation.setContent(textBody(), calculationApp->localContext());
|
||||
m_calculationStore->push(&calculation);
|
||||
m_calculationStore->push(textBody(), calculationApp->localContext(), calculationApp->preferences());
|
||||
m_historyController->reload();
|
||||
m_contentView.mainView()->scrollToCell(0, m_historyController->numberOfRows()-1);
|
||||
m_contentView.textField()->setText("");
|
||||
|
||||
@@ -60,17 +60,17 @@ bool HistoryController::handleEvent(Ion::Events::Event event) {
|
||||
HistoryViewCell::SubviewType subviewType = selectedCell->selectedSubviewType();
|
||||
EditExpressionController * editController = (EditExpressionController *)parentResponder();
|
||||
Calculation * calculation = m_calculationStore->calculationAtIndex(focusRow);
|
||||
Calculation newCalculation;
|
||||
const char * text;
|
||||
if (subviewType == HistoryViewCell::SubviewType::Input) {
|
||||
newCalculation = *calculation;
|
||||
text = calculation->text();
|
||||
} else {
|
||||
char outputText[Calculation::k_maximalExpressionTextLength];
|
||||
calculation->output()->writeTextInBuffer(outputText, Calculation::k_maximalExpressionTextLength);
|
||||
App * calculationApp = (App *)app();
|
||||
newCalculation.setContent(outputText, calculationApp->localContext());
|
||||
text = outputText;
|
||||
}
|
||||
m_selectableTableView.deselectTable();
|
||||
m_calculationStore->push(&newCalculation);
|
||||
App * calculationApp = (App *)app();
|
||||
m_calculationStore->push(text, calculationApp->localContext(), calculationApp->preferences());
|
||||
reload();
|
||||
m_selectableTableView.scrollToCell(0, numberOfRows()-1);
|
||||
app()->setFirstResponder(editController);
|
||||
|
||||
Reference in New Issue
Block a user