Blame view

emulateur/epsilon-nofrendo/apps/calculation/history_view_cell.cpp 6.12 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
  #include "history_view_cell.h"
  #include "app.h"
  #include "../constant.h"
  #include "selectable_table_view.h"
  #include <assert.h>
  #include <string.h>
  
  namespace Calculation {
  
  HistoryViewCell::HistoryViewCell(Responder * parentResponder) :
    Responder(parentResponder),
    m_inputLayout(nullptr),
    m_exactOutputLayout(nullptr),
    m_approximateOutputLayout(nullptr),
    m_inputView(this),
    m_scrollableOutputView(this),
    m_selectedSubviewType(HistoryViewCell::SubviewType::Output)
  {
  }
  
  HistoryViewCell::~HistoryViewCell() {
    if (m_inputLayout != nullptr) {
      delete m_inputLayout;
      m_inputLayout = nullptr;
    }
    if (m_exactOutputLayout != nullptr) {
      delete m_exactOutputLayout;
      m_exactOutputLayout = nullptr;
    }
    if (m_approximateOutputLayout != nullptr) {
      delete m_approximateOutputLayout;
      m_approximateOutputLayout = nullptr;
    }
  }
  
  Shared::ScrollableExactApproximateExpressionsView * HistoryViewCell::outputView() {
    return &m_scrollableOutputView;
  
  }
  void HistoryViewCell::setEven(bool even) {
    EvenOddCell::setEven(even);
    m_inputView.setBackgroundColor(backgroundColor());
    m_scrollableOutputView.evenOddCell()->setEven(even);
  }
  
  void HistoryViewCell::setHighlighted(bool highlight) {
    m_highlighted = highlight;
    m_inputView.setBackgroundColor(backgroundColor());
    m_scrollableOutputView.evenOddCell()->setHighlighted(false);
    if (isHighlighted()) {
      if (m_selectedSubviewType == SubviewType::Input) {
        m_inputView.setBackgroundColor(Palette::Select);
      } else {
        m_scrollableOutputView.evenOddCell()->setHighlighted(true);
      }
    }
    reloadScroll();
  }
  
  Poincare::ExpressionLayout * HistoryViewCell::expressionLayout() const {
    if (m_selectedSubviewType == SubviewType::Input) {
      return m_inputLayout;
    } else {
      return m_scrollableOutputView.expressionLayout();
    }
  }
  
  void HistoryViewCell::reloadCell() {
    m_scrollableOutputView.evenOddCell()->reloadCell();
    layoutSubviews();
    reloadScroll();
  }
  
  void HistoryViewCell::reloadScroll() {
    m_inputView.reloadScroll();
    m_scrollableOutputView.reloadScroll();
  }
  
  KDColor HistoryViewCell::backgroundColor() const {
    KDColor background = m_even ? KDColorWhite : Palette::WallScreen;
    return background;
  }
  
  
  int HistoryViewCell::numberOfSubviews() const {
    return 2;
  }
  
  View * HistoryViewCell::subviewAtIndex(int index) {
    View * views[2] = {&m_inputView, &m_scrollableOutputView};
    return views[index];
  }
  
  void HistoryViewCell::layoutSubviews() {
    KDCoordinate width = bounds().width();
    KDCoordinate height = bounds().height();
    KDSize inputSize = m_inputView.minimalSizeForOptimalDisplay();
    if (inputSize.width() + Metric::HistoryHorizontalMargin > width) {
      m_inputView.setFrame(KDRect(Metric::HistoryHorizontalMargin, k_digitVerticalMargin, width - Metric::HistoryHorizontalMargin, inputSize.height()));
    } else {
      m_inputView.setFrame(KDRect(Metric::HistoryHorizontalMargin, k_digitVerticalMargin, inputSize.width(), inputSize.height()));
    }
    KDSize outputSize = m_scrollableOutputView.minimalSizeForOptimalDisplay();
    if (outputSize.width() + Metric::HistoryHorizontalMargin > width) {
      m_scrollableOutputView.setFrame(KDRect(Metric::HistoryHorizontalMargin, inputSize.height() + 2*k_digitVerticalMargin, width - Metric::HistoryHorizontalMargin, height - inputSize.height() - 3*k_digitVerticalMargin));
    } else {
      m_scrollableOutputView.setFrame(KDRect(width - outputSize.width() - Metric::HistoryHorizontalMargin, inputSize.height() + 2*k_digitVerticalMargin, outputSize.width(), height - inputSize.height() - 3*k_digitVerticalMargin));
    }
  }
  
  void HistoryViewCell::setCalculation(Calculation * calculation) {
    if (m_inputLayout) {
      delete m_inputLayout;
    }
    m_inputLayout = calculation->createInputLayout();
    m_inputView.setExpressionLayout(m_inputLayout);
    App * calculationApp = (App *)app();
    /* Both output expressions have to be updated at the same time. Otherwise,
     * when updating one layout, if the second one still points to a deleted
     * layout, calling to layoutSubviews() would fail. */
    if (m_exactOutputLayout) {
      delete m_exactOutputLayout;
      m_exactOutputLayout = nullptr;
    }
    if (!calculation->shouldOnlyDisplayApproximateOutput(calculationApp->localContext())) {
      m_exactOutputLayout = calculation->createExactOutputLayout(calculationApp->localContext());
    }
    if (m_approximateOutputLayout) {
      delete m_approximateOutputLayout;
    }
    m_approximateOutputLayout = calculation->createApproximateOutputLayout(calculationApp->localContext());
    Poincare::ExpressionLayout * outputExpressions[2] = {m_approximateOutputLayout, m_exactOutputLayout};
    m_scrollableOutputView.setExpressions(outputExpressions);
    I18n::Message equalMessage = calculation->exactAndApproximateDisplayedOutputsAreEqual(calculationApp->localContext()) == Calculation::EqualSign::Equal ? I18n::Message::Equal : I18n::Message::AlmostEqual;
    m_scrollableOutputView.setEqualMessage(equalMessage);
  }
  
  void HistoryViewCell::didBecomeFirstResponder() {
    if (m_selectedSubviewType == SubviewType::Input) {
      app()->setFirstResponder(&m_inputView);
    } else {
      app()->setFirstResponder(&m_scrollableOutputView);
    }
  }
  
  HistoryViewCell::SubviewType HistoryViewCell::selectedSubviewType() {
    return m_selectedSubviewType;
  }
  
  void HistoryViewCell::setSelectedSubviewType(HistoryViewCell::SubviewType subviewType) {
    m_selectedSubviewType = subviewType;
    setHighlighted(isHighlighted());
  }
  
  bool HistoryViewCell::handleEvent(Ion::Events::Event event) {
    if ((event == Ion::Events::Down && m_selectedSubviewType == SubviewType::Input) ||
      (event == Ion::Events::Up && m_selectedSubviewType == SubviewType::Output)) {
      SubviewType otherSubviewType = m_selectedSubviewType == SubviewType::Input ? SubviewType::Output : SubviewType::Input;
      CalculationSelectableTableView * tableView = (CalculationSelectableTableView *)parentResponder();
      tableView->scrollToSubviewOfTypeOfCellAtLocation(otherSubviewType, tableView->selectedColumn(), tableView->selectedRow());
      HistoryViewCell * selectedCell = (HistoryViewCell *)(tableView->selectedCell());
      selectedCell->setSelectedSubviewType(otherSubviewType);
      app()->setFirstResponder(selectedCell);
      return true;
    }
    return false;
  }
  
  }