Added plugin object to qml context
This commit is contained in:
@ -2,6 +2,7 @@
|
|||||||
#include <QtQuick/QQuickItem>
|
#include <QtQuick/QQuickItem>
|
||||||
#include <qchar.h>
|
#include <qchar.h>
|
||||||
#include <qglobal.h>
|
#include <qglobal.h>
|
||||||
|
#include <qobject.h>
|
||||||
#include <qobjectdefs.h>
|
#include <qobjectdefs.h>
|
||||||
#include <qpoint.h>
|
#include <qpoint.h>
|
||||||
#include <qquickitem.h>
|
#include <qquickitem.h>
|
||||||
@ -11,7 +12,11 @@ class LineItem : public QQuickItem
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
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
|
QML_ELEMENT
|
||||||
public:
|
public:
|
||||||
LineItem(QQuickItem *parent = nullptr);
|
LineItem(QQuickItem *parent = nullptr);
|
||||||
@ -20,13 +25,21 @@ public:
|
|||||||
QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override;
|
QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override;
|
||||||
|
|
||||||
QString stupid() const {return stupid_;}
|
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:
|
private:
|
||||||
|
float scale_;
|
||||||
QString stupid_;
|
QString stupid_;
|
||||||
|
QObject* handler_;
|
||||||
|
int prev_size=0;
|
||||||
};
|
};
|
||||||
@ -6,11 +6,13 @@
|
|||||||
#include <QtQuick>
|
#include <QtQuick>
|
||||||
#include <QtWidgets>
|
#include <QtWidgets>
|
||||||
#include <qfileinfo.h>
|
#include <qfileinfo.h>
|
||||||
|
#include <qobject.h>
|
||||||
#include <qobjectdefs.h>
|
#include <qobjectdefs.h>
|
||||||
#include <qpushbutton.h>
|
#include <qpushbutton.h>
|
||||||
#include <qquickview.h>
|
#include <qquickview.h>
|
||||||
#include <qquickwindow.h>
|
#include <qquickwindow.h>
|
||||||
#include <qresource.h>
|
#include <qresource.h>
|
||||||
|
#include <qsggeometry.h>
|
||||||
#include <qurl.h>
|
#include <qurl.h>
|
||||||
#include <qwidget.h>
|
#include <qwidget.h>
|
||||||
|
|
||||||
@ -21,21 +23,48 @@ namespace mg {
|
|||||||
|
|
||||||
class DemoPluginMg : public rqt_gui_cpp::Plugin {
|
class DemoPluginMg : public rqt_gui_cpp::Plugin {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
Q_PROPERTY(QObject* demoPlugin READ demoPlugin NOTIFY demoPluginChanged)
|
||||||
public:
|
public:
|
||||||
DemoPluginMg();
|
DemoPluginMg();
|
||||||
|
|
||||||
virtual void shutdownPlugin() {
|
virtual void shutdownPlugin() {
|
||||||
cleanupResources();
|
cleanupResources();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void initPlugin(qt_gui_cpp::PluginContext &);
|
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) {
|
inline void DemoPluginMg::initPlugin(qt_gui_cpp::PluginContext &pc) {
|
||||||
initResources();
|
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);
|
qv->setResizeMode(QQuickView::ResizeMode::SizeRootObjectToView);
|
||||||
|
QSurfaceFormat format = qv->format();
|
||||||
|
format.setSamples(4);
|
||||||
|
qv->setFormat(format);
|
||||||
QWidget *qw_ = QWidget::createWindowContainer(qv);
|
QWidget *qw_ = QWidget::createWindowContainer(qv);
|
||||||
pc.addWidget(qw_);
|
pc.addWidget(qw_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}; // namespace mg
|
}; // namespace mg
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
build_2025-03-09_02-21-28
|
build_2025-03-10_12-03-53
|
||||||
@ -1,12 +1,15 @@
|
|||||||
import QtQuick 2.3
|
import QtQuick 2.3
|
||||||
import LineItem 1.0
|
import LineItem 1.0
|
||||||
|
import DemoPlugin 1.0
|
||||||
|
|
||||||
Rectangle {
|
Rectangle {
|
||||||
|
id: rr
|
||||||
color: "gray"
|
color: "gray"
|
||||||
|
property var scale: 0.4
|
||||||
Flickable {
|
Flickable {
|
||||||
width: parent.width;
|
width: parent.width;
|
||||||
height: parent.height
|
height: parent.height
|
||||||
contentWidth: 2500*4; contentHeight: 1500*4
|
contentWidth: 3000*rr.scale; contentHeight: 2000*rr.scale
|
||||||
flickDeceleration: 10000
|
flickDeceleration: 10000
|
||||||
leftMargin: Math.max((width - contentWidth)/2,0)
|
leftMargin: Math.max((width - contentWidth)/2,0)
|
||||||
topMargin: Math.max((height - contentHeight)/2,0)
|
topMargin: Math.max((height - contentHeight)/2,0)
|
||||||
@ -17,36 +20,21 @@ Rectangle {
|
|||||||
id: contentRect
|
id: contentRect
|
||||||
anchors.top: parent.top
|
anchors.top: parent.top
|
||||||
anchors.left: parent.left
|
anchors.left: parent.left
|
||||||
width: parent.width/4
|
width: parent.width/rr.scale
|
||||||
height: parent.height/4
|
height: parent.height/rr.scale
|
||||||
|
|
||||||
transform: Scale {yScale: 4; xScale: 4}
|
transform: Scale {yScale: rr.scale; xScale: rr.scale}
|
||||||
transformOrigin: Item.TopLeft
|
transformOrigin: Item.TopLeft
|
||||||
|
|
||||||
Row {
|
Row {
|
||||||
x:0
|
x:0
|
||||||
y: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 {
|
LineItem {
|
||||||
height: 100
|
height: 2000
|
||||||
width: 100
|
width: 3000
|
||||||
|
scale: rr.scale
|
||||||
|
handler: DemoPlug.demoPlugin
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,7 @@
|
|||||||
#include "rqt_demo_plugin/lineItem.hpp"
|
#include "rqt_demo_plugin/lineItem.hpp"
|
||||||
|
#include "rqt_demo_plugin/rqt_demo_plugin.hpp"
|
||||||
|
#include <qglobal.h>
|
||||||
|
#include <qqmlerror.h>
|
||||||
#include <qquickitem.h>
|
#include <qquickitem.h>
|
||||||
#include <qsgflatcolormaterial.h>
|
#include <qsgflatcolormaterial.h>
|
||||||
#include <qsggeometry.h>
|
#include <qsggeometry.h>
|
||||||
@ -7,7 +10,9 @@
|
|||||||
|
|
||||||
LineItem::LineItem(QQuickItem *parent)
|
LineItem::LineItem(QQuickItem *parent)
|
||||||
: QQuickItem(parent)
|
: QQuickItem(parent)
|
||||||
|
, scale_(1)
|
||||||
, stupid_("")
|
, stupid_("")
|
||||||
|
, handler_(nullptr)
|
||||||
{
|
{
|
||||||
setFlag(ItemHasContents, true);
|
setFlag(ItemHasContents, true);
|
||||||
}
|
}
|
||||||
@ -19,12 +24,21 @@ QSGNode *LineItem::updatePaintNode(QSGNode *old, UpdatePaintNodeData *)
|
|||||||
{
|
{
|
||||||
QSGGeometryNode *node = nullptr;
|
QSGGeometryNode *node = nullptr;
|
||||||
QSGGeometry *geometry = 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) {
|
if(!old) {
|
||||||
node = new QSGGeometryNode;
|
node = new QSGGeometryNode;
|
||||||
geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 4);
|
geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), hello_c);
|
||||||
geometry->setLineWidth(5);
|
geometry->setLineWidth(3*scale_);
|
||||||
geometry->setDrawingMode(QSGGeometry::DrawLines);
|
geometry->setDrawingMode(QSGGeometry::DrawLines);
|
||||||
|
geometry->setVertexDataPattern(QSGGeometry::DynamicPattern);
|
||||||
|
geometry->setIndexDataPattern(QSGGeometry::DynamicPattern);
|
||||||
node->setGeometry(geometry);
|
node->setGeometry(geometry);
|
||||||
node->setFlag(QSGNode::OwnsGeometry);
|
node->setFlag(QSGNode::OwnsGeometry);
|
||||||
auto *material = new QSGFlatColorMaterial;
|
auto *material = new QSGFlatColorMaterial;
|
||||||
@ -32,13 +46,26 @@ QSGNode *LineItem::updatePaintNode(QSGNode *old, UpdatePaintNodeData *)
|
|||||||
node->setMaterial(material);
|
node->setMaterial(material);
|
||||||
node->setFlag(QSGNode::OwnsMaterial);
|
node->setFlag(QSGNode::OwnsMaterial);
|
||||||
|
|
||||||
QSGGeometry::Point2D *verticies = geometry->vertexDataAsPoint2D();
|
} else {
|
||||||
QSizeF s = size();
|
node = static_cast<QSGGeometryNode *>(old);
|
||||||
verticies[0].set(0.1 * s.width(), 0.1 * s.height());
|
geometry = node->geometry();
|
||||||
verticies[1].set(0.75 * s.width(), 0.75 * s.height());
|
if(hello_c != prev_size) {
|
||||||
verticies[2].set(0.1 * s.width(), 0.9 * s.height());
|
geometry->allocate(prev_size);
|
||||||
verticies[3].set(0.9 * s.width(), 0.1 * s.height());
|
|
||||||
node->markDirty(QSGNode::DirtyGeometry);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
return node;
|
||||||
}
|
}
|
||||||
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include "pluginlib/class_list_macros.hpp"
|
#include "pluginlib/class_list_macros.hpp"
|
||||||
#include <qnamespace.h>
|
#include <qnamespace.h>
|
||||||
|
#include <qqml.h>
|
||||||
#include <qsurfaceformat.h>
|
#include <qsurfaceformat.h>
|
||||||
PLUGINLIB_EXPORT_CLASS(mg::DemoPluginMg, rqt_gui_cpp::Plugin)
|
PLUGINLIB_EXPORT_CLASS(mg::DemoPluginMg, rqt_gui_cpp::Plugin)
|
||||||
|
|
||||||
@ -14,6 +15,7 @@ mg::DemoPluginMg::DemoPluginMg() : Plugin() {
|
|||||||
static bool typesRegistered = 0;
|
static bool typesRegistered = 0;
|
||||||
if(!typesRegistered) {
|
if(!typesRegistered) {
|
||||||
qmlRegisterType<LineItem>("LineItem", 1, 0, "LineItem");
|
qmlRegisterType<LineItem>("LineItem", 1, 0, "LineItem");
|
||||||
|
qmlRegisterAnonymousType<DemoPluginMg>("DemoPlugin", 1);
|
||||||
typesRegistered = 1;
|
typesRegistered = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user