From b5f4a7b3f049876adc9c8d2cc3b06dfa69e2da42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Wed, 9 May 2018 13:32:27 +0200 Subject: [PATCH] [poincare] Simplify MatrixLayout code Change-Id: I4bfcf3da94296b8314ff47a1a71dbc79150a4027 --- poincare/include/poincare/expression_layout.h | 6 ++--- poincare/src/layout/matrix_layout.cpp | 26 ++++--------------- poincare/src/layout/matrix_layout.h | 3 +-- 3 files changed, 9 insertions(+), 26 deletions(-) diff --git a/poincare/include/poincare/expression_layout.h b/poincare/include/poincare/expression_layout.h index f58c62ca0..411dae424 100644 --- a/poincare/include/poincare/expression_layout.h +++ b/poincare/include/poincare/expression_layout.h @@ -89,8 +89,8 @@ public: virtual ExpressionLayoutCursor cursorRightOf(ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout) = 0; virtual ExpressionLayoutCursor cursorAbove(ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited = false); virtual ExpressionLayoutCursor cursorUnder(ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited = false); - virtual ExpressionLayoutCursor cursorInDescendantsAbove(ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout); - virtual ExpressionLayoutCursor cursorInDescendantsUnder(ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout); + ExpressionLayoutCursor cursorInDescendantsAbove(ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout); + ExpressionLayoutCursor cursorInDescendantsUnder(ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout); /* Expression Engine */ virtual int writeTextInBuffer(char * buffer, int bufferSize, int numberOfSignificantDigits = PrintFloat::k_numberOfStoredSignificantDigits) const = 0; @@ -142,6 +142,7 @@ protected: int * resultScore); virtual void privateAddSibling(ExpressionLayoutCursor * cursor, ExpressionLayout * sibling, bool moveCursor); void collapseOnDirection(HorizontalDirection direction, int absorbingChildIndex); + virtual ExpressionLayoutCursor cursorVerticalOf(VerticalDirection direction, ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited); ExpressionLayout * m_parent; KDCoordinate m_baseline; /* m_baseline is the signed vertical distance from the top of the layout to @@ -152,7 +153,6 @@ protected: bool m_positioned; private: void detachChildAtIndex(int i); - ExpressionLayoutCursor cursorVerticalOf(VerticalDirection direction, ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited); ExpressionLayoutCursor cursorInDescendantsVerticalOf(VerticalDirection direction, ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout); ExpressionLayout * replaceWithJuxtapositionOf(ExpressionLayout * leftChild, ExpressionLayout * rightChild, bool deleteAfterReplace); KDRect m_frame; diff --git a/poincare/src/layout/matrix_layout.cpp b/poincare/src/layout/matrix_layout.cpp index bde566c4c..db5b20791 100644 --- a/poincare/src/layout/matrix_layout.cpp +++ b/poincare/src/layout/matrix_layout.cpp @@ -73,34 +73,18 @@ ExpressionLayoutCursor MatrixLayout::cursorRightOf(ExpressionLayoutCursor cursor return GridLayout::cursorRightOf(cursor, shouldRecomputeLayout); } -ExpressionLayoutCursor MatrixLayout::cursorAbove(ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited) { +ExpressionLayoutCursor MatrixLayout::cursorVerticalOf(VerticalDirection direction, ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited) { bool shouldRemoveGreySquares = false; - for (int childIndex = 0; childIndex < m_numberOfColumns; childIndex++) { + int firstIndex = direction == VerticalDirection::Up ? 0 : numberOfChildren() - m_numberOfColumns; + int lastIndex = direction == VerticalDirection::Up ? m_numberOfColumns : numberOfChildren(); + for (int childIndex = firstIndex; childIndex < lastIndex; childIndex++) { if (cursor.pointedExpressionLayout()->hasAncestor(child(childIndex), true)) { // The cursor is leaving the matrix, so remove the grey squares. shouldRemoveGreySquares = true; break; } } - ExpressionLayoutCursor resultCursor = GridLayout::cursorAbove(cursor, shouldRecomputeLayout, equivalentPositionVisited); - if (resultCursor.isDefined() && shouldRemoveGreySquares) { - assert(hasGreySquares()); - removeGreySquares(); - *shouldRecomputeLayout = true; - } - return resultCursor; -} - -ExpressionLayoutCursor MatrixLayout::cursorUnder(ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited) { - bool shouldRemoveGreySquares = false; - for (int childIndex = numberOfChildren() - m_numberOfColumns; childIndex < m_numberOfChildren; childIndex++) { - if (cursor.pointedExpressionLayout()->hasAncestor(child(childIndex), true)) { - // The cursor is leaving the matrix, so remove the grey squares. - shouldRemoveGreySquares = true; - break; - } - } - ExpressionLayoutCursor resultCursor = GridLayout::cursorUnder(cursor, shouldRecomputeLayout, equivalentPositionVisited); + ExpressionLayoutCursor resultCursor = GridLayout::cursorVerticalOf(direction, cursor, shouldRecomputeLayout, equivalentPositionVisited); if (resultCursor.isDefined() && shouldRemoveGreySquares) { assert(hasGreySquares()); removeGreySquares(); diff --git a/poincare/src/layout/matrix_layout.h b/poincare/src/layout/matrix_layout.h index a5fa5c85f..e2cf2f4e2 100644 --- a/poincare/src/layout/matrix_layout.h +++ b/poincare/src/layout/matrix_layout.h @@ -13,8 +13,6 @@ public: /* Navigation */ ExpressionLayoutCursor cursorLeftOf(ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout) override; ExpressionLayoutCursor cursorRightOf(ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout) override; - ExpressionLayoutCursor cursorAbove(ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited = false) override; - ExpressionLayoutCursor cursorUnder(ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited = false) override; /* Dynamic layout */ void replaceChild(const ExpressionLayout * oldChild, ExpressionLayout * newChild, bool deleteOldChild) override; @@ -35,6 +33,7 @@ protected: void render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) override; KDSize computeSize() override; KDPoint positionOfChild(ExpressionLayout * child) override; + ExpressionLayoutCursor cursorVerticalOf(VerticalDirection direction, ExpressionLayoutCursor cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited) override; private: void childWasReplacedAtIndex(int index); bool isRowEmpty(int index) const;