From 3aad29019e88e5255ce2e341fe2d711a88eb134b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Fri, 21 Oct 2016 13:40:15 +0100 Subject: [PATCH] [apps/calcul] create a cell to represent history line Change-Id: Ib24189e510c10a5541b4ec83ce4dc286fba40706 --- apps/calcul/Makefile | 1 + apps/calcul/history_view_cell.cpp | 59 +++++++++++++++++++++++++++++++ apps/calcul/history_view_cell.h | 28 +++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 apps/calcul/history_view_cell.cpp create mode 100644 apps/calcul/history_view_cell.h diff --git a/apps/calcul/Makefile b/apps/calcul/Makefile index d214874fc..0abd3a5f3 100644 --- a/apps/calcul/Makefile +++ b/apps/calcul/Makefile @@ -2,6 +2,7 @@ app_objs += $(addprefix apps/calcul/,\ app.o\ calcul_controller.o\ calcul.o\ + history_view_cell.o\ ) inline_images += apps/calcul/calcul_icon.png diff --git a/apps/calcul/history_view_cell.cpp b/apps/calcul/history_view_cell.cpp new file mode 100644 index 000000000..9b64f7c8e --- /dev/null +++ b/apps/calcul/history_view_cell.cpp @@ -0,0 +1,59 @@ +#include "history_view_cell.h" +#include +#include + +namespace Calcul { + +HistoryViewCell::HistoryViewCell() : + m_calcul(nullptr), + m_highlighted(false), + m_result(BufferTextView(1.0f, 0.5f)) +{ +} + +int HistoryViewCell::numberOfSubviews() const { + return 1; +} + +View * HistoryViewCell::subviewAtIndex(int index) { + assert(index == 0); + return &m_result; +} + +void HistoryViewCell::layoutSubviews() { + KDCoordinate width = bounds().width(); + KDCoordinate height = bounds().height(); + // Position the result + KDSize prettyPrintSize = KDSize(0,0); + if (m_calcul && m_calcul->layout() != nullptr) { + prettyPrintSize = m_calcul->layout()->size(); + } + KDRect resultFrame(prettyPrintSize.width(), 0, width - prettyPrintSize.width(), height); + m_result.setFrame(resultFrame); +} + +void HistoryViewCell::setCalcul(Calcul * calcul) { + m_calcul = calcul; + char buffer[7]; + m_calcul->evaluation()->convertFloatToText(buffer, 14, 7); + m_result.setText(buffer); +} + +void HistoryViewCell::setHighlighted(bool highlight) { + m_highlighted = highlight; + KDColor backgroundColor = m_highlighted ? Palette::FocusCellBackgroundColor : Palette::CellBackgroundColor; + m_result.setBackgroundColor(backgroundColor); + markRectAsDirty(bounds()); +} + +void HistoryViewCell::drawRect(KDContext * ctx, KDRect rect) const { + // Select background color + KDColor backgroundColor = m_highlighted ? Palette::FocusCellBackgroundColor : Palette::CellBackgroundColor; + ctx->fillRect(rect, backgroundColor); + // Draw the pretty print + if (m_calcul && m_calcul->layout() != nullptr) { + m_calcul->layout()->draw(ctx, KDPointZero, KDColorBlack, backgroundColor); + } +} + +} diff --git a/apps/calcul/history_view_cell.h b/apps/calcul/history_view_cell.h new file mode 100644 index 000000000..7864d9c66 --- /dev/null +++ b/apps/calcul/history_view_cell.h @@ -0,0 +1,28 @@ +#ifndef CALCUL_HISTORY_VIEW_CELL_H +#define CALCUL_HISTORY_VIEW_CELL_H + +#include +#include "calcul.h" + +namespace Calcul { + +class HistoryViewCell : public View { +public: + HistoryViewCell(); + BufferTextView * result(); + void setCalcul(Calcul * calcul); + 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: + Calcul * m_calcul; + bool m_highlighted; + BufferTextView m_result; +}; + +} + +#endif