diff options
Diffstat (limited to 'examples/demos/robotarm/Backend')
-rw-r--r-- | examples/demos/robotarm/Backend/CMakeLists.txt | 14 | ||||
-rw-r--r-- | examples/demos/robotarm/Backend/animatedparam.cpp | 35 | ||||
-rw-r--r-- | examples/demos/robotarm/Backend/animatedparam.h | 32 | ||||
-rw-r--r-- | examples/demos/robotarm/Backend/backend.cpp | 115 | ||||
-rw-r--r-- | examples/demos/robotarm/Backend/backend.h | 66 |
5 files changed, 262 insertions, 0 deletions
diff --git a/examples/demos/robotarm/Backend/CMakeLists.txt b/examples/demos/robotarm/Backend/CMakeLists.txt new file mode 100644 index 00000000..4d2e1eb4 --- /dev/null +++ b/examples/demos/robotarm/Backend/CMakeLists.txt @@ -0,0 +1,14 @@ +find_package(Qt6 REQUIRED COMPONENTS Gui) + +qt_add_qml_module(backendmodule + URI Backend + VERSION 1.0 + SOURCES + animatedparam.cpp + animatedparam.h + backend.cpp + backend.h + RESOURCE_PREFIX "/" +) + +target_link_libraries(backendmodule PUBLIC Qt6::Gui) diff --git a/examples/demos/robotarm/Backend/animatedparam.cpp b/examples/demos/robotarm/Backend/animatedparam.cpp new file mode 100644 index 00000000..e72678e3 --- /dev/null +++ b/examples/demos/robotarm/Backend/animatedparam.cpp @@ -0,0 +1,35 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "animatedparam.h" + +#include <QVariantAnimation> + +AnimatedParam::AnimatedParam(QObject *parent) : QVariantAnimation(parent) +{ + setDuration(1500); + setEasingCurve(QEasingCurve::InOutCubic); + + connect(this, &QVariantAnimation::valueChanged, this, &AnimatedParam::valueChanged); + connect(this, &QAbstractAnimation::stateChanged, this, [this](QAbstractAnimation::State newState, QAbstractAnimation::State) { + m_isRunning = (newState == QAbstractAnimation::Running); + }); +} + +int AnimatedParam::value() const +{ + return currentValue().toInt(); +} + +void AnimatedParam::setValue(int newValue) +{ + stop(); + setStartValue(value()); + setEndValue(newValue); + start(); +} + +bool AnimatedParam::isRunning() const +{ + return m_isRunning; +} diff --git a/examples/demos/robotarm/Backend/animatedparam.h b/examples/demos/robotarm/Backend/animatedparam.h new file mode 100644 index 00000000..e88576f7 --- /dev/null +++ b/examples/demos/robotarm/Backend/animatedparam.h @@ -0,0 +1,32 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#ifndef ANIMATEDPARAM_H +#define ANIMATEDPARAM_H + +#include <QProperty> +#include <QVariantAnimation> + +//! [class definition] +class AnimatedParam : public QVariantAnimation +{ + Q_OBJECT + Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged) + //! [class definition] + +public: + AnimatedParam(QObject *parent = nullptr); + + int value() const; + void setValue(int newValue); + + bool isRunning() const; + +signals: + void valueChanged(); + +private: + QProperty<bool> m_isRunning; +}; + +#endif // ANIMATEDPARAM_H diff --git a/examples/demos/robotarm/Backend/backend.cpp b/examples/demos/robotarm/Backend/backend.cpp new file mode 100644 index 00000000..e51aa13e --- /dev/null +++ b/examples/demos/robotarm/Backend/backend.cpp @@ -0,0 +1,115 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "backend.h" +#include <QTransform> + +Backend::Backend(QObject *parent) : QObject(parent) +{ + connect(&m_rotation1Angle, &AnimatedParam::valueChanged, this, &Backend::rot1AngleChanged); + connect(&m_rotation2Angle, &AnimatedParam::valueChanged, this, &Backend::rot2AngleChanged); + connect(&m_rotation3Angle, &AnimatedParam::valueChanged, this, &Backend::rot3AngleChanged); + connect(&m_rotation4Angle, &AnimatedParam::valueChanged, this, &Backend::rot4AngleChanged); + connect(&m_clawsAngle, &AnimatedParam::valueChanged, this, &Backend::clawsAngleChanged); + + m_status.setBinding([this]() { + return m_isCollision.value() ? QString("Collision!") + : m_rotation1Angle.isRunning() || m_rotation2Angle.isRunning() || m_rotation3Angle.isRunning() + || m_rotation4Angle.isRunning() + ? QString("Busy") + : QString("Ready"); + }); + + connect(&m_rotation1Angle, &AnimatedParam::valueChanged, this, &Backend::detectCollision); + connect(&m_rotation2Angle, &AnimatedParam::valueChanged, this, &Backend::detectCollision); + connect(&m_rotation3Angle, &AnimatedParam::valueChanged, this, &Backend::detectCollision); + connect(&m_rotation4Angle, &AnimatedParam::valueChanged, this, &Backend::detectCollision); +} + +int Backend::rotation1Angle() const +{ + return m_rotation1Angle.value(); +} + +void Backend::setRot1Angle(const int angle) +{ + m_rotation1Angle.setValue(angle); +} + +int Backend::rotation2Angle() const +{ + return m_rotation2Angle.value(); +} + +void Backend::setRot2Angle(const int angle) +{ + m_rotation2Angle.setValue(angle); +} + +int Backend::rotation3Angle() const +{ + return m_rotation3Angle.value(); +} + +void Backend::setRot3Angle(const int angle) +{ + m_rotation3Angle.setValue(angle); +} + +int Backend::rotation4Angle() const +{ + return m_rotation4Angle.value(); +} + +void Backend::setRot4Angle(const int angle) +{ + m_rotation4Angle.setValue(angle); +} + +int Backend::clawsAngle() const +{ + return m_clawsAngle.value(); +} + +void Backend::setClawsAngle(const int angle) +{ + m_clawsAngle.setValue(angle); +} + +QString Backend::status() const +{ + return m_status; +} + +QBindable<QString> Backend::bindableStatus() const +{ + return &m_status; +} + +void Backend::detectCollision() +{ + // simple aproximate collision detection, uses hardcoded model dimensions + + QPolygon pol1(QRect(-70, 0, 70, 300)); + + QTransform t; + + t.rotate(8.7); + t.translate(0, 259); + + t.rotate(-20.); + t.rotate(rotation3Angle()); + + QPolygon pol2 = t.mapToPolygon(QRect(-35, 0, 35, 233)); + t.translate(0, 233); + t.rotate(15); + t.rotate(rotation2Angle()); + + QPolygon pol3 = t.mapToPolygon(QRect(-27, 0, 27, 212)); + t.translate(0, 212); + t.rotate(rotation1Angle()); + + QPolygon pol4 = t.mapToPolygon(QRect(-42, 0, 42, 180)); + + m_isCollision.setValue(pol1.intersects(pol3) || pol1.intersects(pol4) || pol2.intersects(pol4)); +} diff --git a/examples/demos/robotarm/Backend/backend.h b/examples/demos/robotarm/Backend/backend.h new file mode 100644 index 00000000..e7b92234 --- /dev/null +++ b/examples/demos/robotarm/Backend/backend.h @@ -0,0 +1,66 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#ifndef BACKEND_H +#define BACKEND_H + +#include "animatedparam.h" + +#include <QObject> +#include <qqmlregistration.h> + +//! [class definition] +class Backend : public QObject +{ + Q_OBJECT + QML_ELEMENT + Q_PROPERTY(int rotation1Angle READ rotation1Angle WRITE setRot1Angle NOTIFY rot1AngleChanged) + Q_PROPERTY(int rotation2Angle READ rotation2Angle WRITE setRot2Angle NOTIFY rot2AngleChanged) + Q_PROPERTY(int rotation3Angle READ rotation3Angle WRITE setRot3Angle NOTIFY rot3AngleChanged) + Q_PROPERTY(int rotation4Angle READ rotation4Angle WRITE setRot4Angle NOTIFY rot4AngleChanged) + Q_PROPERTY(int clawsAngle READ clawsAngle WRITE setClawsAngle NOTIFY clawsAngleChanged) + Q_PROPERTY(QString status READ status BINDABLE bindableStatus) + //! [class definition] + +public: + explicit Backend(QObject *parent = nullptr); + + int rotation1Angle() const; + void setRot1Angle(const int angle); + + int rotation2Angle() const; + void setRot2Angle(const int angle); + + int rotation3Angle() const; + void setRot3Angle(const int angle); + + int rotation4Angle() const; + void setRot4Angle(const int angle); + + int clawsAngle() const; + void setClawsAngle(const int angle); + + QString status() const; + QBindable<QString> bindableStatus() const; + +signals: + void rot1AngleChanged(); + void rot2AngleChanged(); + void rot3AngleChanged(); + void rot4AngleChanged(); + void clawsAngleChanged(); + +private: + AnimatedParam m_rotation1Angle; + AnimatedParam m_rotation2Angle; + AnimatedParam m_rotation3Angle; + AnimatedParam m_rotation4Angle; + AnimatedParam m_clawsAngle; + + QProperty<QString> m_status; + QProperty<bool> m_isCollision; + + void detectCollision(); +}; + +#endif // BACKEND_H |