Fix placeholder replacement for interface based rule templates

pull/892/head
Michael Zanetti 2022-09-24 13:27:15 +02:00
parent a54dd4d757
commit 5df32dad47
5 changed files with 54 additions and 14 deletions

View File

@ -50,6 +50,8 @@
#include <QMetaEnum>
#include <QCoreApplication>
Q_DECLARE_LOGGING_CATEGORY(dcRuleManager)
RuleTemplates::RuleTemplates(QObject *parent) : QAbstractListModel(parent)
{
@ -62,17 +64,17 @@ RuleTemplates::RuleTemplates(QObject *parent) : QAbstractListModel(parent)
QDir ruleTemplatesDir(":/ruletemplates");
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));
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;
}
QJsonParseError error;
QJsonDocument jsonDoc = QJsonDocument::fromJson(f.readAll(), &error);
f.close();
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;
}
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(nameTemplateContext.toUtf8(), ruleTemplate.value("ruleNameTemplate").toByteArray()),
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
foreach (const QVariant &eventDescriptorVariant, ruleTemplate.value("eventDescriptorTemplates").toList()) {
@ -140,7 +142,7 @@ RuleTemplates::RuleTemplates(QObject *parent) : QAbstractListModel(parent)
QString eventParamName = ruleActionParamTemplate.value("eventParamName").toString();
rapts->addRuleActionParamTemplate(new RuleActionParamTemplate(paramName, eventInterface, eventName, eventParamName));
} 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>();
@ -169,7 +171,7 @@ RuleTemplates::RuleTemplates(QObject *parent) : QAbstractListModel(parent)
QString eventParamName = ruleActionParamTemplate.value("eventParamName").toString();
rapts->addRuleActionParamTemplate(new RuleActionParamTemplate(paramName, eventInterface, eventName, eventParamName));
} 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>();
@ -184,7 +186,7 @@ RuleTemplates::RuleTemplates(QObject *parent) : QAbstractListModel(parent)
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;
}
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
{
Q_UNUSED(source_parent)
@ -347,7 +363,7 @@ bool RuleTemplatesFilterModel::thingsSatisfyRuleTemplate(RuleTemplate *ruleTempl
}
}
if (!haveThing) {
qDebug() << "No thing to satisfy interface" << interfaceName;
qCDebug(dcRuleManager()) << "No thing to satisfy interface" << interfaceName << things->rowCount();
return false;
}
}
@ -364,13 +380,13 @@ bool RuleTemplatesFilterModel::thingsSatisfyRuleTemplate(RuleTemplate *ruleTempl
}
}
if (!haveThing) {
qDebug() << "No thing to satisfy event" << eventDescriptorTemplate->eventName();
qCDebug(dcRuleManager()) << "No thing to satisfy event" << eventDescriptorTemplate->eventName();
return false;
}
}
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;
}
@ -385,7 +401,7 @@ bool RuleTemplatesFilterModel::thingsSatisfyRuleTemplate(RuleTemplate *ruleTempl
}
}
if (!haveThing) {
qDebug() << "No thing to satisfy action" << ruleActionTemplate->actionName();
qCDebug(dcRuleManager()) << "No thing to satisfy action" << ruleActionTemplate->actionName();
return false;
}
}
@ -401,7 +417,7 @@ bool RuleTemplatesFilterModel::thingsSatisfyRuleTemplate(RuleTemplate *ruleTempl
}
}
if (!haveThing) {
qDebug() << "No thing to satisfy exit action" << ruleExitActionTemplate->actionName();
qCDebug(dcRuleManager()) << "No thing to satisfy exit action" << ruleExitActionTemplate->actionName();
return false;
}
}

View File

@ -88,7 +88,7 @@ public:
QStringList filterInterfaceNames() const { return m_filterInterfaceNames; }
void setFilterInterfaceNames(const QStringList &filterInterfaceNames) { if (m_filterInterfaceNames != filterInterfaceNames) { m_filterInterfaceNames = filterInterfaceNames; emit filterInterfaceNamesChanged(); invalidateFilter(); emit countChanged(); }}
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) {
if (index < 0 || index >= rowCount()) {
return nullptr;

View File

@ -63,6 +63,7 @@ void ThingsProxy::setEngine(Engine *engine)
setSortRole(Things::RoleName);
sort(0, sortOrder());
emit countChanged();
connect(sourceModel(), SIGNAL(countChanged()), this, SIGNAL(countChanged()));
connect(sourceModel(), &QAbstractItemModel::dataChanged, this, [this]() {
// Only invalidate the filter if we're actually interested in state changes

View File

@ -59,10 +59,14 @@ Page {
RuleTemplatesFilterModel {
id: ruleTemplatesModel
ruleTemplates: RuleTemplates {}
filterByThings: ThingsProxy { engine: _engine }
filterByThings: ThingsProxy {
id: templatesThingsProxy
engine: _engine
}
}
function addRule() {
print("ruletemplates:", ruleTemplatesModel.count, templatesThingsProxy.count)
if (ruleTemplatesModel.count > 0) {
d.editRulePage = pageStack.push(Qt.resolvedUrl("magic/NewThingMagicPage.qml"))
d.editRulePage.done.connect(function() {

View File

@ -295,6 +295,25 @@ Page {
}
for (selectionId in selectedInterfaces) {
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!")