diff --git a/apps/graph/Makefile b/apps/graph/Makefile index 048c657f9..3060ae503 100644 --- a/apps/graph/Makefile +++ b/apps/graph/Makefile @@ -1,6 +1,7 @@ app_objs += $(addprefix apps/graph/,\ app.o\ evaluate_context.o\ + float_parameter_controller.o\ function.o\ function_store.o\ function_title_cell.o\ diff --git a/apps/graph/float_parameter_controller.cpp b/apps/graph/float_parameter_controller.cpp new file mode 100644 index 000000000..3ce80f7ba --- /dev/null +++ b/apps/graph/float_parameter_controller.cpp @@ -0,0 +1,85 @@ +#include "float_parameter_controller.h" +#include "app.h" +#include "../constant.h" +#include "../apps_container.h" +#include + +namespace Graph { + +FloatParameterController::FloatParameterController(Responder * parentResponder) : + ViewController(parentResponder), + m_selectableTableView(SelectableTableView(this, this, Metric::TopMargin, Metric::RightMargin, + Metric::BottomMargin, Metric::LeftMargin)) +{ +} + +View * FloatParameterController::view() { + return &m_selectableTableView; +} + +void FloatParameterController::didBecomeFirstResponder() { + m_selectableTableView.selectCellAtLocation(0, 0); + app()->setFirstResponder(&m_selectableTableView); +} + +int FloatParameterController::activeCell() { + return m_selectableTableView.selectedRow(); +} + +void FloatParameterController::willDisplayCellForIndex(TableViewCell * cell, int index) { + TextMenuListCell * myCell = (TextMenuListCell *) cell; + char buffer[Constant::FloatBufferSizeInScientificMode]; + Float(parameterAtIndex(index)).convertFloatToText(buffer, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaInScientificMode); + myCell->setAccessoryText(buffer); +} + +bool FloatParameterController::handleEvent(Ion::Events::Event event) { + if (event == Ion::Events::OK) { + editParameter(); + return true; + } + if (event.hasText()) { + editParameter(event.text()); + return true; + } + return false; +} + +void FloatParameterController::editParameter(const char * initialText) { + /* This code assumes that the active cell remains the one which is edited + * until the invocation is performed. This could lead to concurrency issue in + * other cases. */ + char initialTextContent[255]; + int cursorDelta = 0; + if (initialText) { + strlcpy(initialTextContent, initialText, sizeof(initialTextContent)); + cursorDelta = strlen(initialText) > 1 ? -1 : 0; + } else { + TextMenuListCell * textMenuListCell = (TextMenuListCell *)reusableCell(activeCell()); + strlcpy(initialTextContent, textMenuListCell->accessoryText(), sizeof(initialTextContent)); + } + int cursorLocation = strlen(initialTextContent) + cursorDelta; + App * myApp = (App *)app(); + InputViewController * inputController = myApp->inputViewController(); + inputController->edit(this, initialTextContent, cursorLocation, this, + [](void * context, void * sender){ + FloatParameterController * floatParameterController = (FloatParameterController *)context; + int activeCell = floatParameterController->activeCell(); + TextMenuListCell * cell = (TextMenuListCell *)floatParameterController->reusableCell(activeCell); + InputViewController * myInputViewController = (InputViewController *)sender; + const char * textBody = myInputViewController->textBody(); + AppsContainer * appsContainer = (AppsContainer *)floatParameterController->app()->container(); + Context * globalContext = appsContainer->context(); + float floatBody = Expression::parse(textBody)->approximate(*globalContext); + floatParameterController->setParameterAtIndex(activeCell, floatBody); + floatParameterController->willDisplayCellForIndex(cell, activeCell); + }, + [](void * context, void * sender){ + }); +} + +KDCoordinate FloatParameterController::cellHeight() { + return 35; +} + +} diff --git a/apps/graph/float_parameter_controller.h b/apps/graph/float_parameter_controller.h new file mode 100644 index 000000000..a23fe164a --- /dev/null +++ b/apps/graph/float_parameter_controller.h @@ -0,0 +1,35 @@ +#ifndef GRAPH_FLOAT_PARAMETER_CONTROLLER_H +#define GRAPH_FLOAT_PARAMETER_CONTROLLER_H + +#include + +namespace Graph { + +/* This controller edits float parameter of any model (given through + * parameterAtIndex and setParameterAtIndex). */ + +class FloatParameterController : public ViewController, public SimpleListViewDataSource { +public: + FloatParameterController(Responder * parentResponder); + int activeCell(); + void editParameter(const char * initialText = nullptr); + + View * view() override; + //const char * title() const override; + bool handleEvent(Ion::Events::Event event) override; + void didBecomeFirstResponder() override; + //int numberOfRows() override; + KDCoordinate cellHeight() override; + //TableViewCell * reusableCell(int index) override; + //int reusableCellCount() override; + void willDisplayCellForIndex(TableViewCell * cell, int index) override; +private: + virtual float parameterAtIndex(int index) = 0; + virtual void setParameterAtIndex(int parameterIndex, float f) = 0; + SelectableTableView m_selectableTableView; + +}; + +} + +#endif diff --git a/apps/graph/values/values_parameter_controller.cpp b/apps/graph/values/values_parameter_controller.cpp index 0929e6687..37271ade6 100644 --- a/apps/graph/values/values_parameter_controller.cpp +++ b/apps/graph/values/values_parameter_controller.cpp @@ -1,19 +1,14 @@ #include "values_parameter_controller.h" -#include "../app.h" -#include "../../constant.h" -#include "../../apps_container.h" #include namespace Graph { ValuesParameterController::ValuesParameterController(Responder * parentResponder, Interval * interval) : - ViewController(parentResponder), + FloatParameterController(parentResponder), m_interval(interval), m_intervalStartCell(TextMenuListCell((char*)"X Debut")), m_intervalEndCell(TextMenuListCell((char*)"X Fin")), - m_intervalStepCell(TextMenuListCell((char*)"Pas")), - m_selectableTableView(SelectableTableView(this, this, Metric::TopMargin, Metric::RightMargin, - Metric::BottomMargin, Metric::LeftMargin)) + m_intervalStepCell(TextMenuListCell((char*)"Pas")) { } @@ -21,46 +16,25 @@ const char * ValuesParameterController::title() const { return "Regler l'intervalle"; } -View * ValuesParameterController::view() { - return &m_selectableTableView; -} - Graph::Interval * ValuesParameterController::interval() { return m_interval; } -void ValuesParameterController::didBecomeFirstResponder() { - m_selectableTableView.selectCellAtLocation(0, 0); - app()->setFirstResponder(&m_selectableTableView); -} - -int ValuesParameterController::activeCell() { - return m_selectableTableView.selectedRow(); -} - -void ValuesParameterController::willDisplayCellForIndex(TableViewCell * cell, int index) { - TextMenuListCell * myCell = (TextMenuListCell *) cell; - char buffer[Constant::FloatBufferSizeInScientificMode]; +float ValuesParameterController::parameterAtIndex(int index) { switch (index) { case 0: - Float(m_interval->start()).convertFloatToText(buffer, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaInScientificMode); - myCell->setAccessoryText(buffer); - break; + return m_interval->start(); case 1: - Float(m_interval->end()).convertFloatToText(buffer, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaInScientificMode); - myCell->setAccessoryText(buffer); - break; + return m_interval->end(); case 2: - Float(m_interval->step()).convertFloatToText(buffer, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaInScientificMode); - myCell->setAccessoryText(buffer); - break; + return m_interval->step(); default: assert(false); - return; + return 0.0f; } } -void ValuesParameterController::setIntervalParameterAtIndex(int parameterIndex, float f) { +void ValuesParameterController::setParameterAtIndex(int parameterIndex, float f) { switch(parameterIndex) { case 0: m_interval->setStart(f); @@ -76,51 +50,6 @@ void ValuesParameterController::setIntervalParameterAtIndex(int parameterIndex, } } -bool ValuesParameterController::handleEvent(Ion::Events::Event event) { - if (event == Ion::Events::OK) { - editInterval(); - return true; - } - if (event.hasText()) { - editInterval(event.text()); - return true; - } - return false; -} - -void ValuesParameterController::editInterval(const char * initialText) { - /* This code assumes that the active cell remains the one which is edited - * until the invocation is performed. This could lead to concurrency issue in - * other cases. */ - char initialTextContent[255]; - int cursorDelta = 0; - if (initialText) { - strlcpy(initialTextContent, initialText, sizeof(initialTextContent)); - cursorDelta = strlen(initialText) > 1 ? -1 : 0; - } else { - TextMenuListCell * textMenuListCell = (TextMenuListCell *)reusableCell(activeCell()); - strlcpy(initialTextContent, textMenuListCell->accessoryText(), sizeof(initialTextContent)); - } - int cursorLocation = strlen(initialTextContent) + cursorDelta; - App * myApp = (App *)app(); - InputViewController * inputController = myApp->inputViewController(); - inputController->edit(this, initialTextContent, cursorLocation, this, - [](void * context, void * sender){ - ValuesParameterController * valuesParameterController = (ValuesParameterController *)context; - int activeCell = valuesParameterController->activeCell(); - TextMenuListCell * cell = (TextMenuListCell *)valuesParameterController->reusableCell(activeCell); - InputViewController * myInputViewController = (InputViewController *)sender; - const char * textBody = myInputViewController->textBody(); - AppsContainer * appsContainer = (AppsContainer *)valuesParameterController->app()->container(); - Context * globalContext = appsContainer->context(); - float floatBody = Expression::parse(textBody)->approximate(*globalContext); - valuesParameterController->setIntervalParameterAtIndex(activeCell, floatBody); - valuesParameterController->willDisplayCellForIndex(cell, activeCell); - }, - [](void * context, void * sender){ - }); -} - int ValuesParameterController::numberOfRows() { return k_totalNumberOfCell; }; @@ -136,8 +65,4 @@ int ValuesParameterController::reusableCellCount() { return k_totalNumberOfCell; } -KDCoordinate ValuesParameterController::cellHeight() { - return 35; -} - } diff --git a/apps/graph/values/values_parameter_controller.h b/apps/graph/values/values_parameter_controller.h index c0e8e2e08..01c5c38d9 100644 --- a/apps/graph/values/values_parameter_controller.h +++ b/apps/graph/values/values_parameter_controller.h @@ -3,32 +3,25 @@ #include #include "interval.h" +#include "../float_parameter_controller.h" namespace Graph { -class ValuesParameterController : public ViewController, public SimpleListViewDataSource { +class ValuesParameterController : public FloatParameterController { public: ValuesParameterController(Responder * parentResponder, Interval * interval); Interval * interval(); - int activeCell(); - void editInterval(const char * initialText = nullptr); - void setIntervalParameterAtIndex(int parameterIndex, float f); - - View * view() override; const char * title() const override; - bool handleEvent(Ion::Events::Event event) override; - void didBecomeFirstResponder() override; int numberOfRows() override; - KDCoordinate cellHeight() override; TableViewCell * reusableCell(int index) override; int reusableCellCount() override; - void willDisplayCellForIndex(TableViewCell * cell, int index) override; private: + float parameterAtIndex(int index) override; + void setParameterAtIndex(int parameterIndex, float f) override; constexpr static int k_totalNumberOfCell = 3; Interval * m_interval; TextMenuListCell m_intervalStartCell; TextMenuListCell m_intervalEndCell; TextMenuListCell m_intervalStepCell; - SelectableTableView m_selectableTableView; }; }