Git commit b6323e0b462ed6c85062a4c236177149257433f2 by Divyam Madaan. Committed on 31/08/2017 at 20:39. Pushed by dmadaan into branch 'playpiano'. Piano_composition: Append melodies during save M +2 -2 src/activities/piano_composition/MultipleStaff.qml M +1 -1 src/activities/piano_composition/Piano_composition.qml M +35 -19 src/activities/piano_composition/piano_composition.js M +24 -0 src/core/File.cpp M +10 -0 src/core/File.h https://commits.kde.org/gcompris/b6323e0b462ed6c85062a4c236177149257433f2 diff --git a/src/activities/piano_composition/MultipleStaff.qml b/src/activ= ities/piano_composition/MultipleStaff.qml index 0ef67921..9ea33c29 100644 --- a/src/activities/piano_composition/MultipleStaff.qml +++ b/src/activities/piano_composition/MultipleStaff.qml @@ -95,8 +95,8 @@ Item { var staveNotes =3D staves.itemAt(i).notes for(var j =3D 0; j < staveNotes.count; j++) { melody.push({ - "type": staves.itemAt(i).notes.get(j).type, - "note": staves.itemAt(i).notes.get(j).mValue + "type": staveNotes.get(j).type, + "note": staveNotes.get(j).mValue }) } } diff --git a/src/activities/piano_composition/Piano_composition.qml b/src/a= ctivities/piano_composition/Piano_composition.qml index f46a47b9..81d313b2 100644 --- a/src/activities/piano_composition/Piano_composition.qml +++ b/src/activities/piano_composition/Piano_composition.qml @@ -171,7 +171,7 @@ ActivityBase { verticalAlignment: Text.AlignVCenter fontSizeMode: Text.Fit wrapMode: Text.WordWrap - text: Activity.instructions[bar.level - 1] + text: Activity.instructions[bar.level - 1].text } } = diff --git a/src/activities/piano_composition/piano_composition.js b/src/ac= tivities/piano_composition/piano_composition.js index 91b0d2fe..9db6b700 100644 --- a/src/activities/piano_composition/piano_composition.js +++ b/src/activities/piano_composition/piano_composition.js @@ -28,13 +28,30 @@ var currentLevel =3D 0 var numberOfLevel =3D 7 var items var userDir =3D "file://" + GCompris.ApplicationInfo.getSharedWritablePath= () + "/" + "piano_composition" -var userFile =3D "file://" + GCompris.ApplicationInfo.getSharedWritablePat= h() + "/" + "piano_composition" + "/melodies.json" -var instructions =3D ["This is the treble cleff staff for high pitched not= es", - "This is the bass cleff staff for low pitched notes", - "Click on the note symbols to write different length n= otes such as quarter notes, half notes and whole notes", "The black keys ar= e sharp and flat keys, have a # sign.", - "Each black key has two names: flat and sharp. Flat no= tes have b sign", - "Now you can load music", - "Now you can compose your own music"] +var userFile =3D userDir + "/melodies.json" +var instructions =3D [{ + "text": qsTr("This is the treble cleff staff for high pitched note= s") + }, + { + "text": qsTr("This is the bass cleff staff for low pitched notes") + }, + { + "text": qsTr("Click on the note symbols to write different length = notes such as quarter notes, half notes and whole notes") + }, + { + "text": qsTr("The black keys are sharp and flat keys, have a # sig= n.") + }, + { + "text": qsTr("Each black key has two names: flat and sharp. Flat n= otes have b sign") + }, + { + "text": qsTr("Now you can load music") + + }, + { + "text": qsTr("Now you can compose your own music") + } +] = function start(items_) { items =3D items_ @@ -51,22 +68,21 @@ function saveMelody() { console.debug("Created directory " + userDir); } = - if (!items.file.write(JSON.stringify(notes), userFile)) { + var data =3D items.file.read(userFile) + if (!items.file.append(JSON.stringify(notes), userFile)) { Core.showMessageDialog(items.background, - qsTr("Error saving melody to your file (%1)= ") - .arg(userFile), - "", null, "", null, null); - } - else { + qsTr("Error saving melody to your file (%1)") + .arg(userFile), + "", null, "", null, null); + } else { Core.showMessageDialog(items.background, - qsTr("Saved melody to your file (%1)") - .arg(userFile), - "", null, "", null, null); + qsTr("Saved melody to your file (%1)") + .arg(userFile), + "", null, "", null, null); } } = -function stop() { -} +function stop() {} = function initLevel() { items.bar.level =3D currentLevel + 1 @@ -74,7 +90,7 @@ function initLevel() { = function nextLevel() { items.staff2.eraseAllNotes() - if(numberOfLevel <=3D ++currentLevel ) { + if(numberOfLevel <=3D ++currentLevel) { currentLevel =3D 0 } initLevel(); diff --git a/src/core/File.cpp b/src/core/File.cpp index 00169b0b..c18270d2 100644 --- a/src/core/File.cpp +++ b/src/core/File.cpp @@ -115,6 +115,30 @@ bool File::write(const QString& data, const QString& n= ame) return true; } = +bool File::append(const QString& data, const QString& name) +{ + if (!name.isEmpty()) + setName(name); + + if (m_name.isEmpty()) { + emit error("source is empty"); + return false; + } + + QFile file(m_name); + if (!file.open(QFile::WriteOnly | QFile::Append)) { + emit error("could not open file " + m_name); + return false; + } + + QTextStream out(&file); + out << data; + + file.close(); + + return true; +} + void File::init() { qmlRegisterType("GCompris", 1, 0, "File"); diff --git a/src/core/File.h b/src/core/File.h index 04a2a9b5..bb58834d 100644 --- a/src/core/File.h +++ b/src/core/File.h @@ -70,6 +70,16 @@ public: */ Q_INVOKABLE bool write(const QString& data, const QString& name =3D QS= tring()); = + /** + * Apends @p data to a file. + * + * @param data Text data to appends. + * @param name [optional] Filename to append to. If omitted writes to + * the file specified by the member name. + * @returns success of the operation. + * @sa name + */ + Q_INVOKABLE bool append(const QString& data, const QString& name =3D Q= String()); /** * Checks whether file @p path exists. *