From ab74fe6933f0d3bc38043ac1d1bea061636e4345 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Mon, 26 Sep 2016 14:51:10 +0200 Subject: [PATCH] [apps/graphs] Modify how functions are handled by the function store Change-Id: I0d7a989526440a3b6d6a925b30d0d8742017e3f9 --- apps/graph/function.cpp | 28 +++++++++++++++++++--------- apps/graph/function.h | 6 +++++- apps/graph/function_store.cpp | 11 +++++++++-- apps/graph/function_store.h | 15 +++++++-------- apps/graph/graph_app.cpp | 20 ++++++++------------ 5 files changed, 48 insertions(+), 32 deletions(-) diff --git a/apps/graph/function.cpp b/apps/graph/function.cpp index 2e21710b9..4d370b497 100644 --- a/apps/graph/function.cpp +++ b/apps/graph/function.cpp @@ -1,20 +1,30 @@ #include "function.h" +#include Graph::Function::Function() : - m_text(nullptr), + m_name(nullptr), m_expression(nullptr), m_layout(nullptr) { } -Graph::Function::Function(const char * text, KDColor color) : - m_text(text), // FIXME: Copy !! - m_name("f(x)"), - m_color(color), - m_expression(nullptr), - m_layout(nullptr), - m_active(true) -{ +void Graph::Function::setContent(const char * c) { + #if __GLIBC__ + // FIXME: This is ugly. + strncpy(m_text, c, sizeof(m_text)); +#else + strlcpy(m_text, c, sizeof(m_text)); +#endif + m_expression = expression(); + m_layout = layout(); +} + +void Graph::Function::setColor(KDColor color) { + m_color = color; +} + +void Graph::Function::setName(const char * c) { + m_name = c; } Graph::Function::~Function() { diff --git a/apps/graph/function.h b/apps/graph/function.h index fb04c2ded..e0a5265bb 100644 --- a/apps/graph/function.h +++ b/apps/graph/function.h @@ -18,8 +18,12 @@ public: ExpressionLayout * layout(); bool isActive(); void setActive(bool active); + void setContent(const char * c); + void setColor(KDColor m_color); + void setName(const char * c); private: - const char * m_text; + constexpr static int k_bodyLength = 255; + char m_text[k_bodyLength]; const char * m_name; KDColor m_color; Expression * m_expression; diff --git a/apps/graph/function_store.cpp b/apps/graph/function_store.cpp index 67f05b7b0..a96377170 100644 --- a/apps/graph/function_store.cpp +++ b/apps/graph/function_store.cpp @@ -16,9 +16,16 @@ Graph::Function * Graph::FunctionStore::functionAtIndex(int i) { return &m_functions[i]; } -void Graph::FunctionStore::addFunction(Function * f) { +Graph::Function * Graph::FunctionStore::addEmptyFunction() { assert(m_numberOfFunctions < k_maxNumberOfFunctions); - m_functions[m_numberOfFunctions++] = (*f); + Graph::Function addedFunction = Function(); + addedFunction.setColor(defaultColors[numberOfFunctions()%numberOfDefaultColors]); + addedFunction.setName("f(x)"); + addedFunction.setActive(true); + m_functions[m_numberOfFunctions] = addedFunction; + Function * result = &m_functions[m_numberOfFunctions]; + m_numberOfFunctions++; + return result; } void Graph::FunctionStore::removeFunction(Function * f) { diff --git a/apps/graph/function_store.h b/apps/graph/function_store.h index 9a1e75f38..457cae39b 100644 --- a/apps/graph/function_store.h +++ b/apps/graph/function_store.h @@ -4,22 +4,21 @@ #include "function.h" namespace Graph { - + /* FunctionStore is a dumb class. + * Its only job is to store functions and to give them a color. */ class FunctionStore { public: FunctionStore(); Function * functionAtIndex(int i); - void addFunction(Function * f); + Function * addEmptyFunction(); void removeFunction(Function * f); - void pushFunction(const char * functionText); - int numberOfFunctions(); - constexpr static int numberOfDefaultColors = 3; - constexpr static KDColor defaultColors[numberOfDefaultColors] = { - KDColorRed, KDColorGreen, KDColorBlue - }; private: + constexpr static int numberOfDefaultColors = 4; + constexpr static KDColor defaultColors[numberOfDefaultColors] = { + KDColorRed, KDColorGreen, KDColorBlue, KDColorBlack + }; int m_numberOfFunctions; static constexpr int k_maxNumberOfFunctions = 8; Function m_functions[k_maxNumberOfFunctions]; diff --git a/apps/graph/graph_app.cpp b/apps/graph/graph_app.cpp index c6435dd76..b67ce0d92 100644 --- a/apps/graph/graph_app.cpp +++ b/apps/graph/graph_app.cpp @@ -8,18 +8,14 @@ GraphApp::GraphApp() : m_graphController(GraphController(nullptr, &m_functionStore)), m_tabViewController(this, &m_listStackViewController, &m_graphController) { - Graph::Function function = Graph::Function("(x-1)*(x+1)*x", - Graph::FunctionStore::defaultColors[(m_functionStore.numberOfFunctions())%Graph::FunctionStore::numberOfDefaultColors]); - m_functionStore.addFunction(&function); - function = Graph::Function("x*x", - Graph::FunctionStore::defaultColors[(m_functionStore.numberOfFunctions())%Graph::FunctionStore::numberOfDefaultColors]); - m_functionStore.addFunction(&function); - function = Graph::Function("3", - Graph::FunctionStore::defaultColors[(m_functionStore.numberOfFunctions())%Graph::FunctionStore::numberOfDefaultColors]); - m_functionStore.addFunction(&function); - function = Graph::Function("x*x*x", - Graph::FunctionStore::defaultColors[(m_functionStore.numberOfFunctions())%Graph::FunctionStore::numberOfDefaultColors]); - m_functionStore.addFunction(&function); + Graph::Function * function = m_functionStore.addEmptyFunction(); + function->setContent("(x-1)*(x+1)*x"); + function = m_functionStore.addEmptyFunction(); + function->setContent("x*x"); + function = m_functionStore.addEmptyFunction(); + function->setContent("3"); + function = m_functionStore.addEmptyFunction(); + function->setContent("x*x*x"); } ViewController * GraphApp::rootViewController() {