Blame view

build3/apps/shared/values_controller.cpp 9.64 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
  #include "values_controller.h"
  #include "text_field_delegate_app.h"
  #include "../constant.h"
  #include "../apps_container.h"
  #include <assert.h>
  
  using namespace Poincare;
  
  namespace Shared {
  
  ValuesController::ValuesController(Responder * parentResponder, ButtonRowController * header, I18n::Message parameterTitle, IntervalParameterController * intervalParameterController, Interval * interval) :
    EditableCellTableViewController(parentResponder),
    ButtonRowDelegate(header, nullptr),
    m_interval(interval),
    m_numberOfColumns(0),
    m_numberOfColumnsNeedUpdate(true),
    m_abscissaTitleCell(nullptr),
    m_abscissaCells{},
    m_abscissaParameterController(this, intervalParameterController, parameterTitle),
    m_setIntervalButton(this, I18n::Message::IntervalSet, Invocation([](void * context, void * sender) {
      ValuesController * valuesController = (ValuesController *) context;
      StackViewController * stack = ((StackViewController *)valuesController->stackController());
      stack->push(valuesController->intervalParameterController());
    }, this), KDText::FontSize::Small)
  {
  }
  
  const char * ValuesController::title() {
    return I18n::translate(I18n::Message::ValuesTab);
  }
  
  int ValuesController::numberOfColumns() {
    if (m_numberOfColumnsNeedUpdate) {
      updateNumberOfColumns();
      m_numberOfColumnsNeedUpdate = false;
    }
    return m_numberOfColumns;
  }
  
  Interval * ValuesController::interval() {
    return m_interval;
  }
  
  bool ValuesController::handleEvent(Ion::Events::Event event) {
    if (event == Ion::Events::Down) {
      if (selectedRow() == -1) {
        header()->setSelectedButton(-1);
        selectableTableView()->selectCellAtLocation(0,0);
        app()->setFirstResponder(selectableTableView());
        return true;
      }
      return false;
    }
  
    if (event == Ion::Events::Up) {
      if (selectedRow() == -1) {
        header()->setSelectedButton(-1);
        app()->setFirstResponder(tabController());
        return true;
      }
      selectableTableView()->deselectTable();
      header()->setSelectedButton(0);
      return true;
    }
    if (event == Ion::Events::Backspace && selectedRow() > 0 &&
        (selectedRow() < numberOfRows()-1 || m_interval->numberOfElements() == Interval::k_maxNumberOfElements)) {
      m_interval->deleteElementAtIndex(selectedRow()-1);
      selectableTableView()->reloadData();
      return true;
    }
    if (event == Ion::Events::OK || event == Ion::Events::EXE) {
      if (selectedRow() == -1) {
        return header()->handleEvent(event);
      }
      if (selectedRow() == 0) {
        if (selectedColumn() == 0) {
          configureAbscissa();
          return true;
        }
        configureFunction();
        return true;
      }
      return false;
    }
    if (selectedRow() == -1) {
      return header()->handleEvent(event);
    }
    if (event == Ion::Events::Copy && selectedRow() > 0 && selectedColumn() > 0) {
      EvenOddBufferTextCell * cell = (EvenOddBufferTextCell *)selectableTableView()->selectedCell();
      Clipboard::sharedClipboard()->store(cell->text());
      return true;
    }
    return false;
  }
  
  void ValuesController::didBecomeFirstResponder() {
    EditableCellTableViewController::didBecomeFirstResponder();
    if (selectedRow() == -1) {
      selectableTableView()->deselectTable();
      header()->setSelectedButton(0);
    } else {
      header()->setSelectedButton(-1);
    }
  }
  
  void ValuesController::willExitResponderChain(Responder * nextFirstResponder) {
    if (nextFirstResponder == tabController()) {
      selectableTableView()->deselectTable();
      selectableTableView()->scrollToCell(0,0);
      header()->setSelectedButton(-1);
    }
  }
  
  int ValuesController::numberOfButtons(ButtonRowController::Position) const {
    if (isEmpty()) {
      return 0;
    }
    return 1;
  }
  
  Button * ValuesController::buttonAtIndex(int index, ButtonRowController::Position position) const {
    return (Button *)&m_setIntervalButton;
  }
  
  void ValuesController::willDisplayCellAtLocation(HighlightCell * cell, int i, int j) {
    willDisplayCellAtLocationWithDisplayMode(cell, i, j, Expression::FloatDisplayMode::Default);
    if (cellAtLocationIsEditable(i, j)) {
      return;
    }
    // The cell is not a title cell and not editable
    if (j > 0 && i > 0) {
      char buffer[PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)];
      // Special case: last row
      if (j == numberOfRows() - 1) {
        int numberOfIntervalElements = m_interval->numberOfElements();
        if (numberOfIntervalElements < Interval::k_maxNumberOfElements) {
          buffer[0] = 0;
          EvenOddBufferTextCell * myValueCell = (EvenOddBufferTextCell *)cell;
          myValueCell->setText(buffer);
          return;
        }
      }
      // The cell is a value cell
      EvenOddBufferTextCell * myValueCell = (EvenOddBufferTextCell *)cell;
      double x = m_interval->element(j-1);
      Complex<double>::convertFloatToText(evaluationOfAbscissaAtColumn(x, i), buffer, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits);
    myValueCell->setText(buffer);
    }
  }
  
  KDCoordinate ValuesController::columnWidth(int i) {
    switch (i) {
      case 0:
        return k_abscissaCellWidth;
      default:
        return k_ordinateCellWidth;
    }
  }
  
  KDCoordinate ValuesController::cumulatedWidthFromIndex(int i) {
    if (i == 0) {
      return 0;
    } else {
      return k_abscissaCellWidth + (i-1)*k_ordinateCellWidth;
    }
  }
  
  int ValuesController::indexFromCumulatedWidth(KDCoordinate offsetX) {
    if (offsetX <= k_abscissaCellWidth) {
      return 0;
    }
    return (offsetX - k_abscissaCellWidth)/k_ordinateCellWidth+1;
  }
  
  HighlightCell * ValuesController::reusableCell(int index, int type) {
    assert(index >= 0);
    switch (type) {
      case 0:
        assert(index == 0);
        return m_abscissaTitleCell;
      case 1:
        return functionTitleCells(index);
      case 2:
        assert(index < k_maxNumberOfAbscissaCells);
        return m_abscissaCells[index];
      case 3:
        return floatCells(index);
      default:
        assert(false);
        return nullptr;
    }
  }
  
  int ValuesController::reusableCellCount(int type) {
    switch (type) {
      case 0:
        return 1;
      case 1:
        return maxNumberOfFunctions();
      case 2:
        return k_maxNumberOfAbscissaCells;
      case 3:
        return maxNumberOfCells();
      default:
        assert(false);
        return 0;
    }
  }
  
  int ValuesController::typeAtLocation(int i, int j) {
    if (j == 0) {
      if (i == 0) {
        return 0;
      }
      return 1;
    }
    if (i == 0) {
      return 2;
    }
    return 3;
  }
  
  bool ValuesController::isEmpty() const {
    if (functionStore()->numberOfActiveFunctions() == 0) {
      return true;
    }
    return false;
  }
  
  Responder * ValuesController::defaultController() {
    return tabController();
  }
  
  void ValuesController::viewWillAppear() {
    EditableCellTableViewController::viewWillAppear();
    header()->setSelectedButton(-1);
  }
  
  void ValuesController::viewDidDisappear() {
    m_numberOfColumnsNeedUpdate = true;
    EditableCellTableViewController::viewDidDisappear();
  }
  
  Function * ValuesController::functionAtColumn(int i) {
    assert(i > 0);
    return functionStore()->activeFunctionAtIndex(i-1);
  }
  
  Responder * ValuesController::tabController() const {
    return (parentResponder()->parentResponder()->parentResponder()->parentResponder());
  }
  
  StackViewController * ValuesController::stackController() const {
    return (StackViewController *)(parentResponder()->parentResponder()->parentResponder());
  }
  
  void ValuesController::configureAbscissa() {
    StackViewController * stack = stackController();
    stack->push(&m_abscissaParameterController);
  }
  
  void ValuesController::configureFunction() {
  #if COPY_COLUMN
  #else
    /* Temporary: the sequence value controller does not have a function parameter
     * controller yet but it shoult come soon. */
    if (functionParameterController() == nullptr) {
      return;
    }
  #endif
    functionParameterController()->setFunction(functionAtColumn(selectedColumn()));
    StackViewController * stack = stackController();
    stack->push(functionParameterController());
  }
  
  bool ValuesController::cellAtLocationIsEditable(int columnIndex, int rowIndex) {
    if (rowIndex > 0 && columnIndex == 0) {
      return true;
    }
    return false;
  }
  
  bool ValuesController::setDataAtLocation(double floatBody, int columnIndex, int rowIndex) {
    m_interval->setElement(rowIndex-1, floatBody);
    return true;
  }
  
  double ValuesController::dataAtLocation(int columnIndex, int rowIndex) {
    return m_interval->element(rowIndex-1);
  }
  
  int ValuesController::numberOfElements() {
    return m_interval->numberOfElements();
  }
  
  int ValuesController::maxNumberOfElements() const {
    return Interval::k_maxNumberOfElements;
  }
  
  double ValuesController::evaluationOfAbscissaAtColumn(double abscissa, int columnIndex) {
    Function * function = functionAtColumn(columnIndex);
    TextFieldDelegateApp * myApp = (TextFieldDelegateApp *)app();
    return function->evaluateAtAbscissa(abscissa, myApp->localContext());
  }
  
  View * ValuesController::loadView() {
    SelectableTableView * tableView = new SelectableTableView(this, this, 0, 0, k_topMargin, k_rightMargin, k_bottomMargin, k_leftMargin, this, nullptr, true, true, Palette::WallScreenDark, 13, Palette::GreyDark, Palette::GreyMiddle);
    m_abscissaTitleCell = new EvenOddMessageTextCell(KDText::FontSize::Small);
    for (int i = 0; i < k_maxNumberOfAbscissaCells; i++) {
      m_abscissaCells[i] = new EvenOddEditableTextCell(tableView, this, m_draftTextBuffer, KDText::FontSize::Small);
    }
    return tableView;
  }
  
  void ValuesController::unloadView(View * view) {
    delete m_abscissaTitleCell;
    m_abscissaTitleCell = nullptr;
    for (int i = 0; i < k_maxNumberOfAbscissaCells; i++) {
      delete m_abscissaCells[i];
      m_abscissaCells[i] = nullptr;
    }
    EditableCellTableViewController::unloadView(view);
  }
  
  void ValuesController::updateNumberOfColumns() {
      m_numberOfColumns = 1+functionStore()->numberOfActiveFunctions();
  }
  
  }