[apps] Shared: Move part of the implementation of TermSumController to

shared (Integral Graph Controller) and improve bound edition in
TermSumController.
This commit is contained in:
Émilie Feral
2018-01-10 14:07:26 +01:00
committed by EmilieNumworks
parent 31afd260a4
commit 7ea0dbeb56
15 changed files with 506 additions and 356 deletions

View File

@@ -38,6 +38,7 @@ app_objs += $(addprefix apps/shared/,\
simple_interactive_curve_view_controller.o\
store_controller.o\
store_parameter_controller.o\
sum_graph_controller.o\
tab_table_controller.o\
text_field_delegate.o\
text_field_delegate_app.o\
@@ -45,5 +46,6 @@ app_objs += $(addprefix apps/shared/,\
values_function_parameter_controller.o\
values_parameter_controller.o\
values_controller.o\
vertical_cursor_view.o\
zoom_parameter_controller.o\
)

View File

@@ -28,6 +28,11 @@ ViewController * FunctionGraphController::initialisationParameterController() {
}
void FunctionGraphController::viewWillAppear() {
functionGraphView()->setVerticalCursor(false);
functionGraphView()->setCursorView(&m_cursorView);
functionGraphView()->setBannerView(bannerView());
functionGraphView()->setAreaHighlight(NAN,NAN);
if (functionGraphView()->context() == nullptr) {
TextFieldDelegateApp * myApp = (TextFieldDelegateApp *)app();
functionGraphView()->setContext(myApp->localContext());

View File

@@ -10,12 +10,29 @@ FunctionGraphView::FunctionGraphView(InteractiveCurveViewRange * graphRange,
CurveViewCursor * cursor, BannerView * bannerView, View * cursorView) :
CurveView(graphRange, cursor, bannerView, cursorView),
m_selectedFunction(nullptr),
m_verticalCursor(false),
m_highlightedStart(NAN),
m_highlightedEnd(NAN),
m_shouldColorHighlighted(false),
m_xLabels{},
m_yLabels{},
m_context(nullptr)
{
}
void FunctionGraphView::reload() {
CurveView::reload();
if (!std::isnan(m_highlightedStart)) {
float pixelLowerBound = floatToPixel(Axis::Horizontal, m_highlightedStart)-2.0;
float pixelUpperBound = floatToPixel(Axis::Horizontal, m_highlightedEnd)+4.0;
/* We exclude the banner frame from the dirty zone to avoid unnecessary
* redrawing */
KDRect dirtyZone(KDRect(pixelLowerBound, 0, pixelUpperBound-pixelLowerBound,
bounds().height()-m_bannerView->bounds().height()));
markRectAsDirty(dirtyZone);
}
}
void FunctionGraphView::drawRect(KDContext * ctx, KDRect rect) const {
ctx->fillRect(rect, KDColorWhite);
drawGrid(ctx, rect);
@@ -41,6 +58,34 @@ void FunctionGraphView::selectFunction(Function * function) {
}
}
void FunctionGraphView::setVerticalCursor(bool verticalCursor) {
m_verticalCursor = verticalCursor;
}
void FunctionGraphView::setAreaHighlight(float start, float end) {
if (m_highlightedStart != start || m_highlightedEnd != end) {
reload();
m_highlightedStart = start;
m_highlightedEnd = end;
reload();
}
}
void FunctionGraphView::setAreaHighlightColor(bool highlightColor) {
if (m_shouldColorHighlighted != highlightColor) {
reload();
m_shouldColorHighlighted = highlightColor;
reload();
}
}
KDSize FunctionGraphView::cursorSize() {
if (m_verticalCursor) {
return KDSize(1, 0);
}
return CurveView::cursorSize();
}
char * FunctionGraphView::label(Axis axis, int index) const {
return (axis == Axis::Horizontal ? (char *)m_xLabels[index] : (char *)m_yLabels[index]);
}

View File

@@ -13,13 +13,22 @@ class FunctionGraphView : public CurveView {
public:
FunctionGraphView(InteractiveCurveViewRange * graphRange, CurveViewCursor * cursor,
BannerView * bannerView, View * cursorView);
void reload() override;
void drawRect(KDContext * ctx, KDRect rect) const override;
void setContext(Poincare::Context * context);
Poincare::Context * context() const;
void selectFunction(Function * function);
void setVerticalCursor(bool verticalCursor);
void setAreaHighlight(float start, float end);
void setAreaHighlightColor(bool highlightColor);
protected:
Function * m_selectedFunction;
bool m_verticalCursor;
float m_highlightedStart;
float m_highlightedEnd;
bool m_shouldColorHighlighted;
private:
KDSize cursorSize() override;
char * label(Axis axis, int index) const override;
char m_xLabels[k_maxNumberOfXLabels][Poincare::PrintFloat::bufferSizeForFloatsWithPrecision(Constant::ShortNumberOfSignificantDigits)];
char m_yLabels[k_maxNumberOfYLabels][Poincare::PrintFloat::bufferSizeForFloatsWithPrecision(Constant::ShortNumberOfSignificantDigits)];

View File

@@ -0,0 +1,324 @@
#include "sum_graph_controller.h"
#include "../apps_container.h"
#include "../../poincare/src/layout/baseline_relative_layout.h"
#include "../../poincare/src/layout/condensed_sum_layout.h"
#include "../../poincare/src/layout/string_layout.h"
#include "../../poincare/src/layout/horizontal_layout.h"
#include <assert.h>
#include <cmath>
#include <stdlib.h>
using namespace Poincare;
namespace Shared {
SumGraphController::SumGraphController(Responder * parentResponder, FunctionGraphView * graphView, InteractiveCurveViewRange * range, CurveViewCursor * cursor, char sumSymbol) :
SimpleInteractiveCurveViewController(parentResponder, range, graphView, cursor),
m_step(Step::FirstParameter),
m_startSum(NAN),
m_endSum(NAN),
m_function(nullptr),
m_graphView(graphView),
m_legendView(this, sumSymbol),
m_graphRange(range),
m_cursorView()
{
}
void SumGraphController::viewWillAppear() {
m_graphRange->panToMakePointVisible(m_cursor->x(), m_cursor->y(), k_cursorTopMarginRatio, k_cursorRightMarginRatio, k_cursorBottomMarginRatio, k_cursorLeftMarginRatio);
m_graphView->setVerticalCursor(true);
m_graphView->setBannerView(&m_legendView);
m_graphView->setCursorView(&m_cursorView);
m_graphView->setOkView(nullptr);
m_graphView->selectMainView(true);
m_graphView->setAreaHighlightColor(false);
m_graphView->setAreaHighlight(NAN, NAN);
m_graphView->reload();
m_startSum = m_cursor->x();
m_endSum = NAN;
m_step = Step::FirstParameter;
m_legendView.setLegendMessage(legendMessageAtStep(Step::FirstParameter), Step::FirstParameter);
m_legendView.setEditableZone(m_startSum);
m_legendView.setSumSymbol(m_step);
}
void SumGraphController::didEnterResponderChain(Responder * previousFirstResponder) {
app()->setFirstResponder(m_legendView.textField());
}
bool SumGraphController::handleEvent(Ion::Events::Event event) {
if (event == Ion::Events::Plus || event == Ion::Events::Minus) {
return handleZoom(event);
}
if ((int)m_step > 1 && event != Ion::Events::OK && event != Ion::Events::EXE && event != Ion::Events::Back) {
return false;
}
if (event == Ion::Events::Left && !m_legendView.textField()->isEditing()) {
if ((int)m_step > 0 && m_startSum >= m_cursor->x()) {
return false;
}
if (moveCursorHorizontallyToPosition(cursorNextStep(m_cursor->x(), -1))) {
m_graphView->reload();
return true;
}
return false;
}
if (event == Ion::Events::Right && !m_legendView.textField()->isEditing()) {
if (moveCursorHorizontallyToPosition(cursorNextStep(m_cursor->x(), 1))) {
m_graphView->reload();
return true;
}
return false;
}
if (event == Ion::Events::OK || event == Ion::Events::EXE) {
return handleEnter();
}
if (event == Ion::Events::Back && (int)m_step > 0) {
m_step = (Step)((int)m_step-1);
m_legendView.setLegendMessage(legendMessageAtStep(m_step), m_step);
if (m_step == Step::SecondParameter) {
app()->setFirstResponder(m_legendView.textField());
m_graphView->setAreaHighlightColor(false);
m_graphView->setCursorView(&m_cursorView);
m_graphView->reload();
m_endSum = m_cursor->x();
m_legendView.setEditableZone(m_endSum);
m_legendView.setSumSymbol(m_step, m_startSum);
}
if (m_step == Step::FirstParameter) {
m_graphView->setAreaHighlight(NAN,NAN);
moveCursorHorizontallyToPosition(m_startSum);
m_legendView.setLegendMessage(legendMessageAtStep(m_step), m_step);
m_legendView.setEditableZone(m_startSum);
m_legendView.setSumSymbol(m_step);
m_graphView->reload();
}
return true;
}
return false;
}
bool SumGraphController::moveCursorHorizontallyToPosition(double x) {
TextFieldDelegateApp * myApp = (TextFieldDelegateApp *)app();
if (m_function == nullptr) {
return false;
}
double y = m_function->evaluateAtAbscissa(x, myApp->localContext());
m_cursor->moveTo(x, y);
if (m_step == Step::FirstParameter) {
m_startSum = m_cursor->x();
m_legendView.setEditableZone(m_startSum);
}
if (m_step == Step::SecondParameter) {
m_graphView->setAreaHighlight(m_startSum, m_cursor->x());
m_endSum = m_cursor->x();
m_legendView.setEditableZone(m_endSum);
}
m_graphRange->panToMakePointVisible(x, y, k_cursorTopMarginRatio, k_cursorRightMarginRatio, k_cursorBottomMarginRatio, k_cursorLeftMarginRatio);
return true;
}
void SumGraphController::setFunction(Function * function) {
m_graphView->selectFunction(function);
m_function = function;
}
bool SumGraphController::textFieldDidFinishEditing(TextField * textField, const char * text, Ion::Events::Event event) {
AppsContainer * appsContainer = ((TextFieldDelegateApp *)app())->container();
Context * globalContext = appsContainer->globalContext();
double floatBody = Expression::approximateToScalar<double>(text, *globalContext);
if (std::isnan(floatBody) || std::isinf(floatBody)) {
app()->displayWarning(I18n::Message::UndefinedValue);
return false;
}
if (m_step == Step::SecondParameter && floatBody < m_startSum) {
app()->displayWarning(I18n::Message::ForbiddenValue);
return false;
}
if (moveCursorHorizontallyToPosition(floatBody)) {
handleEnter();
m_graphView->reload();
return true;
}
app()->displayWarning(I18n::Message::ForbiddenValue);
return false;
}
bool SumGraphController::textFieldDidAbortEditing(TextField * textField, const char * text) {
char buffer[PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)];
double parameter = NAN;
switch(m_step) {
case Step::FirstParameter:
parameter = m_startSum;
break;
case Step::SecondParameter:
parameter = m_endSum;
break;
default:
assert(false);
}
Complex<double>::convertFloatToText(parameter, buffer, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits, Expression::FloatDisplayMode::Decimal);
textField->setText(buffer);
return true;
}
bool SumGraphController::textFieldDidReceiveEvent(TextField * textField, Ion::Events::Event event) {
if ((event == Ion::Events::OK || event == Ion::Events::EXE) && !textField->isEditing()) {
return handleEnter();
}
if (m_step == Step::Result) {
return handleEvent(event);
}
return TextFieldDelegate::textFieldDidReceiveEvent(textField, event);
}
bool SumGraphController::handleEnter() {
if (m_step == Step::Result) {
StackViewController * stack = (StackViewController *)parentResponder();
stack->pop();
return true;
}
if (m_step == Step::FirstParameter) {
m_step = Step::SecondParameter;
m_graphView->setAreaHighlight(m_startSum,m_startSum);
m_endSum = m_cursor->x();
m_legendView.setEditableZone(m_endSum);
m_legendView.setSumSymbol(m_step, m_startSum);
m_legendView.setLegendMessage(legendMessageAtStep(m_step), m_step);
return true;
}
m_step = (Step)((int)m_step+1);
double sum = computeSum(m_startSum, m_endSum);
m_legendView.setSumSymbol(m_step, m_startSum, m_endSum, sum, m_function->name());
m_legendView.setLegendMessage(I18n::Message::Default, m_step);
m_graphView->setAreaHighlightColor(true);
m_graphView->setCursorView(nullptr);
m_graphView->reload();
myApp->setFirstResponder(this);
return true;
}
/* Legend View */
SumGraphController::LegendView::LegendView(SumGraphController * controller, char sumSymbol) :
m_sum(0.0f, 0.5f, KDColorBlack, Palette::GreyBright),
m_sumLayout(nullptr),
m_legend(KDText::FontSize::Small, I18n::Message::Default, 0.0f, 0.5f, KDColorBlack, Palette::GreyBright),
m_editableZone(controller, m_draftText, m_draftText, TextField::maxBufferSize(), controller, false, KDText::FontSize::Small, 0.0f, 0.5f, KDColorBlack, Palette::GreyBright),
m_sumSymbol(sumSymbol)
{
}
SumGraphController::LegendView::~LegendView() {
if (m_sumLayout != nullptr) {
delete m_sumLayout;
m_sumLayout = nullptr;
}
}
void SumGraphController::LegendView::drawRect(KDContext * ctx, KDRect rect) const {
ctx->fillRect(bounds(), Palette::GreyBright);
}
KDSize SumGraphController::LegendView::minimalSizeForOptimalDisplay() const {
return KDSize(0, k_legendHeight);
}
void SumGraphController::LegendView::setLegendMessage(I18n::Message message, Step step) {
m_legend.setMessage(message);
layoutSubviews(step);
}
void SumGraphController::LegendView::setEditableZone(double d) {
char buffer[PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)];
Complex<double>::convertFloatToText(d, buffer, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits, Expression::FloatDisplayMode::Decimal);
m_editableZone.setText(buffer);
}
void SumGraphController::LegendView::setSumSymbol(Step step, double start, double end, double result, const char * sequenceName) {
if (m_sumLayout) {
delete m_sumLayout;
m_sumLayout = nullptr;
}
const char sigma[] = {' ', m_sumSymbol};
if (step == Step::FirstParameter) {
m_sumLayout = new StringLayout(sigma, sizeof(sigma));
} else if (step == Step::SecondParameter) {
char buffer[PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)];
Complex<double>::convertFloatToText(start, buffer, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits, Expression::FloatDisplayMode::Decimal);
m_sumLayout = new CondensedSumLayout(new StringLayout(sigma, sizeof(sigma)), new StringLayout(buffer, strlen(buffer), KDText::FontSize::Small), nullptr);
} else {
char buffer[2+PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)];
Complex<double>::convertFloatToText(start, buffer, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits, Expression::FloatDisplayMode::Decimal);
ExpressionLayout * start = new StringLayout(buffer, strlen(buffer), KDText::FontSize::Small);
Complex<double>::convertFloatToText(end, buffer, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits, Expression::FloatDisplayMode::Decimal);
ExpressionLayout * end = new StringLayout(buffer, strlen(buffer), KDText::FontSize::Small);
m_sumLayout = new CondensedSumLayout(new StringLayout(sigma, sizeof(sigma)), start, end);
ExpressionLayout * childrenLayouts[3];
strlcpy(buffer, "= ", 3);
Complex<double>::convertFloatToText(result, buffer+2, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits);
childrenLayouts[2] = new StringLayout(buffer, strlen(buffer), KDText::FontSize::Small);
childrenLayouts[1] = new BaselineRelativeLayout(new StringLayout(sequenceName, 1, KDText::FontSize::Small), new StringLayout("n", 1, KDText::FontSize::Small), BaselineRelativeLayout::Type::Subscript);
childrenLayouts[0] = m_sumLayout;
m_sumLayout = new HorizontalLayout(childrenLayouts, 3);
}
m_sum.setExpression(m_sumLayout);
if (step == Step::Result) {
m_sum.setAlignment(0.5f, 0.5f);
} else {
m_sum.setAlignment(0.0f, 0.5f);
}
layoutSubviews(step);
}
int SumGraphController::LegendView::numberOfSubviews() const {
return 3;
}
View * SumGraphController::LegendView::subviewAtIndex(int index) {
assert(index >= 0 && index < 3);
if (index == 0) {
return &m_sum;
}
if (index == 1) {
return &m_editableZone;
}
return &m_legend;
}
void SumGraphController::LegendView::layoutSubviews() {
layoutSubviews(Step::FirstParameter);
}
void SumGraphController::LegendView::layoutSubviews(Step step) {
KDCoordinate width = bounds().width();
KDCoordinate heigth = bounds().height();
KDSize legendSize = m_legend.minimalSizeForOptimalDisplay();
if (legendSize.width() > 0) {
m_sum.setFrame(KDRect(0, k_symbolHeightMargin, width-legendSize.width(), m_sum.minimalSizeForOptimalDisplay().height()));
m_legend.setFrame(KDRect(width-legendSize.width(), 0, legendSize.width(), heigth));
} else {
m_sum.setFrame(bounds());
m_legend.setFrame(KDRectZero);
}
KDSize largeCharSize = KDText::charSize();
switch(step) {
case Step::FirstParameter:
m_editableZone.setFrame(KDRect(2*largeCharSize.width(), k_symbolHeightMargin+k_sigmaHeight/2, k_editableZoneWidth, k_editableZoneHeight));
return;
case Step::SecondParameter:
m_editableZone.setFrame(KDRect(2*largeCharSize.width(), k_symbolHeightMargin+k_sigmaHeight/2-k_editableZoneHeight, k_editableZoneWidth, k_editableZoneHeight));
return;
default:
m_editableZone.setFrame(KDRectZero);
}
}
}

View File

@@ -0,0 +1,89 @@
#ifndef SHARED_SUM_GRAPH_CONTROLLER_H
#define SHARED_SUM_GRAPH_CONTROLLER_H
#include <escher.h>
#include "function_graph_view.h"
#include "interactive_curve_view_range.h"
#include "vertical_cursor_view.h"
#include "curve_view_cursor.h"
#include "simple_interactive_curve_view_controller.h"
#include "function.h"
#include "text_field_delegate.h"
namespace Shared {
class SumGraphController : public SimpleInteractiveCurveViewController, public TextFieldDelegate {
public:
SumGraphController(Responder * parentResponder, FunctionGraphView * curveView, InteractiveCurveViewRange * range, CurveViewCursor * cursor, char sumSymbol);
void viewWillAppear() override;
void didEnterResponderChain(Responder * previousFirstResponder) override;
bool handleEvent(Ion::Events::Event event) override;
void setFunction(Function * function);
bool textFieldDidFinishEditing(TextField * textField, const char * text, Ion::Events::Event event) override;
bool textFieldDidAbortEditing(TextField * textField, const char * text) override;
bool textFieldDidReceiveEvent(TextField * textField, Ion::Events::Event event) override;
protected:
virtual bool moveCursorHorizontallyToPosition(double position);
enum class Step {
FirstParameter = 0,
SecondParameter = 1,
Result = 2
};
Step m_step;
double m_startSum;
double m_endSum;
Function * m_function;
private:
constexpr static float k_cursorTopMarginRatio = 0.07f; // (cursorHeight/2)/graphViewHeight
constexpr static float k_cursorRightMarginRatio = 0.04f; // (cursorWidth/2)/graphViewWidth
constexpr static float k_cursorBottomMarginRatio = 0.28f; // (cursorHeight/2+bannerHeigh)/graphViewHeight
constexpr static float k_cursorLeftMarginRatio = 0.04f; // (cursorWidth/2)/graphViewWidth
virtual double computeSum(double start, double end) = 0;
virtual I18n::Message legendMessageAtStep(Step step) = 0;
virtual double cursorNextStep(double position, int direction) = 0;
Shared::InteractiveCurveViewRange * interactiveCurveViewRange() override { return m_graphRange; }
Shared::CurveView * curveView() override { return m_graphView; }
TextFieldDelegateApp * textFieldDelegateApp() override {
return static_cast<TextFieldDelegateApp *>(app());
}
bool handleEnter();
class LegendView : public View {
public:
LegendView(SumGraphController * controller, char sumSymbol);
~LegendView();
LegendView(const LegendView& other) = delete;
LegendView(LegendView&& other) = delete;
LegendView& operator=(const LegendView& other) = delete;
LegendView& operator=(LegendView&& other) = delete;
TextField * textField() { return &m_editableZone; }
KDSize minimalSizeForOptimalDisplay() const override;
void drawRect(KDContext * ctx, KDRect rect) const override;
void setLegendMessage(I18n::Message message, Step step);
void setEditableZone(double d);
void setSumSymbol(Step step, double start = NAN, double end = NAN, double result = NAN, const char * sequenceName = nullptr);
private:
constexpr static KDCoordinate k_legendHeight = 35;
constexpr static KDCoordinate k_editableZoneWidth = 4*KDText::charSize(KDText::FontSize::Small).width();
constexpr static KDCoordinate k_editableZoneHeight = KDText::charSize(KDText::FontSize::Small).height();
constexpr static KDCoordinate k_symbolHeightMargin = 8;
constexpr static KDCoordinate k_sigmaHeight = 18;
int numberOfSubviews() const override;
View * subviewAtIndex(int index) override;
void layoutSubviews() override;
void layoutSubviews(Step step);
ExpressionView m_sum;
Poincare::ExpressionLayout * m_sumLayout;
MessageTextView m_legend;
TextField m_editableZone;
char m_draftText[TextField::maxBufferSize()];
char m_sumSymbol;
};
FunctionGraphView * m_graphView;
LegendView m_legendView;
InteractiveCurveViewRange * m_graphRange;
VerticalCursorView m_cursorView;
};
}
#endif

View File

@@ -0,0 +1,10 @@
#include "vertical_cursor_view.h"
namespace Shared {
void VerticalCursorView::drawRect(KDContext * ctx, KDRect rect) const {
KDCoordinate height = bounds().height();
ctx->fillRect(KDRect(0, 0, 1, height), KDColorBlack);
}
}

View File

@@ -0,0 +1,16 @@
#ifndef SHARED_VERTICAL_CURSOR_VIEW_H
#define SHARED_VERTICAL_CURSOR_VIEW_H
#include <escher.h>
namespace Shared {
class VerticalCursorView : public View {
public:
using View::View;
void drawRect(KDContext * ctx, KDRect rect) const override;
};
}
#endif