2017-06-12 19:46:18 +02:00
|
|
|
#include "MathFunctionModel.hpp"
|
|
|
|
|
|
|
|
|
|
#include <QtCore/QJsonValue>
|
|
|
|
|
#include <QtGui/QDoubleValidator>
|
|
|
|
|
|
|
|
|
|
#include <QtWidgets/QWidget>
|
|
|
|
|
#include <QtWidgets/QLineEdit>
|
|
|
|
|
#include <QtWidgets/QComboBox>
|
2017-06-13 00:48:56 +02:00
|
|
|
#include <QtWidgets/QLineEdit>
|
2017-06-12 19:46:18 +02:00
|
|
|
#include <QtWidgets/QFormLayout>
|
|
|
|
|
|
|
|
|
|
#include "ExpressionRangeData.hpp"
|
|
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
|
|
#include <QtCore/QDebug>
|
|
|
|
|
|
|
|
|
|
MathFunctionModel::
|
|
|
|
|
MathFunctionModel()
|
|
|
|
|
{
|
|
|
|
|
createNameAndFunctions();
|
|
|
|
|
|
|
|
|
|
_widget = new QWidget();
|
|
|
|
|
|
|
|
|
|
auto l = new QFormLayout();
|
|
|
|
|
|
|
|
|
|
_functionComboBox = new QComboBox;
|
|
|
|
|
|
|
|
|
|
for (auto const & f : _nameAndFunctions)
|
|
|
|
|
{
|
|
|
|
|
_functionComboBox->addItem(std::get<0>(f));
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 15:15:52 +02:00
|
|
|
_secondOperandEdit = new QLineEdit();
|
|
|
|
|
_secondOperandEdit->setValidator(new QDoubleValidator());
|
|
|
|
|
_secondOperandEdit->setText("0.0");
|
|
|
|
|
|
2017-06-13 00:48:56 +02:00
|
|
|
_variableLabel = new QLineEdit();
|
|
|
|
|
_variableLabel->setReadOnly(true);
|
2017-06-12 19:46:18 +02:00
|
|
|
|
2017-06-13 00:48:56 +02:00
|
|
|
_rangeLabel = new QLineEdit();
|
|
|
|
|
_variableLabel->setReadOnly(true);
|
|
|
|
|
_rangeLabel->setMaximumWidth(200);
|
2017-06-12 19:46:18 +02:00
|
|
|
|
|
|
|
|
l->addRow("Function:", _functionComboBox);
|
2017-06-13 15:15:52 +02:00
|
|
|
l->addRow("Second Operand", _secondOperandEdit);
|
2017-06-12 19:46:18 +02:00
|
|
|
l->addRow("Variable:", _variableLabel);
|
|
|
|
|
l->addRow("Range:", _rangeLabel);
|
|
|
|
|
|
|
|
|
|
_widget->setLayout(l);
|
|
|
|
|
|
2017-06-13 15:15:52 +02:00
|
|
|
connect(_secondOperandEdit, SIGNAL(textChanged(QString)),
|
|
|
|
|
this, SLOT(onTextChanged(QString)));
|
|
|
|
|
|
2017-06-12 19:46:18 +02:00
|
|
|
connect(_functionComboBox, SIGNAL(currentIndexChanged(int)),
|
|
|
|
|
this, SLOT(onFunctionIndexChanged(int)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
MathFunctionModel::
|
2017-06-13 15:15:52 +02:00
|
|
|
onFunctionIndexChanged(int)
|
|
|
|
|
{
|
|
|
|
|
processData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
MathFunctionModel::
|
|
|
|
|
onTextChanged(QString)
|
2017-06-12 19:46:18 +02:00
|
|
|
{
|
|
|
|
|
processData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QJsonObject
|
|
|
|
|
MathFunctionModel::
|
|
|
|
|
save() const
|
|
|
|
|
{
|
|
|
|
|
QJsonObject modelJson = NodeDataModel::save();
|
|
|
|
|
|
|
|
|
|
if (_expression)
|
|
|
|
|
modelJson["expression"] = _expression->expression();
|
|
|
|
|
|
|
|
|
|
return modelJson;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
MathFunctionModel::
|
|
|
|
|
restore(QJsonObject const &p)
|
|
|
|
|
{
|
|
|
|
|
QJsonValue v = p["expression"];
|
|
|
|
|
|
2017-06-13 15:15:52 +02:00
|
|
|
//if (!v.isUndefined())
|
|
|
|
|
//{
|
|
|
|
|
//QString str = v.toString();
|
2017-06-12 19:46:18 +02:00
|
|
|
|
2017-06-13 15:15:52 +02:00
|
|
|
//std::vector<double> d;
|
|
|
|
|
//d.push_back(0.0);
|
2017-06-12 19:46:18 +02:00
|
|
|
|
2017-06-13 15:15:52 +02:00
|
|
|
//_expression = std::make_shared<ExpressionRangeData>(str, d);
|
|
|
|
|
//_variableLabel->setText(str);
|
|
|
|
|
//}
|
2017-06-12 19:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
|
MathFunctionModel::
|
|
|
|
|
nPorts(PortType portType) const
|
|
|
|
|
{
|
|
|
|
|
unsigned int result = 1;
|
|
|
|
|
|
|
|
|
|
switch (portType)
|
|
|
|
|
{
|
|
|
|
|
case PortType::In:
|
|
|
|
|
result = 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PortType::Out:
|
|
|
|
|
result = 1;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NodeDataType
|
|
|
|
|
MathFunctionModel::
|
|
|
|
|
dataType(PortType, PortIndex) const
|
|
|
|
|
{
|
|
|
|
|
return ExpressionRangeData().type();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<NodeData>
|
|
|
|
|
MathFunctionModel::
|
|
|
|
|
outData(PortIndex)
|
|
|
|
|
{
|
|
|
|
|
return _expression;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QString
|
|
|
|
|
MathFunctionModel::
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<double>
|
|
|
|
|
MathFunctionModel::
|
|
|
|
|
applyFunction(std::vector<double> const &range) const
|
|
|
|
|
{
|
|
|
|
|
std::vector<double> result;
|
|
|
|
|
|
2017-06-13 15:15:52 +02:00
|
|
|
FunctionPtr const & f =
|
|
|
|
|
std::get<2>(_nameAndFunctions[_functionComboBox->currentIndex()]);
|
|
|
|
|
|
|
|
|
|
QString secondOperandText = _secondOperandEdit->text();
|
|
|
|
|
|
|
|
|
|
bool ok;
|
|
|
|
|
double so = secondOperandText.toDouble(&ok);
|
2017-06-12 19:46:18 +02:00
|
|
|
|
|
|
|
|
for (auto const & d : range)
|
|
|
|
|
{
|
2017-06-13 15:15:52 +02:00
|
|
|
result.push_back(f(d, so));
|
2017-06-12 19:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
MathFunctionModel::
|
|
|
|
|
createNameAndFunctions()
|
|
|
|
|
{
|
|
|
|
|
_nameAndFunctions.push_back(
|
|
|
|
|
std::make_tuple(QString("sin()"),
|
|
|
|
|
QString("sin(%1)"),
|
2017-06-13 15:15:52 +02:00
|
|
|
static_cast<FunctionPtr>([](double a, double b) {
|
|
|
|
|
return sin(a);
|
2017-06-12 19:46:18 +02:00
|
|
|
})));
|
|
|
|
|
|
|
|
|
|
_nameAndFunctions.push_back(
|
|
|
|
|
std::make_tuple(QString("cos()"),
|
|
|
|
|
QString("cos(%1)"),
|
2017-06-13 15:15:52 +02:00
|
|
|
static_cast<FunctionPtr>([](double a, double b) {
|
|
|
|
|
return cos(a);
|
|
|
|
|
})));
|
|
|
|
|
|
|
|
|
|
_nameAndFunctions.push_back(
|
|
|
|
|
std::make_tuple(QString("-"),
|
|
|
|
|
QString(" %1 - %2 "),
|
|
|
|
|
static_cast<FunctionPtr>([](double a, double b) {
|
|
|
|
|
return a - b;
|
|
|
|
|
})));
|
|
|
|
|
|
|
|
|
|
_nameAndFunctions.push_back(
|
|
|
|
|
std::make_tuple(QString("+"),
|
|
|
|
|
QString(" %1 + %2 "),
|
|
|
|
|
static_cast<FunctionPtr>([](double a, double b) {
|
|
|
|
|
return a + b;
|
|
|
|
|
})));
|
|
|
|
|
|
|
|
|
|
_nameAndFunctions.push_back(
|
|
|
|
|
std::make_tuple(QString("*"),
|
|
|
|
|
QString(" %1 * %2 "),
|
|
|
|
|
static_cast<FunctionPtr>([](double a, double b) {
|
|
|
|
|
return a * b;
|
|
|
|
|
})));
|
|
|
|
|
|
|
|
|
|
_nameAndFunctions.push_back(
|
|
|
|
|
std::make_tuple(QString("pow"),
|
|
|
|
|
QString("pow(%1, %2)"),
|
|
|
|
|
static_cast<FunctionPtr>([](double a, double b) {
|
|
|
|
|
return std::pow(a, b);
|
2017-06-12 19:46:18 +02:00
|
|
|
})));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
MathFunctionModel::
|
|
|
|
|
processData()
|
|
|
|
|
{
|
|
|
|
|
auto inputExpression = _inputExpression.lock();
|
|
|
|
|
|
|
|
|
|
if (inputExpression)
|
|
|
|
|
{
|
|
|
|
|
QString input = inputExpression->expression();
|
|
|
|
|
std::vector<double> const & inputRange = inputExpression->range();
|
|
|
|
|
std::vector<double> modifiedRange = applyFunction(inputRange);
|
|
|
|
|
|
|
|
|
|
QString tt = std::get<1>(_nameAndFunctions[_functionComboBox->currentIndex()]);
|
|
|
|
|
|
2017-06-13 15:15:52 +02:00
|
|
|
_expression = std::make_shared<ExpressionRangeData>(tt.arg(input, _secondOperandEdit->text()), modifiedRange);
|
2017-06-12 19:46:18 +02:00
|
|
|
|
|
|
|
|
_variableLabel->setText(_expression->expression());
|
2017-06-13 15:15:52 +02:00
|
|
|
//_variableLabel->adjustSize();
|
2017-06-12 19:46:18 +02:00
|
|
|
|
|
|
|
|
_rangeLabel->setText(convertRangeToText(modifiedRange));
|
2017-06-13 15:15:52 +02:00
|
|
|
//_rangeLabel->adjustSize();
|
2017-06-12 19:46:18 +02:00
|
|
|
|
|
|
|
|
emit dataUpdated(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
MathFunctionModel::
|
|
|
|
|
setInData(std::shared_ptr<NodeData> nodeData, PortIndex portIndex)
|
|
|
|
|
{
|
|
|
|
|
_inputExpression = std::static_pointer_cast<ExpressionRangeData>(nodeData);
|
|
|
|
|
|
|
|
|
|
processData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QWidget *
|
|
|
|
|
MathFunctionModel::
|
|
|
|
|
embeddedWidget()
|
|
|
|
|
{
|
|
|
|
|
return _widget;
|
|
|
|
|
}
|