From b4b2e284e1ca9cbe90bbc9ba0a6b297bc8312cd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Wed, 8 Mar 2017 11:18:55 +0100 Subject: [PATCH] [apps/shared] Factorize tab table controller in a class Change-Id: I0af0da6b9a1216c57c8a4b669d14d8a3473ac48b --- apps/regression/calculation_controller.cpp | 37 ++------------ apps/regression/calculation_controller.h | 13 ++--- apps/shared/Makefile | 1 + .../editable_cell_table_view_controller.cpp | 26 ++-------- .../editable_cell_table_view_controller.h | 10 +--- apps/shared/tab_table_controller.cpp | 49 +++++++++++++++++++ apps/shared/tab_table_controller.h | 29 +++++++++++ apps/statistics/calculation_controller.cpp | 37 ++------------ apps/statistics/calculation_controller.h | 14 ++---- 9 files changed, 97 insertions(+), 119 deletions(-) create mode 100644 apps/shared/tab_table_controller.cpp create mode 100644 apps/shared/tab_table_controller.h diff --git a/apps/regression/calculation_controller.cpp b/apps/regression/calculation_controller.cpp index a6d457508..e18578df8 100644 --- a/apps/regression/calculation_controller.cpp +++ b/apps/regression/calculation_controller.cpp @@ -7,19 +7,18 @@ #include using namespace Poincare; +using namespace Shared; namespace Regression { CalculationController::CalculationController(Responder * parentResponder, ButtonRowController * header, Store * store) : - ViewController(parentResponder), + TabTableController(parentResponder, Metric::CommonTopMargin, Metric::CommonRightMargin, Metric::CommonBottomMargin, Metric::CommonLeftMargin, this, true), ButtonRowDelegate(header, nullptr), m_titleCells{EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small)}, m_r2TitleCell(1.0f, 0.5f), m_columnTitleCell(EvenOddDoubleBufferTextCell(&m_selectableTableView)), m_calculationCells{EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small)}, - m_selectableTableView(SelectableTableView(this, this, Metric::CommonTopMargin, Metric::CommonRightMargin, - Metric::CommonBottomMargin, Metric::CommonLeftMargin, this, true, true, Palette::WallScreenDark)), m_store(store) { for (int k = 0; k < k_maxNumberOfDisplayableRows/2; k++) { @@ -41,10 +40,6 @@ const char * CalculationController::title() const { return "Statistiques"; } -View * CalculationController::view() { - return &m_selectableTableView; -} - bool CalculationController::handleEvent(Ion::Events::Event event) { if (event == Ion::Events::Up) { m_selectableTableView.deselectTable(); @@ -60,7 +55,7 @@ void CalculationController::didBecomeFirstResponder() { } else { m_selectableTableView.selectCellAtLocation(m_selectableTableView.selectedColumn(), m_selectableTableView.selectedRow()); } - app()->setFirstResponder(&m_selectableTableView); + TabTableController::didBecomeFirstResponder(); } void CalculationController::tableViewDidChangeSelection(SelectableTableView * t, int previousSelectedCellX, int previousSelectedCellY) { @@ -181,22 +176,6 @@ 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; -} - HighlightCell * CalculationController::reusableCell(int index, int type) { if (type == 0) { assert(index < k_maxNumberOfDisplayableRows); @@ -250,16 +229,6 @@ int CalculationController::typeAtLocation(int i, int j) { return 4; } -void CalculationController::viewWillAppear() { - m_selectableTableView.reloadData(); -} - -void CalculationController::willExitResponderChain(Responder * nextFirstResponder) { - if (nextFirstResponder == tabController()) { - m_selectableTableView.deselectTable(); - } -} - Responder * CalculationController::tabController() const { return (parentResponder()->parentResponder()->parentResponder()); } diff --git a/apps/regression/calculation_controller.h b/apps/regression/calculation_controller.h index 477ffa3cf..d98f1fa88 100644 --- a/apps/regression/calculation_controller.h +++ b/apps/regression/calculation_controller.h @@ -4,16 +4,16 @@ #include #include "store.h" #include "even_odd_double_buffer_text_cell.h" +#include "../shared/tab_table_controller.h" namespace Regression { -class CalculationController : public ViewController, public ButtonRowDelegate, public AlternateEmptyViewDelegate, public TableViewDataSource, public SelectableTableViewDelegate { +class CalculationController : public Shared::TabTableController, public ButtonRowDelegate, public AlternateEmptyViewDelegate, public SelectableTableViewDelegate { public: CalculationController(Responder * parentResponder, ButtonRowController * header, Store * store); ~CalculationController(); const char * title() const override; - View * view() override; bool handleEvent(Ion::Events::Event event) override; void didBecomeFirstResponder() override; void tableViewDidChangeSelection(SelectableTableView * t, int previousSelectedCellX, int previousSelectedCellY) override; @@ -27,17 +27,11 @@ public: void willDisplayCellAtLocation(HighlightCell * 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; HighlightCell * reusableCell(int index, int type) override; int reusableCellCount(int type) override; int typeAtLocation(int i, int j) override; - void viewWillAppear() override; - void willExitResponderChain(Responder * nextFirstResponder) override; private: - Responder * tabController() const; + Responder * tabController() const override; constexpr static int k_totalNumberOfRows = 11; constexpr static int k_totalNumberOfColumns = 2; constexpr static int k_maxNumberOfDisplayableRows = 10; @@ -49,7 +43,6 @@ private: EvenOddDoubleBufferTextCell m_columnTitleCell; EvenOddDoubleBufferTextCell m_doubleCalculationCells[k_maxNumberOfDisplayableRows/2]; EvenOddBufferTextCell m_calculationCells[k_maxNumberOfDisplayableRows/2]; - SelectableTableView m_selectableTableView; Store * m_store; }; diff --git a/apps/shared/Makefile b/apps/shared/Makefile index 69ff00d24..34ad1cc5b 100644 --- a/apps/shared/Makefile +++ b/apps/shared/Makefile @@ -28,6 +28,7 @@ app_objs += $(addprefix apps/shared/,\ range_parameter_controller.o\ store_controller.o\ store_parameter_controller.o\ + tab_table_controller.o\ text_field_delegate.o\ text_field_delegate_app.o\ values_function_parameter_controller.o\ diff --git a/apps/shared/editable_cell_table_view_controller.cpp b/apps/shared/editable_cell_table_view_controller.cpp index 78d25f39e..417f88755 100644 --- a/apps/shared/editable_cell_table_view_controller.cpp +++ b/apps/shared/editable_cell_table_view_controller.cpp @@ -10,16 +10,10 @@ namespace Shared { EditableCellTableViewController::EditableCellTableViewController(Responder * parentResponder, KDCoordinate topMargin, KDCoordinate rightMargin, KDCoordinate bottomMargin, KDCoordinate leftMargin) : - ViewController(parentResponder), - m_selectableTableView(SelectableTableView(this, this, topMargin, rightMargin, bottomMargin, leftMargin, this, - false, true, Palette::WallScreenDark)) + TabTableController(parentResponder, topMargin, rightMargin, bottomMargin, leftMargin, this, true) { } -View * EditableCellTableViewController::view() { - return &m_selectableTableView; -} - bool EditableCellTableViewController::textFieldDidFinishEditing(TextField * textField, const char * text) { AppsContainer * appsContainer = ((TextFieldDelegateApp *)app())->container(); Context * globalContext = appsContainer->globalContext(); @@ -64,14 +58,6 @@ KDCoordinate EditableCellTableViewController::rowHeight(int j) { return k_cellHeight; } -KDCoordinate EditableCellTableViewController::cumulatedHeightFromIndex(int j) { - return j*k_cellHeight; -} - -int EditableCellTableViewController::indexFromCumulatedHeight(KDCoordinate offsetY) { - return (offsetY-1) / k_cellHeight; -} - void EditableCellTableViewController::willDisplayCellAtLocationWithDisplayMode(HighlightCell * cell, int i, int j, Expression::FloatDisplayMode floatDisplayMode) { EvenOddCell * myCell = (EvenOddCell *)cell; myCell->setEven(j%2 == 0); @@ -104,12 +90,12 @@ void EditableCellTableViewController::didBecomeFirstResponder() { int selectedColumn = m_selectableTableView.selectedColumn(); selectedColumn = selectedColumn >= numberOfColumns() ? numberOfColumns() - 1 : selectedColumn; m_selectableTableView.selectCellAtLocation(selectedColumn, selectedRow); - app()->setFirstResponder(&m_selectableTableView); + TabTableController::didBecomeFirstResponder(); } } void EditableCellTableViewController::viewWillAppear() { - m_selectableTableView.reloadData(); + TabTableController::viewWillAppear(); if (m_selectableTableView.selectedRow() == -1) { m_selectableTableView.selectCellAtLocation(0, 1); } else { @@ -121,12 +107,6 @@ void EditableCellTableViewController::viewWillAppear() { } } -void EditableCellTableViewController::willExitResponderChain(Responder * nextFirstResponder) { - if (nextFirstResponder == tabController()) { - m_selectableTableView.deselectTable(); - } -} - TextFieldDelegateApp * EditableCellTableViewController::textFieldDelegateApp() { return (TextFieldDelegateApp *)app(); } diff --git a/apps/shared/editable_cell_table_view_controller.h b/apps/shared/editable_cell_table_view_controller.h index 954ffbff7..80dbcd275 100644 --- a/apps/shared/editable_cell_table_view_controller.h +++ b/apps/shared/editable_cell_table_view_controller.h @@ -4,14 +4,14 @@ #include #include #include "text_field_delegate.h" +#include "tab_table_controller.h" namespace Shared { -class EditableCellTableViewController : public ViewController, public TableViewDataSource, public SelectableTableViewDelegate, public TextFieldDelegate { +class EditableCellTableViewController : public TabTableController , public SelectableTableViewDelegate, public TextFieldDelegate { public: EditableCellTableViewController(Responder * parentResponder, KDCoordinate topMargin, KDCoordinate rightMargin, KDCoordinate bottomMargin, KDCoordinate leftMargin); - virtual View * view() override; bool textFieldDidFinishEditing(TextField * textField, const char * text) override; void tableViewDidChangeSelection(SelectableTableView * t, int previousSelectedCellX, int previousSelectedCellY) override; @@ -19,14 +19,9 @@ public: int numberOfRows() override; void willDisplayCellAtLocationWithDisplayMode(HighlightCell * cell, int i, int j, Poincare::Expression::FloatDisplayMode FloatDisplayMode); KDCoordinate rowHeight(int j) override; - KDCoordinate cumulatedHeightFromIndex(int j) override; - int indexFromCumulatedHeight(KDCoordinate offsetY) override; void didBecomeFirstResponder() override; void viewWillAppear() override; - void willExitResponderChain(Responder * nextFirstResponder) override; -protected: - SelectableTableView m_selectableTableView; private: TextFieldDelegateApp * textFieldDelegateApp() override; static constexpr KDCoordinate k_cellHeight = 20; @@ -35,7 +30,6 @@ private: virtual float dataAtLocation(int columnIndex, int rowIndex) = 0; virtual int numberOfElements() = 0; virtual int maxNumberOfElements() const = 0; - virtual Responder * tabController() const = 0; }; } diff --git a/apps/shared/tab_table_controller.cpp b/apps/shared/tab_table_controller.cpp new file mode 100644 index 000000000..20632a3b1 --- /dev/null +++ b/apps/shared/tab_table_controller.cpp @@ -0,0 +1,49 @@ +#include "tab_table_controller.h" + +namespace Shared { + +TabTableController::TabTableController(Responder * parentResponder, KDCoordinate topMargin, + KDCoordinate rightMargin, KDCoordinate bottomMargin, KDCoordinate leftMargin, SelectableTableViewDelegate * delegate, bool showIndicators) : + ViewController(parentResponder), + m_selectableTableView(SelectableTableView(this, this, topMargin, rightMargin, bottomMargin, leftMargin, + delegate, showIndicators, true, Palette::WallScreenDark)) +{ +} + +View * TabTableController::view() { + return &m_selectableTableView; +} + +void TabTableController::didBecomeFirstResponder() { + app()->setFirstResponder(&m_selectableTableView); +} + +KDCoordinate TabTableController::cumulatedWidthFromIndex(int i) { + return i*columnWidth(0); +} + +KDCoordinate TabTableController::cumulatedHeightFromIndex(int j) { + return j*rowHeight(0); +} + +int TabTableController::indexFromCumulatedWidth(KDCoordinate offsetX) { + return (offsetX-1) / columnWidth(0); +} + +int TabTableController::indexFromCumulatedHeight(KDCoordinate offsetY) { + return (offsetY-1) / rowHeight(0); +} + +void TabTableController::viewWillAppear() { + m_selectableTableView.reloadData(); +} + +void TabTableController::willExitResponderChain(Responder * nextFirstResponder) { + if (nextFirstResponder == tabController()) { + m_selectableTableView.deselectTable(); + m_selectableTableView.scrollToCell(0,0); + } +} + +} + diff --git a/apps/shared/tab_table_controller.h b/apps/shared/tab_table_controller.h new file mode 100644 index 000000000..3aa531b49 --- /dev/null +++ b/apps/shared/tab_table_controller.h @@ -0,0 +1,29 @@ +#ifndef SHARED_TAB_TABLE_CONTROLLER_H +#define SHARED_TAB_TABLE_CONTROLLER_H + +#include + +namespace Shared { + +class TabTableController : public ViewController, public TableViewDataSource { +public: + TabTableController(Responder * parentResponder, KDCoordinate topMargin, + KDCoordinate rightMargin, KDCoordinate bottomMargin, KDCoordinate leftMargin, SelectableTableViewDelegate * delegate, bool showIndicators); + virtual View * view() override; + + KDCoordinate cumulatedHeightFromIndex(int j) override; + int indexFromCumulatedHeight(KDCoordinate offsetY) override; + KDCoordinate cumulatedWidthFromIndex(int i) override; + int indexFromCumulatedWidth(KDCoordinate offsetX) override; + void didBecomeFirstResponder() override; + void viewWillAppear() override; + void willExitResponderChain(Responder * nextFirstResponder) override; +protected: + SelectableTableView m_selectableTableView; + virtual Responder * tabController() const = 0; +}; + +} + +#endif + diff --git a/apps/statistics/calculation_controller.cpp b/apps/statistics/calculation_controller.cpp index 38051d539..7b8fd715f 100644 --- a/apps/statistics/calculation_controller.cpp +++ b/apps/statistics/calculation_controller.cpp @@ -5,19 +5,18 @@ #include #include +using namespace Shared; using namespace Poincare; namespace Statistics { CalculationController::CalculationController(Responder * parentResponder, ButtonRowController * header, Store * store) : - ViewController(parentResponder), + TabTableController(parentResponder, Metric::CommonTopMargin, Metric::CommonRightMargin, Metric::CommonBottomMargin, Metric::CommonLeftMargin, nullptr, true), ButtonRowDelegate(header, nullptr), m_titleCells{EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small), EvenOddPointerTextCell(KDText::FontSize::Small)}, m_calculationCells{EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small), EvenOddBufferTextCell(KDText::FontSize::Small)}, - m_selectableTableView(SelectableTableView(this, this, Metric::CommonTopMargin, Metric::CommonRightMargin, Metric::CommonBottomMargin, Metric::CommonLeftMargin, - nullptr, true, true, Palette::WallScreenDark)), m_store(store) { for (int k = 0; k < k_maxNumberOfDisplayableRows; k++) { @@ -30,10 +29,6 @@ const char * CalculationController::title() const { return "Stats"; } -View * CalculationController::view() { - return &m_selectableTableView; -} - bool CalculationController::handleEvent(Ion::Events::Event event) { if (event == Ion::Events::Up) { m_selectableTableView.deselectTable(); @@ -49,7 +44,7 @@ void CalculationController::didBecomeFirstResponder() { } else { m_selectableTableView.selectCellAtLocation(m_selectableTableView.selectedColumn(), m_selectableTableView.selectedRow()); } - app()->setFirstResponder(&m_selectableTableView); + TabTableController::didBecomeFirstResponder(); } bool CalculationController::isEmpty() const { @@ -105,22 +100,6 @@ 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; -} - HighlightCell * CalculationController::reusableCell(int index, int type) { assert(index < k_totalNumberOfRows); if (type == 0) { @@ -137,16 +116,6 @@ int CalculationController::typeAtLocation(int i, int j) { return i; } -void CalculationController::viewWillAppear() { - m_selectableTableView.reloadData(); -} - -void CalculationController::willExitResponderChain(Responder * nextFirstResponder) { - if (nextFirstResponder == tabController()) { - m_selectableTableView.deselectTable(); - } -} - Responder * CalculationController::tabController() const { return (parentResponder()->parentResponder()->parentResponder()); } diff --git a/apps/statistics/calculation_controller.h b/apps/statistics/calculation_controller.h index ac58513c4..62f667cb7 100644 --- a/apps/statistics/calculation_controller.h +++ b/apps/statistics/calculation_controller.h @@ -3,15 +3,16 @@ #include #include "store.h" +#include "../shared/tab_table_controller.h" + namespace Statistics { -class CalculationController : public ViewController, public ButtonRowDelegate, public AlternateEmptyViewDelegate, public TableViewDataSource { +class CalculationController : public Shared::TabTableController, public ButtonRowDelegate, public AlternateEmptyViewDelegate { public: CalculationController(Responder * parentResponder, ButtonRowController * header, Store * store); const char * title() const override; - View * view() override; bool handleEvent(Ion::Events::Event event) override; void didBecomeFirstResponder() override; @@ -24,24 +25,17 @@ public: void willDisplayCellAtLocation(HighlightCell * 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; HighlightCell * reusableCell(int index, int type) override; int reusableCellCount(int type) override; int typeAtLocation(int i, int j) override; - void viewWillAppear() override; - void willExitResponderChain(Responder * nextFirstResponder) override; private: - Responder * tabController() const; + Responder * tabController() const override; constexpr static int k_totalNumberOfRows = 13; constexpr static int k_maxNumberOfDisplayableRows = 11; static constexpr KDCoordinate k_cellHeight = 20; static constexpr KDCoordinate k_cellWidth = Ion::Display::Width/2 - Metric::CommonRightMargin/2 - Metric::CommonLeftMargin/2; EvenOddPointerTextCell m_titleCells[k_maxNumberOfDisplayableRows]; EvenOddBufferTextCell m_calculationCells[k_maxNumberOfDisplayableRows]; - SelectableTableView m_selectableTableView; Store * m_store; };