Blame view

epsilon-master/apps/statistics/calculation_controller.cpp 7.3 KB
6663b6c9   adorian   projet complet av...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  #include "calculation_controller.h"
  #include "app.h"
  #include "calculation_selectable_table_view.h"
  #include "../constant.h"
  #include "../apps_container.h"
  #include "../shared/poincare_helpers.h"
  #include <poincare.h>
  #include <assert.h>
  
  using namespace Shared;
  using namespace Poincare;
  
  namespace Statistics {
  
  CalculationController::CalculationController(Responder * parentResponder, ButtonRowController * header, Store * store) :
    TabTableController(parentResponder, this),
    ButtonRowDelegate(header, nullptr),
    m_seriesTitleCells{},
    m_calculationTitleCells{},
    m_calculationCells{},
    m_hideableCell(nullptr),
    m_store(store)
  {
  }
  
  // AlternateEmptyViewDelegate
  
  bool CalculationController::isEmpty() const {
    return m_store->isEmpty();
  }
  
  I18n::Message CalculationController::emptyMessage() {
    return I18n::Message::NoValueToCompute;
  }
  
  Responder * CalculationController::defaultController() {
    return tabController();
  }
  
  // TableViewDataSource
  
  int CalculationController::numberOfColumns() {
    return 1 + m_store->numberOfNonEmptySeries();
  }
  
  void CalculationController::willDisplayCellAtLocation(HighlightCell * cell, int i, int j) {
    EvenOddCell * evenOddCell = (EvenOddCell *)cell;
    evenOddCell->setEven(j%2 == 0);
    evenOddCell->setHighlighted(i == selectedColumn() && j == selectedRow());
    if (i == 0 && j == 0) {
      return;
    }
    if (j == 0) {
      // Display a series title cell
      int seriesNumber = m_store->indexOfKthNonEmptySeries(i-1);
      char titleBuffer[] = {'V', static_cast<char>('1'+seriesNumber), '/', 'N', static_cast<char>('1'+seriesNumber), 0};
      StoreTitleCell * storeTitleCell = static_cast<StoreTitleCell *>(cell);
      storeTitleCell->setText(titleBuffer);
      storeTitleCell->setColor(DoublePairStore::colorOfSeriesAtIndex(seriesNumber));
      return;
    }
    if (i == 0) {
      // Display a calculation title cell
      I18n::Message titles[k_totalNumberOfRows] = {
        I18n::Message::TotalSize,
        I18n::Message::Minimum,
        I18n::Message::Maximum,
        I18n::Message::Range,
        I18n::Message::Mean,
        I18n::Message::StandardDeviationSigma,
        I18n::Message::Deviation,
        I18n::Message::FirstQuartile,
        I18n::Message::ThirdQuartile,
        I18n::Message::Median,
        I18n::Message::InterquartileRange,
        I18n::Message::Sum,
        I18n::Message::SquareSum,
        I18n::Message::SampleStandardDeviationS};
      MarginEvenOddMessageTextCell * calcTitleCell = static_cast<MarginEvenOddMessageTextCell *>(cell);
      calcTitleCell->setMessage(titles[j-1]);
      return;
    }
    // Display a calculation cell
    CalculPointer calculationMethods[k_totalNumberOfRows] = {&Store::sumOfOccurrences, &Store::minValue,
      &Store::maxValue, &Store::range, &Store::mean, &Store::standardDeviation, &Store::variance, &Store::firstQuartile,
      &Store::thirdQuartile, &Store::median, &Store::quartileRange, &Store::sum, &Store::squaredValueSum, &Store::sampleStandardDeviation};
    int seriesIndex = m_store->indexOfKthNonEmptySeries(i-1);
    double calculation = (m_store->*calculationMethods[j-1])(seriesIndex);
    EvenOddBufferTextCell * calculationCell = static_cast<EvenOddBufferTextCell *>(cell);
    char buffer[PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)];
    PoincareHelpers::ConvertFloatToText<double>(calculation, buffer, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits);
    calculationCell->setText(buffer);
  }
  
  KDCoordinate CalculationController::columnWidth(int i) {
    return i == 0 ? k_calculationTitleCellWidth : k_calculationCellWidth;
  }
  
  KDCoordinate CalculationController::cumulatedHeightFromIndex(int j) {
    return j*rowHeight(0);
  }
  
  int CalculationController::indexFromCumulatedHeight(KDCoordinate offsetY) {
    return (offsetY-1) / rowHeight(0);
  }
  
  HighlightCell * CalculationController::reusableCell(int index, int type) {
    assert(index >= 0 && index < reusableCellCount(type));
    if (type == k_hideableCellType) {
      return m_hideableCell;
    }
    if (type == k_calculationTitleCellType) {
      return static_cast<HighlightCell *>(m_calculationTitleCells[index]);
    }
    if (type == k_seriesTitleCellType) {
      return static_cast<HighlightCell *>(m_seriesTitleCells[index]);
    }
    assert(type == k_calculationCellType);
    return static_cast<HighlightCell *>(m_calculationCells[index]);
  }
  
  int CalculationController::reusableCellCount(int type) {
    if (type == k_hideableCellType) {
      return 1;
    }
    if (type == k_calculationTitleCellType) {
      return k_numberOfCalculationTitleCells;
    }
    if (type == k_seriesTitleCellType) {
      return k_numberOfSeriesTitleCells;
    }
    assert(type == k_calculationCellType);
    return k_numberOfCalculationCells;
  }
  
  int CalculationController::typeAtLocation(int i, int j) {
    assert(i >= 0 && i < numberOfColumns());
    assert(j >= 0 && j < numberOfRows());
    if (i == 0 && j == 0) {
      return k_hideableCellType;
    }
    if (i == 0) {
      return k_calculationTitleCellType;
    }
    if (j == 0) {
      return k_seriesTitleCellType;
    }
    return k_calculationCellType;
  }
  
  // ViewController
  const char * CalculationController::title() {
    return I18n::translate(I18n::Message::StatTab);
  }
  
  // Responder
  bool CalculationController::handleEvent(Ion::Events::Event event) {
    if (event == Ion::Events::Up) {
      selectableTableView()->deselectTable();
      app()->setFirstResponder(tabController());
      return true;
    }
    return false;
  }
  
  void CalculationController::didBecomeFirstResponder() {
    if (selectedRow() == -1) {
      selectCellAtLocation(0, 1);
    } else {
      selectCellAtLocation(selectedColumn(), selectedRow());
    }
    TabTableController::didBecomeFirstResponder();
  }
  
  // Private
  
  Responder * CalculationController::tabController() const {
    return (parentResponder()->parentResponder()->parentResponder());
  }
  
  View * CalculationController::loadView() {
    for (int i = 0; i < k_numberOfSeriesTitleCells; i++) {
      m_seriesTitleCells[i] = new StoreTitleCell();
      m_seriesTitleCells[i]->setSeparatorLeft(true);
    }
    for (int i = 0; i < k_numberOfCalculationTitleCells; i++) {
      m_calculationTitleCells[i] = new MarginEvenOddMessageTextCell(KDText::FontSize::Small);
      m_calculationTitleCells[i]->setAlignment(1.0f, 0.5f);
    }
    for (int i = 0; i < k_numberOfCalculationCells; i++) {
      m_calculationCells[i] = new SeparatorEvenOddBufferTextCell(KDText::FontSize::Small);
      m_calculationCells[i]->setTextColor(Palette::GreyDark);
    }
    m_hideableCell = new HideableEvenOddCell();
    m_hideableCell->setHide(true);
  
    CalculationSelectableTableView * selectableTableView = new CalculationSelectableTableView(this, this, this);
    selectableTableView->setBackgroundColor(Palette::WallScreenDark);
    selectableTableView->setVerticalCellOverlap(0);
    selectableTableView->setMargins(k_margin, k_scrollBarMargin, k_scrollBarMargin, k_margin);
    return selectableTableView;
  }
  
  
  void CalculationController::unloadView(View * view) {
    for (int i = 0; i < k_numberOfSeriesTitleCells; i++) {
      delete m_seriesTitleCells[i];
      m_seriesTitleCells[i] = nullptr;
    }
    for (int i = 0; i < k_numberOfCalculationTitleCells; i++) {
      delete m_calculationTitleCells[i];
      m_calculationTitleCells[i] = nullptr;
    }
    for (int i = 0; i < k_numberOfCalculationCells; i++) {
      delete m_calculationCells[i];
      m_calculationCells[i] = nullptr;
    }
    delete m_hideableCell;
    m_hideableCell = nullptr;
    TabTableController::unloadView(view);
  }
  
  }