Commit 33f7dc5e authored by Sandro Andrade's avatar Sandro Andrade

General improvements in QML files

parent 7266f55c
......@@ -71,7 +71,7 @@ void ExerciseController::randomlySelectExerciseOptions()
int maxNote = INT_MIN;
quint8 numberOfSelectedOptions = m_currentExercise[QStringLiteral("numberOfSelectedOptions")].toInt();
for (quint8 i = 0; i < numberOfSelectedOptions; ++i) {
QJsonArray exerciseOptions = m_currentExercise[QStringLiteral("options")].toArray();
QJsonArray exerciseOptions = QJsonObject::fromVariantMap(m_currentExercise)[QStringLiteral("options")].toArray();
quint8 chosenExerciseOption = qrand() % exerciseOptions.size();
QString sequence = exerciseOptions[chosenExerciseOption].toObject()[QStringLiteral("sequence")].toString();
......
......@@ -26,7 +26,7 @@ import QtQuick.Controls 2.0
Item {
id: exerciseView
property var chosenExercises
property var currentExercise;
property var chosenColors: [4]
property Item answerRectangle
property var colors: ["#8dd3c7", "#ffffb3", "#bebada", "#fb8072", "#80b1d3", "#fdb462", "#b3de69", "#fccde5", "#d9d9d9", "#bc80bd", "#ccebc5", "#ffed6f", "#a6cee3", "#1f78b4", "#b2df8a", "#33a02c", "#fb9a99", "#e31a1c", "#fdbf6f", "#ff7f00", "#cab2d6", "#6a3d9a", "#ffff99", "#b15928"]
......@@ -36,15 +36,11 @@ Item {
signal answerClicked(var answerImageSource, var color)
signal showCorrectAnswer(var chosenExercises, var chosenColors)
function clearExerciseGrid() {
exerciseView.visible = false
for (var i = 0; i < answerGrid.children.length; ++i)
answerGrid.children[i].destroy()
}
function highlightRightAnswer() {
var chosenExercises = core.exerciseController.selectedExerciseOptions
for (var i = 0; i < answerGrid.children.length; ++i) {
answerGrid.children[i].enabled = false
if (answerGrid.children[i].model.name != chosenExercises[0])
if (answerGrid.children[i].model.name != chosenExercises[0].name)
answerGrid.children[i].opacity = 0.25
else
answerRectangle = answerGrid.children[i]
......@@ -54,30 +50,32 @@ Item {
})
animation.start()
}
function setCurrentExercise() {
var currentExercise = core.exerciseController.currentExercise
clearExerciseGrid()
var currentExerciseOptions = currentExercise["options"];
if (currentExerciseOptions != undefined) {
var length = currentExerciseOptions.length
answerGrid.columns = Math.min(6, length)
answerGrid.rows = Math.ceil(length/6)
for (var i = 0; i < length; ++i)
answerOption.createObject(answerGrid, {model: currentExerciseOptions[i], index: i, color: colors[i%24]})
exerciseView.visible = true
onCurrentExerciseChanged: {
exerciseView.state = "hidden"
if (exerciseView.currentExercise != undefined) {
var currentExerciseOptions = exerciseView.currentExercise["options"];
if (currentExerciseOptions != undefined) {
var length = currentExerciseOptions.length
answerGrid.columns = Math.min(6, length)
answerGrid.rows = Math.ceil(length/6)
for (var i = 0; i < length; ++i)
answerOption.createObject(answerGrid, {model: currentExerciseOptions[i], index: i, color: colors[i%24]})
exerciseView.visible = true
}
exerciseView.state = "initial"
}
exerciseView.state = "initial"
}
function checkAnswers(answers) {
var answersOk = true
var chosenExercises = core.exerciseController.selectedExerciseOptions
for(var i = 0; i < 4; ++i) {
if (answers[i].toString().split("/").pop().split(".")[0] != chosenExercises[i])
if (answers[i].toString().split("/").pop().split(".")[0] != chosenExercises[i].name)
answersOk = false
}
if (answersOk)
messageText.text = i18n("Congratulations!<br/>You answered correctly!")
messageText.text = i18n("Congratulations, you answered correctly!")
else
messageText.text = i18n("Oops, not this time!<br/>Try again!")
messageText.text = i18n("Oops, not this time! Try again!")
exerciseView.state = "nextQuestion"
}
......@@ -87,13 +85,20 @@ Item {
anchors.centerIn: parent
spacing: 20
Text {
id: userMessage
width: parent.width
horizontalAlignment: Text.AlignHCenter
font.pointSize: 18
text: (exerciseView.currentExercise != undefined) ? i18nc("technical term, do you have a musician friend?", exerciseView.currentExercise["userMessage"]):""
}
Text {
id: messageText
width: parent.width
horizontalAlignment: Text.AlignHCenter
font.pointSize: 18
textFormat: Text.RichText
}
Row {
anchors { horizontalCenter: parent.horizontalCenter }
......@@ -104,24 +109,22 @@ Item {
width: 124; height: 44
text: i18n("new question")
ToolTip {
visible: exerciseView.state == "initial"
text: "click here!"
}
onClicked: {
exerciseView.state = "waitingForAnswer"
var playMode = core.exerciseController.currentExercise["playMode"]
core.exerciseController.randomlySelectExerciseOptions()
var selectedExerciseOptions = core.exerciseController.selectedExerciseOptions
core.soundBackend.playMode = playMode
core.soundBackend.prepareFromExerciseOptions(selectedExerciseOptions)
var newChosenExercises = [];
for (var i = 0; i < selectedExerciseOptions.length; ++i)
newChosenExercises.push(selectedExerciseOptions[i].name);
chosenExercises = newChosenExercises
var chosenExercises = core.exerciseController.selectedExerciseOptions
core.soundBackend.prepareFromExerciseOptions(chosenExercises)
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]) {
if (answerGrid.children[j].children[0].originalText == chosenExercises[i].name) {
chosenColors[i] = answerGrid.children[j].color
break
}
if (playMode != "rhythm")
if (exerciseView.currentExercise["playMode"] != "rhythm")
answerHoverEnter(0, core.exerciseController.chosenRootNote(), 0, "white")
core.soundBackend.play()
}
......@@ -139,11 +142,11 @@ Item {
width: 124; height: 44
text: i18n("give up")
onClicked: {
if (core.exerciseController.currentExercise["playMode"] != "rhythm") {
if (exerciseView.currentExercise["playMode"] != "rhythm") {
highlightRightAnswer()
}
else {
showCorrectAnswer(chosenExercises, chosenColors)
showCorrectAnswer(core.exerciseController.selectedExerciseOptions, chosenColors)
exerciseView.state = "nextQuestion"
}
}
......@@ -161,6 +164,7 @@ Item {
anchors.centerIn: parent
spacing: 10; columns: 2; rows: 1
enabled: exerciseView.state == "waitingForAnswer"
Component {
id: answerOption
......@@ -170,15 +174,15 @@ Item {
property var model
property int index
width: (core.exerciseController.currentExercise["playMode"] != "rhythm") ? 120:119
height: (core.exerciseController.currentExercise["playMode"] != "rhythm") ? 40:59
width: (exerciseView.currentExercise != undefined && exerciseView.currentExercise["playMode"] != "rhythm") ? 120:119
height: (exerciseView.currentExercise != undefined && exerciseView.currentExercise["playMode"] != "rhythm") ? 40:59
Text {
id: option
property string originalText: model.name
visible: core.exerciseController.currentExercise["playMode"] != "rhythm"
visible: exerciseView.currentExercise != undefined && exerciseView.currentExercise["playMode"] != "rhythm"
text: i18nc("technical term, do you have a musician friend?", model.name)
width: parent.width - 4
anchors.centerIn: parent
......@@ -190,19 +194,19 @@ Item {
id: rhythmImage
anchors.centerIn: parent
visible: core.exerciseController.currentExercise["playMode"] == "rhythm"
source: (core.exerciseController.currentExercise["playMode"] == "rhythm") ? "exercise-images/" + model.name + ".png":""
visible: exerciseView.currentExercise != undefined && exerciseView.currentExercise["playMode"] == "rhythm"
source: (exerciseView.currentExercise != undefined && exerciseView.currentExercise["playMode"] == "rhythm") ? "exercise-images/" + model.name + ".png":""
fillMode: Image.Pad
}
MouseArea {
anchors.fill: parent
onClicked: {
if (core.exerciseController.currentExercise["playMode"] != "rhythm") {
if (exerciseView.currentExercise["playMode"] != "rhythm") {
onExited()
if (option.originalText == chosenExercises[0])
messageText.text = i18n("Congratulations!<br/>You answered correctly!")
if (option.originalText == core.exerciseController.selectedExerciseOptions[0].name)
messageText.text = i18n("Congratulations, you answered correctly!")
else
messageText.text = i18n("Oops, not this time!<br/>Try again!")
messageText.text = i18n("Oops, not this time! Try again!")
answerHoverExit(0, core.exerciseController.chosenRootNote() + parseInt(model.sequence), 0)
highlightRightAnswer()
}
......@@ -213,7 +217,7 @@ Item {
hoverEnabled: true
onEntered: {
answerRectangle.color = Qt.darker(answerRectangle.color, 1.1)
if (core.exerciseController.currentExercise["playMode"] != "rhythm") {
if (exerciseView.currentExercise["playMode"] != "rhythm") {
model.sequence.split(' ').forEach(function(note) {
answerHoverEnter(0, core.exerciseController.chosenRootNote() + parseInt(note), 0, colors[answerRectangle.index])
})
......@@ -221,7 +225,7 @@ Item {
}
onExited: {
answerRectangle.color = colors[answerRectangle.index]
if (core.exerciseController.currentExercise["playMode"] != "rhythm") {
if (exerciseView.currentExercise["playMode"] != "rhythm") {
if (!animation.running)
model.sequence.split(' ').forEach(function(note) {
answerHoverExit(0, core.exerciseController.chosenRootNote() + parseInt(note), 0)
......@@ -235,6 +239,16 @@ Item {
}
}
states: [
State {
name: "hidden"
StateChangeScript {
script: {
exerciseView.visible = false
for (var i = 0; i < answerGrid.children.length; ++i)
answerGrid.children[i].destroy()
}
}
},
State {
name: "initial"
StateChangeScript {
......@@ -242,10 +256,8 @@ Item {
newQuestionButton.enabled = true
playQuestionButton.enabled = false
giveUpButton.enabled = false
answerGrid.enabled = false
answerGrid.opacity = 0.25
messageText.text = i18n("%1<br/>Click 'play question' if you want to hear again!",
i18nc("technical term, do you have a musician friend?", core.exerciseController.currentExercise["userMessage"]))
messageText.text = i18n("Click 'new question' to start!")
}
}
},
......@@ -260,10 +272,8 @@ Item {
newQuestionButton.enabled = false
playQuestionButton.enabled = true
giveUpButton.enabled = true
answerGrid.enabled = true
answerGrid.opacity = 1
messageText.text = i18n("%1<br/>Click 'play question' if you want to hear again!",
i18nc("technical term, do you have a musician friend?", core.exerciseController.currentExercise["userMessage"]))
messageText.text = i18n("Click 'play question' if you want to hear again!")
}
}
},
......@@ -274,7 +284,6 @@ Item {
newQuestionButton.enabled = true
playQuestionButton.enabled = false
giveUpButton.enabled = false
answerGrid.enabled = false
}
}
}
......@@ -296,8 +305,4 @@ Item {
onStopped: exerciseView.state = "nextQuestion"
}
Connections {
target: core.exerciseController
onCurrentExerciseChanged: setCurrentExercise()
}
}
......@@ -21,13 +21,18 @@
****************************************************************************/
import QtQuick 2.7
import QtQuick.Controls 2.0
import "pianoview"
import "midiplayer"
Item {
ApplicationWindow {
id: mainItem
visible: true
width: 640
height: 480
title: qsTr("Minuet")
property int menuBarWidth: 280
function exerciseViewStateChanged() {
......@@ -43,7 +48,6 @@ Item {
onBackPressed: {
core.soundBackend.reset()
exerciseView.clearExerciseGrid()
pianoView.clearAllMarks()
}
}
......@@ -71,13 +75,13 @@ Item {
id: pianoView
anchors { bottom: parent.bottom; bottomMargin: 5; horizontalCenter: parent.horizontalCenter }
visible: (Object.keys(core.exerciseController.currentExercise).length > 0 && core.exerciseController.currentExercise["playMode"] != "rhythm")
visible: minuetMenu.currentExercise != undefined && minuetMenu.currentExercise["playMode"] != "rhythm"
}
RhythmAnswerView {
id: rhythmAnswerView
anchors { bottom: parent.bottom; bottomMargin: 14; horizontalCenter: parent.horizontalCenter }
visible: (core.exerciseController.currentExercise["playMode"] == "rhythm")
visible: minuetMenu.currentExercise != undefined && minuetMenu.currentExercise["playMode"] == "rhythm"
exerciseView: exerciseView
onAnswerCompleted: exerciseView.checkAnswers(answers)
......@@ -87,13 +91,14 @@ Item {
width: background.width; height: minuetMenu.height + 20
anchors { top: background.top; horizontalCenter: background.horizontalCenter }
currentExercise: minuetMenu.currentExercise
onAnswerHoverEnter: pianoView.noteMark(chan, pitch, vel, color)
onAnswerHoverExit: pianoView.noteUnmark(chan, pitch, vel)
onAnswerClicked: rhythmAnswerView.answerClicked(answerImageSource, color)
onStateChanged: mainItem.exerciseViewStateChanged()
onShowCorrectAnswer: rhythmAnswerView.showCorrectAnswer(chosenExercises, chosenColors)
onChosenExercisesChanged: rhythmAnswerView.fillCorrectAnswerGrid()
}
}
Binding {
......@@ -111,6 +116,16 @@ Item {
property: "tempo"
value: midiPlayer.tempo
}
Binding {
target: core.exerciseController
property: "currentExercise"
value: minuetMenu.currentExercise
}
Binding {
target: core.soundBackend
property: "playMode"
value: (minuetMenu.currentExercise != undefined) ? minuetMenu.currentExercise["playMode"]:""
}
Connections {
target: core.exerciseController
onSelectedExerciseOptionsChanged: pianoView.clearAllMarks()
......
......@@ -26,6 +26,7 @@ import QtQuick.Controls 2.0
Item {
id: minuetMenu
property var currentExercise: undefined
signal backPressed
Button {
......@@ -34,7 +35,7 @@ Item {
width: (stackView.depth > 1) ? 24:0; height: parent.height
text: "<"
onClicked: {
core.exerciseController.currentExercise = {}
currentExercise = undefined
stackView.pop()
backPressed()
}
......@@ -67,7 +68,7 @@ Item {
onClicked: {
var children = ListView.view.model[index].children
if (!children)
core.exerciseController.currentExercise = ListView.view.model[index]
minuetMenu.currentExercise = ListView.view.model[index]
else
stackView.push(categoryMenu.createObject(stackView, {model: children}))
}
......
......@@ -31,7 +31,6 @@ Column {
"exercise-images/unknown-rhythm.png"
]
property int currentAnswer: 0
property var correctAnswers
property var correctColors: ["#ffffff", "#ffffff", "#ffffff", "#ffffff"]
property ExerciseView exerciseView
property var colors: ["#ffffff", "#ffffff", "#ffffff", "#ffffff"]
......@@ -48,8 +47,9 @@ Column {
if (currentAnswer == 4) {
answerCompleted(answers)
correctColors = exerciseView.chosenColors
var selectedExerciseOptions = core.exerciseController.selectedExerciseOptions
for (var i = 0; i < 4; ++i)
correctAnswerGrid.children[i].opacity = answers[i].toString().split("/").pop().split(".")[0] != correctAnswers[i] ? 1:0
correctAnswerGrid.children[i].opacity = answers[i].toString().split("/").pop().split(".")[0] != selectedExerciseOptions[i].name ? 1:0
}
else {
tempAnswers[currentAnswer] = "exercise-images/current-rhythm.png"
......@@ -59,7 +59,6 @@ Column {
function resetAnswers() {
currentAnswer = 0
answers = ["exercise-images/current-rhythm.png", "exercise-images/unknown-rhythm.png", "exercise-images/unknown-rhythm.png", "exercise-images/unknown-rhythm.png"]
correctAnswers = undefined
colors = ["#ffffff", "#ffffff", "#ffffff", "#ffffff"]
correctColors = ["#ffffff", "#ffffff", "#ffffff", "#ffffff"]
for (var i = 0; i < 4; ++i)
......@@ -68,16 +67,11 @@ Column {
function showCorrectAnswer(chosenExercises, chosenColors) {
var tempAnswers = answers
for (var i = 0; i < 4; ++i)
tempAnswers[i] = "exercise-images/" + chosenExercises[i] + ".png"
tempAnswers[i] = "exercise-images/" + chosenExercises[i].name + ".png"
answers = tempAnswers
colors = chosenColors
currentAnswer = 0
}
function fillCorrectAnswerGrid() {
for (var i = 0; i < 4; ++i)
correctAnswerGrid.children[i].opacity = 0
correctAnswers = exerciseView.chosenExercises
}
spacing: 10
......@@ -101,7 +95,7 @@ Column {
id: correctRhythmImage
anchors.centerIn: parent
source: (correctAnswers != undefined && core.exerciseController.currentExercise["playMode"] == "rhythm") ? "exercise-images/" + correctAnswers[index] + ".png":""
source: (core.exerciseController.selectedExerciseOptions != undefined && core.exerciseController.selectedExerciseOptions[index] != undefined && core.exerciseController.currentExercise["playMode"] == "rhythm") ? "exercise-images/" + core.exerciseController.selectedExerciseOptions[index].name + ".png":""
fillMode: Image.Pad
}
}
......
......@@ -24,12 +24,15 @@
#include "core.h"
#include <QQmlContext>
#include <QQmlApplicationEngine>
namespace Minuet
{
UiController::UiController(QObject *parent)
: IUiController(parent),
m_mainWindow(new MainWindow(qobject_cast<Core *>(parent)))
: IUiController(parent)//,
//m_mainWindow(new MainWindow(qobject_cast<Core *>(parent)))
{
}
......@@ -39,9 +42,12 @@ UiController::~UiController()
bool UiController::initialize(Core *core)
{
Q_UNUSED(core);
QQmlApplicationEngine *engine = new QQmlApplicationEngine(this);
QQmlContext *rootContext = engine->rootContext();
rootContext->setContextProperty(QStringLiteral("core"), core);
engine->load(QUrl::fromLocalFile(QStandardPaths::locate(QStandardPaths::DataLocation, QStringLiteral("qml/Main.qml"))));
m_mainWindow->show();
//m_mainWindow->show();
return true;
}
......
......@@ -27,7 +27,8 @@
#include <QObject>
#include <QJsonArray>
#include <QJsonObject>
#include <QVariantMap>
#include <QDebug>
namespace Minuet
{
......@@ -36,7 +37,7 @@ class MINUETINTERFACES_EXPORT IExerciseController : public QObject
{
Q_OBJECT
Q_PROPERTY(QJsonArray exercises READ exercises)
Q_PROPERTY(QJsonObject currentExercise MEMBER m_currentExercise NOTIFY currentExerciseChanged)
Q_PROPERTY(QVariantMap currentExercise MEMBER m_currentExercise NOTIFY currentExerciseChanged)
Q_PROPERTY(QJsonArray selectedExerciseOptions READ selectedExerciseOptions NOTIFY selectedExerciseOptionsChanged)
public:
......@@ -49,13 +50,13 @@ public Q_SLOTS:
virtual void randomlySelectExerciseOptions() = 0;
Q_SIGNALS:
void currentExerciseChanged(QJsonObject newCurrentExercise);
void currentExerciseChanged(QVariantMap newCurrentExercise);
void selectedExerciseOptionsChanged(QJsonArray newSelectedExerciseOptions);
protected:
explicit IExerciseController(QObject *parent = 0);
QJsonObject m_currentExercise;
QVariantMap m_currentExercise;
QJsonArray m_selectedExerciseOptions;
};
......
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