aboutsummaryrefslogtreecommitdiff
path: root/scatterplot.h
blob: c9284394f52d8c526a757645a27e8a504ed40125 (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
#ifndef SCATTERPLOT_H
#define SCATTERPLOT_H

#include <vector>

#include <QtQuick>

#include <armadillo>

#include "colorscale.h"
#include "scale.h"

class QuadTree;

class Scatterplot
    : public QQuickItem
{
    Q_OBJECT
public:
    Scatterplot(QQuickItem *parent = 0);

    arma::mat XY() const;
    void setColorScale(const ColorScale &colorScale);
    void setXY(const arma::mat &xy, bool updateView);
    void setColorData(const arma::vec &colorData, bool updateView);
    void setOpacityData(const arma::vec &opacityData, bool updateView);
    void setScale(const LinearScale<float> &sx, const LinearScale<float> &sy, bool updateView);
    void setGlyphSize(float glyphSize, bool updateView);
    void setAutoScale(bool autoScale);
    Q_INVOKABLE bool saveToFile(const QUrl &url);

    Q_INVOKABLE float glyphSize() const { return m_glyphSize; }

    static const int PADDING = 20;

signals:
    void xyChanged(const arma::mat &XY) const;
    void xyInteractivelyChanged(const arma::mat &XY) const;
    void colorDataChanged(const arma::vec &colorData) const;
    void opacityDataChanged(const arma::vec &opacityData) const;
    void selectionChanged(const std::vector<bool> &selection) const;
    void selectionInteractivelyChanged(const std::vector<bool> &selection) const;
    void itemBrushed(int item) const;
    void itemInteractivelyBrushed(int item) const;
    void scaleChanged(const LinearScale<float> &sx, const LinearScale<float> &sy) const;
    void glyphSizeChanged(float glyphSize) const;

public slots:
    void setXY(const arma::mat &xy);
    void setColorData(const arma::vec &colorData);
    void setOpacityData(const arma::vec &opacityData);
    void setSelection(const std::vector<bool> &selection);
    void brushItem(int item);
    void setScale(const LinearScale<float> &sx, const LinearScale<float> &sy);
    Q_INVOKABLE void setGlyphSize(float glyphSize);

protected:
    QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);

    void hoverEnterEvent(QHoverEvent *event);
    void hoverMoveEvent(QHoverEvent *event);
    void hoverLeaveEvent(QHoverEvent *event);

private:
    QSGNode *newSceneGraph();
    QSGNode *newGlyphTree();

    void applyManipulation();
    void updateGlyphs(QSGNode *node);
    void updateBrush(QSGNode *node);

    // Data
    arma::mat m_xy;
    arma::vec m_colorData;
    arma::vec m_opacityData;

    // Visuals
    float m_glyphSize;
    ColorScale m_colorScale;

    void autoScale();
    bool m_autoScale;
    LinearScale<float> m_sx, m_sy;

    // Internal state
    bool interactiveSelection(bool mergeSelection);
    std::vector<bool> m_selection;
    int m_brushedItem;

    enum InteractionState {
        INTERACTION_NONE,
        INTERACTION_SELECTING,
        INTERACTION_SELECTED,
        INTERACTION_BEGIN_MOVING,
        INTERACTION_MOVING
    } m_currentInteractionState;

    QPointF m_dragOriginPos, m_dragCurrentPos;

    bool m_shouldUpdateGeometry, m_shouldUpdateMaterials;

    void updateQuadTree();
    QuadTree *m_quadtree;
};

#endif // SCATTERPLOT_H