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
|
//=============================================================================
// 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 "recorderwidget.h"
#include "ui_script_recorder.h"
#include "musescore.h"
namespace Ms {
//---------------------------------------------------------
// ScriptRecorderWidget
//---------------------------------------------------------
ScriptRecorderWidget::ScriptRecorderWidget(MuseScore* score, QWidget* parent)
: QDockWidget(parent), _ui(new Ui::ScriptRecorderWidget), _mscore(score),
_recorder(score), _scriptDir(QDir::homePath())
{
_ui->setupUi(this);
updateDirLabel();
updateActions();
}
//---------------------------------------------------------
// ~ScriptRecorderWidget
//---------------------------------------------------------
ScriptRecorderWidget::~ScriptRecorderWidget()
{
delete _ui;
}
//---------------------------------------------------------
// ScriptRecorderWidget::changeEvent
//---------------------------------------------------------
void ScriptRecorderWidget::changeEvent(QEvent* e)
{
QDockWidget::changeEvent(e);
switch(e->type()) {
case QEvent::LanguageChange:
_ui->retranslateUi(this);
break;
default:
break;
}
}
//---------------------------------------------------------
// ScriptRecorderWidget::scriptFileName
//---------------------------------------------------------
QString ScriptRecorderWidget::scriptFileName() const
{
QDir dir(scriptDir());
return dir.filePath(_ui->scriptNameEdit->text() + ".script");
}
//---------------------------------------------------------
// ScriptRecorderWidget::updateDirLabel
//---------------------------------------------------------
void ScriptRecorderWidget::updateDirLabel()
{
QFontMetrics metrics(_ui->folderLabel->font());
_ui->folderLabel->setText(metrics.elidedText(_scriptDir, Qt::ElideLeft, _ui->folderLabel->width()));
_ui->folderLabel->setToolTip(_scriptDir);
}
//---------------------------------------------------------
// ScriptRecorderWidget::updateActions
//---------------------------------------------------------
void ScriptRecorderWidget::updateActions()
{
QDir dir(scriptDir());
QFileInfo script(scriptFileName());
_ui->recordButton->setEnabled(dir.exists() && !script.completeBaseName().isEmpty() && !script.exists());
_ui->replayButton->setEnabled(script.isFile());
}
//---------------------------------------------------------
// ScriptRecorderWidget::on_changeFolderButton_clicked
//---------------------------------------------------------
void ScriptRecorderWidget::on_changeFolderButton_clicked()
{
QString newDir = QFileDialog::getExistingDirectory(this, "Open scripts directory");
if (!newDir.isEmpty())
_scriptDir = newDir;
updateDirLabel();
updateActions();
}
//---------------------------------------------------------
// ScriptRecorderWidget::on_scriptNameEdit_textEdited
//---------------------------------------------------------
void ScriptRecorderWidget::on_scriptNameEdit_textEdited()
{
updateActions();
}
//---------------------------------------------------------
// ScriptRecorderWidget::on_recordButton_clicked
//---------------------------------------------------------
void ScriptRecorderWidget::on_recordButton_clicked()
{
const bool startRecord = !_recorder.isRecording();
_ui->changeFolderButton->setEnabled(!startRecord);
_ui->scriptNameEdit->setEnabled(!startRecord);
_ui->replayButton->setEnabled(false);
_ui->recordButton->setText(startRecord ? "Stop recording" : "Start recording");
if (startRecord) {
_recorder.setFile(scriptFileName());
_recorder.context().setCwd(scriptDir());
_recorder.setRecording(true);
_recorder.recordInitState();
}
else {
QFileInfo scriptInfo(scriptFileName());
QString scoreName(scriptInfo.completeBaseName() + ".mscx");
_recorder.recordScoreTest(scoreName);
_recorder.close();
}
if (!_recorder.isRecording())
updateActions();
}
//---------------------------------------------------------
// ScriptRecorderWidget::on_replayButton_clicked
//---------------------------------------------------------
void ScriptRecorderWidget::on_replayButton_clicked()
{
auto script = Script::fromFile(scriptFileName());
if (!script)
return;
ScriptContext ctx(_mscore);
ctx.setCwd(scriptDir());
ctx.setStopOnError(false);
script->execute(ctx);
}
}
|