Skip to content

Commit 51fea62

Browse files
Transform component's init and shutdown to signals
1 parent edd2254 commit 51fea62

8 files changed

Lines changed: 105 additions & 35 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2776,10 +2776,9 @@ if(QML)
27762776
src/qml/qmlplayerproxy.cpp
27772777
src/qml/qmlvisibleeffectsmodel.cpp
27782778
src/qml/qmlwaveformoverview.cpp
2779+
src/qml/mixxcomponentlifecycle.cpp
27792780
src/qml/mixxxcontroller.cpp
2780-
src/qml/mixxxcontroller.h
27812781
src/qml/mixxxscreen.cpp
2782-
src/qml/mixxxscreen.h
27832782
# The following sources need to be in this target to get QML_ELEMENT properly interpreted
27842783
src/control/controlmodel.cpp
27852784
src/control/controlsortfiltermodel.cpp

src/controllers/scripting/legacy/controllerscriptenginelegacy.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ bool ControllerScriptEngineLegacy::callShutdownFunction() {
129129

130130
#ifdef MIXXX_USE_QML
131131
for (const auto& controller : m_mixxxController) {
132-
controller->shutdown();
132+
emit controller->shutdown();
133133
}
134134

135135
if (!m_bQmlMode) {
@@ -207,7 +207,7 @@ bool ControllerScriptEngineLegacy::callInitFunction() {
207207
}
208208

209209
for (const auto& controller : m_mixxxController) {
210-
controller->init();
210+
emit controller->init();
211211
}
212212

213213
QHashIterator<QString, std::shared_ptr<QQuickItem>> i(m_rootItems);

src/qml/mixxcomponentlifecycle.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "mixxxcontroller.h"
2+
3+
namespace mixxx {
4+
namespace qml {
5+
6+
} // namespace qml
7+
} // namespace mixxx

src/qml/mixxcomponentlifecycle.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef MIXXX_MIXXXCOMPONENTLIFECYCLE_H
2+
#define MIXXX_MIXXXCOMPONENTLIFECYCLE_H
3+
4+
#include <QObject>
5+
6+
namespace mixxx {
7+
namespace qml {
8+
9+
class MixxxComponentLifecycle {
10+
public:
11+
virtual ~MixxxComponentLifecycle() {
12+
}
13+
signals:
14+
virtual void init() = 0;
15+
virtual void shutdown() = 0;
16+
};
17+
18+
} // namespace qml
19+
} // namespace mixxx
20+
21+
#define MixxxComponentLifecycleIID "MixxxComponentLifecycle"
22+
Q_DECLARE_INTERFACE(mixxx::qml::MixxxComponentLifecycle, MixxxComponentLifecycleIID)
23+
24+
#endif // MIXXX_MIXXXCOMPONENTLIFECYCLE_H

src/qml/mixxxcontroller.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,29 @@
22

33
namespace mixxx {
44
namespace qml {
5-
void MixxxController::init() {
6-
metaObject()->invokeMethod(this, "init");
5+
6+
void MixxxController::componentComplete() {
7+
QObject::connect(this,
8+
&MixxxComponentLifecycle::init,
9+
this,
10+
&MixxxController::initChildrenComponents);
11+
QObject::connect(this,
12+
&MixxxComponentLifecycle::shutdown,
13+
this,
14+
&MixxxController::shutdownChildrenComponents);
715
}
8-
void MixxxController::shutdown() {
9-
metaObject()->invokeMethod(this, "shutdown");
16+
17+
void MixxxController::initChildrenComponents() {
18+
for (auto* childComponent : m_pChildComponents.toList<QList<MixxxComponentLifecycle*>>()) {
19+
emit childComponent->init();
20+
}
1021
}
22+
23+
void MixxxController::shutdownChildrenComponents() {
24+
for (auto* childComponent : m_pChildComponents.toList<QList<MixxxComponentLifecycle*>>()) {
25+
emit childComponent->shutdown();
26+
}
27+
}
28+
1129
} // namespace qml
1230
} // namespace mixxx

src/qml/mixxxcontroller.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,32 @@
66
#include <QObject>
77
#include <QtQml>
88

9-
#include "mixxxscreen.h"
9+
#include "mixxcomponentlifecycle.h"
1010

1111
namespace mixxx {
1212
namespace qml {
1313

14-
class MixxxController : public QObject {
14+
class MixxxController : public QObject, public MixxxComponentLifecycle, public QQmlParserStatus {
1515
Q_OBJECT
16+
Q_INTERFACES(QQmlParserStatus MixxxComponentLifecycle)
1617
QML_ELEMENT
1718
Q_PROPERTY(QString controllerId MEMBER m_controllerId)
1819
Q_PROPERTY(bool debugMode MEMBER m_debugMode)
19-
Q_PROPERTY(QQmlListProperty<MixxxScreen> screens MEMBER m_screens)
20-
Q_CLASSINFO("DefaultProperty", "screens")
20+
Q_PROPERTY(QQmlListProperty<MixxxComponentLifecycle> childComponents MEMBER m_pChildComponents)
21+
Q_CLASSINFO("DefaultProperty", "childComponents")
2122

2223
public:
23-
void init();
24-
void shutdown();
24+
void componentComplete() override;
2525

2626
private:
2727
QString m_controllerId;
2828
bool m_debugMode;
29-
QQmlListProperty<MixxxScreen> m_screens;
29+
QList<MixxxComponentLifecycle*> m_childComponents;
30+
QQmlListProperty<MixxxComponentLifecycle> m_pChildComponents;
31+
32+
private slots:
33+
void initChildrenComponents();
34+
void shutdownChildrenComponents();
3035
};
3136

3237
} // namespace qml

src/qml/mixxxscreen.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,30 @@
22

33
namespace mixxx {
44
namespace qml {
5+
6+
int MixxxScreen::width() {
7+
return m_size.width();
8+
}
9+
10+
void MixxxScreen::setWidth(int value) {
11+
m_size = QSize(value, m_size.height());
12+
}
13+
14+
int MixxxScreen::height() {
15+
return m_size.width();
16+
}
17+
18+
void MixxxScreen::setHeight(int value) {
19+
m_size = QSize(m_size.width(), value);
20+
}
21+
22+
uint MixxxScreen::splashOff() {
23+
return m_splashOff.count();
24+
}
25+
26+
void MixxxScreen::setSplashOff(uint value) {
27+
m_splashOff = std::chrono::milliseconds(value);
28+
}
29+
530
} // namespace qml
631
} // namespace mixxx

src/qml/mixxxscreen.h

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55
#ifndef MIXXX_MIXXXSCREEN_H
66
#define MIXXX_MIXXXSCREEN_H
77

8+
#include <QtQml/qqmlregistration.h>
9+
810
#include <QImage>
911
#include <QObject>
1012
#include <QSize>
11-
#include <QtQmlIntegration>
13+
14+
#include "mixxcomponentlifecycle.h"
1215

1316
namespace mixxx {
1417
namespace qml {
1518

16-
class MixxxScreen : public QObject {
19+
class MixxxScreen : public QObject, public MixxxComponentLifecycle {
1720
Q_OBJECT
1821
QML_ELEMENT
22+
Q_INTERFACES(MixxxComponentLifecycle)
1923
Q_PROPERTY(QString screenId MEMBER m_screenId REQUIRED)
2024
Q_PROPERTY(int width READ width WRITE setWidth)
2125
Q_PROPERTY(int height READ height WRITE setHeight)
@@ -34,24 +38,12 @@ class MixxxScreen : public QObject {
3438
};
3539
Q_ENUM(ColorEndian)
3640

37-
int width() {
38-
return m_size.width();
39-
}
40-
void setWidth(int value) {
41-
m_size = QSize(value, m_size.height());
42-
}
43-
int height() {
44-
return m_size.width();
45-
}
46-
void setHeight(int value) {
47-
m_size = QSize(m_size.width(), value);
48-
}
49-
uint splashOff() {
50-
return m_splashOff.count();
51-
}
52-
void setSplashOff(uint value) {
53-
m_splashOff = std::chrono::milliseconds(value);
54-
}
41+
int width();
42+
void setWidth(int value);
43+
int height();
44+
void setHeight(int value);
45+
uint splashOff();
46+
void setSplashOff(uint value);
5547

5648
private:
5749
QString m_screenId; // The screen identifier.

0 commit comments

Comments
 (0)