diff options
author | Be <be@mixxx.org> | 2020-05-21 23:14:13 -0500 |
---|---|---|
committer | Be <be@mixxx.org> | 2020-05-27 08:56:04 -0500 |
commit | 68b6330f5b7c297a687d0b861cdef8d0fd097449 (patch) | |
tree | fae8c48758e73b615f3e7d29ce2870b5dd177d58 | |
parent | ec1d9aa4988e5d2b640e8a13701b3e644d31f51b (diff) |
move ScriptConnection & ScriptConnectionJSProxy to their own files
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | build/depends.py | 2 | ||||
-rw-r--r-- | src/control/controlobjectscript.h | 2 | ||||
-rw-r--r-- | src/controllers/engine/controllerengine.cpp | 45 | ||||
-rw-r--r-- | src/controllers/engine/controllerengine.h | 56 | ||||
-rw-r--r-- | src/controllers/engine/scriptconnection.cpp | 20 | ||||
-rw-r--r-- | src/controllers/engine/scriptconnection.h | 29 | ||||
-rw-r--r-- | src/controllers/engine/scriptconnectionjsproxy.cpp | 14 | ||||
-rw-r--r-- | src/controllers/engine/scriptconnectionjsproxy.h | 31 |
9 files changed, 109 insertions, 92 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index a828503afa..4163d2f4a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -218,6 +218,8 @@ add_library(mixxx-lib STATIC EXCLUDE_FROM_ALL src/controllers/engine/controllerenginejsproxy.cpp src/controllers/engine/colormapper.cpp src/controllers/engine/colormapperjsproxy.cpp + src/controllers/engine/scriptconnection.cpp + src/controllers/engine/scriptconnectionjsproxy.cpp src/controllers/keyboard/keyboardeventfilter.cpp src/controllers/learningutils.cpp src/controllers/midi/midicontroller.cpp diff --git a/build/depends.py b/build/depends.py index 190461c774..59d43708ca 100644 --- a/build/depends.py +++ b/build/depends.py @@ -923,6 +923,8 @@ class MixxxCore(Feature): "src/controllers/engine/controllerenginejsproxy.cpp", "src/controllers/engine/colorjsproxy.cpp", "src/controllers/engine/colormapperjsproxy.cpp", + "src/controllers/engine/scriptconnection.cpp", + "src/controllers/engine/scriptconnectionjsproxy.cpp", "src/controllers/midi/midimessage.cpp", "src/controllers/midi/midiutils.cpp", "src/controllers/midi/midicontroller.cpp", diff --git a/src/control/controlobjectscript.h b/src/control/controlobjectscript.h index 402810b303..acc6d9ceba 100644 --- a/src/control/controlobjectscript.h +++ b/src/control/controlobjectscript.h @@ -5,7 +5,7 @@ #include "control/controlproxy.h" #include "controllers/controllerdebug.h" -#include "controllers/engine/controllerengine.h" +#include "controllers/engine/scriptconnection.h" // this is used for communicate with controller scripts class ControlObjectScript : public ControlProxy { diff --git a/src/controllers/engine/controllerengine.cpp b/src/controllers/engine/controllerengine.cpp index da6a737622..3c221c06da 100644 --- a/src/controllers/engine/controllerengine.cpp +++ b/src/controllers/engine/controllerengine.cpp @@ -14,16 +14,13 @@ #include "controllers/controllerdebug.h" #include "controllers/engine/colormapperjsproxy.h" #include "controllers/engine/controllerenginejsproxy.h" +#include "controllers/engine/scriptconnectionjsproxy.h" #include "errordialoghandler.h" #include "mixer/playermanager.h" // to tell the msvs compiler about `isnan` #include "util/math.h" #include "util/time.h" -// Used for id's inside controlConnection objects -// (closure compatible version of connectControl) -#include <QUuid> - const int kDecks = 16; // Use 1ms for the Alpha-Beta dt. We're assuming the OS actually gives us a 1ms @@ -706,34 +703,13 @@ QJSValue ControllerEngine::makeConnection(QString group, QString name, const QJS connection.id = QUuid::createUuid(); if (coScript->addScriptConnection(connection)) { - return m_pScriptEngine->newQObject(new ScriptConnectionInvokableWrapper(connection)); + return m_pScriptEngine->newQObject(new ScriptConnectionJSProxy(connection)); } return QJSValue(); } /* -------- ------------------------------------------------------ - Purpose: Execute a ScriptConnection's callback - Input: the value of the connected ControlObject to pass to the callback - -------- ------------------------------------------------------ */ -void ScriptConnection::executeCallback(double value) const { - QJSValueList args; - args << QJSValue(value); - args << QJSValue(key.group); - args << QJSValue(key.item); - QJSValue func = callback; // copy function because QJSValue::call is not const - QJSValue result = func.call(args); - if (result.isError()) { - if (controllerEngine != nullptr) { - controllerEngine->showScriptExceptionDialog(result); - } - qWarning() << "ControllerEngine: Invocation of connection " << id.toString() - << "connected to (" + key.group + ", " + key.item + ") failed:" - << result.toString(); - } -} - -/* -------- ------------------------------------------------------ Purpose: (Dis)connects a ScriptConnection Input: the ScriptConnection to disconnect -------- ------------------------------------------------------ */ @@ -748,13 +724,6 @@ bool ControllerEngine::removeScriptConnection(const ScriptConnection connection) return coScript->removeScriptConnection(connection); } -bool ScriptConnectionInvokableWrapper::disconnect() { - // if the removeScriptConnection succeeded, the connection has been successfully disconnected - bool success = m_scriptConnection.controllerEngine->removeScriptConnection(m_scriptConnection); - m_isConnected = !success; - return success; -} - /* -------- ------------------------------------------------------ Purpose: Triggers the callback function of a ScriptConnection Input: the ScriptConnection to trigger @@ -773,10 +742,6 @@ void ControllerEngine::triggerScriptConnection(const ScriptConnection connection connection.executeCallback(coScript->get()); } -void ScriptConnectionInvokableWrapper::trigger() { - m_scriptConnection.controllerEngine->triggerScriptConnection(m_scriptConnection); -} - // This function is a legacy version of makeConnection with several alternate // ways of invoking it. The callback function can be passed either as a string of // JavaScript code that evaluates to a function or an actual JavaScript function. @@ -850,7 +815,7 @@ QJSValue ControllerEngine::connectControl( "use engine.makeConnection. Returning reference to connection " + connection.id.toString(); - return m_pScriptEngine->newQObject(new ScriptConnectionInvokableWrapper(connection)); + return m_pScriptEngine->newQObject(new ScriptConnectionJSProxy(connection)); } } else if (passedCallback.isQObject()) { // Assume a ScriptConnection and assume that the script author @@ -864,8 +829,8 @@ QJSValue ControllerEngine::connectControl( << "a connection object to disconnect and returning false."; if (!strcmp(qmeta->className(), "ScriptConnectionInvokableWrapper")) { - ScriptConnectionInvokableWrapper* proxy = - (ScriptConnectionInvokableWrapper*)qobject; + ScriptConnectionJSProxy* proxy = + (ScriptConnectionJSProxy*)qobject; proxy->disconnect(); } return QJSValue(false); diff --git a/src/controllers/engine/controllerengine.h b/src/controllers/engine/controllerengine.h index d9f05224e2..b5090d65e6 100644 --- a/src/controllers/engine/controllerengine.h +++ b/src/controllers/engine/controllerengine.h @@ -27,54 +27,7 @@ class ControlObjectScript; class ControllerEngine; class ControllerEngineJSProxy; class EvaluationException; - -// ScriptConnection represents a connection between -// a ControlObject and a script callback function that gets executed when -// the value of the ControlObject changes. -class ScriptConnection { - public: - ConfigKey key; - QUuid id; - QJSValue callback; - ControllerEngine* controllerEngine; - - void executeCallback(double value) const; - - // Required for various QList methods and iteration to work. - inline bool operator==(const ScriptConnection& other) const { - return id == other.id; - } - inline bool operator!=(const ScriptConnection& other) const { - return !(*this == other); - } -}; - -// ScriptConnectionInvokableWrapper is a class providing scripts -// with an interface to ScriptConnection. -class ScriptConnectionInvokableWrapper : public QObject { - Q_OBJECT - Q_PROPERTY(QString id READ readId) - Q_PROPERTY(bool isConnected READ readIsConnected) - public: - ScriptConnectionInvokableWrapper(ScriptConnection conn) { - m_scriptConnection = conn; - m_idString = conn.id.toString(); - m_isConnected = true; - } - const QString& readId() const { - return m_idString; - } - bool readIsConnected() const { - return m_isConnected; - } - Q_INVOKABLE bool disconnect(); - Q_INVOKABLE void trigger(); - - private: - ScriptConnection m_scriptConnection; - QString m_idString; - bool m_isConnected; -}; +class ScriptConnection; class ControllerEngine : public QObject { Q_OBJECT @@ -94,6 +47,10 @@ class ControllerEngine : public QObject { return m_scriptFunctionPrefixes; }; + /// Shows a UI dialog notifying of a script evaluation error. + /// Precondition: QJSValue.isError() == true + void showScriptExceptionDialog(QJSValue evaluationResult, bool bFatal = false); + // Disconnect a ScriptConnection bool removeScriptConnection(const ScriptConnection conn); void triggerScriptConnection(const ScriptConnection conn); @@ -167,9 +124,6 @@ class ControllerEngine : public QObject { void throwJSError(const QString& message); - // Shows a UI dialog notifying of a script evaluation error. - // Precondition: QJSValue.isError() == true - void showScriptExceptionDialog(QJSValue evaluationResult, bool bFatal = false); bool m_bDisplayingExceptionDialog; QJSEngine* m_pScriptEngine; diff --git a/src/controllers/engine/scriptconnection.cpp b/src/controllers/engine/scriptconnection.cpp new file mode 100644 index 0000000000..f059d127e9 --- /dev/null +++ b/src/controllers/engine/scriptconnection.cpp @@ -0,0 +1,20 @@ +#include "controllers/engine/scriptconnection.h" + +#include "controllers/engine/controllerengine.h" + +void ScriptConnection::executeCallback(double value) const { + QJSValueList args; + args << QJSValue(value); + args << QJSValue(key.group); + args << QJSValue(key.item); + QJSValue func = callback; // copy function because QJSValue::call is not const + QJSValue result = func.call(args); + if (result.isError()) { + if (controllerEngine != nullptr) { + controllerEngine->showScriptExceptionDialog(result); + } + qWarning() << "ControllerEngine: Invocation of connection " << id.toString() + << "connected to (" + key.group + ", " + key.item + ") failed:" + << result.toString(); + } +} diff --git a/src/controllers/engine/scriptconnection.h b/src/controllers/engine/scriptconnection.h new file mode 100644 index 0000000000..153a4b83f8 --- /dev/null +++ b/src/controllers/engine/scriptconnection.h @@ -0,0 +1,29 @@ +#pragma once + +#include <QJSValue> +#include <QUuid> + +#include "preferences/configobject.h" + +class ControllerEngine; + +/// ScriptConnection is a connection between a ControlObject and a +/// script callback function that gets executed when the value +/// of the ControlObject changes. +class ScriptConnection { + public: + ConfigKey key; + QUuid id; + QJSValue callback; + ControllerEngine* controllerEngine; + + void executeCallback(double value) const; + + // Required for various QList methods and iteration to work. + inline bool operator==(const ScriptConnection& other) const { + return id == other.id; + } + inline bool operator!=(const ScriptConnection& other) const { + return !(*this == other); + } +}; diff --git a/src/controllers/engine/scriptconnectionjsproxy.cpp b/src/controllers/engine/scriptconnectionjsproxy.cpp new file mode 100644 index 0000000000..408971a2e8 --- /dev/null +++ b/src/controllers/engine/scriptconnectionjsproxy.cpp @@ -0,0 +1,14 @@ +#include "controllers/engine/scriptconnectionjsproxy.h" + +#include "controllers/engine/controllerengine.h" + +bool ScriptConnectionJSProxy::disconnect() { + // if the removeScriptConnection succeeded, the connection has been successfully disconnected + bool success = m_scriptConnection.controllerEngine->removeScriptConnection(m_scriptConnection); + m_isConnected = !success; + return success; +} + +void ScriptConnectionJSProxy::trigger() { + m_scriptConnection.controllerEngine->triggerScriptConnection(m_scriptConnection); +} diff --git a/src/controllers/engine/scriptconnectionjsproxy.h b/src/controllers/engine/scriptconnectionjsproxy.h new file mode 100644 index 0000000000..4c3877ef83 --- /dev/null +++ b/src/controllers/engine/scriptconnectionjsproxy.h @@ -0,0 +1,31 @@ +#pragma once + +#include <QObject> + +#include "controllers/engine/scriptconnection.h" + +/// ScriptConnectionJSProxy provides scripts with an interface to ScriptConnection. +class ScriptConnectionJSProxy : public QObject { + Q_OBJECT + Q_PROPERTY(QString id READ readId) + Q_PROPERTY(bool isConnected READ readIsConnected) + public: + ScriptConnectionJSProxy(ScriptConnection conn) { + m_scriptConnection = conn; + m_idString = conn.id.toString(); + m_isConnected = true; + } + const QString& readId() const { + return m_idString; + } + bool readIsConnected() const { + return m_isConnected; + } + Q_INVOKABLE bool disconnect(); + Q_INVOKABLE void trigger(); + + private: + ScriptConnection m_scriptConnection; + QString m_idString; + bool m_isConnected; +}; |