From eff0a26835bcff56d4d5802f7ed9f5af2be04792 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Thu, 30 Jan 2020 15:46:54 +0100 Subject: [PATCH] [apps/probability] Clean distribution_controller Cleaner .cpp and fix the number of cells --- apps/probability/distribution_controller.cpp | 31 ++------------------ apps/probability/distribution_controller.h | 21 ++++++++----- 2 files changed, 16 insertions(+), 36 deletions(-) diff --git a/apps/probability/distribution_controller.cpp b/apps/probability/distribution_controller.cpp index b7dff18dc..7c4d679b8 100644 --- a/apps/probability/distribution_controller.cpp +++ b/apps/probability/distribution_controller.cpp @@ -32,18 +32,8 @@ namespace Probability { -DistributionController::ContentView::ContentView(SelectableTableView * selectableTableView) : - m_titleView(KDFont::SmallFont, I18n::Message::ChooseDistribution, 0.5f, 0.5f, Palette::GreyDark, Palette::WallScreen), - m_selectableTableView(selectableTableView) -{ -} - -int DistributionController::ContentView::numberOfSubviews() const { - return 2; -} - View * DistributionController::ContentView::subviewAtIndex(int index) { - assert(index >= 0 && index < 2); + assert(index >= 0 && index < numberOfSubviews()); if (index == 0) { return &m_titleView; } @@ -51,6 +41,7 @@ View * DistributionController::ContentView::subviewAtIndex(int index) { } void DistributionController::ContentView::layoutSubviews(bool force) { + assert(KDFont::SmallFont->glyphSize().height() == 14); // otherwise, k_numberOfCells badly computed KDCoordinate titleHeight = KDFont::SmallFont->glyphSize().height()+k_titleMargin; m_titleView.setFrame(KDRect(0, 0, bounds().width(), titleHeight), force); m_selectableTableView->setFrame(KDRect(0, titleHeight, bounds().width(), bounds().height()-titleHeight), force); @@ -80,10 +71,6 @@ DistributionController::DistributionController(Responder * parentResponder, Dist m_selectableTableView.setTopMargin(Metric::CommonTopMargin-ContentView::k_titleMargin); } -View * DistributionController::view() { - return &m_contentView; -} - void Probability::DistributionController::viewWillAppear() { selectRow((int)m_distribution->type()); } @@ -108,20 +95,12 @@ bool Probability::DistributionController::handleEvent(Ion::Events::Event event) return false; } -int Probability::DistributionController::numberOfRows() const { - return k_totalNumberOfModels; -}; - HighlightCell * Probability::DistributionController::reusableCell(int index) { assert(index >= 0); - assert(index < k_totalNumberOfModels); + assert(index < k_numberOfCells); return &m_cells[index]; } -int Probability::DistributionController::reusableCellCount() const { - return k_totalNumberOfModels; -} - void Probability::DistributionController::willDisplayCellForIndex(HighlightCell * cell, int index) { Cell * myCell = (Cell *)cell; myCell->setLabel(m_messages[index]); @@ -151,10 +130,6 @@ void Probability::DistributionController::willDisplayCellForIndex(HighlightCell myCell->reloadCell(); } -KDCoordinate Probability::DistributionController::cellHeight() { - return Metric::ParameterCellHeight; -} - void Probability::DistributionController::setDistributionAccordingToIndex(int index) { if ((int)m_distribution->type() == index) { return; diff --git a/apps/probability/distribution_controller.h b/apps/probability/distribution_controller.h index 5a1bba6f3..349a3519c 100644 --- a/apps/probability/distribution_controller.h +++ b/apps/probability/distribution_controller.h @@ -12,30 +12,35 @@ namespace Probability { class DistributionController : public ViewController, public SimpleListViewDataSource, public SelectableTableViewDataSource { public: DistributionController(Responder * parentResponder, Distribution * m_distribution, ParametersController * parametersController); - View * view() override; + View * view() override { return &m_contentView; } bool handleEvent(Ion::Events::Event event) override; void didBecomeFirstResponder() override; void viewWillAppear() override; - int numberOfRows() const override; + int numberOfRows() const override { return k_totalNumberOfModels; } void willDisplayCellForIndex(HighlightCell * cell, int index) override; - KDCoordinate cellHeight() override; + KDCoordinate cellHeight() override { return k_cellHeight; } HighlightCell * reusableCell(int index) override; - int reusableCellCount() const override; + int reusableCellCount() const override { return k_numberOfCells; } private: class ContentView : public View { public: - ContentView(SelectableTableView * selectableTableView); + ContentView(SelectableTableView * selectableTableView) : + m_titleView(KDFont::SmallFont, I18n::Message::ChooseDistribution, 0.5f, 0.5f, Palette::GreyDark, Palette::WallScreen), + m_selectableTableView(selectableTableView) + {} constexpr static KDCoordinate k_titleMargin = 8; private: - int numberOfSubviews() const override; + int numberOfSubviews() const override { return 2; } View * subviewAtIndex(int index) override; void layoutSubviews(bool force = false) override; - MessageTextView m_titleView;; + MessageTextView m_titleView; SelectableTableView * m_selectableTableView; }; void setDistributionAccordingToIndex(int index); + constexpr static KDCoordinate k_cellHeight = Metric::ParameterCellHeight; constexpr static int k_totalNumberOfModels = 9; - Cell m_cells[k_totalNumberOfModels]; // TODO LEA replace with number of visible cells + constexpr static int k_numberOfCells = (Ion::Display::Height - Metric::TitleBarHeight - 14 - ContentView::k_titleMargin) /k_cellHeight + 1 + 1; // 14 for the small font height, + 1 to get the upper rounding and + 1 for half-displayed rows + Cell m_cells[k_numberOfCells]; SelectableTableView m_selectableTableView; ContentView m_contentView; I18n::Message * m_messages;