From 15de9f93ef09f9e540121d68ee51bcab6ebe19c0 Mon Sep 17 00:00:00 2001 From: Ruben Dashyan Date: Thu, 14 Feb 2019 16:57:44 +0100 Subject: [PATCH] [escher/scroll_view] Implement Decorator::indicatorAtIndex explicitly Instead of using pointer arithmetic --- escher/include/escher/scroll_view.h | 10 ++-------- escher/src/scroll_view.cpp | 28 ++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/escher/include/escher/scroll_view.h b/escher/include/escher/scroll_view.h index cc680eec6..8fd79e7d6 100644 --- a/escher/include/escher/scroll_view.h +++ b/escher/include/escher/scroll_view.h @@ -46,10 +46,7 @@ public: public: BarDecorator(); int numberOfIndicators() const override { return 2; } - View * indicatorAtIndex(int index) override { - assert(0 < index && index <= numberOfIndicators()); - return &m_verticalBar + (index-1); - } + View * indicatorAtIndex(int index) override; KDRect layoutIndicators(KDSize content, KDPoint offset, KDRect frame) override; ScrollViewVerticalBar * verticalBar() { return &m_verticalBar; } ScrollViewHorizontalBar * horizontalBar() { return &m_horizontalBar; } @@ -64,10 +61,7 @@ public: public: ArrowDecorator(); int numberOfIndicators() const override { return 4; } - View * indicatorAtIndex(int index) override { - assert(0 < index && index <= numberOfIndicators()); - return &m_topArrow + (index-1); - } + View * indicatorAtIndex(int index) override; KDRect layoutIndicators(KDSize content, KDPoint offset, KDRect frame) override; void setBackgroundColor(KDColor c) override; private: diff --git a/escher/src/scroll_view.cpp b/escher/src/scroll_view.cpp index 76dcf9bbd..832ac6f4c 100644 --- a/escher/src/scroll_view.cpp +++ b/escher/src/scroll_view.cpp @@ -130,6 +130,16 @@ ScrollView::BarDecorator::BarDecorator() : { } +View * ScrollView::BarDecorator::indicatorAtIndex(int index) { + switch(index) { + case 1: + return &m_verticalBar; + default: + assert(index == 2); + return &m_horizontalBar; + } +} + KDRect ScrollView::BarDecorator::layoutIndicators(KDSize content, KDPoint offset, KDRect frame) { KDCoordinate hBarFrameBreadth = m_barsFrameBreadth * m_horizontalBar.update( content.width(), @@ -162,6 +172,20 @@ ScrollView::ArrowDecorator::ArrowDecorator() : { } +View * ScrollView::ArrowDecorator::indicatorAtIndex(int index) { + switch(index) { + case 1: + return &m_topArrow; + case 2: + return &m_rightArrow; + case 3: + return &m_bottomArrow; + default: + assert(index == 4); + return &m_leftArrow; + } +} + KDRect ScrollView::ArrowDecorator::layoutIndicators(KDSize content, KDPoint offset, KDRect frame) { KDSize arrowSize = KDFont::LargeFont->glyphSize(); KDCoordinate topArrowFrameBreadth = arrowSize.height() * m_topArrow.update(0 < offset.y()); @@ -193,8 +217,8 @@ KDRect ScrollView::ArrowDecorator::layoutIndicators(KDSize content, KDPoint offs } void ScrollView::ArrowDecorator::setBackgroundColor(KDColor c) { - for (int i = 0; i < numberOfIndicators(); i++) { - (&m_topArrow + i)->setBackgroundColor(c); + for (int index = 1; index <= numberOfIndicators(); index++) { + static_cast(indicatorAtIndex(index))->setBackgroundColor(c); } }