RhythmAnswerView.qml 5.87 KB
Newer Older
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
/****************************************************************************
**
** Copyright (C) 2016 by Sandro S. Andrade <sandroandrade@kde.org>
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License as
** published by the Free Software Foundation; either version 2 of
** the License or (at your option) version 3 or any later version
** accepted by the membership of KDE e.V. (or its successor approved
** by the membership of KDE e.V.), which shall act as a proxy 
** defined in Section 14 of version 3 of the license.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/

import QtQuick 2.4
import QtQuick.Controls 1.3

Column {
    property var answers: [
        "exercise-images/current-rhythm.png",
        "exercise-images/unknown-rhythm.png",
        "exercise-images/unknown-rhythm.png",
        "exercise-images/unknown-rhythm.png"
    ]
    property int currentAnswer: 0
34
    property var correctAnswers
35
    property var correctColors: ["#ffffff", "#ffffff", "#ffffff", "#ffffff"]
36
    property ExerciseView exerciseView
37
    property var colors: ["#ffffff", "#ffffff", "#ffffff", "#ffffff"]
38 39 40

    signal answerCompleted(var answers)

41 42 43 44 45 46
    function answerClicked(answerImageSource, color) {
        var tempAnswers = answers
        tempAnswers[currentAnswer] = answerImageSource
        var tempColors = colors
        tempColors[currentAnswer] = color
        colors = tempColors
47
        currentAnswer++
48
        if (currentAnswer == 4) {
49
            answerCompleted(answers)
50
            correctColors = exerciseView.chosenColors
51
            for (var i = 0; i < 4; ++i)
52 53 54
                correctAnswerGrid.children[i].opacity = answers[i].toString().split("/").pop().split(".")[0] != correctAnswers[i] ? 1:0
        }
        else {
55
            tempAnswers[currentAnswer] = "exercise-images/current-rhythm.png"
56
        }
57
        answers = tempAnswers
58 59 60
    }
    function resetAnswers() {
        currentAnswer = 0
61
        answers = ["exercise-images/current-rhythm.png", "exercise-images/unknown-rhythm.png", "exercise-images/unknown-rhythm.png", "exercise-images/unknown-rhythm.png"]
62
        correctAnswers = undefined
63 64
        colors = ["#ffffff", "#ffffff", "#ffffff", "#ffffff"]
        correctColors = ["#ffffff", "#ffffff", "#ffffff", "#ffffff"]
65
    }
66
    function showCorrectAnswer(chosenExercises, chosenColors) {
67
        var tempAnswers = answers
68
        for (var i = 0; i < 4; ++i)
69
            tempAnswers[i] = "exercise-images/" + chosenExercises[i] + ".png"
70 71 72
        answers = tempAnswers
        colors = chosenColors
        currentAnswer = 0
73 74 75 76 77
    }
    function fillCorrectAnswerGrid() {
        for (var i = 0; i < 4; ++i)
            correctAnswerGrid.children[i].opacity = 0
        correctAnswers = exerciseView.chosenExercises
78 79
    }

80 81 82 83 84 85 86
    spacing: 10

    Row {
        id: correctAnswerGrid

        anchors.horizontalCenter: parent.horizontalCenter
        spacing: 10
87

88 89 90 91 92 93
        Repeater {
            model: 4

            Rectangle {
                id: correctAnswerRectangle

Sandro Andrade's avatar
Sandro Andrade committed
94
                width: 119; height: 59
95 96
                color: correctColors[index]
                opacity: 0
97

98 99 100
                Image {
                    id: correctRhythmImage

101
                    anchors.centerIn: parent
102
                    source: (correctAnswers != undefined && exerciseView.userMessage == "the rhythm") ? "exercise-images/" + correctAnswers[index] + ".png":""
103 104 105 106 107
                    fillMode: Image.Pad
                }
            }
        }
    }
108 109

    Rectangle {
110 111
        id: answerRect

112 113 114
        color: "#475057"
        radius: 5
        anchors.horizontalCenter: parent.horizontalCenter
115
        width: answerGrid.width + 20; height: answerGrid.height + 20
116

117 118 119 120 121 122 123 124 125 126 127
        Row {
            id: answerGrid

            anchors.centerIn: parent
            spacing: 10
            Repeater {
                model: 4

                Rectangle {
                    id: answerRectangle

Sandro Andrade's avatar
Sandro Andrade committed
128
                    width: 119
129
                    height: 59
130
                    color: colors[index]
131

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
                    Text {
                        id: option

                        property string originalText

                        visible: false
                        width: parent.width
                        anchors.centerIn: parent
                        horizontalAlignment: Qt.AlignHCenter
                        color: "black"
                        wrapMode: Text.Wrap
                    }
                    Image {
                        id: rhythmImage
                        anchors.centerIn: parent

                        source: answers[index]
                        fillMode: Image.Pad
                    }
                }
            }
        }
    }
    Button {
156 157 158
        id: backspaceButton

        width: answerRect.width; height: 44
159 160
        anchors.horizontalCenter: parent.horizontalCenter
        text: i18n("backspace")
161
        enabled: currentAnswer > 0 && currentAnswer < 4
162 163
        onClicked: {
            if (currentAnswer > 0) {
164 165 166
                var tempAnswers = answers
                var tempColors = colors
                tempAnswers[currentAnswer] = "exercise-images/unknown-rhythm.png"
167
                currentAnswer--
168 169 170 171
                tempAnswers[currentAnswer] = "exercise-images/current-rhythm.png"
                tempColors[currentAnswer] = "#ffffff"
                answers = tempAnswers
                colors = tempColors
172 173
            }
        }
174
        style: MinuetButtonStyle{ labelHorizontalAlignment: Qt.AlignHCenter }
175 176
    }
}