Files
Upsilon/apps/graph/float_parameter_controller.cpp
Émilie Feral 42fe0435c2 [apps/graph] Create a class float parameter controller to be used by
the interval parameter controller and the future window parameter controller

Change-Id: Ibe061ff1792efc4a1795ee40ea5cd4b9f63e7086
2016-11-23 12:17:33 +01:00

86 lines
3.2 KiB
C++

#include "float_parameter_controller.h"
#include "app.h"
#include "../constant.h"
#include "../apps_container.h"
#include <assert.h>
namespace Graph {
FloatParameterController::FloatParameterController(Responder * parentResponder) :
ViewController(parentResponder),
m_selectableTableView(SelectableTableView(this, this, Metric::TopMargin, Metric::RightMargin,
Metric::BottomMargin, Metric::LeftMargin))
{
}
View * FloatParameterController::view() {
return &m_selectableTableView;
}
void FloatParameterController::didBecomeFirstResponder() {
m_selectableTableView.selectCellAtLocation(0, 0);
app()->setFirstResponder(&m_selectableTableView);
}
int FloatParameterController::activeCell() {
return m_selectableTableView.selectedRow();
}
void FloatParameterController::willDisplayCellForIndex(TableViewCell * cell, int index) {
TextMenuListCell * myCell = (TextMenuListCell *) cell;
char buffer[Constant::FloatBufferSizeInScientificMode];
Float(parameterAtIndex(index)).convertFloatToText(buffer, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaInScientificMode);
myCell->setAccessoryText(buffer);
}
bool FloatParameterController::handleEvent(Ion::Events::Event event) {
if (event == Ion::Events::OK) {
editParameter();
return true;
}
if (event.hasText()) {
editParameter(event.text());
return true;
}
return false;
}
void FloatParameterController::editParameter(const char * initialText) {
/* This code assumes that the active cell remains the one which is edited
* until the invocation is performed. This could lead to concurrency issue in
* other cases. */
char initialTextContent[255];
int cursorDelta = 0;
if (initialText) {
strlcpy(initialTextContent, initialText, sizeof(initialTextContent));
cursorDelta = strlen(initialText) > 1 ? -1 : 0;
} else {
TextMenuListCell * textMenuListCell = (TextMenuListCell *)reusableCell(activeCell());
strlcpy(initialTextContent, textMenuListCell->accessoryText(), sizeof(initialTextContent));
}
int cursorLocation = strlen(initialTextContent) + cursorDelta;
App * myApp = (App *)app();
InputViewController * inputController = myApp->inputViewController();
inputController->edit(this, initialTextContent, cursorLocation, this,
[](void * context, void * sender){
FloatParameterController * floatParameterController = (FloatParameterController *)context;
int activeCell = floatParameterController->activeCell();
TextMenuListCell * cell = (TextMenuListCell *)floatParameterController->reusableCell(activeCell);
InputViewController * myInputViewController = (InputViewController *)sender;
const char * textBody = myInputViewController->textBody();
AppsContainer * appsContainer = (AppsContainer *)floatParameterController->app()->container();
Context * globalContext = appsContainer->context();
float floatBody = Expression::parse(textBody)->approximate(*globalContext);
floatParameterController->setParameterAtIndex(activeCell, floatBody);
floatParameterController->willDisplayCellForIndex(cell, activeCell);
},
[](void * context, void * sender){
});
}
KDCoordinate FloatParameterController::cellHeight() {
return 35;
}
}