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

Further refactoring on ExerciseController

parent db92d834
......@@ -109,7 +109,12 @@ Item {
text: i18n("new question")
onClicked: {
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 j = 0; j < answerGrid.children.length; ++j)
if (answerGrid.children[j].children[0].originalText == chosenExercises[i]) {
......
......@@ -39,7 +39,7 @@ Item {
function itemClicked(delegateRect, index) {
var model = delegateRect.ListView.view.model[index].options
if (model != undefined) {
exerciseController.setExerciseOptions(model)
exerciseController.currentExercise = model
minuetMenu.itemChanged(model)
}
}
......@@ -92,17 +92,17 @@ Item {
stackView.push(categoryMenu.createObject(stackView, {model: children}))
var root = delegateRect.ListView.view.model[index].root
if (root != undefined) {
exerciseController.setMinRootNote(root.split('.')[0])
exerciseController.setMaxRootNote(root.split('.')[2])
exerciseController.minRootNote = parseInt(root.split('.')[0])
exerciseController.maxRootNote = parseInt(root.split('.')[2])
}
var playMode = delegateRect.ListView.view.model[index].playMode
if (playMode != undefined) {
if (playMode == "scale") exerciseController.setPlayMode(0) // ScalePlayMode
if (playMode == "chord") exerciseController.setPlayMode(1) // ChordPlayMode
exerciseController.setAnswerLength(1)
exerciseController.answerLength = 1
if (playMode == "rhythm") {
exerciseController.setPlayMode(2) // RhythmPlayMode
exerciseController.setAnswerLength(4)
exerciseController.answerLength = 4
}
}
}
......
......@@ -26,7 +26,10 @@ namespace Minuet
{
IExerciseController::IExerciseController(QObject *parent)
: QObject(parent)
: QObject(parent),
m_minRootNote(0),
m_maxRootNote(0),
m_answerLength(1)
{
}
......
......@@ -24,6 +24,7 @@
#define MINUET_IEXERCISECONTROLLER_H
#include <QtCore/QObject>
#include <QtCore/QJsonArray>
#include "minuetinterfacesexport.h"
......@@ -33,10 +34,28 @@ namespace Minuet
class MINUETINTERFACES_EXPORT IExerciseController : public QObject
{
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:
virtual ~IExerciseController() override;
virtual QJsonObject exercises() const = 0;
public Q_SLOTS:
virtual void randomlySelectOptions() = 0;
protected:
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
ExerciseController::ExerciseController(MidiSequencer *midiSequencer) :
m_midiSequencer(midiSequencer),
m_minRootNote(0),
m_maxRootNote(0),
m_playMode(ScalePlayMode),
m_answerLength(1),
m_chosenRootNote(0)
{
m_exercises["exercises"] = QJsonArray();
......@@ -68,35 +65,17 @@ bool ExerciseController::initialize()
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)
{
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());
QStringList chosenExercises;
Song *song = new Song;
song->setHeader(0, 1, 60);
......@@ -114,8 +93,8 @@ QStringList ExerciseController::randomlyChooseExercises()
}
for (unsigned int i = 0; i < m_answerLength; ++i) {
unsigned int chosenExercise = qrand() % m_exerciseOptions.size();
QString sequence = m_exerciseOptions[chosenExercise].toObject()[QStringLiteral("sequence")].toString();
unsigned int chosenExercise = qrand() % m_currentExercise.size();
QString sequence = m_currentExercise[chosenExercise].toObject()[QStringLiteral("sequence")].toString();
if (m_playMode != RhythmPlayMode) {
int minNote = INT_MAX;
......@@ -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) {
m_midiSequencer->appendEvent(m_midiSequencer->SMFNoteOn(9, 80, 120), barStart);
}
return chosenExercises;
}
unsigned int ExerciseController::chosenRootNote()
......
......@@ -25,9 +25,8 @@
#include <interfaces/iexercisecontroller.h>
#include <QJsonArray>
#include <QJsonObject>
#include <QStringList>
#include <QJsonObject>
#include "minuetshellexport.h"
......@@ -53,17 +52,15 @@ public:
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 setAnswerLength(unsigned int answerLength);
Q_INVOKABLE QStringList randomlyChooseExercises();
Q_INVOKABLE unsigned int chosenRootNote();
Q_INVOKABLE void playChoosenExercise();
QString errorString() const;
QJsonObject exercises() const;
virtual QJsonObject exercises() const override;
public Q_SLOTS:
virtual void randomlySelectOptions();
private:
bool mergeJsonFiles(const QString directoryName, QJsonObject &targetObject, bool applyDefinitionsFlag = false, QString commonKey = "", QString mergeKey = "");
......@@ -78,11 +75,7 @@ private:
MidiSequencer *m_midiSequencer;
QJsonObject m_exercises;
QJsonObject m_definitions;
QJsonArray m_exerciseOptions;
unsigned int m_minRootNote;
unsigned int m_maxRootNote;
PlayMode m_playMode;
unsigned int m_answerLength;
unsigned int m_chosenRootNote;
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