From 4c4d85a78dafa340285990ec60d23d3e1a22bf24 Mon Sep 17 00:00:00 2001 From: Pimpest <82343504+pimpest@users.noreply.github.com> Date: Sun, 9 Mar 2025 11:48:38 +0100 Subject: [PATCH] Added new qml module for drawing lines --- CMakeLists.txt | 7 ++- include/rqt_demo_plugin/lineItem.hpp | 32 ++++++++++++++ include/rqt_demo_plugin/rqt_demo_plugin.hpp | 2 + .../colcon-core/packages/rqt_demo_plugin | 2 +- log/latest_build | 2 +- resources/qml/helloworld.qml | 13 ++++-- src/lineItem.cpp | 44 +++++++++++++++++++ src/rqt_demo_plugin.cpp | 6 +++ 8 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 include/rqt_demo_plugin/lineItem.hpp create mode 100644 src/lineItem.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 67db5e7..5b072ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,7 +20,11 @@ find_package(Qt5QuickCompiler) set(CMAKE_AUTOMOC ON) set(QT_QML_GENERATE_QMLLS_INI ON) -qt_wrap_cpp(MOC_FILES include/rqt_demo_plugin/rqt_demo_plugin.hpp) + +qt_wrap_cpp( + MOC_FILES + include/rqt_demo_plugin/rqt_demo_plugin.hpp + include/rqt_demo_plugin/lineItem.hpp) qtquick_compiler_add_resources(RCC_SOURCES "resources/res.qrc") @@ -28,6 +32,7 @@ add_library( rqt_demo_plugin SHARED src/rqt_demo_plugin.cpp + src/lineItem.cpp ${RCC_SOURCES} ${MOC_FILES} ) diff --git a/include/rqt_demo_plugin/lineItem.hpp b/include/rqt_demo_plugin/lineItem.hpp new file mode 100644 index 0000000..670d4f6 --- /dev/null +++ b/include/rqt_demo_plugin/lineItem.hpp @@ -0,0 +1,32 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include + +class LineItem : public QQuickItem +{ + Q_OBJECT + + Q_PROPERTY(QString stupid READ stupid WRITE setStupid) + QML_ELEMENT +public: + LineItem(QQuickItem *parent = nullptr); + ~LineItem(); + + QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override; + + QString stupid() const {return stupid_;} + + void setStupid(const QString &str) { + stupid_ = str; + qDebug() << str; + } + + +private: + QString stupid_; +}; \ No newline at end of file diff --git a/include/rqt_demo_plugin/rqt_demo_plugin.hpp b/include/rqt_demo_plugin/rqt_demo_plugin.hpp index 414732c..d3180cf 100644 --- a/include/rqt_demo_plugin/rqt_demo_plugin.hpp +++ b/include/rqt_demo_plugin/rqt_demo_plugin.hpp @@ -1,5 +1,7 @@ #pragma once + #include "rqt_gui_cpp/plugin.h" + #include #include #include diff --git a/install/rqt_demo_plugin/share/colcon-core/packages/rqt_demo_plugin b/install/rqt_demo_plugin/share/colcon-core/packages/rqt_demo_plugin index 476314d..ffc2a1e 100644 --- a/install/rqt_demo_plugin/share/colcon-core/packages/rqt_demo_plugin +++ b/install/rqt_demo_plugin/share/colcon-core/packages/rqt_demo_plugin @@ -1 +1 @@ -pluginlib:rclcpp:rqt:rqt_gui_cpp \ No newline at end of file +pluginlib:rclcpp:rqt:rqt_gui_cpp:rqt_image_view \ No newline at end of file diff --git a/log/latest_build b/log/latest_build index f95b3bf..b60e059 120000 --- a/log/latest_build +++ b/log/latest_build @@ -1 +1 @@ -build_2025-03-08_17-19-32 \ No newline at end of file +build_2025-03-09_02-21-28 \ No newline at end of file diff --git a/resources/qml/helloworld.qml b/resources/qml/helloworld.qml index c577f72..17ae47b 100644 --- a/resources/qml/helloworld.qml +++ b/resources/qml/helloworld.qml @@ -1,11 +1,12 @@ import QtQuick 2.3 +import LineItem 1.0 Rectangle { color: "gray" Flickable { width: parent.width; height: parent.height - contentWidth: 2500; contentHeight: 1500 + contentWidth: 2500*4; contentHeight: 1500*4 flickDeceleration: 10000 leftMargin: Math.max((width - contentWidth)/2,0) topMargin: Math.max((height - contentHeight)/2,0) @@ -16,10 +17,10 @@ Rectangle { id: contentRect anchors.top: parent.top anchors.left: parent.left - width: parent.width - height: parent.height + width: parent.width/4 + height: parent.height/4 - transform: Scale {yScale: 1; xScale: 1} + transform: Scale {yScale: 4; xScale: 4} transformOrigin: Item.TopLeft Row { @@ -43,6 +44,10 @@ Rectangle { text: "Hello, World!" } } + LineItem { + height: 100 + width: 100 + } } } } diff --git a/src/lineItem.cpp b/src/lineItem.cpp new file mode 100644 index 0000000..b43ec81 --- /dev/null +++ b/src/lineItem.cpp @@ -0,0 +1,44 @@ +#include "rqt_demo_plugin/lineItem.hpp" +#include +#include +#include +#include +#include + +LineItem::LineItem(QQuickItem *parent) + : QQuickItem(parent) + , stupid_("") +{ + setFlag(ItemHasContents, true); +} + +LineItem::~LineItem() = default; + + +QSGNode *LineItem::updatePaintNode(QSGNode *old, UpdatePaintNodeData *) +{ + QSGGeometryNode *node = nullptr; + QSGGeometry *geometry = nullptr; + + if(!old) { + node = new QSGGeometryNode; + geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 4); + geometry->setLineWidth(5); + geometry->setDrawingMode(QSGGeometry::DrawLines); + node->setGeometry(geometry); + node->setFlag(QSGNode::OwnsGeometry); + auto *material = new QSGFlatColorMaterial; + material->setColor(QColor(255,0,0)); + node->setMaterial(material); + node->setFlag(QSGNode::OwnsMaterial); + + QSGGeometry::Point2D *verticies = geometry->vertexDataAsPoint2D(); + QSizeF s = size(); + verticies[0].set(0.1 * s.width(), 0.1 * s.height()); + verticies[1].set(0.75 * s.width(), 0.75 * s.height()); + verticies[2].set(0.1 * s.width(), 0.9 * s.height()); + verticies[3].set(0.9 * s.width(), 0.1 * s.height()); + node->markDirty(QSGNode::DirtyGeometry); + } + return node; +} \ No newline at end of file diff --git a/src/rqt_demo_plugin.cpp b/src/rqt_demo_plugin.cpp index b9a92e6..f7d998e 100644 --- a/src/rqt_demo_plugin.cpp +++ b/src/rqt_demo_plugin.cpp @@ -1,4 +1,5 @@ #include "rqt_demo_plugin/rqt_demo_plugin.hpp" +#include "rqt_demo_plugin/lineItem.hpp" #include "pluginlib/class_list_macros.hpp" #include @@ -10,4 +11,9 @@ mg::DemoPluginMg::DemoPluginMg() : Plugin() { fmt.setSwapInterval(0); fmt.setRenderableType(QSurfaceFormat::OpenGLES); QSurfaceFormat::setDefaultFormat(fmt); + static bool typesRegistered = 0; + if(!typesRegistered) { + qmlRegisterType("LineItem", 1, 0, "LineItem"); + typesRegistered = 1; + } } \ No newline at end of file