diff --git a/apps/calculation/additional_outputs/complex_graph_cell.cpp b/apps/calculation/additional_outputs/complex_graph_cell.cpp index aabe9b630..e4249e8f2 100644 --- a/apps/calculation/additional_outputs/complex_graph_cell.cpp +++ b/apps/calculation/additional_outputs/complex_graph_cell.cpp @@ -6,7 +6,7 @@ using namespace Poincare; namespace Calculation { ComplexGraphView::ComplexGraphView(ComplexModel * complexModel) : - CurveView(complexModel), + LabeledCurveView(complexModel), m_complex(complexModel) { } diff --git a/apps/calculation/additional_outputs/complex_graph_cell.h b/apps/calculation/additional_outputs/complex_graph_cell.h index bf440202f..7777c9991 100644 --- a/apps/calculation/additional_outputs/complex_graph_cell.h +++ b/apps/calculation/additional_outputs/complex_graph_cell.h @@ -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; }; diff --git a/apps/calculation/additional_outputs/trigonometry_graph_cell.h b/apps/calculation/additional_outputs/trigonometry_graph_cell.h index 1bf126707..aea81926a 100644 --- a/apps/calculation/additional_outputs/trigonometry_graph_cell.h +++ b/apps/calculation/additional_outputs/trigonometry_graph_cell.h @@ -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; }; diff --git a/apps/regression/graph_view.cpp b/apps/regression/graph_view.cpp index afdbdab0c..3b7b1371a 100644 --- a/apps/regression/graph_view.cpp +++ b/apps/regression/graph_view.cpp @@ -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]; -} - } diff --git a/apps/regression/graph_view.h b/apps/regression/graph_view.h index 17269ad1c..22db0b635 100644 --- a/apps/regression/graph_view.h +++ b/apps/regression/graph_view.h @@ -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]; }; } diff --git a/apps/shared/Makefile b/apps/shared/Makefile index e97b68ed0..98e4a8fe6 100644 --- a/apps/shared/Makefile +++ b/apps/shared/Makefile @@ -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 \ diff --git a/apps/shared/curve_view.h b/apps/shared/curve_view.h index 2512ec432..99d4ddd07 100644 --- a/apps/shared/curve_view.h +++ b/apps/shared/curve_view.h @@ -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 diff --git a/apps/shared/function_graph_view.cpp b/apps/shared/function_graph_view.cpp index ee8cd73fc..587a49fcb 100644 --- a/apps/shared/function_graph_view.cpp +++ b/apps/shared/function_graph_view.cpp @@ -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; diff --git a/apps/shared/function_graph_view.h b/apps/shared/function_graph_view.h index 0ec2f11cf..4e3f0a55a 100644 --- a/apps/shared/function_graph_view.h +++ b/apps/shared/function_graph_view.h @@ -2,14 +2,14 @@ #define SHARED_FUNCTION_GRAPH_VIEW_H #include -#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; }; diff --git a/apps/shared/labeled_curve_view.cpp b/apps/shared/labeled_curve_view.cpp new file mode 100644 index 000000000..2a10c6655 --- /dev/null +++ b/apps/shared/labeled_curve_view.cpp @@ -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]; + } +} + +} diff --git a/apps/shared/labeled_curve_view.h b/apps/shared/labeled_curve_view.h new file mode 100644 index 000000000..cc094702b --- /dev/null +++ b/apps/shared/labeled_curve_view.h @@ -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 diff --git a/apps/statistics/box_axis_view.cpp b/apps/statistics/box_axis_view.cpp index 61ecfd092..3d898c738 100644 --- a/apps/statistics/box_axis_view.cpp +++ b/apps/statistics/box_axis_view.cpp @@ -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]; -} - } diff --git a/apps/statistics/box_axis_view.h b/apps/statistics/box_axis_view.h index 2fbfe0df8..b3caef60d 100644 --- a/apps/statistics/box_axis_view.h +++ b/apps/statistics/box_axis_view.h @@ -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 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; }; diff --git a/apps/statistics/box_view.h b/apps/statistics/box_view.h index 6ce1a9275..b95cbbc29 100644 --- a/apps/statistics/box_view.h +++ b/apps/statistics/box_view.h @@ -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; diff --git a/apps/statistics/histogram_view.cpp b/apps/statistics/histogram_view.cpp index 5f0caa14d..9b1d028f4 100644 --- a/apps/statistics/histogram_view.cpp +++ b/apps/statistics/histogram_view.cpp @@ -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]; diff --git a/apps/statistics/histogram_view.h b/apps/statistics/histogram_view.h index 32117ffde..c6859097c 100644 --- a/apps/statistics/histogram_view.h +++ b/apps/statistics/histogram_view.h @@ -5,13 +5,13 @@ #include #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];