[apps/regression] Create a model data inheriting from the Apps::data

Change-Id: Ie0aed12437f63176329625da30986e4eff91d877
This commit is contained in:
Émilie Feral
2016-12-29 18:08:47 +01:00
parent 9c1095092a
commit 764f12124b
7 changed files with 228 additions and 155 deletions

View File

@@ -1,7 +1,7 @@
app_objs += $(addprefix apps/regression/,\
app.o\
calculation_controller.o\
data_controller.o\
data.o\
graph_controller.o\
)

View File

@@ -5,13 +5,14 @@ namespace Regression {
App::App(Container * container) :
TextFieldDelegateApp(container, &m_tabViewController, "Resgression", ImageStore::RegressionIcon),
m_data(),
m_calculationController(CalculationController(&m_calculationAlternateEmptyViewController)),
m_calculationAlternateEmptyViewController(AlternateEmptyViewController(&m_tabViewController, &m_calculationController, &m_calculationController)),
m_graphController(GraphController(&m_graphHeader, &m_graphHeader)),
m_graphHeader(HeaderViewController(&m_graphAlternateEmptyViewController, &m_graphController, &m_graphController)),
m_graphAlternateEmptyViewController(AlternateEmptyViewController(nullptr, &m_graphHeader, &m_graphController)),
m_graphStackViewController(StackViewController(&m_tabViewController, &m_graphAlternateEmptyViewController)),
m_dataController(DataController(nullptr)),
m_dataController(DataController(nullptr, &m_data)),
m_dataStackViewController(StackViewController(&m_tabViewController, &m_dataController)),
m_tabViewController(&m_modalViewController, &m_dataStackViewController, &m_graphStackViewController, &m_calculationAlternateEmptyViewController)
{

View File

@@ -3,7 +3,8 @@
#include <escher.h>
#include "../text_field_delegate_app.h"
#include "data_controller.h"
#include "data.h"
#include "../data_controller.h"
#include "graph_controller.h"
#include "calculation_controller.h"
@@ -13,13 +14,14 @@ class App : public TextFieldDelegateApp {
public:
App(Container * container);
private:
Data m_data;
CalculationController m_calculationController;
AlternateEmptyViewController m_calculationAlternateEmptyViewController;
GraphController m_graphController;
HeaderViewController m_graphHeader;
AlternateEmptyViewController m_graphAlternateEmptyViewController;
StackViewController m_graphStackViewController;
DataController m_dataController;
::DataController m_dataController;
StackViewController m_dataStackViewController;
TabViewController m_tabViewController;
};

161
apps/regression/data.cpp Normal file
View File

@@ -0,0 +1,161 @@
#include "data.h"
#include <assert.h>
#include <float.h>
#include <math.h>
#include <string.h>
namespace Regression {
Data::Data() :
::Data(),
m_xCursorPosition(NAN),
m_yCursorPosition(NAN),
m_xMin(0.0f),
m_xMax(10.0f),
m_yMin(0.0f),
m_yMax(10.0f),
m_xGridUnit(1.0f),
m_yGridUnit(1.0f)
{
}
/* Raw numeric data */
float Data::xValueAtIndex(int index) {
assert(index < m_numberOfPairs);
return m_xValues[index];
}
float Data::yValueAtIndex(int index) {
assert(index < m_numberOfPairs);
return m_yValues[index];
}
void Data::setXValueAtIndex(float value, int index) {
if (index >= k_maxNumberOfPairs) {
return;
}
m_xValues[index] = value;
if (index >= m_numberOfPairs) {
m_yValues[index] = 0.0f;
m_numberOfPairs++;
}
initCursorPosition();
initWindowParameters();
}
void Data::setYValueAtIndex(float value, int index) {
if (index >= k_maxNumberOfPairs) {
return;
}
m_yValues[index] = value;
if (index >= m_numberOfPairs) {
m_xValues[index] = 0.0f;
m_numberOfPairs++;
}
initCursorPosition();
initWindowParameters();
}
void Data::deletePairAtIndex(int index) {
m_numberOfPairs--;
for (int k = index; k < m_numberOfPairs; k++) {
m_xValues[k] = m_xValues[k+1];
m_yValues[k] = m_yValues[k+1];
}
m_xValues[m_numberOfPairs] = 0.0f;
m_yValues[m_numberOfPairs] = 0.0f;
initCursorPosition();
initWindowParameters();
}
/* Cursor */
float Data::xCursorPosition() {
return m_xCursorPosition;
}
float Data::yCursorPosition() {
return m_yCursorPosition;
}
/* CurveViewWindow */
float Data::xMin() {
return m_xMin;
}
float Data::xMax() {
return m_xMax;
}
float Data::yMin() {
return m_yMin;
}
float Data::yMax() {
return m_yMax;
}
float Data::xGridUnit() {
return m_xGridUnit;
}
float Data::yGridUnit() {
return m_yGridUnit;
}
float Data::maxXValue() {
float max = -FLT_MAX;
for (int k = 0; k < m_numberOfPairs; k++) {
if (m_xValues[k] > max) {
max = m_xValues[k];
}
}
return max;
}
float Data::maxYValue() {
float max = -FLT_MAX;
for (int k = 0; k < m_numberOfPairs; k++) {
if (m_yValues[k] > max) {
max = m_yValues[k];
}
}
return max;
}
float Data::minXValue() {
float min = FLT_MAX;
for (int k = 0; k < m_numberOfPairs; k++) {
if (m_xValues[k] < min) {
min = m_xValues[k];
}
}
return min;
}
float Data::minYValue() {
float min = FLT_MAX;
for (int k = 0; k < m_numberOfPairs; k++) {
if (m_yValues[k] < min) {
min = m_yValues[k];
}
}
return min;
}
void Data::initCursorPosition() {
m_xCursorPosition = (m_xMin+m_xMax)/2.0f;
}
void Data::initWindowParameters() {
m_xMin = minXValue();
m_xMax = maxXValue();
m_yMin = minYValue();
m_yMax = maxYValue();
m_xGridUnit = computeGridUnit(Axis::X, m_xMin, m_xMax);
m_yGridUnit = computeGridUnit(Axis::Y, m_yMin, m_yMax);
}
}

60
apps/regression/data.h Normal file
View File

@@ -0,0 +1,60 @@
#ifndef REGRESSION_DATA_H
#define REGRESSION_DATA_H
#include "../curve_view_window.h"
#include "../data.h"
namespace Regression {
class Data : public CurveViewWindow, public ::Data {
public:
Data();
// Delete the implicit copy constructor: the object is heavy
Data(const Data&) = delete;
// Raw numeric data
float xValueAtIndex(int index) override;
float yValueAtIndex(int index) override;
void setXValueAtIndex(float value, int index) override;
void setYValueAtIndex(float value, int index) override;
void deletePairAtIndex(int index) override;
// Cursor
float xCursorPosition();
float yCursorPosition();
//CurveViewWindow
float xMin() override;
float xMax() override;
float yMin() override;
float yMax() override;
float xGridUnit() override;
float yGridUnit() override;
constexpr static int k_maxNumberOfPairs = 500;
private:
float maxXValue();
float maxYValue();
float minXValue();
float minYValue();
void initWindowParameters();
void initCursorPosition();
// Raw numeric data
int m_xValues[k_maxNumberOfPairs];
float m_yValues[k_maxNumberOfPairs];
// Cursor
float m_xCursorPosition;
float m_yCursorPosition;
// Window bounds of the data
float m_xMin;
float m_xMax;
float m_yMin;
float m_yMax;
float m_xGridUnit;
float m_yGridUnit;
};
}
#endif

View File

@@ -1,112 +0,0 @@
#include "data_controller.h"
#include "app.h"
#include "../apps_container.h"
#include "../constant.h"
#include <assert.h>
namespace Regression {
DataController::DataController(Responder * parentResponder) :
EditableCellTableViewController(parentResponder, Metric::TopMargin, Metric::RightMargin, Metric::BottomMargin, Metric::LeftMargin),
m_editableCells{EvenOddEditableTextCell(&m_selectableTableView, this, m_draftTextBuffer), EvenOddEditableTextCell(&m_selectableTableView, this, m_draftTextBuffer), EvenOddEditableTextCell(&m_selectableTableView, this, m_draftTextBuffer), EvenOddEditableTextCell(&m_selectableTableView, this, m_draftTextBuffer), EvenOddEditableTextCell(&m_selectableTableView, this, m_draftTextBuffer),
EvenOddEditableTextCell(&m_selectableTableView, this, m_draftTextBuffer), EvenOddEditableTextCell(&m_selectableTableView, this, m_draftTextBuffer), EvenOddEditableTextCell(&m_selectableTableView, this, m_draftTextBuffer), EvenOddEditableTextCell(&m_selectableTableView, this, m_draftTextBuffer), EvenOddEditableTextCell(&m_selectableTableView, this, m_draftTextBuffer),
EvenOddEditableTextCell(&m_selectableTableView, this, m_draftTextBuffer), EvenOddEditableTextCell(&m_selectableTableView, this, m_draftTextBuffer), EvenOddEditableTextCell(&m_selectableTableView, this, m_draftTextBuffer), EvenOddEditableTextCell(&m_selectableTableView, this, m_draftTextBuffer), EvenOddEditableTextCell(&m_selectableTableView, this, m_draftTextBuffer),
EvenOddEditableTextCell(&m_selectableTableView, this, m_draftTextBuffer), EvenOddEditableTextCell(&m_selectableTableView, this, m_draftTextBuffer), EvenOddEditableTextCell(&m_selectableTableView, this, m_draftTextBuffer), EvenOddEditableTextCell(&m_selectableTableView, this, m_draftTextBuffer), EvenOddEditableTextCell(&m_selectableTableView, this, m_draftTextBuffer)}
{
}
const char * DataController::title() const {
return "Donnees";
}
int DataController::numberOfColumns() {
return 2;
};
KDCoordinate DataController::columnWidth(int i) {
return k_cellWidth;
}
KDCoordinate DataController::cumulatedWidthFromIndex(int i) {
return i*k_cellWidth;
}
int DataController::indexFromCumulatedWidth(KDCoordinate offsetX) {
return (offsetX-1) / k_cellWidth;
}
TableViewCell * DataController::reusableCell(int index, int type) {
assert(index >= 0);
switch (type) {
case 0:
assert(index < k_numberOfTitleCells);
return &m_titleCells[index];
case 1:
assert(index < k_maxNumberOfEditableCells);
return &m_editableCells[index];
default:
assert(false);
return nullptr;
}
}
int DataController::reusableCellCount(int type) {
if (type == 0) {
return k_numberOfTitleCells;
}
return k_maxNumberOfEditableCells;
}
void DataController::willDisplayCellAtLocation(TableViewCell * cell, int i, int j) {
EditableCellTableViewController::willDisplayCellAtLocation(cell, i, j);
if (cellAtLocationIsEditable(i, j)) {
return;
}
EvenOddPointerTextCell * mytitleCell = (EvenOddPointerTextCell *)cell;
if (i == 0) {
mytitleCell->setText("Valeurs");
return;
}
mytitleCell->setText("Effectifs");
}
int DataController::typeAtLocation(int i, int j) {
return j!=0;
}
bool DataController::handleEvent(Ion::Events::Event event) {
if (event == Ion::Events::Up) {
m_selectableTableView.deselectTable();
assert(m_selectableTableView.selectedRow() == -1);
app()->setFirstResponder(tabController());
return true;
}
return false;
}
Responder * DataController::tabController() const {
return (parentResponder()->parentResponder());
}
bool DataController::cellAtLocationIsEditable(int columnIndex, int rowIndex) {
if (rowIndex > 0) {
return true;
}
return false;
}
void DataController::setDataAtLocation(float floatBody, int columnIndex, int rowIndex) {
}
float DataController::dataAtLocation(int columnIndex, int rowIndex) {
}
int DataController::numberOfElements() {
return 8;
}
int DataController::maxNumberOfElements() const {
return 8;
}
}

View File

@@ -1,39 +0,0 @@
#ifndef REGRESSION_DATA_CONTROLLER_H
#define REGRESSION_DATA_CONTROLLER_H
#include <escher.h>
#include "../editable_cell_table_view_controller.h"
namespace Regression {
class DataController : public EditableCellTableViewController {
public:
DataController(Responder * parentResponder);
const char * title() const override;
int numberOfColumns() override;
void willDisplayCellAtLocation(TableViewCell * cell, int i, int j) override;
KDCoordinate columnWidth(int i) override;
KDCoordinate cumulatedWidthFromIndex(int i) override;
int indexFromCumulatedWidth(KDCoordinate offsetX) override;
TableViewCell * reusableCell(int index, int type) override;
int reusableCellCount(int type) override;
int typeAtLocation(int i, int j) override;
bool handleEvent(Ion::Events::Event event) override;
private:
static constexpr KDCoordinate k_cellWidth = 100;
constexpr static int k_maxNumberOfEditableCells = 20;
constexpr static int k_numberOfTitleCells = 2;
Responder * tabController() const;
bool cellAtLocationIsEditable(int columnIndex, int rowIndex) override;
void setDataAtLocation(float floatBody, int columnIndex, int rowIndex) override;
float dataAtLocation(int columnIndex, int rowIndex) override;
int numberOfElements() override;
int maxNumberOfElements() const override;
char m_draftTextBuffer[EditableTextCell::k_bufferLength];
EvenOddEditableTextCell m_editableCells[k_maxNumberOfEditableCells];
EvenOddPointerTextCell m_titleCells[k_numberOfTitleCells];
};
}
#endif