[apps/probability] Move Calculation model to snapshot

Change-Id: If04e331a7ee81667c7aa8c302479d054189dd63a
This commit is contained in:
Émilie Feral
2017-05-18 17:04:49 +02:00
parent e814b0c0c0
commit 1cc75e5d76
12 changed files with 51 additions and 58 deletions

View File

@@ -20,13 +20,16 @@ const Image * App::Descriptor::icon() {
}
App::Snapshot::Snapshot() :
m_law{}
m_law{},
m_calculation{}
{
new(m_law) BinomialLaw();
new(m_calculation) LeftIntegralCalculation();
}
App::Snapshot::~Snapshot() {
law()->~Law();
calculation()->~Calculation();
}
App * App::Snapshot::unpack(Container * container) {
@@ -42,9 +45,13 @@ Law * App::Snapshot::law() {
return (Law *)m_law;
}
Calculation * App::Snapshot::calculation() {
return (Calculation *)m_calculation;
}
App::App(Container * container, Snapshot * snapshot) :
TextFieldDelegateApp(container, snapshot, &m_stackViewController),
m_lawController(nullptr, snapshot->law()),
m_lawController(nullptr, snapshot->law(), snapshot->calculation()),
m_stackViewController(&m_modalViewController, &m_lawController)
{
}

View File

@@ -10,6 +10,9 @@
#include "law/normal_law.h"
#include "law/poisson_law.h"
#include "law/uniform_law.h"
#include "calculation/left_integral_calculation.h"
#include "calculation/right_integral_calculation.h"
#include "calculation/finite_integral_calculation.h"
constexpr static size_t max(const int * data, int seed = 0) {
return (*data == 0 ? seed : max(data+1, *data > seed ? *data : seed));
@@ -32,10 +35,14 @@ public:
App * unpack(Container * container) override;
Descriptor * descriptor() override;
Law * law();
Calculation * calculation();
private:
constexpr static int k_lawSizes[] = {sizeof(BinomialLaw),sizeof(ExponentialLaw), sizeof(NormalLaw), sizeof(PoissonLaw), sizeof(UniformLaw), 0};
constexpr static size_t k_lawSize = max(k_lawSizes);
char m_law[k_lawSize];
constexpr static int k_calculationSizes[] = {sizeof(LeftIntegralCalculation),sizeof(FiniteIntegralCalculation), sizeof(RightIntegralCalculation), 0};
constexpr static size_t k_calculationSize = max(k_calculationSizes);
char m_calculation[k_calculationSize];
};
private:
App(Container * container, Snapshot * snapshot);

View File

@@ -9,8 +9,8 @@ class Calculation {
public:
enum class Type : uint8_t{
LeftIntegral,
RightIntegral,
FiniteIntegral
FiniteIntegral,
RightIntegral
};
Calculation();
virtual ~Calculation() = default;

View File

@@ -14,7 +14,7 @@ namespace Probability {
CalculationController::ContentView::ContentView(Responder * parentResponder, CalculationController * calculationController, Calculation * calculation, Law * law) :
m_titleView(MessageTextView(KDText::FontSize::Small, I18n::Message::ComputeProbability, 0.5f, 0.5f, Palette::GreyDark, Palette::WallScreen)),
m_lawCurveView(LawCurveView(law)),
m_lawCurveView(LawCurveView(law, calculation)),
m_imageTableView(ImageTableView(parentResponder, calculation, calculationController)),
m_calculationCell{EditableTextCell(parentResponder, calculationController, m_draftTextBuffer),
EditableTextCell(parentResponder, calculationController, m_draftTextBuffer),
@@ -23,12 +23,6 @@ CalculationController::ContentView::ContentView(Responder * parentResponder, Cal
{
}
void CalculationController::ContentView::setCalculation(Calculation * calculation, int index) {
m_calculation = calculation;
m_lawCurveView.setCalculation(calculation);
m_imageTableView.setCalculation(calculation, index);
}
int CalculationController::ContentView::numberOfSubviews() const {
return 2*m_calculation->numberOfParameters() + 3;
}
@@ -130,21 +124,15 @@ EditableTextCell * CalculationController::ContentView::calculationCellAtIndex(in
return &m_calculationCell[index];
}
CalculationController::CalculationController(Responder * parentResponder, Law * law) :
CalculationController::CalculationController(Responder * parentResponder, Law * law, Calculation * calculation) :
ViewController(parentResponder),
m_calculation(new LeftIntegralCalculation()),
m_calculation(calculation),
m_contentView(ContentView(this, this, m_calculation, law)),
m_law(law),
m_highlightedSubviewIndex(1)
{
assert(law != nullptr);
}
CalculationController::~CalculationController() {
if (m_calculation) {
delete m_calculation;
m_calculation = nullptr;
}
assert(calculation != nullptr);
}
View * CalculationController::view() {
@@ -160,26 +148,25 @@ void CalculationController::reload() {
m_contentView.lawCurveView()->reload();
}
void CalculationController::setCalculationAccordingToIndex(int index) {
if (m_calculation != nullptr) {
delete m_calculation;
m_calculation = nullptr;
void CalculationController::setCalculationAccordingToIndex(int index, bool forceReinitialisation) {
if ((int)m_calculation->type() == index && !forceReinitialisation) {
return;
}
m_calculation->~Calculation();
switch (index) {
case 0:
m_calculation = new LeftIntegralCalculation();
new(m_calculation) LeftIntegralCalculation();
break;
case 1:
m_calculation = new FiniteIntegralCalculation();
new(m_calculation) FiniteIntegralCalculation();
break;
case 2:
m_calculation = new RightIntegralCalculation();
new(m_calculation) RightIntegralCalculation();
break;
default:
return;
}
m_calculation->setLaw(m_law);
m_contentView.setCalculation(m_calculation, index);
}
bool CalculationController::handleEvent(Ion::Events::Event event) {

View File

@@ -12,16 +12,11 @@ namespace Probability {
class CalculationController : public ViewController, public Shared::TextFieldDelegate {
public:
CalculationController(Responder * parentResponder, Law * law);
~CalculationController();
CalculationController(const CalculationController& other) = delete;
CalculationController(CalculationController&& other) = delete;
CalculationController& operator=(const CalculationController& other) = delete;
CalculationController& operator=(CalculationController&& other) = delete;
CalculationController(Responder * parentResponder, Law * law, Calculation * calculation);
View * view() override;
const char * title() override;
void reload();
void setCalculationAccordingToIndex(int index);
void setCalculationAccordingToIndex(int index, bool forceReinitialisation = false);
bool handleEvent(Ion::Events::Event event) override;
void didBecomeFirstResponder() override;
void selectSubview(int subviewIndex);

View File

@@ -58,15 +58,10 @@ ImageTableView::ImageTableView(Responder * parentResponder, Calculation * calcul
Responder(parentResponder),
m_selectableTableView(this, this, 0, 0, 0, 0, 0, 0, this, nullptr, false, false),
m_isSelected(false),
m_selectedIcon(0),
m_calculation(calculation),
m_calculationController(calculationController)
{
}
void ImageTableView::setCalculation(Calculation * calculation, int index) {
m_calculation = calculation;
m_selectedIcon = index;
assert(m_calculation != nullptr);
}
void ImageTableView::didBecomeFirstResponder() {
@@ -104,7 +99,7 @@ void ImageTableView::select(bool select) {
willDisplayCellForIndex(m_selectableTableView.cellAtLocation(0,0), 0);
} else {
m_isSelected = select;
m_selectableTableView.selectCellAtLocation(0, m_selectedIcon);
m_selectableTableView.selectCellAtLocation(0, (int)m_calculation->type());
}
}
@@ -138,7 +133,7 @@ void ImageTableView::willDisplayCellForIndex(HighlightCell * cell, int index) {
const Image * images[3] = {ImageStore::Calcul1Icon, ImageStore::Calcul2Icon, ImageStore::Calcul3Icon};
const Image * focusedImages[3] = {ImageStore::FocusedCalcul1Icon, ImageStore::FocusedCalcul2Icon, ImageStore::FocusedCalcul3Icon};
if (!m_isSelected) {
myCell->setImage(images[m_selectedIcon], focusedImages[m_selectedIcon]);
myCell->setImage(images[(int)m_calculation->type()], focusedImages[(int)m_calculation->type()]);
} else {
myCell->setImage(images[index], focusedImages[index]);
}

View File

@@ -51,13 +51,13 @@ static I18n::Message sMessages[] = {
I18n::Message::Poisson
};
LawController::LawController(Responder * parentResponder, Law * law) :
LawController::LawController(Responder * parentResponder, Law * law, Calculation * calculation) :
ViewController(parentResponder),
m_selectableTableView(this, this, 0, 1, Metric::CommonTopMargin-ContentView::k_titleMargin, Metric::CommonRightMargin,
Metric::CommonBottomMargin, Metric::CommonLeftMargin, this),
m_contentView(&m_selectableTableView),
m_law(law),
m_parametersController(nullptr, law)
m_parametersController(nullptr, law, calculation)
{
m_messages = sMessages;
assert(m_law != nullptr);
@@ -139,6 +139,7 @@ void Probability::LawController::setLawAccordingToIndex(int index) {
default:
return;
}
m_parametersController.reinitCalculation();
}
}

View File

@@ -11,7 +11,7 @@ namespace Probability {
class LawController : public ViewController, public SimpleListViewDataSource, public SelectableTableViewDataSource {
public:
LawController(Responder * parentResponder, Law * m_law);
LawController(Responder * parentResponder, Law * m_law, Calculation * calculation);
View * view() override;
bool handleEvent(Ion::Events::Event event) override;
void didBecomeFirstResponder() override;

View File

@@ -6,17 +6,14 @@ using namespace Shared;
namespace Probability {
LawCurveView::LawCurveView(Law * law) :
LawCurveView::LawCurveView(Law * law, Calculation * calculation) :
CurveView(law, nullptr, nullptr, nullptr),
m_labels{},
m_law(law),
m_calculation(nullptr)
m_calculation(calculation)
{
assert(law != nullptr);
}
void LawCurveView::setCalculation(Calculation * calculation) {
m_calculation = calculation;
assert(calculation != nullptr);
}
void LawCurveView::drawRect(KDContext * ctx, KDRect rect) const {

View File

@@ -12,8 +12,7 @@ namespace Probability {
class LawCurveView : public Shared::CurveView {
public:
LawCurveView(Law * law);
void setCalculation(Calculation * calculation);
LawCurveView(Law * law, Calculation * calculation);
void drawRect(KDContext * ctx, KDRect rect) const override;
protected:
char * label(Axis axis, int index) const override;

View File

@@ -67,12 +67,12 @@ void ParametersController::ContentView::layoutSubviews() {
/* Parameters Controller */
ParametersController::ParametersController(Responder * parentResponder, Law * law) :
ParametersController::ParametersController(Responder * parentResponder, Law * law, Calculation * calculation) :
FloatParameterController(parentResponder),
m_selectableTableView(nullptr),
m_menuListCell{},
m_law(law),
m_calculationController(nullptr, law)
m_calculationController(nullptr, law, calculation)
{
assert(m_law != nullptr);
}
@@ -81,6 +81,10 @@ const char * ParametersController::title() {
return I18n::translate(m_law->title());
}
void ParametersController::reinitCalculation() {
m_calculationController.setCalculationAccordingToIndex(0, true);
}
void ParametersController::viewWillAppear() {
FloatParameterController::viewWillAppear();
for (int i = 0; i < m_law->numberOfParameter(); i++) {
@@ -125,6 +129,7 @@ bool ParametersController::setParameterAtIndex(int parameterIndex, float f) {
return false;
}
m_law->setParameterAtIndex(f, parameterIndex);
m_calculationController.setCalculationAccordingToIndex(0, true);
return true;
}
@@ -137,7 +142,6 @@ bool ParametersController::textFieldDidFinishEditing(TextField * textField, cons
}
void ParametersController::buttonAction() {
m_calculationController.setCalculationAccordingToIndex(0);
m_calculationController.selectSubview(1);
m_calculationController.reload();
StackViewController * stack = stackController();

View File

@@ -10,8 +10,9 @@ namespace Probability {
class ParametersController : public Shared::FloatParameterController {
public:
ParametersController(Responder * parentResponder, Law * m_law);
ParametersController(Responder * parentResponder, Law * m_law, Calculation * calculation);
const char * title() override;
void reinitCalculation();
void viewWillAppear() override;
int numberOfRows() override;
void willDisplayCellForIndex(HighlightCell * cell, int index) override;