aboutsummaryrefslogtreecommitdiff
path: root/barchart.cpp
diff options
context:
space:
mode:
authorSamuel Fadel <samuelfadel@gmail.com>2016-01-20 12:01:45 +0100
committerSamuel Fadel <samuelfadel@gmail.com>2016-01-20 12:01:45 +0100
commitcbc0e75760a4bff62db8d715a366d819635532ba (patch)
tree39e762f6ec4aee18feaecc4285e7715263eecefe /barchart.cpp
parent50c09dd308c027f1b93019e5bdd913e216eff9ec (diff)
BarChart: moved code from updatePaintNode to other helper functions.
Diffstat (limited to 'barchart.cpp')
-rw-r--r--barchart.cpp35
1 files changed, 19 insertions, 16 deletions
diff --git a/barchart.cpp b/barchart.cpp
index 7938c7a..cb794ac 100644
--- a/barchart.cpp
+++ b/barchart.cpp
@@ -2,6 +2,8 @@
#include <algorithm>
+#include <QOpenGLPaintDevice>
+#include <QPainter>
#include <QSGGeometryNode>
#include <QSGFlatColorMaterial>
@@ -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;
}