Blame view

epsilon-master/apps/probability/calculation_cell.cpp 2.35 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
  #include "calculation_cell.h"
  #include "responder_image_cell.h"
  #include "../i18n.h"
  #include <assert.h>
  
  namespace Probability {
  
  CalculationCell::CalculationCell(Responder * parentResponder, char * draftTextBuffer, TextFieldDelegate * textFieldDelegate) :
    m_text(KDText::FontSize::Large, I18n::Message::Default, 0.5f, 0.5f),
    m_calculation(parentResponder, textFieldDelegate, draftTextBuffer),
    m_isResponder(true)
  {
  }
  
  Responder * CalculationCell::responder() {
    if (m_isResponder) {
      return &m_calculation;
    }
    return nullptr;
  }
  
  void CalculationCell::setResponder(bool shouldBeResponder) {
    m_isResponder = shouldBeResponder;
  }
  
  void CalculationCell::setHighlighted(bool highlight) {
    HighlightCell::setHighlighted(highlight);
    m_calculation.setHighlighted(highlight);
  }
  
  KDSize CalculationCell::minimalSizeForOptimalDisplay() const {
    KDSize textSize = m_text.minimalSizeForOptimalDisplay();
    return KDSize(2*k_margin+textSize.width()+calculationCellWidth()+2*ResponderImageCell::k_outline, KDText::charSize().height());
  }
  
  void CalculationCell::drawRect(KDContext * ctx, KDRect rect) const {
    ctx->fillRect(bounds(), KDColorWhite);
    if (m_isResponder) {
      KDSize textSize = m_text.minimalSizeForOptimalDisplay();
      ctx->strokeRect(KDRect(2*k_margin+textSize.width(), 0, calculationCellWidth()+2*ResponderImageCell::k_outline, ImageCell::k_height+2*ResponderImageCell::k_outline), Palette::GreyMiddle);
    }
  }
  
  EditableTextCell * CalculationCell::editableTextCell() {
    return &m_calculation;
  }
  
  MessageTextView * CalculationCell::messageTextView() {
    return &m_text;
  }
  
  int CalculationCell::numberOfSubviews() const {
    return 2;
  }
  
  View * CalculationCell::subviewAtIndex(int index) {
    assert(index >= 0 && index < 2);
    if (index == 0) {
      return &m_text;
    }
    return &m_calculation;
  }
  
  void CalculationCell::layoutSubviews() {
    KDSize textSize = m_text.minimalSizeForOptimalDisplay();
    m_text.setFrame(KDRect(k_margin, 0, textSize.width(), bounds().height()));
    m_calculation.setFrame(KDRect(2*k_margin+textSize.width()+ResponderImageCell::k_outline, ResponderImageCell::k_outline, calculationCellWidth(), ImageCell::k_height));
  }
  
  KDCoordinate CalculationCell::calculationCellWidth() const {
    KDCoordinate calculationCellWidth = m_calculation.minimalSizeForOptimalDisplay().width();
    return min(k_maxTextFieldWidth, max(k_minTextFieldWidth, calculationCellWidth));
  }
  
  }