File: script.h

package info (click to toggle)
musescore3 3.2.3%2Bdfsg2-16
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 214,188 kB
  • sloc: cpp: 291,198; xml: 200,238; sh: 3,779; ansic: 1,447; python: 393; makefile: 244; perl: 82; pascal: 79
file content (120 lines) | stat: -rw-r--r-- 4,225 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//=============================================================================
//  MuseScore
//  Music Composition & Notation
//
//  Copyright (C) 2018 Werner Schweer
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2
//  as published by the Free Software Foundation and appearing in
//  the file LICENCE.GPL
//=============================================================================

#ifndef __SCRIPT_H__
#define __SCRIPT_H__

#include "scriptentry.h"

namespace Ms {

class MasterScore;
class MuseScore;
class ScoreView;

//---------------------------------------------------------
//   ScriptContext
//---------------------------------------------------------

class ScriptContext {
      MuseScore* _mscore;
      QDir _cwd; // current working directory
      bool _relativePaths = true;
      bool _stopOnError = true;
      std::unique_ptr<QTextStream> _execLog;

   public:
      ScriptContext(MuseScore* mscore);

      MuseScore* mscore() { return _mscore; }
      const MuseScore* mscore() const { return _mscore; }

      const QDir& cwd() const { return _cwd; }
      void setCwd(QDir dir) { _cwd = dir; }
      bool relativePaths() const { return _relativePaths; }
      void setRelativePaths(bool rel) { _relativePaths = rel; }
      bool stopOnError() const { return _stopOnError; }
      void setStopOnError(bool stop) { _stopOnError = stop; }

      QString absoluteFilePath(const QString& filePath) const { return _cwd.absoluteFilePath(filePath); }
      QString relativeFilePath(const QString& filePath) const { return _cwd.relativeFilePath(filePath); }
      QTextStream& execLog(); // logging stream to be used while executing scripts
      };

//---------------------------------------------------------
//   Script
//---------------------------------------------------------

class Script final {
      std::vector<std::unique_ptr<ScriptEntry>> _entries;

   public:
      const ScriptEntry& entry(size_t n) const { return *_entries[n]; }
      const ScriptEntry& lastEntry() const { return *_entries.back(); }
      size_t nentries() const { return _entries.size(); }
      bool empty() const { return _entries.empty(); }

      bool execute(ScriptContext& ctx) const;

      void clear() { _entries.clear(); }
      void addEntry(std::unique_ptr<ScriptEntry>&& e) { if (e) _entries.push_back(std::move(e)); }
      void addEntry(ScriptEntry* e) { _entries.emplace_back(e); }
      void addCommand(const QByteArray& cmd) { _entries.emplace_back(new CommandScriptEntry(cmd)); }
      void addCommand(const QString& cmd) { addCommand(cmd.toLatin1()); }

      void addFromLine(const QString& line);

      static std::unique_ptr<Script> fromFile(QString fileName);
      void writeToFile(QString fileName) const;
      };

//---------------------------------------------------------
//   ScriptRecorder
//    Records script writing it to a file just on the fly
//    so that the script is preserved in case actions
//    series wasn't finished properly (e.g. in case of
//    MuseScore crash).
//---------------------------------------------------------

class ScriptRecorder {
      QFile _file;
      QTextStream _stream;
      Script _script;
      ScriptContext _ctx;

      bool _recording = false;
      size_t _recorded = 0;

      bool ensureFileOpen();
      void syncRecord();

   public:
      ScriptRecorder(MuseScore* context) : _ctx(context) {}

      bool isRecording() const { return _recording; }
      void setRecording(bool r) { _recording = r; }

      void close() { setRecording(false); _file.close(); _script.clear(); _recorded = 0; }
      void setFile(QString fileName) { close(); _file.setFileName(fileName); }
      ScriptContext& context() { return _ctx; }
      const ScriptContext& context() const { return _ctx; }

      void recordInitState();
      void recordCommand(const QString& name);
      void recordPaletteElement(Element* e);
      void recordInspectorValueChange(const Element*, const InspectorItem&, const QVariant& value);
      void recordCurrentScoreChange();
      void recordScoreTest(QString scoreName = QString());
      };

}     // namespace Ms
#endif