Blame view

build1/epsilon-master/apps/variable_box_controller.cpp 9.32 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
  #include "variable_box_controller.h"
  #include "shared/poincare_helpers.h"
  #include "constant.h"
  #include <escher/metric.h>
  #include <assert.h>
  
  using namespace Poincare;
  using namespace Shared;
  
  /* ContentViewController */
  
  VariableBoxController::ContentViewController::ContentViewController(Responder * parentResponder, GlobalContext * context) :
    ViewController(parentResponder),
    m_context(context),
    m_sender(nullptr),
    m_firstSelectedRow(0),
    m_previousSelectedRow(0),
    m_currentPage(Page::RootMenu),
    m_selectableTableView(this)
  {
    m_selectableTableView.setMargins(0);
    m_selectableTableView.setShowsIndicators(false);
  }
  
  View * VariableBoxController::ContentViewController::view() {
    return &m_selectableTableView;
  }
  
  const char * VariableBoxController::ContentViewController::title() {
    return I18n::translate(I18n::Message::Variables);
  }
  void VariableBoxController::ContentViewController::didBecomeFirstResponder() {
    m_selectableTableView.reloadData();
    m_selectableTableView.scrollToCell(0,0);
    selectCellAtLocation(0, m_firstSelectedRow);
    app()->setFirstResponder(&m_selectableTableView);
  }
  
  bool VariableBoxController::ContentViewController::handleEvent(Ion::Events::Event event) {
  #if MATRIX_VARIABLES
    if (event == Ion::Events::Back && m_currentPage == Page::RootMenu) {
  #else
    if (event == Ion::Events::Back && m_currentPage == Page::Scalar) {
  #endif
      m_firstSelectedRow = 0;
      app()->dismissModalViewController();
      return true;
    }
    if (event == Ion::Events::Back || event == Ion::Events::Left) {
      m_firstSelectedRow = m_previousSelectedRow;
  #if MATRIX_VARIABLES
      m_selectableTableView.deselectTable();
      m_currentPage = Page::RootMenu;
  #endif
      app()->setFirstResponder(this);
      return true;
    }
    if (event == Ion::Events::OK || event == Ion::Events::EXE || (event == Ion::Events::Right && m_currentPage == Page::RootMenu)) {
    if (m_currentPage == Page::RootMenu) {
        m_previousSelectedRow = selectedRow();
        m_firstSelectedRow = 0;
        m_selectableTableView.deselectTable();
        m_currentPage = pageAtIndex(m_previousSelectedRow);
        app()->setFirstResponder(this);
        return true;
      }
      m_firstSelectedRow = 0;
      char label[3];
      putLabelAtIndexInBuffer(selectedRow(), label);
      const char * editedText = label;
      m_sender->handleEventWithText(editedText);
  #if MATRIX_VARIABLES
      m_selectableTableView.deselectTable();
      m_currentPage = Page::RootMenu;
  #endif
      app()->dismissModalViewController();
      return true;
    }
    if (event == Ion::Events::Backspace && m_currentPage != Page::RootMenu) {
      if (m_currentPage == Page::Scalar) {
        const Symbol symbol('A'+selectedRow());
        m_context->setExpressionForSymbolName(nullptr, &symbol, *m_context);
      }
      if (m_currentPage == Page::Matrix) {
        const Symbol symbol = Symbol::matrixSymbol('0'+(char)selectedRow());
        m_context->setExpressionForSymbolName(nullptr, &symbol, *m_context);
      }
      m_selectableTableView.reloadData();
      return true;
    }
    return false;
  }
  
  int VariableBoxController::ContentViewController::numberOfRows() {
    switch (m_currentPage) {
      case Page::RootMenu:
        return k_numberOfMenuRows;
      case Page::Scalar:
        return GlobalContext::k_maxNumberOfScalarExpressions;
  #if LIST_VARIABLES
      case Page::List:
        return GlobalContext::k_maxNumberOfListExpressions;
  #endif
      case Page::Matrix:
        return GlobalContext::k_maxNumberOfMatrixExpressions;
      default:
        return 0;
    }
  }
  
  HighlightCell * VariableBoxController::ContentViewController::reusableCell(int index, int type) {
    assert(type < 2);
    assert(index >= 0);
    if (type == 0) {
      assert(index < k_maxNumberOfDisplayedRows);
      return &m_leafCells[index];
    }
    assert(index < k_numberOfMenuRows);
    return &m_nodeCells[index];
  }
  
  int VariableBoxController::ContentViewController::reusableCellCount(int type) {
    assert(type < 2);
    if (type == 0) {
      return k_maxNumberOfDisplayedRows;
    }
    return k_numberOfMenuRows;
  }
  
  void VariableBoxController::ContentViewController::willDisplayCellForIndex(HighlightCell * cell, int index) {
    if (m_currentPage == Page::RootMenu) {
      I18n::Message label = nodeLabelAtIndex(index);
      MessageTableCell * myCell = (MessageTableCell *)cell;
      myCell->setMessage(label);
      return;
    }
    VariableBoxLeafCell * myCell = (VariableBoxLeafCell *)cell;
    char label[3];
    putLabelAtIndexInBuffer(index, label);
    myCell->setLabel(label);
    const Expression * evaluation = expressionForIndex(index);
    if (m_currentPage == Page::Scalar) {
      myCell->displayExpression(false);
      char buffer[PrintFloat::k_maxComplexBufferLength];
      PoincareHelpers::WriteTextInBuffer(evaluation, buffer, PrintFloat::k_maxComplexBufferLength, Constant::ShortNumberOfSignificantDigits);
      myCell->setSubtitle(buffer);
      return;
    }
    myCell->displayExpression(true);
    if (evaluation) {
      /* TODO: implement list contexts */
      // TODO: handle matrix and scalar!
      ExpressionLayout * layout = expressionLayoutForIndex(index);
      const Matrix * matrixEvaluation = static_cast<const Matrix *>(evaluation);
      myCell->setExpressionLayout(layout);
      char buffer[2*PrintFloat::bufferSizeForFloatsWithPrecision(2)+1];
      int numberOfChars = PrintFloat::convertFloatToText<float>(matrixEvaluation->numberOfRows(), buffer, PrintFloat::bufferSizeForFloatsWithPrecision(2), 2, PrintFloat::Mode::Decimal);
      buffer[numberOfChars++] = 'x';
      PrintFloat::convertFloatToText<float>(matrixEvaluation->numberOfColumns(), buffer+numberOfChars, PrintFloat::bufferSizeForFloatsWithPrecision(2), 2, PrintFloat::Mode::Decimal);
      myCell->setSubtitle(buffer);
    } else {
      myCell->setExpressionLayout(nullptr);
      myCell->setSubtitle(I18n::translate(I18n::Message::Empty));
    }
  }
  
  KDCoordinate VariableBoxController::ContentViewController::rowHeight(int index) {
    if (m_currentPage == Page::RootMenu || m_currentPage == Page::Scalar) {
      return Metric::ToolboxRowHeight;
    }
    ExpressionLayout * expressionLayout = expressionLayoutForIndex(index);
    if (expressionLayout) {
      return max(expressionLayout->size().height()+k_leafMargin, Metric::ToolboxRowHeight);
    }
    return Metric::ToolboxRowHeight;
  }
  
  int VariableBoxController::ContentViewController::typeAtLocation(int i, int j) {
    if (m_currentPage == Page::RootMenu) {
      return 1;
    }
    return 0;
  }
  
  void VariableBoxController::ContentViewController::reloadData() {
    m_selectableTableView.reloadData();
  }
  
  void VariableBoxController::ContentViewController::resetPage() {
  #if MATRIX_VARIABLES
    m_currentPage = Page::RootMenu;
  #else
    m_currentPage = Page::Scalar;
  #endif
  }
  
  void VariableBoxController::ContentViewController::viewDidDisappear() {
    m_selectableTableView.deselectTable();
    ViewController::viewDidDisappear();
  }
  
  VariableBoxController::ContentViewController::Page VariableBoxController::ContentViewController::pageAtIndex(int index) {
  #if LIST_VARIABLES
    Page pages[3] = {Page::Scalar, Page::List, Page::Matrix};
  #else
    Page pages[2] = {Page::Scalar, Page::Matrix};
  #endif
    return pages[index];
  }
  
  void VariableBoxController::ContentViewController::putLabelAtIndexInBuffer(int index, char * buffer) {
    if (m_currentPage == Page::Scalar) {
      buffer[0] = 'A' + index;
      buffer[1] = 0;
    }
  #if LIST_VARIABLES
    if (m_currentPage == Page::List) {
      buffer[0] = 'L';
      buffer[1] = '0' + index;
      buffer[2] = 0;
    }
  #endif
    if (m_currentPage == Page::Matrix) {
      buffer[0] = 'M';
      buffer[1] = '0' + index;
      buffer[2] = 0;
    }
  }
  
  I18n::Message VariableBoxController::ContentViewController::nodeLabelAtIndex(int index) {
    assert(m_currentPage == Page::RootMenu);
  #if LIST_VARIABLES
    I18n::Message labels[3] = {I18n::Message::Number, I18n::Message::List, I18n::Message::Matrix};
  #else
    I18n::Message labels[2] = {I18n::Message::Number, I18n::Message::Matrix};
  #endif
    return labels[index];
  }
  
  const Expression * VariableBoxController::ContentViewController::expressionForIndex(int index) {
    if (m_currentPage == Page::Scalar) {
      const Symbol symbol = Symbol('A'+index);
      return m_context->expressionForSymbol(&symbol);
    }
    if (m_currentPage == Page::Matrix) {
      const Symbol symbol = Symbol::matrixSymbol('0'+(char)index);
      return m_context->expressionForSymbol(&symbol);
    }
  #if LIST_VARIABLES
    if (m_currentPage == Page::List) {
      return nullptr;
    }
  #endif
    return nullptr;
  }
  
  ExpressionLayout * VariableBoxController::ContentViewController::expressionLayoutForIndex(int index) {
    if (m_currentPage == Page::Matrix) {
      const Symbol symbol = Symbol::matrixSymbol('0'+(char)index);
      return m_context->expressionLayoutForSymbol(&symbol, Constant::ShortNumberOfSignificantDigits);
    }
  #if LIST_VARIABLES
    if (m_currentPage == Page::List) {
      return nullptr;
    }
  #endif
    return nullptr;
  }
  
  VariableBoxController::VariableBoxController(GlobalContext * context) :
    StackViewController(nullptr, &m_contentViewController, KDColorWhite, Palette::PurpleBright, Palette::PurpleDark),
    m_contentViewController(this, context)
  {
  }
  
  void VariableBoxController::didBecomeFirstResponder() {
    app()->setFirstResponder(&m_contentViewController);
  }
  
  void VariableBoxController::setSender(Responder * sender) {
    m_contentViewController.setSender(sender);
  }
  
  void VariableBoxController::viewWillAppear() {
    StackViewController::viewWillAppear();
    m_contentViewController.resetPage();
    m_contentViewController.reloadData();
  }
  
  void VariableBoxController::viewDidDisappear() {
    StackViewController::viewDidDisappear();
  }