From 0beceede5f19d50f859e2d50f4fe8ef14ed84944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Mon, 14 Oct 2019 09:46:21 +0200 Subject: [PATCH] [apps/shared][apps/calculation] Create CurveView::drawLabel and use it in complex_graph_view --- .../additional_outputs/complex_graph_view.cpp | 4 ++ apps/shared/curve_view.cpp | 43 +++++++++++++++++++ apps/shared/curve_view.h | 1 + 3 files changed, 48 insertions(+) diff --git a/apps/calculation/additional_outputs/complex_graph_view.cpp b/apps/calculation/additional_outputs/complex_graph_view.cpp index 1b62461bd..26de82ed4 100644 --- a/apps/calculation/additional_outputs/complex_graph_view.cpp +++ b/apps/calculation/additional_outputs/complex_graph_view.cpp @@ -14,6 +14,10 @@ void ComplexGraphView::drawRect(KDContext * ctx, KDRect rect) const { ctx->fillRect(rect, KDColorWhite); drawAxes(ctx, rect); drawDot(ctx, rect, m_complex->x(), m_complex->y(), KDColorBlack); + drawLabel(ctx, rect, Axis::Horizontal, m_complex->x()); + drawLabel(ctx, rect, Axis::Vertical, m_complex->y()); + + } } diff --git a/apps/shared/curve_view.cpp b/apps/shared/curve_view.cpp index ed7cacfe1..ceffbc973 100644 --- a/apps/shared/curve_view.cpp +++ b/apps/shared/curve_view.cpp @@ -230,6 +230,49 @@ void CurveView::simpleDrawBothAxesLabels(KDContext * ctx, KDRect rect) const { drawLabels(ctx, rect, Axis::Horizontal, true); } +//TODO: factorize +void CurveView::drawLabel(KDContext * ctx, KDRect rect, Axis axis, float grad) const { + Axis otherAxis = (axis == Axis::Horizontal) ? Axis::Vertical : Axis::Horizontal; + float axisCoordinate = std::round(floatToPixel(otherAxis, 0.0f)); + KDCoordinate labelPosition = std::round(floatToPixel(axis, grad)); + KDRect graduation = axis == Axis::Horizontal ? + KDRect( + labelPosition, + axisCoordinate -(k_labelGraduationLength-2)/2, + 1, + k_labelGraduationLength) : + KDRect( + axisCoordinate-(k_labelGraduationLength-2)/2, + labelPosition, + k_labelGraduationLength, + 1); + ctx->fillRect(graduation, KDColorBlack); + + char labelBuffer[k_labelBufferMaxSize]; + PrintFloat::ConvertFloatToText( + grad, + labelBuffer, + k_labelBufferMaxSize, + k_labelBufferMaxGlyphLength, + k_numberSignificantDigits, + Preferences::PrintFloatMode::Decimal); + + float xPosition = 0.0f; + float yPosition = 0.0f; + KDSize textSize = k_font->stringSize(labelBuffer); + if (axis == Axis::Horizontal) { + xPosition = labelPosition - textSize.width()/2; + yPosition = axisCoordinate + k_labelMargin; + } else { + yPosition = labelPosition - textSize.height()/2; + xPosition = axisCoordinate - k_labelMargin - textSize.width(); + } + KDPoint origin = KDPoint(xPosition, yPosition); + if (rect.intersects(KDRect(origin, textSize))) { + ctx->drawString(labelBuffer, origin, k_font, KDColorBlack, KDColorWhite); + } +} + void CurveView::drawLabels(KDContext * ctx, KDRect rect, Axis axis, bool shiftOrigin, bool graduationOnly, bool fixCoordinate, KDCoordinate fixedCoordinate, KDColor backgroundColor) const { int numberLabels = numberOfLabels(axis); if (numberLabels <= 1) { diff --git a/apps/shared/curve_view.h b/apps/shared/curve_view.h index c33f9badf..eea6cf319 100644 --- a/apps/shared/curve_view.h +++ b/apps/shared/curve_view.h @@ -72,6 +72,7 @@ protected: bool fillBar, KDColor defaultColor, KDColor highlightColor, float highlightLowerBound = INFINITY, float highlightUpperBound = -INFINITY) const; void computeLabels(Axis axis); void simpleDrawBothAxesLabels(KDContext * ctx, KDRect rect) const; + void drawLabel(KDContext * ctx, KDRect rect, Axis axis, float grad) const; void drawLabels(KDContext * ctx, KDRect rect, Axis axis, bool shiftOrigin, bool graduationOnly = false, bool fixCoordinate = false, KDCoordinate fixedCoordinate = 0, KDColor backgroundColor = KDColorWhite) const; View * m_bannerView; CurveViewCursor * m_curveViewCursor;