Commit 57c2f6f2 authored by Sandro Andrade's avatar Sandro Andrade

Further refactoring on ExerciseController

parent db92d834
...@@ -109,7 +109,12 @@ Item { ...@@ -109,7 +109,12 @@ Item {
text: i18n("new question") text: i18n("new question")
onClicked: { onClicked: {
exerciseView.state = "waitingForAnswer" exerciseView.state = "waitingForAnswer"
chosenExercises = exerciseController.randomlyChooseExercises() exerciseController.randomlySelectOptions()
var selectedOptions = exerciseController.selectedOptions
var newChosenExercises = [];
for (var i = 0; i < selectedOptions.length; ++i)
newChosenExercises.push(selectedOptions[i].name);
chosenExercises = newChosenExercises
for (var i = 0; i < chosenExercises.length; ++i) for (var i = 0; i < chosenExercises.length; ++i)
for (var j = 0; j < answerGrid.children.length; ++j) for (var j = 0; j < answerGrid.children.length; ++j)
if (answerGrid.children[j].children[0].originalText == chosenExercises[i]) { if (answerGrid.children[j].children[0].originalText == chosenExercises[i]) {
......
...@@ -39,7 +39,7 @@ Item { ...@@ -39,7 +39,7 @@ Item {
function itemClicked(delegateRect, index) { function itemClicked(delegateRect, index) {
var model = delegateRect.ListView.view.model[index].options var model = delegateRect.ListView.view.model[index].options
if (model != undefined) { if (model != undefined) {
exerciseController.setExerciseOptions(model) exerciseController.currentExercise = model
minuetMenu.itemChanged(model) minuetMenu.itemChanged(model)
} }
} }
...@@ -92,17 +92,17 @@ Item { ...@@ -92,17 +92,17 @@ Item {
stackView.push(categoryMenu.createObject(stackView, {model: children})) stackView.push(categoryMenu.createObject(stackView, {model: children}))
var root = delegateRect.ListView.view.model[index].root var root = delegateRect.ListView.view.model[index].root
if (root != undefined) { if (root != undefined) {
exerciseController.setMinRootNote(root.split('.')[0]) exerciseController.minRootNote = parseInt(root.split('.')[0])
exerciseController.setMaxRootNote(root.split('.')[2]) exerciseController.maxRootNote = parseInt(root.split('.')[2])
} }
var playMode = delegateRect.ListView.view.model[index].playMode var playMode = delegateRect.ListView.view.model[index].playMode
if (playMode != undefined) { if (playMode != undefined) {
if (playMode == "scale") exerciseController.setPlayMode(0) // ScalePlayMode if (playMode == "scale") exerciseController.setPlayMode(0) // ScalePlayMode
if (playMode == "chord") exerciseController.setPlayMode(1) // ChordPlayMode if (playMode == "chord") exerciseController.setPlayMode(1) // ChordPlayMode
exerciseController.setAnswerLength(1) exerciseController.answerLength = 1
if (playMode == "rhythm") { if (playMode == "rhythm") {
exerciseController.setPlayMode(2) // RhythmPlayMode exerciseController.setPlayMode(2) // RhythmPlayMode
exerciseController.setAnswerLength(4) exerciseController.answerLength = 4
} }
} }
} }
......
...@@ -26,7 +26,10 @@ namespace Minuet ...@@ -26,7 +26,10 @@ namespace Minuet
{ {
IExerciseController::IExerciseController(QObject *parent) IExerciseController::IExerciseController(QObject *parent)
: QObject(parent) : QObject(parent),
m_minRootNote(0),
m_maxRootNote(0),
m_answerLength(1)
{ {
} }
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#define MINUET_IEXERCISECONTROLLER_H #define MINUET_IEXERCISECONTROLLER_H
#include <QtCore/QObject> #include <QtCore/QObject>
#include <QtCore/QJsonArray>
#include "minuetinterfacesexport.h" #include "minuetinterfacesexport.h"
...@@ -33,10 +34,28 @@ namespace Minuet ...@@ -33,10 +34,28 @@ namespace Minuet
class MINUETINTERFACES_EXPORT IExerciseController : public QObject class MINUETINTERFACES_EXPORT IExerciseController : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(quint8 minRootNote MEMBER m_minRootNote)
Q_PROPERTY(quint8 maxRootNote MEMBER m_maxRootNote)
Q_PROPERTY(QJsonArray currentExercise MEMBER m_currentExercise)
Q_PROPERTY(quint8 answerLength MEMBER m_answerLength)
Q_PROPERTY(QJsonArray selectedOptions MEMBER m_selectedOptions)
public: public:
virtual ~IExerciseController() override;
virtual QJsonObject exercises() const = 0;
public Q_SLOTS:
virtual void randomlySelectOptions() = 0;
protected:
explicit IExerciseController(QObject *parent = 0); explicit IExerciseController(QObject *parent = 0);
~IExerciseController() override;
quint8 m_minRootNote;
quint8 m_maxRootNote;
QJsonArray m_currentExercise;
quint8 m_answerLength;
QJsonArray m_selectedOptions;
}; };
} }
......
...@@ -40,10 +40,7 @@ namespace Minuet ...@@ -40,10 +40,7 @@ namespace Minuet
ExerciseController::ExerciseController(MidiSequencer *midiSequencer) : ExerciseController::ExerciseController(MidiSequencer *midiSequencer) :
m_midiSequencer(midiSequencer), m_midiSequencer(midiSequencer),
m_minRootNote(0),
m_maxRootNote(0),
m_playMode(ScalePlayMode), m_playMode(ScalePlayMode),
m_answerLength(1),
m_chosenRootNote(0) m_chosenRootNote(0)
{ {
m_exercises["exercises"] = QJsonArray(); m_exercises["exercises"] = QJsonArray();
...@@ -68,35 +65,17 @@ bool ExerciseController::initialize() ...@@ -68,35 +65,17 @@ bool ExerciseController::initialize()
return definitionsMerge & exercisesMerge; return definitionsMerge & exercisesMerge;
} }
void ExerciseController::setExerciseOptions(QJsonArray exerciseOptions)
{
m_exerciseOptions = exerciseOptions;
}
void ExerciseController::setMinRootNote(unsigned int minRootNote)
{
m_minRootNote = minRootNote;
}
void ExerciseController::setMaxRootNote(unsigned int maxRootNote)
{
m_maxRootNote = maxRootNote;
}
void ExerciseController::setPlayMode(PlayMode playMode) void ExerciseController::setPlayMode(PlayMode playMode)
{ {
m_playMode = playMode; m_playMode = playMode;
} }
void ExerciseController::setAnswerLength(unsigned int answerLength) void ExerciseController::randomlySelectOptions()
{ {
m_answerLength = answerLength; while (!m_selectedOptions.isEmpty())
} m_selectedOptions.removeFirst();
QStringList ExerciseController::randomlyChooseExercises()
{
qsrand(QDateTime::currentDateTimeUtc().toTime_t()); qsrand(QDateTime::currentDateTimeUtc().toTime_t());
QStringList chosenExercises;
Song *song = new Song; Song *song = new Song;
song->setHeader(0, 1, 60); song->setHeader(0, 1, 60);
...@@ -114,8 +93,8 @@ QStringList ExerciseController::randomlyChooseExercises() ...@@ -114,8 +93,8 @@ QStringList ExerciseController::randomlyChooseExercises()
} }
for (unsigned int i = 0; i < m_answerLength; ++i) { for (unsigned int i = 0; i < m_answerLength; ++i) {
unsigned int chosenExercise = qrand() % m_exerciseOptions.size(); unsigned int chosenExercise = qrand() % m_currentExercise.size();
QString sequence = m_exerciseOptions[chosenExercise].toObject()[QStringLiteral("sequence")].toString(); QString sequence = m_currentExercise[chosenExercise].toObject()[QStringLiteral("sequence")].toString();
if (m_playMode != RhythmPlayMode) { if (m_playMode != RhythmPlayMode) {
int minNote = INT_MAX; int minNote = INT_MAX;
...@@ -162,13 +141,11 @@ QStringList ExerciseController::randomlyChooseExercises() ...@@ -162,13 +141,11 @@ QStringList ExerciseController::randomlyChooseExercises()
} }
} }
chosenExercises << m_exerciseOptions[chosenExercise].toObject()[QStringLiteral("name")].toString(); m_selectedOptions.append(m_currentExercise[chosenExercise]);
} }
if (m_playMode == RhythmPlayMode) { if (m_playMode == RhythmPlayMode) {
m_midiSequencer->appendEvent(m_midiSequencer->SMFNoteOn(9, 80, 120), barStart); m_midiSequencer->appendEvent(m_midiSequencer->SMFNoteOn(9, 80, 120), barStart);
} }
return chosenExercises;
} }
unsigned int ExerciseController::chosenRootNote() unsigned int ExerciseController::chosenRootNote()
......
...@@ -25,9 +25,8 @@ ...@@ -25,9 +25,8 @@
#include <interfaces/iexercisecontroller.h> #include <interfaces/iexercisecontroller.h>
#include <QJsonArray>
#include <QJsonObject>
#include <QStringList> #include <QStringList>
#include <QJsonObject>
#include "minuetshellexport.h" #include "minuetshellexport.h"
...@@ -35,7 +34,7 @@ class MidiSequencer; ...@@ -35,7 +34,7 @@ class MidiSequencer;
namespace Minuet namespace Minuet
{ {
class MINUETSHELL_EXPORT ExerciseController : public IExerciseController class MINUETSHELL_EXPORT ExerciseController : public IExerciseController
{ {
Q_OBJECT Q_OBJECT
...@@ -44,7 +43,7 @@ class MINUETSHELL_EXPORT ExerciseController : public IExerciseController ...@@ -44,7 +43,7 @@ class MINUETSHELL_EXPORT ExerciseController : public IExerciseController
public: public:
explicit ExerciseController(MidiSequencer *midiSequencer = 0); explicit ExerciseController(MidiSequencer *midiSequencer = 0);
virtual ~ExerciseController(); virtual ~ExerciseController();
bool initialize(); bool initialize();
enum PlayMode { enum PlayMode {
...@@ -53,18 +52,16 @@ public: ...@@ -53,18 +52,16 @@ public:
RhythmPlayMode RhythmPlayMode
}; };
Q_INVOKABLE void setExerciseOptions(QJsonArray exerciseOptions);
Q_INVOKABLE void setMinRootNote(unsigned int minRootNote);
Q_INVOKABLE void setMaxRootNote(unsigned int maxRootNote);
Q_INVOKABLE void setPlayMode(PlayMode playMode); Q_INVOKABLE void setPlayMode(PlayMode playMode);
Q_INVOKABLE void setAnswerLength(unsigned int answerLength);
Q_INVOKABLE QStringList randomlyChooseExercises();
Q_INVOKABLE unsigned int chosenRootNote(); Q_INVOKABLE unsigned int chosenRootNote();
Q_INVOKABLE void playChoosenExercise(); Q_INVOKABLE void playChoosenExercise();
QString errorString() const; QString errorString() const;
QJsonObject exercises() const; virtual QJsonObject exercises() const override;
public Q_SLOTS:
virtual void randomlySelectOptions();
private: private:
bool mergeJsonFiles(const QString directoryName, QJsonObject &targetObject, bool applyDefinitionsFlag = false, QString commonKey = "", QString mergeKey = ""); bool mergeJsonFiles(const QString directoryName, QJsonObject &targetObject, bool applyDefinitionsFlag = false, QString commonKey = "", QString mergeKey = "");
QJsonArray applyDefinitions(QJsonArray exercises, QJsonArray definitions); QJsonArray applyDefinitions(QJsonArray exercises, QJsonArray definitions);
...@@ -78,11 +75,7 @@ private: ...@@ -78,11 +75,7 @@ private:
MidiSequencer *m_midiSequencer; MidiSequencer *m_midiSequencer;
QJsonObject m_exercises; QJsonObject m_exercises;
QJsonObject m_definitions; QJsonObject m_definitions;
QJsonArray m_exerciseOptions;
unsigned int m_minRootNote;
unsigned int m_maxRootNote;
PlayMode m_playMode; PlayMode m_playMode;
unsigned int m_answerLength;
unsigned int m_chosenRootNote; unsigned int m_chosenRootNote;
QString m_errorString; QString m_errorString;
}; };
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment