sub_controller.cpp
9.46 KB
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
#include "sub_controller.h"
#include "helpers.h"
#include "../global_preferences.h"
#include "../apps_container.h"
#include "../shared/poincare_helpers.h"
#include <assert.h>
#include <cmath>
using namespace Poincare;
using namespace Shared;
namespace Settings {
SubController::SubController(Responder * parentResponder) :
ViewController(parentResponder),
m_editableCell(&m_selectableTableView, this, m_draftTextBuffer),
m_selectableTableView(this),
m_messageTreeModel(nullptr)
{
m_selectableTableView.setTopMargin(k_topBottomMargin);
m_selectableTableView.setBottomMargin(k_topBottomMargin);
for (int i = 0; i < k_totalNumberOfCell; i++) {
m_cells[i].setMessageFontSize(KDText::FontSize::Large);
m_cells[i].setAccessoryFontSize(KDText::FontSize::Small);
m_cells[i].setAccessoryTextColor(Palette::GreyDark);
}
m_complexFormatLayout[0] = Helpers::CartesianComplexFormat(KDText::FontSize::Large);
m_complexFormatLayout[1] = Helpers::PolarComplexFormat(KDText::FontSize::Large);
for (int i = 0; i < 2; i++) {
m_complexFormatCells[i].setExpressionLayout(m_complexFormatLayout[i]);
}
m_editableCell.setMessage(I18n::Message::SignificantFigures);
m_editableCell.setMessageFontSize(KDText::FontSize::Large);
}
SubController::~SubController() {
for (int i = 0; i < 2; i++) {
if (m_complexFormatLayout[i]) {
delete m_complexFormatLayout[i];
m_complexFormatLayout[i] = nullptr;
}
}
}
const char * SubController::title() {
if (m_messageTreeModel) {
return I18n::translate(m_messageTreeModel->label());
}
return "";
}
View * SubController::view() {
return &m_selectableTableView;
}
void SubController::didEnterResponderChain(Responder * previousFirstResponder) {
if (m_messageTreeModel->label() == I18n::Message::ExamMode) {
m_selectableTableView.reloadData();
}
}
void SubController::didBecomeFirstResponder() {
selectCellAtLocation(0, valueIndexForPreference(m_messageTreeModel->label()));
app()->setFirstResponder(&m_selectableTableView);
}
bool SubController::handleEvent(Ion::Events::Event event) {
/* We hide here the activation hardware test app: in the menu "about", by
* clicking on '6' on the last row. */
if ((event == Ion::Events::Six || event == Ion::Events::LowerT || event == Ion::Events::UpperT) && m_messageTreeModel->label() == I18n::Message::About && selectedRow() == numberOfRows()-1) {
app()->displayModalViewController(&m_hardwareTestPopUpController, 0.f, 0.f, Metric::ExamPopUpTopMargin, Metric::PopUpRightMargin, Metric::ExamPopUpBottomMargin, Metric::PopUpLeftMargin);
return true;
}
if (event == Ion::Events::OK || event == Ion::Events::EXE) {
/* Behaviour of "Exam mode" menu*/
if (m_messageTreeModel->label() == I18n::Message::ExamMode) {
if (GlobalPreferences::sharedGlobalPreferences()->examMode() == GlobalPreferences::ExamMode::Activate) {
return false;
}
AppsContainer * container = (AppsContainer *)(app()->container());
container->displayExamModePopUp(true);
return true;
}
/* Behaviour of "About" menu */
if (m_messageTreeModel->label() == I18n::Message::About) {
if (selectedRow() == 0) {
MessageTableCellWithBuffer * myCell = (MessageTableCellWithBuffer *)m_selectableTableView.selectedCell();
if (strcmp(myCell->accessoryText(), Ion::patchLevel()) == 0) {
myCell->setAccessoryText(Ion::softwareVersion());
return true;
}
myCell->setAccessoryText(Ion::patchLevel());
return true;
}
return false;
}
/* Generic behaviour of preference menu*/
assert(m_messageTreeModel->label() != I18n::Message::DisplayMode || selectedRow() != numberOfRows()-1); // In that case, events OK and EXE are handled by the cell
setPreferenceWithValueIndex(m_messageTreeModel->label(), selectedRow());
AppsContainer * myContainer = (AppsContainer * )app()->container();
myContainer->refreshPreferences();
StackViewController * stack = stackController();
stack->pop();
return true;
}
if (event == Ion::Events::Left) {
stackController()->pop();
return true;
}
return false;
}
int SubController::numberOfRows() {
if (m_messageTreeModel) {
return m_messageTreeModel->numberOfChildren();
}
return 0;
}
HighlightCell * SubController::reusableCell(int index, int type) {
if (type == 2) {
assert(index == 0);
return &m_editableCell;
} else if (type == 1) {
assert(index >= 0 && index < 2);
return &m_complexFormatCells[index];
}
assert(index >= 0 && index < k_totalNumberOfCell);
return &m_cells[index];
}
int SubController::reusableCellCount(int type) {
switch (type) {
case 0:
return k_totalNumberOfCell;
case 1:
return 2;
case 2:
return 1;
default:
assert(false);
return 0;
}
}
int SubController::typeAtLocation(int i, int j) {
if (m_messageTreeModel->label() == I18n::Message::ComplexFormat) {
return 1;
}
if (m_messageTreeModel->label() == I18n::Message::DisplayMode && j == numberOfRows()-1) {
return 2;
}
return 0;
}
KDCoordinate SubController::rowHeight(int j) {
return Metric::ParameterCellHeight;
}
KDCoordinate SubController::cumulatedHeightFromIndex(int j) {
return rowHeight(0) * j;
}
int SubController::indexFromCumulatedHeight(KDCoordinate offsetY) {
KDCoordinate height = rowHeight(0);
if (height == 0) {
return 0;
}
return (offsetY - 1) / height;
}
void SubController::willDisplayCellForIndex(HighlightCell * cell, int index) {
if (m_messageTreeModel->label() == I18n::Message::ComplexFormat) {
return;
}
/* Number of significants figure row */
if (m_messageTreeModel->label() == I18n::Message::DisplayMode && index == numberOfRows()-1) {
MessageTableCellWithEditableText * myCell = (MessageTableCellWithEditableText *)cell;
char buffer[3];
Integer(Preferences::sharedPreferences()->numberOfSignificantDigits()).writeTextInBuffer(buffer, 3);
myCell->setAccessoryText(buffer);
return;
}
MessageTableCellWithBuffer * myCell = (MessageTableCellWithBuffer *)cell;
myCell->setMessage(m_messageTreeModel->children(index)->label());
myCell->setMessageFontSize(KDText::FontSize::Large);
myCell->setAccessoryText("");
if (m_messageTreeModel->label() == I18n::Message::ExamMode && GlobalPreferences::sharedGlobalPreferences()->examMode() == GlobalPreferences::ExamMode::Activate) {
myCell->setMessage(I18n::Message::ExamModeActive);
}
if (m_messageTreeModel->label() == I18n::Message::About) {
myCell->setMessageFontSize(KDText::FontSize::Small);
const char * messages[] = {
Ion::softwareVersion(),
Ion::serialNumber(),
Ion::fccId()
};
assert(index >= 0 && index < 3);
myCell->setAccessoryText(messages[index]);
}
}
void SubController::setMessageTreeModel(const MessageTree * messageTreeModel) {
m_messageTreeModel = (MessageTree *)messageTreeModel;
}
void SubController::viewWillAppear() {
m_selectableTableView.reloadData();
}
void SubController::viewDidDisappear() {
m_selectableTableView.deselectTable();
}
bool SubController::textFieldShouldFinishEditing(TextField * textField, Ion::Events::Event event) {
return (event == Ion::Events::Up && selectedRow() > 0)
|| TextFieldDelegate::textFieldShouldFinishEditing(textField, event);
}
bool SubController::textFieldDidFinishEditing(TextField * textField, const char * text, Ion::Events::Event event) {
Context * globalContext = textFieldDelegateApp()->localContext();
float floatBody = PoincareHelpers::ApproximateToScalar<float>(text, *globalContext);
if (std::isnan(floatBody) || std::isinf(floatBody)) {
floatBody = PrintFloat::k_numberOfPrintedSignificantDigits;
}
if (floatBody < 1) {
floatBody = 1;
}
if (floatBody > PrintFloat::k_numberOfStoredSignificantDigits) {
floatBody = PrintFloat::k_numberOfStoredSignificantDigits;
}
Preferences::sharedPreferences()->setNumberOfSignificantDigits((char)std::round(floatBody));
m_selectableTableView.reloadCellAtLocation(0, selectedRow());
if (event == Ion::Events::Up || event == Ion::Events::OK) {
m_selectableTableView.handleEvent(event);
}
return true;
}
StackViewController * SubController::stackController() const {
return (StackViewController *)parentResponder();
}
void SubController::setPreferenceWithValueIndex(I18n::Message message, int valueIndex) {
if (message == I18n::Message::AngleUnit) {
Preferences::sharedPreferences()->setAngleUnit((Expression::AngleUnit)valueIndex);
}
if (message == I18n::Message::DisplayMode) {
Preferences::sharedPreferences()->setDisplayMode((PrintFloat::Mode)valueIndex);
}
if (message == I18n::Message::EditionMode) {
Preferences::sharedPreferences()->setEditionMode((Preferences::EditionMode)valueIndex);
}
if (message == I18n::Message::ComplexFormat) {
Preferences::sharedPreferences()->setComplexFormat((Expression::ComplexFormat)valueIndex);
}
}
int SubController::valueIndexForPreference(I18n::Message message) {
if (message == I18n::Message::AngleUnit) {
return (int)Preferences::sharedPreferences()->angleUnit();
}
if (message == I18n::Message::DisplayMode) {
return (int)Preferences::sharedPreferences()->displayMode();
}
if (message == I18n::Message::EditionMode) {
return (int)Preferences::sharedPreferences()->editionMode();
}
if (message == I18n::Message::ComplexFormat) {
return (int)Preferences::sharedPreferences()->complexFormat();
}
return 0;
}
Shared::TextFieldDelegateApp * SubController::textFieldDelegateApp() {
return (Shared::TextFieldDelegateApp *)app();
}
}