[apps/sequence] Fix the insertion of sequence terms in text fields.

For instance, the serialization of the V_n layout is "v_{n}", which is
what we want to insert in an EditableExpressionView, but not in a Text
Field for which we want "v(n)".

Change-Id: Iab38058d982322891b530b3afd2d303a266643f9
This commit is contained in:
Léa Saviot
2018-01-19 17:31:37 +01:00
parent 64efb45c24
commit e29691e1f1
3 changed files with 49 additions and 19 deletions

View File

@@ -115,14 +115,19 @@ void MathToolbox::setSenderAndAction(Responder * sender, Action action) {
m_action = action;
}
void MathToolbox::actionForEditableExpressionView(void * sender, const char * text) {
void MathToolbox::actionForEditableExpressionView(void * sender, const char * text, bool removeArguments) {
EditableExpressionView * expressionLayoutEditorSender = static_cast<EditableExpressionView *>(sender);
// Replace the arguments with Empty chars.
int textToInsertMaxLength = strlen(text);
char textToInsert[textToInsertMaxLength];
Shared::ToolboxHelpers::TextToParseIntoLayoutForCommandText(text, textToInsert, textToInsertMaxLength);
// Create the layout
Expression * resultExpression = Expression::parse(textToInsert);
Expression * resultExpression = nullptr;
if (removeArguments) {
// Replace the arguments with Empty chars.
int textToInsertMaxLength = strlen(text);
char textToInsert[textToInsertMaxLength];
Shared::ToolboxHelpers::TextToParseIntoLayoutForCommandText(text, textToInsert, textToInsertMaxLength);
// Create the layout
resultExpression = Expression::parse(textToInsert);
} else {
resultExpression = Expression::parse(text);
}
if (resultExpression == nullptr) {
return;
}
@@ -145,24 +150,30 @@ void MathToolbox::actionForEditableExpressionView(void * sender, const char * te
expressionLayoutEditorSender->insertLayoutAtCursor(resultLayout, pointedLayout);
}
void MathToolbox::actionForTextField(void * sender, const char * text) {
void MathToolbox::actionForTextField(void * sender, const char * text, bool removeArguments) {
TextField * textFieldSender = static_cast<TextField *>(sender);
if (!textFieldSender->isEditing()) {
textFieldSender->setEditing(true);
}
int maxTextToInsertLength = strlen(text);
char textToInsert[maxTextToInsertLength];
// Translate the message and remove the arguments.
Shared::ToolboxHelpers::TextToInsertForCommandText(text, textToInsert, maxTextToInsertLength);
textFieldSender->insertTextAtLocation(textToInsert, textFieldSender->cursorLocation());
int newCursorLocation = textFieldSender->cursorLocation() + Shared::ToolboxHelpers::CursorIndexInCommandText(text);
int newCursorLocation = textFieldSender->cursorLocation();
if (removeArguments) {
int maxTextToInsertLength = strlen(text);
char textToInsert[maxTextToInsertLength];
// Translate the message and remove the arguments.
Shared::ToolboxHelpers::TextToInsertForCommandText(text, textToInsert, maxTextToInsertLength);
textFieldSender->insertTextAtLocation(textToInsert, textFieldSender->cursorLocation());
newCursorLocation+= Shared::ToolboxHelpers::CursorIndexInCommandText(textToInsert);
} else {
textFieldSender->insertTextAtLocation(text, textFieldSender->cursorLocation());
newCursorLocation+= Shared::ToolboxHelpers::CursorIndexInCommandText(text);
}
textFieldSender->setCursorLocation(newCursorLocation);
}
bool MathToolbox::selectLeaf(ToolboxMessageTree * selectedMessageTree) {
ToolboxMessageTree * messageTree = selectedMessageTree;
m_selectableTableView.deselectTable();
m_action(sender(), I18n::translate(messageTree->insertedText()));
m_action(sender(), I18n::translate(messageTree->insertedText()), true);
app()->dismissModalViewController();
return true;
}

View File

@@ -8,11 +8,11 @@
class MathToolbox : public Toolbox {
public:
typedef void (*Action)(void * sender, const char * text);
typedef void (*Action)(void * sender, const char * text, bool removeArguments);
MathToolbox();
void setSenderAndAction(Responder * sender, Action action);
static void actionForEditableExpressionView(void * sender, const char * text);
static void actionForTextField(void * sender, const char * text);
static void actionForEditableExpressionView(void * sender, const char * text, bool removeArguments = true);
static void actionForTextField(void * sender, const char * text, bool removeArguments = true);
protected:
bool selectLeaf(ToolboxMessageTree * selectedMessageTree) override;
const ToolboxMessageTree * rootModel() override;

View File

@@ -115,7 +115,26 @@ bool SequenceToolbox::selectAddedCell(int selectedRow){
int bufferSize = 10;
char buffer[bufferSize];
m_addedCellLayout[selectedRow]->writeTextInBuffer(buffer, bufferSize);
m_action(sender(), buffer);
if (m_action == MathToolbox::actionForTextField) {
// DIRTY. The symbols are layouted using a Subscript VerticalOffsetLayout,
// which serializes into "_{}", but we want parentheses for text fields. We
// thus need to remove any underscores, and changes brackets into
// parentheses.
for (int i = 0; i < bufferSize; i++) {
if (buffer[i] == '{') {
buffer[i] = '(';
}
if (buffer[i] == '}') {
buffer[i] = ')';
}
if (buffer[i] == '_') {
memmove(&buffer[i], &buffer[i+1], bufferSize - (i+1) + 1);
bufferSize--;
i--;
}
}
}
m_action(sender(), buffer, false);
app()->dismissModalViewController();
return true;
}