Horizontal layout finished.

Change-Id: Id02daf672668d0c946e3008bdfd0e3f00d165864
This commit is contained in:
Felix Raimundo
2016-03-24 18:24:16 +01:00
parent 4707459a19
commit 7f660ffbd5
7 changed files with 27 additions and 28 deletions

View File

@@ -14,6 +14,5 @@ float Addition::operateApproximatevelyOn(float a, float b) {
}
ExpressionLayout * Addition::createLayout(ExpressionLayout * parent) {
//FIXME: There can be more than two operands now! :-)
return new HorizontalLayout(parent, operand(0), '+', operand(1));
return new HorizontalLayout(parent, m_operands, m_numberOfOperands, '+');
}

View File

@@ -30,6 +30,7 @@ int CommutativeOperation::numberOfOperands() {
}
Expression * CommutativeOperation::operand(int i) {
assert(i < m_numberOfOperands);
return m_operands[i];
}

View File

@@ -42,10 +42,10 @@ class Expression;
%%
[0-9]+ { yylval->string = yytext; return(INTEGER); }
[A-Za-z]+ { yylval->string = yytext; return(SYMBOL); }
sin {return(SINUS);}
cos {return(COSINUS);}
tan {return(TANGENT);}
[A-Za-z]+ { yylval->string = yytext; return(SYMBOL); }
\+ { return(PLUS); }
\- { return(MINUS); }
\* { return(MULTIPLY); }

View File

@@ -1,29 +1,28 @@
extern "C" {
#include <kandinsky.h>
#include <assert.h>
#include <kandinsky.h>
#include <stdlib.h>
}
#include "horizontal_layout.h"
#include "string_layout.h"
HorizontalLayout::HorizontalLayout(ExpressionLayout * parent, int number_of_operands, Expression ** operands, char symbol) {
m_number_of_operands = number_of_operands;
// FIXME: This implementation is not optimal as the operator layout is created and stored a lot of times.
// The reason for this is how the layouts are drawn.
m_children_layouts = (ExpressionLayout **)malloc((2*m_number_of_operands-)*sizeof(ExpressionLayout *));
m_operator_layout = new StringLayout(this, string, 1);
HorizontalLayout::HorizontalLayout(ExpressionLayout * parent, Expression ** operands,int number_of_operands, char symbol) : ExpressionLayout(parent) {
assert(number_of_operands > 0);
m_number_of_children = 2*number_of_operands-1;
m_children_layouts = (ExpressionLayout **)malloc(m_number_of_children*sizeof(ExpressionLayout *));
char string[2] = {symbol, '\0'};
for (int i=1; i<m_number_of_operands; i++) {
m_operands[2*i-1] = m_operator_layout;
m_operands[2*i] = operands[i]->createLayout();
m_children_layouts[0] = operands[0]->createLayout(this);
for (int i=1; i<number_of_operands; i++) {
m_children_layouts[2*i-1] = new StringLayout(this, string, 1);
m_children_layouts[2*i] = operands[i]->createLayout(this);
}
}
HorizontalLayout::~HorizontalLayout() {
for (int i(0); i<m_numberOfOperands; i++) {
delete m_operands[2*i];
for (int i=0; i<m_number_of_children; i++) {
delete m_children_layouts[i];
}
free(m_operands);
delete m_operator_layout;
free(m_children_layouts);
}
void HorizontalLayout::render(KDPoint point) { }
@@ -42,23 +41,25 @@ KDSize HorizontalLayout::computeSize() {
}
ExpressionLayout * HorizontalLayout::child(uint16_t index) {
if (index >= 2*m_number_of_operands) {
assert(index <= m_number_of_children);
if (index < m_number_of_children) {
return m_children_layouts[index];
} else {
return nullptr;
}
return m_children_layouts[index];
}
KDPoint HorizontalLayout::positionOfChild(ExpressionLayout * child) {
KDPoint position = (KDPoint){.x = 0, .y = 0};
uint16_t index = 0;
for (int i=0;i<3;i++) {
if (m_children[i] == child) {
for (int i=0;i<m_number_of_children;i++) {
if (m_children_layouts[i] == child) {
index = i;
break;
}
}
if (index > 0) {
ExpressionLayout * previousChild = m_children[index-1];
ExpressionLayout * previousChild = m_children_layouts[index-1];
assert(previousChild != nullptr);
position.x = previousChild->origin().x + previousChild->size().width;
}

View File

@@ -6,7 +6,7 @@
class HorizontalLayout : public ExpressionLayout {
public:
HorizontalLayout(ExpressionLayout * parent, int number_of_operands, Expression ** operands, char symbol);
HorizontalLayout(ExpressionLayout * parent, Expression ** operands, int number_of_children, char symbol);
~HorizontalLayout();
protected:
void render(KDPoint point) override;
@@ -14,9 +14,8 @@ class HorizontalLayout : public ExpressionLayout {
ExpressionLayout * child(uint16_t index) override;
KDPoint positionOfChild(ExpressionLayout * child) override;
private:
int m_number_of_operands;
int m_number_of_children;
ExpressionLayout ** m_children_layouts;
ExpressionLayout * m_operator_layout;
};
#endif

View File

@@ -17,6 +17,5 @@ Expression::Type Product::type() {
}
ExpressionLayout * Product::createLayout(ExpressionLayout * parent) {
//FIXME: There can be more than two factors now! :-)
return new HorizontalLayout(parent, operand(0), '*', operand(1));
return new HorizontalLayout(parent, m_operands, m_numberOfOperands, '*');
}

View File

@@ -14,5 +14,5 @@ Expression::Type Subtraction::type() {
}
ExpressionLayout * Subtraction::createLayout(ExpressionLayout * parent) {
return new HorizontalLayout(parent, m_operands[0], '-', m_operands[1]);
return new HorizontalLayout(parent, m_operands, 2, '-');
}