-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[js] WIP on enumerating devices from js / qml
- Loading branch information
Showing
8 changed files
with
217 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/plugins/score-plugin-js/JS/Qml/DeviceEnumerator.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#include "DeviceEnumerator.hpp" | ||
|
||
#include <Device/Protocol/ProtocolFactoryInterface.hpp> | ||
#include <Device/Protocol/ProtocolList.hpp> | ||
|
||
#include <Explorer/DocumentPlugin/DeviceDocumentPlugin.hpp> | ||
|
||
#include <wobjectimpl.h> | ||
W_OBJECT_IMPL(JS::GlobalDeviceEnumerator) | ||
namespace JS | ||
{ | ||
/** | ||
var e = Score.enumerateDevices(); | ||
e.enumerate = true; | ||
console.log(e.devices); | ||
*/ | ||
|
||
GlobalDeviceEnumerator::GlobalDeviceEnumerator() { } | ||
GlobalDeviceEnumerator::~GlobalDeviceEnumerator() | ||
{ | ||
m_enumerate = false; | ||
reprocess(); | ||
} | ||
|
||
void GlobalDeviceEnumerator::setContext(const score::DocumentContext* doc) | ||
{ | ||
this->doc = doc; | ||
reprocess(); | ||
//list = &doc.plugin<Explorer::DeviceDocumentPlugin>(); | ||
} | ||
|
||
QQmlListProperty<DeviceIdentifier> GlobalDeviceEnumerator::devices() | ||
{ | ||
|
||
return QQmlListProperty<DeviceIdentifier>( | ||
this, nullptr, [](QQmlListProperty<DeviceIdentifier>* d) -> long long { | ||
qDebug() << "sz: " << ((GlobalDeviceEnumerator*)d->object)->m_raw_list.size(); | ||
return ((GlobalDeviceEnumerator*)d->object)->m_raw_list.size(); | ||
}, [](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()) | ||
return self->m_raw_list[index]; | ||
else | ||
return nullptr; | ||
}); | ||
} | ||
void GlobalDeviceEnumerator::setEnumerate(bool b) | ||
{ | ||
if(m_enumerate == b) | ||
return; | ||
|
||
m_enumerate = b; | ||
enumerateChanged(b); | ||
|
||
reprocess(); | ||
} | ||
|
||
void GlobalDeviceEnumerator::reprocess() | ||
{ | ||
for(auto& [k, v] : this->m_current_enums) | ||
{ | ||
for(auto& [ename, e] : v) | ||
delete e; | ||
} | ||
this->m_current_enums.clear(); | ||
this->m_known_devices.clear(); | ||
|
||
if(!this->doc) | ||
return; | ||
if(!this->m_enumerate) | ||
return; | ||
|
||
auto& doc = *this->doc; | ||
for(auto& protocol : doc.app.interfaces<Device::ProtocolFactoryList>()) | ||
{ | ||
auto enums = protocol.getEnumerators(doc); | ||
for(auto& [name, enumerator] : enums) | ||
{ | ||
connect( | ||
enumerator, &Device::DeviceEnumerator::deviceAdded, this, | ||
[this, | ||
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}); | ||
}, | ||
Qt::QueuedConnection); | ||
connect( | ||
enumerator, &Device::DeviceEnumerator::deviceRemoved, this, | ||
[this, proto = &protocol](const QString& name) { | ||
this->deviceRemoved(proto, name); | ||
{ | ||
auto& vec = this->m_known_devices[proto]; | ||
auto it = ossia::find_key(vec, name); | ||
if(it != vec.end()) | ||
vec.erase(it); | ||
} | ||
{ | ||
auto it = ossia::find_if(this->m_raw_list, [&](auto& di) { | ||
return di->protocol == proto && di->name == name; | ||
}); | ||
if(it != this->m_raw_list.end()) | ||
{ | ||
delete *it; | ||
this->m_raw_list.erase(it); | ||
} | ||
} | ||
}, Qt::QueuedConnection); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#pragma once | ||
|
||
#include <Device/Protocol/DeviceSettings.hpp> | ||
#include <Device/Protocol/ProtocolFactoryInterface.hpp> | ||
|
||
#include <QQmlListProperty> | ||
|
||
#include <unordered_map> | ||
#include <verdigris> | ||
|
||
namespace Explorer | ||
{ | ||
class DeviceDocumentPlugin; | ||
} | ||
namespace Device | ||
{ | ||
struct DeviceSettings; | ||
class DeviceList; | ||
class ProtocolFactory; | ||
class DeviceEnumerator; | ||
} | ||
|
||
namespace JS | ||
{ | ||
struct DeviceIdentifier | ||
{ | ||
W_GADGET(DeviceIdentifier) | ||
public: | ||
QString name; | ||
Device::DeviceSettings settings; | ||
Device::ProtocolFactory* protocol{}; | ||
}; | ||
|
||
class GlobalDeviceEnumerator : public QObject | ||
{ | ||
W_OBJECT(GlobalDeviceEnumerator) | ||
|
||
public: | ||
explicit GlobalDeviceEnumerator(); | ||
~GlobalDeviceEnumerator(); | ||
|
||
void setContext(const score::DocumentContext* doc); | ||
W_SLOT(setContext) | ||
|
||
void deviceAdded( | ||
Device::ProtocolFactory* factory, const QString& name, | ||
Device::DeviceSettings settings) W_SIGNAL(deviceAdded, factory, name, settings) | ||
void deviceRemoved(Device::ProtocolFactory* factory, const QString& name) | ||
W_SIGNAL(deviceRemoved, factory, name) | ||
// QList<Device::DeviceSettings> devices(); | ||
QQmlListProperty<JS::DeviceIdentifier> devices(); | ||
|
||
W_PROPERTY(QQmlListProperty<JS::DeviceIdentifier>, devices READ devices) | ||
|
||
bool enumerate() { return m_enumerate; } | ||
void setEnumerate(bool b); | ||
void enumerateChanged(bool b) W_SIGNAL(enumerateChanged, b) | ||
W_PROPERTY(bool, enumerate READ enumerate WRITE setEnumerate NOTIFY enumerateChanged) | ||
|
||
private: | ||
void reprocess(); | ||
const score::DocumentContext* doc{}; | ||
|
||
std::unordered_map< | ||
Device::ProtocolFactory*, std::vector<std::pair<QString, Device::DeviceSettings>>> | ||
m_known_devices; | ||
std::unordered_map<Device::ProtocolFactory*, Device::DeviceEnumerators> | ||
m_current_enums; | ||
|
||
std::vector<DeviceIdentifier*> m_raw_list; | ||
|
||
bool m_enumerate{}; | ||
}; | ||
} | ||
|
||
Q_DECLARE_METATYPE(JS::GlobalDeviceEnumerator*) | ||
W_REGISTER_ARGTYPE(Device::ProtocolFactory*) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,5 +119,4 @@ QVariantList EditJsContext::selectedObjects() | |
list.push_back(QVariant::fromValue(c.data())); | ||
return list; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters