i0mmonplot.cpp 7.59 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
#include <cutreader.h>
#endif

I0MMonPlot::I0MMonPlot(QWidget *parent, CumbiaPool *cup, const CuControlsFactoryPool &fpoo)
Giacomo Strangolino's avatar
Giacomo Strangolino committed
21
22
    : QuPlotBase(parent),
      m_symstyles (QList<QwtSymbol::Style> () <<  QwtSymbol::XCross << QwtSymbol::Cross
Giacomo Strangolino's avatar
Giacomo Strangolino committed
23
                                               << QwtSymbol::Rect << QwtSymbol::Triangle << QwtSymbol::Star1 << QwtSymbol::Diamond << QwtSymbol::Ellipse) {
Giacomo Strangolino's avatar
Giacomo Strangolino committed
24
25
    m_ctrl_fpool = fpoo;
    m_cu_poo = cup;
Giacomo Strangolino's avatar
Giacomo Strangolino committed
26
    m_time_mode = m_empty = true;
27
    m_numsamples = 1;
Giacomo Strangolino's avatar
Giacomo Strangolino committed
28
    m_ymin = m_ymax = 0;
Giacomo Strangolino's avatar
Giacomo Strangolino committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    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> &)));
    }
Giacomo Strangolino's avatar
Giacomo Strangolino committed
43
44
45

    setXAxisAutoscaleEnabled(false);
    setYAxisAutoscaleEnabled(false);
Giacomo Strangolino's avatar
Giacomo Strangolino committed
46
47
48
49
50
51
}

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

Giacomo Strangolino's avatar
Giacomo Strangolino committed
52
53
54
55
bool I0MMonPlot::yAutoScale() const {
    return m_ymax == m_ymin;
}

Giacomo Strangolino's avatar
Giacomo Strangolino committed
56
void I0MMonPlot::onDataReady(const QList<CuData> &dl) {
57
    qDebug() << __PRETTY_FUNCTION__ << "data szi" <<  dl.size();
Giacomo Strangolino's avatar
Giacomo Strangolino committed
58
    double x = 0.0;
Giacomo Strangolino's avatar
Giacomo Strangolino committed
59

Giacomo Strangolino's avatar
Giacomo Strangolino committed
60
    if(!m_time_mode && dl.size() == 2) { // correlate
61
62
63
64
65
66
67
68
69
70
71
        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;
Giacomo Strangolino's avatar
Giacomo Strangolino committed
72
            printf("x (%f) averaged across %ld samples\n", x, v.size());
73
74
75
76
77
        }
        else
            x = xd["value"].toDouble();
        // y
        const std::vector<double> &yt = yd["value"].toDoubleVector();
Giacomo Strangolino's avatar
Giacomo Strangolino committed
78
79
80
81
82
        foreach(int i, m_lambdas.keys()) {
            if(i < static_cast<int>(yt.size()) ) {
                appendData(QString("lambda %1").arg(m_lambdas[i]), x, yt[i]);
                yv << yt[i];
                xv << x;
83
84
            }
        }
Giacomo Strangolino's avatar
Giacomo Strangolino committed
85
        m_set_bounds(xv, yv);
86
    }
Giacomo Strangolino's avatar
Giacomo Strangolino committed
87
    else if(m_time_mode) { // trend in time
88

89
        const CuData &da = dl[0];
90
        const std::vector<double> &ystdv = da["value"].toDoubleVector();
Giacomo Strangolino's avatar
Giacomo Strangolino committed
91
        QVector<double> xv {0.0}; // m_set_bounds needs a vector, say 0 and data size
92
#if QT_VERSION < QT_VERSION_CHECK(5, 12, 0)
93
        const QVector<double> yv = QVector<double>::fromStdVector(ystdv);
94
#else
Giacomo Strangolino's avatar
Giacomo Strangolino committed
95
        const QVector<double> yv(ystdv.begin(), ystdv.end());
96
#endif
Giacomo Strangolino's avatar
Giacomo Strangolino committed
97
98
        foreach(int idx, m_lambdas.keys()) {
            const QString curveName = QString("lambda %1").arg(m_lambdas[idx]);
Giacomo Strangolino's avatar
Giacomo Strangolino committed
99
100
            const QuPlotCurve *c = curve(curveName);
            xv << c->data()->size();
Giacomo Strangolino's avatar
Giacomo Strangolino committed
101
102
            if(idx < static_cast<int>(yv.size()) && curve(curveName) != nullptr ) {
                m_set_bounds(xv, yv);
Giacomo Strangolino's avatar
Giacomo Strangolino committed
103
                appendData(curveName, xv[1], yv[idx]);
104
105
            }
        }
Giacomo Strangolino's avatar
Giacomo Strangolino committed
106
107
108
109
110
111
        m_curves_style();
    }
    else {
        printf("\e[1;31mI0MMonPlot::onDataReady: no time mode: data size %d\e[0m\n", dl.size());
        for(int i = 0; i < dl.size(); i++)
            printf("\e[1;31mI0MMonPlot::onDataReady: %s\e[0m\n", datos(dl[i]));
112
    }
Giacomo Strangolino's avatar
Giacomo Strangolino committed
113
    m_empty = false;
Giacomo Strangolino's avatar
Giacomo Strangolino committed
114
115
}

Giacomo Strangolino's avatar
Giacomo Strangolino committed
116
void I0MMonPlot::setSources(const QString &x, const QString &y, const QMap<int, double> lambdamap, int nsamples)
Giacomo Strangolino's avatar
Giacomo Strangolino committed
117
{
Giacomo Strangolino's avatar
Giacomo Strangolino committed
118
    qDebug() << __PRETTY_FUNCTION__ << x << y << lambdamap;
Giacomo Strangolino's avatar
Giacomo Strangolino committed
119
    QuPlotBase::clearPlot();
120
    m_multiread_plu_i->unsetSources();
Giacomo Strangolino's avatar
Giacomo Strangolino committed
121
122
    m_curvesymap.clear();
    m_empty = true;
123
    m_numsamples = nsamples;
Giacomo Strangolino's avatar
Giacomo Strangolino committed
124
125
    m_lambdas = lambdamap;
    m_time_mode = (x == "time");
126
    m_reset_scales();
Giacomo Strangolino's avatar
Giacomo Strangolino committed
127
128
129
130
131
132
    foreach(double l, lambdamap.values()) {
        QString cuna = QString("lambda %1").arg(l);
        QuPlotCurve *crv = curve(cuna);
        if(!crv) {
            crv = new QuPlotCurve(cuna);
            addCurve(cuna, crv);
133
        }
Giacomo Strangolino's avatar
Giacomo Strangolino committed
134
135
136
        m_curvesymap[cuna] = m_symstyles[m_curvesymap.size() % m_symstyles.size()];
    }
    if(m_time_mode) {
137
138
139
140
141
        if(m_multiread_plu_i)
            m_multiread_plu_i->insertSource(y, 0);
    }
    else {
        if(m_multiread_plu_i ) {
142
143
144
            QString xsrc;
            x.contains(QRegularExpression("\\(\\d,%\\d\\)")) ? xsrc = QString(x).arg(nsamples) : xsrc = x;
            m_multiread_plu_i->insertSource(xsrc, 0);
145
146
147
148
            m_multiread_plu_i->insertSource(y, 1);
        }
    }
}
Giacomo Strangolino's avatar
Giacomo Strangolino committed
149

Giacomo Strangolino's avatar
Giacomo Strangolino committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
void I0MMonPlot::setYBounds(double ymi, double yma) {
    if(ymi != yma && ymi < yma)  {
        m_ymin = ymi;
        m_ymax = yma;
        setYLowerBound(ymi);
        setYUpperBound(yma);
    }
}

void I0MMonPlot::setYBoundsAuto(bool a) {
    if(a) {
        m_ymin = m_ymax = 0;
        m_empty = true;
    }
}

Giacomo Strangolino's avatar
Giacomo Strangolino committed
166
void I0MMonPlot::m_set_bounds(const QVector<double> &xv, const QVector<double> &yv)
167
{
Giacomo Strangolino's avatar
Giacomo Strangolino committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
    if(m_ymin == m_ymax) { // auto scale Y
        double ylb, yub;
        const auto [ym, yM] = std::minmax_element(begin(yv), end(yv));
                ylb = m_empty ? (*ym) : yLowerBound();
                yub = m_empty ? (*yM) : yUpperBound();
                // y auto scale?
                if(*yM == *ym) {
            if(ylb >= *ym)
                ylb = (*ym - 0.5);
            if(yub <= *yM)
                yub = (*yM + 0.5);
        } else {
            if(ylb >= *ym)
                ylb = (*ym - 0.15 * (*yM - *ym));
            if(yub <= *yM)
                yub = (*yM + 0.15 * (*yM - *ym));
        }
185

Giacomo Strangolino's avatar
Giacomo Strangolino committed
186
187
188
189
190
191
192
193
194
195
        if(ylb != yLowerBound())
            setYLowerBound(ylb);
        if(yub != yUpperBound())
            setYUpperBound(yub);
    }

    const auto [xm, xM] = std::minmax_element(begin(xv), end(xv));
    double xlb, xub;
    xlb = m_empty ? (*xm) : xLowerBound();
    xub = m_empty ? (*xM) : xUpperBound();
196

Giacomo Strangolino's avatar
Giacomo Strangolino committed
197
    if(*xm == *xM) {
198
199
200
201
202
        if(xlb >= *xm)
            xlb = (*xm - 0.5);
        if(xub <= *xM)
            xub = (*xM + 0.5);
    } else {
203
        if(xlb >= *xm)
Giacomo Strangolino's avatar
Giacomo Strangolino committed
204
            xlb = (*xm - 0.15 * (*xM - *xm));
205
        if(xub <= *xM)
Giacomo Strangolino's avatar
Giacomo Strangolino committed
206
            xub = (*xM + 0.15 * (*xM - *xm));
207
    }
Giacomo Strangolino's avatar
Giacomo Strangolino committed
208

209
210
211
212
213
214
215
    if(xlb != xLowerBound())
        setXLowerBound(xlb);
    if(xub != xUpperBound())
        setXUpperBound(xub);
}

void I0MMonPlot::m_reset_scales() {
Giacomo Strangolino's avatar
Giacomo Strangolino committed
216
217
218
219
220
221
    setXLowerBound(-0.5);
    setXUpperBound(0.5);
    if(m_ymin == m_ymax) {
        setYLowerBound(-0.5);
        setYUpperBound(0.5);
    }
222
}
Giacomo Strangolino's avatar
Giacomo Strangolino committed
223

Giacomo Strangolino's avatar
Giacomo Strangolino committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
void I0MMonPlot::m_curves_style() {
    foreach(QString cn, m_curvesymap.keys()) {
        QuPlotCurve *c = curve(cn);
        if(c && 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() != m_curvesymap[cn])
            c->setSymbol(new QwtSymbol(m_curvesymap[cn], c->brush(), c->pen(), QSize(6, 6)));
    }
Giacomo Strangolino's avatar
Giacomo Strangolino committed
239
}
Giacomo Strangolino's avatar
Giacomo Strangolino committed
240