File: ModuloModel.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 (74 lines) | stat: -rw-r--r-- 1,958 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
#include "ModuloModel.hpp"

#include "IntegerData.hpp"

#include <QtGui/QDoubleValidator>
QJsonObject ModuloModel::save() const
{
    QJsonObject modelJson;
    modelJson["name"] = name();
    return modelJson;
}
unsigned int ModuloModel::nPorts(PortType portType) const
{
    unsigned int result = 1;
    switch (portType)
    {
        case PortType::In: result = 2; break;
        case PortType::Out: result = 1;
        default: break;
    }
    return result;
}
std::shared_ptr<NodeDataType> ModuloModel::dataType(PortType, PortIndex) const
{
    return IntegerData().type();
}
std::shared_ptr<NodeData> ModuloModel::outData(PortIndex)
{
    return _result;
}
void ModuloModel::setInData(std::shared_ptr<NodeData> data, PortIndex portIndex)
{
    auto numberData = std::dynamic_pointer_cast<IntegerData>(data);
    if (portIndex == 0)
    {
        _number1 = numberData;
    }
    else
    {
        _number2 = numberData;
    }
    {
        PortIndex const outPortIndex = 0;
        auto n1 = _number1.lock();
        auto n2 = _number2.lock();
        if (n2 && (n2->number() == 0.0))
        {
            modelValidationState = NodeValidationState::Error;
            modelValidationError = QStringLiteral("Division by zero error");
            _result.reset();
        }
        else if (n1 && n2)
        {
            modelValidationState = NodeValidationState::Valid;
            modelValidationError = QString();
            _result = std::make_shared<IntegerData>(n1->number() % n2->number());
        }
        else
        {
            modelValidationState = NodeValidationState::Warning;
            modelValidationError = QStringLiteral("Missing or incorrect inputs");
            _result.reset();
        }
        Q_EMIT dataUpdated(outPortIndex);
    }
}
NodeValidationState ModuloModel::validationState() const
{
    return modelValidationState;
}
QString ModuloModel::validationMessage() const
{
    return modelValidationError;
}