Numworks Epsilon  1.4.1
Graphing Calculator Operating System
calculation_controller.cpp
Go to the documentation of this file.
2 #include "../constant.h"
3 #include "../apps_container.h"
4 #include "../../poincare/src/layout/baseline_relative_layout.h"
5 #include "../../poincare/src/layout/string_layout.h"
6 #include <poincare.h>
7 #include <assert.h>
8 
9 using namespace Poincare;
10 using namespace Shared;
11 
12 namespace Regression {
13 
14 CalculationController::CalculationController(Responder * parentResponder, ButtonRowController * header, Store * store) :
15  TabTableController(parentResponder, this),
16  ButtonRowDelegate(header, nullptr),
17  m_titleCells{},
18  m_r2TitleCell(nullptr),
19  m_columnTitleCell(nullptr),
20  m_doubleCalculationCells{},
21  m_calculationCells{},
22  m_store(store)
23 {
24  m_r2Layout = new BaselineRelativeLayout(new StringLayout("r", 1, KDText::FontSize::Small), new StringLayout("2", 1, KDText::FontSize::Small), BaselineRelativeLayout::Type::Superscript);
25 }
26 
28  if (m_r2Layout) {
29  delete m_r2Layout;
30  m_r2Layout = nullptr;
31  }
32 }
33 
35  return I18n::translate(I18n::Message::StatTab);
36 }
37 
39  if (event == Ion::Events::Up) {
41  app()->setFirstResponder(tabController());
42  return true;
43  }
44  if (event == Ion::Events::Copy && selectedColumn() == 1 && selectedRow() > 0) {
45  if (selectedRow() <= k_totalNumberOfDoubleBufferRows) {
47  if (myCell->firstTextSelected()) {
49  } else {
51  }
52  } else {
55  }
56  return true;
57  }
58  return false;
59 }
60 
62  if (selectedRow() == -1) {
64  } else {
66  }
67  TabTableController::didBecomeFirstResponder();
68 }
69 
70 void CalculationController::tableViewDidChangeSelection(SelectableTableView * t, int previousSelectedCellX, int previousSelectedCellY) {
71  /* To prevent selecting cell with no content (top left corner of the table),
72  * as soon as the selected cell is the top left corner, we either reselect
73  * the previous cell or select the tab controller depending on from which cell
74  * the selection comes. This trick does not create an endless loop as the
75  * previous cell cannot be the top left corner cell if it also is the
76  * selected one. */
77  if (t->selectedRow() == 0 && t->selectedColumn() == 0) {
78  if (previousSelectedCellX == 0 && previousSelectedCellY == 1) {
80  app()->setFirstResponder(tabController());
81  } else {
82  t->selectCellAtLocation(previousSelectedCellX, previousSelectedCellY);
83  }
84  }
85  if (t->selectedColumn() == 1 && t->selectedRow() >= 0 && t->selectedRow() <= k_totalNumberOfDoubleBufferRows) {
87  bool firstSubCellSelected = true;
88  if (previousSelectedCellX == 1 && previousSelectedCellY >= 0 && previousSelectedCellY <= k_totalNumberOfDoubleBufferRows) {
89  EvenOddDoubleBufferTextCell * myPreviousCell = (EvenOddDoubleBufferTextCell *)t->cellAtLocation(previousSelectedCellX, previousSelectedCellY);
90  firstSubCellSelected = myPreviousCell->firstTextSelected();
91  }
92  myCell->selectFirstText(firstSubCellSelected);
93  }
94 }
95 
97  if (m_store->numberOfPairs() == 0) {
98  return true;
99  }
100  return false;
101 }
102 
104  return I18n::Message::NoValueToCompute;
105 }
106 
108  return tabController();
109 }
110 
112  return k_totalNumberOfRows;
113 }
114 
116  return k_totalNumberOfColumns;
117 }
118 
120  EvenOddCell * myCell = (EvenOddCell *)cell;
121  myCell->setEven(j%2 == 0);
122  myCell->setHighlighted(i == selectedColumn() && j == selectedRow());
123  if (j == 0 && i > 0) {
125  myCell->setFirstText("x");
126  myCell->setSecondText("y");
127  return;
128  }
129  if (i == 0) {
130  if (j == numberOfRows()-1) {
131  EvenOddExpressionCell * myCell = (EvenOddExpressionCell *)cell;
132  myCell->setExpression(m_r2Layout);
133  return;
134  }
136  if (j == 0) {
137  myCell->setMessage(I18n::Message::Default);
138  return;
139  }
140  myCell->setAlignment(1.0f, 0.5f);
141  I18n::Message titles[k_totalNumberOfRows-1] = {I18n::Message::Mean, I18n::Message::Sum, I18n::Message::SquareSum, I18n::Message::StandardDeviation, I18n::Message::Deviation, I18n::Message::NumberOfDots, I18n::Message::Covariance, I18n::Message::Sxy, I18n::Message::Regression, I18n::Message::A, I18n::Message::B, I18n::Message::R, I18n::Message::Default};
142  myCell->setMessage(titles[j-1]);
143  return;
144  }
145  if (i == 1 && j > 0 && j <= k_totalNumberOfDoubleBufferRows) {
146  ArgCalculPointer calculationMethods[k_totalNumberOfDoubleBufferRows] = {&Store::meanOfColumn, &Store::sumOfColumn,
148  double calculation1 = (m_store->*calculationMethods[j-1])(0);
149  double calculation2 = (m_store->*calculationMethods[j-1])(1);
151  char buffer[PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)];
152  PrintFloat::convertFloatToText<double>(calculation1, buffer, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits);
153  myCell->setFirstText(buffer);
154  PrintFloat::convertFloatToText<double>(calculation2, buffer, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits);
155  myCell->setSecondText(buffer);
156  return;
157  }
158  if (i == 1 && j == 9) {
159  EvenOddBufferTextCell * myCell = (EvenOddBufferTextCell *)cell;
160  myCell->setText("ax+b");
161  return;
162  }
163  if (i == 1 && j > k_totalNumberOfDoubleBufferRows) {
164  assert(j != 9);
165  CalculPointer calculationMethods[k_totalNumberOfRows-k_totalNumberOfDoubleBufferRows] = {&Store::numberOfPairs, &Store::covariance,
167  double calculation = (m_store->*calculationMethods[j-k_totalNumberOfDoubleBufferRows-1])();
168  EvenOddBufferTextCell * myCell = (EvenOddBufferTextCell *)cell;
169  char buffer[PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)];
170  PrintFloat::convertFloatToText<double>(calculation, buffer, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits);
171  myCell->setText(buffer);
172  return;
173  }
174 }
175 
177  return k_cellWidth;
178 }
179 
181  return k_cellHeight;
182 }
183 
185  if (type == 0) {
186  assert(index < k_maxNumberOfDisplayableRows);
187  assert(m_titleCells[index] != nullptr);
188  return m_titleCells[index];
189  }
190  if (type == 1) {
191  assert(index == 0);
192  return m_r2TitleCell;
193  }
194  if (type == 2) {
195  assert(index == 0);
196  return m_columnTitleCell;
197  }
198  if (type == 3) {
199  assert(index < k_totalNumberOfDoubleBufferRows);
200  assert(m_doubleCalculationCells[index] != nullptr);
201  return m_doubleCalculationCells[index];
202  }
203  assert(index < k_totalNumberOfRows-k_totalNumberOfDoubleBufferRows);
204  assert(m_calculationCells[index] != nullptr);
205  return m_calculationCells[index];
206 }
207 
209  if (type == 0) {
210  return k_maxNumberOfDisplayableRows;
211  }
212  if (type == 1) {
213  return 1;
214  }
215  if (type == 2) {
216  return 1;
217  }
218  if (type == 3) {
219  return k_totalNumberOfDoubleBufferRows;
220  }
221  return k_totalNumberOfRows-k_totalNumberOfDoubleBufferRows;
222 }
223 
225  if (i == 0 && j == k_totalNumberOfRows-1) {
226  return 1;
227  }
228  if (i == 0) {
229  return 0;
230  }
231  if (j == 0) {
232  return 2;
233  }
234  if (j > 0 && j <= k_totalNumberOfDoubleBufferRows) {
235  return 3;
236  }
237  return 4;
238 }
239 
240 Responder * CalculationController::tabController() const {
242 }
243 
244 View * CalculationController::loadView() {
245  SelectableTableView * tableView = new SelectableTableView(this, this, this, this);
246  tableView->setVerticalCellOverlap(0);
248 ;
249  m_r2TitleCell = new EvenOddExpressionCell(1.0f, 0.5f);
250  m_columnTitleCell = new EvenOddDoubleBufferTextCell(tableView);
251  for (int i = 0; i < k_maxNumberOfDisplayableRows; i++) {
252  m_titleCells[i] = new EvenOddMessageTextCell(KDText::FontSize::Small);
253  }
254  for (int i = 0; i < k_totalNumberOfDoubleBufferRows; i++) {
255  m_doubleCalculationCells[i] = new EvenOddDoubleBufferTextCell();
256  m_doubleCalculationCells[i]->setTextColor(Palette::GreyDark);
257  m_doubleCalculationCells[i]->setParentResponder(tableView);
258  }
259  for (int i = 0; i < k_totalNumberOfRows-k_totalNumberOfDoubleBufferRows;i++) {
260  m_calculationCells[i] = new EvenOddBufferTextCell(KDText::FontSize::Small);
261  m_calculationCells[i]->setTextColor(Palette::GreyDark);
262  }
263  return tableView;
264 }
265 
266 void CalculationController::unloadView(View * view) {
267  delete m_r2TitleCell;
268  m_r2TitleCell = nullptr;
269  delete m_columnTitleCell;
270  m_columnTitleCell = nullptr;
271  for (int i = 0; i < k_totalNumberOfDoubleBufferRows; i++) {
272  delete m_doubleCalculationCells[i];
273  m_doubleCalculationCells[i] = nullptr;
274  }
275  for (int i = 0; i < k_totalNumberOfRows-k_totalNumberOfDoubleBufferRows;i++) {
276  delete m_calculationCells[i];
277  m_calculationCells[i] = nullptr;
278  }
279  for (int i = 0; i < k_maxNumberOfDisplayableRows; i++) {
280  delete m_titleCells[i];
281  m_titleCells[i] = nullptr;
282  }
283  TabTableController::unloadView(view);
284 }
285 
286 }
287 
double squaredCorrelationCoefficient()
Definition: store.cpp:210
double columnProductSum()
Definition: store.cpp:154
void setFirstText(const char *textContent)
void setAlignment(float horizontalAlignment, float verticalAlignment)
#define assert(e)
Definition: assert.h:9
void setBackgroundColor(KDColor c)
Definition: scroll_view.h:32
double standardDeviationOfColumn(int i)
Definition: store.cpp:174
HighlightCell * selectedCell()
virtual void setEven(bool even)
int16_t KDCoordinate
Definition: coordinate.h:6
Responder * parentResponder() const
Definition: responder.cpp:12
static constexpr KDColor GreyDark
Definition: palette.h:15
double meanOfColumn(int i)
Definition: store.cpp:162
double(Store::* CalculPointer)()
Definition: store.h:49
void setVerticalCellOverlap(KDCoordinate o)
Definition: table_view.h:14
constexpr Event Up
Definition: events.h:62
bool selectCellAtLocation(int i, int j, bool setFirstResponder=true)
void tableViewDidChangeSelection(SelectableTableView *t, int previousSelectedCellX, int previousSelectedCellY) override
double correlationCoefficient()
Definition: store.cpp:201
HighlightCell * cellAtLocation(int i, int j)
Definition: table_view.cpp:30
KDCoordinate rowHeight(int j) override
double varianceOfColumn(int i)
Definition: store.cpp:169
void setTextColor(KDColor textColor)
double(Store::* ArgCalculPointer)(int)
Definition: store.h:48
void setText(const char *textContent)
void store(const char *storedText)
Definition: clipboard.cpp:9
enum Message uint16_t enum Language uint16_t const char * translate(Message m, Language l=(Language) 0)
Definition: i18n.cpp:5
void setParentResponder(Responder *responder)
Definition: responder.cpp:16
static constexpr int LargeNumberOfSignificantDigits
Definition: constant.h:6
double yIntercept()
Definition: store.cpp:186
KDCoordinate columnWidth(int i) override
HighlightCell * reusableCell(int index, int type) override
void setMessage(I18n::Message textContent, KDColor textColor=KDColorBlack)
constexpr Event Copy
Definition: events.h:123
double squaredValueSumOfColumn(int i)
Definition: store.cpp:146
void setFirstResponder(Responder *responder)
Definition: app.cpp:62
bool handleEvent(Ion::Events::Event event) override
Definition: view.h:23
static Clipboard * sharedClipboard()
Definition: clipboard.cpp:5
void willDisplayCellAtLocation(HighlightCell *cell, int i, int j) override
App * app()
Definition: responder.cpp:77
virtual void setHighlighted(bool highlight)
double numberOfPairs()
Definition: store.cpp:122
double slope()
Definition: store.cpp:182
SelectableTableView * selectableTableView()
void setExpression(Poincare::ExpressionLayout *expressionLayout)
double covariance()
Definition: store.cpp:178
void setSecondText(const char *textContent)
int typeAtLocation(int i, int j) override
static constexpr KDColor WallScreenDark
Definition: palette.h:20