~baltix/gcompris-qt/0.97.1

« back to all changes in this revision

Viewing changes to src/activities/lang/Quiz.qml

  • Committer: Mantas Kriaučiūnas
  • Date: 2020-07-06 18:07:11 UTC
  • Revision ID: baltix@gmail.com-20200706180711-g254osu02cn8bc8p
GCompris-QT 0.97.1 with Lithuanian translation

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GCompris - Quiz.qml
 
2
*
 
3
* Copyright (C) Siddhesh suthar <siddhesh.it@gmail.com> (Qt Quick port)
 
4
*
 
5
* Authors:
 
6
*   Pascal Georges (pascal.georges1@free.fr) (GTK+ version)
 
7
*   Holger Kaelberer <holger.k@elberer.de> (Qt Quick port of imageid)
 
8
*   Siddhesh suthar <siddhesh.it@gmail.com> (Qt Quick port)
 
9
*   Bruno Coudoin <bruno.coudoin@gcompris.net> (Integration Lang dataset)
 
10
*
 
11
*   This program is free software; you can redistribute it and/or modify
 
12
*   it under the terms of the GNU General Public License as published by
 
13
*   the Free Software Foundation; either version 3 of the License, or
 
14
*   (at your option) any later version.
 
15
*
 
16
*   This program is distributed in the hope that it will be useful,
 
17
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
*   GNU General Public License for more details.
 
20
*
 
21
*   You should have received a copy of the GNU General Public License
 
22
*   along with this program; if not, see <https://www.gnu.org/licenses/>.
 
23
*/
 
24
import QtQuick 2.6
 
25
import GCompris 1.0
 
26
import QtGraphicalEffects 1.0
 
27
 
 
28
import "../../core"
 
29
import "lang.js" as Activity
 
30
import "quiz.js" as QuizActivity
 
31
 
 
32
Item {
 
33
    id: quiz
 
34
    opacity: 0
 
35
 
 
36
    property alias background: background
 
37
    property alias bonus: bonus
 
38
    property alias score: score
 
39
    property alias wordImage: wordImage
 
40
    property alias imageFrame: imageFrame
 
41
    property alias wordListModel: wordListModel
 
42
    property alias wordListView: wordListView
 
43
    property alias parser: parser
 
44
    property var goodWord
 
45
    property bool horizontalLayout: background.width >= background.height
 
46
    property bool buttonsBlocked: false
 
47
 
 
48
    function init(loadedItems_, wordList_, mode_) {
 
49
        opacity = 1
 
50
        loadedItems_.forceActiveFocus()
 
51
        return QuizActivity.init(loadedItems_, wordList_, mode_)
 
52
    }
 
53
 
 
54
    onGoodWordChanged: Activity.playWord(goodWord.voice)
 
55
 
 
56
    Behavior on opacity { PropertyAnimation { duration: 200 } }
 
57
 
 
58
    Image {
 
59
        id: background
 
60
        source: "qrc:/gcompris/src/activities/lang/resource/imageid-bg.svg"
 
61
        fillMode: Image.PreserveAspectCrop
 
62
        sourceSize.width: Math.max(parent.width, parent.height)
 
63
        height: parent.height
 
64
        anchors.fill: parent
 
65
 
 
66
        property bool keyNavigation: false
 
67
        
 
68
        Keys.enabled: !quiz.buttonsBlocked
 
69
 
 
70
        Keys.onEscapePressed: {
 
71
            imageReview.start()
 
72
        }
 
73
        Keys.onRightPressed: {
 
74
            keyNavigation = true
 
75
            wordListView.incrementCurrentIndex()
 
76
        }
 
77
        Keys.onLeftPressed:  {
 
78
            keyNavigation = true
 
79
            wordListView.decrementCurrentIndex()
 
80
        }
 
81
        Keys.onDownPressed:  {
 
82
            keyNavigation = true
 
83
            wordListView.incrementCurrentIndex()
 
84
        }
 
85
        Keys.onUpPressed:  {
 
86
            keyNavigation = true
 
87
            wordListView.decrementCurrentIndex()
 
88
        }
 
89
        Keys.onSpacePressed:  {
 
90
            keyNavigation = true
 
91
            wordListView.currentItem.children[1].pressed()
 
92
        }
 
93
        Keys.onEnterPressed:  {
 
94
            keyNavigation = true
 
95
            wordListView.currentItem.children[1].pressed()
 
96
        }
 
97
        Keys.onReturnPressed:  {
 
98
            keyNavigation = true
 
99
            wordListView.currentItem.children[1].pressed()
 
100
        }
 
101
        Keys.onReleased: {
 
102
            if (event.key === Qt.Key_Back) {
 
103
                event.accepted = true
 
104
                imageReview.start()
 
105
            }
 
106
        }
 
107
 
 
108
        JsonParser {
 
109
            id: parser
 
110
 
 
111
            onError: console.error("Lang: Error parsing json: " + msg);
 
112
        }
 
113
 
 
114
        ListModel {
 
115
            id: wordListModel
 
116
        }
 
117
 
 
118
        Grid {
 
119
            id: gridId
 
120
            columns: quiz.horizontalLayout ? 2 : 1
 
121
            spacing: 0.5 * ApplicationInfo.ratio
 
122
            anchors.fill: parent
 
123
            anchors.margins: 10 * ApplicationInfo.ratio
 
124
 
 
125
            Item {
 
126
                width: quiz.horizontalLayout
 
127
                       ? background.width * 0.40
 
128
                       : background.width - gridId.anchors.margins * 2
 
129
                height: quiz.horizontalLayout
 
130
                        ? background.height - bar.height
 
131
                        : (background.height - bar.height) * 0.4
 
132
 
 
133
                Image {
 
134
                    id: imageFrame
 
135
                    anchors {
 
136
                        horizontalCenter: parent.horizontalCenter
 
137
                        verticalCenter: parent.verticalCenter
 
138
                    }
 
139
                    source: "qrc:/gcompris/src/activities/lang/resource/imageid_frame.svg"
 
140
                    sourceSize.width: quiz.horizontalLayout ? parent.width * 0.7 : quiz.width - repeatItem.width - score.width - 50 * ApplicationInfo.ratio
 
141
                    z: 11
 
142
                    visible: QuizActivity.mode !== 3
 
143
 
 
144
                    Image {
 
145
                        id: wordImage
 
146
                        // Images are not svg
 
147
                        width: Math.min(parent.width, parent.height) * 0.9
 
148
                        height: width
 
149
                        anchors.centerIn: parent
 
150
 
 
151
                        property string nextSource
 
152
                        function changeSource(nextSource_) {
 
153
                            nextSource = nextSource_
 
154
                            animImage.start()
 
155
                        }
 
156
 
 
157
                        SequentialAnimation {
 
158
                            id: animImage
 
159
                            PropertyAnimation {
 
160
                                target: wordImage
 
161
                                property: "opacity"
 
162
                                to: 0
 
163
                                duration: 100
 
164
                            }
 
165
                            PropertyAction {
 
166
                                target: wordImage
 
167
                                property: "source"
 
168
                                value: wordImage.nextSource
 
169
                            }
 
170
                            PropertyAnimation {
 
171
                                target: wordImage
 
172
                                property: "opacity"
 
173
                                to: 1
 
174
                                duration: 100
 
175
                            }
 
176
                        }
 
177
                        MouseArea {
 
178
                            anchors.fill: parent
 
179
                            onClicked: Activity.playWord(goodWord.voice)
 
180
                        }
 
181
                    }
 
182
                }
 
183
            }
 
184
 
 
185
 
 
186
            ListView {
 
187
                id: wordListView
 
188
                width: quiz.horizontalLayout
 
189
                       ? background.width * 0.55
 
190
                       : background.width - gridId.anchors.margins * 2
 
191
                height: quiz.horizontalLayout
 
192
                        ? background.height - bar.height
 
193
                        : (background.height - bar.height) * 0.60
 
194
                spacing: 2 * ApplicationInfo.ratio
 
195
                orientation: Qt.Vertical
 
196
                verticalLayoutDirection: ListView.TopToBottom
 
197
                interactive: false
 
198
                model: wordListModel
 
199
 
 
200
                highlight:  Rectangle {
 
201
                    width: wordListView.width
 
202
                    height: wordListView.buttonHeight
 
203
                    color: "lightsteelblue"
 
204
                    radius: 5
 
205
                    visible: background.keyNavigation
 
206
                    y: wordListView.currentItem ? wordListView.currentItem.y : 0
 
207
                    Behavior on y {
 
208
                        SpringAnimation {
 
209
                            spring: 3
 
210
                            damping: 0.2
 
211
                        }
 
212
                    }
 
213
                }
 
214
                highlightFollowsCurrentItem: false
 
215
                focus: true
 
216
                keyNavigationWraps: true
 
217
 
 
218
                property int buttonHeight: height / wordListModel.count * 0.9
 
219
 
 
220
                delegate: Item {
 
221
 
 
222
                    id: wordListViewDelegate
 
223
 
 
224
                    width: wordListView.width
 
225
                    height: wordListView.buttonHeight
 
226
                    anchors.right: parent.right
 
227
                    anchors.left: parent.left
 
228
 
 
229
                    Image {
 
230
                        id: wordImageQuiz
 
231
                        width: height
 
232
                        height: wordListView.buttonHeight
 
233
                        mipmap: true
 
234
                        source: image
 
235
                        z: 7
 
236
                        fillMode: Image.PreserveAspectFit
 
237
                        anchors.leftMargin: 5 * ApplicationInfo.ratio
 
238
                        visible:  (QuizActivity.mode == 1) ? true : false  // hide images after first mini game
 
239
                    }
 
240
 
 
241
                    AnswerButton {
 
242
                        id: wordRectangle
 
243
                        width: parent.width * 0.6
 
244
                        height: wordListView.buttonHeight
 
245
                        textLabel: translatedTxt
 
246
                        anchors.left: wordImageQuiz.left
 
247
                        anchors.right: parent.right
 
248
                        blockAllButtonClicks: quiz.buttonsBlocked
 
249
                        onPressed: quiz.buttonsBlocked = true
 
250
                        isCorrectAnswer: translatedTxt === quiz.goodWord.translatedTxt
 
251
                        onIncorrectlyPressed: {
 
252
                            // push the error to have it asked again
 
253
                            QuizActivity.remainingWords.unshift(quiz.goodWord);
 
254
                            QuizActivity.nextSubLevelQuiz();
 
255
                        }
 
256
                        onCorrectlyPressed: {
 
257
                            QuizActivity.nextSubLevelQuiz();
 
258
                        }
 
259
                    }
 
260
                }
 
261
            }
 
262
        }
 
263
 
 
264
        BarButton {
 
265
            id: repeatItem
 
266
            source: "qrc:/gcompris/src/core/resource/bar_repeat.svg";
 
267
            sourceSize.width: 80 * ApplicationInfo.ratio
 
268
 
 
269
            z: 12
 
270
            anchors {
 
271
                top: parent.top
 
272
                left: parent.left
 
273
                margins: 10 * ApplicationInfo.ratio
 
274
            }
 
275
            onClicked: Activity.playWord(goodWord.voice)
 
276
            Behavior on opacity { PropertyAnimation { duration: 200 } }
 
277
        }
 
278
 
 
279
        Score {
 
280
            id: score
 
281
            parent: quiz
 
282
        }
 
283
 
 
284
        Bonus {
 
285
            id: bonus
 
286
            onWin: imageReview.nextMiniGame()
 
287
        }
 
288
    }
 
289
}