Numworks Epsilon  1.4.1
Graphing Calculator Operating System
values_controller.cpp
Go to the documentation of this file.
1 #include "values_controller.h"
2 #include <assert.h>
3 #include "../../constant.h"
4 
5 using namespace Shared;
6 using namespace Poincare;
7 
8 namespace Graph {
9 
10 ValuesController::ValuesController(Responder * parentResponder, CartesianFunctionStore * functionStore, Interval * interval, ButtonRowController * header) :
11  Shared::ValuesController(parentResponder, header, I18n::Message::XColumn, &m_intervalParameterController, interval),
12  m_functionTitleCells{},
13  m_floatCells{},
14  m_functionStore(functionStore),
15  m_functionParameterController(this),
16  m_intervalParameterController(this, m_interval),
17  m_derivativeParameterController(this)
18 {
19 }
20 
22  if ((event == Ion::Events::OK || event == Ion::Events::EXE) && selectedRow() == 0
23  && selectedColumn()>0 && isDerivativeColumn(selectedColumn())) {
24  configureDerivativeFunction();
25  return true;
26  }
28 }
29 
32  // The cell is the abscissa title cell:
33  if (j == 0 && i == 0) {
34  EvenOddMessageTextCell * mytitleCell = (EvenOddMessageTextCell *)cell;
35  mytitleCell->setMessage(I18n::Message::X);
36  return;
37  }
38  // The cell is a function title cell:
39  if (j == 0 && i > 0) {
40  FunctionTitleCell * myFunctionCell = (FunctionTitleCell *)cell;
41  CartesianFunction * function = functionAtColumn(i);
42  char bufferName[6] = {0, 0, '(', 'x', ')', 0};
43  const char * name = nullptr;
44  if (isDerivativeColumn(i)) {
45  bufferName[0] = *function->name();
46  bufferName[1] = '\'';
47  name = bufferName;
48  } else {
49  bufferName[1] = *function->name();
50  name = &bufferName[1];
51  }
52  myFunctionCell->setText(name);
53  myFunctionCell->setColor(function->color());
54  }
55 }
56 
58  if (m_functionStore->numberOfDefinedFunctions() == 0) {
59  return I18n::Message::NoFunction;
60  }
61  return I18n::Message::NoActivatedFunction;
62 }
63 
65  return &m_intervalParameterController;
66 }
67 
68 CartesianFunction * ValuesController::functionAtColumn(int i) {
69  assert(i > 0);
70  int index = 1;
71  for (int k = 0; k < m_functionStore->numberOfDefinedFunctions(); k++) {
72  if (m_functionStore->definedFunctionAtIndex(k)->isActive()) {
73  if (i == index) {
74  return m_functionStore->definedFunctionAtIndex(k);
75  }
76  index++;
77  if (m_functionStore->definedFunctionAtIndex(k)->displayDerivative()) {
78  if (i == index) {
79  return m_functionStore->definedFunctionAtIndex(k);
80  }
81  index++;
82  }
83  }
84  }
85  assert(false);
86  return nullptr;
87 }
88 
89 bool ValuesController::isDerivativeColumn(int i) {
90  assert(i >= 1);
91  int index = 1;
92  for (int k = 0; k < m_functionStore->numberOfDefinedFunctions(); k++) {
93  if (m_functionStore->definedFunctionAtIndex(k)->isActive()) {
94  if (i == index) {
95  return false;
96  }
97  index++;
98  if (m_functionStore->definedFunctionAtIndex(k)->displayDerivative()) {
99  if (i == index) {
100  return true;
101  }
102  index++;
103  }
104  }
105  }
106  assert(false);
107  return false;
108 }
109 
110 void ValuesController::configureDerivativeFunction() {
111  CartesianFunction * function = functionAtColumn(selectedColumn());
112  m_derivativeParameterController.setFunction(function);
114  stack->push(&m_derivativeParameterController);
115 }
116 
117 int ValuesController::maxNumberOfCells() {
118  return k_maxNumberOfCells;
119 }
120 
121 int ValuesController::maxNumberOfFunctions() {
122  return k_maxNumberOfFunctions;
123 }
124 
125 FunctionTitleCell * ValuesController::functionTitleCells(int j) {
126  assert(j >= 0 && j < k_maxNumberOfFunctions);
127  return m_functionTitleCells[j];
128 }
129 
130 EvenOddBufferTextCell * ValuesController::floatCells(int j) {
131  assert(j >= 0 && j < k_maxNumberOfCells);
132  return m_floatCells[j];
133 }
134 
135 CartesianFunctionStore * ValuesController::functionStore() const {
136  return m_functionStore;
137 }
138 
139 FunctionParameterController * ValuesController::functionParameterController() {
140  return &m_functionParameterController;
141 }
142 
143 double ValuesController::evaluationOfAbscissaAtColumn(double abscissa, int columnIndex) {
144  CartesianFunction * function = functionAtColumn(columnIndex);
146  if (isDerivativeColumn(columnIndex)) {
147  return function->approximateDerivative(abscissa, myApp->localContext());
148  }
149  return function->evaluateAtAbscissa(abscissa, myApp->localContext());
150 }
151 
152 View * ValuesController::loadView() {
153  for (int i = 0; i < k_maxNumberOfFunctions; i++) {
155  }
156  for (int i = 0; i < k_maxNumberOfCells; i++) {
157  m_floatCells[i] = new EvenOddBufferTextCell();
158  }
160 }
161 
162 void ValuesController::unloadView(View * view) {
163  for (int i = 0; i < k_maxNumberOfCells; i++) {
164  delete m_floatCells[i];
165  m_floatCells[i] = nullptr;
166  }
167  for (int i = 0; i < k_maxNumberOfFunctions; i++) {
168  delete m_functionTitleCells[i];
169  m_functionTitleCells[i] = nullptr;
170  }
172 }
173 
175  int result = 1;
176  for (int i = 0; i < m_functionStore->numberOfActiveFunctions(); i++) {
177  if (m_functionStore->activeFunctionAtIndex(i)->isActive()) {
178  result += 1 + m_functionStore->activeFunctionAtIndex(i)->displayDerivative();
179  }
180  }
181  m_numberOfColumns = result;
182 }
183 
184 }
185 
Definition: i18n.h:6
#define assert(e)
Definition: assert.h:9
constexpr Event EXE
Definition: events.h:114
StackViewController * stackController() const
void push(ViewController *vc, KDColor textColor=Palette::SubTab, KDColor backgroundColor=KDColorWhite, KDColor separatorColor=Palette::GreyBright)
void setColor(KDColor color) override
virtual void willDisplayCellAtLocation(HighlightCell *cell, int i, int j) override
ValuesController(Responder *parentResponder, ButtonRowController *header, I18n::Message parameterTitle, IntervalParameterController *intervalParameterController, Interval *interval)
Definition: app.cpp:9
void willDisplayCellAtLocation(HighlightCell *cell, int i, int j) override
virtual Poincare::Context * localContext()
void updateNumberOfColumns() override
virtual bool handleEvent(Ion::Events::Event event) override
I18n::Message emptyMessage() override
void setText(const char *textContent)
Shared::IntervalParameterController * intervalParameterController() override
bool handleEvent(Ion::Events::Event event) override
void setMessage(I18n::Message textContent, KDColor textColor=KDColorBlack)
Definition: view.h:23
CartesianFunction * activeFunctionAtIndex(int i) override
App * app()
Definition: responder.cpp:77
constexpr Event OK
Definition: events.h:65
CartesianFunction * definedFunctionAtIndex(int i) override
void unloadView(View *view) override