[apps] Toolbox layouts are created by parsing the command of the cell.

Change-Id: I511e8af2b2247d1df3ca968191566c2b3f01fe11
This commit is contained in:
Léa Saviot
2018-01-09 15:45:28 +01:00
parent 5ec88dff8e
commit 1967fce13a
6 changed files with 62 additions and 77 deletions

View File

@@ -11,50 +11,22 @@ using namespace Poincare;
* and the text which would be edited by clicking on the row. When the node is a
* subtree, the edited text is set at I18n::Message::Default. */
const int pointedLayoutPathIntegral[] = {0};
const int pointedLayoutPathSum[] = {0};
const ToolboxMessageTree calculChildren[4] = {
ToolboxMessageTree(I18n::Message::DiffCommandWithArg, I18n::Message::DerivateNumber, I18n::Message::DiffCommandWithArg, nullptr, 0),
ToolboxMessageTree(I18n::Message::IntCommandWithArg, I18n::Message::Integral, I18n::Message::IntCommandWithArg, nullptr, 0,
new IntegralLayout(
new EmptyVisibleLayout(),
new EmptyVisibleLayout(),
new EmptyVisibleLayout(),
false),
const_cast<int *>(&pointedLayoutPathIntegral[0]),
1),
ToolboxMessageTree(I18n::Message::SumCommandWithArg, I18n::Message::Sum, I18n::Message::SumCommandWithArg, nullptr, 0,
new SumLayout(
new EmptyVisibleLayout(),
new EmptyVisibleLayout(),
new EmptyVisibleLayout(),
false),
const_cast<int *>(&pointedLayoutPathSum[0]),
1),
ToolboxMessageTree(I18n::Message::DiffCommandWithArg, I18n::Message::DerivateNumber, I18n::Message::DiffCommandWithArg),
ToolboxMessageTree(I18n::Message::IntCommandWithArg, I18n::Message::Integral, I18n::Message::IntCommandWithArg),
ToolboxMessageTree(I18n::Message::SumCommandWithArg, I18n::Message::Sum, I18n::Message::SumCommandWithArg),
ToolboxMessageTree(I18n::Message::ProductCommandWithArg, I18n::Message::Product, I18n::Message::ProductCommandWithArg)};
const int pointedLayoutPathConj[] = {0};
const ToolboxMessageTree complexChildren[5] = {
ToolboxMessageTree(I18n::Message::AbsCommandWithArg,I18n::Message::ComplexAbsoluteValue, I18n::Message::AbsCommandWithArg),
ToolboxMessageTree(I18n::Message::ArgCommandWithArg, I18n::Message::Agument, I18n::Message::ArgCommandWithArg),
ToolboxMessageTree(I18n::Message::ReCommandWithArg, I18n::Message::RealPart, I18n::Message::ReCommandWithArg),
ToolboxMessageTree(I18n::Message::ImCommandWithArg, I18n::Message::ImaginaryPart, I18n::Message::ImCommandWithArg),
ToolboxMessageTree(I18n::Message::ConjCommandWithArg, I18n::Message::Conjugate, I18n::Message::ConjCommandWithArg, nullptr, 0,
new ConjugateLayout(
new EmptyVisibleLayout()),
const_cast<int *>(&pointedLayoutPathConj[0]),
1)};
ToolboxMessageTree(I18n::Message::ConjCommandWithArg, I18n::Message::Conjugate, I18n::Message::ConjCommandWithArg)};
const int pointedLayoutPathBinomial[] = {0, 0};
const ToolboxMessageTree probabilityChildren[2] = {
ToolboxMessageTree(I18n::Message::BinomialCommandWithArg, I18n::Message::Combination, I18n::Message::BinomialCommandWithArg, nullptr, 0,
new ParenthesisLayout(
new GridLayout(Poincare::ExpressionLayoutArray(
new EmptyVisibleLayout(),
new EmptyVisibleLayout()).array(),
2, 1, false)),
const_cast<int *>(&pointedLayoutPathBinomial[0]),
2),
ToolboxMessageTree(I18n::Message::BinomialCommandWithArg, I18n::Message::Combination, I18n::Message::BinomialCommandWithArg),
ToolboxMessageTree(I18n::Message::PermuteCommandWithArg, I18n::Message::Permutation, I18n::Message::PermuteCommandWithArg)};
const ToolboxMessageTree arithmeticChildren[4] = {
@@ -100,8 +72,6 @@ const ToolboxMessageTree predictionChildren[3] = {
ToolboxMessageTree(I18n::Message::PredictionCommandWithArg, I18n::Message::Prediction, I18n::Message::PredictionCommandWithArg),
ToolboxMessageTree(I18n::Message::ConfidenceCommandWithArg, I18n::Message::Confidence, I18n::Message::ConfidenceCommandWithArg)};
const int pointedLayoutPathAbs[] = {0};
const int pointedLayoutPathRoot[] = {1};
const int pointedLayoutPathLog[] = {3,0};
#if LIST_ARE_DEFINED
const ToolboxMessageTree menu[12] = {
@@ -110,21 +80,9 @@ const ToolboxMessageTree menu[11] = {
#else
const ToolboxMessageTree menu[10] = {
#endif
ToolboxMessageTree(I18n::Message::AbsCommandWithArg, I18n::Message::AbsoluteValue, I18n::Message::AbsCommandWithArg, nullptr, 0,
new AbsoluteValueLayout(
new EmptyVisibleLayout()),
const_cast<int *>(&pointedLayoutPathAbs[0]),
1),
ToolboxMessageTree(I18n::Message::RootCommandWithArg, I18n::Message::NthRoot, I18n::Message::RootCommandWithArg, nullptr, 0,
new NthRootLayout(
new EmptyVisibleLayout(),
new EmptyVisibleLayout()),
const_cast<int *>(&pointedLayoutPathRoot[0]),
1),
ToolboxMessageTree(I18n::Message::LogCommandWithArg, I18n::Message::BasedLogarithm, I18n::Message::LogCommandWithArg, nullptr, 0,
LayoutEngine::createLogLayout(nullptr, new EmptyVisibleLayout()),
const_cast<int *>(&pointedLayoutPathLog[0]),
2),
ToolboxMessageTree(I18n::Message::AbsCommandWithArg, I18n::Message::AbsoluteValue, I18n::Message::AbsCommandWithArg),
ToolboxMessageTree(I18n::Message::RootCommandWithArg, I18n::Message::NthRoot, I18n::Message::RootCommandWithArg),
ToolboxMessageTree(I18n::Message::LogCommandWithArg, I18n::Message::BasedLogarithm, I18n::Message::LogCommandWithArg, nullptr, 0, const_cast<int *>(&pointedLayoutPathLog[0]), 2),
ToolboxMessageTree(I18n::Message::Calculation, I18n::Message::Default, I18n::Message::Default, calculChildren, 4),
ToolboxMessageTree(I18n::Message::ComplexNumber, I18n::Message::Default, I18n::Message::Default, complexChildren, 5),
ToolboxMessageTree(I18n::Message::Probability, I18n::Message::Default, I18n::Message::Default, probabilityChildren, 2),
@@ -175,13 +133,38 @@ bool MathToolbox::selectLeaf(ToolboxMessageTree * selectedMessageTree) {
return true;
}
// Deal with ExpressionEditor::Controller for now.
ToolboxMessageTree * messageTree = selectedMessageTree;
m_selectableTableView.deselectTable();
ExpressionLayout * newLayout = selectedMessageTree->layout()->clone();
ExpressionLayout * pointedLayout = newLayout;
for (int i = 0; i < selectedMessageTree->pointedPathLength(); i++) {
pointedLayout = pointedLayout->editableChild(selectedMessageTree->pointedPath()[i]);
// Translate the message and replace the arguments with Empty chars.
const char * editedText = I18n::translate(messageTree->insertedText());
char strippedEditedText[strlen(editedText)];
Shared::ToolboxHelpers::TextToParseIntoLayoutForCommandMessage(messageTree->insertedText(), strippedEditedText);
// Create the layout
Expression * resultExpression = Expression::parse(strippedEditedText);
if (resultExpression != nullptr) {
ExpressionLayout * resultLayout = resultExpression->createLayout();
// Find the pointed layout.
ExpressionLayout * pointedLayout = resultLayout;
if (messageTree->pointedPath() != nullptr) {
for (int i = 0; i < messageTree->pointedPathLength(); i++) {
assert(messageTree->pointedPath()[i] < pointedLayout->numberOfChildren());
pointedLayout = pointedLayout->editableChild(messageTree->pointedPath()[i]);
}
} else if (resultLayout->isHorizontal()) {
// If the layout is horizontal, pick the first open parenthesis.
for (int i = 0; i < resultLayout->numberOfChildren(); i++) {
if (resultLayout->editableChild(i)->isLeftParenthesis()) {
pointedLayout = resultLayout->editableChild(i);
break;
}
}
} else if (resultLayout->numberOfChildren() > 0) {
// Else, if the layout has children, pick the first one.
pointedLayout = resultLayout->editableChild(0);
}
// Insert the layout
expressionEditorControllerSender()->insertLayoutAtCursor(resultLayout, pointedLayout);
}
expressionEditorControllerSender()->insertLayoutAtCursor(newLayout, pointedLayout);
app()->dismissModalViewController();
return true;
}

View File

@@ -1,4 +1,5 @@
#include "toolbox_helpers.h"
#include <ion/charset.h>
#include <string.h>
namespace Shared {
@@ -40,5 +41,21 @@ void TextToInsertForCommandText(const char * command, char * buffer) {
buffer[currentNewTextIndex] = 0;
}
void TextToParseIntoLayoutForCommandMessage(I18n::Message message, char * buffer) {
const char * messageText = I18n::translate(message);
TextToInsertForCommandText(messageText, buffer);
size_t bufferLength = strlen(buffer);
for (size_t i = 0; i < bufferLength; i++) {
if (buffer[i] == '(' || buffer[i] == ',') {
// Shift the buffer to make room for the new char. Use memmove to avoid
// overwritting.
memmove(&buffer[i+2], &buffer[i+1], bufferLength - (i+1) + 1);
bufferLength++;
i++;
buffer[i] = Ion::Charset::Empty;
}
}
}
}
}

View File

@@ -17,6 +17,8 @@ void TextToInsertForCommandMessage(I18n::Message message, char * buffer);
void TextToInsertForCommandText(const char * command, char * buffer);
/* Removes the arguments from a command:
* - Removes text between parentheses, except commas */
void TextToParseIntoLayoutForCommandMessage(I18n::Message message, char * buffer);
/* Removes the arguments from a command and replaces them with empty chars. */
}
}

View File

@@ -70,7 +70,6 @@ objs += $(addprefix escher/src/,\
tiled_view.o\
timer.o\
toolbox.o\
toolbox_message_tree.o\
view.o\
view_controller.o\
warning_controller.o\

View File

@@ -6,27 +6,24 @@
class ToolboxMessageTree : public MessageTree {
public:
constexpr ToolboxMessageTree(I18n::Message label = (I18n::Message)0, I18n::Message text = (I18n::Message)0, I18n::Message insertedText = (I18n::Message)0, const ToolboxMessageTree * children = nullptr, int numberOfChildren = 0, Poincare::ExpressionLayout * layout = nullptr, int * pointedLayoutPath = nullptr, int pointedLayoutPathLength = 0) :
constexpr ToolboxMessageTree(I18n::Message label = (I18n::Message)0, I18n::Message text = (I18n::Message)0, I18n::Message insertedText = (I18n::Message)0, const ToolboxMessageTree * children = nullptr, int numberOfChildren = 0, int * pointedLayoutPath = nullptr, int pointedLayoutPathLength = 0) :
MessageTree(label, numberOfChildren),
m_children(children),
m_text(text),
m_insertedText(insertedText),
m_layout(layout),
m_pointedLayoutPath(pointedLayoutPath),
m_pointedLayoutPathLength(pointedLayoutPathLength)
{
};
const MessageTree * children(int index) const override;
I18n::Message text() const;
I18n::Message insertedText() const;
Poincare::ExpressionLayout * layout() const { return m_layout; }
const MessageTree * children(int index) const override { return &m_children[index]; }
I18n::Message text() const { return m_text; }
I18n::Message insertedText() const { return m_insertedText; }
int * pointedPath() const { return m_pointedLayoutPath; }
int pointedPathLength() const { return m_pointedLayoutPathLength; }
private:
const ToolboxMessageTree * m_children;
I18n::Message m_text;
I18n::Message m_insertedText;
Poincare::ExpressionLayout * m_layout;
int * m_pointedLayoutPath;
int m_pointedLayoutPathLength;
};

View File

@@ -1,13 +0,0 @@
#include <escher/toolbox_message_tree.h>
I18n::Message ToolboxMessageTree::text() const {
return m_text;
}
I18n::Message ToolboxMessageTree::insertedText() const {
return m_insertedText;
}
const MessageTree * ToolboxMessageTree::children(int index) const {
return &m_children[index];
}