Numworks Epsilon  1.4.1
Graphing Calculator Operating System
parameters_controller.cpp
Go to the documentation of this file.
2 #include "../constant.h"
3 #include "app.h"
4 #include <assert.h>
5 #include <string.h>
6 
7 namespace Probability {
8 
9 ParametersController::ContentView::ContentView(Responder * parentResponder, SelectableTableView * selectableTableView) :
10  m_numberOfParameters(1),
11  m_titleView(KDText::FontSize::Small, I18n::Message::ChooseParameters, 0.5f, 0.5f, Palette::GreyDark, Palette::WallScreen),
12  m_firstParameterDefinition(KDText::FontSize::Small, (I18n::Message)0, 0.5f, 0.5f, KDColorBlack, Palette::WallScreen),
13  m_secondParameterDefinition(KDText::FontSize::Small, (I18n::Message)0, 0.5f, 0.5f, KDColorBlack, Palette::WallScreen),
14  m_selectableTableView(selectableTableView)
15 {
16 }
17 
18 void ParametersController::ContentView::drawRect(KDContext * ctx, KDRect rect) const {
19  int tableHeight = m_selectableTableView->minimalSizeForOptimalDisplay().height()+ Metric::CommonTopMargin + Metric::CommonBottomMargin;
20  ctx->fillRect(KDRect(0, tableHeight, bounds().width(), bounds().height() - tableHeight), Palette::WallScreen);
21 }
22 
23 MessageTextView * ParametersController::ContentView::parameterDefinitionAtIndex(int index) {
24  assert(index >= 0 && index < 2);
25  if (index == 0) {
26  return &m_firstParameterDefinition;
27  }
28  return &m_secondParameterDefinition;
29 }
30 
31 void ParametersController::ContentView::setNumberOfParameters(int numberOfParameters) {
32  m_numberOfParameters = numberOfParameters;
33 }
34 
35 int ParametersController::ContentView::numberOfSubviews() const {
36  return m_numberOfParameters+2;
37 }
38 
39 View * ParametersController::ContentView::subviewAtIndex(int index) {
40  assert(index >= 0 && index < 5);
41  if (index == 0) {
42  return &m_titleView;
43  }
44  if (index == 1) {
45  return m_selectableTableView;
46  }
47  if (index == 2) {
48  return &m_firstParameterDefinition;
49  }
50  return &m_secondParameterDefinition;
51 }
52 
53 void ParametersController::ContentView::layoutSubviews() {
54  KDCoordinate titleHeight = KDText::charSize(KDText::FontSize::Small).height()+k_titleMargin;
55  m_titleView.setFrame(KDRect(0, 0, bounds().width(), titleHeight));
56  KDCoordinate tableHeight = m_selectableTableView->minimalSizeForOptimalDisplay().height() + Metric::CommonTopMargin + Metric::CommonBottomMargin;
57  m_selectableTableView->setFrame(KDRect(0, titleHeight, bounds().width(), tableHeight));
59  KDCoordinate defOrigin = (titleHeight+tableHeight)/2+(bounds().height()-textHeight)/2;
60  m_secondParameterDefinition.setFrame(KDRectZero);
61  if (m_numberOfParameters == 2) {
62  defOrigin = (titleHeight+tableHeight)/2+(bounds().height()-2*textHeight-k_textMargin)/2;
63  m_secondParameterDefinition.setFrame(KDRect(0, defOrigin+textHeight+k_textMargin, bounds().width(), textHeight));
64  }
65  m_firstParameterDefinition.setFrame(KDRect(0, defOrigin, bounds().width(), textHeight));
66 }
67 
68 /* Parameters Controller */
69 
70 ParametersController::ParametersController(Responder * parentResponder, Law * law, CalculationController * calculationController) :
71  FloatParameterController(parentResponder),
72  m_selectableTableView(nullptr),
73  m_menuListCell{},
74  m_law(law),
75  m_calculationController(calculationController)
76 {
77  assert(m_law != nullptr);
78 }
79 
81  return I18n::translate(m_law->title());
82 }
83 
85  if (event == Ion::Events::Left) {
86  stackController()->pop();
87  return true;
88  }
89  return false;
90 }
92  m_calculationController->setCalculationAccordingToIndex(0, true);
93 }
94 
96  App::Snapshot * snapshot = (App::Snapshot *)app()->snapshot();
98  FloatParameterController::didBecomeFirstResponder();
99 }
100 
102  FloatParameterController::viewWillAppear();
103  for (int i = 0; i < m_law->numberOfParameter(); i++) {
104  contentView()->parameterDefinitionAtIndex(i)->setMessage(m_law->parameterDefinitionAtIndex(i));
105  }
106  contentView()->layoutSubviews();
107 }
108 
110  return 1+m_law->numberOfParameter();
111 }
112 
114  if (index == numberOfRows()-1) {
115  if (selectedRow() != numberOfRows()-1) {
116  cell->setHighlighted(false);
117  }
118  return;
119  }
121  myCell->setMessage(m_law->parameterNameAtIndex(index));
122  FloatParameterController::willDisplayCellForIndex(cell, index);
123 }
124 
125 HighlightCell * ParametersController::reusableParameterCell(int index, int type) {
126  assert(index >= 0);
127  assert(index < 2);
128  return m_menuListCell[index];
129 }
130 
131 int ParametersController::reusableParameterCellCount(int type) {
132  return m_law->numberOfParameter();
133 }
134 
135 double ParametersController::parameterAtIndex(int index) {
136  return m_law->parameterValueAtIndex(index);
137 }
138 
139 bool ParametersController::setParameterAtIndex(int parameterIndex, double f) {
140  if (!m_law->authorizedValueAtIndex(f, parameterIndex)) {
141  app()->displayWarning(I18n::Message::ForbiddenValue);
142  return false;
143  }
144  m_law->setParameterAtIndex(f, parameterIndex);
145  m_calculationController->setCalculationAccordingToIndex(0, true);
146  return true;
147 }
148 
149 bool ParametersController::textFieldDidFinishEditing(TextField * textField, const char * text, Ion::Events::Event event) {
150  if (FloatParameterController::textFieldDidFinishEditing(textField, text, event)) {
151  m_selectableTableView->reloadData();
152  return true;
153  }
154  return false;
155 }
156 
157 void ParametersController::buttonAction() {
159  stack->push(m_calculationController, KDColorWhite, Palette::SubTab, Palette::SubTab);
160 }
161 
162 I18n::Message ParametersController::okButtonText() {
163  return I18n::Message::Next;
164 }
165 
166 View * ParametersController::loadView() {
167  m_selectableTableView = (SelectableTableView *)FloatParameterController::loadView();
168  for (int i = 0; i < k_maxNumberOfCells; i++) {
169  m_menuListCell[i] = new MessageTableCellWithEditableText(m_selectableTableView, this, m_draftTextBuffer);
170  }
171  ContentView * contentView = (ContentView *)new ContentView(this, m_selectableTableView);
172  contentView->setNumberOfParameters(m_law->numberOfParameter());
173  return contentView;
174 }
175 
176 void ParametersController::unloadView(View * view) {
177  delete m_selectableTableView;
178  m_selectableTableView = nullptr;
179  for (int i = 0; i < k_maxNumberOfCells; i++) {
180  delete m_menuListCell[i];
181  m_menuListCell[i] = nullptr;
182  }
183  FloatParameterController::unloadView(view);
184 }
185 
186 SelectableTableView * ParametersController::selectableTableView() {
187  return m_selectableTableView;
188 }
189 
190 ParametersController::ContentView * ParametersController::contentView() {
191  return (ContentView *)view();
192 }
193 
194 }
195 
Definition: i18n.h:6
bool handleEvent(Ion::Events::Event event) override
#define assert(e)
Definition: assert.h:9
virtual I18n::Message parameterNameAtIndex(int index)=0
virtual I18n::Message title()=0
int16_t KDCoordinate
Definition: coordinate.h:6
void push(ViewController *vc, KDColor textColor=Palette::SubTab, KDColor backgroundColor=KDColorWhite, KDColor separatorColor=Palette::GreyBright)
virtual void setParameterAtIndex(float f, int index)=0
Snapshot * snapshot()
Definition: app.cpp:41
Definition: text.h:8
virtual int numberOfParameter()=0
virtual float parameterValueAtIndex(int index)=0
void setCalculationAccordingToIndex(int index, bool forceReinitialisation=false)
void reloadData(bool setFirstResponder=true)
enum Message uint16_t enum Language uint16_t const char * translate(Message m, Language l=(Language) 0)
Definition: i18n.cpp:5
constexpr KDColor KDColorWhite
Definition: color.h:42
static constexpr KDColor SubTab
Definition: palette.h:21
void displayWarning(I18n::Message warningMessage)
Definition: app.cpp:97
void willDisplayCellForIndex(HighlightCell *cell, int index) override
ParametersController(Responder *parentResponder, Law *m_law, CalculationController *calculationController)
static constexpr KDCoordinate CommonBottomMargin
Definition: metric.h:11
constexpr KDColor KDColorBlack
Definition: color.h:41
constexpr Event Left
Definition: events.h:61
virtual I18n::Message parameterDefinitionAtIndex(int index)=0
static constexpr KDColor WallScreen
Definition: palette.h:19
constexpr KDRect KDRectZero
Definition: rect.h:70
Definition: rect.h:26
void fillRect(KDRect rect, KDColor color)
Definition: context_rect.cpp:8
void setMessage(I18n::Message message)
Definition: view.h:23
static constexpr KDCoordinate CommonTopMargin
Definition: metric.h:10
static constexpr KDSize charSize(FontSize size=FontSize::Large)
Definition: text.h:16
App * app()
Definition: responder.cpp:77
void setActivePage(Page activePage)
Definition: app.cpp:62
virtual void setHighlighted(bool highlight)
virtual bool authorizedValueAtIndex(float x, int index) const =0
constexpr KDCoordinate height() const
Definition: size.h:11
Definition: palette.h:6