Numworks Epsilon  1.4.1
Graphing Calculator Operating System
selectable_table_view.cpp
Go to the documentation of this file.
2 #include <assert.h>
3 
5  TableView(dataSource, selectionDataSource),
6  Responder(parentResponder),
7  m_selectionDataSource(selectionDataSource),
8  m_delegate(delegate)
9 {
11  assert(m_selectionDataSource != nullptr);
12 }
13 
16 }
17 
20 }
21 
24 }
25 
28 }
29 
30 void SelectableTableView::reloadData(bool setFirstResponder) {
31  int col = selectedColumn();
32  int row = selectedRow();
33  deselectTable();
34  /* FIXME: The problem with calling deselectTable is that at this point in time
35  * the datasource's model is very likely to have changed. Therefore it's
36  * rather complicated to get a pointer to the currently selected cell (in
37  * order to deselect it). */
38  /* As a workaround, datasources can reset the highlighted state in their
39  * willDisplayCell callback. */
41  selectCellAtLocation(col, row, setFirstResponder);
42 }
43 
45  int col = selectedColumn();
46  int row = selectedRow();
47  selectColumn(0);
48  selectRow(-1);
49  selectCellAtLocation(col, row);
50 }
51 
53  unhighlightSelectedCell();
54 }
55 
57  unhighlightSelectedCell();
58  int previousSelectedCellX = selectedColumn();
59  int previousSelectedCellY = selectedRow();
60  selectColumn(0);
61  selectRow(-1);
62  if (m_delegate) {
63  m_delegate->tableViewDidChangeSelection(this, previousSelectedCellX, previousSelectedCellY);
64  }
65 }
66 
67 bool SelectableTableView::selectCellAtLocation(int i, int j, bool setFirstResponder) {
68  if (i < 0 || i >= dataSource()->numberOfColumns()) {
69  return false;
70  }
71  if (j < 0 || j >= dataSource()->numberOfRows()) {
72  return false;
73  }
74  unhighlightSelectedCell();
75  int previousX = selectedColumn();
76  int previousY = selectedRow();
77  selectColumn(i);
78  selectRow(j);
79  if (selectedRow() >= 0) {
80  scrollToCell(i, j);
81  }
82  HighlightCell * cell = selectedCell();
83  if (cell) {
84  cell->setHighlighted(true);
85  // Update first responder
86  if ((i != previousX || j != previousY) && setFirstResponder) {
87  if (cell->responder()) {
88  app()->setFirstResponder(cell->responder());
89  } else {
90  app()->setFirstResponder(this);
91  }
92  }
93  }
94  if (m_delegate) {
95  m_delegate->tableViewDidChangeSelection(this, previousX, previousY);
96  }
97  return true;
98 }
99 
101  if (selectedColumn() < 0 || selectedRow() < 0) {
102  return nullptr;
103  }
105 }
106 
108  if (event == Ion::Events::Down) {
110  }
111  if (event == Ion::Events::Up) {
113  }
114  if (event == Ion::Events::Left) {
116  }
117  if (event == Ion::Events::Right) {
119  }
120  return false;
121 }
122 
123 void SelectableTableView::unhighlightSelectedCell() {
124  if (selectedColumn() >= 0 && selectedColumn() < dataSource()->numberOfColumns() &&
125  selectedRow() >= 0 && selectedRow() < dataSource()->numberOfRows()) {
127  previousCell->setHighlighted(false);
128  }
129 }
#define assert(e)
Definition: assert.h:9
HighlightCell * selectedCell()
SelectableTableView(Responder *parentResponder, TableViewDataSource *dataSource, SelectableTableViewDataSource *selectionDataSource=nullptr, SelectableTableViewDelegate *delegate=nullptr)
SelectableTableViewDelegate * m_delegate
void layoutSubviews() override
Definition: table_view.cpp:40
virtual void tableViewDidChangeSelection(SelectableTableView *t, int previousSelectedCellX, int previousSelectedCellY)
constexpr Event Up
Definition: events.h:62
constexpr Event Down
Definition: events.h:63
bool selectCellAtLocation(int i, int j, bool setFirstResponder=true)
virtual void scrollToCell(int i, int j)
Definition: table_view.cpp:26
HighlightCell * cellAtLocation(int i, int j)
Definition: table_view.cpp:30
virtual void didEnterResponderChain(Responder *previousFirstResponder) override
TableViewDataSource * dataSource()
Definition: table_view.cpp:20
void setCommonMargins()
Definition: scroll_view.cpp:27
void reloadData(bool setFirstResponder=true)
virtual Responder * responder()
constexpr uint8_t numberOfColumns
Definition: keyboard.h:39
constexpr Event Left
Definition: events.h:61
void setFirstResponder(Responder *responder)
Definition: app.cpp:62
constexpr Event Right
Definition: events.h:64
virtual bool handleEvent(Ion::Events::Event event) override
virtual void willExitResponderChain(Responder *nextFirstResponder) override
constexpr uint8_t numberOfRows
Definition: keyboard.h:35
App * app()
Definition: responder.cpp:77
virtual void setHighlighted(bool highlight)
SelectableTableViewDataSource * m_selectionDataSource