Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions src/control/controlobjectscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,7 @@ void ControlObjectScript::slotValueChanged(double value, QObject*) {
// itself. Otherwise the this may crash since the disconnect call
// happens during conn.function.call() in the middle of the loop below.
QList<ControllerEngineConnection> connections = m_connectedScriptFunctions;
for(auto&& conn: connections) {
QScriptValueList args;
args << QScriptValue(value);
args << QScriptValue(getKey().group);
args << QScriptValue(getKey().item);
QScriptValue result = conn.function.call(conn.context, args);
if (result.isError()) {
qWarning() << "ControllerEngine: Invocation of callback" << conn.id
<< "failed:" << result.toString();
}
for (auto&& conn: connections) {
conn.executeCallback(value);
}
}
26 changes: 23 additions & 3 deletions src/controllers/controllerengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,23 @@ QScriptValue ControllerEngine::connectControl(
return QScriptValue(false);
}

/* -------- ------------------------------------------------------
Purpose: Execute a ControllerEngineConnection's callback
Input: the value of the connected ControlObject to pass to the callback
-------- ------------------------------------------------------ */
void ControllerEngineConnection::executeCallback(double value) const {
QScriptValueList args;
args << QScriptValue(value);
args << QScriptValue(key.group);
args << QScriptValue(key.item);
QScriptValue func = function; // copy function because QScriptValue::call is not const
QScriptValue result = func.call(context, args);
if (result.isError()) {
qWarning() << "ControllerEngine: Invocation of callback" << id
<< "failed:" << result.toString();
}
}

/* -------- ------------------------------------------------------
Purpose: (Dis)connects a ControllerEngineConnection
Input: the ControllerEngineConnection to disconnect
Expand Down Expand Up @@ -835,13 +852,16 @@ void ControllerEngineConnectionScriptValue::disconnect() {
Input: the ControllerEngineConnection to trigger
-------- ------------------------------------------------------ */
void ControllerEngine::triggerControl(const ControllerEngineConnection conn) {
ControlObjectScript* coScript = getControlObjectScript(conn.key.group, conn.key.item);
if (m_pEngine == nullptr) {
return;
}

if (m_pEngine == nullptr || coScript == nullptr) {
ControlObjectScript* coScript = getControlObjectScript(conn.key.group, conn.key.item);
if (coScript == nullptr) {
return;
}

coScript->emitValueChanged();
conn.executeCallback(coScript->get());
}

void ControllerEngineConnectionScriptValue::trigger() {
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/controllerengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class ControllerEngineConnection {
QScriptValue function;
ControllerEngine *ce;
QScriptValue context;

void executeCallback(double value) const;
};

class ControllerEngineConnectionScriptValue : public QObject {
Expand Down
25 changes: 25 additions & 0 deletions src/test/controllerengine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,28 @@ TEST_F(ControllerEngineTest, connectControl_DisconnectByConnectionObject) {
// The counter should have been incremented exactly once.
EXPECT_DOUBLE_EQ(1.0, pass->get());
}

TEST_F(ControllerEngineTest, connectControl_TriggerByConnectionObject) {
// Test that triggering using the 'trigger' method on the connection
// object returned from connectControl works.
auto co = std::make_unique<ControlObject>(ConfigKey("[Test]", "co"));
auto counter = std::make_unique<ControlObject>(ConfigKey("[Test]", "counter"));

ScopedTemporaryFile script(makeTemporaryFile(
"var incrementCounterCO = function () {"
" var counter = engine.getValue('[Test]', 'counter');"
" engine.setValue('[Test]', 'counter', counter + 1);"
"};"
"var connection1 = engine.connectControl('[Test]', 'co', incrementCounterCO);"
// Make a second connection with the same ControlObject
// to check that triggering a connection object only triggers that callback,
// not every callback connected to its ControlObject.
"var connection2 = engine.connectControl('[Test]', 'co', incrementCounterCO);"
"connection1.trigger();"
));

cEngine->evaluate(script->fileName());
EXPECT_FALSE(cEngine->hasErrors(script->fileName()));
// The counter should have been incremented exactly once.
EXPECT_DOUBLE_EQ(1.0, counter->get());
}