diff --git a/src/plugins/score-plugin-js/JS/Qml/DeviceEnumerator.cpp b/src/plugins/score-plugin-js/JS/Qml/DeviceEnumerator.cpp index 13cfef5199..cc3669a2bd 100644 --- a/src/plugins/score-plugin-js/JS/Qml/DeviceEnumerator.cpp +++ b/src/plugins/score-plugin-js/JS/Qml/DeviceEnumerator.cpp @@ -9,6 +9,7 @@ #include W_OBJECT_IMPL(JS::GlobalDeviceEnumerator) +W_OBJECT_IMPL(JS::DeviceIdentifier) namespace JS { /** @@ -33,20 +34,24 @@ void GlobalDeviceEnumerator::setContext(const score::DocumentContext* doc) QQmlListProperty GlobalDeviceEnumerator::devices() { + using CountFunction = QQmlListProperty::CountFunction; + using AtFunction = QQmlListProperty::AtFunction; - return QQmlListProperty( - this, nullptr, [](QQmlListProperty* d) -> long long { - qDebug() << "sz: " << ((GlobalDeviceEnumerator*)d->object)->m_raw_list.size(); + CountFunction count = +[](QQmlListProperty* d) -> qsizetype { return ((GlobalDeviceEnumerator*)d->object)->m_raw_list.size(); - }, [](QQmlListProperty* d, qsizetype index) -> DeviceIdentifier* { + }; + AtFunction at = +[](QQmlListProperty* 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(this, nullptr, count, at); } + void GlobalDeviceEnumerator::setEnumerate(bool b) { if(m_enumerate == b) @@ -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; @@ -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( diff --git a/src/plugins/score-plugin-js/JS/Qml/DeviceEnumerator.hpp b/src/plugins/score-plugin-js/JS/Qml/DeviceEnumerator.hpp index 2b6aa5df90..2754c98494 100644 --- a/src/plugins/score-plugin-js/JS/Qml/DeviceEnumerator.hpp +++ b/src/plugins/score-plugin-js/JS/Qml/DeviceEnumerator.hpp @@ -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