From cbc0e75760a4bff62db8d715a366d819635532ba Mon Sep 17 00:00:00 2001 From: Samuel Fadel Date: Wed, 20 Jan 2016 12:01:45 +0100 Subject: BarChart: moved code from updatePaintNode to other helper functions. --- barchart.cpp | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'barchart.cpp') diff --git a/barchart.cpp b/barchart.cpp index 7938c7a..cb794ac 100644 --- a/barchart.cpp +++ b/barchart.cpp @@ -2,6 +2,8 @@ #include +#include +#include #include #include @@ -160,11 +162,24 @@ void BarChart::updateBarNodeColor(QSGNode *barNode, const QColor &color) barGeomNode->markDirty(QSGNode::DirtyMaterial); } -void BarChart::updateBars(QSGNode *root) +void BarChart::updateBars(QSGNode *node) { - float barWidth = 1.0f / m_values.n_elem; + int numValues = (int) m_values.n_elem; + float barWidth = 1.0f / numValues; - QSGNode *node = root->firstChild(); + // First, make sure we have the same number of values & bars + while (numValues > node->childCount()) { + QSGNode *barNode = newBarNode(); + node->prependChildNode(barNode); + } + while (numValues < root->childCount()) { + // NOTE: as stated in docs, QSGNode's children are stored in a + // linked list. Hence, this operation should be as fast as expected + node->removeChildNode(node->firstChild()); + } + + // Then, update each bar to reflect the values + node = node->firstChild(); float x = 0; for (auto it = m_originalIndices.cbegin(); it != m_originalIndices.cend(); it++) { updateBarNodeGeom(node, x, barWidth, m_scale(m_values[*it])); @@ -193,25 +208,13 @@ QSGNode *BarChart::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *) QSGNode *node = root->firstChild(); if (m_shouldUpdateBars) { - int numValues = (int) m_values.n_elem; - // First, make sure we have the same number of values & bars - while (numValues > node->childCount()) { - QSGNode *barNode = newBarNode(); - node->prependChildNode(barNode); - } - while (numValues < root->childCount()) { - // NOTE: as stated in docs, QSGNode's children are stored in a - // linked list. Hence, this operation should be as fast as expected - node->removeChildNode(node->firstChild()); - } - - // Then, update the bars to reflect the values updateBars(node); m_shouldUpdateBars = false; } node = node->nextSibling(); updateHoverHints(node); + node = node->nextSibling(); return root; } -- cgit v1.2.3