[apps/sequence/list] Create a class sequence expression cell inheriting

from function expression cell

Change-Id: Icaec669c684ebbaa353f6801ca2f976e5fb76a9a
This commit is contained in:
Émilie Feral
2017-02-07 14:30:04 +01:00
parent 460e692309
commit 7b9dbb88a5
5 changed files with 166 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
app_objs += $(addprefix apps/sequence/,\
app.o\
list/sequence_expression_cell.o\
values/values_controller.o\
sequence.o\
sequence_store.o\

View File

@@ -0,0 +1,116 @@
#include "sequence_expression_cell.h"
using namespace Shared;
namespace Sequence {
SequenceExpressionCell::SequenceExpressionCell(Responder * parentResponder) :
FunctionExpressionCell(),
Responder(parentResponder),
m_sequence(nullptr),
m_numberOfSubCells(1),
m_selectedSubCell(0),
m_firstInitialConditionView(EvenOddExpressionCell()),
m_secondInitialConditionView(EvenOddExpressionCell())
{
}
int SequenceExpressionCell::selectedSubCell() {
return m_selectedSubCell;
}
void SequenceExpressionCell::selectSubCell(int selectedSubCell) {
m_selectedSubCell = selectedSubCell;
m_expressionView.setHighlighted(selectedSubCell == 0);
m_firstInitialConditionView.setHighlighted(selectedSubCell == 1);
m_secondInitialConditionView.setHighlighted(selectedSubCell == 2);
reloadCell();
}
void SequenceExpressionCell::setFunction(Function * f) {
FunctionExpressionCell::setFunction(f);
m_sequence = (Sequence *)f;
m_numberOfSubCells = (int)m_sequence->type()+1;
if (m_numberOfSubCells > 1) {
m_firstInitialConditionView.setExpression(m_sequence->firstInitialConditionLayout());
}
if (m_numberOfSubCells > 2) {
m_secondInitialConditionView.setExpression(m_sequence->secondInitialConditionLayout());
}
}
void SequenceExpressionCell::reloadCell() {
FunctionExpressionCell::reloadCell();
m_firstInitialConditionView.setBackgroundColor(backgroundColor());
m_secondInitialConditionView.setBackgroundColor(backgroundColor());
if (m_numberOfSubCells > 1 && m_sequence) {
bool active = m_sequence->isActive();
KDColor textColor = active ? KDColorBlack : Palette::GreyDark;
m_firstInitialConditionView.setTextColor(textColor);
m_secondInitialConditionView.setTextColor(textColor);
}
}
void SequenceExpressionCell::setHighlighted(bool highlight) {
m_expressionView.setHighlighted(false);
m_firstInitialConditionView.setHighlighted(false);
m_secondInitialConditionView.setHighlighted(false);
TableViewCell::setHighlighted(highlight);
if (isHighlighted()) {
if (m_selectedSubCell == 0) {
m_expressionView.setHighlighted(true);
}
if (m_selectedSubCell == 1) {
m_firstInitialConditionView.setHighlighted(true);
}
if (m_selectedSubCell == 2) {
m_secondInitialConditionView.setHighlighted(true);
}
}
reloadCell();
}
void SequenceExpressionCell::setEven(bool even) {
m_expressionView.setEven(even);
m_firstInitialConditionView.setEven(even);
m_secondInitialConditionView.setEven(even);
reloadCell();
}
int SequenceExpressionCell::numberOfSubviews() const {
return m_numberOfSubCells;
}
View * SequenceExpressionCell::subviewAtIndex(int index) {
if (index == 0) {
return &m_expressionView;
}
if (index == 1) {
return &m_firstInitialConditionView;
}
return &m_secondInitialConditionView;
}
void SequenceExpressionCell::layoutSubviews() {
KDCoordinate cellHeight = bounds().height()/m_numberOfSubCells;
KDRect expressionFrame(k_separatorThickness, 0, bounds().width() - k_separatorThickness, cellHeight);
m_expressionView.setFrame(expressionFrame);
expressionFrame = KDRect(k_separatorThickness, cellHeight, bounds().width() - k_separatorThickness, cellHeight);
m_firstInitialConditionView.setFrame(expressionFrame);
expressionFrame = KDRect(k_separatorThickness, 2*cellHeight, bounds().width() - k_separatorThickness, cellHeight);
m_secondInitialConditionView.setFrame(expressionFrame);
}
bool SequenceExpressionCell::handleEvent(Ion::Events::Event event) {
if (m_selectedSubCell < 2 && event == Ion::Events::Down) {
selectSubCell(m_selectedSubCell+1);
return true;
}
if (m_selectedSubCell > 0 && event == Ion::Events::Up) {
selectSubCell(m_selectedSubCell-1);
return true;
}
return false;
}
}

View File

@@ -0,0 +1,32 @@
#ifndef SEQUENCE_SEQUENCE_EXPRESSION_CELL_H
#define SEQUENCE_SEQUENCE_EXPRESSION_CELL_H
#include "../sequence.h"
#include "../../shared/function_expression_cell.h"
namespace Sequence {
class SequenceExpressionCell : public Shared::FunctionExpressionCell, public Responder {
public:
SequenceExpressionCell(Responder * parentResponder = nullptr);
void setFunction(Shared::Function * f) override;
int selectedSubCell();
void selectSubCell(int index);
void reloadCell() override;
void setHighlighted(bool highlight) override;
void setEven(bool even) override;
int numberOfSubviews() const override;
View * subviewAtIndex(int index) override;
void layoutSubviews() override;
bool handleEvent(Ion::Events::Event event) override;
private:
Sequence * m_sequence;
int m_numberOfSubCells;
int m_selectedSubCell;
EvenOddExpressionCell m_firstInitialConditionView;
EvenOddExpressionCell m_secondInitialConditionView;
};
}
#endif

View File

@@ -6,7 +6,7 @@ namespace Shared {
FunctionExpressionCell::FunctionExpressionCell() :
EvenOddCell(),
m_function(nullptr),
m_expressionView(ExpressionView())
m_expressionView(EvenOddExpressionCell())
{
}
@@ -17,7 +17,6 @@ void FunctionExpressionCell::setFunction(Function * f) {
void FunctionExpressionCell::reloadCell() {
EvenOddCell::reloadCell();
m_expressionView.setBackgroundColor(backgroundColor());
if (m_function) {
bool active = m_function->isActive();
KDColor textColor = active ? KDColorBlack : Palette::GreyDark;
@@ -25,6 +24,16 @@ void FunctionExpressionCell::reloadCell() {
}
}
void FunctionExpressionCell::setEven(bool even) {
EvenOddCell::setEven(even);
m_expressionView.setEven(even);
}
void FunctionExpressionCell::setHighlighted(bool highlight) {
EvenOddCell::setHighlighted(highlight);
m_expressionView.setHighlighted(highlight);
}
Function * FunctionExpressionCell::function() {
return m_function;
}

View File

@@ -12,15 +12,18 @@ public:
virtual void setFunction(Function * f);
Function * function();
void reloadCell() override;
void setEven(bool even) override;
void setHighlighted(bool highlight) override;
int numberOfSubviews() const override;
View * subviewAtIndex(int index) override;
void layoutSubviews() override;
void drawRect(KDContext * ctx, KDRect rect) const override;
private:
static constexpr KDCoordinate k_emptyRowHeight = 50;
protected:
constexpr static KDCoordinate k_separatorThickness = 1;
Function * m_function;
ExpressionView m_expressionView;
EvenOddExpressionCell m_expressionView;
private:
static constexpr KDCoordinate k_emptyRowHeight = 50;
};
}