range_parameter_controller.cpp 4.7 KB
#include "range_parameter_controller.h"
#include "text_field_delegate_app.h"
#include "../apps_container.h"
#include <assert.h>

using namespace Poincare;

namespace Shared {

RangeParameterController::RangeParameterController(Responder * parentResponder, InteractiveCurveViewRange * interactiveRange) :
  FloatParameterController(parentResponder),
  m_interactiveRange(interactiveRange),
  m_xRangeCells{},
  m_yRangeCells{},
  m_yAutoCell(nullptr)
{
}

const char * RangeParameterController::title() {
  return I18n::translate(I18n::Message::Axis);
}

int RangeParameterController::numberOfRows() {
  return k_numberOfTextCell+2;
}

int RangeParameterController::typeAtLocation(int i, int j) {
  if (j == numberOfRows()-1) {
    return 0;
  }
  if (j >= 0 && j < 2) {
    return 1;
  }
  if (j == 2) {
    return 2;
  }
  return 3;
}

void RangeParameterController::willDisplayCellForIndex(HighlightCell * cell, int index) {
  if (index == numberOfRows()-1) {
    return;
  }
  if (index == 2) {
    SwitchView * switchView = (SwitchView *)m_yAutoCell->accessoryView();
    switchView->setState(m_interactiveRange->yAuto());
    return;
  }
  MessageTableCellWithEditableText * myCell = (MessageTableCellWithEditableText *)cell;
  I18n::Message labels[k_numberOfTextCell+1] = {I18n::Message::XMin, I18n::Message::XMax, I18n::Message::Default, I18n::Message::YMin, I18n::Message::YMax};
  myCell->setMessage(labels[index]);
  KDColor yColor = m_interactiveRange->yAuto() ? Palette::GreyDark : KDColorBlack;
  KDColor colors[k_numberOfTextCell+1] = {KDColorBlack, KDColorBlack, KDColorBlack, yColor, yColor};
  myCell->setTextColor(colors[index]);
  FloatParameterController::willDisplayCellForIndex(cell, index);
}

bool RangeParameterController::textFieldDidFinishEditing(TextField * textField, const char * text, Ion::Events::Event event) {
  if (FloatParameterController::textFieldDidFinishEditing(textField, text, event)) {
    selectableTableView()->reloadData();
    return true;
  }
  return false;
}

bool RangeParameterController::handleEvent(Ion::Events::Event event) {
  if (activeCell() == 2 && (event == Ion::Events::OK || event == Ion::Events::EXE)) {
    m_interactiveRange->setYAuto(!m_interactiveRange->yAuto());
    selectableTableView()->reloadData();
    return true;
  }
  return FloatParameterController::handleEvent(event);
}

double RangeParameterController::parameterAtIndex(int parameterIndex) {
  ParameterGetterPointer getters[k_numberOfTextCell] = {&InteractiveCurveViewRange::xMin,
    &InteractiveCurveViewRange::xMax, &InteractiveCurveViewRange::yMin, &InteractiveCurveViewRange::yMax};
  int index = parameterIndex > 2 ? parameterIndex - 1 : parameterIndex;
  return (m_interactiveRange->*getters[index])();
}

bool RangeParameterController::setParameterAtIndex(int parameterIndex, double f) {
  ParameterSetterPointer setters[k_numberOfTextCell] = {&InteractiveCurveViewRange::setXMin,
    &InteractiveCurveViewRange::setXMax, &InteractiveCurveViewRange::setYMin, &InteractiveCurveViewRange::setYMax};
  int index = parameterIndex > 2 ? parameterIndex - 1 : parameterIndex;
  (m_interactiveRange->*setters[index])(f);
  return true;
}

HighlightCell * RangeParameterController::reusableParameterCell(int index, int type) {
  if (type == 2) {
    assert(index == 0);
    return m_yAutoCell;
  }
  if (type == 1) {
    assert(index >= 0);
    assert(index < k_numberOfEditableTextCell);
    return m_xRangeCells[index];
  }
  assert(index >= 0);
  assert(index < k_numberOfConvertibleTextCell);
  return m_yRangeCells[index];
}

int RangeParameterController::reusableParameterCellCount(int type) {
  if (type == 2) {
    return 1;
  }
  if (type == 1) {
    return k_numberOfEditableTextCell;
  }
  return k_numberOfConvertibleTextCell;
}

View * RangeParameterController::loadView() {
  SelectableTableView * tableView = (SelectableTableView *)FloatParameterController::loadView();
  m_yAutoCell = new MessageTableCellWithSwitch(I18n::Message::YAuto);
  for (int i = 0; i < k_numberOfEditableTextCell; i++) {
    m_xRangeCells[i] = new MessageTableCellWithEditableText(tableView, this, m_draftTextBuffer, I18n::Message::Default);
  }
  for (int i = 0; i < k_numberOfConvertibleTextCell; i++) {
    m_yRangeCells[i] = new MessageTableCellWithConvertibleEditableText(tableView, this, m_draftTextBuffer, I18n::Message::Default, m_interactiveRange);
  }
  return tableView;
}

void RangeParameterController::unloadView(View * view) {
  delete m_yAutoCell;
  m_yAutoCell = nullptr;
  for (int i = 0; i < k_numberOfEditableTextCell; i++) {
    delete m_xRangeCells[i];
    m_xRangeCells[i] = nullptr;
  }
  for (int i = 0; i < k_numberOfConvertibleTextCell; i++) {
    delete m_yRangeCells[i];
    m_yRangeCells[i] = nullptr;
  }
  FloatParameterController::unloadView(view);
}

}