From 219d6e67316a2bd4201b557c2aaa60115612618b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Tue, 20 Sep 2016 15:49:53 +0200 Subject: [PATCH] [escher] change structure of table view cell and define label view Change-Id: Iaeb3f567c4e1be6dfea3076ee7465c21e5ad98e9 --- apps/graph/list/parameter_controller.cpp | 27 +++------ apps/graph/list/parameter_controller.h | 8 +-- escher/Makefile | 1 + escher/include/escher.h | 1 + escher/include/escher/label_view.h | 20 +++++++ escher/include/escher/table_view.h | 2 +- escher/include/escher/table_view_cell.h | 28 +++++---- escher/src/label_view.cpp | 22 +++++++ escher/src/table_view.cpp | 4 ++ escher/src/table_view_cell.cpp | 73 ++++++++++++++++-------- 10 files changed, 125 insertions(+), 61 deletions(-) create mode 100644 escher/include/escher/label_view.h create mode 100644 escher/src/label_view.cpp diff --git a/apps/graph/list/parameter_controller.cpp b/apps/graph/list/parameter_controller.cpp index 5a3948ac7..36850f4a8 100644 --- a/apps/graph/list/parameter_controller.cpp +++ b/apps/graph/list/parameter_controller.cpp @@ -1,19 +1,14 @@ #include "parameter_controller.h" #include - -static const char * sMessages[] = { - "Couleur de la fonction", - "Activer/Desactiver", - "Supprimer la fonction" -}; - ParameterController::ParameterController(Responder * parentResponder) : ViewController(parentResponder), + m_colorCell(TableViewCell("Couleur de la fonction")), + m_enableCell(TableViewCell("Activer/Desactiver")), + m_deleteCell(TableViewCell("Supprimer la fonction")), m_tableView(TableView(this)), m_activeCell(0) { - m_messages = sMessages; } const char * ParameterController::title() const { @@ -33,12 +28,12 @@ void ParameterController::setActiveCell(int index) { return; } TableViewCell * previousCell = (TableViewCell *)(m_tableView.cellAtIndex(m_activeCell)); - previousCell->setFocused(false); + previousCell->setHighlighted(false); m_activeCell = index; m_tableView.scrollToRow(index); TableViewCell * cell = (TableViewCell *)(m_tableView.cellAtIndex(index)); - cell->setFocused(true); + cell->setHighlighted(true); } @@ -56,7 +51,7 @@ bool ParameterController::handleEvent(Ion::Events::Event event) { setActiveCell(m_activeCell-1); return true; case Ion::Events::Event::ENTER: - switch (m_activeCell) { + /*switch (m_activeCell) { case 0: return true; case 1: @@ -64,7 +59,7 @@ bool ParameterController::handleEvent(Ion::Events::Event event) { return true; case 2: return true; - } + }*/ default: return false; } @@ -78,18 +73,14 @@ int ParameterController::numberOfCells() { View * ParameterController::reusableCell(int index) { assert(index >= 0); assert(index < k_totalNumberOfCell); - return &m_cells[index]; + View * cells[] = {&m_colorCell, &m_enableCell, &m_deleteCell}; + return cells[index]; } int ParameterController::reusableCellCount() { return k_totalNumberOfCell; } -void ParameterController::willDisplayCellForIndex(View * cell, int index) { - TableViewCell * myCell = (TableViewCell *)cell; - myCell->setMessage(m_messages[index]); -} - KDCoordinate ParameterController::cellHeight() { return 35; } diff --git a/apps/graph/list/parameter_controller.h b/apps/graph/list/parameter_controller.h index 81ff14728..7ca23bcbc 100644 --- a/apps/graph/list/parameter_controller.h +++ b/apps/graph/list/parameter_controller.h @@ -17,18 +17,16 @@ public: void setActiveCell(int index); int numberOfCells() override; - void willDisplayCellForIndex(View * cell, int index) override; KDCoordinate cellHeight() override; View * reusableCell(int index) override; int reusableCellCount() override; private: constexpr static int k_totalNumberOfCell = 3; - // !!! CAUTION: The order here is important - // The cells should be initialized *before* the tableview! - TableViewCell m_cells[k_totalNumberOfCell]; + TableViewCell m_colorCell; + TableViewCell m_enableCell; + TableViewCell m_deleteCell; TableView m_tableView; - const char ** m_messages; int m_activeCell; Graph::Function * m_function; Graph::FunctionStore * m_functionStore; diff --git a/escher/Makefile b/escher/Makefile index 90739c5bd..b74cb14d5 100644 --- a/escher/Makefile +++ b/escher/Makefile @@ -5,6 +5,7 @@ objs += $(addprefix escher/src/,\ childless_view.o\ container.o\ invocation.o\ + label_view.o\ palette.o\ responder.o\ scroll_view.o\ diff --git a/escher/include/escher.h b/escher/include/escher.h index 6ca7a331a..294deebb0 100644 --- a/escher/include/escher.h +++ b/escher/include/escher.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include diff --git a/escher/include/escher/label_view.h b/escher/include/escher/label_view.h new file mode 100644 index 000000000..53176193c --- /dev/null +++ b/escher/include/escher/label_view.h @@ -0,0 +1,20 @@ +#ifndef ESCHER_LABEL_VIEW_H +#define ESCHER_LABEL_VIEW_H + +#include +#include + +class LabelView : public ChildlessView { + +public: + LabelView(const char * label, KDColor backgroundColor = KDColorWhite, KDColor textColor = KDColorBlack); + void setBackgroundColor(KDColor backgroundColor); + void setTextColor(KDColor textColor); + void drawRect(KDContext * ctx, KDRect rect) const override; +private: + const char * m_label; + KDColor m_backgroundColor; + KDColor m_textColor; +}; + +#endif diff --git a/escher/include/escher/table_view.h b/escher/include/escher/table_view.h index a5f5b78ea..d2e959bc8 100644 --- a/escher/include/escher/table_view.h +++ b/escher/include/escher/table_view.h @@ -6,7 +6,7 @@ class TableViewDataSource { public: virtual int numberOfCells() = 0; - virtual void willDisplayCellForIndex(View * cell, int index) = 0; + virtual void willDisplayCellForIndex(View * cell, int index); virtual KDCoordinate cellHeight() = 0; virtual View * reusableCell(int index) = 0; virtual int reusableCellCount() = 0; diff --git a/escher/include/escher/table_view_cell.h b/escher/include/escher/table_view_cell.h index 62c38203f..d0f050543 100644 --- a/escher/include/escher/table_view_cell.h +++ b/escher/include/escher/table_view_cell.h @@ -1,24 +1,28 @@ #ifndef ESCHER_TABLE_VIEW_CELL_H #define ESCHER_TABLE_VIEW_CELL_H -#include +#include +#include +#include -class TableViewCell : public ChildlessView { + +class TableViewCell : public View { public: - TableViewCell(); - void setMessage(const char * message); - bool isFocused() const; - void setFocused(bool focused); + TableViewCell(char * label); + LabelView * labelView(); + virtual View * contentView() const; + bool isHighlighted() const; + void setHighlighted(bool highlight); void drawRect(KDContext * ctx, KDRect rect) const override; + + int numberOfSubviews() const override; + View * subviewAtIndex(int index) override; + void layoutSubviews() override; private: - constexpr static KDColor k_separatorColor = KDColor(0xB4B7B9); - constexpr static KDColor k_tableBackgroundColor = KDColor(0xF0F3F5); - constexpr static KDColor k_focusedCellBackgroundColor = KDColor(0xBFD3EB); - constexpr static KDColor k_cellBackgroundColor = KDColor(0xFCFCFC); constexpr static KDCoordinate k_margin = 20; constexpr static KDCoordinate k_marginLabel = 5; - const char * m_message; - bool m_focused; + bool m_highlighted; + LabelView m_labelView; }; #endif diff --git a/escher/src/label_view.cpp b/escher/src/label_view.cpp new file mode 100644 index 000000000..2a33f1cb6 --- /dev/null +++ b/escher/src/label_view.cpp @@ -0,0 +1,22 @@ +#include +#include + +LabelView::LabelView(const char * label, KDColor backgroundColor, KDColor textColor) : + ChildlessView(), + m_label(label), + m_backgroundColor(backgroundColor), + m_textColor(textColor) +{ +} + +void LabelView::setBackgroundColor(KDColor backgroundColor) { + m_backgroundColor = backgroundColor; +} + +void LabelView::setTextColor(KDColor textColor) { + m_textColor = textColor; +} + +void LabelView::drawRect(KDContext * ctx, KDRect rect) const { + ctx->drawString(m_label, KDPointZero, m_textColor, m_backgroundColor); +} diff --git a/escher/src/table_view.cpp b/escher/src/table_view.cpp index f76f98a9f..138c36a67 100644 --- a/escher/src/table_view.cpp +++ b/escher/src/table_view.cpp @@ -5,6 +5,10 @@ extern "C" { #define MIN(x,y) ((x)<(y) ? (x) : (y)) +void TableViewDataSource::willDisplayCellForIndex(View * cell, int index) { +} + + TableView::TableView(TableViewDataSource * dataSource) : ScrollView(&m_contentView), m_contentView(TableView::ContentView(this, dataSource)) diff --git a/escher/src/table_view_cell.cpp b/escher/src/table_view_cell.cpp index 342ce0eaf..ee20b9a65 100644 --- a/escher/src/table_view_cell.cpp +++ b/escher/src/table_view_cell.cpp @@ -1,46 +1,69 @@ #include +#include -constexpr KDColor TableViewCell::k_separatorColor; -constexpr KDColor TableViewCell::k_tableBackgroundColor; -constexpr KDColor TableViewCell::k_focusedCellBackgroundColor; -constexpr KDColor TableViewCell::k_cellBackgroundColor; constexpr KDCoordinate TableViewCell::k_margin; constexpr KDCoordinate TableViewCell::k_marginLabel; -TableViewCell::TableViewCell() : - ChildlessView(), - m_focused(false), - m_message(nullptr) +TableViewCell::TableViewCell(char * label) : + View(), + m_highlighted(false), + m_labelView(LabelView(label, Palette::CellBackgroundColor, KDColorBlack)) { } -bool TableViewCell::isFocused() const { - return m_focused; +int TableViewCell::numberOfSubviews() const { + if (contentView() == nullptr) { + return 1; + } + return 2; } -void TableViewCell::setFocused(bool focused) { - m_focused = focused; - markRectAsDirty(bounds()); +View * TableViewCell::subviewAtIndex(int index) { + if (index == 0) { + return &m_labelView; + } + assert(numberOfSubviews() == 2 && index == 1); + return contentView(); } -void TableViewCell::setMessage(const char * message) { - m_message = message; +void TableViewCell::layoutSubviews() { + KDCoordinate width = bounds().width(); + KDCoordinate height = bounds().height(); + m_labelView.setFrame(KDRect(k_margin + k_marginLabel, k_marginLabel, 3*width/4, height)); + View * content = contentView(); + if (content) { + content->setFrame(KDRect(k_margin + k_marginLabel + 3*width/4, k_marginLabel, width/4-k_margin, height-k_marginLabel)); + } +} + +LabelView * TableViewCell::labelView() { + return &m_labelView; +} + +View * TableViewCell::contentView() const { + return nullptr; +} + +bool TableViewCell::isHighlighted() const { + return m_highlighted; +} + +void TableViewCell::setHighlighted(bool highlight) { + m_highlighted = highlight; + KDColor backgroundColor = highlight? Palette::FocusCellBackgroundColor : Palette::CellBackgroundColor; + m_labelView.setBackgroundColor(backgroundColor); markRectAsDirty(bounds()); } void TableViewCell::drawRect(KDContext * ctx, KDRect rect) const { - KDCoordinate width = bounds().width(); KDCoordinate height = bounds().height(); - KDColor backgroundColor = (m_focused ? k_focusedCellBackgroundColor : k_cellBackgroundColor); - KDColor textColor = (m_focused ? KDColorWhite : KDColorBlack); + KDColor backgroundColor = (m_highlighted ? Palette::FocusCellBackgroundColor : Palette::CellBackgroundColor); ctx->fillRect(KDRect(k_margin+1, 1, width-2*k_margin-1, height-1), backgroundColor); - ctx->fillRect(KDRect(0,0,k_margin,height), k_tableBackgroundColor); - ctx->fillRect(KDRect(k_margin,0,width-2*k_margin,1), k_separatorColor); - ctx->fillRect(KDRect(k_margin,0,1,height), k_separatorColor); - ctx->fillRect(KDRect(width-k_margin,0,1,height), k_separatorColor); - ctx->fillRect(KDRect(width-k_margin+1,0,k_margin, height), k_tableBackgroundColor); - - ctx->drawString(m_message, KDPoint(k_margin+k_marginLabel, k_marginLabel), textColor, backgroundColor); + ctx->fillRect(KDRect(0,0,k_margin,height), Palette::BackgroundColor); + ctx->fillRect(KDRect(k_margin,0,width-2*k_margin,1), Palette::LineColor); + ctx->fillRect(KDRect(k_margin,0,1,height), Palette::LineColor); + ctx->fillRect(KDRect(width-k_margin,0,1,height), Palette::LineColor); + ctx->fillRect(KDRect(width-k_margin+1,0,k_margin, height), Palette::BackgroundColor); }