Numworks Epsilon  1.4.1
Graphing Calculator Operating System
histogram_parameter_controller.cpp
Go to the documentation of this file.
2 #include "app.h"
3 #include <assert.h>
4 #include <cmath>
5 
6 using namespace Shared;
7 
8 namespace Statistics {
9 
10 HistogramParameterController::HistogramParameterController(Responder * parentResponder, Store * store) :
11  FloatParameterController(parentResponder),
12  m_cells{},
13  m_store(store)
14 {
15 }
16 
18  return I18n::translate(I18n::Message::HistogramSet);
19 }
20 
22  return 1+k_numberOfCells;
23 }
24 
26  if (index == numberOfRows()-1) {
27  return;
28  }
30  I18n::Message labels[k_numberOfCells] = {I18n::Message::RectangleWidth, I18n::Message::BarStart};
31  myCell->setMessage(labels[index]);
32  FloatParameterController::willDisplayCellForIndex(cell, index);
33 }
34 
35 double HistogramParameterController::parameterAtIndex(int index) {
36  assert(index >= 0 && index < k_numberOfCells);
37  if (index == 0) {
38  return m_store->barWidth();
39  }
40  return m_store->firstDrawnBarAbscissa();
41 }
42 
43 bool HistogramParameterController::setParameterAtIndex(int parameterIndex, double f) {
44  assert(parameterIndex >= 0 && parameterIndex < k_numberOfCells);
45  if (parameterIndex == 0) {
46  double newNumberOfBars = std::ceil((m_store->maxValue() - m_store->minValue())/f);
47  if (f <= 0.0f || newNumberOfBars > Store::k_maxNumberOfBars || m_store->firstDrawnBarAbscissa() > m_store->maxValue()+f) {
48  app()->displayWarning(I18n::Message::ForbiddenValue);
49  return false;
50  }
51  m_store->setBarWidth(f);
52  } else {
53  double newNumberOfBars = ceilf((m_store->maxValue() - f)/m_store->barWidth());
54  if (newNumberOfBars > Store::k_maxNumberOfBars || f > m_store->maxValue()+m_store->barWidth()) {
55  app()->displayWarning(I18n::Message::ForbiddenValue);
56  return false;
57  }
58  m_store->setFirstDrawnBarAbscissa(f);
59  }
60  return true;
61 }
62 
63 HighlightCell * HistogramParameterController::reusableParameterCell(int index, int type) {
64  assert(index >= 0 && index < k_numberOfCells);
65  return m_cells[index];
66 }
67 
68 int HistogramParameterController::reusableParameterCellCount(int type) {
69  return k_numberOfCells;
70 }
71 
72 View * HistogramParameterController::loadView() {
73  SelectableTableView * tableView = (SelectableTableView *)FloatParameterController::loadView();
74  for (int i = 0; i < k_numberOfCells; i++) {
75  m_cells[i] = new MessageTableCellWithEditableText(tableView, this, m_draftTextBuffer, I18n::Message::Default);
76  }
77  return tableView;
78 }
79 
80 void HistogramParameterController::unloadView(View * view) {
81  for (int i = 0; i < k_numberOfCells; i++) {
82  delete m_cells[i];
83  m_cells[i] = nullptr;
84  }
85  FloatParameterController::unloadView(view);
86 }
87 
88 }
89 
void setFirstDrawnBarAbscissa(double firstDrawnBarAbscissa)
Definition: store.cpp:44
#define assert(e)
Definition: assert.h:9
double maxValue()
Definition: store.cpp:98
double barWidth()
Definition: store.cpp:29
double firstDrawnBarAbscissa()
Definition: store.cpp:40
double minValue()
Definition: store.cpp:108
enum Message uint16_t enum Language uint16_t const char * translate(Message m, Language l=(Language) 0)
Definition: i18n.cpp:5
void displayWarning(I18n::Message warningMessage)
Definition: app.cpp:97
void setBarWidth(double barWidth)
Definition: store.cpp:33
#define ceil(x)
Definition: math.h:170
static constexpr double k_maxNumberOfBars
Definition: store.h:41
void willDisplayCellForIndex(HighlightCell *cell, int index) override
void setMessage(I18n::Message message)
Definition: view.h:23
App * app()
Definition: responder.cpp:77
#define ceilf(x)
Definition: math.h:135