Skip to content

Commit

Permalink
Prepping QML/CA UI factory
Browse files Browse the repository at this point in the history
  • Loading branch information
malachib committed Jul 17, 2024
1 parent 2b45d3b commit 6b3174a
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 7 deletions.
1 change: 1 addition & 0 deletions test/qt/lib/CAContainer.qml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ GridLayout {
id: root
// List of 'ControllerApplication'
property var model
required property Runtime runtime

columns: 2
flow: GridLayout.TopToBottom
Expand Down
4 changes: 3 additions & 1 deletion test/qt/lib/include/j1939/qt/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
#include <j1939/NAME/industry_groups.h>
#include <j1939/pgn/enum.h>

#include "runtime.h"

namespace embr::j1939::qt { inline namespace v1 {

class Plugin
{
public:
static void init();
static void init(Runtime*);
};

// Guidance from
Expand Down
11 changes: 9 additions & 2 deletions test/qt/lib/include/j1939/qt/runtime.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <QHash>
#include <QObject>
#include <QQmlEngine>
#include <QQuickItem>
Expand All @@ -9,14 +10,20 @@ namespace embr::j1939::qt { inline namespace v1 {

class QmlFactory : public QObject
{
QHash<const QMetaObject*, QQmlComponent*> mapping_;
QQmlEngine* engine_;

Q_OBJECT

public:
QmlFactory(QQmlEngine* parent = nullptr) : QObject(parent) {}
QmlFactory(QQmlEngine* parent) :
QObject(parent),
engine_(parent)
{}

Q_INVOKABLE QQuickItem* create(const QObject*);

Q_INVOKABLE QQuickItem* create();
void map(const QMetaObject* key, const QString& qmlFile);
};

class Runtime : public QObject
Expand Down
3 changes: 3 additions & 0 deletions test/qt/lib/include/j1939/qt/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <QObject>
#include <QCanBusDevice>
#include <QQuickItem>

#include "cs/generic.h"
#include "cs/network.h"
Expand Down Expand Up @@ -48,6 +49,8 @@ class Session : public QObject
QList<cs_type>& clients() { return css_; }
QList<const QObject*> frameLog() const { return frameLog_; }

Q_INVOKABLE QQuickItem* createQmlFromCa(QObject*);

signals:
void frameLogChanged();
};
Expand Down
3 changes: 2 additions & 1 deletion test/qt/lib/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

namespace embr::j1939::qt { inline namespace v1 {

void Plugin::init()
void Plugin::init(Runtime* runtime)
{
qmlRegisterType<embr::j1939::qt::v1::Runtime>("j1939", 1, 0, "Runtime");
qmlRegisterType<embr::j1939::qt::DataField>("j1939", 1, 0, "DataField");
qmlRegisterType<embr::j1939::qt::v1::Pdu>("j1939", 1, 0, "Pdu");
// DEBT: Unclear what the major difference between this and qmlRegisterInterface is
Expand Down
23 changes: 22 additions & 1 deletion test/qt/lib/runtime.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
#include <QQmlContext>

#include "j1939/qt/runtime.h"

// Guidance from
// https://stackoverflow.com/questions/66618613/qml-c-classes-with-bring-your-own-component

namespace embr::j1939::qt { inline namespace v1 {

QQuickItem* QmlFactory::create()
QQuickItem* QmlFactory::create(const QObject* o)
{
const QMetaObject* meta = o->metaObject();
auto i = mapping_.find(meta);

if(i != mapping_.end())
{
QQmlContext* context = new QQmlContext(engine_);
QObject* created = i.value()->create(context);
auto item = dynamic_cast<QQuickItem*>(created);
return item;
}

return nullptr;
//return {};
}

void QmlFactory::map(const QMetaObject* key, const QString& qmlFile)
{
// DEBT: Does this auto-free if reassigned?
QUrl url = QUrl::fromLocalFile(qmlFile);
auto component = new QQmlComponent(engine_, url, this);
mapping_[key] = component;
}

}}
6 changes: 6 additions & 0 deletions test/qt/lib/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,10 @@ void Session::setDevice(QCanBusDevice* device)
}); */
}


QQuickItem* Session::createQmlFromCa(QObject* o)
{
return runtime_->caQmlFactory()->create(o);
}

}}
1 change: 1 addition & 0 deletions test/qt/oel/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Window {

CAContainer {
model: Session.clients
runtime: Session.runtime
}

RowLayout {
Expand Down
9 changes: 7 additions & 2 deletions test/qt/oel/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ int main(int argc, char *argv[])
Qt::QueuedConnection
);

j1939::qt::Plugin::init();

auto runtime = new j1939::qt::Runtime(&engine);

j1939::qt::Plugin::init(runtime);

auto session = new j1939::qt::Session(runtime);

auto oel = new j1939::qt::ca::OEL(session);
Expand All @@ -41,6 +42,10 @@ int main(int argc, char *argv[])
session->clients().push_back(lcmd);
session->clients().push_back(ccvs);


runtime->caQmlFactory()->map(
&j1939::qt::ca::v1::CCVS::staticMetaObject, "oel/CCVS.qml");

qmlRegisterSingletonInstance("j1939", 1, 0, "Session", session);

engine.rootContext()->setContextObject(new embr::j1939::qt::v1::API(&app));
Expand Down

0 comments on commit 6b3174a

Please sign in to comment.