From c12c268807cc96c4d6b2c6a6388bfdf16460939a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Tue, 31 Jul 2018 11:39:00 +0200 Subject: [PATCH] [poincare] LayoutReference::addChildAtIndex needs current childrenCount --- poincare/include/poincare/layout_reference.h | 2 +- poincare/src/equal.cpp | 2 +- poincare/src/factorial.cpp | 3 ++- poincare/src/grid_layout_node.cpp | 15 ++++++++++++--- poincare/src/horizontal_layout_node.cpp | 4 ++-- poincare/src/layout_engine.cpp | 6 +++--- poincare/src/layout_reference.cpp | 7 ++++--- poincare/src/matrix.cpp | 2 +- poincare/src/power.cpp | 1 + poincare/src/store.cpp | 2 +- poincare/src/vertical_offset_layout_node.cpp | 6 +++--- 11 files changed, 31 insertions(+), 19 deletions(-) diff --git a/poincare/include/poincare/layout_reference.h b/poincare/include/poincare/layout_reference.h index a47737af5..978f6d77d 100644 --- a/poincare/include/poincare/layout_reference.h +++ b/poincare/include/poincare/layout_reference.h @@ -73,7 +73,7 @@ public: // Tree modification // Add - void addChildAtIndex(LayoutReference l, int index, LayoutCursor * cursor); + void addChildAtIndex(LayoutReference l, int index, int currentNumberOfChildren, LayoutCursor * cursor); void addSibling(LayoutCursor * cursor, LayoutReference sibling, bool moveCursor); // Replace //void replaceChildAtIndex(int oldChildIndex, LayoutReference newChild) { TreeReference::replaceTreeChildAtIndex(oldChildIndex, newChild); } diff --git a/poincare/src/equal.cpp b/poincare/src/equal.cpp index b4abf61e5..06f8a69d4 100644 --- a/poincare/src/equal.cpp +++ b/poincare/src/equal.cpp @@ -52,7 +52,7 @@ Expression * Equal::shallowReduce(Context& context, Preferences::AngleUnit angle LayoutRef Equal::createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const { HorizontalLayoutRef result; result.addOrMergeChildAtIndex(operand(0)->createLayout(floatDisplayMode, numberOfSignificantDigits), 0, false); - result.addChildAtIndex(CharLayoutRef('='), result.numberOfChildren(), nullptr); + result.addChildAtIndex(CharLayoutRef('='), result.numberOfChildren(), result.numberOfChildren(), nullptr); result.addOrMergeChildAtIndex(operand(1)->createLayout(floatDisplayMode, numberOfSignificantDigits), result.numberOfChildren(), false); return result; } diff --git a/poincare/src/factorial.cpp b/poincare/src/factorial.cpp index b3f222a20..2f0deecb4 100644 --- a/poincare/src/factorial.cpp +++ b/poincare/src/factorial.cpp @@ -95,7 +95,8 @@ std::complex Factorial::computeOnComplex(const std::complex c, Preferences LayoutRef Factorial::createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const { HorizontalLayoutRef result; result.addOrMergeChildAtIndex(operand(0)->createLayout(floatDisplayMode, numberOfSignificantDigits), 0, false); - result.addChildAtIndex(CharLayoutRef('!'), result.numberOfChildren(), nullptr); + int childrenCount = result.numberOfChildren(); + result.addChildAtIndex(CharLayoutRef('!'), childrenCount, childrenCount, nullptr); return result; } diff --git a/poincare/src/grid_layout_node.cpp b/poincare/src/grid_layout_node.cpp index 9516ae0a6..82710f63f 100644 --- a/poincare/src/grid_layout_node.cpp +++ b/poincare/src/grid_layout_node.cpp @@ -94,18 +94,27 @@ void GridLayoutNode::moveCursorDown(LayoutCursor * cursor, bool * shouldRecomput //TODO void GridLayoutNode::addEmptyRow(EmptyLayoutNode::Color color) { LayoutRef thisRef = LayoutRef(this); - int insertionIndex = numberOfChildren(); + int previousNumberOfChildren = numberOfChildren(); for (int i = 0; i < m_numberOfColumns; i++) { - thisRef.addChildAtIndex(EmptyLayoutRef(color), insertionIndex, nullptr); + thisRef.addChildAtIndex( + EmptyLayoutRef(color), + previousNumberOfChildren, + previousNumberOfChildren + i, + nullptr); } m_numberOfRows++; } void GridLayoutNode::addEmptyColumn(EmptyLayoutNode::Color color) { LayoutRef thisRef = LayoutRef(this); + int previousNumberOfChildren = numberOfChildren(); m_numberOfColumns++; for (int i = 0; i < m_numberOfRows; i++) { - thisRef.addChildAtIndex(EmptyLayoutRef(color), i*m_numberOfColumns + m_numberOfColumns-1, nullptr); + thisRef.addChildAtIndex( + EmptyLayoutRef(color), + i*m_numberOfColumns + m_numberOfColumns-1, + previousNumberOfChildren + i, + nullptr); } } diff --git a/poincare/src/horizontal_layout_node.cpp b/poincare/src/horizontal_layout_node.cpp index 27cc75312..26363a666 100644 --- a/poincare/src/horizontal_layout_node.cpp +++ b/poincare/src/horizontal_layout_node.cpp @@ -248,7 +248,7 @@ void HorizontalLayoutNode::didRemoveChildAtIndex(int index, LayoutCursor * curso * sibling (e.g. a VerticalOffsetLayout), add an empty layout at index 0 */ if (!force && index == 0 && numberOfChildren() > 0 && childAtIndex(0)->mustHaveLeftSibling()) { - HorizontalLayoutRef(this).addChildAtIndex(EmptyLayoutRef(), 0, cursor); + HorizontalLayoutRef(this).addChildAtIndex(EmptyLayoutRef(), 0, numberOfChildren(), cursor); } } @@ -354,7 +354,7 @@ void HorizontalLayoutRef::addOrMergeChildAtIndex(LayoutRef l, int index, bool re if (l.isHorizontal()) { mergeChildrenAtIndex(HorizontalLayoutRef(l.node()), index, removeEmptyChildren, cursor); } else { - addChildAtIndex(l, index, cursor); + addChildAtIndex(l, index, numberOfChildren(),cursor); } } diff --git a/poincare/src/layout_engine.cpp b/poincare/src/layout_engine.cpp index 2fc1958cf..5a8270905 100644 --- a/poincare/src/layout_engine.cpp +++ b/poincare/src/layout_engine.cpp @@ -34,7 +34,7 @@ LayoutRef LayoutEngine::createPrefixLayout(const Expression * expression, Prefer if (numberOfOperands > 0) { args.addOrMergeChildAtIndex(expression->operand(0)->createLayout(floatDisplayMode, numberOfSignificantDigits), 0, true); for (int i = 1; i < numberOfOperands; i++) { - args.addChildAtIndex(CharLayoutRef(','), args.numberOfChildren(), nullptr); + args.addChildAtIndex(CharLayoutRef(','), args.numberOfChildren(), args.numberOfChildren(), nullptr); args.addOrMergeChildAtIndex(expression->operand(i)->createLayout(floatDisplayMode, numberOfSignificantDigits), args.numberOfChildren(), true); } } @@ -57,7 +57,7 @@ LayoutRef LayoutEngine::createStringLayout(const char * buffer, int bufferSize, assert(bufferSize > 0); HorizontalLayoutRef resultLayout; for (int i = 0; i < bufferSize; i++) { - resultLayout.addChildAtIndex(CharLayoutRef(buffer[i], fontSize), i, nullptr); + resultLayout.addChildAtIndex(CharLayoutRef(buffer[i], fontSize), i, i, nullptr); } return resultLayout; } @@ -65,7 +65,7 @@ LayoutRef LayoutEngine::createStringLayout(const char * buffer, int bufferSize, LayoutRef LayoutEngine::createLogLayout(LayoutRef argument, LayoutRef index) { HorizontalLayoutRef resultLayout = HorizontalLayoutRef(createStringLayout("log", 3)); VerticalOffsetLayoutRef offsetLayout = VerticalOffsetLayoutRef(index, VerticalOffsetLayoutNode::Type::Subscript); - resultLayout.addChildAtIndex(offsetLayout, resultLayout.numberOfChildren(), nullptr); + resultLayout.addChildAtIndex(offsetLayout, resultLayout.numberOfChildren(), resultLayout.numberOfChildren(), nullptr); resultLayout.addOrMergeChildAtIndex(createParenthesedLayout(argument, false), resultLayout.numberOfChildren(), true); return resultLayout; } diff --git a/poincare/src/layout_reference.cpp b/poincare/src/layout_reference.cpp index 50d6b8da2..1bd5b3d4a 100644 --- a/poincare/src/layout_reference.cpp +++ b/poincare/src/layout_reference.cpp @@ -80,9 +80,10 @@ void LayoutReference::replaceWithJuxtapositionOf(LayoutRef leftChild, LayoutRef p.removeChild(*this, cursor->layoutReference() == *this ? cursor : nullptr); } -void LayoutReference::addChildAtIndex(LayoutRef l, int index, LayoutCursor * cursor) { +void LayoutReference::addChildAtIndex(LayoutRef l, int index, int currentNumberOfChildren, LayoutCursor * cursor) { int newIndex = index; - if (!this->node()->willAddChildAtIndex(l.node(), &newIndex, cursor)) { + int newCurrentNumberOfChildren = currentNumberOfChildren; + if (!this->node()->willAddChildAtIndex(l.node(), &newIndex, /*&newCurrentNumberOfChildren /*TODO*/, cursor)) { return; } LayoutRef nextPointedLayout(nullptr); @@ -97,7 +98,7 @@ void LayoutReference::addChildAtIndex(LayoutRef l, int index, LayoutCursor * cur } } - this->addChildTreeAtIndex(l, newIndex, numberOfChildren()); + this->addChildTreeAtIndex(l, newIndex, newCurrentNumberOfChildren); if (cursor != nullptr) { if (this->isAllocationFailure()) { diff --git a/poincare/src/matrix.cpp b/poincare/src/matrix.cpp index 8607697b5..6f75b332e 100644 --- a/poincare/src/matrix.cpp +++ b/poincare/src/matrix.cpp @@ -219,7 +219,7 @@ LayoutRef Matrix::createLayout(Preferences::PrintFloatMode floatDisplayMode, int MatrixLayoutRef layout; LayoutRef castedLayout(layout.node()); for (int i = 0; i < numberOfOperands(); i++) { - castedLayout.addChildAtIndex(operand(i)->createLayout(floatDisplayMode, numberOfSignificantDigits), i, nullptr); + castedLayout.addChildAtIndex(operand(i)->createLayout(floatDisplayMode, numberOfSignificantDigits), i, i, nullptr); } layout.setNumberOfRows(numberOfRows()); layout.setNumberOfColumns(numberOfColumns()); diff --git a/poincare/src/power.cpp b/poincare/src/power.cpp index 528c73733..3d3d496bc 100644 --- a/poincare/src/power.cpp +++ b/poincare/src/power.cpp @@ -183,6 +183,7 @@ LayoutRef Power::createLayout(Preferences::PrintFloatMode floatDisplayMode, int indiceOperand->createLayout(floatDisplayMode, numberOfSignificantDigits), VerticalOffsetLayoutNode::Type::Superscript), result.numberOfChildren(), + result.numberOfChildren(), nullptr); return result; } diff --git a/poincare/src/store.cpp b/poincare/src/store.cpp index 091e4162d..096b0370d 100644 --- a/poincare/src/store.cpp +++ b/poincare/src/store.cpp @@ -36,7 +36,7 @@ Expression * Store::shallowReduce(Context& context, Preferences::AngleUnit angle LayoutRef Store::createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const { HorizontalLayoutRef result = HorizontalLayoutRef(); result.addOrMergeChildAtIndex(value()->createLayout(floatDisplayMode, numberOfSignificantDigits), 0, false); - result.addChildAtIndex(CharLayoutRef(Ion::Charset::Sto), result.numberOfChildren(), nullptr); + result.addChildAtIndex(CharLayoutRef(Ion::Charset::Sto), result.numberOfChildren(), result.numberOfChildren(), nullptr); result.addOrMergeChildAtIndex(symbol()->createLayout(floatDisplayMode, numberOfSignificantDigits), result.numberOfChildren(), false); return result; } diff --git a/poincare/src/vertical_offset_layout_node.cpp b/poincare/src/vertical_offset_layout_node.cpp index f7c1a699b..47040f812 100644 --- a/poincare/src/vertical_offset_layout_node.cpp +++ b/poincare/src/vertical_offset_layout_node.cpp @@ -255,16 +255,16 @@ bool VerticalOffsetLayoutNode::willAddSibling(LayoutCursor * cursor, LayoutNode { leftParenthesisIndex--; } - parentRef.addChildAtIndex(leftParenthesis, leftParenthesisIndex, nullptr); + parentRef.addChildAtIndex(leftParenthesis, leftParenthesisIndex, parentRef.numberOfChildren(), nullptr); idxInParent++; // Add the Right parenthesis RightParenthesisLayoutRef rightParenthesis = RightParenthesisLayoutRef(); if (cursor->position() == LayoutCursor::Position::Right) { - parentRef.addChildAtIndex(rightParenthesis, idxInParent + 1, nullptr); + parentRef.addChildAtIndex(rightParenthesis, idxInParent + 1, parentRef.numberOfChildren(), nullptr); } else { assert(cursor->position() == LayoutCursor::Position::Left); - parentRef.addChildAtIndex(rightParenthesis, idxInParent, nullptr); + parentRef.addChildAtIndex(rightParenthesis, idxInParent, parentRef.numberOfChildren(), nullptr); } if (rightParenthesis.parent().isDefined()) { cursor->setLayoutReference(rightParenthesis);