[apps/shared] Factorize go to parameter controllers in parent class

(used in regression, graph, sequence)

Change-Id: I5e54bca17f9de7b21aeb705ea87818b064662904
This commit is contained in:
Émilie Feral
2017-03-30 16:08:40 +02:00
parent 4968ffff49
commit e76cab75c1
10 changed files with 92 additions and 114 deletions

View File

@@ -8,10 +8,8 @@ using namespace Poincare;
namespace Regression {
GoToParameterController::GoToParameterController(Responder * parentResponder, Store * store, CurveViewCursor * cursor) :
FloatParameterController(parentResponder),
m_abscisseCell(MessageTableCellWithEditableText(&m_selectableTableView, this, m_draftTextBuffer)),
Shared::GoToParameterController(parentResponder, store, cursor, I18n::Message::X),
m_store(store),
m_cursor(cursor),
m_xPrediction(true)
{
}
@@ -27,15 +25,6 @@ const char * GoToParameterController::title() {
return "Prediction sachant y";
}
int GoToParameterController::numberOfRows() {
return 2;
}
float GoToParameterController::previousParameterAtIndex(int index) {
assert(index == 0);
return m_previousParameter;
}
float GoToParameterController::parameterAtIndex(int index) {
assert(index == 0);
if (m_xPrediction) {
@@ -50,33 +39,26 @@ bool GoToParameterController::setParameterAtIndex(int parameterIndex, float f) {
app()->displayWarning(I18n::Message::ForbiddenValue);
return false;
}
float x = m_store->xValueForYValue(f);
if (m_xPrediction) {
float y = m_store->yValueForXValue(f);
if (fabsf(y) > k_maxDisplayableFloat) {
app()->displayWarning(I18n::Message::ForbiddenValue);
return false;
}
m_cursor->moveTo(f, y);
x = m_store->yValueForXValue(f);
}
if (fabsf(x) > k_maxDisplayableFloat) {
app()->displayWarning(I18n::Message::ForbiddenValue);
return false;
}
if (isnan(x)) {
app()->displayWarning(I18n::Message::ValueNotReachedByRegression);
return false;
}
if (m_xPrediction) {
m_cursor->moveTo(f, x);
} else {
float x = m_store->xValueForYValue(f);
if (fabsf(x) > k_maxDisplayableFloat) {
app()->displayWarning(I18n::Message::ForbiddenValue);
return false;
}
m_cursor->moveTo(x, f);
}
return true;
}
HighlightCell * GoToParameterController::reusableParameterCell(int index, int type) {
assert(index == 0);
return &m_abscisseCell;
}
int GoToParameterController::reusableParameterCellCount(int type) {
return 1;
}
void GoToParameterController::willDisplayCellForIndex(HighlightCell * cell, int index) {
if (index == numberOfRows()-1) {
return;
@@ -90,31 +72,4 @@ void GoToParameterController::willDisplayCellForIndex(HighlightCell * cell, int
FloatParameterController::willDisplayCellForIndex(cell, index);
}
bool GoToParameterController::textFieldDidFinishEditing(TextField * textField, const char * text) {
AppsContainer * appsContainer = ((TextFieldDelegateApp *)app())->container();
Context * globalContext = appsContainer->globalContext();
float floatBody = Expression::parse(text)->approximate(*globalContext);
float parameter = m_store->yValueForXValue(floatBody);
if (!m_xPrediction) {
parameter = m_store->xValueForYValue(floatBody);
}
if (isnan(parameter)) {
app()->displayWarning(I18n::Message::ValueNotReachedByRegression);
return false;
}
return FloatParameterController::textFieldDidFinishEditing(textField, text);
}
void GoToParameterController::viewWillAppear() {
m_previousParameter = parameterAtIndex(0);
}
void GoToParameterController::buttonAction() {
m_store->centerAxisAround(CurveViewRange::Axis::X, m_cursor->x());
m_store->centerAxisAround(CurveViewRange::Axis::Y, m_cursor->y());
StackViewController * stack = (StackViewController *)parentResponder();
stack->pop();
stack->pop();
}
}

View File

@@ -2,34 +2,21 @@
#define REGRESSION_GO_TO_PARAMETER_CONTROLLER_H
#include <escher.h>
#include "../shared/float_parameter_controller.h"
#include "../shared/curve_view_cursor.h"
#include "../shared/go_to_parameter_controller.h"
#include "store.h"
namespace Regression {
class GoToParameterController : public Shared::FloatParameterController {
class GoToParameterController : public Shared::GoToParameterController {
public:
GoToParameterController(Responder * parentResponder, Store * store, Shared::CurveViewCursor * cursor);
void setXPrediction(bool xPrediction);
const char * title() override;
int numberOfRows() override;
void willDisplayCellForIndex(HighlightCell * cell, int index) override;
bool textFieldDidFinishEditing(TextField * textField, const char * text) override;
void viewWillAppear() override;
private:
constexpr static float k_maxDisplayableFloat = 1E8f;
HighlightCell * reusableParameterCell(int index, int type) override;
int reusableParameterCellCount(int type) override;
float previousParameterAtIndex(int index) override;
float parameterAtIndex(int index) override;
bool setParameterAtIndex(int parameterIndex, float f) override;
void buttonAction() override;
char m_draftTextBuffer[MessageTableCellWithEditableText::k_bufferLength];
MessageTableCellWithEditableText m_abscisseCell;
float m_previousParameter;
Store * m_store;
Shared::CurveViewCursor * m_cursor;
bool m_xPrediction;
};

View File

@@ -10,6 +10,7 @@ app_objs += $(addprefix apps/shared/,\
float_parameter_controller.o\
function.o\
function_curve_parameter_controller.o\
function_go_to_parameter_controller.o\
function_graph_view.o\
function_graph_controller.o\
function_store.o\

View File

@@ -30,6 +30,7 @@ public:
protected:
int activeCell();
StackViewController * stackController();
virtual float parameterAtIndex(int index) = 0;
SelectableTableView m_selectableTableView;
private:
constexpr static int k_buttonMargin = 6;
@@ -38,7 +39,6 @@ private:
virtual HighlightCell * reusableParameterCell(int index, int type) = 0;
TextFieldDelegateApp * textFieldDelegateApp() override;
virtual float previousParameterAtIndex(int index) = 0;
virtual float parameterAtIndex(int index) = 0;
virtual bool setParameterAtIndex(int parameterIndex, float f) = 0;
ButtonWithSeparator m_okButton;
};

View File

@@ -8,7 +8,7 @@ FunctionCurveParameterController::FunctionCurveParameterController(InteractiveCu
m_goToCell(MessageTableCellWithChevron(I18n::Message::Goto)),
m_selectableTableView(SelectableTableView(this, this, 0, 1, Metric::CommonTopMargin, Metric::CommonRightMargin,
Metric::CommonBottomMargin, Metric::CommonLeftMargin)),
m_goToParameterController(GoToParameterController(this, graphRange, cursor, symbol)),
m_goToParameterController(FunctionGoToParameterController(this, graphRange, cursor, symbol)),
m_function(nullptr)
{
}

View File

@@ -2,7 +2,7 @@
#define SHARED_FUNCTION_CURVE_PARAMETER_CONTROLLER_H
#include <escher.h>
#include "go_to_parameter_controller.h"
#include "function_go_to_parameter_controller.h"
#include "function.h"
#include "curve_view_cursor.h"
#include "interactive_curve_view_range.h"
@@ -21,7 +21,7 @@ protected:
MessageTableCellWithChevron m_goToCell;
SelectableTableView m_selectableTableView;
private:
GoToParameterController m_goToParameterController;
FunctionGoToParameterController m_goToParameterController;
Function * m_function;
};

View File

@@ -0,0 +1,44 @@
#include "function_go_to_parameter_controller.h"
#include "text_field_delegate_app.h"
#include <assert.h>
using namespace Poincare;
namespace Shared {
FunctionGoToParameterController::FunctionGoToParameterController(Responder * parentResponder, InteractiveCurveViewRange * graphRange, CurveViewCursor * cursor, I18n::Message symbol) :
GoToParameterController(parentResponder, graphRange, cursor, symbol),
m_function(nullptr)
{
}
const char * FunctionGoToParameterController::title() {
return I18n::translate(I18n::Message::Goto);
}
float FunctionGoToParameterController::parameterAtIndex(int index) {
assert(index == 0);
return m_cursor->x();
}
bool FunctionGoToParameterController::setParameterAtIndex(int parameterIndex, float f) {
assert(parameterIndex == 0);
TextFieldDelegateApp * myApp = (TextFieldDelegateApp *)app();
float y = m_function->evaluateAtAbscissa(f, myApp->localContext());
if (fabsf(f) > k_maxDisplayableFloat || fabsf(y) > k_maxDisplayableFloat) {
app()->displayWarning(I18n::Message::ForbiddenValue);
return false;
}
if (isnan(y) || isinf(y)) {
app()->displayWarning(I18n::Message::ValueNotReachedByFunction);
return false;
}
m_cursor->moveTo(f, y);
return true;
}
void FunctionGoToParameterController::setFunction(Function * function) {
m_function = function;
}
}

View File

@@ -0,0 +1,22 @@
#ifndef SHARED_FUNCTION_GO_TO_PARAMETER_CONTROLLER_H
#define SHARED_FUNCTION_GO_TO_PARAMETER_CONTROLLER_H
#include "go_to_parameter_controller.h"
#include "function.h"
namespace Shared {
class FunctionGoToParameterController : public GoToParameterController {
public:
FunctionGoToParameterController(Responder * parentResponder, InteractiveCurveViewRange * graphRange, CurveViewCursor * cursor, I18n::Message symbol);
const char * title() override;
void setFunction(Function * function);
private:
float parameterAtIndex(int index) override;
bool setParameterAtIndex(int parameterIndex, float f) override;
Function * m_function;
};
}
#endif

View File

@@ -6,17 +6,12 @@ namespace Shared {
GoToParameterController::GoToParameterController(Responder * parentResponder, InteractiveCurveViewRange * graphRange, CurveViewCursor * cursor, I18n::Message symbol) :
FloatParameterController(parentResponder),
m_abscisseCell(MessageTableCellWithEditableText(&m_selectableTableView, this, m_draftTextBuffer, symbol)),
m_graphRange(graphRange),
m_cursor(cursor),
m_function(nullptr)
m_abscisseCell(MessageTableCellWithEditableText(&m_selectableTableView, this, m_draftTextBuffer, symbol)),
m_graphRange(graphRange)
{
}
const char * GoToParameterController::title() {
return I18n::translate(I18n::Message::Goto);
}
void GoToParameterController::viewWillAppear() {
m_previousParameter = parameterAtIndex(0);
FloatParameterController::viewWillAppear();
@@ -31,23 +26,6 @@ float GoToParameterController::previousParameterAtIndex(int index) {
return m_previousParameter;
}
float GoToParameterController::parameterAtIndex(int index) {
assert(index == 0);
return m_cursor->x();
}
bool GoToParameterController::setParameterAtIndex(int parameterIndex, float f) {
assert(parameterIndex == 0);
TextFieldDelegateApp * myApp = (TextFieldDelegateApp *)app();
float y = m_function->evaluateAtAbscissa(f, myApp->localContext());
if (fabsf(f) > k_maxDisplayableFloat || fabsf(y) > k_maxDisplayableFloat) {
app()->displayWarning(I18n::Message::ForbiddenValue);
return false;
}
m_cursor->moveTo(f, y);
return true;
}
HighlightCell * GoToParameterController::reusableParameterCell(int index, int type) {
assert(index == 0);
return &m_abscisseCell;
@@ -57,10 +35,6 @@ int GoToParameterController::reusableParameterCellCount(int type) {
return 1;
}
void GoToParameterController::setFunction(Function * function) {
m_function = function;
}
void GoToParameterController::buttonAction() {
m_graphRange->centerAxisAround(CurveViewRange::Axis::X, m_cursor->x());
m_graphRange->centerAxisAround(CurveViewRange::Axis::Y, m_cursor->y());

View File

@@ -5,31 +5,26 @@
#include "float_parameter_controller.h"
#include "curve_view_cursor.h"
#include "interactive_curve_view_range.h"
#include "function.h"
namespace Shared {
class GoToParameterController : public FloatParameterController {
public:
GoToParameterController(Responder * parentResponder, InteractiveCurveViewRange * graphRange, CurveViewCursor * cursor, I18n::Message symbol);
const char * title() override;
void viewWillAppear() override;
void setFunction(Function * function);
int numberOfRows() override;
private:
protected:
constexpr static float k_maxDisplayableFloat = 1E8f;
CurveViewCursor * m_cursor;
private:
void buttonAction() override;
HighlightCell * reusableParameterCell(int index, int type) override;
int reusableParameterCellCount(int type) override;
float previousParameterAtIndex(int index) override;
float parameterAtIndex(int index) override;
bool setParameterAtIndex(int parameterIndex, float f) override;
char m_draftTextBuffer[MessageTableCellWithEditableText::k_bufferLength];
MessageTableCellWithEditableText m_abscisseCell;
float m_previousParameter;
InteractiveCurveViewRange * m_graphRange;
CurveViewCursor * m_cursor;
Function * m_function;
};
}