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
|
#include "PlotModel.hpp"
#include "ExpressionRangeData.hpp"
#include <QtCharts/QChart>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QtCore/QDebug>
#include <QtCore/QEvent>
#include <QtCore/QJsonValue>
#include <QtGui/QDoubleValidator>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QFormLayout>
#include <QtWidgets/QGraphicsScene>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QWidget>
#include <cmath>
using namespace QtCharts;
PlotModel::PlotModel()
{
QChart *chart = new QChart();
_chartView = new QChartView(chart);
_chartView->setRenderHint(QPainter::Antialiasing);
}
void PlotModel::onFunctionIndexChanged(int index)
{
processData();
}
QJsonObject PlotModel::save() const
{
QJsonObject modelJson = NodeDataModel::save();
// if (_expression)
// modelJson["expression"] = _expression->expression();
return modelJson;
}
void PlotModel::restore(QJsonObject const &p)
{
QJsonValue v = p["expression"];
// if (!v.isUndefined())
//{
// QString str = v.toString();
// std::vector<double> d;
// d.push_back(0.0);
//_expression = std::make_shared<ExpressionRangeData>(str, d);
//_variableLabel->setText(str);
//}
}
unsigned int PlotModel::nPorts(PortType portType) const
{
unsigned int result = 1;
switch (portType)
{
case PortType::In: result = 2; break;
case PortType::Out: result = 0;
default: break;
}
return result;
}
bool PlotModel::eventFilter(QObject *object, QEvent *event)
{
// if (object == _label)
//{
// int w = _label->width();
// int h = _label->height();
// if (event->type() == QEvent::Resize)
//{
////QPixmap pixmap(w, h);
////_chart->scene()->render(&painter, 0, 0);
////if (d)
////{
////_label->setPixmap(d->pixmap().scaled(w, h, Qt::KeepAspectRatio));
////}
//}
//}
return false;
}
std::shared_ptr<NodeDataType> PlotModel::dataType(PortType, PortIndex) const
{
return ExpressionRangeData().type();
}
std::shared_ptr<NodeData> PlotModel::outData(PortIndex)
{
return std::shared_ptr<NodeData>();
}
// QString
// PlotModel::
// convertRangeToText(std::vector<double> const &range) const
//{
// QString result("(");
// for (std::size_t i = 0; i < range.size() - 1; ++i)
//{
// result = result + QString::number(range[i]) + ", ";
//}
// result = result + QString::number(range.back()) + ")";
// return result;
//}
void PlotModel::processData()
{
auto n1 = _input1.lock();
auto n2 = _input2.lock();
if (n1 && n2)
{
QString input1 = n1->expression();
QString input2 = n2->expression();
std::vector<double> const &inputRange1 = n1->range();
std::vector<double> const &inputRange2 = n2->range();
if (inputRange1.size() != inputRange2.size())
{
emit dataInvalidated(0);
return;
}
QLineSeries *series = new QLineSeries();
for (std::size_t i = 0; i < inputRange1.size(); ++i)
{
series->append(inputRange1[i], inputRange2[i]);
}
_chartView->chart()->legend()->hide();
_chartView->chart()->removeAllSeries();
_chartView->chart()->addSeries(series);
_chartView->chart()->createDefaultAxes();
_chartView->chart()->setTitle("X-Y Plot");
}
}
void PlotModel::setInData(std::shared_ptr<NodeData> data, PortIndex portIndex)
{
auto numberData = std::dynamic_pointer_cast<ExpressionRangeData>(data);
if (portIndex == 0)
{
_input1 = numberData;
}
else
{
_input2 = numberData;
}
processData();
}
QWidget *PlotModel::embeddedWidget()
{
return _chartView;
}
|