Added plugin object to qml context

This commit is contained in:
2025-03-10 12:04:23 +01:00
parent 4c4d85a78d
commit a170490cbb
6 changed files with 99 additions and 40 deletions

View File

@ -2,6 +2,7 @@
#include <QtQuick/QQuickItem>
#include <qchar.h>
#include <qglobal.h>
#include <qobject.h>
#include <qobjectdefs.h>
#include <qpoint.h>
#include <qquickitem.h>
@ -11,7 +12,11 @@ class LineItem : public QQuickItem
{
Q_OBJECT
Q_PROPERTY(QString stupid READ stupid WRITE setStupid)
Q_PROPERTY(float scale READ scale WRITE setScale NOTIFY scaleChanged)
Q_PROPERTY(QString stupid READ stupid WRITE setStupid NOTIFY stupidChanged)
Q_PROPERTY(QObject* handler READ handler WRITE setHandler NOTIFY handlerChanged)
QML_ELEMENT
public:
LineItem(QQuickItem *parent = nullptr);
@ -20,13 +25,21 @@ public:
QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override;
QString stupid() const {return stupid_;}
QObject* handler() const {return handler_;}
void setStupid(const QString &str) {stupid_ = str;}
void setScale(const float scale) {scale_ = scale;}
void setHandler(QObject* const obj) {handler_ = obj;}
signals:
void stupidChanged(const QString &str);
void handlerChanged(QObject* const obj);
void setStupid(const QString &str) {
stupid_ = str;
qDebug() << str;
}
private:
float scale_;
QString stupid_;
QObject* handler_;
int prev_size=0;
};

View File

@ -6,11 +6,13 @@
#include <QtQuick>
#include <QtWidgets>
#include <qfileinfo.h>
#include <qobject.h>
#include <qobjectdefs.h>
#include <qpushbutton.h>
#include <qquickview.h>
#include <qquickwindow.h>
#include <qresource.h>
#include <qsggeometry.h>
#include <qurl.h>
#include <qwidget.h>
@ -21,21 +23,48 @@ namespace mg {
class DemoPluginMg : public rqt_gui_cpp::Plugin {
Q_OBJECT
Q_PROPERTY(QObject* demoPlugin READ demoPlugin NOTIFY demoPluginChanged)
public:
DemoPluginMg();
virtual void shutdownPlugin() {
cleanupResources();
}
virtual void initPlugin(qt_gui_cpp::PluginContext &);
QObject * demoPlugin() const {
return (QObject *)(this);
}
const QSGGeometry::Point2D hello[8] = {
{0.2,0.2}, {0.8,0.2},
{0.2,0.2}, {0.2,0.8},
{0.2,0.4}, {0.4,0.8},
{0.2,0.4}, {0.1,0.1},
};
const int hello_c = 8;
signals:
void demoPluginChanged(QObject * const);
};
inline void DemoPluginMg::initPlugin(qt_gui_cpp::PluginContext &pc) {
initResources();
QQuickView *qv = new QQuickView(QUrl("qrc:/qml/helloworld.qml"));
const QUrl qrl("qrc:/qml/helloworld.qml");
QQuickView *qv = new QQuickView();
qv->rootContext()->setContextProperty("DemoPlug", this);
qv->setSource(qrl);
qv->setResizeMode(QQuickView::ResizeMode::SizeRootObjectToView);
QSurfaceFormat format = qv->format();
format.setSamples(4);
qv->setFormat(format);
QWidget *qw_ = QWidget::createWindowContainer(qv);
pc.addWidget(qw_);
}
}; // namespace mg

View File

@ -1 +1 @@
build_2025-03-09_02-21-28
build_2025-03-10_12-03-53

View File

@ -1,12 +1,15 @@
import QtQuick 2.3
import LineItem 1.0
import DemoPlugin 1.0
Rectangle {
id: rr
color: "gray"
property var scale: 0.4
Flickable {
width: parent.width;
height: parent.height
contentWidth: 2500*4; contentHeight: 1500*4
contentWidth: 3000*rr.scale; contentHeight: 2000*rr.scale
flickDeceleration: 10000
leftMargin: Math.max((width - contentWidth)/2,0)
topMargin: Math.max((height - contentHeight)/2,0)
@ -17,36 +20,21 @@ Rectangle {
id: contentRect
anchors.top: parent.top
anchors.left: parent.left
width: parent.width/4
height: parent.height/4
width: parent.width/rr.scale
height: parent.height/rr.scale
transform: Scale {yScale: 4; xScale: 4}
transform: Scale {yScale: rr.scale; xScale: rr.scale}
transformOrigin: Item.TopLeft
Row {
x:0
y:0
spacing: 4
Rectangle {
height: 100
width: 100
color: "green"
Text {
text: "Hello, World!"
}
}
Rectangle {
height: 100
width: 100
color: "red"
Text {
text: "Hello, World!"
}
}
LineItem {
height: 100
width: 100
height: 2000
width: 3000
scale: rr.scale
handler: DemoPlug.demoPlugin
}
}
}

View File

@ -1,4 +1,7 @@
#include "rqt_demo_plugin/lineItem.hpp"
#include "rqt_demo_plugin/rqt_demo_plugin.hpp"
#include <qglobal.h>
#include <qqmlerror.h>
#include <qquickitem.h>
#include <qsgflatcolormaterial.h>
#include <qsggeometry.h>
@ -7,7 +10,9 @@
LineItem::LineItem(QQuickItem *parent)
: QQuickItem(parent)
, stupid_("")
, scale_(1)
, stupid_("")
, handler_(nullptr)
{
setFlag(ItemHasContents, true);
}
@ -19,12 +24,21 @@ QSGNode *LineItem::updatePaintNode(QSGNode *old, UpdatePaintNodeData *)
{
QSGGeometryNode *node = nullptr;
QSGGeometry *geometry = nullptr;
mg::DemoPluginMg *plugin = static_cast<mg::DemoPluginMg *>(handler_);
if(!plugin) {
qmlEngine(this)->throwError("Handler plugin was not provided");
return old;
}
const int hello_c = plugin->hello_c;
if(!old) {
node = new QSGGeometryNode;
geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 4);
geometry->setLineWidth(5);
geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), hello_c);
geometry->setLineWidth(3*scale_);
geometry->setDrawingMode(QSGGeometry::DrawLines);
geometry->setVertexDataPattern(QSGGeometry::DynamicPattern);
geometry->setIndexDataPattern(QSGGeometry::DynamicPattern);
node->setGeometry(geometry);
node->setFlag(QSGNode::OwnsGeometry);
auto *material = new QSGFlatColorMaterial;
@ -32,13 +46,26 @@ QSGNode *LineItem::updatePaintNode(QSGNode *old, UpdatePaintNodeData *)
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);
} else {
node = static_cast<QSGGeometryNode *>(old);
geometry = node->geometry();
if(hello_c != prev_size) {
geometry->allocate(prev_size);
}
}
if(hello_c != prev_size) {
const QSizeF s = size();
QSGGeometry::Point2D *verticies = geometry->vertexDataAsPoint2D();
for(int i = 0; i<hello_c; i++) {
verticies[i].x = plugin->hello[i].x * s.width();
verticies[i].y = plugin->hello[i].y * s.height();
}
geometry->markVertexDataDirty();
node->markDirty(QSGNode::DirtyGeometry);
prev_size = hello_c;
}
return node;
}

View File

@ -3,6 +3,7 @@
#include "pluginlib/class_list_macros.hpp"
#include <qnamespace.h>
#include <qqml.h>
#include <qsurfaceformat.h>
PLUGINLIB_EXPORT_CLASS(mg::DemoPluginMg, rqt_gui_cpp::Plugin)
@ -14,6 +15,7 @@ mg::DemoPluginMg::DemoPluginMg() : Plugin() {
static bool typesRegistered = 0;
if(!typesRegistered) {
qmlRegisterType<LineItem>("LineItem", 1, 0, "LineItem");
qmlRegisterAnonymousType<DemoPluginMg>("DemoPlugin", 1);
typesRegistered = 1;
}
}