[escher] create a class selectable table view

Change-Id: I4e6372ed7d586d35b9b96f8cb1d29bf693713dda
This commit is contained in:
Émilie Feral
2016-10-25 11:40:36 +02:00
parent 5d7e57b228
commit c5bb3df9a8
4 changed files with 99 additions and 1 deletions

View File

@@ -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\

View File

@@ -19,6 +19,7 @@
#include <escher/responder.h>
#include <escher/scroll_view.h>
#include <escher/scroll_view_indicator.h>
#include <escher/selectable_table_view.h>
#include <escher/simple_table_view_data_source.h>
#include <escher/simple_list_view_data_source.h>
#include <escher/solid_color_view.h>

View File

@@ -0,0 +1,30 @@
#ifndef ESCHER_SELECTABLE_TABLE_VIEW_H
#define ESCHER_SELECTABLE_TABLE_VIEW_H
#include <escher/table_view.h>
#include <escher/app.h>
#include <escher/table_view_data_source.h>
#include <escher/responder.h>
/* 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

View File

@@ -0,0 +1,66 @@
#include <escher/selectable_table_view.h>
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;
}
}