File: pattern.cpp

package info (click to toggle)
tiatracker 1.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,664 kB
  • sloc: cpp: 8,875; asm: 664; makefile: 8
file content (59 lines) | stat: -rw-r--r-- 1,504 bytes parent folder | download | duplicates (2)
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
/* TIATracker, (c) 2016 Andre "Kylearan" Wichmann.
 * Website: https://bitbucket.org/kylearan/tiatracker
 * Email: andre.wichmann@gmx.de
 * See the file "license.txt" for information on usage and redistribution
 * of this file.
 */

#include "pattern.h"
#include <QJsonArray>
#include <mainwindow.h>


namespace Track {

Pattern::Pattern()
{

}

/*************************************************************************/

void Pattern::toJson(QJsonObject &json) {
    json["name"] = name;
    json["evenspeed"] = evenSpeed;
    json["oddspeed"] = oddSpeed;
    QJsonArray noteArray;
    for (int i = 0; i < notes.size(); ++i) {
        QJsonObject noteJson;
        notes[i].toJson(noteJson);
        noteArray.append(noteJson);
    }
    json["notes"] = noteArray;
}

/*************************************************************************/

bool Pattern::fromJson(const QJsonObject &json) {
    name = json["name"].toString();
    if (json.contains("evenspeed")) {
        evenSpeed = json["evenspeed"].toInt();
        oddSpeed = json["oddspeed"].toInt();
    } else {
        evenSpeed = 3;
        oddSpeed = 3;
    }
    QJsonArray noteArray = json["notes"].toArray();
    notes.clear();
    for (int i = 0; i < noteArray.size(); ++i) {
        Note newNote;
        if (!newNote.fromJson(noteArray[i].toObject())) {
            MainWindow::displayMessage("A pattern has an invalid note: " + name);
            return false;
        }
        notes.append(newNote);
    }
    return true;
}

}