From c5bb3df9a888998f9123844944d80ef7d0e4fbe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Tue, 25 Oct 2016 11:40:36 +0200 Subject: [PATCH] [escher] create a class selectable table view Change-Id: I4e6372ed7d586d35b9b96f8cb1d29bf693713dda --- escher/Makefile | 3 +- escher/include/escher.h | 1 + escher/include/escher/selectable_table_view.h | 30 +++++++++ escher/src/selectable_table_view.cpp | 66 +++++++++++++++++++ 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 escher/include/escher/selectable_table_view.h create mode 100644 escher/src/selectable_table_view.cpp diff --git a/escher/Makefile b/escher/Makefile index 7bae469b0..512b72060 100644 --- a/escher/Makefile +++ b/escher/Makefile @@ -10,6 +10,7 @@ objs += $(addprefix escher/src/,\ invocation.o\ input_view_controller.o\ list_view.o\ + list_view_cell.o\ list_view_data_source.o\ metric.o\ palette.o\ @@ -17,6 +18,7 @@ objs += $(addprefix escher/src/,\ responder.o\ scroll_view.o\ scroll_view_indicator.o\ + selectable_table_view.o\ simple_list_view_data_source.o\ simple_table_view_data_source.o\ solid_color_view.o\ @@ -29,7 +31,6 @@ objs += $(addprefix escher/src/,\ tab_view_controller.o\ table_view.o\ table_view_cell.o\ - list_view_cell.o\ table_view_data_source.o\ text_field.o\ text_list_view_cell.o\ diff --git a/escher/include/escher.h b/escher/include/escher.h index 37c7a2f36..37f01c12c 100644 --- a/escher/include/escher.h +++ b/escher/include/escher.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include diff --git a/escher/include/escher/selectable_table_view.h b/escher/include/escher/selectable_table_view.h new file mode 100644 index 000000000..7b7372008 --- /dev/null +++ b/escher/include/escher/selectable_table_view.h @@ -0,0 +1,30 @@ +#ifndef ESCHER_SELECTABLE_TABLE_VIEW_H +#define ESCHER_SELECTABLE_TABLE_VIEW_H + +#include +#include +#include +#include + +/* SelectableTableView is a Table View that handles selection. To implement it, + * it needs a class which should be both data source and view controller. This + * takes the selectable table view as instance variable and makes it first + * responder. The selectable table view bubles up events when they do not + * concern selection. */ + +class SelectableTableView : public TableView, public Responder { +public: + SelectableTableView(Responder * parentResponder, TableViewDataSource * dataSource, KDCoordinate topMargin, + KDCoordinate rightMargin, KDCoordinate bottomMargin, KDCoordinate leftMargin); + int selectedRow(); + int selectedColumn(); + virtual bool handleEvent(Ion::Events::Event event) override; + virtual void didBecomeFirstResponder() override; + void deselectTable(); + bool setSelectedCellAtLocation(int i, int j); +private: + int m_selectedCellX; + int m_selectedCellY; +}; + +#endif diff --git a/escher/src/selectable_table_view.cpp b/escher/src/selectable_table_view.cpp new file mode 100644 index 000000000..80ccda41e --- /dev/null +++ b/escher/src/selectable_table_view.cpp @@ -0,0 +1,66 @@ +#include + +SelectableTableView::SelectableTableView(Responder * parentResponder, TableViewDataSource * dataSource, + KDCoordinate topMargin, KDCoordinate rightMargin, KDCoordinate bottomMargin, KDCoordinate leftMargin) : + TableView(dataSource, topMargin, rightMargin, bottomMargin, leftMargin), + Responder(parentResponder), + m_selectedCellX(0), + m_selectedCellY(-1) +{ +} + +int SelectableTableView::selectedRow() { + return m_selectedCellY; +} + +int SelectableTableView::selectedColumn() { + return m_selectedCellX; +} + +void SelectableTableView::didBecomeFirstResponder() { + reloadData(); +} + +void SelectableTableView::deselectTable() { + TableViewCell * previousCell = cellAtLocation(m_selectedCellX, m_selectedCellY); + previousCell->setHighlighted(false); + m_selectedCellX = 0; + m_selectedCellY = -1; +} + +bool SelectableTableView::setSelectedCellAtLocation(int i, int j) { + if (i < 0 || i >= dataSource()->numberOfColumns()) { + return false; + } + if (j < 0 || j >= dataSource()->numberOfRows()) { + return false; + } + + if (m_selectedCellY >= 0) { + TableViewCell * previousCell = cellAtLocation(m_selectedCellX, m_selectedCellY); + previousCell->setHighlighted(false); + } + m_selectedCellX = i; + m_selectedCellY = j; + if (m_selectedCellY >= 0) { + scrollToCell(i, j); + TableViewCell * cell = cellAtLocation(i, j); + cell->setHighlighted(true); + } + return true; +} + +bool SelectableTableView::handleEvent(Ion::Events::Event event) { + switch (event) { + case Ion::Events::Event::DOWN_ARROW: + return setSelectedCellAtLocation(m_selectedCellX, m_selectedCellY+1); + case Ion::Events::Event::UP_ARROW: + return setSelectedCellAtLocation(m_selectedCellX, m_selectedCellY-1); + case Ion::Events::Event::LEFT_ARROW: + return setSelectedCellAtLocation(m_selectedCellX-1, m_selectedCellY); + case Ion::Events::Event::RIGHT_ARROW: + return setSelectedCellAtLocation(m_selectedCellX+1, m_selectedCellY); + default: + return false; + } +}