[poincare] Clean some layouts

This commit is contained in:
Léa Saviot
2018-08-10 16:27:56 +02:00
parent 50d6eee4f4
commit 5dd043bf75
12 changed files with 103 additions and 54 deletions

View File

@@ -20,18 +20,32 @@ public:
return strlcpy(buffer, description(), bufferSize < descriptionLength ? bufferSize : descriptionLength);
*/
}
// Rendering
void invalidAllSizesPositionsAndBaselines() override {}
// Tree navigation
void moveCursorLeft(LayoutCursor * cursor, bool * shouldRecomputeLayout) override {}
void moveCursorRight(LayoutCursor * cursor, bool * shouldRecomputeLayout) override {}
void moveCursorUp(LayoutCursor * cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited = false) override {}
void moveCursorDown(LayoutCursor * cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited = false) override {}
LayoutCursor equivalentCursor(LayoutCursor * cursor) override { return LayoutCursor(); }
void deleteBeforeCursor(LayoutCursor * cursor) override {}
// Tree modification
// Collapse
bool shouldCollapseSiblingsOnLeft() const override { return false; }
bool shouldCollapseSiblingsOnRight() const override { return false; }
int leftCollapsingAbsorbingChildIndex() const override { return 0; }
int rightCollapsingAbsorbingChildIndex() const override { return 0; }
int leftCollapsingAbsorbingChildIndex() const override { assert(false); return 0; }
int rightCollapsingAbsorbingChildIndex() const override { assert(false); return 0; }
void didCollapseSiblings(LayoutCursor * cursor) override {}
//User input
void deleteBeforeCursor(LayoutCursor * cursor) override {}
// Other
LayoutNode * layoutToPointWhenInserting() override { return this; }
bool hasText() const override { return false; }
bool isCollapsable(int * numberOfOpenParenthesis, bool goingLeft) const override { return false; }
bool canBeOmittedMultiplicationLeftFactor() const override { return false; }
bool canBeOmittedMultiplicationRightFactor() const override { return false; }
@@ -45,7 +59,6 @@ public:
bool isEmpty() const override { return false; }
bool isMatrix() const override { return false; }
bool hasUpperLeftIndex() const override { return false; }
bool hasText() const override { return false; }
bool willAddChildAtIndex(LayoutNode * l, int * index, int * currentNumberOfChildren, LayoutCursor * cursor) override { return false; }
bool willAddSibling(LayoutCursor * cursor, LayoutNode * sibling, bool moveCursor) override { return false; }

View File

@@ -56,4 +56,3 @@ private:
}
#endif

View File

@@ -10,9 +10,11 @@ namespace Poincare {
class CeilingLayoutNode : public BracketPairLayoutNode {
public:
using BracketPairLayoutNode::BracketPairLayoutNode;
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override {
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, "ceil");
}
// TreeNode
static CeilingLayoutNode * FailedAllocationStaticNode();
CeilingLayoutNode * failedAllocationStaticNode() override { return FailedAllocationStaticNode(); }

View File

@@ -1,22 +1,24 @@
#ifndef POINCARE_CHAR_LAYOUT_NODE_H
#define POINCARE_CHAR_LAYOUT_NODE_H
#include <poincare/allocation_failure_layout_node.h>
#include <poincare/layout_cursor.h>
#include <poincare/layout_node.h>
#include <poincare/layout_reference.h>
#include <ion/charset.h>
namespace Poincare {
class CharLayoutNode : public LayoutNode {
public:
CharLayoutNode(char c = 'a', KDText::FontSize fontSize = KDText::FontSize::Large) :
CharLayoutNode(char c = Ion::Charset::Empty, KDText::FontSize fontSize = KDText::FontSize::Large) :
LayoutNode(),
m_char(c),
m_fontSize(fontSize)
{}
// CharLayout
void setChar(char c) { m_char = c; }
virtual void setChar(char c) { m_char = c; }
KDText::FontSize fontSize() const { return m_fontSize; }
void setFontSize(KDText::FontSize fontSize) { m_fontSize = fontSize; }
@@ -57,6 +59,11 @@ private:
KDText::FontSize m_fontSize;
};
class AllocationFailureCharLayoutNode : public AllocationFailureLayoutNode<CharLayoutNode> {
public:
void setChar(char c) override {}
};
class CharLayoutRef : public LayoutReference {
public:
CharLayoutRef(char c, KDText::FontSize fontSize = KDText::FontSize::Large) :

View File

@@ -17,6 +17,8 @@ public:
* a view with no cursor. */
void moveCursorLeft(LayoutCursor * cursor, bool * shouldRecomputeLayout) override { assert(false); }
void moveCursorRight(LayoutCursor * cursor, bool * shouldRecomputeLayout) override { assert(false); }
void moveCursorUp(LayoutCursor * cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited = false) override { assert(false); }
void moveCursorDown(LayoutCursor * cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited = false) override { assert(false); }
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override {
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, "sum");
}

View File

@@ -21,7 +21,9 @@ public:
void moveCursorUp(LayoutCursor * cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited = false) override;
void moveCursorDown(LayoutCursor * cursor, bool * shouldRecomputeLayout, bool equivalentPositionVisited = false) override;
void deleteBeforeCursor(LayoutCursor * cursor) override;
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
bool shouldCollapseSiblingsOnLeft() const override { return true; }
bool shouldCollapseSiblingsOnRight() const override { return true; }
int leftCollapsingAbsorbingChildIndex() const override { return 0; }

View File

@@ -40,22 +40,27 @@ public:
}
// TreeNode
static GridLayoutNode * FailedAllocationStaticNode();
GridLayoutNode * failedAllocationStaticNode() override { return FailedAllocationStaticNode(); }
size_t size() const override { return sizeof(GridLayoutNode); }
void didAddChildAtIndex(int newNumberOfChildren) override;
int numberOfChildren() const override { return m_numberOfRows * m_numberOfColumns; }
void eraseNumberOfChildren() override {
m_numberOfRows = 0;
m_numberOfColumns = 0;
}
#if TREE_LOG
const char * description() const override { return "GridLayout"; }
#if POINCARE_TREE_LOG
virtual void logNodeName(std::ostream & stream) const override {
stream << "GridLayout";
}
#endif
protected:
// GridLayoutNode
void addEmptyRow(EmptyLayoutNode::Color color);
void addEmptyColumn(EmptyLayoutNode::Color color);
void deleteRowAtIndex(int index);
void deleteColumnAtIndex(int index);
virtual void addEmptyRow(EmptyLayoutNode::Color color);
virtual void addEmptyColumn(EmptyLayoutNode::Color color);
virtual void deleteRowAtIndex(int index);
virtual void deleteColumnAtIndex(int index);
bool childIsRightOfGrid(int index) const;
bool childIsLeftOfGrid(int index) const;
bool childIsTopOfGrid(int index) const;
@@ -82,6 +87,26 @@ private:
void render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) override {}
};
class GridLayoutRef : public LayoutReference {
public:
GridLayoutRef(const GridLayoutNode * n) : LayoutReference(n) {}
GridLayoutRef() : LayoutReference(TreePool::sharedPool()->createTreeNode<GridLayoutNode>()) {}
void setDimensions(int rows, int columns);
void addChildAtIndex(LayoutReference l, int index, int currentNumberOfChildren, LayoutCursor * cursor) override {
LayoutReference::addChildAtIndex(l, index, currentNumberOfChildren, cursor);
}
private:
GridLayoutNode * node() const { return static_cast<GridLayoutNode *>(LayoutReference::node()); }
void setNumberOfRows(int rows) {
assert(rows >= 0);
node()->setNumberOfRows(rows);
}
void setNumberOfColumns(int columns) {
assert(columns >= 0);
node()->setNumberOfColumns(columns);
}
};
}
#endif

View File

@@ -33,7 +33,6 @@ public:
static MatrixLayoutNode * FailedAllocationStaticNode();
MatrixLayoutNode * failedAllocationStaticNode() override { return FailedAllocationStaticNode(); }
size_t size() const override { return sizeof(MatrixLayoutNode); }
void didAddChildAtIndex(int newNumberOfChildren) override;
#if POINCARE_TREE_LOG
virtual void logNodeName(std::ostream & stream) const override {
stream << "MatrixLayout";
@@ -61,13 +60,18 @@ private:
class AllocationFailureMatrixLayoutNode : public AllocationFailureLayoutNode<MatrixLayoutNode> {
void setNumberOfRows(int numberOfRows) override {}
void setNumberOfColumns(int numberOfColumns) override {}
protected:
void addEmptyRow(EmptyLayoutNode::Color color) override {}
void addEmptyColumn(EmptyLayoutNode::Color color) override {}
void deleteRowAtIndex(int index) override {}
void deleteColumnAtIndex(int index) override {}
};
class MatrixLayoutRef : public LayoutReference {
class MatrixLayoutRef : public GridLayoutRef {
friend class MatrixLayoutNode;
public:
MatrixLayoutRef(const MatrixLayoutNode * n) : LayoutReference(n) {}
MatrixLayoutRef() : LayoutReference(TreePool::sharedPool()->createTreeNode<MatrixLayoutNode>()) {}
MatrixLayoutRef(const MatrixLayoutNode * n) : GridLayoutRef(n) {}
MatrixLayoutRef() : GridLayoutRef(TreePool::sharedPool()->createTreeNode<MatrixLayoutNode>()) {}
MatrixLayoutRef(LayoutRef l1, LayoutRef l2, LayoutRef l3, LayoutRef l4) :
MatrixLayoutRef()
{
@@ -80,20 +84,8 @@ public:
bool hasGreySquares() const { return node()->hasGreySquares(); }
void addGreySquares() { node()->addGreySquares(); }
void removeGreySquares() { node()->removeGreySquares(); }
void setDimensions(int rows, int columns);
void addChildAtIndex(LayoutReference l, int index, int currentNumberOfChildren, LayoutCursor * cursor) override {
LayoutReference::addChildAtIndex(l, index, currentNumberOfChildren, cursor);
}
private:
MatrixLayoutNode * node() const { return static_cast<MatrixLayoutNode *>(LayoutReference::node()); }
void setNumberOfRows(int rows) {
assert(rows >= 0);
node()->setNumberOfRows(rows);
}
void setNumberOfColumns(int columns) {
assert(columns >= 0);
node()->setNumberOfColumns(columns);
}
};
}

View File

@@ -75,7 +75,7 @@ void BracketPairLayoutNode::deleteBeforeCursor(LayoutCursor * cursor) {
// Case: Left of the operand. Delete the layout, keep the operand.
LayoutRef rootRef = LayoutRef(root());
LayoutRef thisRef = LayoutRef(this);
LayoutRef child = childLayout();
LayoutRef child = LayoutRef(childLayout());
thisRef.replaceChildWithGhostInPlace(child);
// WARNING: do not call "this" afterwards
if (rootRef.isAllocationFailure()) {

View File

@@ -1,13 +1,11 @@
#include <poincare/char_layout_node.h>
#include <poincare/allocation_failure_layout_node.h>
#include <poincare/layout_helper.h>
#include <poincare/serialization_helper.h>
#include <ion/charset.h>
namespace Poincare {
CharLayoutNode * CharLayoutNode::FailedAllocationStaticNode() {
static AllocationFailureLayoutNode<CharLayoutNode> failure;
static AllocationFailureCharLayoutNode failure;
TreePool::sharedPool()->registerStaticNodeIfRequired(&failure);
return &failure;
}

View File

@@ -1,4 +1,5 @@
#include <poincare/grid_layout_node.h>
#include <poincare/allocation_failure_layout_node.h>
#include <poincare/empty_layout_node.h>
#include <poincare/layout_helper.h>
@@ -6,6 +7,12 @@ namespace Poincare {
static inline KDCoordinate max(KDCoordinate x, KDCoordinate y) { return x > y ? x : y; }
GridLayoutNode * GridLayoutNode::FailedAllocationStaticNode() {
static AllocationFailureLayoutNode<GridLayoutNode> failure;
TreePool::sharedPool()->registerStaticNodeIfRequired(&failure);
return &failure;
}
// LayoutNode
void GridLayoutNode::moveCursorLeft(LayoutCursor * cursor, bool * shouldRecomputeLayout) {
@@ -90,11 +97,16 @@ void GridLayoutNode::moveCursorDown(LayoutCursor * cursor, bool * shouldRecomput
LayoutNode::moveCursorDown(cursor, shouldRecomputeLayout, equivalentPositionVisited);
}
void GridLayoutNode::didAddChildAtIndex(int newNumberOfChildren) {
GridLayoutRef(this).setDimensions(1, newNumberOfChildren);
}
// Protected
void GridLayoutNode::addEmptyRow(EmptyLayoutNode::Color color) {
LayoutRef thisRef = LayoutRef(this);
GridLayoutRef thisRef = GridLayoutRef(this);
int previousNumberOfChildren = numberOfChildren();
int columnsCount = m_numberOfColumns;
int previousRowCount = m_numberOfRows;
for (int i = 0; i < columnsCount; i++) {
thisRef.addChildAtIndex(
EmptyLayoutRef(color),
@@ -103,13 +115,11 @@ void GridLayoutNode::addEmptyRow(EmptyLayoutNode::Color color) {
nullptr);
// WARNING: Do not access "this" afterwards
}
if (!thisRef.isAllocationFailure()) {
m_numberOfRows++;
}
thisRef.setDimensions(previousRowCount + 1, columnsCount);
}
void GridLayoutNode::addEmptyColumn(EmptyLayoutNode::Color color) {
LayoutRef thisRef = LayoutRef(this);
GridLayoutRef thisRef = GridLayoutRef(this);
int previousNumberOfChildren = numberOfChildren();
int rowsCount = m_numberOfRows;
int futureColumnsCount = m_numberOfColumns + 1;
@@ -121,9 +131,7 @@ void GridLayoutNode::addEmptyColumn(EmptyLayoutNode::Color color) {
nullptr);
// WARNING: Do not access "this" afterwards
}
if (!thisRef.isAllocationFailure()) {
m_numberOfColumns++;
}
thisRef.setDimensions(rowsCount, futureColumnsCount);
}
void GridLayoutNode::deleteRowAtIndex(int index) {
@@ -259,4 +267,15 @@ KDCoordinate GridLayoutNode::width() const {
return totalWidth;
}
// Grid Layout Reference
void GridLayoutRef::setDimensions(int rows, int columns) {
if (!isAllocationFailure()) {
/* If the node is an allocation failure, setNumberOfRows and
* setNumberOfColumns will do nothing. */
assert(rows * columns == numberOfChildren());
}
setNumberOfRows(rows);
setNumberOfColumns(columns);
}
}

View File

@@ -15,11 +15,6 @@ MatrixLayoutNode * MatrixLayoutNode::FailedAllocationStaticNode() {
// MatrixLayoutNode
void MatrixLayoutNode::didAddChildAtIndex(int newNumberOfChildren) {
setNumberOfRows(1);
setNumberOfColumns(newNumberOfChildren);
}
void MatrixLayoutNode::addGreySquares() {
if (!hasGreySquares()) {
LayoutRef thisRef(this);
@@ -252,7 +247,9 @@ bool MatrixLayoutNode::isColumnEmpty(int index) const {
}
bool MatrixLayoutNode::hasGreySquares() const {
assert(m_numberOfRows*m_numberOfColumns - 1 >= 0);
if (numberOfChildren() == 0) {
return false;
}
LayoutNode * lastChild = const_cast<MatrixLayoutNode *>(this)->childAtIndex(m_numberOfRows * m_numberOfColumns - 1);
if (lastChild->isEmpty()
&& !lastChild->isHorizontal()
@@ -298,11 +295,4 @@ void MatrixLayoutNode::didReplaceChildAtIndex(int index, LayoutCursor * cursor,
}
}
// Matrix Layout Reference
void MatrixLayoutRef::setDimensions(int rows, int columns) {
assert(rows * columns == numberOfChildren());
setNumberOfRows(rows);
setNumberOfColumns(columns);
}
}