i0mmonplot.cpp 7.12 KB
Newer Older
Giacomo Strangolino's avatar
Giacomo Strangolino committed
1
2
3
4
#include "i0mmonplot.h"
#include <cumbiapool.h>
#include <cucontrolsfactorypool.h>
#include <quplotcurve.h>
5
6
7
#include <qutimescaledraw.h>
#include <qwt_symbol.h>
#include <qwt_date_scale_engine.h>
Giacomo Strangolino's avatar
Giacomo Strangolino committed
8
9
10
11
12
13
14
15

#include <cupluginloader.h>
#include <qumultireaderplugininterface.h>
#include <cumacros.h>

#include <QtDebug>

#ifdef QUMBIA_TANGO_CONTROLS_VERSION
16
#include <QRegularExpression>
Giacomo Strangolino's avatar
Giacomo Strangolino committed
17
18
19
20
21
22
23
#include <cutreader.h>
#endif

I0MMonPlot::I0MMonPlot(QWidget *parent, CumbiaPool *cup, const CuControlsFactoryPool &fpoo)
    : QuPlotBase(parent) {
    m_ctrl_fpool = fpoo;
    m_cu_poo = cup;
24
    m_numsamples = 1;
Giacomo Strangolino's avatar
Giacomo Strangolino committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    CuPluginLoader plo;
    QObject *plugin_qob;
    m_multiread_plu_i = plo.get<QuMultiReaderPluginInterface>("libcumbia-multiread-plugin.so", &plugin_qob);
    if(!m_multiread_plu_i)
        perr("I0MMonPlot: failed to load plugin \"libcumbia-multiread-plugin.so\"");
    else {
        int mode = 0;
#ifdef QUMBIA_TANGO_CONTROLS_VERSION
        mode = CuTReader::Manual;
#endif
        m_multiread_plu_i->init(cup, fpoo, mode); // 0 sync mode
        connect(m_multiread_plu_i->get_qobject(), SIGNAL(onSeqReadComplete(const QList<CuData> &)),
                this, SLOT(onDataReady(const QList<CuData> &)));
    }
}

bool I0MMonPlot::pluginLoaded() const {
    return m_multiread_plu_i != nullptr;
}

void I0MMonPlot::onDataReady(const QList<CuData> &dl) {
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    qDebug() << __PRETTY_FUNCTION__ << "data szi" <<  dl.size();
    double x;
    if(dl.size() == 2) { // correlate
        QVector<double> yv, xv;
        const CuData& xd = dl[0];
        const CuData& yd = dl[1];
        printf("\e[1;35mx %s , y %s\e[0m\n", vtoc2(xd, "value"), vtoc2(yd, "value"));
        if(xd.has("data_format_str", "vector")) {
            // average
            double sum = 0.0;
            const std::vector<double> &v = xd["value"].toDoubleVector();
            for(size_t i = 0; i < v.size(); i++)
                sum += v[i];
            x = sum / m_numsamples;
60
            printf("x (%f) averaged across %ld samples\n", v.size());
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
        }
        else
            x = xd["value"].toDouble();
        // y
        const std::vector<double> &yt = yd["value"].toDoubleVector();
        for(int i = 0; i < m_lambda_idxs.size(); i++) {
            if(m_lambda_idxs[i] < static_cast<int>(yt.size()) ) {
                yv.push_back(yt[m_lambda_idxs[i]]);
                xv.push_back(x);
            }
        }
        QuPlotCurve * c = curve(m_curvenames.first());
        if(c) {
            if(c->style() != QwtPlotCurve::Dots) {
                c->setStyle(QwtPlotCurve::Dots);
                QPen p = c->pen();
                p.setWidthF(2.0f);
                c->setPen(p);
            }
#if CUMBIA_QTCONTROLS_VERSION > 0x010202
            c->setMaxDataSizeForSymbol(0); // disable first points symbol
#endif
            if(!c->symbol() || c->symbol()->style() != QwtSymbol::XCross)
                c->setSymbol(new QwtSymbol(QwtSymbol::XCross, c->brush(), c->pen(), QSize(10, 10)));
            printf("\e[1;32mI0MMonPlot::onDataReady: curve: %s { ", qstoc(c->title().text()));
            for(int i = 0; i < xv.size(); i++)
                printf("(%.2f, %.2f), ", xv[i], yv[i]);
            printf(" }\e[0m\n");
            m_set_bounds(xv, yv, c->dataSize());
            appendData(m_curvenames.first(), xv.data(), yv.data(), xv.size());
        }
    }
    else if(dl.size() == 1) { // trend
        const CuData &da = dl[0];
        const std::vector<double> &yv = da["value"].toDoubleVector();
        CuVariant ts = da["timestamp_ms"];
        ts.getType() == CuVariant::LongInt ? x = static_cast<qint64>(ts.toLongInt()) : x = ts.toDouble();
        printf("\e[0;33mdata %s -- timestamp %f\e[0m\n", datos(da), x);
        for(int i = 0; i < m_lambda_idxs.size(); i++) {
            const QString curveName = QString("lambda %1").arg(m_lambda_idxs[i]);
            if(m_lambda_idxs[i] < static_cast<int>(yv.size()) ) {
                qDebug() << __PRETTY_FUNCTION__ << "APPENDING" << QDateTime::fromMSecsSinceEpoch(x) << yv[m_lambda_idxs[i]];
                appendData(curveName, x, yv[m_lambda_idxs[i]]);
            }
        }
        refresh();
    }
Giacomo Strangolino's avatar
Giacomo Strangolino committed
108
109
}

110
void I0MMonPlot::setSources(const QString &x, const QString &y, const QList<int> &lambda_idxs, int nsamples)
Giacomo Strangolino's avatar
Giacomo Strangolino committed
111
112
113
{
    qDebug() << __PRETTY_FUNCTION__ << x << y << lambda_idxs;
    QuPlotBase::clearPlot();
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
    m_multiread_plu_i->unsetSources();
    m_numsamples = nsamples;
    m_lambda_idxs = lambda_idxs;
    if(x == "time") {
        m_set_x_trend_scale();
        foreach(int l, lambda_idxs) {
            addCurve(QString("lambda %1").arg(l));
            m_curvenames = QStringList() << QString("lambda %1").arg(l);
        }
        if(m_multiread_plu_i)
            m_multiread_plu_i->insertSource(y, 0);
    }
    else {
        m_reset_scales();
        addCurve(y);
        m_curvenames = QStringList() << y;
        if(m_multiread_plu_i ) {
131
132
133
            QString xsrc;
            x.contains(QRegularExpression("\\(\\d,%\\d\\)")) ? xsrc = QString(x).arg(nsamples) : xsrc = x;
            m_multiread_plu_i->insertSource(xsrc, 0);
134
135
136
137
            m_multiread_plu_i->insertSource(y, 1);
        }
    }
}
Giacomo Strangolino's avatar
Giacomo Strangolino committed
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
164
165
166
167
168
169
170
171
void I0MMonPlot::m_set_bounds(const QVector<double> &xv, const QVector<double> &yv, int curve_datasiz)
{
    // scales
    const auto [xm, xM] = std::minmax_element(begin(xv), end(xv));
            const auto [ym, yM] = std::minmax_element(begin(yv), end(yv));

            double xlb, xub, ylb, yub;
            xlb = (curve_datasiz == 0) ? (*xm) : xLowerBound();
            xub = (curve_datasiz == 0) ? (*xM) : xUpperBound();
            ylb = (curve_datasiz == 0) ? (*ym) : yLowerBound();
            yub = (curve_datasiz == 0) ? (*yM) : yUpperBound();

            if(*xm == *xM) {
        if(xlb >= *xm)
            xlb = (*xm - 0.5);
        if(xub <= *xM)
            xub = (*xM + 0.5);
    } else {
        if(xlb > *xm)
            xlb = (*xm - 0.05 * (*xM - *xm));
        if(xub < *xM)
            xub = (*xM + 0.05 * (*xM - *xm));
    }
    if(*yM == *ym) {
        if(ylb >= *ym)
            ylb = (*ym - 0.5);
        if(yub <= *yM)
            yub = (*yM + 0.5);
    } else {
        if(ylb > *ym)
            ylb = (*ym - 0.05 * (*yM - *ym));
        if(yub < *yM)
            yub = (*yM + 0.05 * (*yM - *ym));
Giacomo Strangolino's avatar
Giacomo Strangolino committed
172
    }
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    if(xlb != xLowerBound())
        setXLowerBound(xlb);
    if(xub != xUpperBound())
        setXUpperBound(xub);
    if(ylb != yLowerBound())
        setYLowerBound(ylb);
    if(yub != yUpperBound())
        setYUpperBound(yub);
}

void I0MMonPlot::m_reset_scales() {
    setAxisScaleDraw(QwtPlot::xBottom, new QwtScaleDraw());
    setAxisScaleEngine(QwtPlot::xBottom, new QwtLinearScaleEngine());
    setXAxisAutoscaleEnabled(false);
    setYAxisAutoscaleEnabled(false);
    setXLowerBound(-0.5); setXUpperBound(0.5);
    setYLowerBound(-0.5); setYUpperBound(0.5);
}
Giacomo Strangolino's avatar
Giacomo Strangolino committed
191

192
193
194
195
196
197
198
199
void I0MMonPlot::m_set_x_trend_scale() {
    setXAxisAutoscaleEnabled(true);
    QuTimeScaleDraw *tsd = new QuTimeScaleDraw();
    tsd->setDateEnabled(false);
    setAxisScaleDraw(QwtPlot::xBottom, tsd);
    setAxisLabelRotation(QwtPlot::xBottom, -50.0);
    setAxisScaleEngine(QwtPlot::xBottom, new QwtDateScaleEngine(Qt::LocalTime));
    setAxisLabelAlignment( QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom );
Giacomo Strangolino's avatar
Giacomo Strangolino committed
200
}