Blame view

build2/epsilon-master/apps/calculation/history_controller.cpp 6.06 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  #include "history_controller.h"
  #include "app.h"
  #include "../apps_container.h"
  #include <assert.h>
  
  using namespace Shared;
  
  namespace Calculation {
  
  HistoryController::HistoryController(Responder * parentResponder, CalculationStore * calculationStore) :
    DynamicViewController(parentResponder),
    m_calculationHistory{},
    m_calculationStore(calculationStore)
  {
  }
  
  void HistoryController::reload() {
    selectableTableView()->reloadData();
  }
  
  void HistoryController::didBecomeFirstResponder() {
    selectCellAtLocation(0, numberOfRows()-1);
    app()->setFirstResponder(selectableTableView());
  }
  
  void HistoryController::willExitResponderChain(Responder * nextFirstResponder) {
    if (nextFirstResponder == parentResponder()) {
      selectableTableView()->deselectTable();
    }
  }
  
  bool HistoryController::handleEvent(Ion::Events::Event event) {
    if (event == Ion::Events::Down) {
      selectableTableView()->deselectTable();
      app()->setFirstResponder(parentResponder());
      return true;
    }
    if (event == Ion::Events::Up) {
      return true;
    }
    if (event == Ion::Events::OK || event == Ion::Events::EXE) {
      int focusRow = selectedRow();
      HistoryViewCell * selectedCell = (HistoryViewCell *)selectableTableView()->selectedCell();
      HistoryViewCell::SubviewType subviewType = selectedCell->selectedSubviewType();
      EditExpressionController * editController = (EditExpressionController *)parentResponder();
      selectableTableView()->deselectTable();
      app()->setFirstResponder(editController);
      Calculation * calculation = m_calculationStore->calculationAtIndex(focusRow);
      if (subviewType == HistoryViewCell::SubviewType::Input) {
        editController->insertTextBody(calculation->inputText());
      } else {
        ScrollableExactApproximateExpressionsView::SubviewType outputSubviewType = selectedCell->outputView()->selectedSubviewType();
        if (outputSubviewType == ScrollableExactApproximateExpressionsView::SubviewType::ExactOutput) {
          editController->insertTextBody(calculation->exactOutputText());
        } else {
          editController->insertTextBody(calculation->approximateOutputText());
        }
      }
      return true;
    }
    if (event == Ion::Events::Backspace) {
      int focusRow = selectedRow();
      HistoryViewCell * selectedCell = (HistoryViewCell *)selectableTableView()->selectedCell();
      HistoryViewCell::SubviewType subviewType = selectedCell->selectedSubviewType();
      selectableTableView()->deselectTable();
      EditExpressionController * editController = (EditExpressionController *)parentResponder();
      m_calculationStore->deleteCalculationAtIndex(focusRow);
      reload();
      if (numberOfRows()== 0) {
        app()->setFirstResponder(editController);
        return true;
      }
      if (focusRow > 0) {
        selectableTableView()->selectCellAtLocation(0, focusRow-1);
      } else {
        selectableTableView()->selectCellAtLocation(0, 0);
      }
      if (subviewType == HistoryViewCell::SubviewType::Input) {
        tableViewDidChangeSelection(selectableTableView(), 0, selectedRow());
      } else {
        tableViewDidChangeSelection(selectableTableView(), 0, -1);
      }
      selectableTableView()->scrollToCell(0, selectedRow());
      return true;
    }
    if (event == Ion::Events::Clear) {
      selectableTableView()->deselectTable();
      m_calculationStore->deleteAll();
      reload();
      app()->setFirstResponder(parentResponder());
      return true;
    }
    if (event == Ion::Events::Back) {
      EditExpressionController * editController = (EditExpressionController *)parentResponder();
      selectableTableView()->deselectTable();
      app()->setFirstResponder(editController);
      return true;
    }
    return false;
  }
  
  void HistoryController::tableViewDidChangeSelection(SelectableTableView * t, int previousSelectedCellX, int previousSelectedCellY) {
    HistoryViewCell * selectedCell = (HistoryViewCell *)(t->selectedCell());
    if (selectedCell == nullptr) {
      return;
    }
    if (previousSelectedCellY == -1) {
      selectedCell->setSelectedSubviewType(HistoryViewCell::SubviewType::Output);
    } else if (selectedRow() < previousSelectedCellY) {
      selectedCell->setSelectedSubviewType(HistoryViewCell::SubviewType::Output);
    } else if (selectedRow() > previousSelectedCellY) {
      selectedCell->setSelectedSubviewType(HistoryViewCell::SubviewType::Input);
    }
    app()->setFirstResponder(selectedCell);
    selectedCell->reloadCell();
  }
  
  int HistoryController::numberOfRows() {
    return m_calculationStore->numberOfCalculations();
  };
  
  HighlightCell * HistoryController::reusableCell(int index, int type) {
    assert(type == 0);
    assert(index >= 0);
    assert(index < k_maxNumberOfDisplayedRows);
    return m_calculationHistory[index];
  }
  
  int HistoryController::reusableCellCount(int type) {
    assert(type == 0);
    return k_maxNumberOfDisplayedRows;
  }
  
  void HistoryController::willDisplayCellForIndex(HighlightCell * cell, int index) {
    HistoryViewCell * myCell = (HistoryViewCell *)cell;
    myCell->setCalculation(m_calculationStore->calculationAtIndex(index));
    myCell->setEven(index%2 == 0);
    myCell->reloadCell();
  }
  
  KDCoordinate HistoryController::rowHeight(int j) {
    if (j >= m_calculationStore->numberOfCalculations()) {
      return 0;
    }
    Calculation * calculation = m_calculationStore->calculationAtIndex(j);
    App * calculationApp = (App *)app();
    return calculation->height(calculationApp->localContext()) + 3*HistoryViewCell::k_digitVerticalMargin;
  }
  
  int HistoryController::typeAtLocation(int i, int j) {
    return 0;
  }
  
  void HistoryController::scrollToCell(int i, int j) {
    selectableTableView()->scrollToCell(i, j);
  }
  
  CalculationSelectableTableView * HistoryController::selectableTableView() {
    return (CalculationSelectableTableView *)view();
  }
  
  View * HistoryController::loadView() {
    CalculationSelectableTableView * tableView = new CalculationSelectableTableView(this, this, this, this);
  for (int i = 0; i < k_maxNumberOfDisplayedRows; i++) {
      m_calculationHistory[i] = new HistoryViewCell(tableView);
    }
    return tableView;
  }
  
  void HistoryController::unloadView(View * view) {
    for (int i = 0; i < k_maxNumberOfDisplayedRows; i++) {
      delete m_calculationHistory[i];
      m_calculationHistory[i] = nullptr;
    }
    delete view;
  }
  
  }