Files
Upsilon/apps/shared/editable_cell_table_view_controller.cpp
Émilie Feral 5c7d763291 [apps/shared] Make tab table controller dynamic view controller
Change-Id: Id3a383e72173779eb295dc998f780041f62f8bf0
2017-04-21 10:08:09 +02:00

117 lines
4.9 KiB
C++

#include "editable_cell_table_view_controller.h"
#include "../apps_container.h"
#include "../constant.h"
#include "text_field_delegate_app.h"
#include <assert.h>
using namespace Poincare;
namespace Shared {
EditableCellTableViewController::EditableCellTableViewController(Responder * parentResponder, KDCoordinate topMargin,
KDCoordinate rightMargin, KDCoordinate bottomMargin, KDCoordinate leftMargin) :
TabTableController(parentResponder, this, topMargin, rightMargin, bottomMargin, leftMargin, this, true)
{
}
bool EditableCellTableViewController::textFieldDidFinishEditing(TextField * textField, const char * text) {
AppsContainer * appsContainer = ((TextFieldDelegateApp *)app())->container();
Context * globalContext = appsContainer->globalContext();
float floatBody = Expression::parse(text)->approximate(*globalContext);
if (isnan(floatBody) || isinf(floatBody)) {
app()->displayWarning(I18n::Message::UndefinedValue);
return false;
}
if (!setDataAtLocation(floatBody, selectableTableView()->selectedColumn(), selectableTableView()->selectedRow())) {
app()->displayWarning(I18n::Message::ForbiddenValue);
return false;
}
selectableTableView()->reloadData();
selectableTableView()->selectCellAtLocation(selectableTableView()->selectedColumn(), selectableTableView()->selectedRow()+1);
return true;
}
void EditableCellTableViewController::tableViewDidChangeSelection(SelectableTableView * t, int previousSelectedCellX, int previousSelectedCellY) {
if (previousSelectedCellX == t->selectedColumn() && previousSelectedCellY == t->selectedRow()) {
return;
}
if (cellAtLocationIsEditable(previousSelectedCellX, previousSelectedCellY)) {
EvenOddEditableTextCell * myCell = (EvenOddEditableTextCell *)t->cellAtLocation(previousSelectedCellX, previousSelectedCellY);
myCell->setEditing(false);
if (app()->firstResponder() == myCell->editableTextCell()->textField()) {
app()->setFirstResponder(t);
}
}
if (cellAtLocationIsEditable(t->selectedColumn(), t->selectedRow())) {
EvenOddEditableTextCell * myCell = (EvenOddEditableTextCell *)t->cellAtLocation(t->selectedColumn(), t->selectedRow());
app()->setFirstResponder(myCell);
}
}
int EditableCellTableViewController::numberOfRows() {
int numberOfModelElements = numberOfElements();
if (numberOfModelElements >= maxNumberOfElements()) {
return 1 + numberOfModelElements;
}
return 2 + numberOfModelElements;
}
KDCoordinate EditableCellTableViewController::rowHeight(int j) {
return k_cellHeight;
}
void EditableCellTableViewController::willDisplayCellAtLocationWithDisplayMode(HighlightCell * cell, int i, int j, Expression::FloatDisplayMode floatDisplayMode) {
EvenOddCell * myCell = (EvenOddCell *)cell;
myCell->setEven(j%2 == 0);
// The cell is editable
if (cellAtLocationIsEditable(i, j)) {
EvenOddEditableTextCell * myEditableValueCell = (EvenOddEditableTextCell *)cell;
char buffer[Complex::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)];
// Special case 1: last row
if (j == numberOfRows() - 1) {
/* Display an empty line only if there is enough space for a new element in
* data */
if (numberOfElements() < maxNumberOfElements() && !myEditableValueCell->isEditing()) {
buffer[0] = 0;
myEditableValueCell->setText(buffer);
return;
}
}
if (!myEditableValueCell->isEditing()) {
Complex::convertFloatToText(dataAtLocation(i, j), buffer, Complex::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits, floatDisplayMode);
myEditableValueCell->setText(buffer);
}
return;
}
}
void EditableCellTableViewController::didBecomeFirstResponder() {
if (selectableTableView()->selectedRow() >= 0) {
int selectedRow = selectableTableView()->selectedRow();
selectedRow = selectedRow >= numberOfRows() ? numberOfRows()-1 : selectedRow;
int selectedColumn = selectableTableView()->selectedColumn();
selectedColumn = selectedColumn >= numberOfColumns() ? numberOfColumns() - 1 : selectedColumn;
selectableTableView()->selectCellAtLocation(selectedColumn, selectedRow);
TabTableController::didBecomeFirstResponder();
}
}
void EditableCellTableViewController::viewWillAppear() {
TabTableController::viewWillAppear();
if (selectableTableView()->selectedRow() == -1) {
selectableTableView()->selectCellAtLocation(0, 1);
} else {
int selectedRow = selectableTableView()->selectedRow();
selectedRow = selectedRow >= numberOfRows() ? numberOfRows()-1 : selectedRow;
int selectedColumn = selectableTableView()->selectedColumn();
selectedColumn = selectedColumn >= numberOfColumns() ? numberOfColumns() - 1 : selectedColumn;
selectableTableView()->selectCellAtLocation(selectedColumn, selectedRow);
}
}
TextFieldDelegateApp * EditableCellTableViewController::textFieldDelegateApp() {
return (TextFieldDelegateApp *)app();
}
}