Numworks Epsilon  1.4.1
Graphing Calculator Operating System
list_parameter_controller.cpp
Go to the documentation of this file.
2 #include "list_controller.h"
3 #include "../app.h"
4 #include "../../apps_container.h"
5 
6 using namespace Poincare;
7 using namespace Shared;
8 
9 namespace Sequence {
10 
11 ListParameterController::ListParameterController(ListController * listController, SequenceStore * sequenceStore) :
12  Shared::ListParameterController(listController, sequenceStore, I18n::Message::SequenceColor, I18n::Message::DeleteSequence, this),
13  m_typeCell(I18n::Message::SequenceType),
14  m_initialRankCell(&m_selectableTableView, this, m_draftTextBuffer, I18n::Message::FirstTermIndex),
15  m_typeParameterController(this, sequenceStore, listController, TableCell::Layout::Horizontal, Metric::CommonTopMargin, Metric::CommonRightMargin,
16  Metric::CommonBottomMargin, Metric::CommonLeftMargin),
17  m_sequence(nullptr)
18 {
19 }
20 
22  return I18n::translate(I18n::Message::SequenceOptions);
23 }
24 
27  m_sequence = (Sequence *)function;
28 }
29 
31  bool hasAdditionalRow = hasInitialRankRow();
32 #if FUNCTION_COLOR_CHOICE
33  if (event == Ion::Events::OK || event == Ion::Events::EXE || (event == Ion::Events::Right && selectedRow() == 1)) {
34 #else
35  if (event == Ion::Events::OK || event == Ion::Events::EXE || (event == Ion::Events::Right && selectedRow() == 0)) {
36 #endif
37  int selectedRowIndex = selectedRow();
38 #if FUNCTION_COLOR_CHOICE
39  if (selectedRowIndex == 0) {
40  return handleEnterOnRow(selectedRowIndex);
41  }
42  if (selectedRowIndex == 1) {
43 #else
44  if (selectedRowIndex == 0) {
45 #endif
47  m_typeParameterController.setSequence(m_sequence);
48  stack->push(&m_typeParameterController);
49  return true;
50  }
51 #if FUNCTION_COLOR_CHOICE
52  if (selectedRowIndex == 2+hasAdditionalRow) {
53 
54 #else
55  if (selectedRowIndex == 1+hasAdditionalRow) {
56 #endif
57  return handleEnterOnRow(selectedRowIndex-hasAdditionalRow-1);
58  }
59 #if FUNCTION_COLOR_CHOICE
60  if (selectedRowIndex == 3+hasAdditionalRow) {
61 #else
62  if (selectedRowIndex == 2+hasAdditionalRow) {
63 #endif
66  static_cast<App *>(app())->localContext()->resetCache();
68  stack->pop();
69  return true;
70  }
71  }
72  }
73  return false;
74 }
75 
77  if (hasInitialRankRow()) {
78  return k_totalNumberOfCell;
79  }
80  return k_totalNumberOfCell-1;
81 };
82 
84  switch (index) {
85  /*case 0:
86  return Shared::ListParameterController::reusableCell(index);*/
87  case 0://1:
88  return &m_typeCell;
89  case 1:
90  if (hasInitialRankRow()) {
91  return &m_initialRankCell;
92  }
93  default:
94  return Shared::ListParameterController::reusableCell(index-1-hasInitialRankRow());
95  }
96 }
97 
99  return k_totalNumberOfCell;
100 }
101 
103  cell->setHighlighted(index == selectedRow()); // See FIXME in SelectableTableView::reloadData()
105  if (cell == &m_typeCell && m_sequence != nullptr) {
106  m_typeCell.setExpression(m_sequence->definitionName());
107  }
108  if (cell == &m_initialRankCell && m_sequence != nullptr) {
110  if (myCell->isEditing()) {
111  return;
112  }
114  Integer(m_sequence->initialRank()).writeTextInBuffer(buffer, Sequence::k_initialRankNumberOfDigits+1);
115  myCell->setAccessoryText(buffer);
116  }
117 }
118 
120  return event == Ion::Events::Down || event == Ion::Events::Up || TextFieldDelegate::textFieldShouldFinishEditing(textField, event);
121 }
122 
124  AppsContainer * appsContainer = ((TextFieldDelegateApp *)app())->container();
125  Context * globalContext = appsContainer->globalContext();
126  double floatBody = Expression::approximateToScalar<double>(text, *globalContext);
127  int index = std::round(floatBody);
128  if (std::isnan(floatBody) || std::isinf(floatBody)) {
129  app()->displayWarning(I18n::Message::UndefinedValue);
130  return false;
131  }
132  if (index < 0) {
133  app()->displayWarning(I18n::Message::ForbiddenValue);
134  return false;
135  }
136  m_sequence->setInitialRank(index);
137  // Invalidate sequence context cache when changing sequence type
138  static_cast<App *>(app())->localContext()->resetCache();
141  return true;
142 }
143 
144 void ListParameterController::tableViewDidChangeSelection(SelectableTableView * t, int previousSelectedCellX, int previousSelectedCellY) {
145  if (previousSelectedCellX == t->selectedColumn() && previousSelectedCellY == t->selectedRow()) {
146  return;
147  }
148  if (!hasInitialRankRow()) {
149  return;
150  }
151 #if FUNCTION_COLOR_CHOICE
152  if (previousSelectedCellY == 2) {
153 #else
154  if (previousSelectedCellY == 1) {
155 #endif
156  MessageTableCellWithEditableText * myCell = (MessageTableCellWithEditableText *)t->cellAtLocation(previousSelectedCellX, previousSelectedCellY);
157  myCell->setEditing(false);
159  }
160 #if FUNCTION_COLOR_CHOICE
161  if (t->selectedRow() == 2) {
162 #else
163  if (t->selectedRow() == 1) {
164 #endif
166  app()->setFirstResponder(myNewCell);
167  }
168 }
169 
171  return (TextFieldDelegateApp *)app();
172 }
173 
174 bool ListParameterController::hasInitialRankRow() {
175  return m_sequence && m_sequence->type() != Sequence::Type::Explicit;
176 }
177 
178 }
Definition: i18n.h:6
#define isinf(x)
Definition: math.h:44
static constexpr int k_initialRankNumberOfDigits
Definition: sequence.h:58
HighlightCell * selectedCell()
HighlightCell * reusableCell(int index) override
constexpr Event EXE
Definition: events.h:114
void push(ViewController *vc, KDColor textColor=Palette::SubTab, KDColor backgroundColor=KDColorWhite, KDColor separatorColor=Palette::GreyBright)
Responder * parentResponder() const
Definition: responder.cpp:12
Shared::TextFieldDelegateApp * textFieldDelegateApp() override
int initialRank() const
Definition: sequence.h:25
constexpr Event Up
Definition: events.h:62
constexpr Event Down
Definition: events.h:63
HighlightCell * cellAtLocation(int i, int j)
Definition: table_view.cpp:30
bool textFieldDidFinishEditing(TextField *textField, const char *text, Ion::Events::Event event) override
bool handleEvent(Ion::Events::Event event) override
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 reloadCellAtLocation(int i, int j)
Definition: table_view.cpp:49
#define round(x)
Definition: math.h:192
Poincare::Context * globalContext()
#define isnan(x)
Definition: math.h:43
void setFunction(Shared::Function *function) override
void willDisplayCellForIndex(HighlightCell *cell, int index) override
Poincare::ExpressionLayout * definitionName()
Definition: sequence.cpp:229
bool textFieldShouldFinishEditing(TextField *textField, Ion::Events::Event event) override
void willDisplayCellForIndex(HighlightCell *cell, int index) override
Definition: app.cpp:7
Definition: metric.h:6
HighlightCell * reusableCell(int index) override
void setFirstResponder(Responder *responder)
Definition: app.cpp:62
constexpr Event Right
Definition: events.h:64
virtual bool textFieldShouldFinishEditing(TextField *textField, Ion::Events::Event event)=0
virtual bool handleEvent(Ion::Events::Event event) override
void setExpression(Poincare::ExpressionLayout *expressionLayout)
void setInitialRank(int rank)
Definition: sequence.cpp:138
App * app()
Definition: responder.cpp:77
constexpr Event OK
Definition: events.h:65
virtual void setHighlighted(bool highlight)
void tableViewDidChangeSelection(SelectableTableView *t, int previousSelectedCellX, int previousSelectedCellY) override
virtual void removeFunction(Function *f)=0
virtual void setFunction(Function *function)