Numworks Epsilon  1.4.1
Graphing Calculator Operating System
history_controller.cpp
Go to the documentation of this file.
1 #include "history_controller.h"
2 #include "app.h"
3 #include "../apps_container.h"
4 #include <assert.h>
5 
6 namespace Calculation {
7 
8 HistoryController::HistoryController(Responder * parentResponder, CalculationStore * calculationStore) :
9  DynamicViewController(parentResponder),
10  m_calculationHistory{},
11  m_calculationStore(calculationStore)
12 {
13 }
14 
16  selectableTableView()->reloadData();
17 }
18 
21  app()->setFirstResponder(selectableTableView());
22 }
23 
25  if (nextFirstResponder == parentResponder()) {
26  selectableTableView()->deselectTable();
27  }
28 }
29 
31  if (event == Ion::Events::Down) {
32  selectableTableView()->deselectTable();
34  return true;
35  }
36  if (event == Ion::Events::Up) {
37  return true;
38  }
39  if (event == Ion::Events::OK || event == Ion::Events::EXE) {
40  int focusRow = selectedRow();
41  HistoryViewCell * selectedCell = (HistoryViewCell *)selectableTableView()->selectedCell();
42  HistoryViewCell::SubviewType subviewType = selectedCell->selectedSubviewType();
44  selectableTableView()->deselectTable();
45  app()->setFirstResponder(editController);
46  Calculation * calculation = m_calculationStore->calculationAtIndex(focusRow);
47  if (subviewType == HistoryViewCell::SubviewType::Input) {
48  editController->insertTextBody(calculation->inputText());
49  } else {
50  OutputExpressionsView::SubviewType outputSubviewType = selectedCell->outputView()->selectedSubviewType();
51  if (outputSubviewType == OutputExpressionsView::SubviewType::ExactOutput) {
52  editController->insertTextBody(calculation->exactOutputText());
53  } else {
54  editController->insertTextBody(calculation->approximateOutputText());
55  }
56  }
57  return true;
58  }
59  if (event == Ion::Events::Backspace) {
60  int focusRow = selectedRow();
61  HistoryViewCell * selectedCell = (HistoryViewCell *)selectableTableView()->selectedCell();
62  HistoryViewCell::SubviewType subviewType = selectedCell->selectedSubviewType();
63  selectableTableView()->deselectTable();
65  m_calculationStore->deleteCalculationAtIndex(focusRow);
66  reload();
67  if (numberOfRows()== 0) {
68  app()->setFirstResponder(editController);
69  return true;
70  }
71  if (focusRow > 0) {
72  selectableTableView()->selectCellAtLocation(0, focusRow-1);
73  } else {
74  selectableTableView()->selectCellAtLocation(0, 0);
75  }
76  if (subviewType == HistoryViewCell::SubviewType::Input) {
77  tableViewDidChangeSelection(selectableTableView(), 0, selectedRow());
78  } else {
79  tableViewDidChangeSelection(selectableTableView(), 0, -1);
80  }
81  selectableTableView()->scrollToCell(0, selectedRow());
82  return true;
83  }
84  if (event == Ion::Events::Clear) {
85  selectableTableView()->deselectTable();
86  m_calculationStore->deleteAll();
87  reload();
89  return true;
90  }
91  if (event == Ion::Events::Back) {
93  selectableTableView()->deselectTable();
94  app()->setFirstResponder(editController);
95  return true;
96  }
97  if (event == Ion::Events::Copy) {
98  HistoryViewCell * selectedCell = (HistoryViewCell *)selectableTableView()->selectedCell();
99  HistoryViewCell::SubviewType subviewType = selectedCell->selectedSubviewType();
100  int focusRow = selectedRow();
101  Calculation * calculation = m_calculationStore->calculationAtIndex(focusRow);
102  if (subviewType == HistoryViewCell::SubviewType::Input) {
103  Clipboard::sharedClipboard()->store(calculation->inputText());
104  } else {
105  OutputExpressionsView::SubviewType outputSubviewType = selectedCell->outputView()->selectedSubviewType();
106  if (outputSubviewType == OutputExpressionsView::SubviewType::ExactOutput) {
107  Clipboard::sharedClipboard()->store(calculation->exactOutputText());
108  } else {
109  Clipboard::sharedClipboard()->store(calculation->approximateOutputText());
110  }
111  }
112  return true;
113  }
114  return false;
115 }
116 
117 void HistoryController::tableViewDidChangeSelection(SelectableTableView * t, int previousSelectedCellX, int previousSelectedCellY) {
118  HistoryViewCell * selectedCell = (HistoryViewCell *)(t->selectedCell());
119  if (selectedCell == nullptr) {
120  return;
121  }
122  if (previousSelectedCellY == -1) {
124  } else if (selectedRow() < previousSelectedCellY) {
126  } else if (selectedRow() > previousSelectedCellY) {
128  }
129  app()->setFirstResponder(selectedCell);
130  selectedCell->reloadCell();
131 }
132 
134  return m_calculationStore->numberOfCalculations();
135 };
136 
138  assert(type == 0);
139  assert(index >= 0);
140  assert(index < k_maxNumberOfDisplayedRows);
141  return m_calculationHistory[index];
142 }
143 
145  assert(type == 0);
146  return k_maxNumberOfDisplayedRows;
147 }
148 
150  HistoryViewCell * myCell = (HistoryViewCell *)cell;
151  myCell->setCalculation(m_calculationStore->calculationAtIndex(index));
152  myCell->setEven(index%2 == 0);
153  myCell->reloadCell();
154 }
155 
157  if (j >= m_calculationStore->numberOfCalculations()) {
158  return 0;
159  }
160  Calculation * calculation = m_calculationStore->calculationAtIndex(j);
161  KDCoordinate inputHeight = calculation->inputLayout()->size().height();
162  App * calculationApp = (App *)app();
163  Poincare::ExpressionLayout * approximateLayout = calculation->approximateOutputLayout(calculationApp->localContext());
164  KDCoordinate approximateOutputHeight = approximateLayout->size().height();
165  if (calculation->shouldDisplayApproximateOutput(calculationApp->localContext())) {
166  return inputHeight + approximateOutputHeight + 3*HistoryViewCell::k_digitVerticalMargin;
167  }
168  Poincare::ExpressionLayout * exactLayout = calculation->exactOutputLayout(calculationApp->localContext());
169  KDCoordinate exactOutputHeight = exactLayout->size().height();
170  KDCoordinate outputHeight = max(exactLayout->baseline(), approximateLayout->baseline()) + max(exactOutputHeight-exactLayout->baseline(), approximateOutputHeight-approximateLayout->baseline());
171  return inputHeight + outputHeight + 3*HistoryViewCell::k_digitVerticalMargin;
172 }
173 
175  int result = 0;
176  for (int k = 0; k < j; k++) {
177  result += rowHeight(k);
178  }
179  return result;
180 }
181 
183  int result = 0;
184  int j = 0;
185  while (result < offsetY && j < numberOfRows()) {
186  result += rowHeight(j++);
187  }
188  return (result < offsetY || offsetY == 0) ? j : j - 1;
189 }
190 
192  return 0;
193 }
194 
196  selectableTableView()->scrollToCell(i, j);
197 }
198 
199 CalculationSelectableTableView * HistoryController::selectableTableView() {
201 }
202 
204  CalculationSelectableTableView * tableView = new CalculationSelectableTableView(this, this, this, this);
205 for (int i = 0; i < k_maxNumberOfDisplayedRows; i++) {
206  m_calculationHistory[i] = new HistoryViewCell(tableView);
207  }
208  return tableView;
209 }
210 
212  for (int i = 0; i < k_maxNumberOfDisplayedRows; i++) {
213  delete m_calculationHistory[i];
214  m_calculationHistory[i] = nullptr;
215  }
216  delete view;
217 }
218 
219 }
void setCalculation(Calculation *calculation)
Calculation * calculationAtIndex(int i)
void willDisplayCellForIndex(HighlightCell *cell, int index) override
#define assert(e)
Definition: assert.h:9
HighlightCell * selectedCell()
void tableViewDidChangeSelection(SelectableTableView *t, int previousSelectedCellX, int previousSelectedCellY) override
constexpr Event EXE
Definition: events.h:114
HistoryController(Responder *parentResponder, CalculationStore *calculationStore)
int16_t KDCoordinate
Definition: coordinate.h:6
Responder * parentResponder() const
Definition: responder.cpp:12
void setSelectedSubviewType(HistoryViewCell::SubviewType subviewType)
constexpr Event Up
Definition: events.h:62
constexpr Event Down
Definition: events.h:63
bool selectCellAtLocation(int i, int j, bool setFirstResponder=true)
bool handleEvent(Ion::Events::Event event) override
int indexFromCumulatedHeight(KDCoordinate offsetY) override
constexpr Event Back
Definition: events.h:66
virtual Poincare::Context * localContext()
void store(const char *storedText)
Definition: clipboard.cpp:9
void reloadData(bool setFirstResponder=true)
KDCoordinate cumulatedHeightFromIndex(int j) override
static constexpr KDCoordinate k_digitVerticalMargin
HighlightCell * reusableCell(int index, int type) override
void willExitResponderChain(Responder *nextFirstResponder) override
constexpr Event Copy
Definition: events.h:123
void setFirstResponder(Responder *responder)
Definition: app.cpp:62
Definition: view.h:23
void unloadView(View *view) override
KDCoordinate rowHeight(int j) override
static Clipboard * sharedClipboard()
Definition: clipboard.cpp:5
OutputExpressionsView * outputView()
App * app()
Definition: responder.cpp:77
constexpr Event OK
Definition: events.h:65
int typeAtLocation(int i, int j) override
constexpr Event Clear
Definition: events.h:125
void setEven(bool even) override
int reusableCellCount(int type) override
constexpr Event Backspace
Definition: events.h:76
constexpr KDCoordinate height() const
Definition: size.h:11