File: script.cpp

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 (206 lines) | stat: -rw-r--r-- 6,234 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//=============================================================================
//  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
//=============================================================================

#include "script.h"

#include "musescore.h"
#include "scoreview.h"
#include "scriptentry.h"
#include "testscript.h"

#include "libmscore/score.h"

namespace Ms {

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

ScriptContext::ScriptContext(MuseScore* context)
   : _mscore(context), _cwd(QDir::current())
      {
      }

//---------------------------------------------------------
//   ScriptContext::execLog
//    Returns logging stream to be used by script entries
//    at execution time.
//    Current implementation returns a stream writing to
//    stdout performing its initialization if necessary.
//---------------------------------------------------------

QTextStream& ScriptContext::execLog()
      {
      if (!_execLog)
            _execLog.reset(new QTextStream(stdout));
      return *_execLog;
      }

//---------------------------------------------------------
//   Script::execute
//---------------------------------------------------------

bool Script::execute(ScriptContext& ctx) const
      {
      bool success = true;
      for (const auto& e : _entries) {
            if (e) {
                  if (!e->execute(ctx)) {
                        ctx.execLog() << "Script::execute: operation failed:" << e->serialize() << endl;
                        if (ctx.stopOnError())
                              return false;
                        success = false;
                        }
                  }
            }
      return success;
      }

//---------------------------------------------------------
//   Script::addFromLine
//---------------------------------------------------------

void Script::addFromLine(const QString& line)
      {
      addEntry(ScriptEntry::deserialize(line));
      }

//---------------------------------------------------------
//   Script::fromFile
//---------------------------------------------------------

std::unique_ptr<Script> Script::fromFile(QString fileName)
      {
      QFile file(fileName);
      if (!file.open(QIODevice::ReadOnly))
            return nullptr;
      QTextStream stream(&file);

      QString line;
      auto script = std::unique_ptr<Script>(new Script());
      while (stream.readLineInto(&line))
            script->addFromLine(line);
      return script;
      }

//---------------------------------------------------------
//   Script::writeToFile
//---------------------------------------------------------

void Script::writeToFile(QString fileName) const
      {
      QFile file(fileName);
      if (!file.open(QIODevice::WriteOnly))
            return;
      QTextStream stream(&file);

      for (const auto& e : _entries) {
            if (e)
                  stream << e->serialize() << '\n';
            }
      }

//---------------------------------------------------------
//   ScriptRecorder::ensureFileOpen
//---------------------------------------------------------

bool ScriptRecorder::ensureFileOpen()
      {
      if (_file.isOpen())
            return true;
      if (_file.open(QIODevice::WriteOnly)) {
            _stream.setDevice(&_file);
            return true;
            }
      return false;
      }

//---------------------------------------------------------
//   ScriptRecorder::syncRecord
//---------------------------------------------------------

void ScriptRecorder::syncRecord()
      {
      if (!_recording)
            return;
      if (!ensureFileOpen())
            return;
      for (; _recorded < _script.nentries(); ++_recorded)
            _stream << _script.entry(_recorded).serialize() << endl;
      }

//---------------------------------------------------------
//   ScriptRecorder::recordInitState
//---------------------------------------------------------

void ScriptRecorder::recordInitState()
      {
      if (_recording)
            _script.addEntry(new InitScriptEntry(_ctx));
      syncRecord();
      }

//---------------------------------------------------------
//   ScriptRecorder::recordCommand
//---------------------------------------------------------

void ScriptRecorder::recordCommand(const QString& name)
      {
      if (_recording)
            _script.addCommand(name);
      syncRecord();
      }

//---------------------------------------------------------
//   ScriptRecorder::recordPaletteElement
//---------------------------------------------------------

void ScriptRecorder::recordPaletteElement(Element* e)
      {
      if (_recording)
            _script.addEntry(PaletteElementScriptEntry::fromContext(e, _ctx));
      syncRecord();
      }

//---------------------------------------------------------
//   ScriptRecorder::recordCurrentScoreChange
//---------------------------------------------------------

void ScriptRecorder::recordCurrentScoreChange()
      {
      if (_recording)
            _script.addEntry(ExcerptChangeScriptEntry::fromContext(_ctx));
      syncRecord();
      }

//---------------------------------------------------------
//   ScriptRecorder::recordInspectorValueChange
//---------------------------------------------------------

void ScriptRecorder::recordInspectorValueChange(const Element* e, const InspectorItem& ii, const QVariant& value)
      {
      if (_recording)
            _script.addEntry(InspectorScriptEntry::fromContext(e, ii, value));
      syncRecord();
      }

//---------------------------------------------------------
//   ScriptRecorder::recordScoreTest
//---------------------------------------------------------

void ScriptRecorder::recordScoreTest(QString scoreName)
      {
      if (_recording)
            _script.addEntry(ScoreTestScriptEntry::fromContext(_ctx, scoreName));
      syncRecord();
      }
}