[apps/calculation] ExpressionWithEqualSignView inherits from

ExpressionView instead of composing

This is useful for future change on
ScrollableExactApproximateExpressionView
This commit is contained in:
Émilie Feral
2020-01-20 17:52:27 +01:00
committed by Léa Saviot
parent 88ab0e686c
commit 3de0ee7f98
4 changed files with 25 additions and 23 deletions

View File

@@ -3,24 +3,30 @@
namespace Calculation {
KDSize ExpressionWithEqualSignView::minimalSizeForOptimalDisplay() const {
KDSize expressionSize = m_expressionView.minimalSizeForOptimalDisplay();
KDSize expressionSize = ExpressionView::minimalSizeForOptimalDisplay();
KDSize equalSize = m_equalSign.minimalSizeForOptimalDisplay();
return KDSize(expressionSize.width() + equalSize.width() + Metric::CommonLargeMargin, expressionSize.height());
}
View * ExpressionWithEqualSignView::subviewAtIndex(int index) {
if (index == 0) {
return &m_expressionView;
void ExpressionWithEqualSignView::drawRect(KDContext * ctx, KDRect rect) const {
if (m_layout.isUninitialized()) {
return;
}
assert(index == 1);
// Do not color the whole background to avoid coloring behind the equal symbol
KDSize expressionSize = ExpressionView::minimalSizeForOptimalDisplay();
ctx->fillRect(KDRect(0, 0, expressionSize), m_backgroundColor);
m_layout.draw(ctx, drawingOrigin(), m_textColor, m_backgroundColor, m_selectionStart, m_selectionEnd, Palette::Select);
}
View * ExpressionWithEqualSignView::subviewAtIndex(int index) {
assert(index == 0);
return &m_equalSign;
}
void ExpressionWithEqualSignView::layoutSubviews(bool force) {
KDSize expressionSize = m_expressionView.minimalSizeForOptimalDisplay();
KDSize expressionSize = ExpressionView::minimalSizeForOptimalDisplay();
KDSize equalSize = m_equalSign.minimalSizeForOptimalDisplay();
KDCoordinate expressionBaseline = m_expressionView.layout().baseline();
m_expressionView.setFrame(KDRect(0, 0, expressionSize), force);
KDCoordinate expressionBaseline = layout().baseline();
m_equalSign.setFrame(KDRect(expressionSize.width() + Metric::CommonLargeMargin, expressionBaseline - equalSize.height()/2, equalSize), force);
}

View File

@@ -7,22 +7,17 @@
namespace Calculation {
class ExpressionWithEqualSignView : public View {
class ExpressionWithEqualSignView : public ExpressionView {
public:
ExpressionWithEqualSignView() :
m_expressionView(),
m_equalSign(KDFont::LargeFont, I18n::Message::Equal, 0.5f, 0.5f, KDColorBlack)
{}
ExpressionView * expressionView() { return &m_expressionView; }
KDSize minimalSizeForOptimalDisplay() const override;
void setBackgroundColor(KDColor color) { m_expressionView.setBackgroundColor(color); }
Poincare::Layout layout() const { return m_expressionView.layout(); }
bool setLayout(Poincare::Layout layout) { return m_expressionView.setLayout(layout); }
void drawRect(KDContext * ctx, KDRect rect) const override;
private:
View * subviewAtIndex(int index) override;
void layoutSubviews(bool force = false) override;
int numberOfSubviews() const override { return 2; }
ExpressionView m_expressionView;
int numberOfSubviews() const override { return 1; }
MessageTextView m_equalSign;
};

View File

@@ -26,19 +26,20 @@ public:
KDSize minimalSizeForOptimalDisplay() const override;
KDPoint drawingOrigin() const;
KDPoint absoluteDrawingOrigin() const;
private:
protected:
/* Warning: we do not need to delete the previous expression layout when
* deleting object or setting a new expression layout. Indeed, the expression
* layout is always possessed by a controller which only gives a pointer to
* the expression view (without cloning it). The named controller is then
* responsible for freeing the expression layout when required. */
mutable Poincare::Layout m_layout; // TODO find better way to have minimalSizeForOptimalDisplay const
Poincare::Layout * m_selectionStart;
Poincare::Layout * m_selectionEnd;
float m_horizontalAlignment;
float m_verticalAlignment;
KDColor m_textColor;
KDColor m_backgroundColor;
Poincare::Layout * m_selectionStart;
Poincare::Layout * m_selectionEnd;
private:
float m_horizontalAlignment;
float m_verticalAlignment;
KDCoordinate m_horizontalMargin;
};

View File

@@ -8,12 +8,12 @@ static inline KDCoordinate maxCoordinate(KDCoordinate x, KDCoordinate y) { retur
ExpressionView::ExpressionView(float horizontalAlignment, float verticalAlignment,
KDColor textColor, KDColor backgroundColor, Poincare::Layout * selectionStart, Poincare::Layout * selectionEnd ) :
m_layout(),
m_textColor(textColor),
m_backgroundColor(backgroundColor),
m_selectionStart(selectionStart),
m_selectionEnd(selectionEnd),
m_horizontalAlignment(horizontalAlignment),
m_verticalAlignment(verticalAlignment),
m_textColor(textColor),
m_backgroundColor(backgroundColor),
m_horizontalMargin(0)
{
}