MuseScore  3.4
Music composition and notation
scriptentry.h
Go to the documentation of this file.
1 //=============================================================================
2 // MuseScore
3 // Music Composition & Notation
4 //
5 // Copyright (C) 2018 Werner Schweer
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2
9 // as published by the Free Software Foundation and appearing in
10 // the file LICENCE.GPL
11 //=============================================================================
12 
13 #ifndef __SCRIPTENTRY_H__
14 #define __SCRIPTENTRY_H__
15 
16 #include "libmscore/element.h"
17 
18 namespace Ms {
19 
20 struct InspectorItem;
21 class ScriptContext;
22 
23 //---------------------------------------------------------
24 // ScriptEntry
25 //---------------------------------------------------------
26 
27 class ScriptEntry {
28  protected:
29  static constexpr const char* SCRIPT_INIT = "init";
30  static constexpr const char* SCRIPT_CMD = "cmd";
31  static constexpr const char* SCRIPT_PALETTE = "palette";
32  static constexpr const char* SCRIPT_INSPECTOR = "inspector";
33  static constexpr const char* SCRIPT_EXCERPT_CHANGE = "excerpt";
34  static constexpr const char* SCRIPT_TEST = "test";
35 
36  static QString entryTemplate(const char* entryType) { return QString("%1 %2").arg(entryType); }
37 
38  public:
39  virtual ~ScriptEntry() = default;
40  virtual bool execute(ScriptContext& ctx) const = 0;
41  virtual QString serialize() const = 0;
42  static std::unique_ptr<ScriptEntry> deserialize(const QString& line);
43  };
44 
45 //---------------------------------------------------------
46 // InitScriptEntry
47 // Initializes the environment for the script.
48 //---------------------------------------------------------
49 
50 class InitScriptEntry : public ScriptEntry {
51  QString _filePath;
52  public:
53  explicit InitScriptEntry(const ScriptContext& ctx);
54  explicit InitScriptEntry(const QString& filePath) : _filePath(filePath) {}
55  bool execute(ScriptContext& ctx) const override;
56  QString serialize() const override;
57  static std::unique_ptr<ScriptEntry> deserialize(const QStringList& tokens);
58  };
59 
60 //---------------------------------------------------------
61 // CommandScriptEntry
62 //---------------------------------------------------------
63 
65  QByteArray _command;
66  public:
67  explicit CommandScriptEntry(const QByteArray& cmd) : _command(cmd) {}
68  explicit CommandScriptEntry(const QString& cmd) : _command(cmd.toLatin1()) {}
69  explicit CommandScriptEntry(const char* cmd) : _command(cmd) {}
70  bool execute(ScriptContext& ctx) const override;
71  QString serialize() const override { return entryTemplate(SCRIPT_CMD).arg(_command.constData()); };
72  };
73 
74 //---------------------------------------------------------
75 // PaletteElementScriptEntry
76 //---------------------------------------------------------
77 
80  std::vector<std::pair<Pid, QString>> _props;
81  static const std::initializer_list<Pid> _pids;
82 
83  PaletteElementScriptEntry(ElementType type, std::vector<std::pair<Pid, QString>>&& props) : _type(type), _props(props) {}
84 
85  public:
86  bool execute(ScriptContext& ctx) const override;
87  static std::unique_ptr<ScriptEntry> fromContext(const Element* e, ScriptContext& ctx);
88  QString serialize() const override;
89  static std::unique_ptr<ScriptEntry> deserialize(const QStringList& tokens);
90  };
91 
92 //---------------------------------------------------------
93 // InspectorScriptEntry
94 //---------------------------------------------------------
95 
100  QVariant _val;
101 
102  public:
103  InspectorScriptEntry(ElementType type, int parentLevel, Pid pid, QVariant value)
104  : _type(type), _parentLevel(parentLevel), _pid(pid), _val(std::move(value)) {}
105  bool execute(ScriptContext& ctx) const override;
106  QString serialize() const override;
107  static std::unique_ptr<ScriptEntry> fromContext(const Element*, const InspectorItem&, const QVariant&);
108  static std::unique_ptr<ScriptEntry> deserialize(const QStringList& tokens);
109  };
110 
111 //---------------------------------------------------------
112 // ExcerptChangeEntry
114 //---------------------------------------------------------
115 
117  int _index;
118 
119  public:
120  ExcerptChangeScriptEntry(int idx) : _index(idx) {}
121  bool execute(ScriptContext& ctx) const override;
122  QString serialize() const override;
123  static std::unique_ptr<ScriptEntry> fromContext(const ScriptContext&);
124  static std::unique_ptr<ScriptEntry> deserialize(const QStringList& tokens);
125  };
126 } // namespace Ms
127 #endif
static std::unique_ptr< ScriptEntry > deserialize(const QString &line)
Definition: scriptentry.cpp:37
CommandScriptEntry(const QByteArray &cmd)
Definition: scriptentry.h:67
Pid
Definition: property.h:62
static constexpr const char * SCRIPT_EXCERPT_CHANGE
Definition: scriptentry.h:33
InspectorScriptEntry(ElementType type, int parentLevel, Pid pid, QVariant value)
Definition: scriptentry.h:103
CommandScriptEntry(const char *cmd)
Definition: scriptentry.h:69
static constexpr const char * SCRIPT_INSPECTOR
Definition: scriptentry.h:32
Base class of score layout elements.
Definition: element.h:158
Represents switching between excerpt tabs.
Definition: scriptentry.h:116
virtual bool execute(ScriptContext &ctx) const =0
static QString entryTemplate(const char *entryType)
Definition: scriptentry.h:36
Definition: scriptentry.h:78
virtual ~ScriptEntry()=default
Definition: inspectorBase.h:37
QByteArray _command
Definition: scriptentry.h:65
ElementType _type
Definition: scriptentry.h:79
static constexpr const char * SCRIPT_TEST
Definition: scriptentry.h:34
QString serialize() const override
Definition: scriptentry.h:71
int _parentLevel
Definition: scriptentry.h:98
static constexpr const char * SCRIPT_CMD
Definition: scriptentry.h:30
Definition: scriptentry.h:96
InitScriptEntry(const QString &filePath)
Definition: scriptentry.h:54
ElementType _type
Definition: scriptentry.h:97
std::vector< std::pair< Pid, QString > > _props
Definition: scriptentry.h:80
Definition: aeolus.cpp:26
static constexpr const char * SCRIPT_INIT
Definition: scriptentry.h:29
Definition: script.h:28
Definition: scriptentry.h:50
PaletteElementScriptEntry(ElementType type, std::vector< std::pair< Pid, QString >> &&props)
Definition: scriptentry.h:83
int _index
Definition: scriptentry.h:117
QVariant _val
Definition: scriptentry.h:100
ExcerptChangeScriptEntry(int idx)
Definition: scriptentry.h:120
Definition: scriptentry.h:64
Pid _pid
Definition: scriptentry.h:99
ElementType
Definition: types.h:34
static const std::initializer_list< Pid > _pids
Definition: scriptentry.h:81
static constexpr const char * SCRIPT_PALETTE
Definition: scriptentry.h:31
Definition: scriptentry.h:27
CommandScriptEntry(const QString &cmd)
Definition: scriptentry.h:68
virtual QString serialize() const =0
QString _filePath
Definition: scriptentry.h:51