Numworks Epsilon  1.4.1
Graphing Calculator Operating System
variable_box_leaf_cell.cpp
Go to the documentation of this file.
2 #include <assert.h>
3 
4 using namespace Poincare;
5 
8  m_labelView(KDText::FontSize::Small, 0, 0.5, KDColorBlack, KDColorWhite),
9  m_subtitleView(KDText::FontSize::Small, 0, 0.5, Palette::GreyDark, KDColorWhite),
10  m_expressionView(1.0f, 0.5f),
11  m_displayExpression(false)
12 {
13 }
14 
15 void VariableBoxLeafCell::displayExpression(bool displayExpression) {
16  m_displayExpression = displayExpression;
17 }
18 
19 int VariableBoxLeafCell::numberOfSubviews() const {
20  if (m_displayExpression) {
21  return 3;
22  }
23  return 2;
24 }
25 
26 View * VariableBoxLeafCell::subviewAtIndex(int index) {
27  if (index == 0) {
28  return &m_labelView;
29  }
30  if (index == 1) {
31  return &m_subtitleView;
32  }
33  assert(numberOfSubviews() == 3 && index == 2);
34  return &m_expressionView;
35 }
36 
37 void VariableBoxLeafCell::layoutSubviews() {
38  KDCoordinate width = bounds().width();
39  KDCoordinate height = bounds().height();
40  if (numberOfSubviews() == 3) {
41  KDSize labelSize = m_labelView.minimalSizeForOptimalDisplay();
42  KDSize subtitleSize = m_subtitleView.minimalSizeForOptimalDisplay();
43  m_labelView.setFrame(KDRect(k_separatorThickness+k_widthMargin, k_separatorThickness, labelSize.width(), height/2 - k_separatorThickness));
44  m_subtitleView.setFrame(KDRect(k_separatorThickness+k_widthMargin, height/2, subtitleSize.width(), height/2-k_separatorThickness));
45  m_subtitleView.setAlignment(0.0f, 0.5f);
46  KDCoordinate maxTextSize = subtitleSize.width() > labelSize.width() ? subtitleSize.width() : labelSize.width();
47  KDCoordinate expressionAvailableSize = width-k_separatorThickness-3*k_widthMargin-maxTextSize;
48  m_expressionView.setFrame(KDRect(width - expressionAvailableSize - k_widthMargin, k_separatorThickness, expressionAvailableSize, height-2*k_separatorThickness));
49  if (m_expressionView.minimalSizeForOptimalDisplay().width() > expressionAvailableSize) {
50  m_expressionView.setAlignment(0.0f, 0.5f);
51  } else {
52  m_expressionView.setAlignment(1.0f, 0.5f);
53  }
54  return;
55  }
56  m_labelView.setFrame(KDRect(k_separatorThickness+k_widthMargin, k_separatorThickness, width/2-k_separatorThickness-k_widthMargin, height-2*k_separatorThickness));
57  m_subtitleView.setFrame(KDRect(width/2, 1, width/2-k_separatorThickness-k_widthMargin, height-2*k_separatorThickness));
58  m_subtitleView.setAlignment(1.0f, 0.5f);
59  return;
60 }
61 
64  KDColor backgroundColor = isHighlighted()? Palette::Select : KDColorWhite;
65  m_labelView.setBackgroundColor(backgroundColor);
66  m_subtitleView.setBackgroundColor(backgroundColor);
67  m_expressionView.setBackgroundColor(backgroundColor);
68 }
69 
70 void VariableBoxLeafCell::setLabel(const char * text) {
71  m_labelView.setText(text);
72 }
73 
74 void VariableBoxLeafCell::setSubtitle(const char * text) {
75  m_subtitleView.setText(text);
76  layoutSubviews();
77 }
78 
80  m_expressionView.setExpression(expressionLayout);
81 }
82 
84  KDCoordinate width = bounds().width();
85  KDCoordinate height = bounds().height();
86  KDColor backgroundColor = isHighlighted() ? Palette::Select : KDColorWhite;
87  ctx->fillRect(KDRect(k_separatorThickness, k_separatorThickness, width-2*k_separatorThickness, height-2*k_separatorThickness), backgroundColor);
88  ctx->fillRect(KDRect(0, height-k_separatorThickness, width, k_separatorThickness), Palette::GreyBright);
89  ctx->fillRect(KDRect(0, 0, width, k_separatorThickness), Palette::GreyBright);
90  ctx->fillRect(KDRect(0, 0, k_separatorThickness, height-k_separatorThickness), Palette::GreyBright);
91  ctx->fillRect(KDRect(width-k_separatorThickness, 0, k_separatorThickness, height-k_separatorThickness), Palette::GreyBright);
92  }
void setBackgroundColor(KDColor backgroundColor)
#define assert(e)
Definition: assert.h:9
void setText(const char *text) override
void setFrame(KDRect frame)
Definition: view.cpp:125
int16_t KDCoordinate
Definition: coordinate.h:6
constexpr KDCoordinate width() const
Definition: size.h:10
void setExpression(Poincare::ExpressionLayout *expressionLayout)
void setLabel(const char *text)
KDSize minimalSizeForOptimalDisplay() const override
void setAlignment(float horizontalAlignment, float verticalAlignment)
Definition: text.h:8
Definition: size.h:6
constexpr KDColor KDColorWhite
Definition: color.h:42
void setExpressionLayout(Poincare::ExpressionLayout *expressionLayout)
static constexpr KDColor Select
Definition: palette.h:17
void displayExpression(bool displayExpression)
constexpr KDColor KDColorBlack
Definition: color.h:41
#define false
Definition: stdbool.h:9
bool isHighlighted() const
Definition: rect.h:26
void fillRect(KDRect rect, KDColor color)
Definition: context_rect.cpp:8
Definition: color.h:6
void drawRect(KDContext *ctx, KDRect rect) const override
Definition: view.h:23
void setAlignment(float horizontalAlignment, float verticalAlignment)
Definition: text_view.cpp:24
KDCoordinate width() const
Definition: rect.h:39
void setSubtitle(const char *text)
virtual void reloadCell()
void setBackgroundColor(KDColor backgroundColor)
Definition: text_view.cpp:14
static constexpr KDColor GreyBright
Definition: palette.h:13
KDCoordinate height() const
Definition: rect.h:40
KDRect bounds() const
Definition: view.cpp:157
KDSize minimalSizeForOptimalDisplay() const override
Definition: text_view.cpp:35
Definition: palette.h:6