Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ else()
endif()

find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Widgets Gui OpenGL)
find_package(Qt${QT_VERSION_MAJOR} QUIET COMPONENTS Quick)
message(STATUS "QT_VERSION: ${QT_VERSION}, QT_DIR: ${QT_DIR}")

if (${QT_VERSION} VERSION_LESS 5.11.0)
Expand Down Expand Up @@ -129,6 +130,12 @@ set(HPP_HEADER_FILES
include/QtNodes/internal/UndoCommands.hpp
)

if(Qt${QT_VERSION_MAJOR}Quick_FOUND AND ${QT_VERSION} VERSION_GREATER_EQUAL 6.0.0)
message(STATUS "Qt Quick found")
list(APPEND CPP_SOURCE_FILES src/QmlWrapper.cpp)
list(APPEND HPP_HEADER_FILES include/QtNodes/internal/QmlWrapper.hpp)
endif()

# If we want to give the option to build a static library,
# set BUILD_SHARED_LIBS option to OFF
add_library(QtNodes
Expand All @@ -155,6 +162,12 @@ target_link_libraries(QtNodes
Qt${QT_VERSION_MAJOR}::Gui
Qt${QT_VERSION_MAJOR}::OpenGL
)
if(Qt${QT_VERSION_MAJOR}Quick_FOUND AND ${QT_VERSION} VERSION_GREATER_EQUAL 6.0.0)
target_link_libraries(QtNodes
PUBLIC
Qt${QT_VERSION_MAJOR}::Quick
)
endif()

target_compile_definitions(QtNodes
PUBLIC
Expand Down
3 changes: 3 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ add_subdirectory(dynamic_ports)

add_subdirectory(lock_nodes_and_connections)

if(Qt${QT_VERSION_MAJOR}Quick_FOUND AND ${QT_VERSION} VERSION_GREATER_EQUAL 6.0.0)
add_subdirectory(calculator_qml)
endif()
39 changes: 39 additions & 0 deletions examples/calculator_qml/AdditionModel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include "DecimalData.hpp"
#include "MathOperationDataModel.hpp"

#include <QtNodes/NodeDelegateModel>

#include <QtCore/QObject>
#include <QtWidgets/QLabel>

/// The model dictates the number of inputs and outputs for the Node.
/// In this example it has no logic.
class AdditionModel : public MathOperationDataModel
{
public:
~AdditionModel() = default;

public:
QString caption() const override { return QStringLiteral("Addition"); }

QString name() const override { return QStringLiteral("Addition"); }

private:
void compute() override
{
PortIndex const outPortIndex = 0;

auto n1 = _number1.lock();
auto n2 = _number2.lock();

if (n1 && n2) {
_result = std::make_shared<DecimalData>(n1->number() + n2->number());
} else {
_result.reset();
}

Q_EMIT dataUpdated(outPortIndex);
}
};
33 changes: 33 additions & 0 deletions examples/calculator_qml/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
set(CALC_SOURCE_FILES
main.cpp MathOperationDataModel.cpp MyNodeEditor.cpp NumberDisplayDataModel.cpp
NumberSourceDataModel.cpp StringDataModel.cpp)

set(CALC_HEADER_FILES
AdditionModel.hpp
DivisionModel.hpp
DecimalData.hpp
MathOperationDataModel.hpp
MultiplicationModel.hpp
MyNodeEditor.hpp
NumberDisplayDataModel.hpp
NumberSourceDataModel.hpp
SubtractionModel.hpp
StringDataModel.hpp
StringData.hpp)

set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
cmake_policy(SET CMP0071 NEW)

qt_standard_project_setup(REQUIRES 6.5)

qt_add_resources(QT_RESOURCES "main.qrc")
qt_add_executable(calculator_qml ${CALC_SOURCE_FILES} ${CALC_HEADER_FILES}
${QT_RESOURCES})

qt_add_qml_module(
calculator_qml VERSION 1.0 URI hello QML_FILES main.qml
)

target_link_libraries(calculator_qml PRIVATE Qt6::Gui QtNodes)
29 changes: 29 additions & 0 deletions examples/calculator_qml/DecimalData.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <QtNodes/NodeData>

using QtNodes::NodeData;
using QtNodes::NodeDataType;

/// 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", "Decimal"}; }

double number() const { return _number; }

QString numberAsText() const { return QString::number(_number, 'f'); }

private:
double _number;
};
74 changes: 74 additions & 0 deletions examples/calculator_qml/DivisionModel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#pragma once

#include "DecimalData.hpp"
#include "MathOperationDataModel.hpp"

#include <QtNodes/NodeDelegateModel>

#include <QtCore/QObject>
#include <QtWidgets/QLabel>

/// 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("Division"); }

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("Dividend");
else if (portIndex == 1)
return QStringLiteral("Divisor");

break;

case PortType::Out:
return QStringLiteral("Result");

default:
break;
}
return QString();
}

QString name() const override { return QStringLiteral("Division"); }

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("Division by zero error");
_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("Missing or incorrect inputs");
_result.reset();
}

Q_EMIT dataUpdated(outPortIndex);
}
};
42 changes: 42 additions & 0 deletions examples/calculator_qml/MathOperationDataModel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#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 (!data) {
Q_EMIT dataInvalidated(0);
}

if (portIndex == 0) {
_number1 = numberData;
} else {
_number2 = numberData;
}

compute();
}
47 changes: 47 additions & 0 deletions examples/calculator_qml/MathOperationDataModel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#pragma once

#include <QtNodes/NodeDelegateModel>

#include <QtCore/QJsonObject>
#include <QtCore/QObject>
#include <QtWidgets/QLabel>

#include <iostream>

class DecimalData;

using QtNodes::NodeData;
using QtNodes::NodeDataType;
using QtNodes::NodeDelegateModel;
using QtNodes::PortIndex;
using QtNodes::PortType;

/// The model dictates the number of inputs and outputs for the Node.
/// In this example it has no logic.
class MathOperationDataModel : public NodeDelegateModel
{
Q_OBJECT

public:
~MathOperationDataModel() = default;

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; }

protected:
virtual void compute() = 0;

protected:
std::weak_ptr<DecimalData> _number1;
std::weak_ptr<DecimalData> _number2;

std::shared_ptr<DecimalData> _result;
};
44 changes: 44 additions & 0 deletions examples/calculator_qml/MultiplicationModel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once

#include <QtNodes/NodeDelegateModel>

#include <QtCore/QObject>
#include <QtWidgets/QLabel>

#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("Multiplication"); }

QString name() const override { return QStringLiteral("Multiplication"); }

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("Missing or incorrect inputs");
_result.reset();
}

Q_EMIT dataUpdated(outPortIndex);
}
};
29 changes: 29 additions & 0 deletions examples/calculator_qml/MyNodeEditor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "MyNodeEditor.hpp"

#include "AdditionModel.hpp"
#include "DivisionModel.hpp"
#include "MultiplicationModel.hpp"
#include "NumberDisplayDataModel.hpp"
#include "NumberSourceDataModel.hpp"
#include "StringDataModel.hpp"
#include "SubtractionModel.hpp"

std::shared_ptr<QtNodes::NodeDelegateModelRegistry> MyNodeEditor::registerDataModels() const
{
auto ret = std::make_shared<QtNodes::NodeDelegateModelRegistry>();
ret->registerModel<NumberSourceDataModel>("Sources");

ret->registerModel<NumberDisplayDataModel>("Displays");

ret->registerModel<AdditionModel>("Operators");

ret->registerModel<SubtractionModel>("Operators");

ret->registerModel<MultiplicationModel>("Operators");

ret->registerModel<DivisionModel>("Operators");

ret->registerModel<StringDataModel>("Sources");

return ret;
}
Loading
Loading