Blame view

build3/apps/graph/list/list_controller.cpp 4.31 KB
6663b6c9   adorian   projet complet av...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  #include "list_controller.h"
  #include "../app.h"
  #include "../../i18n.h"
  #include <assert.h>
  #include <escher/metric.h>
  
  using namespace Shared;
  
  namespace Graph {
  
  ListController::ListController(Responder * parentResponder, CartesianFunctionStore * functionStore, ButtonRowController * header, ButtonRowController * footer) :
    Shared::ListController(parentResponder, functionStore, header, footer, I18n::Message::AddFunction),
    m_functionTitleCells{},
    m_expressionCells{},
    m_parameterController(this, functionStore, I18n::Message::FunctionColor, I18n::Message::DeleteFunction)
  {
  }
  
  const char * ListController::title() {
    return I18n::translate(I18n::Message::FunctionTab);
  }
  
  int ListController::numberOfRows() {
    if (m_functionStore->numberOfFunctions() == m_functionStore->maxNumberOfFunctions()) {
      return m_functionStore->numberOfFunctions();
    }
    return 1 + m_functionStore->numberOfFunctions();
  };
  
  KDCoordinate ListController::rowHeight(int j) {
    if (m_functionStore->numberOfFunctions() < m_functionStore->maxNumberOfFunctions() && j == numberOfRows() - 1) {
      return Metric::StoreRowHeight;
    }
    Function * function = m_functionStore->functionAtIndex(j);
    if (function->layout() == nullptr) {
      return Metric::StoreRowHeight;
    }
    KDCoordinate functionSize = function->layout()->size().height();
    return functionSize + Metric::StoreRowHeight - KDText::charSize().height();
  }
  
  void ListController::editExpression(Function * function, Ion::Events::Event event) {
    char * initialText = nullptr;
    char initialTextContent[TextField::maxBufferSize()];
    if (event == Ion::Events::OK || event == Ion::Events::EXE) {
      strlcpy(initialTextContent, function->text(), sizeof(initialTextContent));
      initialText = initialTextContent;
    }
    App * myApp = (App *)app();
    InputViewController * inputController = myApp->inputViewController();
    inputController->edit(this, event, function, initialText,
      [](void * context, void * sender){
      Shared::Function * myFunction = (Shared::Function *)context;
      InputViewController * myInputViewController = (InputViewController *)sender;
      const char * textBody = myInputViewController->textBody();
      myFunction->setContent(textBody);
      },
      [](void * context, void * sender){
      });
  }
  
  ListParameterController * ListController::parameterController() {
    return &m_parameterController;
  }
  
  int ListController::maxNumberOfRows() {
    return k_maxNumberOfRows;
  }
  
  HighlightCell * ListController::titleCells(int index) {
    assert(index >= 0 && index < k_maxNumberOfRows);
    return m_functionTitleCells[index];
  }
  
  HighlightCell * ListController::expressionCells(int index) {
    assert(index >= 0 && index < k_maxNumberOfRows);
    return m_expressionCells[index];
  }
  
  
  void ListController::willDisplayTitleCellAtIndex(HighlightCell * cell, int j) {
    FunctionTitleCell * myFunctionCell = (FunctionTitleCell *)cell;
    CartesianFunction * function = ((CartesianFunctionStore *)m_functionStore)->functionAtIndex(j);
    char bufferName[5] = {*function->name(),'(', m_functionStore->symbol(),')', 0};
    myFunctionCell->setText(bufferName);
    KDColor functionNameColor = function->isActive() ? function->color() : Palette::GreyDark;
    myFunctionCell->setColor(functionNameColor);
  }
  
  void ListController::willDisplayExpressionCellAtIndex(HighlightCell * cell, int j) {
    FunctionExpressionCell * myCell = (FunctionExpressionCell *)cell;
    Function * f = m_functionStore->functionAtIndex(j);
    myCell->setExpression(f->layout());
    bool active = f->isActive();
    KDColor textColor = active ? KDColorBlack : Palette::GreyDark;
    myCell->setTextColor(textColor);
  }
  
  bool ListController::removeFunctionRow(Function * function) {
    if (m_functionStore->numberOfFunctions() > 1) {
      m_functionStore->removeFunction(function);
      return true;
    }
    return false;
  }
  
  View * ListController::loadView() {
    for (int i = 0; i < k_maxNumberOfRows; i++) {
      m_functionTitleCells[i] = new FunctionTitleCell(FunctionTitleCell::Orientation::VerticalIndicator);
      m_expressionCells[i] = new FunctionExpressionCell();
    }
    return Shared::ListController::loadView();
  }
  
  void ListController::unloadView(View * view) {
    for (int i = 0; i < k_maxNumberOfRows; i++) {
      delete m_functionTitleCells[i];
      m_functionTitleCells[i] = nullptr;
      delete m_expressionCells[i];
      m_expressionCells[i] = nullptr;
    }
    Shared::ListController::unloadView(view);
  }
  
  }