[calculation] Make the results of calculation expressions

Change-Id: I9eb685b21944cfc6cc346a42d442f064ae5938dd
This commit is contained in:
Émilie Feral
2016-11-25 13:51:32 +01:00
parent 713a41cffe
commit 0448278e36
10 changed files with 72 additions and 35 deletions

View File

@@ -6,7 +6,8 @@ namespace Calculation {
Calculation::Calculation() :
m_expression(nullptr),
m_layout(nullptr),
m_evaluation(0.0f)
m_evaluation(nullptr),
m_evaluationLayout(nullptr)
{
}
@@ -26,7 +27,20 @@ Calculation & Calculation::operator= (const Calculation & other) {
if (m_expression && other.m_layout) {
m_layout = m_expression->createLayout();
}
m_evaluation = other.m_evaluation;
if (m_evaluation != nullptr) {
delete m_evaluation;
}
m_evaluation = nullptr;
if (other.m_evaluation) {
m_evaluation = other.m_evaluation->clone();
}
if (m_evaluationLayout != nullptr) {
delete m_evaluationLayout;
}
m_evaluationLayout = nullptr;
if (m_evaluation && other.m_evaluationLayout) {
m_evaluationLayout = m_evaluation->createLayout();
}
return *this;
}
@@ -39,8 +53,15 @@ void Calculation::setContent(const char * c, Context * context) {
if (m_layout != nullptr) {
delete m_layout;
}
m_layout = expression()->createLayout();
m_evaluation = m_expression->approximate(*context);
m_layout = m_expression->createLayout();
if (m_evaluation != nullptr) {
delete m_evaluationLayout;
}
m_evaluation = m_expression->createEvaluation(*context);
if (m_evaluationLayout != nullptr) {
delete m_evaluationLayout;
}
m_evaluationLayout = m_evaluation->createLayout();
}
Calculation::~Calculation() {
@@ -50,6 +71,12 @@ Calculation::~Calculation() {
if (m_expression != nullptr) {
delete m_expression;
}
if (m_evaluation != nullptr) {
delete m_evaluation;
}
if (m_evaluationLayout != nullptr) {
delete m_evaluationLayout;
}
}
const char * Calculation::text() {
@@ -64,10 +91,14 @@ ExpressionLayout * Calculation::layout() {
return m_layout;
}
float Calculation::evaluation() {
Expression * Calculation::evaluation() {
return m_evaluation;
}
ExpressionLayout * Calculation::evaluationLayout() {
return m_evaluationLayout;
}
bool Calculation::isEmpty() {
if (m_expression == nullptr) {
return true;

View File

@@ -13,7 +13,8 @@ public:
const char * text();
Expression * expression();
ExpressionLayout * layout();
float evaluation();
Expression * evaluation();
ExpressionLayout * evaluationLayout();
void setContent(const char * c, Context * context);
bool isEmpty();
private:
@@ -21,7 +22,8 @@ private:
char m_text[k_bodyLength];
Expression * m_expression;
ExpressionLayout * m_layout;
float m_evaluation;
Expression * m_evaluation;
ExpressionLayout * m_evaluationLayout;
};
}

View File

@@ -4,19 +4,19 @@
namespace Calculation {
EvaluateContext::EvaluateContext(::Context * parentContext, CalculationStore * calculationStore) :
m_ansValue(Float(0.0f)),
m_ansValue(nullptr),
m_calculationStore(calculationStore),
m_context(parentContext)
{
}
Float * EvaluateContext::ansValue() {
Expression * EvaluateContext::ansValue() {
if (m_calculationStore->numberOfCalculations() == 0) {
return defaultExpression();
}
Calculation * lastCalculation = m_calculationStore->calculationAtIndex(m_calculationStore->numberOfCalculations()-1);
m_ansValue = Float(lastCalculation->evaluation());
return &m_ansValue;
m_ansValue = lastCalculation->evaluation();
return m_ansValue;
}
const Expression * EvaluateContext::expressionForSymbol(const Symbol * symbol) {

View File

@@ -9,10 +9,10 @@ namespace Calculation {
class EvaluateContext : public ::Context {
public:
EvaluateContext(Context * parentContext, CalculationStore * calculationStore);
Float * ansValue();
Expression * ansValue();
const Expression * expressionForSymbol(const Symbol * symbol) override;
private:
Float m_ansValue;
Expression * m_ansValue;
CalculationStore * m_calculationStore;
::Context * m_context;
};

View File

@@ -47,9 +47,8 @@ bool HistoryController::handleEvent(Ion::Events::Event event) {
if (subviewType == HistoryViewCell::SubviewType::PrettyPrint) {
editController->setTextBody(calculation->text());
} else {
char buffer[Constant::FloatBufferSizeInScientificMode];
Float(calculation->evaluation()).convertFloatToText(buffer, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaInScientificMode);
editController->setTextBody(buffer);
char * resultText = calculation->evaluation()->text();
editController->setTextBody(resultText);
}
m_selectableTableView.deselectTable();
app()->setFirstResponder(editController);
@@ -65,10 +64,10 @@ bool HistoryController::handleEvent(Ion::Events::Event event) {
if (subviewType == HistoryViewCell::SubviewType::PrettyPrint) {
newCalculation = *calculation;
} else {
char buffer[Constant::FloatBufferSizeInScientificMode];
Float(calculation->evaluation()).convertFloatToText(buffer, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaInScientificMode);
App * calculationApp = (App *)app();
newCalculation.setContent(buffer, calculationApp->evaluateContext());
char * resultText = calculation->evaluation()->text();
/* TODO: this will work when we will parse float */
//App * calculationApp = (App *)app();
//newCalculation.setContent(resultText, calculationApp->evaluateContext());
}
m_selectableTableView.deselectTable();
m_calculationStore->push(&newCalculation);
@@ -152,7 +151,8 @@ void HistoryController::willDisplayCellForIndex(TableViewCell * cell, int index)
KDCoordinate HistoryController::rowHeight(int j) {
Calculation * calculation = m_calculationStore->calculationAtIndex(j);
KDCoordinate prettyPrintHeight = calculation->layout()->size().height();
return prettyPrintHeight + k_resultHeight + 3*HistoryViewCell::k_digitVerticalMargin;
KDCoordinate resultHeight = calculation->evaluationLayout()->size().height();
return prettyPrintHeight + resultHeight + 3*HistoryViewCell::k_digitVerticalMargin;
}
KDCoordinate HistoryController::cumulatedHeightFromIndex(int j) {

View File

@@ -32,7 +32,6 @@ public:
void tableViewDidChangeSelection(SelectableTableView * t, int previousSelectedCellX, int previousSelectedCellY) override;
private:
constexpr static int k_maxNumberOfDisplayedRows = 10;
constexpr static KDCoordinate k_resultHeight = 12;
HistoryViewCell m_calculationHistory[k_maxNumberOfDisplayedRows];
CalculationSelectableTableView m_selectableTableView;
CalculationStore * m_calculationStore;

View File

@@ -9,7 +9,7 @@ namespace Calculation {
HistoryViewCell::HistoryViewCell() :
Responder(nullptr),
m_prettyPrint(PrettyPrintView(this)),
m_result(BufferTextView(1.0f, 0.5f)),
m_result(ExpressionView()),
m_selectedSubviewType(HistoryViewCell::SubviewType::Result)
{
}
@@ -52,9 +52,7 @@ void HistoryViewCell::layoutSubviews() {
void HistoryViewCell::setCalculation(Calculation * calculation) {
m_prettyPrint.setExpression(calculation->layout());
char buffer[Constant::FloatBufferSizeInScientificMode];
Float(calculation->evaluation()).convertFloatToText(buffer, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaInScientificMode);
m_result.setText(buffer);
m_result.setExpression(calculation->evaluationLayout());
}
void HistoryViewCell::reloadCell() {

View File

@@ -29,7 +29,7 @@ public:
private:
constexpr static KDCoordinate k_resultWidth = 80;
PrettyPrintView m_prettyPrint;
BufferTextView m_result;
ExpressionView m_result;
SubviewType m_selectedSubviewType;
};

View File

@@ -23,17 +23,24 @@ bool ExpressionTextFieldDelegate::cursorInToken(TextField * textField, const cha
}
bool ExpressionTextFieldDelegate::textFieldDidReceiveEvent(TextField * textField, Ion::Events::Event event) {
if (event == Ion::Events::OK && Expression::parse(textField->text()) == nullptr) {
if (textField->textLength() == 0) {
if (event == Ion::Events::OK) {
Expression * exp = Expression::parse(textField->text());
if (exp == nullptr) {
if (textField->textLength() == 0) {
return true;
}
textField->app()->displayWarning("Attention a la syntaxe jeune padawan");
return true;
}
textField->app()->displayWarning("Attention a la syntaxe jeune padawan");
return true;
}
if (event == Ion::Events::OK &&
isnan(Expression::parse(textField->text())->approximate(*evaluateContext()))) {
Expression * evaluation = exp->createEvaluation(*evaluateContext());
if (evaluation == nullptr) {
delete exp;
textField->app()->displayWarning("Relis ton cours de maths, veux tu?");
return true;
} else {
delete evaluation;
delete exp;
}
}
if (event == Ion::Events::Toolbox) {
AppsContainer * appsContainer = (AppsContainer *)textField->app()->container();

View File

@@ -27,7 +27,7 @@ float Float::approximate(Context& context) const {
}
Expression * Float::createEvaluation(Context& context) const {
return (Expression *)this;
return new Float(m_float);
}
Expression::Type Float::type() const {