summaryrefslogtreecommitdiffstats
path: root/src/qmlapplication.cpp
blob: 26b0bd7810c9e1c349b1868a2216c40f5f1357ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "qmlapplication.h"

#include "moc_qmlapplication.cpp"
#include "qml/asyncimageprovider.h"
#include "qml/qmlconfigproxy.h"
#include "qml/qmlcontrolproxy.h"
#include "qml/qmleffectmanifestparametersmodel.h"
#include "qml/qmleffectslotproxy.h"
#include "qml/qmleffectsmanagerproxy.h"
#include "qml/qmllibraryproxy.h"
#include "qml/qmllibrarytracklistmodel.h"
#include "qml/qmlplayermanagerproxy.h"
#include "qml/qmlplayerproxy.h"
#include "qml/qmlvisibleeffectsmodel.h"
#include "qml/qmlwaveformoverview.h"
#include "soundio/soundmanager.h"

namespace {
const QString kMainQmlFileName = QStringLiteral("skins/QMLDemo/main.qml");

// Converts a (capturing) lambda into a function pointer that can be passed to
// qmlRegisterSingletonType.
template<class F>
auto lambda_to_singleton_type_factory_ptr(F&& f) {
    static F fn = std::forward<F>(f);
    return [](QQmlEngine* pEngine, QJSEngine* pScriptEngine) -> QObject* {
        return fn(pEngine, pScriptEngine);
    };
}
} // namespace

namespace mixxx {
namespace skin {
namespace qml {

QmlApplication::QmlApplication(
        QApplication* app,
        std::shared_ptr<CoreServices> pCoreServices)
        : m_pCoreServices(pCoreServices),
          m_mainFilePath(pCoreServices->getSettings()->getResourcePath() + kMainQmlFileName),
          m_pAppEngine(nullptr),
          m_fileWatcher({m_mainFilePath}) {
    m_pCoreServices->initialize(app);
    SoundDeviceError result = m_pCoreServices->getSoundManager()->setupDevices();
    if (result != SOUNDDEVICE_ERROR_OK) {
        qCritical() << "Error setting up sound devices" << result;
        exit(result);
    }

    qmlRegisterType<QmlControlProxy>("Mixxx", 0, 1, "ControlProxy");
    qmlRegisterType<QmlWaveformOverview>("Mixxx", 0, 1, "WaveformOverview");

    // Any uncreateable non-singleton types registered here require arguments
    // that we don't want to expose to QML directly. Instead, they can be
    // retrieved by member properties or methods from the singleton types.
    //
    // The alternative would be to register their *arguments* in the QML
    // system, which would improve nothing, or we had to expose them as
    // singletons to that they can be accessed by components instantiated by
    // QML, which would also be suboptimal.

    qmlRegisterSingletonType<QmlEffectsManagerProxy>("Mixxx",
            0,
            1,
            "EffectsManager",
            lambda_to_singleton_type_factory_ptr(
                    [pCoreServices](QQmlEngine* pEngine,
                            QJSEngine* pScriptEngine) -> QObject* {
                        Q_UNUSED(pScriptEngine);

                        QmlEffectsManagerProxy* pEffectsManagerProxy =
                                new QmlEffectsManagerProxy(
                                        pCoreServices->getEffectsManager(),
                                        pEngine);
                        return pEffectsManagerProxy;
                    }));
    qmlRegisterUncreatableType<QmlVisibleEffectsModel>("Mixxx",
            0,
            1,
            "VisibleEffectsModel",
            "VisibleEffectsModel objects can't be created directly, please use "
            "Mixxx.EffectsManager.visibleEffectsModel");
    qmlRegisterUncreatableType<QmlEffectManifestParametersModel>("Mixxx",
            0,
            1,
            "EffectManifestParametersModel",
            "EffectManifestParametersModel objects can't be created directly, "
            "please use Mixxx.EffectsSlot.parametersModel");
    qmlRegisterUncreatableType<QmlEffectSlotProxy>("Mixxx",
            0,
            1,
            "EffectSlotProxy",
            "EffectSlotProxy objects can't be created directly, please use "
            "Mixxx.EffectsManager.getEffectSlot(rackNumber, unitNumber, effectNumber)");

    qmlRegisterSingletonType<QmlPlayerManagerProxy>("Mixxx",
            0,
            1,
            "PlayerManager",
            lambda_to_singleton_type_factory_ptr(
                    [pCoreServices](QQmlEngine* pEngine,
                            QJSEngine* pScriptEngine) -> QObject* {
                        Q_UNUSED(pScriptEngine);

                        QmlPlayerManagerProxy* pPlayerManagerProxy =
                                new QmlPlayerManagerProxy(
                                        pCoreServices->getPlayerManager(),
                                        pEngine);
                        return pPlayerManagerProxy;
                    }));
    qmlRegisterUncreatableType<QmlPlayerProxy>("Mixxx",
            0,
            1,
            "Player",
            "Player objects can't be created directly, please use "
            "Mixxx.PlayerManager.getPlayer(group)");

    const auto pSettings = m_pCoreServices->getSettings();
    qmlRegisterSingletonType<QmlConfigProxy>("Mixxx",
            0,
            1,
            "Config",
            lambda_to_singleton_type_factory_ptr(
                    [pSettings](QQmlEngine* pEngine,
                            QJSEngine* pScriptEngine) -> QObject* {
                        Q_UNUSED(pScriptEngine);

                        QmlConfigProxy* pConfigProxy =
                                new QmlConfigProxy(
                                        pSettings,
                                        pEngine);
                        return pConfigProxy;
                    }));

    qmlRegisterUncreatableType<QmlLibraryTrackListModel>("Mixxx",
            0,
            1,
            "LibraryTrackListModel",
            "LibraryTrackListModel objects can't be created directly, "
            "please use Mixxx.Library.model");
    qmlRegisterSingletonType<QmlLibraryProxy>("Mixxx",
            0,
            1,
            "Library",
            lambda_to_singleton_type_factory_ptr(
                    [pCoreServices](QQmlEngine* pEngine,
                            QJSEngine* pScriptEngine) -> QObject* {
                        Q_UNUSED(pScriptEngine);

                        QmlLibraryProxy* pLibraryProxy = new QmlLibraryProxy(
                                pCoreServices->getLibrary(),
                                pEngine);
                        return pLibraryProxy;
                    }));

    loadQml(m_mainFilePath);

    connect(&m_fileWatcher,
            &QFileSystemWatcher::fileChanged,
            this,
            &QmlApplication::loadQml);
}

void QmlApplication::loadQml(const QString& path) {
    // QQmlApplicationEngine::load creates a new window but also leaves the old one,
    // so it is necessary to destroy the old QQmlApplicationEngine and create a new one.
    m_pAppEngine = std::make_unique<QQmlApplicationEngine>();

    const QFileInfo fileInfo(path);
    m_pAppEngine->addImportPath(fileInfo.absoluteDir().absolutePath());

    // No memory leak here, the QQmlEngine takes ownership of the provider
    QQuickAsyncImageProvider* pImageProvider = new AsyncImageProvider();
    m_pAppEngine->addImageProvider(AsyncImageProvider::kProviderName, pImageProvider);

    m_pAppEngine->load(path);
    if (m_pAppEngine->rootObjects().isEmpty()) {
        qCritical() << "Failed to load QML file" << path;
    }
}

} // namespace qml
} // namespace skin
} // namespace mixxx