[poincare] Cleaned the dynamic methods for layouts.

Change-Id: I17db05b01c75a638a56fe2c197a175fd1b04840d
This commit is contained in:
Léa Saviot
2018-01-04 16:49:51 +01:00
parent 211227e682
commit 23eccd2c75
6 changed files with 46 additions and 25 deletions

View File

@@ -23,8 +23,10 @@ public:
int numberOfChildren() const override { return m_numberOfChildren; }
const ExpressionLayout * const * children() const override { return m_children; };
void addNonEmptyChildrenAtIndex(const ExpressionLayout * const * operands, int numberOfOperands, int indexForInsertion);
bool addChildAtIndex(ExpressionLayout * operand, int index) override;
void removeChildAtIndex(int index, bool deleteAfterRemoval) override;
void mergeChildrenAtIndex(DynamicLayoutHierarchy * eL, int index); // WITHOUT delete.
bool isEmpty() const override;
protected:

View File

@@ -50,6 +50,8 @@ public:
ExpressionLayout * replaceWithJuxtapositionOf(ExpressionLayout * leftChild, ExpressionLayout * rightChild, bool deleteAfterReplace);
virtual void replaceChild(const ExpressionLayout * oldChild, ExpressionLayout * newChild, bool deleteOldChild = true);
void detachChild(const ExpressionLayout * e); // Removes a child WITHOUT deleting it
void detachChildren(); //Removes all children WITHOUT deleting them
/* Dynamic Layout*/
virtual bool addChildAtIndex(ExpressionLayout * child, int index) { return false; }

View File

@@ -42,6 +42,40 @@ DynamicLayoutHierarchy::~DynamicLayoutHierarchy() {
delete[] m_children;
}
void DynamicLayoutHierarchy::mergeChildrenAtIndex(DynamicLayoutHierarchy * eL, int index) {
int indexOfEL = indexOfChild(eL);
if (indexOfEL >= 0) {
removeChildAtIndex(indexOfEL, false);
}
addNonEmptyChildrenAtIndex(eL->children(), eL->numberOfChildren(), index);
eL->detachChildren();
delete eL;
}
void DynamicLayoutHierarchy::addNonEmptyChildrenAtIndex(const ExpressionLayout * const * operands, int numberOfOperands, int indexForInsertion) {
assert(numberOfOperands > 0);
const ExpressionLayout ** newOperands = new const ExpressionLayout * [m_numberOfChildren+numberOfOperands];
int currentIndex = 0;
assert(indexForInsertion <= m_numberOfChildren);
for (int i=0; i<indexForInsertion; i++) {
newOperands[currentIndex++] = m_children[i];
}
for (int i=0; i<numberOfOperands; i++) {
if (!operands[i]->isEmpty()
|| (i < numberOfOperands-1 && operands[i+1]->mustHaveLeftBrother()))
{
const_cast<ExpressionLayout *>(operands[i])->setParent(this);
newOperands[currentIndex++] = operands[i];
}
}
for (int i=indexForInsertion; i<m_numberOfChildren; i++) {
newOperands[currentIndex++] = m_children[i];
}
delete[] m_children;
m_children = newOperands;
m_numberOfChildren = currentIndex;
}
bool DynamicLayoutHierarchy::addChildAtIndex(ExpressionLayout * child, int index) {
assert(index >= 0 && index <= m_numberOfChildren);
const ExpressionLayout ** newChildren = new const ExpressionLayout * [m_numberOfChildren+1];

View File

@@ -197,6 +197,12 @@ void ExpressionLayout::detachChild(const ExpressionLayout * e) {
}
}
void ExpressionLayout::detachChildren() {
for (int i = 0; i <numberOfChildren(); i++) {
detachChildAtIndex(i);
}
}
void ExpressionLayout::removeChildAtIndex(int index, bool deleteAfterRemoval) {
assert(index >= 0 && index < numberOfChildren());
replaceChild(editableChild(index), new EmptyVisibleLayout(), deleteAfterRemoval);

View File

@@ -50,7 +50,7 @@ void HorizontalLayout::replaceChild(const ExpressionLayout * oldChild, Expressio
// new layout then destroy it.
if (newChild->isHorizontal()) {
int indexForInsertion = indexOfChild(const_cast<ExpressionLayout *>(oldChild));
mergeChildrenAtIndex(newChild, indexForInsertion + 1);
mergeChildrenAtIndex(static_cast<HorizontalLayout *>(newChild), indexForInsertion + 1);
removeChildAtIndex(indexForInsertion, deleteOldChild);
return;
}
@@ -65,11 +65,10 @@ void HorizontalLayout::addOrMergeChildAtIndex(ExpressionLayout * eL, int index)
newIndex = index == 0 ? 0 : index - 1;
}
if (eL->isHorizontal()) {
mergeChildrenAtIndex(eL, newIndex);
mergeChildrenAtIndex(static_cast<HorizontalLayout *>(eL), newIndex);
return;
}
addChildAtIndex(eL, newIndex);
}
bool HorizontalLayout::moveLeft(ExpressionLayoutCursor * cursor) {
@@ -228,27 +227,6 @@ KDPoint HorizontalLayout::positionOfChild(ExpressionLayout * child) {
return KDPoint(x, y);
}
void HorizontalLayout::mergeChildrenAtIndex(ExpressionLayout * eL, int index) {
int indexOfEL = indexOfChild(eL);
if (indexOfEL >= 0) {
removeChildAtIndex(indexOfEL, false);
}
int numChildren = eL->numberOfChildren();
int currentAdditionIndex = index;
for (int i = 0; i < numChildren; i++) {
ExpressionLayout * currentChild = eL->editableChild(i);
// Do not add empty children if we can
if (!currentChild->isEmpty()
|| i == numChildren - 1
|| (i < numChildren - 1 && eL->editableChild(i+1)->mustHaveLeftBrother()))
{
addChildAtIndex(currentChild, currentAdditionIndex++);
eL->detachChild(currentChild);
}
}
delete eL;
}
bool HorizontalLayout::moveVertically(ExpressionLayout::VerticalDirection direction, ExpressionLayoutCursor * cursor, ExpressionLayout * previousLayout, ExpressionLayout * previousPreviousLayout) {
// Prevent looping fom child to parent
if (previousPreviousLayout == this) {

View File

@@ -38,7 +38,6 @@ protected:
KDSize computeSize() override;
void computeBaseline() override;
KDPoint positionOfChild(ExpressionLayout * child) override;
void mergeChildrenAtIndex(ExpressionLayout * eL, int index); // WITHOUT delete.
private:
bool moveVertically(ExpressionLayout::VerticalDirection direction, ExpressionLayoutCursor * cursor, ExpressionLayout * previousLayout, ExpressionLayout * previousPreviousLayout);
};