From 1d248a2475eb071f60276057ae01ed876e755cfb Mon Sep 17 00:00:00 2001 From: Christophe Henry Date: Tue, 16 Jul 2024 10:15:19 +0200 Subject: [PATCH] Transform component's init and shutdown to signals --- CMakeLists.txt | 3 +- res/controllers/Denon-DN-S3700.midi.xml | 4 +-- res/controllers/Denon-DN-S3700.qml | 10 ++---- .../legacy/controllerscriptenginelegacy.cpp | 4 +-- src/qml/mixxcomponentlifecycle.cpp | 7 ++++ src/qml/mixxcomponentlifecycle.h | 24 ++++++++++++++ src/qml/mixxxcontroller.cpp | 26 ++++++++++++--- src/qml/mixxxcontroller.h | 19 +++++++---- src/qml/mixxxscreen.cpp | 25 +++++++++++++++ src/qml/mixxxscreen.h | 32 +++++++------------ 10 files changed, 109 insertions(+), 45 deletions(-) create mode 100644 src/qml/mixxcomponentlifecycle.cpp create mode 100644 src/qml/mixxcomponentlifecycle.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 75d3f94e8879..7cb2877a29d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2776,10 +2776,9 @@ if(QML) src/qml/qmlplayerproxy.cpp src/qml/qmlvisibleeffectsmodel.cpp src/qml/qmlwaveformoverview.cpp + src/qml/mixxcomponentlifecycle.cpp src/qml/mixxxcontroller.cpp - src/qml/mixxxcontroller.h src/qml/mixxxscreen.cpp - src/qml/mixxxscreen.h # The following sources need to be in this target to get QML_ELEMENT properly interpreted src/control/controlmodel.cpp src/control/controlsortfiltermodel.cpp diff --git a/res/controllers/Denon-DN-S3700.midi.xml b/res/controllers/Denon-DN-S3700.midi.xml index 80353f62e4c7..56a8794e3a41 100644 --- a/res/controllers/Denon-DN-S3700.midi.xml +++ b/res/controllers/Denon-DN-S3700.midi.xml @@ -7,9 +7,7 @@ - + - - diff --git a/res/controllers/Denon-DN-S3700.qml b/res/controllers/Denon-DN-S3700.qml index 506d37698113..69080bd4cdd7 100644 --- a/res/controllers/Denon-DN-S3700.qml +++ b/res/controllers/Denon-DN-S3700.qml @@ -5,16 +5,12 @@ import "Mixxx" MixxxController { id: controller - function init() { - console.error(controller.controllerId, controller.debugMode); - } - function shutdown() { - console.error(`Shutting down ${controller.controllerId} with debug mode ${controller.debugMode}`); - } + onInit: console.error(controller.controllerId, controller.debugMode) + onShutdown: console.error(`Shutting down ${controller.controllerId} with debug mode ${controller.debugMode}`) MixxxScreen { screenId: "screen 7" splashOff: 5000 - Component.onCompleted: console.error(`MixxxScreen.identifier=${screenId} ${splashOff}`) + onInit: console.error(`MixxxScreen.identifier=${screenId} ${splashOff}`) } } diff --git a/src/controllers/scripting/legacy/controllerscriptenginelegacy.cpp b/src/controllers/scripting/legacy/controllerscriptenginelegacy.cpp index 64c00f30bf14..cfed71aa332c 100644 --- a/src/controllers/scripting/legacy/controllerscriptenginelegacy.cpp +++ b/src/controllers/scripting/legacy/controllerscriptenginelegacy.cpp @@ -129,7 +129,7 @@ bool ControllerScriptEngineLegacy::callShutdownFunction() { #ifdef MIXXX_USE_QML for (const auto& controller : m_mixxxController) { - controller->shutdown(); + emit controller->shutdown(); } if (!m_bQmlMode) { @@ -207,7 +207,7 @@ bool ControllerScriptEngineLegacy::callInitFunction() { } for (const auto& controller : m_mixxxController) { - controller->init(); + emit controller->init(); } QHashIterator> i(m_rootItems); diff --git a/src/qml/mixxcomponentlifecycle.cpp b/src/qml/mixxcomponentlifecycle.cpp new file mode 100644 index 000000000000..367bf9d0fad8 --- /dev/null +++ b/src/qml/mixxcomponentlifecycle.cpp @@ -0,0 +1,7 @@ +#include "mixxxcontroller.h" + +namespace mixxx { +namespace qml { + +} // namespace qml +} // namespace mixxx diff --git a/src/qml/mixxcomponentlifecycle.h b/src/qml/mixxcomponentlifecycle.h new file mode 100644 index 000000000000..f226607d8b66 --- /dev/null +++ b/src/qml/mixxcomponentlifecycle.h @@ -0,0 +1,24 @@ +#ifndef MIXXX_MIXXXCOMPONENTLIFECYCLE_H +#define MIXXX_MIXXXCOMPONENTLIFECYCLE_H + +#include + +namespace mixxx { +namespace qml { + +class MixxxComponentLifecycle { + public: + virtual ~MixxxComponentLifecycle() { + } + signals: + virtual void init() = 0; + virtual void shutdown() = 0; +}; + +} // namespace qml +} // namespace mixxx + +#define MixxxComponentLifecycleIID "MixxxComponentLifecycle" +Q_DECLARE_INTERFACE(mixxx::qml::MixxxComponentLifecycle, MixxxComponentLifecycleIID) + +#endif // MIXXX_MIXXXCOMPONENTLIFECYCLE_H diff --git a/src/qml/mixxxcontroller.cpp b/src/qml/mixxxcontroller.cpp index 4419e74769eb..48512987d0ef 100644 --- a/src/qml/mixxxcontroller.cpp +++ b/src/qml/mixxxcontroller.cpp @@ -2,11 +2,29 @@ namespace mixxx { namespace qml { -void MixxxController::init() { - metaObject()->invokeMethod(this, "init"); + +void MixxxController::componentComplete() { + QObject::connect(this, + &MixxxComponentLifecycle::init, + this, + &MixxxController::initChildrenComponents); + QObject::connect(this, + &MixxxComponentLifecycle::shutdown, + this, + &MixxxController::shutdownChildrenComponents); } -void MixxxController::shutdown() { - metaObject()->invokeMethod(this, "shutdown"); + +void MixxxController::initChildrenComponents() { + for (auto* childComponent : m_pChildComponents.toList>()) { + emit childComponent->init(); + } } + +void MixxxController::shutdownChildrenComponents() { + for (auto* childComponent : m_pChildComponents.toList>()) { + emit childComponent->shutdown(); + } +} + } // namespace qml } // namespace mixxx diff --git a/src/qml/mixxxcontroller.h b/src/qml/mixxxcontroller.h index 112c43ec6643..ec562c015a78 100644 --- a/src/qml/mixxxcontroller.h +++ b/src/qml/mixxxcontroller.h @@ -6,27 +6,32 @@ #include #include -#include "mixxxscreen.h" +#include "mixxcomponentlifecycle.h" namespace mixxx { namespace qml { -class MixxxController : public QObject { +class MixxxController : public QObject, public MixxxComponentLifecycle, public QQmlParserStatus { Q_OBJECT + Q_INTERFACES(QQmlParserStatus MixxxComponentLifecycle) QML_ELEMENT Q_PROPERTY(QString controllerId MEMBER m_controllerId) Q_PROPERTY(bool debugMode MEMBER m_debugMode) - Q_PROPERTY(QQmlListProperty screens MEMBER m_screens) - Q_CLASSINFO("DefaultProperty", "screens") + Q_PROPERTY(QQmlListProperty childComponents MEMBER m_pChildComponents) + Q_CLASSINFO("DefaultProperty", "childComponents") public: - void init(); - void shutdown(); + void componentComplete() override; private: QString m_controllerId; bool m_debugMode; - QQmlListProperty m_screens; + QList m_childComponents; + QQmlListProperty m_pChildComponents; + + private slots: + void initChildrenComponents(); + void shutdownChildrenComponents(); }; } // namespace qml diff --git a/src/qml/mixxxscreen.cpp b/src/qml/mixxxscreen.cpp index d1e2568a7c9d..b3a72b83f920 100644 --- a/src/qml/mixxxscreen.cpp +++ b/src/qml/mixxxscreen.cpp @@ -2,5 +2,30 @@ namespace mixxx { namespace qml { + +int MixxxScreen::width() { + return m_size.width(); +} + +void MixxxScreen::setWidth(int value) { + m_size = QSize(value, m_size.height()); +} + +int MixxxScreen::height() { + return m_size.width(); +} + +void MixxxScreen::setHeight(int value) { + m_size = QSize(m_size.width(), value); +} + +uint MixxxScreen::splashOff() { + return m_splashOff.count(); +} + +void MixxxScreen::setSplashOff(uint value) { + m_splashOff = std::chrono::milliseconds(value); +} + } // namespace qml } // namespace mixxx diff --git a/src/qml/mixxxscreen.h b/src/qml/mixxxscreen.h index df90679a1782..904844e49dd3 100644 --- a/src/qml/mixxxscreen.h +++ b/src/qml/mixxxscreen.h @@ -5,17 +5,21 @@ #ifndef MIXXX_MIXXXSCREEN_H #define MIXXX_MIXXXSCREEN_H +#include + #include #include #include -#include + +#include "mixxcomponentlifecycle.h" namespace mixxx { namespace qml { -class MixxxScreen : public QObject { +class MixxxScreen : public QObject, public MixxxComponentLifecycle { Q_OBJECT QML_ELEMENT + Q_INTERFACES(MixxxComponentLifecycle) Q_PROPERTY(QString screenId MEMBER m_screenId REQUIRED) Q_PROPERTY(int width READ width WRITE setWidth) Q_PROPERTY(int height READ height WRITE setHeight) @@ -34,24 +38,12 @@ class MixxxScreen : public QObject { }; Q_ENUM(ColorEndian) - int width() { - return m_size.width(); - } - void setWidth(int value) { - m_size = QSize(value, m_size.height()); - } - int height() { - return m_size.width(); - } - void setHeight(int value) { - m_size = QSize(m_size.width(), value); - } - uint splashOff() { - return m_splashOff.count(); - } - void setSplashOff(uint value) { - m_splashOff = std::chrono::milliseconds(value); - } + int width(); + void setWidth(int value); + int height(); + void setHeight(int value); + uint splashOff(); + void setSplashOff(uint value); private: QString m_screenId; // The screen identifier.