aboutsummaryrefslogtreecommitdiff
path: root/barchart.cpp
blob: a4d97914127daba39032168d64e46e6818e96d98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "barchart.h"

#include <algorithm>

#include "geometry.h"
#include "scale.h"

static const QColor OUTLINE_COLOR(0, 0, 0);
static const QColor BAR_COLOR(128, 128, 128);
static const float DEFAULT_OPACITY = 0.8f;

BarChart::BarChart(QQuickItem *parent)
    : QQuickItem(parent)
    , m_shouldUpdateBars(false)
{
    setClip(true);
    setFlag(QQuickItem::ItemHasContents);
    // setAcceptedMouseButtons(Qt::LeftButton);
    setAcceptHoverEvents(true);
}

BarChart::~BarChart()
{
}

void BarChart::setValues(const arma::vec &values)
{
    m_values = values;
    m_shouldUpdateBars = true;
    emit valuesChanged(values);
}

QSGNode *BarChart::newBarNode() const
{
    // A bar node is:
    // opacityNode [outlineGeomNode barGeomNode]

    QSGGeometryNode *outlineGeomNode = new QSGGeometryNode;
    QSGGeometry *outlineGeometry =
        new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 4);
    outlineGeometry->setDrawingMode(GL_LINE_LOOP);
    outlineGeomNode->setGeometry(outlineGeometry);
    outlineGeomNode->setFlag(QSGNode::OwnsGeometry);
    QSGFlatColorMaterial *material = new QSGFlatColorMaterial;
    material->setColor(OUTLINE_COLOR);
    outlineGeomNode->setMaterial(material);
    outlineGeomNode->setFlag(QSGNode::OwnsMaterial);

    QSGGeometryNode *barGeomNode = new QSGGeometryNode;
    QSGGeometry *barGeometry =
        new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 4);
    barGeometry->setDrawingMode(GL_POLYGON);
    barGeomNode->setGeometry(barGeometry);
    barGeomNode->setFlag(QSGNode::OwnsGeometry);
    material = new QSGFlatColorMaterial;
    material->setColor(BAR_COLOR);
    barGeomNode->setMaterial(material);
    barGeomNode->setFlag(QSGNode::OwnsMaterial);

    QSGOpacityNode *opacityNode = new QSGOpacityNode;
    opacityNode->setOpacity(DEFAULT_OPACITY);
    opacityNode->appendChildNode(barGeomNode);
    opacityNode->appendChildNode(outlineGeomNode);

    return opacityNode;
}

void BarChart::updateBarNodeGeom(QSGNode *barNode, float x, float barWidth, float barHeight)
{
    QSGGeometryNode *outlineGeomNode =
        static_cast<QSGGeometryNode *>(barNode->firstChild());
    QSGGeometryNode *barGeomNode =
        static_cast<QSGGeometryNode *>(barNode->firstChild()->nextSibling());

    float y = height() - barHeight;
    updateRectGeometry(outlineGeomNode->geometry(), x, y, barWidth, barHeight);
    updateRectGeometry(barGeomNode->geometry(), x, y, barWidth, barHeight);
}

void BarChart::updateBars(QSGNode *root)
{
    QSGNode *node = root->firstChild();
    float x = 0;
    float barWidth = width() / m_values.n_elem;
    LinearScale<float> heightScale(m_values.min(), m_values.max(), 0, height());

    for (auto it = m_values.cbegin(); it != m_values.cend(); it++) {
        updateBarNodeGeom(node, x, barWidth, heightScale((float) *it));
        x += barWidth;
        node = node->nextSibling();
    }
}

QSGNode *BarChart::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
{
    QSGNode *root = oldNode ? oldNode : new QSGNode;
    if (m_shouldUpdateBars) {
        int numValues = (int) m_values.n_elem;
        // First, make sure we have the same number of values & bars
        while (numValues > root->childCount()) {
            QSGNode *barNode = newBarNode();
            root->appendChildNode(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
            root->removeChildNode(root->firstChild());
        }

        // Then, update the geometry of bars to reflect the values
        updateBars(root);
        m_shouldUpdateBars = false;
    }

    return root;
}

void BarChart::hoverMoveEvent(QHoverEvent *event)
{
    // TODO
}

void BarChart::mousePressEvent(QMouseEvent *event)
{
    // TODO
}