Skip to content

Commit

Permalink
Transform component's init and shutdown to signals
Browse files Browse the repository at this point in the history
  • Loading branch information
christophehenry committed Jul 16, 2024
1 parent edd2254 commit 1d248a2
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 45 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions res/controllers/Denon-DN-S3700.midi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
</info>
<controller id="DN-S3700">
<scriptfiles>
<file filename="Denon-DN-S3700.qml" functionprefix=""/>
<file filename="Denon-DN-S3700.qml" />
</scriptfiles>
<controls />
<outputs/>
</controller>
</MixxxControllerPreset>
10 changes: 3 additions & 7 deletions res/controllers/Denon-DN-S3700.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -207,7 +207,7 @@ bool ControllerScriptEngineLegacy::callInitFunction() {
}

for (const auto& controller : m_mixxxController) {
controller->init();
emit controller->init();
}

QHashIterator<QString, std::shared_ptr<QQuickItem>> i(m_rootItems);
Expand Down
7 changes: 7 additions & 0 deletions src/qml/mixxcomponentlifecycle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "mixxxcontroller.h"

namespace mixxx {
namespace qml {

} // namespace qml
} // namespace mixxx
24 changes: 24 additions & 0 deletions src/qml/mixxcomponentlifecycle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef MIXXX_MIXXXCOMPONENTLIFECYCLE_H
#define MIXXX_MIXXXCOMPONENTLIFECYCLE_H

#include <QObject>

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
26 changes: 22 additions & 4 deletions src/qml/mixxxcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QList<MixxxComponentLifecycle*>>()) {
emit childComponent->init();
}
}

void MixxxController::shutdownChildrenComponents() {
for (auto* childComponent : m_pChildComponents.toList<QList<MixxxComponentLifecycle*>>()) {
emit childComponent->shutdown();
}
}

} // namespace qml
} // namespace mixxx
19 changes: 12 additions & 7 deletions src/qml/mixxxcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,32 @@
#include <QObject>
#include <QtQml>

#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)

Check failure on line 16 in src/qml/mixxxcontroller.h

View workflow job for this annotation

GitHub Actions / clazy

Undefined interface

Check failure on line 16 in src/qml/mixxxcontroller.h

View workflow job for this annotation

GitHub Actions / clang-tidy

Undefined interface

Check failure on line 16 in src/qml/mixxxcontroller.h

View workflow job for this annotation

GitHub Actions / Ubuntu 22.04

Undefined interface

Check failure on line 16 in src/qml/mixxxcontroller.h

View workflow job for this annotation

GitHub Actions / coverage

Undefined interface

Check failure on line 16 in src/qml/mixxxcontroller.h

View workflow job for this annotation

GitHub Actions / macOS 12 arm64

Undefined interface
QML_ELEMENT
Q_PROPERTY(QString controllerId MEMBER m_controllerId)
Q_PROPERTY(bool debugMode MEMBER m_debugMode)
Q_PROPERTY(QQmlListProperty<MixxxScreen> screens MEMBER m_screens)
Q_CLASSINFO("DefaultProperty", "screens")
Q_PROPERTY(QQmlListProperty<MixxxComponentLifecycle> childComponents MEMBER m_pChildComponents)
Q_CLASSINFO("DefaultProperty", "childComponents")

public:
void init();
void shutdown();
void componentComplete() override;

private:
QString m_controllerId;
bool m_debugMode;
QQmlListProperty<MixxxScreen> m_screens;
QList<MixxxComponentLifecycle*> m_childComponents;
QQmlListProperty<MixxxComponentLifecycle> m_pChildComponents;

private slots:
void initChildrenComponents();
void shutdownChildrenComponents();
};

} // namespace qml
Expand Down
25 changes: 25 additions & 0 deletions src/qml/mixxxscreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 12 additions & 20 deletions src/qml/mixxxscreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
#ifndef MIXXX_MIXXXSCREEN_H
#define MIXXX_MIXXXSCREEN_H

#include <QtQml/qqmlregistration.h>

#include <QImage>
#include <QObject>
#include <QSize>
#include <QtQmlIntegration>

#include "mixxcomponentlifecycle.h"

namespace mixxx {
namespace qml {

class MixxxScreen : public QObject {
class MixxxScreen : public QObject, public MixxxComponentLifecycle {
Q_OBJECT
QML_ELEMENT
Q_INTERFACES(MixxxComponentLifecycle)

Check failure on line 22 in src/qml/mixxxscreen.h

View workflow job for this annotation

GitHub Actions / clang-tidy

Undefined interface

Check failure on line 22 in src/qml/mixxxscreen.h

View workflow job for this annotation

GitHub Actions / Ubuntu 22.04

Undefined interface

Check failure on line 22 in src/qml/mixxxscreen.h

View workflow job for this annotation

GitHub Actions / coverage

Undefined interface

Check failure on line 22 in src/qml/mixxxscreen.h

View workflow job for this annotation

GitHub Actions / macOS 12 arm64

Undefined interface
Q_PROPERTY(QString screenId MEMBER m_screenId REQUIRED)
Q_PROPERTY(int width READ width WRITE setWidth)
Q_PROPERTY(int height READ height WRITE setHeight)
Expand All @@ -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.
Expand Down

0 comments on commit 1d248a2

Please sign in to comment.