Skip to content

Commit b414375

Browse files
committed
[ext] added enum support
1 parent 7021dff commit b414375

File tree

21 files changed

+244
-40
lines changed

21 files changed

+244
-40
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import QtQuick
22

33
import MuseApi.Controls
4+
import MuseApi.Engraving
45

56
ExtensionBlank {
67

@@ -11,6 +12,24 @@ ExtensionBlank {
1112

1213
color: api.theme.backgroundPrimaryColor
1314

15+
Component.onCompleted: {
16+
const score = api.engraving.curScore;
17+
const measure = score.firstMeasure;
18+
console.log("measure.type:", measure.type
19+
, ", api.engraving.Element.MEASURE:", api.engraving.Element.MEASURE
20+
, ", Element:", Element
21+
, ", Element.MEASURE:", Element.MEASURE
22+
, ", IconCode:", IconCode
23+
, ", IconCode.STAR:", IconCode.STAR
24+
)
25+
26+
if (measure.type === api.engraving.Element.MEASURE) {
27+
console.log("this is measure")
28+
} else {
29+
console.log("this is not measure")
30+
}
31+
}
32+
1433
StyledFlickable {
1534
anchors.fill: parent
1635

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
const Element = api.engraving.Element;
3+
4+
function main()
5+
{
6+
console.log("macros.js")
7+
8+
const score = api.engraving.curScore;
9+
const measure = score.firstMeasure;
10+
console.log("measure.type:", measure.type, "Element.Measure:", Element.MEASURE)
11+
12+
if (measure.type === Element.MEASURE) {
13+
console.log("this is measure")
14+
}
15+
}
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
{
22
"uri": "musescore://extensions/dev/apitests",
3-
"type": "form",
3+
"type": "composite",
44
"title": "Api tests",
55
"description": "This is a development extension for API tests.",
66
"ui_context": "Any",
77

8-
"path": "Main.qml"
8+
"actions": [
9+
{
10+
"code": "myform",
11+
"type": "form",
12+
"title": "Form",
13+
"path": "Form.qml"
14+
},
15+
{
16+
"code": "mymacros",
17+
"type": "macros",
18+
"title": "Macros",
19+
"path": "macros.js"
20+
}
21+
]
922
}

src/engraving/api/v1/engravingapiv1.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222

2323
#include "engravingapiv1.h"
2424

25+
#include <QQmlEngine>
2526
#include <QJSValueIterator>
2627

28+
#include "apitypes.h"
29+
2730
#include "qmlpluginapi.h"
2831

2932
#include "log.h"
@@ -95,3 +98,9 @@ PluginAPI* EngravingApiV1::api() const
9598

9699
return m_api;
97100
}
101+
102+
QJSValue EngravingApiV1::elementTypeEnum() const
103+
{
104+
static const QJSValue enval = makeEnum<enums::ElementType>(muse::api::EnumType::Int);
105+
return enval;
106+
}

src/engraving/api/v1/engravingapiv1.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
*/
2222
#pragma once
2323

24+
#include <QJSValue>
25+
2426
#include "global/api/apiobject.h"
27+
#include "global/api/apiutils.h"
2528

2629
#include "extensions/api/v1/iapiv1object.h"
2730

@@ -44,7 +47,7 @@ class EngravingApiV1 : public muse::api::ApiObject, public muse::extensions::api
4447

4548
Q_PROPERTY(apiv1::Score * curScore READ curScore CONSTANT)
4649

47-
Q_PROPERTY(apiv1::Enum * Element READ elementTypeEnum CONSTANT)
50+
Q_PROPERTY(QJSValue Element READ elementTypeEnum CONSTANT)
4851
Q_PROPERTY(apiv1::Enum * Accidental READ accidentalTypeEnum CONSTANT)
4952
Q_PROPERTY(apiv1::Enum * AccidentalBracket READ accidentalBracketEnum CONSTANT)
5053
Q_PROPERTY(apiv1::Enum * OrnamentStyle READ ornamentStyleEnum CONSTANT)
@@ -167,7 +170,7 @@ class EngravingApiV1 : public muse::api::ApiObject, public muse::extensions::api
167170

168171
apiv1::Score* curScore() const { return api()->curScore(); }
169172

170-
apiv1::Enum* elementTypeEnum() const { return api()->get_elementTypeEnum(); }
173+
QJSValue elementTypeEnum() const;
171174
apiv1::Enum* accidentalTypeEnum() const { return api()->get_accidentalTypeEnum(); }
172175
apiv1::Enum* accidentalBracketEnum() const { return api()->get_accidentalBracketEnum(); }
173176
apiv1::Enum* ornamentStyleEnum() const { return api()->get_ornamentStyleEnum(); }
@@ -330,6 +333,15 @@ class EngravingApiV1 : public muse::api::ApiObject, public muse::extensions::api
330333
Q_INVOKABLE void quit() { api()->quit(); }
331334

332335
private:
336+
337+
template<typename T>
338+
QJSValue makeEnum(muse::api::EnumType type) const
339+
{
340+
return muse::api::enumToJsValue(engine(),
341+
QMetaEnum::fromType<T>(),
342+
type);
343+
}
344+
333345
mutable PluginAPI* m_api = nullptr;
334346
mutable bool m_selfApi = false;
335347
};

src/engraving/api/v1/qmlpluginapi.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ void PluginAPI::setup(QQmlEngine* e)
231231
}
232232

233233
engravingApi->setApi(this);
234+
m_engine = engravingApi->engine();
234235
}
235236

236237
PluginAPI::PluginAPI(QQuickItem* parent)

src/engraving/api/v1/qmlpluginapi.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
#include "extensions/api/v1/ipluginapiv1.h"
2727

28+
#include "global/api/apiutils.h"
29+
2830
#include "modularity/ioc.h"
2931
#include "actions/iactionsdispatcher.h"
3032
#include "context/iglobalcontext.h"
@@ -36,6 +38,8 @@
3638
#include "cursor.h"
3739
#include "enums.h"
3840

41+
#include "log.h"
42+
3943
namespace mu::engraving {
4044
class EngravingItem;
4145
class Score;
@@ -65,6 +69,14 @@ class Score;
6569
return cppName; \
6670
}
6771

72+
#define DECLARE_API_ENUM_JSVAL(qmlName, getterName, enumType) \
73+
/** Accessed using qmlName.VALUE*/ \
74+
Q_PROPERTY(QJSValue qmlName READ getterName CONSTANT) \
75+
QJSValue getterName() const { \
76+
static const QJSValue enval = makeEnum(qmlName); \
77+
return enval; \
78+
}
79+
6880
#define DECLARE_API_ENUM2(qmlName, cppName, enumName1, enumName2) \
6981
/** Accessed using qmlName.VALUE*/ \
7082
Q_PROPERTY(mu::engraving::apiv1::Enum * qmlName READ get_##cppName CONSTANT) \
@@ -139,7 +151,8 @@ class PluginAPI : public QQuickItem, public muse::extensions::apiv1::IPluginApiV
139151
public:
140152
// Should be initialized in qmlpluginapi.cpp
141153
/// Contains mu::engraving::ElementType enumeration values
142-
DECLARE_API_ENUM(Element, elementTypeEnum, mu::engraving::apiv1::enums::ElementType)
154+
DECLARE_API_ENUM(Element, elementTypeEnum, mu::engraving::apiv1::enums::ElementType)
155+
//DECLARE_API_ENUM_JSVAL(Element, elementTypeEnum, mu::engraving::apiv1::enums::ElementType)
143156
/// Contains mu::engraving::AccidentalType enumeration values
144157
DECLARE_API_ENUM(Accidental, accidentalTypeEnum, mu::engraving::apiv1::enums::AccidentalType)
145158
/// Contains mu::engraving::AccidentalBracket enumeration values
@@ -591,6 +604,19 @@ class PluginAPI : public QQuickItem, public muse::extensions::apiv1::IPluginApiV
591604
private:
592605
mu::engraving::Score* currentScore() const;
593606

607+
template<typename T>
608+
QJSValue makeEnum(const QString& name) const
609+
{
610+
IF_ASSERT_FAILED(m_engine) {
611+
return QJSValue();
612+
}
613+
return muse::api::enumToJsValue(m_engine,
614+
QMetaEnum::fromType<T>(),
615+
muse::api::EnumType::Int,
616+
name);
617+
}
618+
619+
muse::api::IApiEngine* m_engine = nullptr;
594620
QString m_pluginType;
595621
QString m_title;
596622
QString m_version;

src/engraving/engravingmodule.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ void EngravingModule::registerApi()
144144
auto api = ioc()->resolve<muse::api::IApiRegister>(moduleName());
145145
if (api) {
146146
api->regApiCreator(moduleName(), "api.engraving.v1", new muse::api::ApiCreator<apiv1::EngravingApiV1>());
147+
148+
api->regEnum<apiv1::enums::ElementType>("MuseApi.Engraving", muse::api::EnumType::Int, "Element");
147149
}
148150
#endif
149151
}

src/framework/autobot/internal/scriptengine.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ void ScriptEngine::dump(const QString& name, const QJSValue& val)
210210
}
211211

212212
QString str = name + "\n";
213-
for (const QString& prop : props) {
213+
for (const QString& prop : std::as_const(props)) {
214214
str += " " + prop + ": " + val.property(prop).toString() + "\n";
215215
}
216216

@@ -259,3 +259,9 @@ QJSValue ScriptEngine::newArray(size_t length)
259259
{
260260
return m_engine->newArray(uint(length));
261261
}
262+
263+
QJSValue ScriptEngine::freeze(const QJSValue& val)
264+
{
265+
static QJSValue freezeFn = m_engine->evaluate("Object.freeze");
266+
return freezeFn.call({ val });
267+
}

src/framework/autobot/internal/scriptengine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class ScriptEngine : public muse::api::IApiEngine
7272
QJSValue newQObject(QObject* o) override;
7373
QJSValue newObject() override;
7474
QJSValue newArray(size_t length = 0) override;
75+
QJSValue freeze(const QJSValue& val) override;
7576

7677
static void dump(const QString& name, const QJSValue& val);
7778

0 commit comments

Comments
 (0)