Skip to content

Commit

Permalink
Switch to lazy initialization of enumerations wrappers for plugins
Browse files Browse the repository at this point in the history
To avoid slow MuseScore start caused by creating enumeration wrappers
on startup, This is mainly the issue for SymId enumeration because
of a large number of its items.
  • Loading branch information
dmitrio95 committed Jun 11, 2020
1 parent 8e3a8ae commit 8f70650
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 85 deletions.
81 changes: 22 additions & 59 deletions mscore/plugin/api/qmlpluginapi.cpp
Expand Up @@ -24,70 +24,34 @@
#include "shortcut.h"
#include "musescore.h"
#include "libmscore/musescoreCore.h"
#include "libmscore/score.h"

#include <QQmlEngine>

namespace Ms {
namespace PluginAPI {

Enum* PluginAPI::elementTypeEnum;
Enum* PluginAPI::accidentalTypeEnum;
Enum* PluginAPI::beamModeEnum;
Enum* PluginAPI::placementEnum;
Enum* PluginAPI::glissandoTypeEnum;
Enum* PluginAPI::layoutBreakTypeEnum;
Enum* PluginAPI::lyricsSyllabicEnum;
Enum* PluginAPI::directionEnum;
Enum* PluginAPI::directionHEnum;
Enum* PluginAPI::ornamentStyleEnum;
Enum* PluginAPI::glissandoStyleEnum;
Enum* PluginAPI::tidEnum;
Enum* PluginAPI::alignEnum;
Enum* PluginAPI::noteTypeEnum;
Enum* PluginAPI::playEventTypeEnum;
Enum* PluginAPI::noteHeadTypeEnum;
Enum* PluginAPI::noteHeadSchemeEnum;
Enum* PluginAPI::noteHeadGroupEnum;
Enum* PluginAPI::noteValueTypeEnum;
Enum* PluginAPI::segmentTypeEnum;
Enum* PluginAPI::spannerAnchorEnum;
Enum* PluginAPI::symIdEnum;

//---------------------------------------------------------
// PluginAPI::initEnums
//---------------------------------------------------------

void PluginAPI::initEnums() {
static bool initialized = false;
if (initialized)
return;

PluginAPI::elementTypeEnum = wrapEnum<Ms::ElementType>();
PluginAPI::accidentalTypeEnum = wrapEnum<Ms::AccidentalType>();
PluginAPI::beamModeEnum = wrapEnum<Ms::Beam::Mode>();
PluginAPI::placementEnum = wrapEnum<Ms::Placement>();
PluginAPI::glissandoTypeEnum = wrapEnum<Ms::GlissandoType>();
PluginAPI::layoutBreakTypeEnum = wrapEnum<Ms::LayoutBreak::Type>();
PluginAPI::lyricsSyllabicEnum = wrapEnum<Ms::Lyrics::Syllabic>();
PluginAPI::directionEnum = wrapEnum<Ms::Direction>();
PluginAPI::directionHEnum = wrapEnum<Ms::MScore::DirectionH>();
PluginAPI::ornamentStyleEnum = wrapEnum<Ms::MScore::OrnamentStyle>();
PluginAPI::glissandoStyleEnum = wrapEnum<Ms::GlissandoStyle>();
PluginAPI::tidEnum = wrapEnum<Ms::Tid>();
PluginAPI::alignEnum = wrapEnum<Ms::Align>();
PluginAPI::noteTypeEnum = wrapEnum<Ms::NoteType>();
PluginAPI::playEventTypeEnum = wrapEnum<Ms::PlayEventType>();
PluginAPI::noteHeadTypeEnum = wrapEnum<Ms::NoteHead::Type>();
PluginAPI::noteHeadSchemeEnum = wrapEnum<Ms::NoteHead::Scheme>();
PluginAPI::noteHeadGroupEnum = wrapEnum<Ms::NoteHead::Group>();
PluginAPI::noteValueTypeEnum = wrapEnum<Ms::Note::ValueType>();
PluginAPI::segmentTypeEnum = wrapEnum<Ms::SegmentType>();
PluginAPI::spannerAnchorEnum = wrapEnum<Ms::Spanner::Anchor>();
PluginAPI::symIdEnum = wrapEnum<Ms::SymId>();

initialized = true;
}
Enum* PluginAPI::elementTypeEnum = nullptr;
Enum* PluginAPI::accidentalTypeEnum = nullptr;
Enum* PluginAPI::beamModeEnum = nullptr;
Enum* PluginAPI::placementEnum = nullptr;
Enum* PluginAPI::glissandoTypeEnum = nullptr;
Enum* PluginAPI::layoutBreakTypeEnum = nullptr;
Enum* PluginAPI::lyricsSyllabicEnum = nullptr;
Enum* PluginAPI::directionEnum = nullptr;
Enum* PluginAPI::directionHEnum = nullptr;
Enum* PluginAPI::ornamentStyleEnum = nullptr;
Enum* PluginAPI::glissandoStyleEnum = nullptr;
Enum* PluginAPI::tidEnum = nullptr;
Enum* PluginAPI::alignEnum = nullptr;
Enum* PluginAPI::noteTypeEnum = nullptr;
Enum* PluginAPI::playEventTypeEnum = nullptr;
Enum* PluginAPI::noteHeadTypeEnum = nullptr;
Enum* PluginAPI::noteHeadSchemeEnum = nullptr;
Enum* PluginAPI::noteHeadGroupEnum = nullptr;
Enum* PluginAPI::noteValueTypeEnum = nullptr;
Enum* PluginAPI::segmentTypeEnum = nullptr;
Enum* PluginAPI::spannerAnchorEnum = nullptr;
Enum* PluginAPI::symIdEnum = nullptr;

//---------------------------------------------------------
// PluginAPI
Expand All @@ -96,7 +60,6 @@ void PluginAPI::initEnums() {
PluginAPI::PluginAPI(QQuickItem* parent)
: Ms::QmlPlugin(parent)
{
initEnums();
setRequiresScore(true); // by default plugins require a score to work
}

Expand Down
57 changes: 31 additions & 26 deletions mscore/plugin/api/qmlpluginapi.h
Expand Up @@ -16,8 +16,11 @@
#include "config.h"
#include "../qmlplugin.h"
#include "enums.h"
#include "libmscore/lyrics.h"
#include "libmscore/mscore.h"
#include "libmscore/utils.h"
#include "libmscore/score.h"
#include "libmscore/spanner.h"

namespace Ms {

Expand All @@ -36,10 +39,14 @@ class FractionWrapper;
class MsProcess;
class Score;

#define DECLARE_API_ENUM(qmlName, cppName) \
#define DECLARE_API_ENUM(qmlName, cppName, enumName) \
Q_PROPERTY(Ms::PluginAPI::Enum* qmlName READ get_##cppName CONSTANT) \
static Enum* cppName; \
static Enum* get_##cppName() { return cppName; }
static Enum* get_##cppName() { \
if (!cppName) \
cppName = wrapEnum<enumName>(); \
return cppName; \
}

//---------------------------------------------------------
/// \class PluginAPI
Expand Down Expand Up @@ -89,77 +96,75 @@ class PluginAPI : public Ms::QmlPlugin {

// Should be initialized in qmlpluginapi.cpp
/// Contains Ms::ElementType enumeration values
DECLARE_API_ENUM( Element, elementTypeEnum )
DECLARE_API_ENUM( Element, elementTypeEnum, Ms::ElementType )
/// Contains Ms::AccidentalType enumeration values
DECLARE_API_ENUM( Accidental, accidentalTypeEnum )
DECLARE_API_ENUM( Accidental, accidentalTypeEnum, Ms::AccidentalType )
/// Contains Ms::Beam::Mode enumeration values
DECLARE_API_ENUM( Beam, beamModeEnum )
DECLARE_API_ENUM( Beam, beamModeEnum, Ms::Beam::Mode )
/// Contains Ms::Placement enumeration values
/// \note In MuseScore 2.X this enumeration was available as
/// Element.ABOVE and Element.BELOW.
DECLARE_API_ENUM( Placement, placementEnum )
DECLARE_API_ENUM( Placement, placementEnum, Ms::Placement )
/// Contains Ms::GlissandoType enumeration values
DECLARE_API_ENUM( Glissando, glissandoTypeEnum ) // was probably absent in 2.X
DECLARE_API_ENUM( Glissando, glissandoTypeEnum, Ms::GlissandoType ) // was probably absent in 2.X
/// Contains Ms::LayoutBreak::Type enumeration values
DECLARE_API_ENUM( LayoutBreak, layoutBreakTypeEnum )
DECLARE_API_ENUM( LayoutBreak, layoutBreakTypeEnum, Ms::LayoutBreak::Type )
/// Contains Ms::Lyrics::Syllabic enumeration values
DECLARE_API_ENUM( Lyrics, lyricsSyllabicEnum )
DECLARE_API_ENUM( Lyrics, lyricsSyllabicEnum, Ms::Lyrics::Syllabic )
/// Contains Ms::Direction enumeration values
/// \note In MuseScore 2.X this enumeration was available as
/// MScore.UP, MScore.DOWN, MScore.AUTO.
DECLARE_API_ENUM( Direction, directionEnum )
DECLARE_API_ENUM( Direction, directionEnum, Ms::Direction )
/// Contains Ms::MScore::DirectionH enumeration values
/// \note In MuseScore 2.X this enumeration was available as
/// MScore.LEFT, MScore.RIGHT, MScore.AUTO.
DECLARE_API_ENUM( DirectionH, directionHEnum )
DECLARE_API_ENUM( DirectionH, directionHEnum, Ms::MScore::DirectionH )
/// Contains Ms::MScore::OrnamentStyle enumeration values
/// \note In MuseScore 2.X this enumeration was available as
/// MScore.DEFAULT, MScore.BAROQUE.
DECLARE_API_ENUM( OrnamentStyle, ornamentStyleEnum )
DECLARE_API_ENUM( OrnamentStyle, ornamentStyleEnum, Ms::MScore::OrnamentStyle )
/// Contains Ms::GlissandoStyle enumeration values
/// \note In MuseScore 2.X this enumeration was available as
/// MScore.CHROMATIC, MScore.WHITE_KEYS, MScore.BLACK_KEYS,
/// MScore.DIATONIC.
DECLARE_API_ENUM( GlissandoStyle, glissandoStyleEnum )
DECLARE_API_ENUM( GlissandoStyle, glissandoStyleEnum, Ms::GlissandoStyle )
/// Contains Ms::Tid enumeration values
/// \note In MuseScore 2.X this enumeration was available as
/// TextStyleType (TextStyleType.TITLE etc.)
DECLARE_API_ENUM( Tid, tidEnum )
DECLARE_API_ENUM( Tid, tidEnum, Ms::Tid )
/// Contains Ms::Align enumeration values
/// \since MuseScore 3.3
DECLARE_API_ENUM( Align, alignEnum )
DECLARE_API_ENUM( Align, alignEnum, Ms::Align )
/// Contains Ms::NoteType enumeration values
/// \since MuseScore 3.2.1
DECLARE_API_ENUM( NoteType, noteTypeEnum )
DECLARE_API_ENUM( NoteType, noteTypeEnum, Ms::NoteType )
/// Contains Ms::PlayEventType enumeration values
/// \since MuseScore 3.3
DECLARE_API_ENUM( PlayEventType, playEventTypeEnum )
DECLARE_API_ENUM( PlayEventType, playEventTypeEnum, Ms::PlayEventType )
/// Contains Ms::NoteHead::Type enumeration values
/// \note In MuseScore 2.X this enumeration was available in
/// NoteHead class (e.g. NoteHead.HEAD_QUARTER).
DECLARE_API_ENUM( NoteHeadType, noteHeadTypeEnum )
DECLARE_API_ENUM( NoteHeadType, noteHeadTypeEnum, Ms::NoteHead::Type )
/// Contains Ms::NoteHead::Scheme enumeration values
/// \since MuseScore 3.5
DECLARE_API_ENUM( NoteHeadScheme, noteHeadSchemeEnum )
DECLARE_API_ENUM( NoteHeadScheme, noteHeadSchemeEnum, Ms::NoteHead::Scheme )
/// Contains Ms::NoteHead::Group enumeration values
/// \note In MuseScore 2.X this enumeration was available in
/// NoteHead class (e.g. NoteHead.HEAD_TRIANGLE).
DECLARE_API_ENUM( NoteHeadGroup, noteHeadGroupEnum )
DECLARE_API_ENUM( NoteHeadGroup, noteHeadGroupEnum, Ms::NoteHead::Group )
/// Contains Ms::Note::ValueType enumeration values
/// \note In MuseScore 2.X this enumeration was available as
/// Note.OFFSET_VAL, Note.USER_VAL
DECLARE_API_ENUM( NoteValueType, noteValueTypeEnum )
DECLARE_API_ENUM( NoteValueType, noteValueTypeEnum, Ms::Note::ValueType )
/// Contains Ms::SegmentType enumeration values
DECLARE_API_ENUM( Segment, segmentTypeEnum )
DECLARE_API_ENUM( Spanner, spannerAnchorEnum ) // probably unavailable in 2.X
DECLARE_API_ENUM( Segment, segmentTypeEnum, Ms::SegmentType )
DECLARE_API_ENUM( Spanner, spannerAnchorEnum, Ms::Spanner::Anchor ) // probably unavailable in 2.X
/// Contains Ms::SymId enumeration values
/// \since MuseScore 3.5
DECLARE_API_ENUM( SymId, symIdEnum )
DECLARE_API_ENUM( SymId, symIdEnum, Ms::SymId )

QFile logFile;

static void initEnums();

signals:
/// Indicates that the plugin was launched.
/// Implement \p onRun() function in your plugin to handle this signal.
Expand Down

0 comments on commit 8f70650

Please sign in to comment.