MuseScore/mscore/qmlplugin.cpp
Maurizio M. Gavioli d11f1b118b Fixes the following Q_INVOKABLE methods returning a QObject* by turning them into a property:
- Measure:
-- firstSegment
-- lastSegment
- MeasureBase:
-- nextMeasure
-- nextMeasureMM (new)
-- prevMeasure
-- prevMeasureMM (new)
- Score:
-- firstMeasure
-- firstMeasureMM (new)
-- (for firstSegment(), see special cases below)
-- lastMeasure
-- lastMeasureMM (new)
-- lastSegment

- Segment:
-- next (renamed from `next1`)
-- nextInMeasure (renamed from `next`)
-- prev (renamed from `prev1`)
-- prevInMeasure (renamed from prev)

Special cases:

- Cursor: The prototype of the `Q_INVOKABLE Ms::Note* Cursor::addNote(int pitch)` was wrong: corrected in `Q_INVOKABLE void Cursor::addNote(int pitch)`.
- QmlPlugin: `Q_INVOKABLE Score* QmlPlugin::readScore()` and `Q_INVOKABLE Score* QmlPlugin::newScore()` has been kept, as they are intended to be called from QML; code has been added to ensure the C++ ownership of the returned object.
- Score: `Q_INVOKABLE Segment* Score::firstSegment(Segment::Type segType)` is kept (as it needs a parameters), but code is added to ensure C++ ownership of the returned Segment*.
- Segment: `Ms::Element* Segment::element(int track)` has been made NOT Q_INVOKABLE; a variant `Q_INVOKABLE Ms::Element* elementAt(int track)` has been added specifically for QML with code to ensure the C++ ownership of the returned Element* (this was the cause for the crash of the Walk plug-in).
- FiguredBass: `Q_INVOKABLE Ms::FiguredBassItem* FiguredBass::addItem()` has been removed; plugin interface for FiguredBass needs to be redesigned anyway.

The few occurrences in the supplied plug-ins of the methods whose names did change have been updated.
2014-07-06 01:56:30 +02:00

141 lines
4 KiB
C++

//=============================================================================
// 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 "qmlplugin.h"
#include "shortcut.h"
#include "musescoreCore.h"
#include "libmscore/score.h"
#include <QQmlEngine>
namespace Ms {
extern MuseScoreCore* mscoreCore;
//---------------------------------------------------------
// QmlPlugin
//---------------------------------------------------------
QmlPlugin::QmlPlugin(QQuickItem* parent)
: QQuickItem(parent)
{
msc = 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
//---------------------------------------------------------
Score* QmlPlugin::readScore(const QString& name)
{
Score * score = msc->openScore(name);
// tell QML not to garbage collect this score
if (score)
QQmlEngine::setObjectOwnership(score, QQmlEngine::CppOwnership);
return 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();
msc->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(s.toLatin1().data());
if (sc)
msc->cmd(sc->action());
else
qDebug("QmlPlugin:cmd: not found <%s>", qPrintable(s));
}
//---------------------------------------------------------
// newQProcess
//---------------------------------------------------------
MsProcess* QmlPlugin::newQProcess()
{
return 0; // TODO: new MsProcess(this);
}
}