Add a Probability app

Change-Id: I1c12b1de3cc4a7cf955f6d8389a4fdf69c4bff1c
This commit is contained in:
Romain Goyet
2016-08-19 16:20:05 +02:00
parent 2c8a7788b7
commit 2905975b43
8 changed files with 215 additions and 0 deletions

View File

@@ -1,4 +1,5 @@
include apps/graph/Makefile
include apps/probability/Makefile
#include apps/picview/Makefile
app_objs += $(addprefix apps/,\

View File

@@ -0,0 +1,5 @@
app_objs += $(addprefix apps/probability/,\
app.o\
law/law_cell.o\
law/law_controller.o\
)

13
apps/probability/app.cpp Normal file
View File

@@ -0,0 +1,13 @@
#include "app.h"
Probability::App::App() :
::App(),
m_lawController(LawController(nullptr)),
m_stackViewController(this)
{
m_stackViewController.push(&m_lawController);
}
ViewController * Probability::App::rootViewController() {
return &m_stackViewController;
}

23
apps/probability/app.h Normal file
View File

@@ -0,0 +1,23 @@
#ifndef PROBABILITY_PROBABILITY_APP_H
#define PROBABILITY_PROBABILITY_APP_H
#include <escher.h>
#include "law/law_controller.h"
namespace Probability {
class App : public ::App {
public:
App();
protected:
ViewController * rootViewController() override;
private:
LawController m_lawController;
//ListController m_listController;
//GraphController m_graphController;
StackViewController m_stackViewController;
};
}
#endif

View File

@@ -0,0 +1,29 @@
#include "law_cell.h"
Probability::LawCell::LawCell() :
ChildlessView(),
Responder(nullptr),
m_focused(false),
m_even(false)
{
m_message = "NULL";
}
void Probability::LawCell::drawRect(KDContext * ctx, KDRect rect) const {
KDColor background = m_even ? KDColor(0xEEEEEE) : KDColor(0x777777);
ctx->fillRect(rect, background);
ctx->drawString(m_message, KDPointZero, m_focused);
}
void Probability::LawCell::setMessage(const char * message) {
m_message = message;
}
void Probability::LawCell::setFocused(bool focused) {
m_focused = focused;
markRectAsDirty(bounds());
}
void Probability::LawCell::setEven(bool even) {
m_even = even;
}

View File

@@ -0,0 +1,24 @@
#ifndef PROBABILITY_LAW_CELL_H
#define PROBABILITY_LAW_CELL_H
#include <escher.h>
namespace Probability {
class LawCell : public ChildlessView, public Responder {
public:
LawCell();
void setMessage(const char * message);
void setEven(bool even);
void drawRect(KDContext * ctx, KDRect rect) const override;
void setFocused(bool focused) override;
private:
const char * m_message;
bool m_focused;
bool m_even;
};
}
#endif

View File

@@ -0,0 +1,82 @@
#include "law_controller.h"
#include <assert.h>
static const char * sMessages[] = {
"Loi Normale",
"Exponentielle",
"Student",
"Khi 2",
"Binomiale",
"Poisson",
"Geometrique"
};
Probability::LawController::LawController(Responder * parentResponder) :
ViewController(parentResponder),
m_tableView(TableView(this)),
m_activeCell(0),
m_manualScrolling(0)
{
m_messages = sMessages;
}
View * Probability::LawController::view() {
return &m_tableView;
}
const char * Probability::LawController::title() const {
return "Type de Loi";
}
void Probability::LawController::setActiveCell(int index) {
if (index < 0 || index >= k_totalNumberOfModels) {
return;
}
m_activeCell = index;
m_tableView.scrollToRow(index);
LawCell * cell = (LawCell *)(m_tableView.cellAtIndex(index));
cell->setParentResponder(this);
app()->focus(cell);
}
bool Probability::LawController::handleEvent(ion_event_t event) {
switch (event) {
case DOWN_ARROW:
setActiveCell(m_activeCell+1);
return true;
case UP_ARROW:
setActiveCell(m_activeCell-1);
return true;
case ENTER:
m_manualScrolling += 10;
m_tableView.setContentOffset({0, m_manualScrolling});
return true;
default:
return false;
}
}
int Probability::LawController::numberOfCells() {
return k_totalNumberOfModels;
};
View * Probability::LawController::reusableCell(int index) {
assert(index >= 0);
assert(index < k_maxNumberOfCells);
return &m_cells[index];
}
int Probability::LawController::reusableCellCount() {
return k_maxNumberOfCells;
}
void Probability::LawController::willDisplayCellForIndex(View * cell, int index) {
LawCell * myCell = (LawCell *)cell;
myCell->setMessage(m_messages[index]);
myCell->setEven(index%2 == 0);
}
KDCoordinate Probability::LawController::cellHeight() {
return 40;
}

View File

@@ -0,0 +1,38 @@
#ifndef PROBABILITY_LAW_CONTROLLER_H
#define PROBABILITY_LAW_CONTROLLER_H
#include <escher.h>
#include "law_cell.h"
namespace Probability {
class LawController : public ViewController, public TableViewDataSource {
public:
LawController(Responder * parentResponder);
void setActiveCell(int index);
View * view() override;
const char * title() const override;
bool handleEvent(ion_event_t event) override;
int numberOfCells() override;
void willDisplayCellForIndex(View * cell, int index) override;
KDCoordinate cellHeight() override;
View * reusableCell(int index) override;
int reusableCellCount() override;
private:
constexpr static int k_totalNumberOfModels = 7;
constexpr static int k_maxNumberOfCells = 10;
// !!! CAUTION: The order here is important
// The cells should be initialized *before* the tableview!
LawCell m_cells[k_maxNumberOfCells];
TableView m_tableView;
const char ** m_messages;
int m_activeCell;
KDCoordinate m_manualScrolling;
};
}
#endif