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 18, 2024
1 parent edd2254 commit 2e34045
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 85 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2777,9 +2777,7 @@ if(QML)
src/qml/qmlvisibleeffectsmodel.cpp
src/qml/qmlwaveformoverview.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(`Starting controller ${controller.controllerId} with debug mode ${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.screenId=${screenId}, MixxxScreen.splashOff=${splashOff}`)
}
}
47 changes: 3 additions & 44 deletions src/controllers/scripting/legacy/controllerscriptenginelegacy.cpp
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 Expand Up @@ -261,48 +261,7 @@ bool ControllerScriptEngineLegacy::callInitFunction() {
// ControllerScriptEngineBase::handleQMLErrors
}

QListIterator<std::shared_ptr<mixxx::qml::MixxxController>> controllers(m_mixxxController);
bool controllersSuccess = true;
while (controllers.hasNext()) {
const QMetaObject* metaObject = controllers.next()->metaObject();

VERIFY_OR_DEBUG_ASSERT(metaObject) {
qCWarning(m_logger) << "Invalid meta object for controller";
continue;
}

QMetaMethod initFunction;
bool typed = false;
int methodIdx = metaObject->indexOfMethod(kQmlComponentInitFunctionUntypedSignature);

if (methodIdx == -1 || !metaObject->method(methodIdx).isValid()) {
qCDebug(m_logger) << "QML controller has no valid untyped init method.";
methodIdx = metaObject->indexOfMethod(kQmlComponentFunctionTypedSignature);
typed = true;
}

initFunction = metaObject->method(methodIdx);

if (!initFunction.isValid()) {
qCDebug(m_logger) << "QML controller has no valid untyped init method; Skipping.";
continue;
}

qCDebug(m_logger) << "Executing init on QML controller";
if (typed) {
success &= initFunction.invoke(i.value().get(),
Qt::DirectConnection,
Q_ARG(QString, controllerName),
Q_ARG(bool, m_logger().isDebugEnabled()));
} else {
success &= initFunction.invoke(i.value().get(),
Qt::DirectConnection,
Q_ARG(QVariant, controllerName),
Q_ARG(QVariant, m_logger().isDebugEnabled()));
}
}

return success && controllersSuccess;
return success;
}
#endif
}
Expand Down
35 changes: 31 additions & 4 deletions src/qml/mixxxcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,38 @@

namespace mixxx {
namespace qml {
void MixxxController::init() {
metaObject()->invokeMethod(this, "init");

MixxxController::MixxxController(QObject* parent)
: QObject(parent), m_pChildren(this, &m_children) {
}
void MixxxController::shutdown() {
metaObject()->invokeMethod(this, "shutdown");

void MixxxController::classBegin() {
}

void MixxxController::componentComplete() {
QObject::connect(this,
&MixxxController::init,
this,
&MixxxController::initChildrenComponents);
QObject::connect(this,
&MixxxController::shutdown,
this,
&MixxxController::shutdownChildrenComponents);
}

void MixxxController::initChildrenComponents() {
for (auto* childComponent : m_children) {
// Try emit init signal
QMetaObject::invokeMethod(childComponent, "init", Qt::DirectConnection);
}
}

void MixxxController::shutdownChildrenComponents() {
for (auto* childComponent : m_children) {
// Try emit shutdown signal
QMetaObject::invokeMethod(childComponent, "shutdown", Qt::DirectConnection);
}
}

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

#include "mixxxscreen.h"

namespace mixxx {
namespace qml {

class MixxxController : public QObject {
class MixxxController : public QObject, public QQmlParserStatus {
Q_OBJECT
Q_INTERFACES(QQmlParserStatus)
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<QObject> childComponents MEMBER m_pChildren)
Q_CLASSINFO("DefaultProperty", "childComponents")

public:
explicit MixxxController(QObject* parent = nullptr);
void classBegin() override;
void componentComplete() override;

signals:
void init();
void shutdown();

private:
QString m_controllerId;
bool m_debugMode;
QQmlListProperty<MixxxScreen> m_screens;
QList<QObject*> m_children;
QQmlListProperty<QObject> m_pChildren;

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
31 changes: 12 additions & 19 deletions src/qml/mixxxscreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
#ifndef MIXXX_MIXXXSCREEN_H
#define MIXXX_MIXXXSCREEN_H

#include <QtQml/qqmlregistration.h>

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

namespace mixxx {
namespace qml {
Expand All @@ -34,24 +35,16 @@ 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);

signals:
void init();
void shutdown();

private:
QString m_screenId; // The screen identifier.
Expand Down

0 comments on commit 2e34045

Please sign in to comment.