From d42bbaf25a3362709b17cb2a49470e1c0081eabb Mon Sep 17 00:00:00 2001 From: Felix Raimundo Date: Wed, 16 Mar 2016 14:05:29 +0100 Subject: [PATCH] Add exponent layout. Change-Id: Iffce8dcdd4ecf10a8ed4170494b59fa6c524b9cb --- poincare/Makefile | 2 +- poincare/src/layout/exponent_layout.cpp | 55 +++++++++++++++++++++++++ poincare/src/layout/exponent_layout.h | 21 ++++++++++ poincare/src/power.cpp | 4 +- 4 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 poincare/src/layout/exponent_layout.cpp create mode 100644 poincare/src/layout/exponent_layout.h diff --git a/poincare/Makefile b/poincare/Makefile index d18f442cd..581ad8012 100644 --- a/poincare/Makefile +++ b/poincare/Makefile @@ -1,6 +1,6 @@ SFLAGS += -Ipoincare/include objs += $(addprefix poincare/src/, addition.o context.o expression.o float.o integer.o fraction.o expression_lexer.o expression_parser.o power.o product.o symbol.o) -objs += $(addprefix poincare/src/layout/, expression_layout.o fraction_layout.o horizontal_layout.o string_layout.o) +objs += $(addprefix poincare/src/layout/, expression_layout.o fraction_layout.o horizontal_layout.o exponent_layout.o string_layout.o) tests += $(addprefix poincare/test/, integer.cpp) # Even though flex and bison will generate both implementation and headers at diff --git a/poincare/src/layout/exponent_layout.cpp b/poincare/src/layout/exponent_layout.cpp new file mode 100644 index 000000000..2dc202bf9 --- /dev/null +++ b/poincare/src/layout/exponent_layout.cpp @@ -0,0 +1,55 @@ +#include +#include +#include "exponent_layout.h" + +// TODO(fraimundo): Find a better name. +#define EXPONENT_HEIGHT 5 + +ExponentLayout::ExponentLayout(ExpressionLayout * parent, Expression * base, Expression * exponent) : +ExpressionLayout(parent) { + m_base_layout = base->createLayout(this); + m_exponent_layout = exponent->createLayout(this); +} + +ExponentLayout::~ExponentLayout() { + delete m_exponent_layout; + delete m_base_layout; +} + +// There is nothing to draw for a power, only the position of the children matters +void ExponentLayout::render(KDPoint point) { } + +KDSize ExponentLayout::computeSize() { + KDSize s; + KDSize exponent_size, base_size; + exponent_size = m_exponent_layout->size(); + base_size = m_base_layout->size(); + s.height = base_size.height + exponent_size.height - EXPONENT_HEIGHT; + s.width = base_size.width + exponent_size.width; + return s; +} + +ExpressionLayout * ExponentLayout::child(uint16_t index) { + switch (index) { + case 0: + return m_base_layout; + case 1: + return m_exponent_layout; + default: + return nullptr; + } +} + +KDPoint ExponentLayout::positionOfChild(ExpressionLayout * child) { + KDPoint p; + if (child == m_base_layout) { + p.x = 0; + p.y = m_exponent_layout->size().height - EXPONENT_HEIGHT; + } else if (child == m_exponent_layout) { + p.x = m_base_layout->size().width; + p.y = 0; + } else { + assert(false); // Should not happen + } + return p; +} diff --git a/poincare/src/layout/exponent_layout.h b/poincare/src/layout/exponent_layout.h new file mode 100644 index 000000000..7f79fc64b --- /dev/null +++ b/poincare/src/layout/exponent_layout.h @@ -0,0 +1,21 @@ +#ifndef POINCARE_EXPONENT_LAYOUT_H +#define POINCARE_EXPONENT_LAYOUT_H + +#include +#include + +class ExponentLayout : public ExpressionLayout { + public: + ExponentLayout(ExpressionLayout * parent, Expression * base, Expression * exponent); + ~ExponentLayout(); + protected: + void render(KDPoint point) override; + KDSize computeSize() override; + ExpressionLayout * child(uint16_t index) override; + KDPoint positionOfChild(ExpressionLayout * child) override; + private: + ExpressionLayout * m_base_layout; + ExpressionLayout * m_exponent_layout; +}; + +#endif diff --git a/poincare/src/power.cpp b/poincare/src/power.cpp index e68890198..7a22c5ae6 100644 --- a/poincare/src/power.cpp +++ b/poincare/src/power.cpp @@ -1,5 +1,5 @@ #include -#include "layout/horizontal_layout.h" +#include "layout/exponent_layout.h" Power::Power(Expression * base, Expression * exponent) : m_base(base), @@ -17,5 +17,5 @@ float Power::approximate(Context& context) { } ExpressionLayout * Power::createLayout(ExpressionLayout * parent) { - return new HorizontalLayout(parent, m_base, '^', m_exponent); + return new ExponentLayout(parent, m_base, m_exponent); }