aboutsummaryrefslogtreecommitdiff
path: root/main.h
blob: fd7afbf57df84b8640dd0e267b9acc2a5de662e0 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef MAIN_H
#define MAIN_H

#include <QObject>
#include <armadillo>

#include "barchart.h"
#include "colormap.h"
#include "colorscale.h"
#include "continuouscolorscale.h"
#include "projectionobserver.h"
#include "scatterplot.h"
#include "voronoisplat.h"

class Main : public QObject
{
    Q_OBJECT
    Q_ENUMS(ObserverType)
    Q_ENUMS(ColorScaleType)
public:
    static Main *instance() {
        // FIXME: Possibly dangerous
        static Main *m = 0;
        if (m == 0) {
            m = new Main();
        }

        return m;
    }

    Q_INVOKABLE bool saveData() const {
        bool ret = true;
        if (m_cp.n_elem > 0 && m_indicesSavePath.size() > 0) {
            ret = ret && m_cp.save(m_cpSavePath, arma::raw_ascii);
        }
        if (m_cpIndices.n_elem > 0 && m_cpSavePath.size() > 0) {
            ret = ret && m_cpIndices.save(m_indicesSavePath, arma::raw_ascii);
        }

        return ret;
    }

    Q_INVOKABLE bool loadDataset(const std::string &path) { return m_dataset.load(path, arma::raw_ascii); }

    Q_INVOKABLE void setIndicesSavePath(const std::string &path) {
        m_indicesSavePath = path;
    }
    Q_INVOKABLE void setIndicesSavePath(const QString &path) {
        setIndicesSavePath(path.toStdString());
    }
    Q_INVOKABLE void setCPSavePath(const std::string &path) {
        m_cpSavePath = path;
    }
    Q_INVOKABLE void setCPSavePath(const QString &path) {
        setCPSavePath(path.toStdString());
    }

    arma::mat X() const { return m_dataset.cols(0, m_dataset.n_cols - 2); }
    arma::vec labels() const { return m_dataset.col(m_dataset.n_cols - 1); }

    enum ObserverType {
        ObserverCurrent      = ProjectionObserver::OBSERVER_CURRENT,
        ObserverDiffPrevious = ProjectionObserver::OBSERVER_DIFF_PREVIOUS,
        ObserverDiffOriginal = ProjectionObserver::OBSERVER_DIFF_ORIGINAL
    };

    Q_INVOKABLE bool setObserverType(ObserverType observerType) {
        return projectionObserver->setType((int) observerType);
    }

    enum ColorScaleType {
        ColorScaleCategorical,
        ColorScaleContinuous,
        ColorScaleDivergent,
        ColorScaleRainbow
    };

    // No need to be static: this class is a singleton
    ColorScale COLOR_SCALE_CATEGORICAL;
    ColorScale COLOR_SCALE_CONTINUOUS;
    ColorScale COLOR_SCALE_DIVERGENT;
    ColorScale COLOR_SCALE_RAINBOW;

    Q_INVOKABLE void setCPPlotColorScale(ColorScaleType colorScaleType) {
        cpPlot->setColorScale(getColorScale(colorScaleType));
    }

    Q_INVOKABLE void setRPPlotColorScale(ColorScaleType colorScaleType) {
        rpPlot->setColorScale(getColorScale(colorScaleType));
    }

    Q_INVOKABLE void setColormapColorScale(ColorScaleType colorScaleType) {
        colormap->setColorScale(getColorScale(colorScaleType));
    }

    Q_INVOKABLE void setBarChartColorScale(ColorScaleType colorScaleType) {
        barChart->setColorScale(getColorScale(colorScaleType));
    }

    Q_INVOKABLE void setSplatColorScale(ColorScaleType colorScaleType) {
        splat->setColorScale(getColorScale(colorScaleType));
    }

    // Pointer to visual components whose values are set in the main() function
    // after components are instantiated by the QtQuick engine
    BarChart *barChart;
    Colormap *colormap;
    Scatterplot *cpPlot, *rpPlot;
    VoronoiSplat *splat;

    ProjectionObserver *projectionObserver;

public slots:
    void setCPIndices(const arma::uvec &indices) { m_cpIndices = indices; }
    void setCP(const arma::mat &cp) {
        if (cp.n_cols != 2
            || cp.n_rows != m_cpIndices.n_elem) {
            return;
        }

        m_cp = cp;
    }

private:
    Main(QObject *parent = 0)
        : QObject(parent)
        , COLOR_SCALE_CATEGORICAL{
            QColor("#1f77b4"), QColor("#ff7f0e"), QColor("#2ca02c"),
            QColor("#d62728"), QColor("#9467bd"), QColor("#8c564b"),
            QColor("#e377c2"), QColor("#17becf"), QColor("#7f7f7f"),
          }
        , COLOR_SCALE_CONTINUOUS{ContinuousColorScale::builtin(ContinuousColorScale::HEATED_OBJECTS)}
        , COLOR_SCALE_DIVERGENT{ContinuousColorScale::builtin(ContinuousColorScale::RED_GRAY_BLUE)}
        , COLOR_SCALE_RAINBOW{ContinuousColorScale::builtin(ContinuousColorScale::RAINBOW)}
        , barChart(0)
        , cpPlot(0)
        , rpPlot(0)
        , splat(0)
    {
    }
    ~Main() {}

    ColorScale &getColorScale(ColorScaleType colorScaleType) {
        switch (colorScaleType) {
        case ColorScaleCategorical:
            return COLOR_SCALE_CATEGORICAL;
        case ColorScaleContinuous:
            return COLOR_SCALE_CONTINUOUS;
        case ColorScaleDivergent:
            return COLOR_SCALE_DIVERGENT;
        case ColorScaleRainbow:
            // fall-through
        default:
            return COLOR_SCALE_RAINBOW;
        }
    }

    arma::mat m_dataset, m_cp;
    arma::uvec m_cpIndices;
    std::string m_indicesSavePath, m_cpSavePath;
};

#endif // MAIN_H