From 280fe277caa065c5528bde904ac9db36d8e5f22b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Fri, 30 Dec 2016 12:20:50 +0100 Subject: [PATCH] [apps/regression] Create a banner view class Change-Id: I30ad498b2c63c1a7edb09a05c40dd1f94373ead1 --- apps/regression/Makefile | 1 + apps/regression/banner_view.cpp | 88 +++++++++++++++++++++++++++++++++ apps/regression/banner_view.h | 29 +++++++++++ 3 files changed, 118 insertions(+) create mode 100644 apps/regression/banner_view.cpp create mode 100644 apps/regression/banner_view.h diff --git a/apps/regression/Makefile b/apps/regression/Makefile index ebe72bc81..0ead1f201 100644 --- a/apps/regression/Makefile +++ b/apps/regression/Makefile @@ -1,5 +1,6 @@ app_objs += $(addprefix apps/regression/,\ app.o\ + banner_view.o\ calculation_controller.o\ data.o\ graph_controller.o\ diff --git a/apps/regression/banner_view.cpp b/apps/regression/banner_view.cpp new file mode 100644 index 000000000..a61b99016 --- /dev/null +++ b/apps/regression/banner_view.cpp @@ -0,0 +1,88 @@ +#include "banner_view.h" +#include "../constant.h" +#include +#include + +namespace Regression { + +BannerView::BannerView(Data * data) : + m_regressionTypeView(nullptr, 0.5f, 0.5f), + m_slopeView(0.5f, 0.5f), + m_yInterceptView(0.5f, 0.5f), + m_xView(0.5f, 0.5f), + m_yView(0.5f, 0.5f), + m_data(data) +{ +} + +void BannerView::reload() { + markRectAsDirty(bounds()); + m_regressionTypeView.setText("y = ax+b"); + char buffer[k_maxNumberOfCharacters]; + const char * legend = "a = "; + int legendLength = strlen(legend); + strlcpy(buffer, legend, legendLength+1); + float slope = m_data->slope(); + Float(slope).convertFloatToText(buffer+legendLength, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaInScientificMode); + m_slopeView.setText(buffer); + + legend = "b = "; + legendLength = strlen(legend); + strlcpy(buffer, legend, legendLength+1); + float yIntercept = m_data->yIntercept(); + Float(yIntercept).convertFloatToText(buffer+legendLength, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaInScientificMode); + m_yInterceptView.setText(buffer); + + legend = "x = "; + legendLength = strlen(legend); + strlcpy(buffer, legend, legendLength+1); + float x = m_data->xCursorPosition(); + Float(x).convertFloatToText(buffer+legendLength, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaInScientificMode); + m_xView.setText(buffer); + + legend = "y = "; + legendLength = strlen(legend); + strlcpy(buffer, legend, legendLength+1); + float y = m_data->yValueForXValue(x); + Float(y).convertFloatToText(buffer+legendLength, Constant::FloatBufferSizeInScientificMode, Constant::NumberOfDigitsInMantissaInScientificMode); + m_yView.setText(buffer); +} + +void BannerView::drawRect(KDContext * ctx, KDRect rect) const { + ctx->fillRect(bounds(), KDColorWhite); +} + +int BannerView::numberOfSubviews() const { + return 5; +} + +View * BannerView::subviewAtIndex(int index) { + assert(index >= 0); + switch (index) { + case 0: + return &m_regressionTypeView; + case 1: + return &m_slopeView; + case 2: + return &m_yInterceptView; + case 3: + return &m_xView; + case 4: + return &m_yView; + default: + assert(false); + return nullptr; + } +} + +void BannerView::layoutSubviews() { + KDCoordinate height = bounds().height(); + KDCoordinate width = bounds().width(); + m_regressionTypeView.setFrame(KDRect(0, 0, width/3, height/2)); + m_slopeView.setFrame(KDRect(width/3, 0, width/3, height/2)); + m_yInterceptView.setFrame(KDRect(2*width/3, 0, width/3, height/2)); + m_xView.setFrame(KDRect(0, height/2, width/3, height/2)); + m_yView.setFrame(KDRect(width/3, height/2, width/3, height/2)); +} + +} diff --git a/apps/regression/banner_view.h b/apps/regression/banner_view.h new file mode 100644 index 000000000..61cb725fa --- /dev/null +++ b/apps/regression/banner_view.h @@ -0,0 +1,29 @@ +#ifndef REGRESSION_BANNER_VIEW_H +#define REGRESSION_BANNER_VIEW_H + +#include +#include "data.h" + +namespace Regression { + +class BannerView : public View { +public: + BannerView(Data * data); + void reload(); + void drawRect(KDContext * ctx, KDRect rect) const override; +private: + constexpr static int k_maxNumberOfCharacters = 50; + int numberOfSubviews() const override; + View * subviewAtIndex(int index) override; + void layoutSubviews() override; + PointerTextView m_regressionTypeView; + BufferTextView m_slopeView; + BufferTextView m_yInterceptView; + BufferTextView m_xView; + BufferTextView m_yView; + Data * m_data; +}; + +} + +#endif