From 140e9430214320fcd0e0fa60c7cb42ffeca0666d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Fri, 9 Dec 2016 15:10:09 +0100 Subject: [PATCH] [apps/graph/graph] Merge two redundant methods in graph view Change-Id: Icc154656143e81696abba18574c3806bcfdd4c7a --- apps/graph/graph/graph_controller.cpp | 4 ++-- apps/graph/graph/graph_view.cpp | 34 ++++----------------------- apps/graph/graph/graph_view.h | 3 +-- liba/include/float.h | 2 +- 4 files changed, 8 insertions(+), 35 deletions(-) diff --git a/apps/graph/graph/graph_controller.cpp b/apps/graph/graph/graph_controller.cpp index 776227e42..921bf4b24 100644 --- a/apps/graph/graph/graph_controller.cpp +++ b/apps/graph/graph/graph_controller.cpp @@ -158,7 +158,7 @@ bool GraphController::handleEvent(Ion::Events::Event event) { return true; } if (event == Ion::Events::Up) { - Function * f = m_view.moveCursorUp(); + Function * f = m_view.moveCursorVertically(1.0f); if (f == nullptr) { m_view.initCursorPosition(); m_view.setCursorVisible(false); @@ -168,7 +168,7 @@ bool GraphController::handleEvent(Ion::Events::Event event) { return true; } if (event == Ion::Events::Down) { - Function * f = m_view.moveCursorDown(); + Function * f = m_view.moveCursorVertically(-1.0f); if (f == nullptr) { return false; } diff --git a/apps/graph/graph/graph_view.cpp b/apps/graph/graph/graph_view.cpp index a8727eeb7..1fde9df9f 100644 --- a/apps/graph/graph/graph_view.cpp +++ b/apps/graph/graph/graph_view.cpp @@ -99,43 +99,17 @@ void GraphView::moveCursorHorizontally(KDCoordinate xOffset) { reload(); } -Function * GraphView::moveCursorUp() { +Function * GraphView::moveCursorVertically(int direction) { float x = pixelToFloat(Axis::Horizontal, m_xCursorPosition); Function * actualFunction = m_functionStore->activeFunctionAtIndex(m_indexFunctionSelectedByCursor); float y = actualFunction->evaluateAtAbscissa(x, m_evaluateContext); Function * nextFunction = actualFunction; - float nextY = FLT_MAX; + float nextY = direction > 0 ? FLT_MAX : -FLT_MAX; for (int i = 0; i < m_functionStore->numberOfActiveFunctions(); i++) { Function * f = m_functionStore->activeFunctionAtIndex(i); float newY = f->evaluateAtAbscissa(x, m_evaluateContext); - if (newY > y && newY < nextY) { - m_indexFunctionSelectedByCursor = i; - nextY = newY; - nextFunction = f; - } - } - if (nextFunction == actualFunction) { - return nullptr; - } - float xMargin = pixelToFloat(Axis::Horizontal, k_cursorMarginToBorder) - pixelToFloat(Axis::Horizontal, 0); - float yMargin = pixelToFloat(Axis::Vertical, 0) - pixelToFloat(Axis::Vertical, k_cursorMarginToBorder); - m_graphWindow->panToMakePointVisible(x, nextY, xMargin, yMargin); - m_xCursorPosition = floatToPixel(Axis::Horizontal, x); - m_yCursorPosition = floatToPixel(Axis::Vertical, nextY); - reload(); - return nextFunction; -} - -Function * GraphView::moveCursorDown() { - float x = pixelToFloat(Axis::Horizontal, m_xCursorPosition); - Function * actualFunction = m_functionStore->activeFunctionAtIndex(m_indexFunctionSelectedByCursor); - float y = actualFunction->evaluateAtAbscissa(x, m_evaluateContext); - Function * nextFunction = actualFunction; - float nextY = -FLT_MAX; - for (int i = 0; i < m_functionStore->numberOfActiveFunctions(); i++) { - Function * f = m_functionStore->activeFunctionAtIndex(i); - float newY = f->evaluateAtAbscissa(x, m_evaluateContext); - if (newY < y && newY > nextY) { + bool isNextFunction = direction > 0 ? (newY > y && newY < nextY) : (newY < y && newY > nextY); + if (isNextFunction) { m_indexFunctionSelectedByCursor = i; nextY = newY; nextFunction = f; diff --git a/apps/graph/graph/graph_view.h b/apps/graph/graph/graph_view.h index 5abfb81cf..36c57aa23 100644 --- a/apps/graph/graph/graph_view.h +++ b/apps/graph/graph/graph_view.h @@ -22,8 +22,7 @@ public: void setCursorVisible(bool visibleCursor); void initCursorPosition(); void moveCursorHorizontally(KDCoordinate xOffset); - Function * moveCursorUp(); - Function * moveCursorDown(); + Function * moveCursorVertically(int direction); void setContext(Context * evaluateContext); Context * context() const; int indexFunctionSelectedByCursor(); diff --git a/liba/include/float.h b/liba/include/float.h index 9b486b820..d0485dc26 100644 --- a/liba/include/float.h +++ b/liba/include/float.h @@ -1,6 +1,6 @@ #ifndef LIBA_FLOAT_H #define LIBA_FLOAT_H -#define FLT_MAX 1E+37 +#define FLT_MAX 1E+37f #endif