Fix placeholder replacement for interface based rule templates
This commit is contained in:
parent
a54dd4d757
commit
5df32dad47
@ -50,6 +50,8 @@
|
|||||||
#include <QMetaEnum>
|
#include <QMetaEnum>
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
|
|
||||||
|
Q_DECLARE_LOGGING_CATEGORY(dcRuleManager)
|
||||||
|
|
||||||
RuleTemplates::RuleTemplates(QObject *parent) : QAbstractListModel(parent)
|
RuleTemplates::RuleTemplates(QObject *parent) : QAbstractListModel(parent)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -62,17 +64,17 @@ RuleTemplates::RuleTemplates(QObject *parent) : QAbstractListModel(parent)
|
|||||||
QDir ruleTemplatesDir(":/ruletemplates");
|
QDir ruleTemplatesDir(":/ruletemplates");
|
||||||
|
|
||||||
foreach (const QString &templateFile, ruleTemplatesDir.entryList({"*.json"})) {
|
foreach (const QString &templateFile, ruleTemplatesDir.entryList({"*.json"})) {
|
||||||
qDebug() << "Loading rule template:" << ruleTemplatesDir.absoluteFilePath(templateFile);
|
qCDebug(dcRuleManager()) << "Loading rule template:" << ruleTemplatesDir.absoluteFilePath(templateFile);
|
||||||
QFile f(ruleTemplatesDir.absoluteFilePath(templateFile));
|
QFile f(ruleTemplatesDir.absoluteFilePath(templateFile));
|
||||||
if (!f.open(QFile::ReadOnly)) {
|
if (!f.open(QFile::ReadOnly)) {
|
||||||
qWarning() << "Cannot open rule template file for reading:" << ruleTemplatesDir.absoluteFilePath(templateFile);
|
qCWarning(dcRuleManager()) << "Cannot open rule template file for reading:" << ruleTemplatesDir.absoluteFilePath(templateFile);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
QJsonParseError error;
|
QJsonParseError error;
|
||||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(f.readAll(), &error);
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(f.readAll(), &error);
|
||||||
f.close();
|
f.close();
|
||||||
if (error.error != QJsonParseError::NoError) {
|
if (error.error != QJsonParseError::NoError) {
|
||||||
qWarning() << "Error reading rule template json from file:" << ruleTemplatesDir.absoluteFilePath(templateFile) << error.offset << error.errorString();
|
qCWarning(dcRuleManager()) << "Error reading rule template json from file:" << ruleTemplatesDir.absoluteFilePath(templateFile) << error.offset << error.errorString();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
foreach (const QVariant &ruleTemplateVariant, jsonDoc.toVariant().toMap().value("templates").toList()) {
|
foreach (const QVariant &ruleTemplateVariant, jsonDoc.toVariant().toMap().value("templates").toList()) {
|
||||||
@ -85,7 +87,7 @@ RuleTemplates::RuleTemplates(QObject *parent) : QAbstractListModel(parent)
|
|||||||
qApp->translate(descriptionContext.toUtf8(), ruleTemplate.value("description").toByteArray()),
|
qApp->translate(descriptionContext.toUtf8(), ruleTemplate.value("description").toByteArray()),
|
||||||
qApp->translate(nameTemplateContext.toUtf8(), ruleTemplate.value("ruleNameTemplate").toByteArray()),
|
qApp->translate(nameTemplateContext.toUtf8(), ruleTemplate.value("ruleNameTemplate").toByteArray()),
|
||||||
this);
|
this);
|
||||||
qDebug() << "Loading rule template" << ruleTemplate.value("description").toString() << tr(ruleTemplate.value("description").toByteArray());
|
qCDebug(dcRuleManager()) << "Loading rule template" << ruleTemplate.value("description").toString() << tr(ruleTemplate.value("description").toByteArray());
|
||||||
|
|
||||||
// EventDescriptorTemplate
|
// EventDescriptorTemplate
|
||||||
foreach (const QVariant &eventDescriptorVariant, ruleTemplate.value("eventDescriptorTemplates").toList()) {
|
foreach (const QVariant &eventDescriptorVariant, ruleTemplate.value("eventDescriptorTemplates").toList()) {
|
||||||
@ -140,7 +142,7 @@ RuleTemplates::RuleTemplates(QObject *parent) : QAbstractListModel(parent)
|
|||||||
QString eventParamName = ruleActionParamTemplate.value("eventParamName").toString();
|
QString eventParamName = ruleActionParamTemplate.value("eventParamName").toString();
|
||||||
rapts->addRuleActionParamTemplate(new RuleActionParamTemplate(paramName, eventInterface, eventName, eventParamName));
|
rapts->addRuleActionParamTemplate(new RuleActionParamTemplate(paramName, eventInterface, eventName, eventParamName));
|
||||||
} else {
|
} else {
|
||||||
qWarning() << "Invalid rule action param name on rule template:" << paramName;
|
qCWarning(dcRuleManager()) << "Invalid rule action param name on rule template:" << paramName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
QMetaEnum selectionModeEnum = QMetaEnum::fromType<RuleActionTemplate::SelectionMode>();
|
QMetaEnum selectionModeEnum = QMetaEnum::fromType<RuleActionTemplate::SelectionMode>();
|
||||||
@ -169,7 +171,7 @@ RuleTemplates::RuleTemplates(QObject *parent) : QAbstractListModel(parent)
|
|||||||
QString eventParamName = ruleActionParamTemplate.value("eventParamName").toString();
|
QString eventParamName = ruleActionParamTemplate.value("eventParamName").toString();
|
||||||
rapts->addRuleActionParamTemplate(new RuleActionParamTemplate(paramName, eventInterface, eventName, eventParamName));
|
rapts->addRuleActionParamTemplate(new RuleActionParamTemplate(paramName, eventInterface, eventName, eventParamName));
|
||||||
} else {
|
} else {
|
||||||
qWarning() << "Invalid rule exit action param name on rule template:" << paramName;
|
qCWarning(dcRuleManager()) << "Invalid rule exit action param name on rule template:" << paramName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
QMetaEnum selectionModeEnum = QMetaEnum::fromType<RuleActionTemplate::SelectionMode>();
|
QMetaEnum selectionModeEnum = QMetaEnum::fromType<RuleActionTemplate::SelectionMode>();
|
||||||
@ -184,7 +186,7 @@ RuleTemplates::RuleTemplates(QObject *parent) : QAbstractListModel(parent)
|
|||||||
|
|
||||||
m_list.append(t);
|
m_list.append(t);
|
||||||
}
|
}
|
||||||
// qDebug() << "Loaded" << m_list.count() << "rule templates";
|
qCDebug(dcRuleManager()) << "Loaded" << m_list.count() << "rule templates";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -286,6 +288,20 @@ RepeatingOption *RuleTemplates::loadRepeatingOption(const QVariantMap &repeating
|
|||||||
return repeatingOption;
|
return repeatingOption;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RuleTemplatesFilterModel::setFilterByThings(ThingsProxy *filterThingsProxy)
|
||||||
|
{
|
||||||
|
if (m_filterThingsProxy != filterThingsProxy) {
|
||||||
|
m_filterThingsProxy = filterThingsProxy;
|
||||||
|
emit filterByThingsChanged(); invalidateFilter();
|
||||||
|
|
||||||
|
qCDebug(dcRuleManager()) << "Setting things proxy:" << filterThingsProxy->rowCount();
|
||||||
|
connect(m_filterThingsProxy, &ThingsProxy::countChanged, this, [this](){
|
||||||
|
qCDebug(dcRuleManager()) << "proxy count hcanged";
|
||||||
|
invalidateFilter();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool RuleTemplatesFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
|
bool RuleTemplatesFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
|
||||||
{
|
{
|
||||||
Q_UNUSED(source_parent)
|
Q_UNUSED(source_parent)
|
||||||
@ -347,7 +363,7 @@ bool RuleTemplatesFilterModel::thingsSatisfyRuleTemplate(RuleTemplate *ruleTempl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!haveThing) {
|
if (!haveThing) {
|
||||||
qDebug() << "No thing to satisfy interface" << interfaceName;
|
qCDebug(dcRuleManager()) << "No thing to satisfy interface" << interfaceName << things->rowCount();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -364,13 +380,13 @@ bool RuleTemplatesFilterModel::thingsSatisfyRuleTemplate(RuleTemplate *ruleTempl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!haveThing) {
|
if (!haveThing) {
|
||||||
qDebug() << "No thing to satisfy event" << eventDescriptorTemplate->eventName();
|
qCDebug(dcRuleManager()) << "No thing to satisfy event" << eventDescriptorTemplate->eventName();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ruleTemplate->stateEvaluatorTemplate() && !thingsSatisfyStateEvaluatorTemplate(ruleTemplate->stateEvaluatorTemplate(), things)) {
|
if (ruleTemplate->stateEvaluatorTemplate() && !thingsSatisfyStateEvaluatorTemplate(ruleTemplate->stateEvaluatorTemplate(), things)) {
|
||||||
qDebug() << "No thing to satisfy state evaluator template";
|
qCDebug(dcRuleManager()) << "No thing to satisfy state evaluator template";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -385,7 +401,7 @@ bool RuleTemplatesFilterModel::thingsSatisfyRuleTemplate(RuleTemplate *ruleTempl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!haveThing) {
|
if (!haveThing) {
|
||||||
qDebug() << "No thing to satisfy action" << ruleActionTemplate->actionName();
|
qCDebug(dcRuleManager()) << "No thing to satisfy action" << ruleActionTemplate->actionName();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -401,7 +417,7 @@ bool RuleTemplatesFilterModel::thingsSatisfyRuleTemplate(RuleTemplate *ruleTempl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!haveThing) {
|
if (!haveThing) {
|
||||||
qDebug() << "No thing to satisfy exit action" << ruleExitActionTemplate->actionName();
|
qCDebug(dcRuleManager()) << "No thing to satisfy exit action" << ruleExitActionTemplate->actionName();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -88,7 +88,7 @@ public:
|
|||||||
QStringList filterInterfaceNames() const { return m_filterInterfaceNames; }
|
QStringList filterInterfaceNames() const { return m_filterInterfaceNames; }
|
||||||
void setFilterInterfaceNames(const QStringList &filterInterfaceNames) { if (m_filterInterfaceNames != filterInterfaceNames) { m_filterInterfaceNames = filterInterfaceNames; emit filterInterfaceNamesChanged(); invalidateFilter(); emit countChanged(); }}
|
void setFilterInterfaceNames(const QStringList &filterInterfaceNames) { if (m_filterInterfaceNames != filterInterfaceNames) { m_filterInterfaceNames = filterInterfaceNames; emit filterInterfaceNamesChanged(); invalidateFilter(); emit countChanged(); }}
|
||||||
ThingsProxy* filterByThings() const { return m_filterThingsProxy; }
|
ThingsProxy* filterByThings() const { return m_filterThingsProxy; }
|
||||||
void setFilterByThings(ThingsProxy* filterThingsProxy) {if (m_filterThingsProxy != filterThingsProxy) { m_filterThingsProxy = filterThingsProxy; emit filterByThingsChanged(); invalidateFilter(); }}
|
void setFilterByThings(ThingsProxy* filterThingsProxy);
|
||||||
Q_INVOKABLE RuleTemplate* get(int index) {
|
Q_INVOKABLE RuleTemplate* get(int index) {
|
||||||
if (index < 0 || index >= rowCount()) {
|
if (index < 0 || index >= rowCount()) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|||||||
@ -63,6 +63,7 @@ void ThingsProxy::setEngine(Engine *engine)
|
|||||||
|
|
||||||
setSortRole(Things::RoleName);
|
setSortRole(Things::RoleName);
|
||||||
sort(0, sortOrder());
|
sort(0, sortOrder());
|
||||||
|
emit countChanged();
|
||||||
connect(sourceModel(), SIGNAL(countChanged()), this, SIGNAL(countChanged()));
|
connect(sourceModel(), SIGNAL(countChanged()), this, SIGNAL(countChanged()));
|
||||||
connect(sourceModel(), &QAbstractItemModel::dataChanged, this, [this]() {
|
connect(sourceModel(), &QAbstractItemModel::dataChanged, this, [this]() {
|
||||||
// Only invalidate the filter if we're actually interested in state changes
|
// Only invalidate the filter if we're actually interested in state changes
|
||||||
|
|||||||
@ -59,10 +59,14 @@ Page {
|
|||||||
RuleTemplatesFilterModel {
|
RuleTemplatesFilterModel {
|
||||||
id: ruleTemplatesModel
|
id: ruleTemplatesModel
|
||||||
ruleTemplates: RuleTemplates {}
|
ruleTemplates: RuleTemplates {}
|
||||||
filterByThings: ThingsProxy { engine: _engine }
|
filterByThings: ThingsProxy {
|
||||||
|
id: templatesThingsProxy
|
||||||
|
engine: _engine
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function addRule() {
|
function addRule() {
|
||||||
|
print("ruletemplates:", ruleTemplatesModel.count, templatesThingsProxy.count)
|
||||||
if (ruleTemplatesModel.count > 0) {
|
if (ruleTemplatesModel.count > 0) {
|
||||||
d.editRulePage = pageStack.push(Qt.resolvedUrl("magic/NewThingMagicPage.qml"))
|
d.editRulePage = pageStack.push(Qt.resolvedUrl("magic/NewThingMagicPage.qml"))
|
||||||
d.editRulePage.done.connect(function() {
|
d.editRulePage.done.connect(function() {
|
||||||
|
|||||||
@ -295,6 +295,25 @@ Page {
|
|||||||
}
|
}
|
||||||
for (selectionId in selectedInterfaces) {
|
for (selectionId in selectedInterfaces) {
|
||||||
rule.name = rule.name.replace("%" + selectionId, qsTr("any " + app.interfaceToDisplayName(selectedInterfaces[selectionId])))
|
rule.name = rule.name.replace("%" + selectionId, qsTr("any " + app.interfaceToDisplayName(selectedInterfaces[selectionId])))
|
||||||
|
for (var j = 0; j < rule.actions.count; j++) {
|
||||||
|
var action = rule.actions.get(j);
|
||||||
|
for(var k = 0; k < action.ruleActionParams.count; k++) {
|
||||||
|
var actionParam = action.ruleActionParams.get(k);
|
||||||
|
print("replacing args", typeof actionParam.value)
|
||||||
|
if (typeof actionParam.value === "string") {
|
||||||
|
actionParam.value = actionParam.value.replace("%" + selectionId, qsTr("A thing"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (var j = 0; j < rule.exitActions.count; j++) {
|
||||||
|
var action = rule.exitActions.get(j);
|
||||||
|
for(var k = 0; k < action.ruleActionParams.count; k++) {
|
||||||
|
var actionParam = action.ruleActionParams.get(k);
|
||||||
|
if (typeof actionParam.value === "string") {
|
||||||
|
actionParam.value = actionParam.value.replace("%" + selectionId, qsTr("A thing"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
print("Rule complete!")
|
print("Rule complete!")
|
||||||
|
|||||||
Reference in New Issue
Block a user