[apps/shared] Factorize CurveView::label

There was a lot of code duplication.
I removed the initialization of xLabel{} and yLabels{} because those are
scratch buffers that shouldn't be accessed before being written to
anyway.
This commit is contained in:
Romain Goyet
2020-03-10 18:29:01 -04:00
committed by LeaNumworks
parent ef249b0bdb
commit 2bf83c43a8
16 changed files with 86 additions and 60 deletions

View File

@@ -6,7 +6,7 @@ using namespace Poincare;
namespace Calculation {
ComplexGraphView::ComplexGraphView(ComplexModel * complexModel) :
CurveView(complexModel),
LabeledCurveView(complexModel),
m_complex(complexModel)
{
}

View File

@@ -1,24 +1,19 @@
#ifndef CALCULATION_ADDITIONAL_OUTPUTS_COMPLEX_GRAPH_CELL_H
#define CALCULATION_ADDITIONAL_OUTPUTS_COMPLEX_GRAPH_CELL_H
#include "../../shared/curve_view.h"
#include "../../shared/labeled_curve_view.h"
#include "complex_model.h"
#include "illustration_cell.h"
namespace Calculation {
class ComplexGraphView : public Shared::CurveView {
class ComplexGraphView : public Shared::LabeledCurveView {
public:
ComplexGraphView(ComplexModel * complexModel);
void drawRect(KDContext * ctx, KDRect rect) const override;
private:
char * label(Axis axis, int index) const override {
return (axis == Axis::Horizontal ? (char *)m_xLabels[index] : (char *)m_yLabels[index]);
}
// '-' + significant digits + ".E-" + 2 digits (the represented dot is a float, so it is bounded by 1E38 and 1E-38
size_t labelMaxGlyphLengthSize() const override { return 1 + Poincare::Preferences::VeryShortNumberOfSignificantDigits + 3 + 2; }
char m_xLabels[k_maxNumberOfXLabels][k_labelBufferMaxSize];
char m_yLabels[k_maxNumberOfYLabels][k_labelBufferMaxSize];
ComplexModel * m_complex;
};

View File

@@ -12,7 +12,6 @@ public:
TrigonometryGraphView(TrigonometryModel * model);
void drawRect(KDContext * ctx, KDRect rect) const override;
private:
char * label(Axis axis, int index) const override { return nullptr; }
TrigonometryModel * m_model;
};

View File

@@ -9,10 +9,8 @@ using namespace Shared;
namespace Regression {
GraphView::GraphView(Store * store, CurveViewCursor * cursor, BannerView * bannerView, Shared::CursorView * cursorView) :
CurveView(store, cursor, bannerView, cursorView),
m_store(store),
m_xLabels{},
m_yLabels{}
LabeledCurveView(store, cursor, bannerView, cursorView),
m_store(store)
{
}
@@ -41,13 +39,4 @@ void GraphView::drawRect(KDContext * ctx, KDRect rect) const {
}
}
char * GraphView::label(Axis axis, int index) const {
if (axis == Axis::Vertical) {
assert(index < k_maxNumberOfXLabels);
return (char *)m_yLabels[index];
}
assert(index < k_maxNumberOfYLabels);
return (char *)m_xLabels[index];
}
}

View File

@@ -3,19 +3,16 @@
#include "store.h"
#include "../constant.h"
#include "../shared/curve_view.h"
#include "../shared/labeled_curve_view.h"
namespace Regression {
class GraphView : public Shared::CurveView {
class GraphView : public Shared::LabeledCurveView {
public:
GraphView(Store * store, Shared::CurveViewCursor * cursor, Shared::BannerView * bannerView, Shared::CursorView * cursorView);
void drawRect(KDContext * ctx, KDRect rect) const override;
private:
char * label(Axis axis, int index) const override;
Store * m_store;
char m_xLabels[k_maxNumberOfXLabels][k_labelBufferMaxSize];
char m_yLabels[k_maxNumberOfYLabels][k_labelBufferMaxSize];
};
}

View File

@@ -45,6 +45,7 @@ app_shared_src = $(addprefix apps/shared/,\
interactive_curve_view_controller.cpp \
interval.cpp \
interval_parameter_controller.cpp \
labeled_curve_view.cpp \
language_controller.cpp \
layout_field_delegate.cpp \
list_parameter_controller.cpp \

View File

@@ -100,7 +100,7 @@ private:
float min(Axis axis) const;
float max(Axis axis) const;
float gridUnit(Axis axis) const;
virtual char * label(Axis axis, int index) const = 0;
virtual char * label(Axis axis, int index) const { return nullptr; }
virtual size_t labelMaxGlyphLengthSize() const { return k_labelBufferMaxGlyphLength; }
int numberOfLabels(Axis axis) const;
/* Recursively join two dots (dichotomy). The method stops when the

View File

@@ -8,13 +8,11 @@ namespace Shared {
FunctionGraphView::FunctionGraphView(InteractiveCurveViewRange * graphRange,
CurveViewCursor * cursor, BannerView * bannerView, CursorView * cursorView) :
CurveView(graphRange, cursor, bannerView, cursorView),
LabeledCurveView(graphRange, cursor, bannerView, cursorView),
m_selectedRecord(),
m_highlightedStart(NAN),
m_highlightedEnd(NAN),
m_shouldColorHighlighted(false),
m_xLabels{},
m_yLabels{},
m_context(nullptr)
{
}
@@ -67,10 +65,6 @@ void FunctionGraphView::setAreaHighlightColor(bool highlightColor) {
}
}
char * FunctionGraphView::label(Axis axis, int index) const {
return (axis == Axis::Horizontal ? (char *)m_xLabels[index] : (char *)m_yLabels[index]);
}
void FunctionGraphView::reloadBetweenBounds(float start, float end) {
if (start == end) {
return;

View File

@@ -2,14 +2,14 @@
#define SHARED_FUNCTION_GRAPH_VIEW_H
#include <escher.h>
#include "curve_view.h"
#include "labeled_curve_view.h"
#include "function.h"
#include "../constant.h"
#include "interactive_curve_view_range.h"
namespace Shared {
class FunctionGraphView : public CurveView {
class FunctionGraphView : public LabeledCurveView {
public:
FunctionGraphView(InteractiveCurveViewRange * graphRange, CurveViewCursor * cursor,
BannerView * bannerView, CursorView * cursorView);
@@ -26,9 +26,6 @@ protected:
float m_highlightedEnd;
bool m_shouldColorHighlighted;
private:
char * label(Axis axis, int index) const override;
char m_xLabels[k_maxNumberOfXLabels][k_labelBufferMaxSize];
char m_yLabels[k_maxNumberOfYLabels][k_labelBufferMaxSize];
Poincare::Context * m_context;
};

View File

@@ -0,0 +1,31 @@
#include "labeled_curve_view.h"
namespace Shared {
char * HorizontallyLabeledCurveView::label(Axis axis, int index) const {
if (axis == Axis::Horizontal) {
assert(index < k_maxNumberOfXLabels);
return m_xLabels[index];
}
return nullptr;
}
char * VerticallyLabeledCurveView::label(Axis axis, int index) const {
if (axis == Axis::Vertical) {
assert(index < k_maxNumberOfYLabels);
return m_yLabels[index];
}
return nullptr;
}
char * LabeledCurveView::label(Axis axis, int index) const {
if (axis == Axis::Horizontal) {
assert(index < k_maxNumberOfXLabels);
return m_xLabels[index];
} else {
assert(index < k_maxNumberOfYLabels);
return m_yLabels[index];
}
}
}

View File

@@ -0,0 +1,37 @@
#ifndef SHARED_LABELED_CURVE_VIEW_H
#define SHARED_LABELED_CURVE_VIEW_H
#include "curve_view.h"
/* This CurveView subclass provides label storage for common use cases */
namespace Shared {
class HorizontallyLabeledCurveView : public CurveView {
public:
using CurveView::CurveView;
private:
char * label(Axis axis, int index) const override;
mutable char m_xLabels[k_maxNumberOfXLabels][k_labelBufferMaxSize];
};
class VerticallyLabeledCurveView : public CurveView {
public:
using CurveView::CurveView;
private:
char * label(Axis axis, int index) const override;
mutable char m_yLabels[k_maxNumberOfYLabels][k_labelBufferMaxSize];
};
class LabeledCurveView : public CurveView {
public:
using CurveView::CurveView;
private:
char * label(Axis axis, int index) const override;
mutable char m_xLabels[k_maxNumberOfXLabels][k_labelBufferMaxSize];
mutable char m_yLabels[k_maxNumberOfYLabels][k_labelBufferMaxSize];
};
}
#endif

View File

@@ -12,8 +12,4 @@ void BoxAxisView::drawRect(KDContext * ctx, KDRect rect) const {
drawLabelsAndGraduations(ctx, rect, Axis::Horizontal, false, false, true, k_axisMargin);
}
char * BoxAxisView::label(Axis axis, int index) const {
return axis == Axis::Vertical ? nullptr : (char *)m_labels[index];
}
}

View File

@@ -3,24 +3,21 @@
#include "box_range.h"
#include "store.h"
#include "../shared/curve_view.h"
#include "../shared/labeled_curve_view.h"
#include "../constant.h"
#include <poincare/print_float.h>
namespace Statistics {
class BoxAxisView : public Shared::CurveView {
class BoxAxisView : public Shared::HorizontallyLabeledCurveView {
public:
BoxAxisView(Store * store) :
CurveView(&m_boxRange),
m_labels{},
HorizontallyLabeledCurveView(&m_boxRange),
m_boxRange(BoxRange(store))
{}
void drawRect(KDContext * ctx, KDRect rect) const override;
private:
constexpr static KDCoordinate k_axisMargin = 3;
char * label(Axis axis, int index) const override;
char m_labels[k_maxNumberOfXLabels][k_labelBufferMaxSize];
BoxRange m_boxRange;
};

View File

@@ -37,7 +37,6 @@ private:
static constexpr KDCoordinate k_quantileBarWidth = 2;
KDCoordinate boxLowerBoundPixel() const;
KDCoordinate boxUpperBoundPixel() const;
char * label(Axis axis, int index) const override { return nullptr; }
Store * m_store;
BoxRange m_boxRange;
int m_series;

View File

@@ -8,10 +8,9 @@ using namespace Shared;
namespace Statistics {
HistogramView::HistogramView(HistogramController * controller, Store * store, int series, Shared::BannerView * bannerView, KDColor selectedHistogramColor, KDColor notSelectedHistogramColor, KDColor selectedBarColor) :
CurveView(store, nullptr, bannerView, nullptr),
HorizontallyLabeledCurveView(store, nullptr, bannerView, nullptr),
m_controller(controller),
m_store(store),
m_labels{},
m_highlightedBarStart(NAN),
m_highlightedBarEnd(NAN),
m_series(series),
@@ -63,10 +62,6 @@ void HistogramView::setHighlight(float start, float end) {
}
}
char * HistogramView::label(Axis axis, int index) const {
return axis == Axis::Vertical ? nullptr : (char *)m_labels[index];
}
float HistogramView::EvaluateHistogramAtAbscissa(float abscissa, void * model, void * context) {
Store * store = (Store *)model;
float totalSize = ((float *)context)[0];

View File

@@ -5,13 +5,13 @@
#include <poincare/print_float.h>
#include "store.h"
#include "../constant.h"
#include "../shared/curve_view.h"
#include "../shared/labeled_curve_view.h"
namespace Statistics {
class HistogramController;
class HistogramView : public Shared::CurveView {
class HistogramView : public Shared::HorizontallyLabeledCurveView {
public:
HistogramView(HistogramController * controller, Store * store, int series, Shared::BannerView * bannerView, KDColor selectedHistogramColor = Palette::Select, KDColor notSelectedHistogramColor = Palette::GreyMiddle, KDColor selectedBarColor = Palette::YellowDark);
int series() const { return m_series; }
@@ -21,7 +21,6 @@ public:
void setHighlight(float start, float end);
void setDisplayLabels(bool display) { m_displayLabels = display; }
private:
char * label(Axis axis, int index) const override;
HistogramController * m_controller;
Store * m_store;
char m_labels[k_maxNumberOfXLabels][k_labelBufferMaxSize];