Skip to content

Commit

Permalink
Merge pull request #2412 from jeetee/fix_cursor_addNote_cursor_position
Browse files Browse the repository at this point in the history
Fix #100181 cursor addNote segment reference
  • Loading branch information
lasconic committed Mar 29, 2016
2 parents 73f7e7a + 3ce4f22 commit 386bb4f
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 40 deletions.
1 change: 1 addition & 0 deletions libmscore/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ void Cursor::addNote(int pitch)
{
NoteVal nval(pitch);
_score->addPitch(nval, false);
_segment = _score->inputState().segment();
}

//---------------------------------------------------------
Expand Down
3 changes: 1 addition & 2 deletions mtest/libmscore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

subdirs(
album barline beam breath chordsymbol clef clef_courtesy compat concertpitch copypaste
copypastesymbollist dynamic earlymusic element hairpin instrumentchange join keysig layout links parts measure midi
midimapping note plugins repeat selectionfilter selectionrangedelete spanners split splitstaff timesig tools transpose tuplet text
copypastesymbollist cursor dynamic earlymusic element hairpin instrumentchange join keysig layout links parts measure midi midimapping note plugins repeat selectionfilter selectionrangedelete spanners split splitstaff timesig tools transpose tuplet text
)

install(FILES
Expand Down
17 changes: 17 additions & 0 deletions mtest/libmscore/cursor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#=============================================================================
# MuseScore
# Music Composition & Notation
# $Id:$
#
# Copyright (C) 2011 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 LICENSE.GPL
#=============================================================================

set(TARGET tst_cursor)

include(${PROJECT_SOURCE_DIR}/mtest/cmake.inc)

77 changes: 77 additions & 0 deletions mtest/libmscore/cursor/tst_cursor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 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 <QtTest/QtTest>
#include "mtest/testutils.h"

#include "libmscore/cursor.h"
#include "libmscore/score.h"

using namespace Ms;

//---------------------------------------------------------
// TestCursor
//---------------------------------------------------------

class TestCursor : public QObject, public MTest
{
Q_OBJECT

private slots:
void initTestCase();
void testAddNoteTickUpdate();
};

//---------------------------------------------------------
// initTestCase
//---------------------------------------------------------

void TestCursor::initTestCase()
{
initMTest();
}

//---------------------------------------------------------
/// testAddTickUpdate
/// Add an element (note) and verify the tick updated with the correct amount
//---------------------------------------------------------

void TestCursor::testAddNoteTickUpdate()
{
//test setup
score = new Score();
score->appendPart("voice");
score->appendMeasures(2);
Cursor c(score);
//creation of cursor - check default values
QCOMPARE(c.score(), score);
QCOMPARE(c.track(), 0);
QCOMPARE(c.voice(), 0);
QCOMPARE(c.tick(), 0);
QVERIFY(c.segment() == nullptr);
//set the cursor at input position (start of score)
c.rewind(0);
QVERIFY(c.segment() != nullptr);

//actual test
//add 4 times a 1/8th, totalling a half note
c.setDuration(1, 8);
c.addNote(60);
c.addNote(60);
c.addNote(60);
c.addNote(60);
QCOMPARE(c.tick(), MScore::division * 2); //one division == 1 crotchet
}

QTEST_MAIN(TestCursor)
#include "tst_cursor.moc"

11 changes: 4 additions & 7 deletions share/plugins/random.qml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import QtQuick 2.1
import MuseScore 1.0

MuseScore {
version: "2.0"
version: "2.1"
description: "Create random score."
menuPath: "Plugins.random"
requiresScore: false
Expand All @@ -20,7 +20,7 @@ MuseScore {
}

onRun: {
var measures = 18;
var measures = 18; //in 4/4 default time signature
var numerator = 3;
var denominator = 4;
var octaves = 2;
Expand All @@ -41,26 +41,23 @@ MuseScore {
cursor.add(ts);

cursor.rewind(0);
cursor.setDuration(1, denominator);

var realMeasures = Math.floor((measures + numerator - 1) / numerator);
var realMeasures = Math.ceil(measures * denominator / numerator);
console.log(realMeasures);
var notes = realMeasures * numerator;
var notes = realMeasures * 4; //number of 1/4th notes

for (var i = 0; i < notes; ++i) {

if (Math.random() < 0.5) {
cursor.setDuration(1, 8);
addNote(key, cursor);
cursor.next();
addNote(key, cursor);
}
else {
cursor.setDuration(1, 4);
addNote(key, cursor);
}

cursor.next();
}
Qt.quit();
}
Expand Down
61 changes: 30 additions & 31 deletions share/plugins/random2.qml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import QtQuick.Controls 1.0
import QtQuick.Layouts 1.0

MuseScore {
version: "2.0"
version: "2.1"
description: "Create random score."
menuPath: "Plugins.random2"
requiresScore: false
Expand All @@ -13,8 +13,6 @@ MuseScore {
width: 150
height: 75

property variant octaves : 2;

onRun: { }

function addNote(key, cursor) {
Expand All @@ -23,22 +21,22 @@ MuseScore {
var keyo = [ 0, 7, 2, 4 ];

var idx = Math.random() * 6;
var octave = Math.floor(Math.random() * 2);
var octave = Math.floor(Math.random() * octaves.value);
var pitch = cdur[Math.floor(idx)] + octave * 12 + 60;
var pitch = pitch + keyo[key];
console.log("Add note pitch "+pitch);
cursor.addNote(pitch);
}

function createScore() {
var measures = 18;
var measures = 18; //in 4/4 default time signature
var numerator = 3;
var denominator = 4;
var key = 3;
var key = 2; //index in keyo from addNote function above

var score = newScore("Random2.mscz", "piano", measures);

score.startCmd()
score.startCmd();
score.addText("title", "==Random2==");
score.addText("subtitle", "Another subtitle");

Expand All @@ -51,32 +49,32 @@ MuseScore {
ts.setSig(numerator, denominator);
cursor.add(ts);

cursor.rewind(0);
cursor.setDuration(1, denominator);

var realMeasures = Math.floor((measures * 2 + numerator - 1) / numerator);
var realMeasures = Math.ceil(measures * denominator / numerator);
console.log(realMeasures);
var notes = realMeasures * numerator;

for (var i = 0; i < notes; ++i) {
if (Math.random() < 0.5) {
console.log("Adding two notes at ", i);
cursor.setDuration(1, 8);
addNote(key, cursor);
cursor.next();
addNote(key, cursor);
}
else {
console.log("Adding note at ", i);
cursor.setDuration(1, 4);
addNote(key, cursor);
}

cursor.next();
}
score.endCmd()
var notes = realMeasures * 4; //number of 1/4th notes

for (var staff = 0; staff < 2; ++staff) { //piano has two staves to fill
cursor.track = staff * 4; //4 voice tracks per staff
cursor.rewind(0); //go to the start of the score
//add notes
for (var i = 0; i < notes; ++i) {
if (Math.random() < 0.4) {
console.log("Adding two notes at ", i);
cursor.setDuration(1, 8);
addNote(key, cursor);
addNote(key, cursor);
}
else {
console.log("Adding note at ", i);
cursor.setDuration(1, 4);
addNote(key, cursor);
}
} //done adding notes to this staff
}
score.endCmd();
Qt.quit();
}

GridLayout {
anchors.fill: parent
columns: 2
Expand All @@ -89,12 +87,13 @@ MuseScore {
}

SpinBox {
id: octaves
minimumValue: 1
maximumValue: 3
stepSize: 1
Layout.fillWidth: true
Layout.preferredHeight: 25

value: 1
}

Button {
Expand Down

0 comments on commit 386bb4f

Please sign in to comment.