Skip to content

Commit

Permalink
js: Finish device enumeration implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Nov 23, 2024
1 parent d932658 commit 8f2b6ff
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
25 changes: 16 additions & 9 deletions src/plugins/score-plugin-js/JS/Qml/DeviceEnumerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <wobjectimpl.h>
W_OBJECT_IMPL(JS::GlobalDeviceEnumerator)
W_OBJECT_IMPL(JS::DeviceIdentifier)
namespace JS
{
/**
Expand All @@ -33,20 +34,24 @@ void GlobalDeviceEnumerator::setContext(const score::DocumentContext* doc)

QQmlListProperty<DeviceIdentifier> GlobalDeviceEnumerator::devices()
{
using CountFunction = QQmlListProperty<DeviceIdentifier>::CountFunction;
using AtFunction = QQmlListProperty<DeviceIdentifier>::AtFunction;

return QQmlListProperty<DeviceIdentifier>(
this, nullptr, [](QQmlListProperty<DeviceIdentifier>* d) -> long long {
qDebug() << "sz: " << ((GlobalDeviceEnumerator*)d->object)->m_raw_list.size();
CountFunction count = +[](QQmlListProperty<DeviceIdentifier>* d) -> qsizetype {
return ((GlobalDeviceEnumerator*)d->object)->m_raw_list.size();
}, [](QQmlListProperty<DeviceIdentifier>* d, qsizetype index) -> DeviceIdentifier* {
};
AtFunction at = +[](QQmlListProperty<DeviceIdentifier>* d,
qsizetype index) -> DeviceIdentifier* {
auto self = (GlobalDeviceEnumerator*)d->object;
qDebug() << "sz: " << self->m_raw_list.size() << " => " << index;
if(index >= 0 && index < self->m_raw_list.size())
if(index >= 0 && index < std::ssize(self->m_raw_list))
return self->m_raw_list[index];
else
return nullptr;
});
};

return QQmlListProperty<DeviceIdentifier>(this, nullptr, count, at);
}

void GlobalDeviceEnumerator::setEnumerate(bool b)
{
if(m_enumerate == b)
Expand All @@ -67,6 +72,9 @@ void GlobalDeviceEnumerator::reprocess()
}
this->m_current_enums.clear();
this->m_known_devices.clear();
for(auto d : m_raw_list)
delete d;
m_raw_list.clear();

if(!this->doc)
return;
Expand All @@ -85,8 +93,7 @@ void GlobalDeviceEnumerator::reprocess()
proto = &protocol](const QString& name, const Device::DeviceSettings& devs) {
this->deviceAdded(proto, name, devs);
this->m_known_devices[proto].emplace_back(name, devs);
m_raw_list.push_back(
new DeviceIdentifier{.name = name, .settings = devs, .protocol = proto});
m_raw_list.push_back(new DeviceIdentifier{name, devs, proto});
},
Qt::QueuedConnection);
connect(
Expand Down
20 changes: 14 additions & 6 deletions src/plugins/score-plugin-js/JS/Qml/DeviceEnumerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,24 @@ class DeviceEnumerator;

namespace JS
{
struct DeviceIdentifier
struct DeviceIdentifier : public QObject
{
W_GADGET(DeviceIdentifier)
W_OBJECT(DeviceIdentifier)
public:
QString name{};
Device::DeviceSettings settings{};
explicit DeviceIdentifier(
QString a, Device::DeviceSettings b, Device::ProtocolFactory* c)
: name{std::move(a)}
, settings{std::move(b)}
, protocol{c}
{
}
QString name;
Device::DeviceSettings settings;
Device::ProtocolFactory* protocol{};

W_PROPERTY(QString, MEMBER name)
W_PROPERTY(Device::DeviceSettings, MEMBER settings)
W_PROPERTY(QString, name MEMBER name)
W_PROPERTY(Device::DeviceSettings, settings MEMBER settings)
W_PROPERTY(Device::ProtocolFactory*, protocol MEMBER protocol)
};

class GlobalDeviceEnumerator : public QObject
Expand Down

0 comments on commit 8f2b6ff

Please sign in to comment.