mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-20 06:10:31 +01:00
[apps] Redesign Probability Calculation page
This commit is contained in:
committed by
EmilieNumworks
parent
e8793c429c
commit
fd06d428e5
@@ -6,6 +6,14 @@
|
||||
#include "calculation/left_integral_calculation.h"
|
||||
#include "calculation/right_integral_calculation.h"
|
||||
#include "calculation/finite_integral_calculation.h"
|
||||
#include "images/calcul1_icon.h"
|
||||
#include "images/calcul2_icon.h"
|
||||
#include "images/calcul3_icon.h"
|
||||
#include "images/calcul4_icon.h"
|
||||
#include "images/focused_calcul1_icon.h"
|
||||
#include "images/focused_calcul2_icon.h"
|
||||
#include "images/focused_calcul3_icon.h"
|
||||
#include "images/focused_calcul4_icon.h"
|
||||
#include <assert.h>
|
||||
#include <cmath>
|
||||
|
||||
@@ -14,9 +22,9 @@ using namespace Shared;
|
||||
|
||||
namespace Probability {
|
||||
|
||||
CalculationController::ContentView::ContentView(Responder * parentResponder, CalculationController * calculationController, Calculation * calculation, Law * law) :
|
||||
CalculationController::ContentView::ContentView(SelectableTableView * selectableTableView, Law * law, Calculation * calculation) :
|
||||
m_titleView(KDText::FontSize::Small, I18n::Message::ComputeProbability, 0.5f, 0.5f, Palette::GreyDark, Palette::WallScreen),
|
||||
m_calculationView(parentResponder, calculationController, calculation, law),
|
||||
m_selectableTableView(selectableTableView),
|
||||
m_lawCurveView(law, calculation)
|
||||
{
|
||||
}
|
||||
@@ -33,25 +41,45 @@ View * CalculationController::ContentView::subviewAtIndex(int index) {
|
||||
if (index == 1) {
|
||||
return &m_lawCurveView;
|
||||
}
|
||||
return &m_calculationView;
|
||||
return m_selectableTableView;
|
||||
}
|
||||
|
||||
void CalculationController::ContentView::layoutSubviews() {
|
||||
KDCoordinate titleHeight = KDText::charSize(KDText::FontSize::Small).height()+k_titleHeightMargin;
|
||||
m_titleView.setFrame(KDRect(0, 0, bounds().width(), titleHeight));
|
||||
KDCoordinate calculationHeight = m_calculationView.minimalSizeForOptimalDisplay().height();
|
||||
m_calculationView.setFrame(KDRect(0, titleHeight, bounds().width(), bounds().height()-titleHeight));
|
||||
KDCoordinate calculationHeight = ResponderImageCell::k_oneCellHeight;
|
||||
m_selectableTableView->setFrame(KDRect(0, titleHeight, bounds().width(), calculationHeight));
|
||||
m_lawCurveView.setFrame(KDRect(0, titleHeight+calculationHeight, bounds().width(), bounds().height() - calculationHeight - titleHeight));
|
||||
}
|
||||
|
||||
CalculationController::CalculationController(Responder * parentResponder, Law * law, Calculation * calculation) :
|
||||
ViewController(parentResponder),
|
||||
m_contentView(this, this, calculation, law),
|
||||
m_contentView(&m_selectableTableView, law, calculation),
|
||||
m_selectableTableView(this, this, 0, 0, 0, 0, 0, 0, this, nullptr, false, true, KDColorWhite),
|
||||
m_imageCell(&m_selectableTableView, law, calculation, this),
|
||||
m_draftTextBuffer{},
|
||||
m_calculation(calculation),
|
||||
m_law(law)
|
||||
{
|
||||
assert(law != nullptr);
|
||||
assert(calculation != nullptr);
|
||||
for (int i = 0; i < k_numberOfCalculationCells; i++) {
|
||||
m_calculationCells[i].editableTextCell()->setParentResponder(&m_selectableTableView);
|
||||
m_calculationCells[i].editableTextCell()->textField()->setDelegate(this);
|
||||
m_calculationCells[i].editableTextCell()->textField()->setDraftTextBuffer(m_draftTextBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
void CalculationController::didEnterResponderChain(Responder * previousResponder) {
|
||||
App::Snapshot * snapshot = (App::Snapshot *)app()->snapshot();
|
||||
snapshot->setActivePage(App::Snapshot::Page::Calculations);
|
||||
updateTitle();
|
||||
reloadLawCurveView();
|
||||
m_selectableTableView.reloadData();
|
||||
}
|
||||
|
||||
void CalculationController::didBecomeFirstResponder() {
|
||||
app()->setFirstResponder(&m_selectableTableView);
|
||||
}
|
||||
|
||||
View * CalculationController::view() {
|
||||
@@ -62,12 +90,157 @@ const char * CalculationController::title() {
|
||||
return m_titleBuffer;
|
||||
}
|
||||
|
||||
void CalculationController::viewWillAppear() {
|
||||
ViewController::viewWillAppear();
|
||||
selectCellAtLocation(1, 0);
|
||||
}
|
||||
|
||||
void CalculationController::viewDidDisappear() {
|
||||
m_selectableTableView.deselectTable();
|
||||
ViewController::viewDidDisappear();
|
||||
}
|
||||
|
||||
int CalculationController::numberOfRows() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int CalculationController::numberOfColumns() {
|
||||
return m_calculation->numberOfParameters()+1;
|
||||
}
|
||||
|
||||
KDCoordinate CalculationController::columnWidth(int i) {
|
||||
if (i == 0) {
|
||||
return m_imageCell.minimalSizeForOptimalDisplay().width();
|
||||
}
|
||||
return m_calculationCells[i-1].minimalSizeForOptimalDisplay().width();
|
||||
}
|
||||
|
||||
KDCoordinate CalculationController::rowHeight(int j) {
|
||||
return ResponderImageCell::k_oneCellHeight;
|
||||
}
|
||||
|
||||
KDCoordinate CalculationController::cumulatedWidthFromIndex(int j) {
|
||||
int result = 0;
|
||||
for (int k = 0; k < j; k++) {
|
||||
result += columnWidth(k);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int CalculationController::indexFromCumulatedWidth(KDCoordinate offsetX) {
|
||||
int result = 0;
|
||||
int i = 0;
|
||||
while (result < offsetX && i < numberOfColumns()) {
|
||||
result += columnWidth(i++);
|
||||
}
|
||||
return (result < offsetX || offsetX == 0) ? i : i - 1;
|
||||
}
|
||||
|
||||
KDCoordinate CalculationController::cumulatedHeightFromIndex(int j) {
|
||||
return rowHeight(0) * j;
|
||||
}
|
||||
|
||||
int CalculationController::indexFromCumulatedHeight(KDCoordinate offsetY) {
|
||||
KDCoordinate height = rowHeight(0);
|
||||
if (height == 0) {
|
||||
return 0;
|
||||
}
|
||||
return (offsetY - 1) / height;
|
||||
}
|
||||
|
||||
HighlightCell * CalculationController::reusableCell(int index, int type) {
|
||||
if (type == 0) {
|
||||
assert(index == 0);
|
||||
return &m_imageCell;
|
||||
}
|
||||
assert(index >= 0 && index < k_numberOfCalculationCells);
|
||||
return &m_calculationCells[index];
|
||||
}
|
||||
|
||||
int CalculationController::reusableCellCount(int type) {
|
||||
if (type == 0) {
|
||||
return 1;
|
||||
}
|
||||
return k_numberOfCalculationCells;
|
||||
}
|
||||
|
||||
int CalculationController::typeAtLocation(int i, int j) {
|
||||
if (i == 0 && j == 0) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void CalculationController::willDisplayCellAtLocation(HighlightCell * cell, int i, int j) {
|
||||
if (i == 0) {
|
||||
ResponderImageCell * myCell = static_cast<ResponderImageCell *>(cell);
|
||||
const Image * images[CalculationTypeController::k_numberOfImages] = {ImageStore::Calcul1Icon, ImageStore::Calcul2Icon, ImageStore::Calcul3Icon, ImageStore::Calcul4Icon};
|
||||
const Image * focusedImages[CalculationTypeController::k_numberOfImages] = {ImageStore::FocusedCalcul1Icon, ImageStore::FocusedCalcul2Icon, ImageStore::FocusedCalcul3Icon, ImageStore::FocusedCalcul4Icon};
|
||||
myCell->setImage(images[(int)m_calculation->type()], focusedImages[(int)m_calculation->type()]);
|
||||
} else {
|
||||
CalculationCell * myCell = static_cast<CalculationCell *>(cell);
|
||||
myCell->messageTextView()->setMessage(m_calculation->legendForParameterAtIndex(i-1));
|
||||
bool calculationCellIsResponder = true;
|
||||
if ((m_law->type() != Law::Type::Normal && i == 3) || (m_calculation->type() == Calculation::Type::Discrete && i == 2)) {
|
||||
calculationCellIsResponder = false;
|
||||
}
|
||||
myCell->setResponder(calculationCellIsResponder);
|
||||
TextField * field = static_cast<CalculationCell *>(cell)->editableTextCell()->textField();
|
||||
if (field->isEditing()) {
|
||||
return;
|
||||
}
|
||||
char buffer[PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)];
|
||||
Complex<double>::convertFloatToText(m_calculation->parameterAtIndex(i-1), buffer, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits, Expression::FloatDisplayMode::Decimal);
|
||||
field->setText(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
bool CalculationController::textFieldDidHandleEvent(::TextField * textField, Ion::Events::Event event, bool returnValue, bool textHasChanged) {
|
||||
if (returnValue && textHasChanged) {
|
||||
m_selectableTableView.reloadData(); //TODO: optimize with reloadCell at index?
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
bool CalculationController::textFieldShouldFinishEditing(TextField * textField, Ion::Events::Event event) {
|
||||
return TextFieldDelegate::textFieldShouldFinishEditing(textField, event)
|
||||
|| (event == Ion::Events::Right && textField->cursorLocation() == textField->draftTextLength() && selectedColumn() < m_calculation->numberOfParameters())
|
||||
|| (event == Ion::Events::Left && textField->cursorLocation() == 0);
|
||||
}
|
||||
|
||||
bool CalculationController::textFieldDidFinishEditing(TextField * textField, const char * text, Ion::Events::Event event) {
|
||||
App * probaApp = (App *)app();
|
||||
Context * globalContext = probaApp->container()->globalContext();
|
||||
double floatBody = Expression::approximateToScalar<double>(text, *globalContext);
|
||||
if (std::isnan(floatBody) || std::isinf(floatBody)) {
|
||||
app()->displayWarning(I18n::Message::UndefinedValue);
|
||||
return false;
|
||||
}
|
||||
if (m_calculation->type() != Calculation::Type::FiniteIntegral && selectedColumn() == 2) {
|
||||
if (floatBody < 0.0) {
|
||||
floatBody = 0.0;
|
||||
}
|
||||
if (floatBody > 1.0) {
|
||||
floatBody = 1.0;
|
||||
}
|
||||
}
|
||||
if (!m_law->isContinuous() && (selectedColumn() == 1 || m_calculation->type() == Calculation::Type::FiniteIntegral)) {
|
||||
floatBody = std::round(floatBody);
|
||||
}
|
||||
m_calculation->setParameterAtIndex(floatBody, selectedColumn()-1);
|
||||
if (event == Ion::Events::Right || event == Ion::Events::Left) {
|
||||
m_selectableTableView.handleEvent(event);
|
||||
}
|
||||
reload();
|
||||
return true;
|
||||
}
|
||||
|
||||
void CalculationController::reloadLawCurveView() {
|
||||
m_contentView.lawCurveView()->reload();
|
||||
}
|
||||
|
||||
void CalculationController::reload() {
|
||||
m_contentView.calculationView()->reload();
|
||||
m_selectableTableView.reloadData();
|
||||
reloadLawCurveView();
|
||||
}
|
||||
|
||||
@@ -93,22 +266,10 @@ void CalculationController::setCalculationAccordingToIndex(int index, bool force
|
||||
return;
|
||||
}
|
||||
m_calculation->setLaw(m_law);
|
||||
reload();
|
||||
}
|
||||
|
||||
void CalculationController::viewWillAppear() {
|
||||
reloadLawCurveView();
|
||||
m_contentView.calculationView()->selectSubview(1);
|
||||
}
|
||||
|
||||
void CalculationController::didEnterResponderChain(Responder * previousResponder) {
|
||||
App::Snapshot * snapshot = (App::Snapshot *)app()->snapshot();
|
||||
snapshot->setActivePage(App::Snapshot::Page::Calculations);
|
||||
updateTitle();
|
||||
}
|
||||
|
||||
void CalculationController::didBecomeFirstResponder() {
|
||||
app()->setFirstResponder(m_contentView.calculationView());
|
||||
TextFieldDelegateApp * CalculationController::textFieldDelegateApp() {
|
||||
return (App *)app();
|
||||
}
|
||||
|
||||
void CalculationController::updateTitle() {
|
||||
|
||||
Reference in New Issue
Block a user