From 4d94764c36e3918e31c760fdb355ee72249f5a90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Wed, 20 Dec 2017 11:23:58 +0100 Subject: [PATCH] [expression_editor] Controller code cleaning. Change-Id: Ifb31ad712ce9c59e21fc3d670d05f60a15669d60 --- apps/expression_editor/controller.cpp | 73 ++++++++++--------- apps/expression_editor/controller.h | 4 +- .../poincare/expression_layout_cursor.h | 17 +++-- poincare/src/expression_layout_cursor.cpp | 57 +++++++++++---- 4 files changed, 92 insertions(+), 59 deletions(-) diff --git a/apps/expression_editor/controller.cpp b/apps/expression_editor/controller.cpp index 4a4c2f842..7e747e03d 100644 --- a/apps/expression_editor/controller.cpp +++ b/apps/expression_editor/controller.cpp @@ -1,7 +1,4 @@ #include "controller.h" -#include //TODO move from there. -#include //TODO move from there. -#include //TODO move from there. using namespace Poincare; @@ -31,49 +28,53 @@ void Controller::didBecomeFirstResponder() { } bool Controller::handleEvent(Ion::Events::Event event) { - bool returnValue = false; - if ((event == Ion::Events::Left && m_cursor.moveLeft()) - || (event == Ion::Events::Right && m_cursor.moveRight()) - || (event == Ion::Events::Up && m_cursor.moveUp()) - || (event == Ion::Events::Down && m_cursor.moveDown())) - { - returnValue = true; - } else if (event == Ion::Events::Division) { - EmptyVisibleLayout * child1 = new EmptyVisibleLayout(); - EmptyVisibleLayout * child2 = new EmptyVisibleLayout(); + if (privateHandleEvent(event)) { + m_view.cursorPositionChanged(); + return true; + } + return false; +} - FractionLayout * newChild = new FractionLayout(child1, child2, false); - m_cursor.pointedExpressionLayout()->addBrother(&m_cursor, newChild); - m_cursor.setPointedExpressionLayout(newChild); +bool Controller::privateHandleEvent(Ion::Events::Event event) { + if (handleMoveEvent(event)) { + return true; + } + ExpressionLayout * newPointedLayout = handleAddEvent(event); + if (newPointedLayout != nullptr) { + m_cursor.setPointedExpressionLayout(newPointedLayout); m_cursor.setPosition(ExpressionLayoutCursor::Position::Right); m_cursor.setPositionInside(0); - returnValue = true; - m_expressionLayout->invalidAllSizesPositionsAndBaselines(); - m_view.layoutSubviews(); - } else if (event.hasText()) { - insertTextAtCursor(event.text()); - returnValue = true; m_expressionLayout->invalidAllSizesPositionsAndBaselines(); m_view.layoutSubviews(); + return true; } - m_view.cursorPositionChanged(); - return returnValue; + return false; } -void Controller::insertTextAtCursor(const char * text) { - int textLength = strlen(text); - if (textLength <= 0) { - return; +bool Controller::handleMoveEvent(Ion::Events::Event event) { + if (event == Ion::Events::Left) { + return m_cursor.moveLeft(); } - CharLayout * newChild = nullptr; - for (int i = 0; i < textLength; i++) { - newChild = new CharLayout(text[i]); - m_cursor.pointedExpressionLayout()->addBrother(&m_cursor, newChild); + if (event == Ion::Events::Right) { + return m_cursor.moveRight(); } - assert(newChild != nullptr); - m_cursor.setPointedExpressionLayout(newChild); - m_cursor.setPosition(ExpressionLayoutCursor::Position::Right); - m_cursor.setPositionInside(0); + if (event == Ion::Events::Up) { + return m_cursor.moveUp(); + } + if (event == Ion::Events::Down) { + return m_cursor.moveDown(); + } + return false; +} + +ExpressionLayout * Controller::handleAddEvent(Ion::Events::Event event) { + if (event == Ion::Events::Division) { + return m_cursor.addEmptyFractionLayout(); + } + if (event.hasText()) { + return m_cursor.insertText(event.text()); + } + return nullptr; } } diff --git a/apps/expression_editor/controller.h b/apps/expression_editor/controller.h index f95e5513b..e285ff8a8 100644 --- a/apps/expression_editor/controller.h +++ b/apps/expression_editor/controller.h @@ -18,7 +18,9 @@ public: void didBecomeFirstResponder() override; bool handleEvent(Ion::Events::Event event) override; private: - void insertTextAtCursor(const char * text); + bool privateHandleEvent(Ion::Events::Event event); + bool handleMoveEvent(Ion::Events::Event event); + Poincare::ExpressionLayout * handleAddEvent(Ion::Events::Event event); ExpressionEditorView m_view; Poincare::ExpressionLayout * m_expressionLayout; Poincare::ExpressionLayoutCursor m_cursor; diff --git a/poincare/include/poincare/expression_layout_cursor.h b/poincare/include/poincare/expression_layout_cursor.h index 0fe1f689e..88b29d81f 100644 --- a/poincare/include/poincare/expression_layout_cursor.h +++ b/poincare/include/poincare/expression_layout_cursor.h @@ -29,18 +29,23 @@ public: void setPositionInside(int positionInside) { m_positionInside = positionInside; } KDCoordinate cursorHeight() const { return k_cursorHeight; } - /* Move */ - bool moveLeft(); - bool moveRight(); - bool moveUp(); - bool moveDown(); - /* Comparison */ bool positionIsEquivalentTo(ExpressionLayout * expressionLayout, Position position, int positionIndex = 0); /* Position */ KDPoint middleLeftPoint(); KDPoint middleLeftPointOfCursor(ExpressionLayout * expressionLayout, Position position, int positionInside = 0); + + /* Move */ + bool moveLeft(); + bool moveRight(); + bool moveUp(); + bool moveDown(); + + /* Edition */ + ExpressionLayout * addEmptyFractionLayout(); + ExpressionLayout * insertText(const char * text); + private: constexpr static KDCoordinate k_cursorHeight = 18; ExpressionLayout * m_pointedExpressionLayout; diff --git a/poincare/src/expression_layout_cursor.cpp b/poincare/src/expression_layout_cursor.cpp index 5fd6c0887..1fc9935bc 100644 --- a/poincare/src/expression_layout_cursor.cpp +++ b/poincare/src/expression_layout_cursor.cpp @@ -1,25 +1,12 @@ #include #include +#include //TODO move from there. +#include //TODO move from there. +#include //TODO move from there. #include namespace Poincare { -bool ExpressionLayoutCursor::moveLeft() { - return m_pointedExpressionLayout->moveLeft(this); -} - -bool ExpressionLayoutCursor::moveRight() { - return m_pointedExpressionLayout->moveRight(this); -} - -bool ExpressionLayoutCursor::moveUp() { - return m_pointedExpressionLayout->moveUp(this); -} - -bool ExpressionLayoutCursor::moveDown() { - return m_pointedExpressionLayout->moveDown(this); -} - bool ExpressionLayoutCursor::positionIsEquivalentTo(ExpressionLayout * expressionLayout, Position position, int positionIndex) { assert(expressionLayout != nullptr); return middleLeftPoint() == middleLeftPointOfCursor(expressionLayout, position, positionIndex); @@ -42,5 +29,43 @@ KDPoint ExpressionLayoutCursor::middleLeftPointOfCursor(ExpressionLayout * expre return KDPoint(layoutOrigin.x() + positionInside * KDText::charSize().width(), y); } +bool ExpressionLayoutCursor::moveLeft() { + return m_pointedExpressionLayout->moveLeft(this); +} + +bool ExpressionLayoutCursor::moveRight() { + return m_pointedExpressionLayout->moveRight(this); +} + +bool ExpressionLayoutCursor::moveUp() { + return m_pointedExpressionLayout->moveUp(this); +} + +bool ExpressionLayoutCursor::moveDown() { + return m_pointedExpressionLayout->moveDown(this); +} + +ExpressionLayout * ExpressionLayoutCursor::addEmptyFractionLayout() { + EmptyVisibleLayout * child1 = new EmptyVisibleLayout(); + EmptyVisibleLayout * child2 = new EmptyVisibleLayout(); + FractionLayout * newChild = new FractionLayout(child1, child2, false); + pointedExpressionLayout()->addBrother(this, newChild); + return child1; +} + +ExpressionLayout * ExpressionLayoutCursor::insertText(const char * text) { + int textLength = strlen(text); + if (textLength <= 0) { + return nullptr; + } + CharLayout * newChild = nullptr; + for (int i = 0; i < textLength; i++) { + newChild = new CharLayout(text[i]); + pointedExpressionLayout()->addBrother(this, newChild); + } + assert(newChild != nullptr); + return newChild; +} + }