[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

@@ -10,7 +10,6 @@ app_objs += $(addprefix apps/sequence/,\
graph/graph_controller.o\
graph/graph_view.o\
graph/term_sum_controller.o\
graph/vertical_cursor_view.o\
list/list_controller.o\
list/list_parameter_controller.o\
list/sequence_toolbox.o\

View File

@@ -18,14 +18,6 @@ GraphController::GraphController(Responder * parentResponder, SequenceStore * se
m_graphRange->setDelegate(this);
}
void GraphController::viewWillAppear() {
m_view.setVerticalCursor(false);
m_view.setCursorView(&m_cursorView);
m_view.setBannerView(&m_bannerView);
m_view.setHighlight(-1.0f, -1.0f);
FunctionGraphController::viewWillAppear();
}
I18n::Message GraphController::emptyMessage() {
if (m_sequenceStore->numberOfDefinedFunctions() == 0) {
return I18n::Message::NoSequence;
@@ -42,7 +34,7 @@ BannerView * GraphController::bannerView() {
}
bool GraphController::handleEnter() {
m_termSumController.setSequence(m_sequenceStore->activeFunctionAtIndex(m_indexFunctionSelectedByCursor));
m_termSumController.setFunction(m_sequenceStore->activeFunctionAtIndex(m_indexFunctionSelectedByCursor));
return FunctionGraphController::handleEnter();
}

View File

@@ -14,7 +14,6 @@ namespace Sequence {
class GraphController : public Shared::FunctionGraphController {
public:
GraphController(Responder * parentResponder, SequenceStore * sequenceStore, CurveViewRange * graphRange, Shared::CurveViewCursor * cursor, uint32_t * modelVersion, uint32_t * rangeVersion, Poincare::Expression::AngleUnit * angleUnitVersion, ButtonRowController * header);
void viewWillAppear() override;
I18n::Message emptyMessage() override;
TermSumController * termSumController();
private:

View File

@@ -8,11 +8,7 @@ namespace Sequence {
GraphView::GraphView(SequenceStore * sequenceStore, InteractiveCurveViewRange * graphRange,
CurveViewCursor * cursor, BannerView * bannerView, View * cursorView) :
FunctionGraphView(graphRange, cursor, bannerView, cursorView),
m_sequenceStore(sequenceStore),
m_verticalCursor(false),
m_highlightedDotStart(-1),
m_highlightedDotEnd(-1),
m_shouldColorHighlighted(false)
m_sequenceStore(sequenceStore)
{
}
@@ -33,7 +29,7 @@ void GraphView::drawRect(KDContext * ctx, KDRect rect) const {
continue;
}
drawDot(ctx, rect, x, y, s->color());
if (x >= m_highlightedDotStart && x <= m_highlightedDotEnd && s == m_selectedFunction) {
if (x >= m_highlightedStart && x <= m_highlightedEnd && s == m_selectedFunction) {
KDColor color = m_shouldColorHighlighted ? s->color() : KDColorBlack;
if (y >= 0.0f) {
drawSegment(ctx, rect, Axis::Vertical, x, 0.0f, y, color, 1);
@@ -45,49 +41,8 @@ void GraphView::drawRect(KDContext * ctx, KDRect rect) const {
}
}
void GraphView::setVerticalCursor(bool verticalCursor) {
m_verticalCursor = verticalCursor;
}
void GraphView::reload() {
FunctionGraphView::reload();
if (m_highlightedDotStart >= 0) {
float pixelLowerBound = floatToPixel(Axis::Horizontal, m_highlightedDotStart)-1;
float pixelUpperBound = floatToPixel(Axis::Horizontal, m_highlightedDotEnd)+2;
/* 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 GraphView::setHighlight(int start, int end) {
if (m_highlightedDotStart != start || m_highlightedDotEnd != end) {
reload();
m_highlightedDotStart = start;
m_highlightedDotEnd = end;
reload();
}
}
void GraphView::setHighlightColor(bool highlightColor) {
if (m_shouldColorHighlighted != highlightColor) {
reload();
m_shouldColorHighlighted = highlightColor;
reload();
}
}
float GraphView::samplingRatio() const {
return 5.0f;
}
KDSize GraphView::cursorSize() {
if (m_verticalCursor) {
return KDSize(1, 0);
}
return CurveView::cursorSize();
}
}

View File

@@ -11,18 +11,9 @@ public:
GraphView(SequenceStore * sequenceStore, Shared::InteractiveCurveViewRange * graphRange,
Shared::CurveViewCursor * cursor, Shared::BannerView * bannerView, View * cursorView);
void drawRect(KDContext * ctx, KDRect rect) const override;
void setVerticalCursor(bool verticalCursor);
void reload() override;
void setHighlight(int start, int end);
void setHighlightColor(bool highlightColor);
private:
float samplingRatio() const override;
KDSize cursorSize() override;
SequenceStore * m_sequenceStore;
bool m_verticalCursor;
int m_highlightedDotStart;
int m_highlightedDotEnd;
bool m_shouldColorHighlighted;
};
}

View File

@@ -1,9 +1,6 @@
#include "term_sum_controller.h"
#include "../../shared/text_field_delegate.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 "../app.h"
#include <assert.h>
#include <cmath>
@@ -15,16 +12,7 @@ using namespace Poincare;
namespace Sequence {
TermSumController::TermSumController(Responder * parentResponder, GraphView * graphView, CurveViewRange * graphRange, CurveViewCursor * cursor) :
SimpleInteractiveCurveViewController(parentResponder, graphRange, graphView, cursor),
m_graphView(graphView),
m_legendView(),
m_graphRange(graphRange),
m_sequence(nullptr),
m_cursorView(),
m_bufferCursorPosition(0),
m_step(0),
m_startSum(-1),
m_endSum(-1)
SumGraphController(parentResponder, graphView, graphRange, cursor, Ion::Charset::CapitalSigma)
{
}
@@ -32,236 +20,32 @@ const char * TermSumController::title() {
return I18n::translate(I18n::Message::TermSum);
}
void TermSumController::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->setHighlightColor(false);
m_graphView->setHighlight(-1.0f,-1.0f);
m_graphView->reload();
m_bufferCursorPosition = 0;
m_startSum = -1;
m_endSum = -1;
m_step = 0;
m_legendView.setLegendMessage(I18n::Message::SelectFirstTerm);
m_legendView.setSumSubscript(m_cursor->x());
}
bool TermSumController::handleEvent(Ion::Events::Event event) {
if (event == Ion::Events::Plus || event == Ion::Events::Minus) {
return handleZoom(event);
}
if (m_step > 1 && event != Ion::Events::OK && event != Ion::Events::EXE && event != Ion::Events::Back) {
bool TermSumController::moveCursorHorizontallyToPosition(double position) {
if (position < 0.0) {
return false;
}
if (event == Ion::Events::Left) {
if (m_step > 0 && m_startSum >= m_cursor->x()) {
return false;
}
if (moveCursorHorizontallyToPosition(std::round(m_cursor->x()-1.0))) {
m_graphView->reload();
return true;
}
return false;
}
if (event == Ion::Events::Right) {
if (moveCursorHorizontallyToPosition(std::round(m_cursor->x()+1.0))) {
m_graphView->reload();
return true;
}
return false;
}
if (event.hasText() && event.text()[0] >= '0' && event.text()[0] <= '9') {
m_bufferCursorPosition = 10*m_bufferCursorPosition + event.text()[0]-'0';
if (m_step > 0 && m_bufferCursorPosition < m_startSum) {
return false;
}
if (moveCursorHorizontallyToPosition(m_bufferCursorPosition)) {
m_graphView->reload();
return true;
}
return false;
}
if (event == Ion::Events::OK || event == Ion::Events::EXE) {
if (m_step == 2) {
StackViewController * stack = (StackViewController *)parentResponder();
stack->pop();
return true;
}
if (m_step == 0) {
m_step++;
m_bufferCursorPosition = 0;
m_startSum = m_cursor->x();
m_graphView->setHighlight(m_startSum,m_startSum);
m_legendView.setSumSuperscript(m_startSum, m_cursor->x());
m_legendView.setLegendMessage(I18n::Message::SelectLastTerm);
return true;
}
m_step++;
m_endSum = m_cursor->x();
TextFieldDelegateApp * myApp = (TextFieldDelegateApp *)app();
double sum = m_sequence->sumOfTermsBetweenAbscissa(m_startSum, m_endSum, myApp->localContext());
m_legendView.setSumResult(m_sequence->name(), sum);
m_legendView.setLegendMessage(I18n::Message::Default);
m_graphView->setHighlightColor(true);
m_graphView->setCursorView(nullptr);
m_graphView->reload();
return true;
}
if (event == Ion::Events::Back && m_step > 0) {
m_step--;
m_bufferCursorPosition = 0;
if (m_step == 1) {
m_legendView.setLegendMessage(I18n::Message::SelectLastTerm);
m_graphView->setHighlightColor(false);
m_graphView->setCursorView(&m_cursorView);
m_graphView->reload();
m_legendView.setSumSuperscript(m_startSum, m_cursor->x());
}
if (m_step == 0) {
m_graphView->setHighlight(-1,-1);
moveCursorHorizontallyToPosition(m_startSum);
m_legendView.setLegendMessage(I18n::Message::SelectFirstTerm);
m_legendView.setSumSubscript(m_startSum);
m_graphView->reload();
}
return true;
}
return false;
return SumGraphController::moveCursorHorizontallyToPosition(std::round(position));
}
bool TermSumController::moveCursorHorizontallyToPosition(int position) {
if (position < 0) {
return false;
}
double x = position;
TextFieldDelegateApp * myApp = (TextFieldDelegateApp *)app();
if (m_sequence == nullptr) {
return false;
}
double y = m_sequence->evaluateAtAbscissa(x, myApp->localContext());
m_cursor->moveTo(x, y);
if (m_step == 0) {
m_legendView.setSumSubscript(m_cursor->x());
}
if (m_step == 1) {
m_graphView->setHighlight(m_startSum, m_cursor->x());
m_legendView.setSumSuperscript(m_startSum, m_cursor->x());
}
m_graphRange->panToMakePointVisible(x, y, k_cursorTopMarginRatio, k_cursorRightMarginRatio, k_cursorBottomMarginRatio, k_cursorLeftMarginRatio);
return true;
double TermSumController::computeSum(double start, double end) {
App * myApp = static_cast<App *>(app());
return static_cast<Sequence *>(m_function)->sumOfTermsBetweenAbscissa(m_startSum, m_endSum, myApp->localContext());
}
void TermSumController::setSequence(Sequence * sequence) {
m_graphView->selectFunction(sequence);
m_sequence = sequence;
}
CurveView * TermSumController::curveView() {
return m_graphView;
}
InteractiveCurveViewRange * TermSumController::interactiveCurveViewRange() {
return m_graphRange;
}
/* Legend View */
TermSumController::LegendView::LegendView() :
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)
{
}
TermSumController::LegendView::~LegendView() {
if (m_sumLayout != nullptr) {
delete m_sumLayout;
m_sumLayout = nullptr;
I18n::Message TermSumController::legendMessageAtStep(Step step) {
switch(step) {
case Step::FirstParameter:
return I18n::Message::SelectFirstTerm;
case Step::SecondParameter:
return I18n::Message::SelectLastTerm;
default:
return I18n::Message::Default;
}
}
void TermSumController::LegendView::drawRect(KDContext * ctx, KDRect rect) const {
ctx->fillRect(KDRect(0, bounds().height() - k_legendHeight, bounds().width(), k_legendHeight), Palette::GreyMiddle);
}
KDSize TermSumController::LegendView::minimalSizeForOptimalDisplay() const {
return KDSize(0, k_legendHeight);
}
void TermSumController::LegendView::setLegendMessage(I18n::Message message) {
m_legend.setMessage(message);
layoutSubviews();
}
void TermSumController::LegendView::setSumSubscript(float start) {
if (m_sumLayout) {
delete m_sumLayout;
m_sumLayout = nullptr;
}
const char sigma[] = {' ',Ion::Charset::CapitalSigma};
char buffer[PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)];
Complex<float>::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);
m_sum.setExpression(m_sumLayout);
m_sum.setAlignment(0.0f, 0.5f);
}
void TermSumController::LegendView::setSumSuperscript(float start, float end) {
if (m_sumLayout) {
delete m_sumLayout;
m_sumLayout = nullptr;
}
const char sigma[] = {' ', Ion::Charset::CapitalSigma};
char bufferStart[PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)];
Complex<float>::convertFloatToText(start, bufferStart, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits, Expression::FloatDisplayMode::Decimal);
char bufferEnd[PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)];
Complex<float>::convertFloatToText(end, bufferEnd, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits, Expression::FloatDisplayMode::Decimal);
m_sumLayout = new CondensedSumLayout(new StringLayout(sigma, sizeof(sigma)), new StringLayout(bufferStart, strlen(bufferStart), KDText::FontSize::Small), new StringLayout(bufferEnd, strlen(bufferEnd), KDText::FontSize::Small));
m_sum.setExpression(m_sumLayout);
m_sum.setAlignment(0.0f, 0.5f);
}
void TermSumController::LegendView::setSumResult(const char * sequenceName, double result) {
ExpressionLayout * childrenLayouts[3];
char buffer[2+PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)];
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);
m_sum.setAlignment(0.5f, 0.5f);
}
int TermSumController::LegendView::numberOfSubviews() const {
return 2;
}
View * TermSumController::LegendView::subviewAtIndex(int index) {
assert(index >= 0 && index < 2);
if (index == 0) {
return &m_sum;
}
return &m_legend;
}
void TermSumController::LegendView::layoutSubviews() {
KDCoordinate width = bounds().width();
KDCoordinate heigth = bounds().height();
KDSize legendSize = m_legend.minimalSizeForOptimalDisplay();
if (legendSize.width() > 0) {
m_sum.setFrame(KDRect(0, 0, width-legendSize.width(), heigth));
m_legend.setFrame(KDRect(width-legendSize.width(), 0, legendSize.width(), heigth));
return;
}
m_sum.setFrame(bounds());
m_legend.setFrame(KDRectZero);
double TermSumController::cursorNextStep(double x, int direction) {
double delta = direction > 0 ? 1.0 : -1.0;
return std::round(m_cursor->x()+delta);
}
}

View File

@@ -4,63 +4,19 @@
#include <escher.h>
#include "graph_view.h"
#include "curve_view_range.h"
#include "vertical_cursor_view.h"
#include "../../shared/curve_view_cursor.h"
#include "../../shared/simple_interactive_curve_view_controller.h"
#include "../../shared/sum_graph_controller.h"
namespace Sequence {
class TermSumController : public Shared::SimpleInteractiveCurveViewController {
class TermSumController : public Shared::SumGraphController {
public:
TermSumController(Responder * parentResponder, GraphView * graphView, CurveViewRange * graphRange, Shared::CurveViewCursor * cursor);
const char * title() override;
void viewWillAppear() override;
bool handleEvent(Ion::Events::Event event) override;
bool moveCursorHorizontallyToPosition(int position);
void setSequence(Sequence * sequence);
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
Shared::InteractiveCurveViewRange * interactiveCurveViewRange() override;
Shared::CurveView * curveView() override;
class LegendView : public View {
public:
LegendView();
~LegendView();
LegendView(const LegendView& other) = delete;
LegendView(LegendView&& other) = delete;
LegendView& operator=(const LegendView& other) = delete;
LegendView& operator=(LegendView&& other) = delete;
void drawRect(KDContext * ctx, KDRect rect) const override;
void setLegendMessage(I18n::Message message);
void setSumSubscript(float start);
void setSumSuperscript(float start, float end);
void setSumResult(const char * sequenceName, double result);
KDSize minimalSizeForOptimalDisplay() const override;
private:
constexpr static KDCoordinate k_legendHeight = 35;
void layoutSubviews() override;
int numberOfSubviews() const override;
View * subviewAtIndex(int index) override;
ExpressionView m_sum;
Poincare::ExpressionLayout * m_sumLayout;
MessageTextView m_legend;
};
GraphView * m_graphView;
LegendView m_legendView;
CurveViewRange * m_graphRange;
Sequence * m_sequence;
VerticalCursorView m_cursorView;
/* The user can move the cursor to an abscissa n by typing the right digits.
* To be able to go to abscissa represented by more than one digit, we record
* the value typed by the used up to now (if he typed '1' and '4',
* m_bufferCursorPosition = 14). */
int m_bufferCursorPosition;
int m_step;
int m_startSum;
int m_endSum;
bool moveCursorHorizontallyToPosition(double position) override;
double computeSum(double start, double end) override;
I18n::Message legendMessageAtStep(Step step) override;
double cursorNextStep(double position, int direction) override;
};
}

View File

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

View File

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