forked from RedGl0w/atomic
-
Notifications
You must be signed in to change notification settings - Fork 5
/
list_controller.cpp
328 lines (291 loc) · 10.3 KB
/
list_controller.cpp
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
#include "list_controller.h"
#include <poincare_layouts.h>
#include <poincare_nodes.h>
#include <poincare/float.h>
namespace Atomic {
ListController::InnerView::InnerView(ListController * dataSource) :
ViewController(dataSource),
m_selectableTableView(this, dataSource, dataSource, dataSource)
{
m_selectableTableView.setMargins(0);
m_selectableTableView.setDecoratorType(ScrollView::Decorator::Type::None);
m_selectableTableView.setMargins(0,0,0,0);
}
void ListController::InnerView::setAtom(AtomDef atom) {
m_atom = atom;
m_selectableTableView.reloadData(false);
}
void ListController::InnerView::didBecomeFirstResponder() {
m_selectableTableView.reloadData();
m_selectableTableView.selectCellAtLocation(0,1);
}
ListController::ListController(Responder * parentResponder) :
StackViewController(parentResponder, &m_innerView, Palette::PurpleBright, Palette::PurpleDark),
m_innerView(this),
m_parent(parentResponder)
{
for (int i = 0; i < k_numberOfCellsWithBuffer; i++) {
m_cellsWithBuffer[i].setMessageFont(KDFont::LargeFont);
}
for (int i = 0; i < k_numberOfCellsWithExpression; i++) {
m_cellsWithExpression[i].setMessageFont(KDFont::LargeFont);
}
}
bool ListController::handleEvent(Ion::Events::Event event) {
if (event == Ion::Events::Right || event == Ion::Events::Left) {
return m_parent->handleEvent(event);
}
return false;
}
void ListController::didBecomeFirstResponder() {
selectCellAtLocation(0, 0);
Container::activeApp()->setFirstResponder(&m_innerView);
}
int ListController::numberOfRows() const {
return k_numberOfRow;
}
KDCoordinate ListController::rowHeight(int j) {
if (j == 0) {
return k_atomicCellRowHeight;
}
if (j == 7) {
return 35;
}
return k_classicalRowHeight;
}
HighlightCell * ListController::reusableCell(int index, int type) {
assert(index < k_numberOfRow);
switch (type) {
case 0:
{
assert(index == 0);
return &m_atomicCell;
}
case 1:
{
return &m_cellsWithBuffer[index];
}
case 2:
{
return &m_cellsWithExpression[index];
}
default:
{
assert(false);
return nullptr;
}
}
}
void ListController::tableViewDidChangeSelection(SelectableTableView * t, int previousSelectedCellX, int previousSelectedCellY, bool withinTemporarySelection) {
if (withinTemporarySelection) {
return;
}
// Forbid selecting ListAtomicCell
if (t->selectedRow() == 0) {
t->selectCellAtLocation(0, 1);
}
/* But scroll to the top when we select the first
* cell in order display the ListAtomicCell. */
if (t->selectedRow() == 1) {
t->scrollToCell(0, 0);
}
}
int ListController::reusableCellCount(int type) {
assert(type == 0);
return k_numberOfRow;
}
int ListController::typeAtLocation(int i, int j) {
if (j == 0) {
return 0;
} else if (j == 1 || j == 4) {
return 1;
} else {
return 2;
}
}
void ListController::setAtom(AtomDef atom) {
m_atom = atom;
m_innerView.setAtom(atom);
m_cellsWithExpression[0].setHighlighted(false);
m_cellsWithExpression[1].setHighlighted(false); // FIXME This fix is ugly (just supposing that there's the 2 first cellsWithExpression that can be seen when scrolling on 1st cell)
}
void ListController::willDisplayCellForIndex(HighlightCell * cell, int index) {
switch (index) {
case 0 : {
m_atomicCell.setAtom(m_atom);
return;
}
case 1: {
MessageTableCellWithBuffer * myCell = (MessageTableCellWithBuffer *)cell;
myCell->setMessage(I18n::Message::AtomSymbol);
myCell->setAccessoryText(m_atom.symbol);
myCell->setAccessoryFont(KDFont::SmallFont);
return;
}
case 2: {
MessageTableCellWithExpressionWithCopy * myCell = (MessageTableCellWithExpressionWithCopy *)cell;
myCell->setMessage(I18n::Message::AtomNum);
myCell->setLayoutWithCopy(Poincare::Integer(m_atom.num).createLayout());
return;
}
case 3: {
MessageTableCellWithExpressionWithCopy * myCell = (MessageTableCellWithExpressionWithCopy *)cell;
myCell->setMessage(I18n::Message::AtomNeutrons);
myCell->setLayoutWithCopy(Poincare::Integer(m_atom.neutrons).createLayout());
return;
}
case 4: {
MessageTableCellWithBuffer * myCell = (MessageTableCellWithBuffer *)cell;
myCell->setMessage(I18n::Message::AtomTypes);
myCell->setAccessoryText(I18n::translate(AtomicI18nForType[static_cast<int>(m_atom.type)]));
myCell->setAccessoryFont(KDFont::SmallFont);
return;
}
case 5: {
MessageTableCellWithExpressionWithCopy * myCell = (MessageTableCellWithExpressionWithCopy *)cell;
myCell->setMessage(I18n::Message::AtomMass);
myCell->setLayoutWithCopy(Poincare::FloatNode<double>(m_atom.mass).createLayout(Poincare::Preferences::PrintFloatMode::Decimal, 7));
return;
}
case 6: {
MessageTableCellWithExpressionWithCopy * myCell = (MessageTableCellWithExpressionWithCopy *)cell;
myCell->setMessage(I18n::Message::AtomElectroneg);
myCell->setLayoutWithCopy(Poincare::FloatNode<double>(m_atom.electroneg).createLayout(Poincare::Preferences::PrintFloatMode::Decimal, 5));
return;
}
case 7: {
MessageTableCellWithExpressionWithCopy * myCell = (MessageTableCellWithExpressionWithCopy *)cell;
myCell->setMessage(I18n::Message::AtomEC);
myCell->setLayoutWithCopy(Electronical::createElectronical(m_atom));
return;
}
default: {
assert(false);
}
}
}
Poincare::Layout ListController::Electronical::createElectronical(AtomDef atom) {
Poincare::Layout layouts[6];
int y = (atom.y < 8) ? atom.y : atom.y - 3;
if (atom.y > 0) {
char previousAtom[5] = {'[', ' ', ' ', ']', '\0'};
for (AtomDef a : atomsdefs) {
int ay = (a.y < 8) ? a.y : a.y - 3;
if (ay == y - 1 && a.x == 17) {
memcpy(&previousAtom[1], a.symbol, 2);
}
};
layouts[0] = Poincare::LayoutHelper::String(previousAtom, strlen(previousAtom));
}
bool isException = false;
exceptionStruct exceptionContent = Electronical::exceptions[0]; //We initialize it with random value to silence compilator warning (-Wmaybe-uninitialized)
for(exceptionStruct e : exceptions) {
if (e.num == atom.num) {
isException = true;
exceptionContent = e;
}
}
int indexAtRow = -1;
if (!isException) {
for (AtomDef a : atomsdefs) {
int ay = (a.y < 8) ? a.y : a.y - 3;
if (ay == y) {
indexAtRow = atom.num - a.num + 1;
break;
}
}
assert(indexAtRow != -1);
}
int s=0, f=0, d=0, p=0;
Electronical::rowsSubLayers row = Electronical::rows[y];
if (!isException) {
bool sEnabled = row.s, fEnabled = row.f, dEnabled = row.d, pEnabled = row.p;
int toOrder = indexAtRow;
for (int i = 0; i < indexAtRow; i++) {
if (sEnabled && s < 2) {
s++;
toOrder--;
} else if (fEnabled && f < 14) {
f++;
toOrder--;
} else if (dEnabled && d < 10) {
d++;
toOrder--;
} else if (pEnabled && p < 6) {
p++;
toOrder--;
}
}
assert(toOrder == 0);
} else {
s = exceptionContent.s ? (exceptionContent.sContent) : 0;
f = exceptionContent.f ? (exceptionContent.fContent) : 0;
d = exceptionContent.d ? (exceptionContent.dContent) : 0;
p = exceptionContent.p ? (exceptionContent.pContent) : 0;
}
int index = (layouts[0].isUninitialized()) ? 0 : 1;
// FIXME The 4 conditionnal blocs following are crashing with the 2 "additional" rows (Lanthanide and actinide)
if (s != 0) {
layouts[index] = computeLayer('s', row.sNumber, s);
index++;
}
if (f != 0) {
layouts[index] = computeLayer('f', row.fNumber, f);
index++;
}
if (d != 0) {
layouts[index] = computeLayer('d', row.dNumber, d);
index++;
}
if (p != 0) {
layouts[index] = computeLayer('p', row.pNumber, p);
index++;
}
Poincare::HorizontalLayout result = Poincare::HorizontalLayout::Builder(); // FIXME The display is totally broken
for(int i = 0; i < 5; i++) {
if(layouts[i].isUninitialized()) {
break;
}
result.addChildAtIndex(layouts[i], i, i, nullptr);
}
return result;
}
Poincare::Layout ListController::Electronical::computeLayer(CodePoint c, int subLayoutNumber, int number) {
return Poincare::HorizontalLayout::Builder(
Poincare::Rational::Builder(subLayoutNumber).createLayout(Poincare::Preferences::PrintFloatMode::Decimal, 7),
Poincare::CodePointLayout::Builder(c),
Poincare::VerticalOffsetLayout::Builder(
Poincare::Rational::Builder(number).createLayout(Poincare::Preferences::PrintFloatMode::Decimal, 7),
Poincare::VerticalOffsetLayoutNode::Position::Superscript)
);
}
const ListController::Electronical::rowsSubLayers ListController::Electronical::rows[] = {
{ true, 1, false, -1, false, -1, false, -1 }, // 1s²
{ true, 2, false, -1, false, -1, true, 2 }, // 2s² 2p⁶
{ true, 3, false, -1, false, -1, true, 3 }, // 3s² 3p⁶
{ true, 4, false, -1, true, 3, true, 4 }, // 4s² 3d¹⁰ 4p⁶
{ true, 5, false, -1, true, 4, true, 5 }, // 5s² 4d¹⁰ 5p⁶
{ true, 6, true, 4, true, 5, true, 6 }, // 6s² 4f¹⁴ 5d¹⁰ 6p⁶
{ true, 7, true, 5, true, 6, true, 7 }, // 6s² 4f¹⁴ 5d¹⁰ 6p⁶
};
const ListController::Electronical::exceptionStruct ListController::Electronical::exceptions[18] = {
{ 24, true, 1, false, -1, true, 5, false, -1},
{ 29, true, 1, false, -1, true, 10, false, -1},
{ 41, true, 1, false, -1, true, 4, false, -1},
{ 42, true, 1, false, -1, true, 5, false, -1},
{ 44, true, 1, false, -1, true, 7, false, -1},
{ 45, true, 1, false, -1, true, 8, false, -1},
{ 46, false, -1, false, -1, false, -1, false, -1},
{ 47, true, 1, false, -1, true, 10, false, -1},
{ 57, true, 2, false, -1, true, 1, false, -1},
{ 58, true, 2, true, 1, true, 1, false, -1},
{ 78, true, 1, true, 14, true, 19, false, -1},
{ 89, true, 2, false, -1, true, 1, false, -1},
{ 90, true, 2, false, -1, true, 2, false, -1},
{ 91, true, 2, true, 2, true, 1, false, -1},
{ 92, true, 2, true, 3, true, 1, false, -1},
{ 93, true, 2, true, 4, true, 1, false, -1},
{ 96, true, 2, true, 7, true, 1, false, -1},
{ 103, true, 2, true, 14, false, -1, true, 1},
};
}