Blame view

build3/apps/calculation/output_expressions_view.cpp 5.85 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
  #include "output_expressions_view.h"
  #include "scrollable_output_expressions_view.h"
  #include "../i18n.h"
  #include <assert.h>
  using namespace Poincare;
  
  namespace Calculation {
  
  OutputExpressionsView::OutputExpressionsView(Responder * parentResponder) :
    Responder(parentResponder),
    m_approximateExpressionView(),
    m_approximateSign(KDText::FontSize::Large, I18n::Message::AlmostEqual, 0.5f, 0.5f, Palette::GreyVeryDark),
    m_exactExpressionView(),
    m_selectedSubviewType(OutputExpressionsView::SubviewType::ExactOutput)
  {
  }
  
  void OutputExpressionsView::setExpressions(ExpressionLayout ** expressionsLayout) {
    m_approximateExpressionView.setExpression(expressionsLayout[0]);
    m_exactExpressionView.setExpression(expressionsLayout[1]);
    layoutSubviews();
  }
  
  void OutputExpressionsView::setEqualMessage(I18n::Message equalSignMessage) {
    m_approximateSign.setMessage(equalSignMessage);
  }
  
  void OutputExpressionsView::setHighlighted(bool highlight) {
    // Do not call HighlightCell::setHighlighted to avoid marking all cell as dirty
    m_highlighted = highlight;
    m_exactExpressionView.setBackgroundColor(backgroundColor());
    m_approximateExpressionView.setBackgroundColor(backgroundColor());
    if (highlight) {
      if (m_selectedSubviewType == SubviewType::ExactOutput) {
        m_exactExpressionView.setBackgroundColor(Palette::Select);
      } else {
        m_approximateExpressionView.setBackgroundColor(Palette::Select);
      }
    }
  }
  
  KDColor OutputExpressionsView::backgroundColor() const {
    KDColor background = m_even ? Palette::WallScreen : KDColorWhite;
    return background;
  }
  
  void OutputExpressionsView::reloadCell() {
    setHighlighted(isHighlighted());
    m_approximateSign.setBackgroundColor(backgroundColor());
    if (numberOfSubviews() == 1) {
      m_approximateExpressionView.setTextColor(KDColorBlack);
    } else {
      m_approximateExpressionView.setTextColor(Palette::GreyVeryDark);
    }
    layoutSubviews();
  }
  
  KDSize OutputExpressionsView::minimalSizeForOptimalDisplay() const {
    KDSize approximateExpressionSize = m_approximateExpressionView.minimalSizeForOptimalDisplay();
    if (numberOfSubviews() == 1) {
      return approximateExpressionSize;
    }
    KDSize exactExpressionSize = m_exactExpressionView.minimalSizeForOptimalDisplay();
    KDCoordinate exactBaseline = m_exactExpressionView.expressionLayout()->baseline();
    KDCoordinate approximateBaseline = m_approximateExpressionView.expressionLayout()->baseline();
    KDCoordinate height = max(exactBaseline, approximateBaseline) + max(exactExpressionSize.height()-exactBaseline, approximateExpressionSize.height()-approximateBaseline);
    KDSize approximateSignSize = m_approximateSign.minimalSizeForOptimalDisplay();
    return KDSize(exactExpressionSize.width()+approximateSignSize.width()+approximateExpressionSize.width()+2*k_digitHorizontalMargin, height);
  }
  
  void OutputExpressionsView::didBecomeFirstResponder() {
    if (numberOfSubviews() == 1) {
      setSelectedSubviewType(SubviewType::ApproximativeOutput);
    } else {
      setSelectedSubviewType(SubviewType::ExactOutput);
    }
  }
  
  bool OutputExpressionsView::handleEvent(Ion::Events::Event event) {
    if (numberOfSubviews() == 1) {
      return false;
    }
    ScrollableOutputExpressionsView * scrollResponder = static_cast<ScrollableOutputExpressionsView *>(parentResponder());
    KDCoordinate offset = scrollResponder->manualScrollingOffset().x();
    bool rightExpressionIsVisible = minimalSizeForOptimalDisplay().width() - m_approximateExpressionView.minimalSizeForOptimalDisplay().width() - offset < scrollResponder->bounds().width()
  ;
    bool leftExpressionIsVisible = m_exactExpressionView.minimalSizeForOptimalDisplay().width() - offset < scrollResponder->bounds().width();
    if ((event == Ion::Events::Right && m_selectedSubviewType == SubviewType::ExactOutput && rightExpressionIsVisible) ||
      (event == Ion::Events::Left && m_selectedSubviewType == SubviewType::ApproximativeOutput && leftExpressionIsVisible)) {
      SubviewType otherSubviewType = m_selectedSubviewType == SubviewType::ExactOutput ? SubviewType::ApproximativeOutput : SubviewType::ExactOutput;
      setSelectedSubviewType(otherSubviewType);
      return true;
    }
    return false;
  }
  
  OutputExpressionsView::SubviewType OutputExpressionsView::selectedSubviewType() {
    return m_selectedSubviewType;
  }
  
  void OutputExpressionsView::setSelectedSubviewType(OutputExpressionsView::SubviewType subviewType) {
    m_selectedSubviewType = subviewType;
    setHighlighted(isHighlighted());
  }
  
  int OutputExpressionsView::numberOfSubviews() const {
    if (m_exactExpressionView.expressionLayout() != nullptr) {
      return 3;
    }
    return 1;
  }
  
  View * OutputExpressionsView::subviewAtIndex(int index) {
    View * views[3] = {&m_approximateExpressionView, &m_approximateSign, &m_exactExpressionView};
    return views[index];
  }
  
  void OutputExpressionsView::layoutSubviews() {
    KDCoordinate height = bounds().height();
    KDSize approximateExpressionSize = m_approximateExpressionView.minimalSizeForOptimalDisplay();
    if (numberOfSubviews() == 1) {
      m_approximateExpressionView.setFrame(KDRect(0, 0, approximateExpressionSize.width(), height));
      return;
    }
    KDCoordinate exactBaseline = m_exactExpressionView.expressionLayout()->baseline();
    KDCoordinate approximateBaseline = m_approximateExpressionView.expressionLayout()->baseline();
    KDCoordinate baseline = max(exactBaseline, approximateBaseline);
    KDSize exactExpressionSize = m_exactExpressionView.minimalSizeForOptimalDisplay();
    KDSize approximateSignSize = m_approximateSign.minimalSizeForOptimalDisplay();
    m_exactExpressionView.setFrame(KDRect(0, baseline-exactBaseline, exactExpressionSize));
    m_approximateExpressionView.setFrame(KDRect(2*k_digitHorizontalMargin+exactExpressionSize.width()+approximateSignSize.width(), baseline-approximateBaseline, approximateExpressionSize));
    m_approximateSign.setFrame(KDRect(k_digitHorizontalMargin+exactExpressionSize.width(), baseline-approximateSignSize.height()/2, approximateSignSize));
  }
  
  }