172 lines
6.2 KiB
C++
172 lines
6.2 KiB
C++
#include "multichartview.h"
|
|
#include "ui_multichartview.h"
|
|
|
|
using namespace QtCharts;
|
|
|
|
MultiChartView::MultiChartView(DataQueue *queue, ConversionQueue *cqueue, QWidget *parent) :
|
|
QWidget(parent), dataQueue(queue), conversionQueue(cqueue),
|
|
ui(new Ui::MultiChartView)
|
|
{
|
|
ui->setupUi(this);
|
|
xRangeMax = 0.0;
|
|
xRangeMin = 0.0;
|
|
//connect(dataQueue, SIGNAL(resultReady(QString)), this, SLOT(dataReady(QString)));
|
|
connect(dataQueue, SIGNAL(dataReady(QString, double, double)), this, SLOT(dataConverted(QString, double, double)));
|
|
//connect(&dataQueue, SIGNAL(finished()), &dataQueue, SLOT(deleteLater()));
|
|
//connect(conversionQueue, SIGNAL(dataConverted(QString,double,double)), this, SLOT(dataConverted(QString,double,double)));
|
|
labelFont = QFont("Sans", 10);
|
|
tickLabelFont = QFont("Sans", 7);
|
|
setContentsMargins(QMargins(0,0,0,0));
|
|
}
|
|
|
|
MultiChartView::~MultiChartView()
|
|
{
|
|
std::map<std::string, Chart*>::iterator cit;
|
|
for (cit = chartMap.begin(); cit != chartMap.end(); cit++) {
|
|
delete cit->second;
|
|
}
|
|
std::map<std::string, ChartView*>::iterator vit;
|
|
for (vit = viewMap.begin(); vit != viewMap.end(); vit++) {
|
|
delete vit->second;
|
|
}
|
|
std::map<std::string, DataBuffer*>::iterator ait;
|
|
for (ait = data_buffer.begin(); ait != data_buffer.end(); ait++) {
|
|
delete ait->second;
|
|
}
|
|
delete ui;
|
|
}
|
|
|
|
|
|
void MultiChartView::addArray(const nix::DataArray &array) {
|
|
while (dataQueue->isRunning()){
|
|
// uncool!!!
|
|
}
|
|
this->arrayMap.insert(std::pair<std::string, nix::DataArray>(array.id(), array));
|
|
ArrayInfo ai(array);
|
|
this->infoMap.insert(std::pair<std::string, ArrayInfo>(ai.arrayid, ai));
|
|
|
|
DataBuffer *buff = new DataBuffer(array);
|
|
this->data_buffer.insert(std::pair<std::string, DataBuffer*>(ai.arrayid, buff));
|
|
Chart* chart = createChart(ai);
|
|
ChartView *view = createChartView(chart, ai);
|
|
|
|
double absMin = 0.0, absMax = 0.0;
|
|
std::map<std::string, ArrayInfo*>::iterator it;
|
|
for (it = infoMap.begin(); it != infoMap.end(); it++) {
|
|
if (it->second->xoffset < absMin)
|
|
absMin = it->second->xoffset;
|
|
if (it->second->xmax > absMax)
|
|
absMax = it->second->xmax;
|
|
}
|
|
|
|
ui->scrollBar->setMinimum(0);
|
|
ui->scrollBar->setMaximum(100000);
|
|
ui->scrollBar->setPageStep(100);
|
|
ui->vBox->addWidget(view);
|
|
loadData(ai);
|
|
}
|
|
|
|
Chart* MultiChartView::createChart(const ArrayInfo &info) {
|
|
Chart *chart = new Chart();
|
|
chart->setToolTip(QString::fromStdString(info.name));
|
|
chart->legend()->hide();
|
|
chart->createDefaultAxes();
|
|
chart->entityId = info.arrayid;
|
|
|
|
QValueAxis *axisY = new QValueAxis;
|
|
axisY->setTickCount(10);
|
|
axisY->applyNiceNumbers();
|
|
axisY->setTitleText(QString::fromStdString(info.ylabel));
|
|
axisY->setTitleFont(labelFont);
|
|
axisY->setLabelsFont(tickLabelFont);
|
|
chart->setAxisY(axisY);
|
|
|
|
QValueAxis *axisX = new QValueAxis;
|
|
axisX->setRange(info.xoffset, info.xmax);
|
|
axisX->setTickCount(10);
|
|
axisX->setLabelFormat("%.4f");
|
|
axisX->setTitleFont(labelFont);
|
|
axisX->setTitleText(QString::fromStdString(info.xlabel));
|
|
axisX->setLabelsFont(tickLabelFont);
|
|
axisX->applyNiceNumbers();
|
|
chart->setAxisX(axisX);
|
|
|
|
connect(axisX, SIGNAL(rangeChanged(qreal,qreal)), chart, SLOT(XRangeChanged(qreal,qreal)));
|
|
connect(chart, SIGNAL(newDataRequest(std::string, qreal, qreal)), this, SLOT(newDataRequest(std::string, qreal, qreal)));
|
|
this->chartMap.insert(std::pair<std::string, Chart*>(info.arrayid, chart));
|
|
return chart;
|
|
}
|
|
|
|
ChartView* MultiChartView::createChartView(Chart *chart, const ArrayInfo &info) {
|
|
ChartView *view = new ChartView(chart);
|
|
view->setLineWidth(1);
|
|
view->setRenderHint(QPainter::Antialiasing);
|
|
|
|
this->viewMap.insert(std::pair<std::string, ChartView*>(info.arrayid, view));
|
|
return view;
|
|
}
|
|
|
|
void MultiChartView::loadData(const ArrayInfo &info) {
|
|
if (arrayMap.at(info.arrayid)) {
|
|
std::string vId;
|
|
dataQueue->requestData(arrayMap.at(info.arrayid), data_buffer[info.arrayid], info, 0.0, 1.0, vId);
|
|
}
|
|
}
|
|
|
|
|
|
void MultiChartView::newDataRequest(const std::string &entity_id, qreal min, qreal max) {
|
|
/*
|
|
nix::NDSize offset(1, data_buffer[entity_id]->getBuffer()->size());
|
|
nix::NDSize count(1, 100000);
|
|
std::string request_id;
|
|
std::cerr << offset << count;
|
|
*/
|
|
// dataQueue->requestData(arrayMap[entity_id], count, offset, request_id);
|
|
std::string request_id;
|
|
dataQueue->requestData(arrayMap[entity_id], data_buffer[entity_id], *infoMap[entity_id], min, max, request_id);
|
|
}
|
|
|
|
|
|
void MultiChartView::dataReady(QString requestId){
|
|
std::cerr << "MCV::dataReady: " << requestId.toStdString() << std::endl;
|
|
// need to make sure, that only one task does the conversion on the same buffer at the same time.
|
|
DataResult1D data = dataQueue->getData(requestId);
|
|
if (data.id.empty()) {
|
|
return;
|
|
}
|
|
//conversionQueue->requestConversion(data_buffer[data.entityId]->getBuffer(), data);
|
|
}
|
|
|
|
void MultiChartView::dataConverted(QString entityId, double ymin, double ymax) {
|
|
Chart *chart = chartMap[entityId.toStdString()];
|
|
QLineSeries *series;
|
|
bool newSeries = false;
|
|
if (chart->series().size() == 0) {
|
|
series = new QLineSeries();
|
|
series->replace(*data_buffer[entityId.toStdString()]->getBuffer());
|
|
series->setUseOpenGL(true);
|
|
chart->addSeries(series);
|
|
QPen pen = series->pen();
|
|
pen.setWidth(1);
|
|
series->setPen(pen);
|
|
newSeries = true;
|
|
} else {
|
|
series = static_cast<QLineSeries*>(chart->series().at(0));
|
|
series->replace(*data_buffer[entityId.toStdString()]->getBuffer());
|
|
}
|
|
|
|
if (chart->axisX()->type() == QAbstractAxis::AxisTypeValue) {
|
|
QValueAxis *axisX = static_cast<QValueAxis*>(chart->axisX());
|
|
axisX->setRange(series->at(0).x(), series->at(series->count()-1).x());
|
|
if (newSeries)
|
|
series->attachAxis(axisX);
|
|
}
|
|
if (chart->axisY()->type() == QAbstractAxis::AxisTypeValue) {
|
|
QValueAxis *axisY = static_cast<QValueAxis*>(chart->axisY());
|
|
double range = ymax - ymin;
|
|
axisY->setRange(ymin - 0.2 * range, ymax + 0.2 * range);
|
|
if (newSeries)
|
|
series->attachAxis(axisY);
|
|
}
|
|
}
|