Numworks Epsilon  1.4.1
Graphing Calculator Operating System
law_controller.cpp
Go to the documentation of this file.
1 #include "law_controller.h"
2 #include <assert.h>
3 #include <new>
4 #include "app.h"
5 #include "law/binomial_law.h"
6 #include "law/exponential_law.h"
7 #include "law/normal_law.h"
8 #include "law/poisson_law.h"
9 #include "law/uniform_law.h"
10 #include "images/binomial_icon.h"
11 #include "images/exponential_icon.h"
12 #include "images/normal_icon.h"
13 #include "images/poisson_icon.h"
14 #include "images/uniform_icon.h"
15 #include "images/focused_binomial_icon.h"
16 #include "images/focused_exponential_icon.h"
17 #include "images/focused_normal_icon.h"
18 #include "images/focused_poisson_icon.h"
19 #include "images/focused_uniform_icon.h"
20 
21 namespace Probability {
22 
23 LawController::ContentView::ContentView(SelectableTableView * selectableTableView) :
24  m_titleView(KDText::FontSize::Small, I18n::Message::ChooseLaw, 0.5f, 0.5f, Palette::GreyDark, Palette::WallScreen),
25  m_selectableTableView(selectableTableView)
26 {
27 }
28 
29 int LawController::ContentView::numberOfSubviews() const {
30  return 2;
31 }
32 
33 View * LawController::ContentView::subviewAtIndex(int index) {
34  assert(index >= 0 && index < 2);
35  if (index == 0) {
36  return &m_titleView;
37  }
38  return m_selectableTableView;
39 }
40 
41 void LawController::ContentView::layoutSubviews() {
42  KDCoordinate titleHeight = KDText::charSize(KDText::FontSize::Small).height()+k_titleMargin;
43  m_titleView.setFrame(KDRect(0, 0, bounds().width(), titleHeight));
44  m_selectableTableView->setFrame(KDRect(0, titleHeight, bounds().width(), bounds().height()-titleHeight));
45 }
46 
47 static I18n::Message sMessages[] = {
48  I18n::Message::Binomial,
49  I18n::Message::Uniforme,
51  I18n::Message::Normal,
52  I18n::Message::Poisson
53 };
54 
55 LawController::LawController(Responder * parentResponder, Law * law, ParametersController * parametersController) :
56  ViewController(parentResponder),
57  m_selectableTableView(this),
58  m_contentView(&m_selectableTableView),
59  m_law(law),
60  m_parametersController(parametersController)
61 {
62  assert(m_law != nullptr);
63  m_messages = sMessages;
64  m_selectableTableView.setTopMargin(Metric::CommonTopMargin-ContentView::k_titleMargin);
65 }
66 
68  return &m_contentView;
69 }
70 
72  selectRow((int)m_law->type());
73 }
74 
76  App::Snapshot * snapshot = (App::Snapshot *)app()->snapshot();
78  if (selectedRow() == -1) {
79  selectCellAtLocation(0, 0);
80  } else {
81  selectCellAtLocation(selectedColumn(), selectedRow());
82  }
83  app()->setFirstResponder(&m_selectableTableView);
84 }
85 
87  if (event == Ion::Events::OK || event == Ion::Events::EXE || event == Ion::Events::Right) {
88  StackViewController * stack = (StackViewController *)parentResponder();
89  setLawAccordingToIndex(selectedRow());
90  stack->push(m_parametersController, KDColorWhite, Palette::PurpleBright, Palette::PurpleBright);
91  return true;
92  }
93  return false;
94 }
95 
97  return k_totalNumberOfModels;
98 };
99 
101  assert(index >= 0);
102  assert(index < k_totalNumberOfModels);
103  return &m_cells[index];
104 }
105 
107  return k_totalNumberOfModels;
108 }
109 
111  Cell * myCell = (Cell *)cell;
112  myCell->setLabel(m_messages[index]);
113  const Image * images[5] = {ImageStore::BinomialIcon, ImageStore::UniformIcon, ImageStore::ExponentialIcon,
114  ImageStore::NormalIcon, ImageStore::PoissonIcon};
115  const Image * focusedImages[5] = {ImageStore::FocusedBinomialIcon, ImageStore::FocusedUniformIcon, ImageStore::FocusedExponentialIcon,
116  ImageStore::FocusedNormalIcon, ImageStore::FocusedPoissonIcon};
117  myCell->setImage(images[index], focusedImages[index]);
118  myCell->reloadCell();
119 }
120 
123 }
124 
125 void Probability::LawController::setLawAccordingToIndex(int index) {
126  if ((int)m_law->type() == index) {
127  return;
128  }
129  m_law->~Law();
130  switch (index) {
131  case 0:
132  new(m_law) BinomialLaw();
133  break;
134  case 1:
135  new(m_law) UniformLaw();
136  break;
137  case 2:
138  new(m_law) ExponentialLaw();
139  break;
140  case 3:
141  new(m_law) NormalLaw();
142  break;
143  case 4:
144  new(m_law) PoissonLaw();
145  break;
146  default:
147  return;
148  }
149  m_parametersController->reinitCalculation();
150 }
151 
152 }
Definition: i18n.h:6
#define assert(e)
Definition: assert.h:9
KDCoordinate cellHeight() override
constexpr Event EXE
Definition: events.h:114
static constexpr KDColor PurpleBright
Definition: palette.h:10
int16_t KDCoordinate
Definition: coordinate.h:6
LawController(Responder *parentResponder, Law *m_law, ParametersController *parametersController)
void push(ViewController *vc, KDColor textColor=Palette::SubTab, KDColor backgroundColor=KDColorWhite, KDColor separatorColor=Palette::GreyBright)
void didBecomeFirstResponder() override
Definition: image.h:6
Definition: text.h:8
bool handleEvent(Ion::Events::Event event) override
void reloadCell() override
Definition: cell.cpp:37
constexpr KDColor KDColorWhite
Definition: color.h:42
void setTopMargin(KDCoordinate m)
Definition: scroll_view.h:13
void setImage(const Image *image, const Image *focusedImage)
Definition: cell.cpp:52
void setLabel(I18n::Message message)
Definition: cell.cpp:48
void willDisplayCellForIndex(HighlightCell *cell, int index) override
Definition: rect.h:26
static constexpr KDCoordinate ParameterCellHeight
Definition: metric.h:15
HighlightCell * reusableCell(int index) override
Definition: view.h:23
constexpr Event Right
Definition: events.h:64
void viewWillAppear() override
static constexpr KDCoordinate CommonTopMargin
Definition: metric.h:10
static constexpr KDSize charSize(FontSize size=FontSize::Large)
Definition: text.h:16
void setActivePage(Page activePage)
Definition: app.cpp:62
constexpr Event OK
Definition: events.h:65
constexpr KDCoordinate height() const
Definition: size.h:11
Definition: palette.h:6