From 939a2eacb5ec5cee085aaf0c280006bc4b73f97a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Fri, 23 Dec 2016 15:06:26 +0100 Subject: [PATCH] [apps/statistics] Improve the calculation page based on the data model Change-Id: I5b49d3aee0cae503e6bb35079c98d37c943b2458 --- apps/statistics/calculation_controller.cpp | 130 ++++++++++++++++++++- apps/statistics/calculation_controller.h | 25 +++- 2 files changed, 151 insertions(+), 4 deletions(-) diff --git a/apps/statistics/calculation_controller.cpp b/apps/statistics/calculation_controller.cpp index 82b27884d..ab681e656 100644 --- a/apps/statistics/calculation_controller.cpp +++ b/apps/statistics/calculation_controller.cpp @@ -1,12 +1,18 @@ #include "calculation_controller.h" +#include "../constant.h" +#include +#include namespace Statistics { CalculationController::CalculationController(Responder * parentResponder, Data * data) : ViewController(parentResponder), - m_view(SolidColorView(KDColorGreen)), + m_selectableTableView(SelectableTableView(this, this, Metric::TopMargin, Metric::RightMargin, Metric::BottomMargin, Metric::LeftMargin)), m_data(data) { + for (int k = 0; k < k_maxNumberOfDisplayableRows; k++) { + m_titleCells[k].setAlignment(1.0f, 0.5f); + } } const char * CalculationController::title() const { @@ -14,17 +20,27 @@ const char * CalculationController::title() const { } View * CalculationController::view() { - return &m_view; + return &m_selectableTableView; } bool CalculationController::handleEvent(Ion::Events::Event event) { if (event == Ion::Events::Up) { + m_selectableTableView.deselectTable(); app()->setFirstResponder(tabController()); return true; } return false; } +void CalculationController::didBecomeFirstResponder() { + if (m_selectableTableView.selectedRow() == -1) { + m_selectableTableView.selectCellAtLocation(0, 0); + } else { + m_selectableTableView.selectCellAtLocation(m_selectableTableView.selectedColumn(), m_selectableTableView.selectedRow()); + } + app()->setFirstResponder(&m_selectableTableView); +} + bool CalculationController::isEmpty() { if (m_data->totalSize() == 0) { return true; @@ -40,6 +56,116 @@ Responder * CalculationController::defaultController() { return tabController(); } +int CalculationController::numberOfRows() { + return k_totalNumberOfRows; +} + +int CalculationController::numberOfColumns() { + return 2; +} + +void CalculationController::willDisplayCellAtLocation(TableViewCell * cell, int i, int j) { + EvenOddCell * myCell = (EvenOddCell *)cell; + myCell->setEven(j%2 == 0); + myCell->setHighlighted(i == m_selectableTableView.selectedColumn() && j == m_selectableTableView.selectedRow()); + if (i == 0) { + const char * titles[k_totalNumberOfRows] = {"Effectif total", "Minimum", "Maximum", "Etendu", + "Moyenne", "Ecart-type", "Variance", "Q1", "Q3", "Mediane", "Ecart interquartile", "Somme", + "Somme des carres"}; + EvenOddPointerTextCell * myCell = (EvenOddPointerTextCell *)cell; + myCell->setText(titles[j]); + } else { + float calculation = 0.0f; + switch (j) { + case 0: + calculation = m_data->totalSize(); + break; + case 1: + calculation = m_data->minValue(); + break; + case 2: + calculation = m_data->maxValue(); + break; + case 3: + calculation = m_data->range(); + break; + case 4: + calculation = m_data->mean(); + break; + case 5: + calculation = m_data->standardDeviation(); + break; + case 6: + calculation = m_data->variance(); + break; + case 7: + calculation = m_data->firstQuartile(); + break; + case 8: + calculation = m_data->thirdQuartile(); + break; + case 9: + calculation = m_data->median(); + break; + case 10: + calculation = m_data->quartileRange(); + break; + case 11: + calculation = m_data->sum(); + break; + case 12: + calculation = m_data->squaredValueSum(); + break; + default: + break; + } + EvenOddBufferTextCell * myCell = (EvenOddBufferTextCell *)cell; + char buffer[Constant::FloatBufferSizeInScientificMode]; + Float(calculation).convertFloatToText(buffer, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaInScientificMode); + myCell->setText(buffer); + } +} + +KDCoordinate CalculationController::columnWidth(int i) { + return k_cellWidth; +} + +KDCoordinate CalculationController::rowHeight(int j) { + return k_cellHeight; +} + +KDCoordinate CalculationController::cumulatedWidthFromIndex(int i) { + return i*k_cellWidth; +} + +KDCoordinate CalculationController::cumulatedHeightFromIndex(int j) { + return j*k_cellHeight; +} + +int CalculationController::indexFromCumulatedWidth(KDCoordinate offsetX) { + return (offsetX-1) / k_cellWidth; +} + +int CalculationController::indexFromCumulatedHeight(KDCoordinate offsetY) { + return (offsetY-1) / k_cellHeight; +} + +TableViewCell * CalculationController::reusableCell(int index, int type) { + assert(index < k_totalNumberOfRows); + if (type == 0) { + return &m_titleCells[index]; + } + return &m_calculationCells[index]; +} + +int CalculationController::reusableCellCount(int type) { + return k_maxNumberOfDisplayableRows; +} + +int CalculationController::typeAtLocation(int i, int j) { + return i; +} + Responder * CalculationController::tabController() const { return (parentResponder()->parentResponder()->parentResponder()); } diff --git a/apps/statistics/calculation_controller.h b/apps/statistics/calculation_controller.h index d958c2563..ebec37159 100644 --- a/apps/statistics/calculation_controller.h +++ b/apps/statistics/calculation_controller.h @@ -6,19 +6,40 @@ namespace Statistics { -class CalculationController : public ViewController, public AlternateEmptyViewDelegate { +class CalculationController : public ViewController, public AlternateEmptyViewDelegate, public TableViewDataSource { public: CalculationController(Responder * parentResponder, Data * data); const char * title() const override; View * view() override; bool handleEvent(Ion::Events::Event event) override; + void didBecomeFirstResponder() override; + bool isEmpty() override; const char * emptyMessage() override; Responder * defaultController() override; + + int numberOfRows() override; + int numberOfColumns() override; + void willDisplayCellAtLocation(TableViewCell * cell, int i, int j) override; + KDCoordinate columnWidth(int i) override; + KDCoordinate rowHeight(int j) override; + KDCoordinate cumulatedWidthFromIndex(int i) override; + KDCoordinate cumulatedHeightFromIndex(int j) override; + int indexFromCumulatedWidth(KDCoordinate offsetX) override; + int indexFromCumulatedHeight(KDCoordinate offsetY) override; + TableViewCell * reusableCell(int index, int type) override; + int reusableCellCount(int type) override; + int typeAtLocation(int i, int j) override; private: Responder * tabController() const; - SolidColorView m_view; + constexpr static int k_totalNumberOfRows = 13; + constexpr static int k_maxNumberOfDisplayableRows = 10; + static constexpr KDCoordinate k_cellHeight = 25; + static constexpr KDCoordinate k_cellWidth = 320/2 - Metric::RightMargin; + EvenOddPointerTextCell m_titleCells[k_maxNumberOfDisplayableRows]; + EvenOddBufferTextCell m_calculationCells[k_maxNumberOfDisplayableRows]; + SelectableTableView m_selectableTableView; Data * m_data; };