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 207 208 209 210 211 212 213
|
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2002-2012 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 "config.h"
#ifdef SCRIPT_INTERFACE
#include "qmlplugin.h"
#include "shortcut.h"
#include "libmscore/musescoreCore.h"
#include "libmscore/score.h"
#include <QQmlEngine>
namespace Ms {
//---------------------------------------------------------
// QmlPlugin
//---------------------------------------------------------
QmlPlugin::QmlPlugin(QQuickItem* parent)
: QQuickItem(parent)
{
msc = MuseScoreCore::mscoreCore;
}
QmlPlugin::~QmlPlugin()
{
}
//---------------------------------------------------------
// curScore
//---------------------------------------------------------
Score* QmlPlugin::curScore() const
{
return msc->currentScore();
}
//---------------------------------------------------------
// scores
//---------------------------------------------------------
QQmlListProperty<Score> QmlPlugin::scores()
{
return QQmlListProperty<Score>(this, msc->scores());
}
//---------------------------------------------------------
// writeScore
//---------------------------------------------------------
bool QmlPlugin::writeScore(Score* s, const QString& name, const QString& ext)
{
if(!s)
return false;
return msc->saveAs(s, true, name, ext);
}
//---------------------------------------------------------
// readScore
//
// noninteractive can be used to avoid a 'save changes'
// dialog on closing a score that is either imported
// or was created with an older version of MuseScore
//---------------------------------------------------------
Score* QmlPlugin::readScore(const QString& name, bool noninteractive)
{
Score * score = msc->openScore(name);
// tell QML not to garbage collect this score
if (score) {
QQmlEngine::setObjectOwnership(score, QQmlEngine::CppOwnership);
if (noninteractive)
score->setCreated(false);
}
return score;
}
//---------------------------------------------------------
// closeScore
//---------------------------------------------------------
void QmlPlugin::closeScore(Ms::Score* score)
{
msc->closeScore(score);
}
//---------------------------------------------------------
// newElement
//---------------------------------------------------------
Ms::Element* QmlPlugin::newElement(int t)
{
Score* score = curScore();
if (score == 0)
return 0;
Element* e = Element::create(Element::Type(t), score);
// tell QML not to garbage collect this score
Ms::MScore::qml()->setObjectOwnership(e, QQmlEngine::CppOwnership);
return e;
}
//---------------------------------------------------------
// newScore
//---------------------------------------------------------
Score* QmlPlugin::newScore(const QString& name, const QString& part, int measures)
{
if (msc->currentScore())
msc->currentScore()->endCmd();
Score* score = new Score(MScore::defaultStyle());
score->setName(name);
score->appendPart(part);
score->appendMeasures(measures);
score->doLayout();
int view = msc->appendScore(score);
msc->setCurrentView(0, view);
qApp->processEvents();
// tell QML not to garbage collect this score
QQmlEngine::setObjectOwnership(score, QQmlEngine::CppOwnership);
score->startCmd();
return score;
}
//---------------------------------------------------------
// cmd
//---------------------------------------------------------
void QmlPlugin::cmd(const QString& s)
{
Shortcut* sc = Shortcut::getShortcut(qPrintable(s));
if (sc)
msc->cmd(sc->action());
else
qDebug("QmlPlugin:cmd: not found <%s>", qPrintable(s));
}
//---------------------------------------------------------
// openLog
//---------------------------------------------------------
void QmlPlugin::openLog(const QString& name)
{
if (logFile.isOpen())
logFile.close();
logFile.setFileName(name);
if (!logFile.open(QIODevice::WriteOnly))
qDebug("QmlPlugin::openLog: failed");
}
//---------------------------------------------------------
// closeLog
//---------------------------------------------------------
void QmlPlugin::closeLog()
{
if (logFile.isOpen())
logFile.close();
}
//---------------------------------------------------------
// log
//---------------------------------------------------------
void QmlPlugin::log(const QString& txt)
{
if (logFile.isOpen())
logFile.write(txt.toLocal8Bit());
}
//---------------------------------------------------------
// logn
//---------------------------------------------------------
void QmlPlugin::logn(const QString& txt)
{
log(txt);
if (logFile.isOpen())
logFile.write("\n");
}
//---------------------------------------------------------
// log2
//---------------------------------------------------------
void QmlPlugin::log2(const QString& txt, const QString& txt2)
{
logFile.write(txt.toLocal8Bit());
logFile.write(txt2.toLocal8Bit());
logFile.write("\n");
}
//---------------------------------------------------------
// newQProcess
//---------------------------------------------------------
MsProcess* QmlPlugin::newQProcess()
{
return 0; // TODO: new MsProcess(this);
}
}
#endif
|