添加项目文件。
This commit is contained in:
61
NodeEditorPro/examples/calculator/AdditionModel.hpp
Normal file
61
NodeEditorPro/examples/calculator/AdditionModel.hpp
Normal file
@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include <QtCore/QObject>
|
||||
|
||||
#include <QtWidgets/QLabel>
|
||||
|
||||
#include "NodeDataModel.hpp"
|
||||
|
||||
#include "MathOperationDataModel.hpp"
|
||||
#include "DecimalData.hpp"
|
||||
|
||||
/// The model dictates the number of inputs and outputs for the Node.
|
||||
/// In this example it has no logic.
|
||||
class AdditionModel : public MathOperationDataModel
|
||||
{
|
||||
public:
|
||||
|
||||
virtual
|
||||
~AdditionModel() {}
|
||||
|
||||
public:
|
||||
|
||||
QString
|
||||
caption() const override
|
||||
{
|
||||
return QStringLiteral("加法");
|
||||
}
|
||||
|
||||
QString
|
||||
name() const override
|
||||
{
|
||||
return QStringLiteral("加法");
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void
|
||||
compute() override
|
||||
{
|
||||
PortIndex const outPortIndex = 0;
|
||||
|
||||
auto n1 = _number1.lock();
|
||||
auto n2 = _number2.lock();
|
||||
|
||||
if (n1 && n2)
|
||||
{
|
||||
modelValidationState = NodeValidationState::Valid;
|
||||
modelValidationError = QString();
|
||||
_result = std::make_shared<DecimalData>(n1->number() +
|
||||
n2->number());
|
||||
}
|
||||
else
|
||||
{
|
||||
modelValidationState = NodeValidationState::Warning;
|
||||
modelValidationError = QStringLiteral("未连接或运行失败!");
|
||||
_result.reset();
|
||||
}
|
||||
|
||||
Q_EMIT dataUpdated(outPortIndex);
|
||||
}
|
||||
};
|
5
NodeEditorPro/examples/calculator/CMakeLists.txt
Normal file
5
NodeEditorPro/examples/calculator/CMakeLists.txt
Normal file
@ -0,0 +1,5 @@
|
||||
file(GLOB_RECURSE CPPS ./*.cpp )
|
||||
|
||||
add_executable(calculator ${CPPS})
|
||||
|
||||
target_link_libraries(calculator nodes)
|
46
NodeEditorPro/examples/calculator/Converters.cpp
Normal file
46
NodeEditorPro/examples/calculator/Converters.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
#include "Converters.hpp"
|
||||
|
||||
#include <QtGui/QDoubleValidator>
|
||||
|
||||
#include "DecimalData.hpp"
|
||||
#include "IntegerData.hpp"
|
||||
|
||||
|
||||
std::shared_ptr<NodeData>
|
||||
DecimalToIntegerConverter::
|
||||
operator()(std::shared_ptr<NodeData> data)
|
||||
{
|
||||
auto numberData =
|
||||
std::dynamic_pointer_cast<DecimalData>(data);
|
||||
|
||||
if (numberData)
|
||||
{
|
||||
_integer = std::make_shared<IntegerData>(numberData->number());
|
||||
}
|
||||
else
|
||||
{
|
||||
_integer.reset();
|
||||
}
|
||||
|
||||
return _integer;
|
||||
}
|
||||
|
||||
|
||||
std::shared_ptr<NodeData>
|
||||
IntegerToDecimalConverter::
|
||||
operator()(std::shared_ptr<NodeData> data)
|
||||
{
|
||||
auto numberData =
|
||||
std::dynamic_pointer_cast<IntegerData>(data);
|
||||
|
||||
if (numberData)
|
||||
{
|
||||
_decimal = std::make_shared<DecimalData>(numberData->number());
|
||||
}
|
||||
else
|
||||
{
|
||||
_decimal.reset();
|
||||
}
|
||||
|
||||
return _decimal;
|
||||
}
|
41
NodeEditorPro/examples/calculator/Converters.hpp
Normal file
41
NodeEditorPro/examples/calculator/Converters.hpp
Normal file
@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include "DecimalData.hpp"
|
||||
#include "IntegerData.hpp"
|
||||
|
||||
using QtNodes::PortType;
|
||||
using QtNodes::PortIndex;
|
||||
using QtNodes::NodeData;
|
||||
using QtNodes::NodeDataType;
|
||||
using QtNodes::NodeDataModel;
|
||||
|
||||
class DecimalData;
|
||||
class IntegerData;
|
||||
|
||||
|
||||
class DecimalToIntegerConverter
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
std::shared_ptr<NodeData>
|
||||
operator()(std::shared_ptr<NodeData> data);
|
||||
|
||||
private:
|
||||
|
||||
std::shared_ptr<NodeData> _integer;
|
||||
};
|
||||
|
||||
|
||||
class IntegerToDecimalConverter
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
std::shared_ptr<NodeData>
|
||||
operator()(std::shared_ptr<NodeData> data);
|
||||
|
||||
private:
|
||||
|
||||
std::shared_ptr<NodeData> _decimal;
|
||||
};
|
41
NodeEditorPro/examples/calculator/DecimalData.hpp
Normal file
41
NodeEditorPro/examples/calculator/DecimalData.hpp
Normal file
@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include "NodeDataModel.hpp"
|
||||
|
||||
using QtNodes::NodeDataType;
|
||||
using QtNodes::NodeData;
|
||||
|
||||
/// The class can potentially incapsulate any user data which
|
||||
/// need to be transferred within the Node Editor graph
|
||||
class DecimalData : public NodeData
|
||||
{
|
||||
public:
|
||||
|
||||
DecimalData()
|
||||
: _number(0.0)
|
||||
{}
|
||||
|
||||
DecimalData(double const number)
|
||||
: _number(number)
|
||||
{}
|
||||
|
||||
NodeDataType type() const override
|
||||
{
|
||||
return NodeDataType{ "decimal",
|
||||
QStringLiteral("浮点数") };
|
||||
}
|
||||
|
||||
double number() const
|
||||
{
|
||||
return _number;
|
||||
}
|
||||
|
||||
QString numberAsText() const
|
||||
{
|
||||
return QString::number(_number, 'f');
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
double _number;
|
||||
};
|
95
NodeEditorPro/examples/calculator/DivisionModel.hpp
Normal file
95
NodeEditorPro/examples/calculator/DivisionModel.hpp
Normal file
@ -0,0 +1,95 @@
|
||||
#pragma once
|
||||
|
||||
#include <QtCore/QObject>
|
||||
#include <QtWidgets/QLabel>
|
||||
|
||||
#include "NodeDataModel.hpp"
|
||||
|
||||
#include "MathOperationDataModel.hpp"
|
||||
|
||||
#include "DecimalData.hpp"
|
||||
|
||||
/// The model dictates the number of inputs and outputs for the Node.
|
||||
/// In this example it has no logic.
|
||||
class DivisionModel : public MathOperationDataModel
|
||||
{
|
||||
public:
|
||||
|
||||
virtual
|
||||
~DivisionModel() {}
|
||||
|
||||
public:
|
||||
QString
|
||||
caption() const override
|
||||
{
|
||||
return QStringLiteral("除法");
|
||||
}
|
||||
|
||||
bool
|
||||
portCaptionVisible(PortType portType, PortIndex portIndex) const override
|
||||
{
|
||||
Q_UNUSED(portType); Q_UNUSED(portIndex);
|
||||
return true;
|
||||
}
|
||||
|
||||
QString
|
||||
portCaption(PortType portType, PortIndex portIndex) const override
|
||||
{
|
||||
switch (portType)
|
||||
{
|
||||
case PortType::In:
|
||||
if (portIndex == 0)
|
||||
return QStringLiteral("除数");
|
||||
else if (portIndex == 1)
|
||||
return QStringLiteral("被除数");
|
||||
|
||||
break;
|
||||
|
||||
case PortType::Out:
|
||||
return QStringLiteral("结果");
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
QString
|
||||
name() const override
|
||||
{
|
||||
return QStringLiteral("除法");
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void
|
||||
compute() override
|
||||
{
|
||||
PortIndex const outPortIndex = 0;
|
||||
|
||||
auto n1 = _number1.lock();
|
||||
auto n2 = _number2.lock();
|
||||
|
||||
if (n2 && (n2->number() == 0.0))
|
||||
{
|
||||
modelValidationState = NodeValidationState::Error;
|
||||
modelValidationError = QStringLiteral("被除数无法为0!");
|
||||
_result.reset();
|
||||
}
|
||||
else if (n1 && n2)
|
||||
{
|
||||
modelValidationState = NodeValidationState::Valid;
|
||||
modelValidationError = QString();
|
||||
_result = std::make_shared<DecimalData>(n1->number() /
|
||||
n2->number());
|
||||
}
|
||||
else
|
||||
{
|
||||
modelValidationState = NodeValidationState::Warning;
|
||||
modelValidationError = QStringLiteral("未连接或运行失败!");
|
||||
_result.reset();
|
||||
}
|
||||
|
||||
Q_EMIT dataUpdated(outPortIndex);
|
||||
}
|
||||
};
|
41
NodeEditorPro/examples/calculator/IntegerData.hpp
Normal file
41
NodeEditorPro/examples/calculator/IntegerData.hpp
Normal file
@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include "NodeDataModel.hpp"
|
||||
|
||||
using QtNodes::NodeDataType;
|
||||
using QtNodes::NodeData;
|
||||
|
||||
/// The class can potentially incapsulate any user data which
|
||||
/// need to be transferred within the Node Editor graph
|
||||
class IntegerData : public NodeData
|
||||
{
|
||||
public:
|
||||
|
||||
IntegerData()
|
||||
: _number(0.0)
|
||||
{}
|
||||
|
||||
IntegerData(int const number)
|
||||
: _number(number)
|
||||
{}
|
||||
|
||||
NodeDataType type() const override
|
||||
{
|
||||
return NodeDataType{ "integer",
|
||||
QStringLiteral("整数") };
|
||||
}
|
||||
|
||||
int number() const
|
||||
{
|
||||
return _number;
|
||||
}
|
||||
|
||||
QString numberAsText() const
|
||||
{
|
||||
return QString::number(_number);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
int _number;
|
||||
};
|
10
NodeEditorPro/examples/calculator/MathNodes.hpp
Normal file
10
NodeEditorPro/examples/calculator/MathNodes.hpp
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
//math nodes
|
||||
#include "calculator/AdditionModel.hpp"
|
||||
#include "calculator/DivisionModel.hpp"
|
||||
#include "calculator/MultiplicationModel.hpp"
|
||||
#include "calculator/SubtractionModel.hpp"
|
||||
#include "calculator/NumberSourceDataModel.hpp"
|
||||
#include "calculator/NumberDisplayDataModel.hpp"
|
||||
|
67
NodeEditorPro/examples/calculator/MathOperationDataModel.cpp
Normal file
67
NodeEditorPro/examples/calculator/MathOperationDataModel.cpp
Normal file
@ -0,0 +1,67 @@
|
||||
#include "MathOperationDataModel.hpp"
|
||||
|
||||
#include "DecimalData.hpp"
|
||||
|
||||
unsigned int
|
||||
MathOperationDataModel::
|
||||
nPorts(PortType portType) const
|
||||
{
|
||||
unsigned int result;
|
||||
|
||||
if (portType == PortType::In)
|
||||
result = 2;
|
||||
else
|
||||
result = 1;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
NodeDataType
|
||||
MathOperationDataModel::
|
||||
dataType(PortType, PortIndex) const
|
||||
{
|
||||
return DecimalData().type();
|
||||
}
|
||||
|
||||
|
||||
std::shared_ptr<NodeData>
|
||||
MathOperationDataModel::
|
||||
outData(PortIndex)
|
||||
{
|
||||
return std::static_pointer_cast<NodeData>(_result);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
MathOperationDataModel::
|
||||
setInData(std::shared_ptr<NodeData> data, PortIndex portIndex)
|
||||
{
|
||||
auto numberData =
|
||||
std::dynamic_pointer_cast<DecimalData>(data);
|
||||
|
||||
if (portIndex == 0)
|
||||
{
|
||||
_number1 = numberData;
|
||||
}
|
||||
else
|
||||
{
|
||||
_number2 = numberData;
|
||||
}
|
||||
compute();
|
||||
}
|
||||
|
||||
NodeValidationState
|
||||
MathOperationDataModel::
|
||||
validationState() const
|
||||
{
|
||||
return modelValidationState;
|
||||
}
|
||||
|
||||
|
||||
QString
|
||||
MathOperationDataModel::
|
||||
validationMessage() const
|
||||
{
|
||||
return modelValidationError;
|
||||
}
|
69
NodeEditorPro/examples/calculator/MathOperationDataModel.hpp
Normal file
69
NodeEditorPro/examples/calculator/MathOperationDataModel.hpp
Normal file
@ -0,0 +1,69 @@
|
||||
#pragma once
|
||||
|
||||
#include <QtCore/QObject>
|
||||
#include <QtCore/QJsonObject>
|
||||
#include <QtWidgets/QLabel>
|
||||
|
||||
#include "NodeDataModel.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class DecimalData;
|
||||
|
||||
using QtNodes::PortType;
|
||||
using QtNodes::PortIndex;
|
||||
using QtNodes::NodeData;
|
||||
using QtNodes::NodeDataType;
|
||||
using QtNodes::NodeDataModel;
|
||||
using QtNodes::NodeValidationState;
|
||||
|
||||
/// The model dictates the number of inputs and outputs for the Node.
|
||||
/// In this example it has no logic.
|
||||
class MathOperationDataModel : public NodeDataModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
virtual
|
||||
~MathOperationDataModel() {}
|
||||
|
||||
public:
|
||||
|
||||
unsigned int
|
||||
nPorts(PortType portType) const override;
|
||||
|
||||
NodeDataType
|
||||
dataType(PortType portType,
|
||||
PortIndex portIndex) const override;
|
||||
|
||||
std::shared_ptr<NodeData>
|
||||
outData(PortIndex port) override;
|
||||
|
||||
void
|
||||
setInData(std::shared_ptr<NodeData> data, PortIndex portIndex) override;
|
||||
|
||||
QWidget*
|
||||
embeddedWidget() override { return nullptr; }
|
||||
|
||||
NodeValidationState
|
||||
validationState() const override;
|
||||
|
||||
QString
|
||||
validationMessage() const override;
|
||||
|
||||
protected:
|
||||
|
||||
virtual void
|
||||
compute() = 0;
|
||||
|
||||
protected:
|
||||
|
||||
std::weak_ptr<DecimalData> _number1;
|
||||
std::weak_ptr<DecimalData> _number2;
|
||||
|
||||
std::shared_ptr<DecimalData> _result;
|
||||
|
||||
NodeValidationState modelValidationState = NodeValidationState::Warning;
|
||||
QString modelValidationError = QStringLiteral("未连接或运行失败!");
|
||||
};
|
118
NodeEditorPro/examples/calculator/ModuloModel.cpp
Normal file
118
NodeEditorPro/examples/calculator/ModuloModel.cpp
Normal file
@ -0,0 +1,118 @@
|
||||
#include "ModuloModel.hpp"
|
||||
|
||||
#include <QtGui/QDoubleValidator>
|
||||
|
||||
#include "IntegerData.hpp"
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
115
NodeEditorPro/examples/calculator/ModuloModel.hpp
Normal file
115
NodeEditorPro/examples/calculator/ModuloModel.hpp
Normal file
@ -0,0 +1,115 @@
|
||||
#pragma once
|
||||
|
||||
#include <QtCore/QObject>
|
||||
#include <QtWidgets/QLineEdit>
|
||||
|
||||
#include "NodeDataModel.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using QtNodes::PortType;
|
||||
using QtNodes::PortIndex;
|
||||
using QtNodes::NodeData;
|
||||
using QtNodes::NodeDataType;
|
||||
using QtNodes::NodeDataModel;
|
||||
using QtNodes::NodeValidationState;
|
||||
|
||||
class IntegerData;
|
||||
|
||||
class ModuloModel
|
||||
: public NodeDataModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ModuloModel() = default;
|
||||
|
||||
virtual
|
||||
~ModuloModel() = default;
|
||||
|
||||
public:
|
||||
|
||||
QString
|
||||
caption() const override
|
||||
{
|
||||
return QStringLiteral("求模");
|
||||
}
|
||||
|
||||
bool
|
||||
captionVisible() const override
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
portCaptionVisible(PortType, PortIndex) const override
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
QString
|
||||
portCaption(PortType portType, PortIndex portIndex) const override
|
||||
{
|
||||
switch (portType)
|
||||
{
|
||||
case PortType::In:
|
||||
if (portIndex == 0)
|
||||
return QStringLiteral("求模数");
|
||||
else if (portIndex == 1)
|
||||
return QStringLiteral("被模数");
|
||||
|
||||
break;
|
||||
|
||||
case PortType::Out:
|
||||
return QStringLiteral("结果");
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
QString
|
||||
name() const override
|
||||
{
|
||||
return QStringLiteral("求模");
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
QJsonObject
|
||||
save() const override;
|
||||
|
||||
public:
|
||||
|
||||
unsigned int
|
||||
nPorts(PortType portType) const override;
|
||||
|
||||
NodeDataType
|
||||
dataType(PortType portType, PortIndex portIndex) const override;
|
||||
|
||||
std::shared_ptr<NodeData>
|
||||
outData(PortIndex port) override;
|
||||
|
||||
void
|
||||
setInData(std::shared_ptr<NodeData>, int) override;
|
||||
|
||||
QWidget*
|
||||
embeddedWidget() override { return nullptr; }
|
||||
|
||||
NodeValidationState
|
||||
validationState() const override;
|
||||
|
||||
QString
|
||||
validationMessage() const override;
|
||||
|
||||
private:
|
||||
|
||||
std::weak_ptr<IntegerData> _number1;
|
||||
std::weak_ptr<IntegerData> _number2;
|
||||
|
||||
std::shared_ptr<IntegerData> _result;
|
||||
|
||||
NodeValidationState modelValidationState = NodeValidationState::Warning;
|
||||
QString modelValidationError = QStringLiteral("未连接或运行失败!");
|
||||
};
|
61
NodeEditorPro/examples/calculator/MultiplicationModel.hpp
Normal file
61
NodeEditorPro/examples/calculator/MultiplicationModel.hpp
Normal file
@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include <QtCore/QObject>
|
||||
#include <QtWidgets/QLabel>
|
||||
|
||||
#include "NodeDataModel.hpp"
|
||||
|
||||
#include "MathOperationDataModel.hpp"
|
||||
|
||||
#include "DecimalData.hpp"
|
||||
|
||||
/// The model dictates the number of inputs and outputs for the Node.
|
||||
/// In this example it has no logic.
|
||||
class MultiplicationModel : public MathOperationDataModel
|
||||
{
|
||||
public:
|
||||
|
||||
virtual
|
||||
~MultiplicationModel() {}
|
||||
|
||||
public:
|
||||
|
||||
QString
|
||||
caption() const override
|
||||
{
|
||||
return QStringLiteral("乘法");
|
||||
}
|
||||
|
||||
QString
|
||||
name() const override
|
||||
{
|
||||
return QStringLiteral("乘法");
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void
|
||||
compute() override
|
||||
{
|
||||
PortIndex const outPortIndex = 0;
|
||||
|
||||
auto n1 = _number1.lock();
|
||||
auto n2 = _number2.lock();
|
||||
|
||||
if (n1 && n2)
|
||||
{
|
||||
modelValidationState = NodeValidationState::Valid;
|
||||
modelValidationError = QString();
|
||||
_result = std::make_shared<DecimalData>(n1->number() *
|
||||
n2->number());
|
||||
}
|
||||
else
|
||||
{
|
||||
modelValidationState = NodeValidationState::Warning;
|
||||
modelValidationError = QStringLiteral("未连接或输入错误!");
|
||||
_result.reset();
|
||||
}
|
||||
|
||||
Q_EMIT dataUpdated(outPortIndex);
|
||||
}
|
||||
};
|
102
NodeEditorPro/examples/calculator/NumberDisplayDataModel.cpp
Normal file
102
NodeEditorPro/examples/calculator/NumberDisplayDataModel.cpp
Normal file
@ -0,0 +1,102 @@
|
||||
#include "NumberDisplayDataModel.hpp"
|
||||
|
||||
#include "DecimalData.hpp"
|
||||
|
||||
#include <QtWidgets/QLabel>
|
||||
|
||||
NumberDisplayDataModel::
|
||||
NumberDisplayDataModel()
|
||||
: _label{nullptr}
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
unsigned int
|
||||
NumberDisplayDataModel::
|
||||
nPorts(PortType portType) const
|
||||
{
|
||||
unsigned int result = 1;
|
||||
|
||||
switch (portType)
|
||||
{
|
||||
case PortType::In:
|
||||
result = 1;
|
||||
break;
|
||||
|
||||
case PortType::Out:
|
||||
result = 0;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
NodeDataType
|
||||
NumberDisplayDataModel::
|
||||
dataType(PortType, PortIndex) const
|
||||
{
|
||||
return DecimalData().type();
|
||||
}
|
||||
|
||||
|
||||
std::shared_ptr<NodeData>
|
||||
NumberDisplayDataModel::
|
||||
outData(PortIndex)
|
||||
{
|
||||
std::shared_ptr<NodeData> ptr;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
NumberDisplayDataModel::
|
||||
setInData(std::shared_ptr<NodeData> data, int)
|
||||
{
|
||||
auto numberData = std::dynamic_pointer_cast<DecimalData>(data);
|
||||
|
||||
if (numberData)
|
||||
{
|
||||
modelValidationState = NodeValidationState::Valid;
|
||||
modelValidationError = QString();
|
||||
_label->setText(numberData->numberAsText());
|
||||
}
|
||||
else
|
||||
{
|
||||
modelValidationState = NodeValidationState::Warning;
|
||||
modelValidationError = QStringLiteral("Missing or incorrect inputs");
|
||||
_label->clear();
|
||||
}
|
||||
|
||||
_label->adjustSize();
|
||||
}
|
||||
|
||||
QWidget*
|
||||
NumberDisplayDataModel::
|
||||
embeddedWidget()
|
||||
{
|
||||
if (!_label)
|
||||
{
|
||||
_label = new QLabel();
|
||||
_label->setMargin(3);
|
||||
}
|
||||
|
||||
return _label;
|
||||
}
|
||||
|
||||
NodeValidationState
|
||||
NumberDisplayDataModel::
|
||||
validationState() const
|
||||
{
|
||||
return modelValidationState;
|
||||
}
|
||||
|
||||
|
||||
QString
|
||||
NumberDisplayDataModel::
|
||||
validationMessage() const
|
||||
{
|
||||
return modelValidationError;
|
||||
}
|
80
NodeEditorPro/examples/calculator/NumberDisplayDataModel.hpp
Normal file
80
NodeEditorPro/examples/calculator/NumberDisplayDataModel.hpp
Normal file
@ -0,0 +1,80 @@
|
||||
#pragma once
|
||||
|
||||
#include <QtCore/QObject>
|
||||
|
||||
#include "NodeDataModel.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using QtNodes::PortType;
|
||||
using QtNodes::PortIndex;
|
||||
using QtNodes::NodeData;
|
||||
using QtNodes::NodeDataType;
|
||||
using QtNodes::NodeDataModel;
|
||||
using QtNodes::NodeValidationState;
|
||||
|
||||
class QLabel;
|
||||
|
||||
/// The model dictates the number of inputs and outputs for the Node.
|
||||
/// In this example it has no logic.
|
||||
class NumberDisplayDataModel : public NodeDataModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
NumberDisplayDataModel();
|
||||
|
||||
virtual
|
||||
~NumberDisplayDataModel() {}
|
||||
|
||||
public:
|
||||
|
||||
QString
|
||||
caption() const override
|
||||
{
|
||||
return QStringLiteral("数字显示");
|
||||
}
|
||||
|
||||
bool
|
||||
captionVisible() const override
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
QString
|
||||
name() const override
|
||||
{
|
||||
return QStringLiteral("数字显示");
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
unsigned int
|
||||
nPorts(PortType portType) const override;
|
||||
|
||||
NodeDataType
|
||||
dataType(PortType portType,
|
||||
PortIndex portIndex) const override;
|
||||
|
||||
std::shared_ptr<NodeData>
|
||||
outData(PortIndex port) override;
|
||||
|
||||
void
|
||||
setInData(std::shared_ptr<NodeData> data, int) override;
|
||||
|
||||
QWidget*
|
||||
embeddedWidget() override;
|
||||
|
||||
NodeValidationState
|
||||
validationState() const override;
|
||||
|
||||
QString
|
||||
validationMessage() const override;
|
||||
|
||||
private:
|
||||
|
||||
NodeValidationState modelValidationState = NodeValidationState::Warning;
|
||||
QString modelValidationError = QStringLiteral("未连接或运算失败!");
|
||||
|
||||
QLabel* _label;
|
||||
};
|
129
NodeEditorPro/examples/calculator/NumberSourceDataModel.cpp
Normal file
129
NodeEditorPro/examples/calculator/NumberSourceDataModel.cpp
Normal file
@ -0,0 +1,129 @@
|
||||
#include "NumberSourceDataModel.hpp"
|
||||
|
||||
#include "DecimalData.hpp"
|
||||
|
||||
#include <QtCore/QJsonValue>
|
||||
#include <QtGui/QDoubleValidator>
|
||||
#include <QtWidgets/QLineEdit>
|
||||
|
||||
NumberSourceDataModel::
|
||||
NumberSourceDataModel()
|
||||
: _lineEdit{ nullptr }
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
QJsonObject
|
||||
NumberSourceDataModel::
|
||||
save() const
|
||||
{
|
||||
QJsonObject modelJson = NodeDataModel::save();
|
||||
|
||||
if (_number)
|
||||
modelJson["number"] = QString::number(_number->number());
|
||||
|
||||
return modelJson;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
NumberSourceDataModel::
|
||||
restore(QJsonObject const& p)
|
||||
{
|
||||
QJsonValue v = p["number"];
|
||||
|
||||
if (!v.isUndefined())
|
||||
{
|
||||
QString strNum = v.toString();
|
||||
|
||||
bool ok;
|
||||
double d = strNum.toDouble(&ok);
|
||||
if (ok)
|
||||
{
|
||||
_number = std::make_shared<DecimalData>(d);
|
||||
_lineEdit->setText(strNum);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int
|
||||
NumberSourceDataModel::
|
||||
nPorts(PortType portType) const
|
||||
{
|
||||
unsigned int result = 1;
|
||||
|
||||
switch (portType)
|
||||
{
|
||||
case PortType::In:
|
||||
result = 0;
|
||||
break;
|
||||
|
||||
case PortType::Out:
|
||||
result = 1;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
NumberSourceDataModel::
|
||||
onTextEdited(QString const& string)
|
||||
{
|
||||
Q_UNUSED(string);
|
||||
|
||||
bool ok = false;
|
||||
|
||||
double number = _lineEdit->text().toDouble(&ok);
|
||||
|
||||
if (ok)
|
||||
{
|
||||
_number = std::make_shared<DecimalData>(number);
|
||||
|
||||
Q_EMIT dataUpdated(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
Q_EMIT dataInvalidated(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
NodeDataType
|
||||
NumberSourceDataModel::
|
||||
dataType(PortType, PortIndex) const
|
||||
{
|
||||
return DecimalData().type();
|
||||
}
|
||||
|
||||
|
||||
std::shared_ptr<NodeData>
|
||||
NumberSourceDataModel::
|
||||
outData(PortIndex)
|
||||
{
|
||||
return _number;
|
||||
}
|
||||
|
||||
|
||||
QWidget*
|
||||
NumberSourceDataModel::
|
||||
embeddedWidget()
|
||||
{
|
||||
if (!_lineEdit)
|
||||
{
|
||||
_lineEdit = new QLineEdit();
|
||||
|
||||
_lineEdit->setValidator(new QDoubleValidator());
|
||||
_lineEdit->setMaximumSize(_lineEdit->sizeHint());
|
||||
|
||||
connect(_lineEdit, &QLineEdit::textChanged,
|
||||
this, &NumberSourceDataModel::onTextEdited);
|
||||
|
||||
_lineEdit->setText("0.0");
|
||||
}
|
||||
|
||||
return _lineEdit;
|
||||
}
|
89
NodeEditorPro/examples/calculator/NumberSourceDataModel.hpp
Normal file
89
NodeEditorPro/examples/calculator/NumberSourceDataModel.hpp
Normal file
@ -0,0 +1,89 @@
|
||||
#pragma once
|
||||
|
||||
#include <QtCore/QObject>
|
||||
|
||||
#include "NodeDataModel.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class DecimalData;
|
||||
|
||||
using QtNodes::PortType;
|
||||
using QtNodes::PortIndex;
|
||||
using QtNodes::NodeData;
|
||||
using QtNodes::NodeDataType;
|
||||
using QtNodes::NodeDataModel;
|
||||
using QtNodes::NodeValidationState;
|
||||
|
||||
class QLineEdit;
|
||||
|
||||
/// The model dictates the number of inputs and outputs for the Node.
|
||||
/// In this example it has no logic.
|
||||
class NumberSourceDataModel
|
||||
: public NodeDataModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
NumberSourceDataModel();
|
||||
|
||||
virtual
|
||||
~NumberSourceDataModel() {}
|
||||
|
||||
public:
|
||||
|
||||
QString
|
||||
caption() const override
|
||||
{
|
||||
return QStringLiteral("数字输入");
|
||||
}
|
||||
|
||||
bool
|
||||
captionVisible() const override
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
QString
|
||||
name() const override
|
||||
{
|
||||
return QStringLiteral("数字输入");
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
QJsonObject
|
||||
save() const override;
|
||||
|
||||
void
|
||||
restore(QJsonObject const& p) override;
|
||||
|
||||
public:
|
||||
|
||||
unsigned int
|
||||
nPorts(PortType portType) const override;
|
||||
|
||||
NodeDataType
|
||||
dataType(PortType portType, PortIndex portIndex) const override;
|
||||
|
||||
std::shared_ptr<NodeData>
|
||||
outData(PortIndex port) override;
|
||||
|
||||
void
|
||||
setInData(std::shared_ptr<NodeData>, int) override
|
||||
{ }
|
||||
|
||||
QWidget*
|
||||
embeddedWidget() override;
|
||||
|
||||
private Q_SLOTS:
|
||||
|
||||
void
|
||||
onTextEdited(QString const& string);
|
||||
|
||||
private:
|
||||
|
||||
std::shared_ptr<DecimalData> _number;
|
||||
|
||||
QLineEdit* _lineEdit;
|
||||
};
|
90
NodeEditorPro/examples/calculator/SubtractionModel.hpp
Normal file
90
NodeEditorPro/examples/calculator/SubtractionModel.hpp
Normal file
@ -0,0 +1,90 @@
|
||||
#pragma once
|
||||
|
||||
#include <QtCore/QObject>
|
||||
#include <QtWidgets/QLabel>
|
||||
|
||||
#include "NodeDataModel.hpp"
|
||||
|
||||
#include "MathOperationDataModel.hpp"
|
||||
|
||||
#include "DecimalData.hpp"
|
||||
|
||||
/// The model dictates the number of inputs and outputs for the Node.
|
||||
/// In this example it has no logic.
|
||||
class SubtractionModel : public MathOperationDataModel
|
||||
{
|
||||
public:
|
||||
|
||||
virtual
|
||||
~SubtractionModel() {}
|
||||
|
||||
public:
|
||||
|
||||
QString
|
||||
caption() const override
|
||||
{
|
||||
return QStringLiteral("减法节点");
|
||||
}
|
||||
|
||||
virtual bool
|
||||
portCaptionVisible(PortType portType, PortIndex portIndex) const override
|
||||
{
|
||||
Q_UNUSED(portType); Q_UNUSED(portIndex);
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual QString
|
||||
portCaption(PortType portType, PortIndex portIndex) const override
|
||||
{
|
||||
switch (portType)
|
||||
{
|
||||
case PortType::In:
|
||||
if (portIndex == 0)
|
||||
return QStringLiteral("减数");
|
||||
else if (portIndex == 1)
|
||||
return QStringLiteral("被减数");
|
||||
|
||||
break;
|
||||
|
||||
case PortType::Out:
|
||||
return QStringLiteral("结果");
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
QString
|
||||
name() const override
|
||||
{
|
||||
return QStringLiteral("减法");
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void
|
||||
compute() override
|
||||
{
|
||||
PortIndex const outPortIndex = 0;
|
||||
|
||||
auto n1 = _number1.lock();
|
||||
auto n2 = _number2.lock();
|
||||
|
||||
if (n1 && n2)
|
||||
{
|
||||
modelValidationState = NodeValidationState::Valid;
|
||||
modelValidationError = QString();
|
||||
_result = std::make_shared<DecimalData>(n1->number() -
|
||||
n2->number());
|
||||
}
|
||||
else
|
||||
{
|
||||
modelValidationState = NodeValidationState::Warning;
|
||||
modelValidationError = QStringLiteral("缺失节点或运行失败!");
|
||||
_result.reset();
|
||||
}
|
||||
|
||||
Q_EMIT dataUpdated(outPortIndex);
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user