From 7e7a37bf8e4315fb8c596f52f72106ded3cc5aab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Mon, 27 Jan 2020 11:36:22 +0100 Subject: [PATCH] [apps/solver/solutions_controller] Code cleaning --- apps/solver/solutions_controller.cpp | 73 +++++++++++++--------------- apps/solver/solutions_controller.h | 31 +++++++++--- 2 files changed, 59 insertions(+), 45 deletions(-) diff --git a/apps/solver/solutions_controller.cpp b/apps/solver/solutions_controller.cpp index c81552f04..628a45efe 100644 --- a/apps/solver/solutions_controller.cpp +++ b/apps/solver/solutions_controller.cpp @@ -34,9 +34,11 @@ void SolutionsController::ContentView::drawRect(KDContext * ctx, KDRect rect) co } void SolutionsController::ContentView::setWarning(bool warning) { - m_displayWarningMoreSolutions = warning; - m_selectableTableView.setTopMargin(m_displayWarningMoreSolutions ? 0 : Metric::CommonTopMargin); - layoutSubviews(); + if (m_displayWarningMoreSolutions != warning) { + m_displayWarningMoreSolutions = warning; + m_selectableTableView.setTopMargin(m_displayWarningMoreSolutions ? 0 : Metric::CommonTopMargin); + layoutSubviews(); + } } void SolutionsController::ContentView::setWarningMessages(I18n::Message message0, I18n::Message message1) { @@ -45,16 +47,18 @@ void SolutionsController::ContentView::setWarningMessages(I18n::Message message0 } int SolutionsController::ContentView::numberOfSubviews() const { - return 1+2*m_displayWarningMoreSolutions; + return 1 + 2*m_displayWarningMoreSolutions; } View * SolutionsController::ContentView::subviewAtIndex(int index) { - assert(index >= 0 && index < 1+2*m_displayWarningMoreSolutions); - if (index == 0 && m_displayWarningMoreSolutions) { - return &m_warningMessageView0; - } - if (index == 1 && m_displayWarningMoreSolutions) { - return &m_warningMessageView1; + assert(index >= 0 && index < numberOfSubviews()); + if (m_displayWarningMoreSolutions) { + if (index == 0) { + return &m_warningMessageView0; + } + if (index == 1) { + return &m_warningMessageView1; + } } return &m_selectableTableView; } @@ -81,13 +85,13 @@ SolutionsController::SolutionsController(Responder * parentResponder, EquationSt m_delta2Layout = HorizontalLayout::Builder(VerticalOffsetLayout::Builder(CodePointLayout::Builder('2', KDFont::SmallFont), VerticalOffsetLayoutNode::Position::Superscript), LayoutHelper::String("-4ac", 4, KDFont::SmallFont)); const char * deltaB = "Δ=b"; static_cast(m_delta2Layout).addOrMergeChildAtIndex(LayoutHelper::String(deltaB, strlen(deltaB), KDFont::SmallFont), 0, false); - for (int i = 0; i < EquationStore::k_maxNumberOfExactSolutions; i++) { + for (int i = 0; i < k_numberOfExactValueCells; i++) { m_exactValueCells[i].setParentResponder(m_contentView.selectableTableView()); } - for (int i = 0; i < EquationStore::k_maxNumberOfApproximateSolutions; i++) { + for (int i = 0; i < k_numberOfApproximateValueCells; i++) { m_approximateValueCells[i].setFont(KDFont::LargeFont); } - for (int i = 0; i < EquationStore::k_maxNumberOfSolutions; i++) { + for (int i = 0; i < k_numberOfSymbolCells; i++) { m_symbolCells[i].setAlignment(0.5f, 0.5f); } } @@ -100,10 +104,6 @@ const char * SolutionsController::title() { return I18n::translate(I18n::Message::Solution); } -View * SolutionsController::view() { - return &m_contentView; -} - void SolutionsController::viewWillAppear() { ViewController::viewWillAppear(); bool requireWarning = false; @@ -161,10 +161,6 @@ int SolutionsController::numberOfRows() const { return m_equationStore->numberOfSolutions(); } -int SolutionsController::numberOfColumns() const { - return 2; -} - void SolutionsController::willDisplayCellAtLocation(HighlightCell * cell, int i, int j) { if (i == 0) { // Name of the variable or discriminant @@ -215,10 +211,7 @@ void SolutionsController::willDisplayCellAtLocation(HighlightCell * cell, int i, } KDCoordinate SolutionsController::columnWidth(int i) { - if (i == 0) { - return k_symbolCellWidth; - } - return k_valueCellWidth; + return i == 0 ? k_symbolCellWidth : k_valueCellWidth; } KDCoordinate SolutionsController::rowHeight(int j) { @@ -260,25 +253,29 @@ int SolutionsController::indexFromCumulatedWidth(KDCoordinate offsetX) { } HighlightCell * SolutionsController::reusableCell(int index, int type) { - if (type == 0) { - return &m_symbolCells[index]; - } else if (type == 1) { - return &m_deltaCell; - } else if (type == 2) { - return &m_exactValueCells[index]; + switch (type) { + case k_symbolCellType: + return &m_symbolCells[index]; + case k_deltaCellType: + return &m_deltaCell; + case k_exactValueCellType: + return &m_exactValueCells[index]; + default: + assert(type == k_approximateValueCellType); + return &m_approximateValueCells[index]; } - return &m_approximateValueCells[index]; } int SolutionsController::reusableCellCount(int type) { switch (type) { - case 0: + case k_symbolCellType: return EquationStore::k_maxNumberOfSolutions; - case 1: + case k_deltaCellType: return 1; - case 2: + case k_exactValueCellType: return EquationStore::k_maxNumberOfExactSolutions; default: + assert(type == k_approximateValueCellType); return EquationStore::k_maxNumberOfApproximateSolutions; } } @@ -286,11 +283,11 @@ int SolutionsController::reusableCellCount(int type) { int SolutionsController::typeAtLocation(int i, int j) { if (i == 0) { if (m_equationStore->type() == EquationStore::Type::PolynomialMonovariable && j == m_equationStore->numberOfSolutions()-1) { - return 1; + return k_deltaCellType; } - return 0; + return k_symbolCellType; } - return m_equationStore->type() == EquationStore::Type::Monovariable ? 3 : 2; + return m_equationStore->type() == EquationStore::Type::Monovariable ? k_approximateValueCellType : k_exactValueCellType; } void SolutionsController::didBecomeFirstResponder() { diff --git a/apps/solver/solutions_controller.h b/apps/solver/solutions_controller.h index ab8aab182..f2b420f67 100644 --- a/apps/solver/solutions_controller.h +++ b/apps/solver/solutions_controller.h @@ -15,7 +15,7 @@ public: bool shouldReplaceFuncionsButNotSymbols() const { return m_shouldReplaceFuncionsButNotSymbols; } /* ViewController */ const char * title() override; - View * view() override; + View * view() override { return &m_contentView; } void viewWillAppear() override; void didEnterResponderChain(Responder * previousFirstResponder) override; /* AlternateEmptyViewDefaultDelegate */ @@ -24,7 +24,7 @@ public: virtual Responder * defaultController() override; /* TableViewDataSource */ int numberOfRows() const override; - int numberOfColumns() const override; + int numberOfColumns() const override { return 2; } void willDisplayCellAtLocation(HighlightCell * cell, int i, int j) override; KDCoordinate columnWidth(int i) override; KDCoordinate rowHeight(int j) override; @@ -39,6 +39,7 @@ public: private: class ContentView : public View { public: + constexpr static KDCoordinate k_topMargin = 50; ContentView(SolutionsController * controller); void drawRect(KDContext * ctx, KDRect rect) const override; void setWarning(bool warning); @@ -47,7 +48,7 @@ private: return &m_selectableTableView; } private: - constexpr static KDCoordinate k_topMargin = 50; + constexpr static KDCoordinate k_middleMargin = 50; int numberOfSubviews() const override; View * subviewAtIndex(int index) override; void layoutSubviews(bool force = false) override; @@ -56,15 +57,31 @@ private: SelectableTableView m_selectableTableView; bool m_displayWarningMoreSolutions; }; + + // Cell types + constexpr static int k_symbolCellType = 0; + constexpr static int k_deltaCellType = 1; + constexpr static int k_exactValueCellType = 2; + constexpr static int k_approximateValueCellType = 3; + + // Heights and widths + constexpr static KDCoordinate k_defaultCellHeight = 20; constexpr static int k_symbolCellWidth = 90; constexpr static int k_valueCellWidth = 190; - constexpr static KDCoordinate k_defaultCellHeight = 20; + + // Number of cells + constexpr static int k_maxNumberOfVisibleCells = (Ion::Display::Height - 3 * Meric::TitleBarHeight - ContentView::k_topMargin) / k_defaultCellHeight + 1; + static_assert(k_maxNumberOfVisibleCells <= EquationStore::k_maxNumberOfSolutions + Poincare::Expression::k_maxNumberOfVariables, "We can reduce the number of cells in Solver:SolutionsController."); + constexpr static int k_numberOfSymbolCells = k_maxNumberOfVisibleCells < EquationStore::k_maxNumberOfSolutions ? k_maxNumberOfVisibleCells : EquationStore::k_maxNumberOfSolutions; + constexpr static int k_numberOfExactValueCells = k_maxNumberOfVisibleCells < EquationStore::k_maxNumberOfExactSolutions ? k_maxNumberOfVisibleCells : EquationStore::k_maxNumberOfExactSolutions; + constexpr static int k_numberOfApproximateValueCells = k_maxNumberOfVisibleCells < EquationStore::k_maxNumberOfApproximateSolutions ? k_maxNumberOfVisibleCells : EquationStore::k_maxNumberOfApproximateSolutions; + EquationStore * m_equationStore; - EvenOddBufferTextCell m_symbolCells[EquationStore::k_maxNumberOfSolutions]; + EvenOddBufferTextCell m_symbolCells[k_numberOfSymbolCells]; EvenOddExpressionCell m_deltaCell; Poincare::Layout m_delta2Layout; - Shared::ScrollableTwoExpressionsCell m_exactValueCells[EquationStore::k_maxNumberOfExactSolutions]; - EvenOddBufferTextCell m_approximateValueCells[EquationStore::k_maxNumberOfApproximateSolutions]; + Shared::ScrollableTwoExpressionsCell m_exactValueCells[k_numberOfExactValueCells]; + EvenOddBufferTextCell m_approximateValueCells[k_numberOfApproximateValueCells]; ContentView m_contentView; bool m_shouldReplaceFuncionsButNotSymbols; };