Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2024 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "engravingapiv1.h"
24 :
25 : #include <QJSValueIterator>
26 :
27 : #include "qmlpluginapi.h"
28 :
29 : // #include "log.h"
30 :
31 : using namespace mu::engraving::apiv1;
32 :
33 : /** APIDOC
34 : * Information and actions related to Score
35 : * @namespace Engraving
36 : */
37 :
38 0 : EngravingApiV1::EngravingApiV1(muse::api::IApiEngine* e)
39 0 : : muse::api::ApiObject(e)
40 : {
41 0 : }
42 :
43 0 : EngravingApiV1::~EngravingApiV1()
44 : {
45 0 : if (m_selfApi) {
46 0 : delete m_api;
47 : }
48 0 : }
49 :
50 : //! NOTE Don't remove please
51 : // static void dump(QString name, QJSValue val)
52 : // {
53 : // LOGDA() << "val: " << name;
54 : // QJSValueIterator it(val);
55 : // while (it.hasNext()) {
56 : // it.next();
57 : // LOGDA() << it.name() << ": " << it.value().toString();
58 : // }
59 : // }
60 :
61 0 : void EngravingApiV1::setup(QJSValue globalObj)
62 : {
63 : //! NOTE API v1 provides not only one global `api` object,
64 : //! but also a number of others, for example `curScore`,
65 : //! this is the legacy of the Qml plugin.
66 :
67 0 : QJSValue self = engine()->newQObject(this);
68 :
69 0 : static const QSet<QString> ignore = { "objectName", "objectNameChanged" };
70 :
71 0 : QJSValueIterator it(self);
72 0 : while (it.hasNext()) {
73 0 : it.next();
74 :
75 0 : if (ignore.contains(it.name())) {
76 0 : continue;
77 : }
78 :
79 : // LOGDA() << it.name() << ": " << it.value().toString();
80 0 : globalObj.setProperty(it.name(), it.value());
81 : }
82 0 : }
83 :
84 0 : void EngravingApiV1::setApi(PluginAPI* api)
85 : {
86 0 : m_api = api;
87 0 : }
88 :
89 0 : PluginAPI* EngravingApiV1::api() const
90 : {
91 0 : if (!m_api) {
92 0 : m_api = new PluginAPI(iocContext());
93 0 : m_selfApi = true;
94 : }
95 :
96 0 : return m_api;
97 : }
|