[apps/graph] Fix vertical move on intersecting functions

When two functions are equal, there was a bug when moving the selection
up or down in the graph (the cursor would skip one function)
This commit is contained in:
Léa Saviot
2018-11-15 16:55:38 +01:00
committed by Émilie Feral
parent 2d36f02053
commit e9974a7e7a

View File

@@ -138,19 +138,45 @@ bool StorageFunctionGraphController::moveCursorVertically(int direction) {
int currentActiveFunctionIndex = indexFunctionSelectedByCursor();
StorageFunctionApp * myApp = static_cast<StorageFunctionApp *>(app());
double y = functionStore()->modelForRecord(functionStore()->activeRecordAtIndex(currentActiveFunctionIndex))->evaluateAtAbscissa(m_cursor->x(), myApp->localContext());
int nextActiveFunctionIndex = currentActiveFunctionIndex;
int nextActiveFunctionIndex = -1;
double nextY = direction > 0 ? DBL_MAX : -DBL_MAX;
int activeFunctionsCount = functionStore()->numberOfActiveFunctions();
for (int i = 0; i < activeFunctionsCount; i++) {
if (i == currentActiveFunctionIndex) {
continue;
}
double newY = functionStore()->modelForRecord(functionStore()->activeRecordAtIndex(i))->evaluateAtAbscissa(m_cursor->x(), myApp->localContext());
bool isNextFunction = direction > 0 ? (newY > y && newY < nextY) : (newY < y && newY > nextY);
bool isNextFunction = false;
/* Choosing the next function to select quite complex because we need to
* take care of functions that have the same values at the current x. When
* moving up, if several functions have the same value, we select the
* function of higher index. When going down, we select the function of
* lower index. */
if (direction > 0) {
if (newY > y && newY < nextY) {
isNextFunction = true;
} else if (newY == nextY) {
assert(i > nextActiveFunctionIndex);
isNextFunction = true;
} else if (newY == y && i < currentActiveFunctionIndex) {
isNextFunction = true;
}
} else {
if (newY < y && newY > nextY) {
isNextFunction = true;
} else if (newY == nextY) {
assert(i > nextActiveFunctionIndex);
} else if (newY == y && i > currentActiveFunctionIndex) {
isNextFunction = true;
}
}
if (isNextFunction) {
selectFunctionWithCursor(i);
nextY = newY;
nextActiveFunctionIndex = i;
}
}
if (nextActiveFunctionIndex == currentActiveFunctionIndex) {
if (nextActiveFunctionIndex < 0) {
return false;
}
m_cursor->moveTo(m_cursor->x(), nextY);