From dc3c67a2d29a7a9c7de3a97ccbb245c3f992720d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Thu, 3 Oct 2019 15:10:41 +0200 Subject: [PATCH] [apps/shared][apps/graph][apps/sequence] Clean ValuesController --- apps/graph/values/values_controller.cpp | 177 +++++++++++---------- apps/graph/values/values_controller.h | 63 +++++--- apps/sequence/values/values_controller.cpp | 26 ++- apps/sequence/values/values_controller.h | 32 +++- apps/shared/values_controller.cpp | 94 ++++++----- apps/shared/values_controller.h | 53 ++++-- 6 files changed, 278 insertions(+), 167 deletions(-) diff --git a/apps/graph/values/values_controller.cpp b/apps/graph/values/values_controller.cpp index 3922be41d..dae254a18 100644 --- a/apps/graph/values/values_controller.cpp +++ b/apps/graph/values/values_controller.cpp @@ -9,6 +9,8 @@ using namespace Poincare; namespace Graph { +// Constructors + ValuesController::ValuesController(Responder * parentResponder, InputEventHandlerDelegate * inputEventHandlerDelegate, ButtonRowController * header) : Shared::ValuesController(parentResponder, header), m_selectableTableView(this), @@ -43,6 +45,8 @@ ValuesController::ValuesController(Responder * parentResponder, InputEventHandle m_selectableTableView.setDelegate(this); } +// TableViewDataSource + KDCoordinate ValuesController::columnWidth(int i) { ContinuousFunction::PlotType plotType = plotTypeAtColumn(&i); if (i == 0) { @@ -62,16 +66,6 @@ int ValuesController::indexFromCumulatedWidth(KDCoordinate offsetX) { return TableViewDataSource::indexFromCumulatedWidth(offsetX); } -Shared::Hideable * ValuesController::hideableCellFromType(HighlightCell * cell, int type) { - if (type == k_notEditableValueCellType) { - Shared::HideableEvenOddBufferTextCell * myCell = static_cast(cell); - return static_cast(myCell); - } - assert(type == k_editableValueCellType); - Shared::StoreCell * myCell = static_cast(cell); - return static_cast(myCell); -} - void ValuesController::willDisplayCellAtLocation(HighlightCell * cell, int i, int j) { // Handle hidden cells int typeAtLoc = typeAtLocation(i,j); @@ -133,12 +127,7 @@ int ValuesController::typeAtLocation(int i, int j) { return Shared::ValuesController::typeAtLocation(i, j); } -I18n::Message ValuesController::emptyMessage() { - if (functionStore()->numberOfDefinedModels() == 0) { - return I18n::Message::NoFunction; - } - return I18n::Message::NoActivatedFunction; -} +// SelectableTableViewDelegate void ValuesController::tableViewDidChangeSelection(SelectableTableView * t, int previousSelectedCellX, int previousSelectedCellY, bool withinTemporarySelection) { if (withinTemporarySelection) { @@ -152,6 +141,43 @@ void ValuesController::tableViewDidChangeSelection(SelectableTableView * t, int } } +// AlternateEmptyViewDelegate + +I18n::Message ValuesController::emptyMessage() { + if (functionStore()->numberOfDefinedModels() == 0) { + return I18n::Message::NoFunction; + } + return I18n::Message::NoActivatedFunction; +} + +// Values controller + +void ValuesController::setStartEndMessages(Shared::IntervalParameterController * controller, int column) { + int c = column+1; + m_intervalParameterSelectorController.setStartEndMessages(controller, plotTypeAtColumn(&c)); +} + +// Number of columns memoization + +void ValuesController::updateNumberOfColumns() const { + for (int plotTypeIndex = 0; plotTypeIndex < ContinuousFunction::k_numberOfPlotTypes; plotTypeIndex++) { + m_numberOfValuesColumnsForType[plotTypeIndex] = 0; + } + for (int i = 0; i < functionStore()->numberOfActiveFunctions(); i++) { + Ion::Storage::Record record = functionStore()->activeRecordAtIndex(i); + ExpiringPointer f = functionStore()->modelForRecord(record); + int plotTypeIndex = static_cast(f->plotType()); + m_numberOfValuesColumnsForType[plotTypeIndex] += numberOfColumnsForRecord(record); + } + m_numberOfColumns = 0; + for (int plotTypeIndex = 0; plotTypeIndex < ContinuousFunction::k_numberOfPlotTypes; plotTypeIndex++) { + // Count abscissa column if the sub table does exist + m_numberOfColumns += numberOfColumnsForPlotType(plotTypeIndex); + } +} + +// Model getters + Ion::Storage::Record ValuesController::recordAtColumn(int i) { bool isDerivative = false; return recordAtColumn(i, &isDerivative); @@ -175,6 +201,12 @@ Ion::Storage::Record ValuesController::recordAtColumn(int i, bool * isDerivative return nullptr; } +Shared::Interval * ValuesController::intervalAtColumn(int columnIndex) { + return App::app()->intervalForType(plotTypeAtColumn(&columnIndex)); +} + +// Number of columns + int ValuesController::numberOfColumnsForRecord(Ion::Storage::Record record) const { ExpiringPointer f = functionStore()->modelForRecord(record); ContinuousFunction::PlotType plotType = f->plotType(); @@ -182,12 +214,21 @@ int ValuesController::numberOfColumnsForRecord(Ion::Storage::Record record) cons (plotType == ContinuousFunction::PlotType::Cartesian && f->displayDerivative()); } -Shared::Interval * ValuesController::intervalAtColumn(int columnIndex) { - return App::app()->intervalForType(plotTypeAtColumn(&columnIndex)); +int ValuesController::numberOfColumnsForPlotType(int plotTypeIndex) const { + return m_numberOfValuesColumnsForType[plotTypeIndex] + (m_numberOfValuesColumnsForType[plotTypeIndex] > 0); // Count abscissa column if there is one } -I18n::Message ValuesController::valuesParameterMessageAtColumn(int columnIndex) const { - return ContinuousFunction::ParameterMessageForPlotType(plotTypeAtColumn(&columnIndex)); +int ValuesController::numberOfAbscissaColumnsBeforeColumn(int column) { + int result = 0; + int plotType = column < 0 ? Shared::ContinuousFunction::k_numberOfPlotTypes : (int)plotTypeAtColumn(&column) + 1; + for (int plotTypeIndex = 0; plotTypeIndex < plotType; plotTypeIndex++) { + result += (m_numberOfValuesColumnsForType[plotTypeIndex] > 0); + } + return result; +} + +int ValuesController::numberOfValuesColumns() { + return m_numberOfColumns - numberOfAbscissaColumnsBeforeColumn(-1); } ContinuousFunction::PlotType ValuesController::plotTypeAtColumn(int * i) const { @@ -199,37 +240,7 @@ ContinuousFunction::PlotType ValuesController::plotTypeAtColumn(int * i) const { return static_cast(plotTypeIndex); } -int ValuesController::maxNumberOfCells() { - return k_maxNumberOfCells; -} - -int ValuesController::maxNumberOfFunctions() { - return k_maxNumberOfFunctions; -} - -Shared::BufferFunctionTitleCell * ValuesController::functionTitleCells(int j) { - assert(j >= 0 && j < k_maxNumberOfFunctions); - return &m_functionTitleCells[j]; -} - -EvenOddBufferTextCell * ValuesController::floatCells(int j) { - assert(j >= 0 && j < k_maxNumberOfCells); - return &m_floatCells[j]; -} - -ViewController * ValuesController::functionParameterController() { - bool isDerivative = false; - Ion::Storage::Record record = recordAtColumn(selectedColumn(), &isDerivative); - if (functionStore()->modelForRecord(record)->plotType() != ContinuousFunction::PlotType::Cartesian) { - return nullptr; - } - if (isDerivative) { - m_derivativeParameterController.setRecord(record); - return &m_derivativeParameterController; - } - m_functionParameterController.setRecord(record); - return &m_functionParameterController; -} +// Function evaluation memoization int ValuesController::valuesColumnForAbsoluteColumn(int column) { return column - numberOfAbscissaColumnsBeforeColumn(column); @@ -282,45 +293,51 @@ void ValuesController::fillMemoizedBuffer(int column, int row, int index) { } } -void ValuesController::setStartEndMessages(Shared::IntervalParameterController * controller, int column) { - int c = column+1; - m_intervalParameterSelectorController.setStartEndMessages(controller, plotTypeAtColumn(&c)); +// Parameter controllers + +ViewController * ValuesController::functionParameterController() { + bool isDerivative = false; + Ion::Storage::Record record = recordAtColumn(selectedColumn(), &isDerivative); + if (functionStore()->modelForRecord(record)->plotType() != ContinuousFunction::PlotType::Cartesian) { + return nullptr; + } + if (isDerivative) { + m_derivativeParameterController.setRecord(record); + return &m_derivativeParameterController; + } + m_functionParameterController.setRecord(record); + return &m_functionParameterController; } -void ValuesController::updateNumberOfColumns() const { - for (int plotTypeIndex = 0; plotTypeIndex < ContinuousFunction::k_numberOfPlotTypes; plotTypeIndex++) { - m_numberOfValuesColumnsForType[plotTypeIndex] = 0; - } - for (int i = 0; i < functionStore()->numberOfActiveFunctions(); i++) { - Ion::Storage::Record record = functionStore()->activeRecordAtIndex(i); - ExpiringPointer f = functionStore()->modelForRecord(record); - int plotTypeIndex = static_cast(f->plotType()); - m_numberOfValuesColumnsForType[plotTypeIndex] += numberOfColumnsForRecord(record); - } - m_numberOfColumns = 0; - for (int plotTypeIndex = 0; plotTypeIndex < ContinuousFunction::k_numberOfPlotTypes; plotTypeIndex++) { - // Count abscissa column if the sub table does exist - m_numberOfColumns += numberOfColumnsForPlotType(plotTypeIndex); - } +I18n::Message ValuesController::valuesParameterMessageAtColumn(int columnIndex) const { + return ContinuousFunction::ParameterMessageForPlotType(plotTypeAtColumn(&columnIndex)); } -int ValuesController::numberOfColumnsForPlotType(int plotTypeIndex) const { - return m_numberOfValuesColumnsForType[plotTypeIndex] + (m_numberOfValuesColumnsForType[plotTypeIndex] > 0); // Count abscissa column if there is one -} +// Cells & View -int ValuesController::numberOfAbscissaColumnsBeforeColumn(int column) { - int result = 0; - int plotType = column < 0 ? Shared::ContinuousFunction::k_numberOfPlotTypes : (int)plotTypeAtColumn(&column) + 1; - for (int plotTypeIndex = 0; plotTypeIndex < plotType; plotTypeIndex++) { - result += (m_numberOfValuesColumnsForType[plotTypeIndex] > 0); +Shared::Hideable * ValuesController::hideableCellFromType(HighlightCell * cell, int type) { + if (type == k_notEditableValueCellType) { + Shared::HideableEvenOddBufferTextCell * myCell = static_cast(cell); + return static_cast(myCell); } - return result; + assert(type == k_editableValueCellType); + Shared::StoreCell * myCell = static_cast(cell); + return static_cast(myCell); } -int ValuesController::numberOfValuesColumns() { - return m_numberOfColumns - numberOfAbscissaColumnsBeforeColumn(-1); +Shared::BufferFunctionTitleCell * ValuesController::functionTitleCells(int j) { + assert(j >= 0 && j < k_maxNumberOfFunctions); + return &m_functionTitleCells[j]; } +EvenOddBufferTextCell * ValuesController::floatCells(int j) { + assert(j >= 0 && j < k_maxNumberOfCells); + return &m_floatCells[j]; +} + + +/* ValuesController::ValuesSelectableTableView */ + int writeMatrixBrakets(char * buffer, const int bufferSize, int type) { /* Write the double brackets required in matrix notation. * - type == 1: "[[" diff --git a/apps/graph/values/values_controller.h b/apps/graph/values/values_controller.h index 2930d8c4b..6b3095995 100644 --- a/apps/graph/values/values_controller.h +++ b/apps/graph/values/values_controller.h @@ -17,29 +17,59 @@ namespace Graph { class ValuesController : public Shared::ValuesController, public SelectableTableViewDelegate { public: ValuesController(Responder * parentResponder, InputEventHandlerDelegate * inputEventHandlerDelegate, ButtonRowController * header); - Button * buttonAtIndex(int index, ButtonRowController::Position position) const override { - return const_cast