File: MathFunctionModel.cpp

package info (click to toggle)
qnodeeditor 2.1.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 2,144 kB
  • sloc: cpp: 8,823; makefile: 3
file content (148 lines) | stat: -rw-r--r-- 5,337 bytes parent folder | download
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
142
143
144
145
146
147
148
#include "MathFunctionModel.hpp"

#include "ExpressionRangeData.hpp"

#include <QtCore/QDebug>
#include <QtCore/QJsonValue>
#include <QtGui/QDoubleValidator>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QFormLayout>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QWidget>
#include <cmath>
MathFunctionModel::MathFunctionModel()
{
    createNameAndFunctions();
    _widget = new QWidget();
    auto l = new QFormLayout();
    _functionComboBox = new QComboBox;
    for (auto const &f : _nameAndFunctions)
    {
        _functionComboBox->addItem(std::get<0>(f));
    }
    _secondOperandEdit = new QLineEdit();
    _secondOperandEdit->setValidator(new QDoubleValidator());
    _secondOperandEdit->setText("0.0");
    _variableLabel = new QLineEdit();
    _variableLabel->setReadOnly(true);
    _rangeLabel = new QLineEdit();
    _variableLabel->setReadOnly(true);
    _rangeLabel->setMaximumWidth(200);
    l->addRow("Function:", _functionComboBox);
    l->addRow("Second Operand", _secondOperandEdit);
    l->addRow("Variable:", _variableLabel);
    l->addRow("Range:", _rangeLabel);
    _widget->setLayout(l);
    connect(_secondOperandEdit, SIGNAL(textChanged(QString)), this, SLOT(onTextChanged(QString)));
    connect(_functionComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onFunctionIndexChanged(int)));
}
void MathFunctionModel::onFunctionIndexChanged(int)
{
    processData();
}
void MathFunctionModel::onTextChanged(QString)
{
    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"];
    // 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 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;
}
std::shared_ptr<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;
    FunctionPtr const &f = std::get<2>(_nameAndFunctions[_functionComboBox->currentIndex()]);
    QString secondOperandText = _secondOperandEdit->text();
    bool ok;
    double so = secondOperandText.toDouble(&ok);
    for (auto const &d : range)
    {
        result.push_back(f(d, so));
    }
    return result;
}
void MathFunctionModel::createNameAndFunctions()
{
    _nameAndFunctions.push_back(
        std::make_tuple(QString("sin()"), QString("sin(%1)"), static_cast<FunctionPtr>([](double a, double b) { return sin(a); })));
    _nameAndFunctions.push_back(
        std::make_tuple(QString("cos()"), QString("cos(%1)"), 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); })));
}
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()]);
        _expression = std::make_shared<ExpressionRangeData>(tt.arg(input, _secondOperandEdit->text()), modifiedRange);
        _variableLabel->setText(_expression->expression());
        //_variableLabel->adjustSize();
        _rangeLabel->setText(convertRangeToText(modifiedRange));
        //_rangeLabel->adjustSize();
        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;
}