diff --git a/apps/graph/function.cpp b/apps/graph/function.cpp index 4d370b497..358af7d32 100644 --- a/apps/graph/function.cpp +++ b/apps/graph/function.cpp @@ -1,10 +1,12 @@ #include "function.h" #include -Graph::Function::Function() : - m_name(nullptr), +Graph::Function::Function(const char * text, KDColor color) : + m_name(text), + m_color(color), m_expression(nullptr), - m_layout(nullptr) + m_layout(nullptr), + m_active(true) { } @@ -23,10 +25,6 @@ void Graph::Function::setColor(KDColor color) { m_color = color; } -void Graph::Function::setName(const char * c) { - m_name = c; -} - Graph::Function::~Function() { //FIXME: Free m_text! if (m_layout != nullptr) { diff --git a/apps/graph/function.h b/apps/graph/function.h index e0a5265bb..a5807d7d7 100644 --- a/apps/graph/function.h +++ b/apps/graph/function.h @@ -8,8 +8,7 @@ namespace Graph { class Function { public: - Function(); - Function(const char * text, KDColor m_color); + Function(const char * text = nullptr, KDColor color = KDColorBlack); ~Function(); // Delete expression and layout, if needed const char * text(); const char * name(); @@ -20,7 +19,6 @@ public: void setActive(bool active); void setContent(const char * c); void setColor(KDColor m_color); - void setName(const char * c); private: constexpr static int k_bodyLength = 255; char m_text[k_bodyLength]; diff --git a/apps/graph/function_store.cpp b/apps/graph/function_store.cpp index a96377170..91dcbc161 100644 --- a/apps/graph/function_store.cpp +++ b/apps/graph/function_store.cpp @@ -3,8 +3,9 @@ extern "C" { #include } -constexpr int Graph::FunctionStore::numberOfDefaultColors; -constexpr KDColor Graph::FunctionStore::defaultColors[numberOfDefaultColors]; +constexpr int Graph::FunctionStore::k_numberOfDefaultColors; +constexpr KDColor Graph::FunctionStore::defaultColors[k_numberOfDefaultColors]; +constexpr const char * Graph::FunctionStore::functionNames[k_maxNumberOfFunctions]; Graph::FunctionStore::FunctionStore() : m_numberOfFunctions(0) @@ -18,10 +19,9 @@ Graph::Function * Graph::FunctionStore::functionAtIndex(int i) { Graph::Function * Graph::FunctionStore::addEmptyFunction() { assert(m_numberOfFunctions < k_maxNumberOfFunctions); - Graph::Function addedFunction = Function(); - addedFunction.setColor(defaultColors[numberOfFunctions()%numberOfDefaultColors]); - addedFunction.setName("f(x)"); - addedFunction.setActive(true); + const char * name = firstAvailableName(); + KDColor color = firstAvailableColor(); + Graph::Function addedFunction = Function(name, color); m_functions[m_numberOfFunctions] = addedFunction; Function * result = &m_functions[m_numberOfFunctions]; m_numberOfFunctions++; @@ -43,3 +43,35 @@ void Graph::FunctionStore::removeFunction(Function * f) { int Graph::FunctionStore::numberOfFunctions() { return m_numberOfFunctions; } + +const char * Graph::FunctionStore::firstAvailableName() { + for (int k = 0; k < k_maxNumberOfFunctions; k++) { + int j = 0; + while (j < m_numberOfFunctions) { + if (m_functions[j].name() == functionNames[k]) { + break; + } + j++; + } + if (j == m_numberOfFunctions) { + return functionNames[k]; + } + } + return functionNames[0]; +} + +const KDColor Graph::FunctionStore::firstAvailableColor() { + for (int k = 0; k < k_numberOfDefaultColors; k++) { + int j = 0; + while (j < m_numberOfFunctions) { + if (m_functions[j].color() == defaultColors[k]) { + break; + } + j++; + } + if (j == m_numberOfFunctions) { + return defaultColors[k]; + } + } + return defaultColors[0]; +} diff --git a/apps/graph/function_store.h b/apps/graph/function_store.h index 457cae39b..659020319 100644 --- a/apps/graph/function_store.h +++ b/apps/graph/function_store.h @@ -15,12 +15,18 @@ public: int numberOfFunctions(); private: - constexpr static int numberOfDefaultColors = 4; - constexpr static KDColor defaultColors[numberOfDefaultColors] = { - KDColorRed, KDColorGreen, KDColorBlue, KDColorBlack + const char * firstAvailableName(); + const KDColor firstAvailableColor(); + static constexpr int k_maxNumberOfFunctions = 8; + static constexpr int k_numberOfDefaultColors = 8; + static constexpr KDColor defaultColors[k_numberOfDefaultColors] = { + KDColorRed, KDColorGreen, KDColorBlue, KDColorBlack, + KDColor(0xBFD3EB), KDColor(0xFF00FF), KDColor(0x00FFEB), KDColor(0xBFAA00) + }; + static constexpr const char * functionNames[k_maxNumberOfFunctions] = { + "f", "g", "h", "i", "j", "k", "l", "m" }; int m_numberOfFunctions; - static constexpr int k_maxNumberOfFunctions = 8; Function m_functions[k_maxNumberOfFunctions]; }; diff --git a/apps/graph/list/function_expression_view.cpp b/apps/graph/list/function_expression_view.cpp index ca660f5a0..cf52f868c 100644 --- a/apps/graph/list/function_expression_view.cpp +++ b/apps/graph/list/function_expression_view.cpp @@ -13,6 +13,5 @@ void FunctionExpressionView::drawRect(KDContext * ctx, KDRect rect) const { // Select text color according to the state of the function bool active = m_function->isActive(); KDColor text = active ? KDColorBlack : FunctionCell::k_desactiveTextColor; - ctx->drawString(m_function->text(), KDPointZero, text, background); - // m_function->layout()->draw(ctx, KDPointZero); + m_function->layout()->draw(ctx, KDPointZero, text, background); }