MuseScore  3.4
Music composition and notation
script.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 __SCRIPT_H__
14 #define __SCRIPT_H__
15 
16 #include "scriptentry.h"
17 
18 namespace Ms {
19 
20 class MasterScore;
21 class MuseScore;
22 class ScoreView;
23 
24 //---------------------------------------------------------
25 // ScriptContext
26 //---------------------------------------------------------
27 
30  QDir _cwd; // current working directory
31  bool _relativePaths = true;
32  bool _stopOnError = true;
33  std::unique_ptr<QTextStream> _execLog;
34 
35  public:
37 
38  MuseScore* mscore() { return _mscore; }
39  const MuseScore* mscore() const { return _mscore; }
40 
41  const QDir& cwd() const { return _cwd; }
42  void setCwd(QDir dir) { _cwd = dir; }
43  bool relativePaths() const { return _relativePaths; }
44  void setRelativePaths(bool rel) { _relativePaths = rel; }
45  bool stopOnError() const { return _stopOnError; }
46  void setStopOnError(bool stop) { _stopOnError = stop; }
47 
48  QString absoluteFilePath(const QString& filePath) const { return _cwd.absoluteFilePath(filePath); }
49  QString relativeFilePath(const QString& filePath) const { return _cwd.relativeFilePath(filePath); }
50  QTextStream& execLog(); // logging stream to be used while executing scripts
51  };
52 
53 //---------------------------------------------------------
54 // Script
55 //---------------------------------------------------------
56 
57 class Script final {
58  std::vector<std::unique_ptr<ScriptEntry>> _entries;
59 
60  public:
61  const ScriptEntry& entry(size_t n) const { return *_entries[n]; }
62  const ScriptEntry& lastEntry() const { return *_entries.back(); }
63  size_t nentries() const { return _entries.size(); }
64  bool empty() const { return _entries.empty(); }
65 
66  bool execute(ScriptContext& ctx) const;
67 
68  void clear() { _entries.clear(); }
69  void addEntry(std::unique_ptr<ScriptEntry>&& e) { if (e) _entries.push_back(std::move(e)); }
70  void addEntry(ScriptEntry* e) { _entries.emplace_back(e); }
71  void addCommand(const QByteArray& cmd) { _entries.emplace_back(new CommandScriptEntry(cmd)); }
72  void addCommand(const QString& cmd) { addCommand(cmd.toLatin1()); }
73 
74  void addFromLine(const QString& line);
75 
76  static std::unique_ptr<Script> fromFile(QString fileName);
77  void writeToFile(QString fileName) const;
78  };
79 
80 //---------------------------------------------------------
81 // ScriptRecorder
82 // Records script writing it to a file just on the fly
83 // so that the script is preserved in case actions
84 // series wasn't finished properly (e.g. in case of
85 // MuseScore crash).
86 //---------------------------------------------------------
87 
89  QFile _file;
90  QTextStream _stream;
93 
94  bool _recording = false;
95  size_t _recorded = 0;
96 
97  bool ensureFileOpen();
98  void syncRecord();
99 
100  public:
101  ScriptRecorder(MuseScore* context) : _ctx(context) {}
102 
103  bool isRecording() const { return _recording; }
104  void setRecording(bool r) { _recording = r; }
105 
106  void close() { setRecording(false); _file.close(); _script.clear(); _recorded = 0; }
107  void setFile(QString fileName) { close(); _file.setFileName(fileName); }
108  ScriptContext& context() { return _ctx; }
109  const ScriptContext& context() const { return _ctx; }
110 
111  void recordInitState();
112  void recordCommand(const QString& name);
113  void recordPaletteElement(Element* e);
114  void recordInspectorValueChange(const Element*, const InspectorItem&, const QVariant& value);
115  void recordCurrentScoreChange();
116  void recordScoreTest(QString scoreName = QString());
117  };
118 
119 } // namespace Ms
120 #endif
ScriptContext(MuseScore *mscore)
Definition: script.cpp:28
const ScriptEntry & lastEntry() const
Definition: script.h:62
void clear()
Definition: script.h:68
void setRelativePaths(bool rel)
Definition: script.h:44
size_t nentries() const
Definition: script.h:63
Definition: script.h:57
Definition: musescore.h:180
QDir _cwd
Definition: script.h:30
bool relativePaths() const
Definition: script.h:43
QTextStream & execLog()
Definition: script.cpp:41
Script _script
Definition: script.h:91
QString absoluteFilePath(const QString &filePath) const
Definition: script.h:48
bool isRecording() const
Definition: script.h:103
Base class of score layout elements.
Definition: element.h:158
std::unique_ptr< QTextStream > _execLog
Definition: script.h:33
MuseScore * _mscore
Definition: script.h:29
const MuseScore * mscore() const
Definition: script.h:39
bool _stopOnError
Definition: script.h:32
Definition: inspectorBase.h:37
void setFile(QString fileName)
Definition: script.h:107
bool stopOnError() const
Definition: script.h:45
MuseScore * mscore()
Definition: script.h:38
void close()
Definition: script.h:106
void addEntry(std::unique_ptr< ScriptEntry > &&e)
Definition: script.h:69
ScriptContext & context()
Definition: script.h:108
const ScriptContext & context() const
Definition: script.h:109
Definition: script.h:88
bool _relativePaths
Definition: script.h:31
const ScriptEntry & entry(size_t n) const
Definition: script.h:61
Definition: aeolus.cpp:26
Definition: script.h:28
void addCommand(const QString &cmd)
Definition: script.h:72
void addEntry(ScriptEntry *e)
Definition: script.h:70
QTextStream _stream
Definition: script.h:90
QString relativeFilePath(const QString &filePath) const
Definition: script.h:49
QFile _file
Definition: script.h:89
Definition: scriptentry.h:64
bool empty() const
Definition: script.h:64
void addCommand(const QByteArray &cmd)
Definition: script.h:71
void setStopOnError(bool stop)
Definition: script.h:46
void setCwd(QDir dir)
Definition: script.h:42
Definition: scriptentry.h:27
void setRecording(bool r)
Definition: script.h:104
ScriptRecorder(MuseScore *context)
Definition: script.h:101
std::vector< std::unique_ptr< ScriptEntry > > _entries
Definition: script.h:58
ScriptContext _ctx
Definition: script.h:92
const QDir & cwd() const
Definition: script.h:41