From b56d88cefa5da800f333f3cd28d47b56f9fd3b3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Mon, 24 Oct 2016 16:07:28 +0200 Subject: [PATCH] [apps/graph] make the function title cells consistent Change-Id: Ib59f3d5f8a679cddd8a88e16711138425a6e9ecb --- apps/graph/Makefile | 4 +- apps/graph/function_title_cell.cpp | 52 ++++++++++++++++++++ apps/graph/function_title_cell.h | 32 ++++++++++++ apps/graph/list/function_cell.cpp | 22 --------- apps/graph/list/function_cell.h | 24 --------- apps/graph/list/function_expression_view.cpp | 20 ++++++-- apps/graph/list/function_expression_view.h | 11 ++++- apps/graph/list/function_name_view.cpp | 44 ----------------- apps/graph/list/function_name_view.h | 22 --------- apps/graph/list/list_controller.cpp | 20 ++++++-- apps/graph/list/list_controller.h | 4 +- apps/graph/values/function_title_cell.cpp | 39 --------------- apps/graph/values/function_title_cell.h | 21 -------- apps/graph/values/values_controller.cpp | 11 +++-- apps/graph/values/values_controller.h | 2 +- escher/include/escher/palette.h | 1 + escher/src/palette.cpp | 3 +- 17 files changed, 139 insertions(+), 193 deletions(-) create mode 100644 apps/graph/function_title_cell.cpp create mode 100644 apps/graph/function_title_cell.h delete mode 100644 apps/graph/list/function_cell.cpp delete mode 100644 apps/graph/list/function_cell.h delete mode 100644 apps/graph/list/function_name_view.cpp delete mode 100644 apps/graph/list/function_name_view.h delete mode 100644 apps/graph/values/function_title_cell.cpp delete mode 100644 apps/graph/values/function_title_cell.h diff --git a/apps/graph/Makefile b/apps/graph/Makefile index 73da87c24..d41986df9 100644 --- a/apps/graph/Makefile +++ b/apps/graph/Makefile @@ -4,19 +4,17 @@ app_objs += $(addprefix apps/graph/,\ evaluate_context.o\ function.o\ function_store.o\ + function_title_cell.o\ graph/cursor_view.o\ graph/graph_controller.o\ graph/graph_view.o\ - list/function_cell.o\ list/function_expression_view.o\ - list/function_name_view.o\ list/new_function_cell.o\ list/list_controller.o\ list/parameter_controller.o\ values/abscissa_parameter_controller.o\ values/derivative_parameter_controller.o\ values/function_parameter_controller.o\ - values/function_title_cell.o\ values/interval.o\ values/title_cell.o\ values/value_cell.o\ diff --git a/apps/graph/function_title_cell.cpp b/apps/graph/function_title_cell.cpp new file mode 100644 index 000000000..0b7cc6695 --- /dev/null +++ b/apps/graph/function_title_cell.cpp @@ -0,0 +1,52 @@ +#include "function_title_cell.h" +#include + +namespace Graph { + +FunctionTitleCell::FunctionTitleCell() : + EvenOddCell(), + m_bufferTextView(0.5f, 0.5f) +{ +} + +void FunctionTitleCell::reloadCell() { + EvenOddCell::reloadCell(); + m_bufferTextView.setBackgroundColor(backgroundColor()); +} + +void FunctionTitleCell::setText(const char * title) { + m_bufferTextView.setText(title); +} + +void FunctionTitleCell::setColor(KDColor color) { + m_functionColor = color; + m_bufferTextView.setTextColor(color); +} + +void FunctionTitleCell::setOrientation(Orientation orientation) { + m_orientation = orientation; +} + +int FunctionTitleCell::numberOfSubviews() const { + return 1; +} + +View * FunctionTitleCell::subviewAtIndex(int index) { + assert(index == 0); + return &m_bufferTextView; +} + +void FunctionTitleCell::layoutSubviews() { + m_bufferTextView.setFrame(bounds()); +} + +void FunctionTitleCell::drawRect(KDContext * ctx, KDRect rect) const { + EvenOddCell::drawRect(ctx, rect); + if (m_orientation == Orientation::VerticalIndicator){ + ctx->fillRect(KDRect(0, 0, k_colorIndicatorThickness, bounds().height()), m_functionColor); + } else { + ctx->fillRect(KDRect(0, 0, bounds().width(), k_colorIndicatorThickness), m_functionColor); + } +} + +} diff --git a/apps/graph/function_title_cell.h b/apps/graph/function_title_cell.h new file mode 100644 index 000000000..330e05979 --- /dev/null +++ b/apps/graph/function_title_cell.h @@ -0,0 +1,32 @@ +#ifndef GRAPH_FUNCTION_TITLE_CELL_H +#define GRAPH_FUNCTION_TITLE_CELL_H + +#include +#include "even_odd_cell.h" + +namespace Graph { +class FunctionTitleCell : public EvenOddCell { +public: + enum class Orientation { + HorizontalIndicator, + VerticalIndicator + }; + FunctionTitleCell(); + void setColor(KDColor color); + void setOrientation(Orientation orientation); + void setText(const char * textContent); + void drawRect(KDContext * ctx, KDRect rect) const override; + void reloadCell() override; + int numberOfSubviews() const override; + View * subviewAtIndex(int index) override; + void layoutSubviews() override; + private: + constexpr static KDCoordinate k_colorIndicatorThickness = 2; + KDColor m_functionColor; + BufferTextView m_bufferTextView; + Orientation m_orientation; +}; + +} + +#endif diff --git a/apps/graph/list/function_cell.cpp b/apps/graph/list/function_cell.cpp deleted file mode 100644 index 88ec995ae..000000000 --- a/apps/graph/list/function_cell.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "function_cell.h" - -namespace Graph { - -constexpr KDColor FunctionCell::k_desactiveTextColor; - -FunctionCell::FunctionCell() : - EvenOddCell(), - m_function(nullptr) -{ -} - -void FunctionCell::setFunction(Function * f) { - m_function = f; - markRectAsDirty(bounds()); -} - -Function * FunctionCell::function() { - return m_function; -} - -} diff --git a/apps/graph/list/function_cell.h b/apps/graph/list/function_cell.h deleted file mode 100644 index e29e6e64b..000000000 --- a/apps/graph/list/function_cell.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef GRAPH_FUNCTION_CELL_H -#define GRAPH_FUNCTION_CELL_H - -#include -#include "../function.h" -#include "../even_odd_cell.h" - -namespace Graph { - -class FunctionCell : public EvenOddCell { -public: - FunctionCell(); - void setFunction(Function * f); - Function * function(); - - static constexpr KDColor k_desactiveTextColor = KDColor(0x646464); - -protected: - Function * m_function; -}; - -} - -#endif diff --git a/apps/graph/list/function_expression_view.cpp b/apps/graph/list/function_expression_view.cpp index 4f780af21..01301d2ab 100644 --- a/apps/graph/list/function_expression_view.cpp +++ b/apps/graph/list/function_expression_view.cpp @@ -2,13 +2,27 @@ namespace Graph { +constexpr KDColor FunctionExpressionView::k_separatorColor; + FunctionExpressionView::FunctionExpressionView() : - FunctionCell() + EvenOddCell(), + m_function(nullptr) { } +void FunctionExpressionView::setFunction(Function * f) { + m_function = f; + markRectAsDirty(bounds()); +} + +Function * FunctionExpressionView::function() { + return m_function; +} + void FunctionExpressionView::drawRect(KDContext * ctx, KDRect rect) const { EvenOddCell::drawRect(ctx, rect); + // Color the separator + ctx->fillRect(KDRect(0, 0, k_separatorThickness, bounds().height()), k_separatorColor); if (m_function->layout() == nullptr) { return; } @@ -16,10 +30,10 @@ void FunctionExpressionView::drawRect(KDContext * ctx, KDRect rect) const { KDColor background = backgroundColor(); // Select text color according to the state of the function bool active = m_function->isActive(); - KDColor text = active ? KDColorBlack : FunctionCell::k_desactiveTextColor; + KDColor text = active ? KDColorBlack : Palette::k_desactiveTextColor; //Position the origin of expression KDSize expressionSize = m_function->layout()->size(); - KDPoint origin(0, 0.5f*(m_frame.height() - expressionSize.height())); + KDPoint origin(k_separatorThickness, 0.5f*(m_frame.height() - expressionSize.height())); m_function->layout()->draw(ctx, origin, text, background); } diff --git a/apps/graph/list/function_expression_view.h b/apps/graph/list/function_expression_view.h index e25346e02..e3e79fe41 100644 --- a/apps/graph/list/function_expression_view.h +++ b/apps/graph/list/function_expression_view.h @@ -2,16 +2,23 @@ #define GRAPH_FUNCTION_EXPRESSION_H #include -#include "function_cell.h" +#include "../even_odd_cell.h" +#include "../function.h" namespace Graph { class FunctionCell; -class FunctionExpressionView : public FunctionCell { +class FunctionExpressionView : public EvenOddCell { public: FunctionExpressionView(); + void setFunction(Function * f); + Function * function(); void drawRect(KDContext * ctx, KDRect rect) const override; +private: + constexpr static KDColor k_separatorColor = KDColor(0xEFF2F4); + constexpr static KDCoordinate k_separatorThickness = 1; + Function * m_function; }; } diff --git a/apps/graph/list/function_name_view.cpp b/apps/graph/list/function_name_view.cpp deleted file mode 100644 index b59d4768e..000000000 --- a/apps/graph/list/function_name_view.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "function_name_view.h" -#include "../function_store.h" - -namespace Graph { - -constexpr KDColor FunctionNameView::k_separatorColor; - -FunctionNameView::FunctionNameView() : - FunctionCell() -{ -} - -void FunctionNameView::drawRect(KDContext * ctx, KDRect rect) const { - EvenOddCell::drawRect(ctx, rect); - - KDCoordinate height = bounds().height(); - KDCoordinate width = bounds().width(); - // Color the color indicator - KDColor functionColor = m_function->color(); - ctx->fillRect(KDRect(0, 0, k_colorIndicatorThickness, height), functionColor); - // Color the separator - ctx->fillRect(KDRect(width - k_separatorThickness, 0, k_separatorThickness, height), k_separatorColor); - // Select function name color and the text color according to the state of the function - bool active = m_function->isActive(); - KDColor textColor = active ? KDColorBlack : FunctionCell::k_desactiveTextColor; - KDColor functionNameColor = active ? functionColor : FunctionCell::k_desactiveTextColor; - // Select the background color according to the even line and the cursor selection - KDColor background = backgroundColor(); - // Position the name of the function - const char * functionName = m_function->name(); - KDSize textSize = KDText::stringSize(functionName); - KDCoordinate baseline = textSize.height(); - KDSize expressionSize = textSize; - if (m_function->layout()) { - baseline = m_function->layout()->baseline(); - expressionSize = m_function->layout()->size(); - } - KDPoint origin(0.5f*(k_colorIndicatorThickness + m_frame.width() - 4*textSize.width()), - baseline-textSize.height()+0.5f*(m_frame.height() - expressionSize.height())); - ctx->drawString(functionName, origin, functionNameColor, background); - ctx->drawString(Function::Parameter, origin.translatedBy(KDPoint(textSize.width(), 0)), textColor, background); -} - -} diff --git a/apps/graph/list/function_name_view.h b/apps/graph/list/function_name_view.h deleted file mode 100644 index ddeb4b86f..000000000 --- a/apps/graph/list/function_name_view.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef GRAPH_FUNCTION_NAME_VIEW_H -#define GRAPH_FUNCTION_NAME_VIEW_H - -#include -#include "function_cell.h" - -namespace Graph { - -class FunctionNameView : public FunctionCell { -public: - FunctionNameView(); - - void drawRect(KDContext * ctx, KDRect rect) const override; -private: - constexpr static KDCoordinate k_colorIndicatorThickness = 2; - constexpr static KDCoordinate k_separatorThickness = 1; - constexpr static KDColor k_separatorColor = KDColor(0xEFF2F4); -}; - -} - -#endif diff --git a/apps/graph/list/list_controller.cpp b/apps/graph/list/list_controller.cpp index 24e47d9d9..1b5e87f10 100644 --- a/apps/graph/list/list_controller.cpp +++ b/apps/graph/list/list_controller.cpp @@ -203,8 +203,7 @@ bool ListController::handleEnter() { if (m_activeCelly == numberOfRows() - 1) { return true; } - FunctionNameView * functionCell = (FunctionNameView *)(m_tableView.cellAtLocation(m_activeCellx, m_activeCelly)); - configureFunction(functionCell->function()); + configureFunction(m_functionStore->functionAtIndex(m_activeCelly)); return true; } case 1: @@ -241,7 +240,7 @@ View * ListController::reusableCell(int index, int type) { assert(index < k_maxNumberOfRows); switch (type) { case 0: - return &m_nameCells[index]; + return &m_functionTitleCells[index]; case 1: return &m_expressionCells[index]; case 2: @@ -263,8 +262,19 @@ int ListController::reusableCellCount(int type) { void ListController::willDisplayCellAtLocation(View * cell, int i, int j) { if (j < numberOfRows() - 1) { - FunctionCell * myCell = (FunctionCell *)cell; - myCell->setFunction(m_functionStore->functionAtIndex(j)); + if (i == 0) { + FunctionTitleCell * myFunctionCell = (FunctionTitleCell *)cell; + Function * function = m_functionStore->functionAtIndex(j); + char bufferName[5] = "*(x)"; + bufferName[0] = *function->name(); + myFunctionCell->setText(bufferName); + KDColor functionNameColor = function->isActive() ? function->color() : Palette::k_desactiveTextColor; + myFunctionCell->setColor(functionNameColor); + myFunctionCell->setOrientation(FunctionTitleCell::Orientation::VerticalIndicator); + } else { + FunctionExpressionView * myCell = (FunctionExpressionView *)cell; + myCell->setFunction(m_functionStore->functionAtIndex(j)); + } } EvenOddCell * myCell = (EvenOddCell *)cell; myCell->setEven(j%2 == 0); diff --git a/apps/graph/list/list_controller.h b/apps/graph/list/list_controller.h index bef32c669..94b426500 100644 --- a/apps/graph/list/list_controller.h +++ b/apps/graph/list/list_controller.h @@ -3,7 +3,7 @@ #include #include "../function_store.h" -#include "function_name_view.h" +#include "../function_title_cell.h" #include "function_expression_view.h" #include "new_function_cell.h" #include "parameter_controller.h" @@ -44,7 +44,7 @@ private: constexpr static int k_maxNumberOfRows = 6; // !!! CAUTION: The order here is important // The cells should be initialized *before* the TableView! - FunctionNameView m_nameCells[k_maxNumberOfRows]; + FunctionTitleCell m_functionTitleCells[k_maxNumberOfRows]; FunctionExpressionView m_expressionCells[k_maxNumberOfRows]; EvenOddCell m_emptyCell; NewFunctionCell m_addNewFunction; diff --git a/apps/graph/values/function_title_cell.cpp b/apps/graph/values/function_title_cell.cpp deleted file mode 100644 index 0fdbdf3d5..000000000 --- a/apps/graph/values/function_title_cell.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "function_title_cell.h" -#include "../function.h" - -namespace Graph { - -void FunctionTitleCell::setColor(KDColor color) { - m_functionColor = color; -} - -void FunctionTitleCell::setDerivative(bool derivative) { - m_derivative = derivative; - PointerTextView * pointerTextView = (PointerTextView *)subviewAtIndex(0); - int textwidth = KDText::stringSize("f").width(); - // Here we compute the right horizontal alignment to center "f(x)" or "f'(x)" - if (derivative) { - pointerTextView->setAlignment(0.5f*(m_frame.width() - 5*textwidth)/(m_frame.width() - textwidth), 0.5f); - } else { - pointerTextView->setAlignment(0.5f*(m_frame.width() - 4*textwidth)/(m_frame.width() - textwidth), 0.5f); - } -} - -void FunctionTitleCell::drawRect(KDContext * ctx, KDRect rect) const { - EvenOddCell::drawRect(ctx, rect); - // Write the "(x)" - KDColor background = backgroundColor(); - KDSize textSize = KDText::stringSize("f"); - KDPoint origin(0.5f*(m_frame.width() - 4*textSize.width()), 0.5f*(m_frame.height() - textSize.height())); - if (m_derivative) { - origin = KDPoint(0.5f*(m_frame.width() - 5*textSize.width()), 0.5f*(m_frame.height() - textSize.height())); - ctx->drawString("'", origin.translatedBy(KDPoint(textSize.width(), 0)), m_functionColor , background); - ctx->drawString(Function::Parameter, origin.translatedBy(KDPoint(2*textSize.width(), 0)), m_functionColor , background); - } else { - ctx->drawString(Function::Parameter, origin.translatedBy(KDPoint(textSize.width(), 0)), m_functionColor , background); - } - // Color the color indicator - ctx->fillRect(KDRect(0, 0, bounds().width(), k_colorIndicatorThickness), m_functionColor); -} - -} diff --git a/apps/graph/values/function_title_cell.h b/apps/graph/values/function_title_cell.h deleted file mode 100644 index 59abe61c3..000000000 --- a/apps/graph/values/function_title_cell.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef GRAPH_FUNCTION_TITLE_CELL_H -#define GRAPH_FUNCTION_TITLE_CELL_H - -#include -#include "title_cell.h" - -namespace Graph { -class FunctionTitleCell : public TitleCell { -public: - void setColor(KDColor color); - void drawRect(KDContext * ctx, KDRect rect) const override; - void setDerivative(bool derivative); - private: - constexpr static KDCoordinate k_colorIndicatorThickness = 2; - KDColor m_functionColor; - bool m_derivative; -}; - -} - -#endif diff --git a/apps/graph/values/values_controller.cpp b/apps/graph/values/values_controller.cpp index e95c2dc2e..6bab16165 100644 --- a/apps/graph/values/values_controller.cpp +++ b/apps/graph/values/values_controller.cpp @@ -384,13 +384,16 @@ void ValuesController::willDisplayCellAtLocation(View * cell, int i, int j) { } FunctionTitleCell * myFunctionCell = (FunctionTitleCell *)cell; Function * function = functionAtColumn(i); + char bufferName[6] = "f'(x)"; + bufferName[1] = *function->name(); + myFunctionCell->setText(bufferName + 1); myFunctionCell->setColor(function->color()); + myFunctionCell->setOrientation(FunctionTitleCell::Orientation::HorizontalIndicator); if (isDerivativeColumn(i)) { - myFunctionCell->setDerivative(true); - } else { - myFunctionCell->setDerivative(false); + bufferName[0] = bufferName[1]; + bufferName[1] = '\''; + myFunctionCell->setText(bufferName); } - myFunctionCell->setText(function->name(), function->color()); return; } // The cell is a value cell: diff --git a/apps/graph/values/values_controller.h b/apps/graph/values/values_controller.h index 445ff98a9..c4b90c8b3 100644 --- a/apps/graph/values/values_controller.h +++ b/apps/graph/values/values_controller.h @@ -4,7 +4,7 @@ #include #include "../function_store.h" #include "../evaluate_context.h" -#include "function_title_cell.h" +#include "../function_title_cell.h" #include "value_cell.h" #include "title_cell.h" #include "interval.h" diff --git a/escher/include/escher/palette.h b/escher/include/escher/palette.h index a6561348c..50e18627a 100644 --- a/escher/include/escher/palette.h +++ b/escher/include/escher/palette.h @@ -9,6 +9,7 @@ public: constexpr static KDColor BackgroundColor = KDColor(0xF0F3F5); constexpr static KDColor CellBackgroundColor = KDColor(0xFCFCFC); constexpr static KDColor FocusCellBackgroundColor = KDColor(0xBFD3EB); + constexpr static KDColor k_desactiveTextColor = KDColor(0x646464); }; #endif \ No newline at end of file diff --git a/escher/src/palette.cpp b/escher/src/palette.cpp index f9d630feb..84e87ee6e 100644 --- a/escher/src/palette.cpp +++ b/escher/src/palette.cpp @@ -3,4 +3,5 @@ constexpr KDColor Palette::LineColor; constexpr KDColor Palette::BackgroundColor; constexpr KDColor Palette::CellBackgroundColor; -constexpr KDColor Palette::FocusCellBackgroundColor; \ No newline at end of file +constexpr KDColor Palette::FocusCellBackgroundColor; +constexpr KDColor Palette::k_desactiveTextColor;