Warning, /education/gcompris/src/activities/lang/Quiz.qml is written in an unsupported language. File is not indexed.

0001 /* GCompris - Quiz.qml
0002 *
0003 * Copyright (C) Siddhesh suthar <siddhesh.it@gmail.com> (Qt Quick port)
0004 *
0005 * Authors:
0006 *   Pascal Georges (pascal.georges1@free.fr) (GTK+ version)
0007 *   Holger Kaelberer <holger.k@elberer.de> (Qt Quick port of imageid)
0008 *   Siddhesh suthar <siddhesh.it@gmail.com> (Qt Quick port)
0009 *   Bruno Coudoin <bruno.coudoin@gcompris.net> (Integration Lang dataset)
0010 *
0011 *   SPDX-License-Identifier: GPL-3.0-or-later
0012 */
0013 import QtQuick 2.12
0014 import GCompris 1.0
0015 import QtGraphicalEffects 1.0
0016 
0017 import "../../core"
0018 import "lang.js" as Activity
0019 import "quiz.js" as QuizActivity
0020 
0021 Item {
0022     id: quiz
0023     opacity: 0
0024 
0025     property alias background: background
0026     property alias bonus: bonus
0027     property alias score: score
0028     property alias wordImage: wordImage
0029     property alias imageFrame: imageFrame
0030     property alias wordListModel: wordListModel
0031     property alias wordListView: wordListView
0032     property alias parser: parser
0033     property var goodWord
0034     property bool horizontalLayout: background.width >= background.height
0035     property bool buttonsBlocked: false
0036 
0037     function init(loadedItems_, wordList_, mode_) {
0038         opacity = 1
0039         loadedItems_.forceActiveFocus()
0040         return QuizActivity.init(loadedItems_, wordList_, mode_)
0041     }
0042 
0043     function restoreFocus() {
0044         background.forceActiveFocus();
0045     }
0046 
0047     onGoodWordChanged: Activity.playWord(goodWord.voice)
0048 
0049     Behavior on opacity { PropertyAnimation { duration: 200 } }
0050 
0051     Item {
0052         id: background
0053         anchors.fill: parent
0054 
0055         property bool keyNavigation: false
0056 
0057         Keys.enabled: !quiz.buttonsBlocked
0058 
0059         Keys.onEscapePressed: {
0060             imageReview.start()
0061         }
0062         Keys.onRightPressed: {
0063             keyNavigation = true
0064             wordListView.incrementCurrentIndex()
0065         }
0066         Keys.onLeftPressed:  {
0067             keyNavigation = true
0068             wordListView.decrementCurrentIndex()
0069         }
0070         Keys.onDownPressed:  {
0071             keyNavigation = true
0072             wordListView.incrementCurrentIndex()
0073         }
0074         Keys.onUpPressed:  {
0075             keyNavigation = true
0076             wordListView.decrementCurrentIndex()
0077         }
0078         Keys.onSpacePressed:  {
0079             keyNavigation = true
0080             wordListView.currentItem.children[1].pressed()
0081         }
0082         Keys.onEnterPressed:  {
0083             keyNavigation = true
0084             wordListView.currentItem.children[1].pressed()
0085         }
0086         Keys.onReturnPressed:  {
0087             keyNavigation = true
0088             wordListView.currentItem.children[1].pressed()
0089         }
0090         Keys.onTabPressed: {
0091             repeatItem.clicked()
0092         }
0093         Keys.onReleased: {
0094             if (event.key === Qt.Key_Back) {
0095                 event.accepted = true
0096                 imageReview.start()
0097             }
0098         }
0099 
0100         JsonParser {
0101             id: parser
0102 
0103             onError: console.error("Lang: Error parsing json: " + msg);
0104         }
0105 
0106         ListModel {
0107             id: wordListModel
0108         }
0109 
0110         Grid {
0111             id: gridId
0112             columns: quiz.horizontalLayout ? 2 : 1
0113             spacing: 10 * ApplicationInfo.ratio
0114             anchors.fill: parent
0115             anchors.margins: spacing
0116 
0117             Item {
0118                 id: imageContainer
0119                 width: quiz.horizontalLayout
0120                        ? background.width  - wordListView.width - gridId.spacing * 3
0121                        : wordListView.width
0122                 height: quiz.horizontalLayout
0123                         ? wordListView.height
0124                         : background.height - bar.height - wordListView.height - gridId.spacing * 3
0125 
0126                 Rectangle {
0127                     id: imageFrame
0128                     anchors.centerIn: parent
0129                     color: "#E0E0F7"
0130                     border.color: "#373737"
0131                     border.width: ApplicationInfo.ratio
0132                     width: quiz.horizontalLayout ?
0133                             Math.min(parent.width * 0.6, parent.height - repeatItem.height * 2 - gridId.spacing * 2) :
0134                             Math.min(parent.height, parent.width - repeatItem.height * 2 - gridId.spacing * 2)
0135                     height: width
0136                     radius: width * 0.1
0137                     z: 11
0138                     visible: QuizActivity.mode !== 3
0139 
0140                     Image {
0141                         id: wordImage
0142                         // Images are not svg
0143                         width: parent.width * 0.9
0144                         height: width
0145                         anchors.centerIn: parent
0146 
0147                         property string nextSource
0148                         function changeSource(nextSource_) {
0149                             nextSource = nextSource_
0150                             animImage.start()
0151                         }
0152 
0153                         SequentialAnimation {
0154                             id: animImage
0155                             PropertyAnimation {
0156                                 target: wordImage
0157                                 property: "opacity"
0158                                 to: 0
0159                                 duration: 100
0160                             }
0161                             PropertyAction {
0162                                 target: wordImage
0163                                 property: "source"
0164                                 value: wordImage.nextSource
0165                             }
0166                             PropertyAnimation {
0167                                 target: wordImage
0168                                 property: "opacity"
0169                                 to: 1
0170                                 duration: 100
0171                             }
0172                         }
0173                         MouseArea {
0174                             anchors.fill: parent
0175                             onClicked: Activity.playWord(goodWord.voice)
0176                         }
0177                     }
0178                 }
0179             }
0180 
0181             ListView {
0182                 id: wordListView
0183                 width: quiz.horizontalLayout
0184                        ? background.width * 0.55
0185                        : background.width - gridId.anchors.margins * 2
0186                 height: quiz.horizontalLayout
0187                         ? background.height - bar.height
0188                         : (background.height - bar.height) * 0.6
0189                 spacing: 2 * ApplicationInfo.ratio
0190                 orientation: Qt.Vertical
0191                 verticalLayoutDirection: ListView.TopToBottom
0192                 interactive: false
0193                 model: wordListModel
0194 
0195                 highlight:  Rectangle {
0196                     width: (QuizActivity.mode == 1) ? wordListView.width - wordListView.buttonHeight :
0197                                                         wordListView.width
0198                     height: wordListView.buttonHeight
0199                     color: "#AAFFFFFF"
0200                     radius: 5
0201                     visible: background.keyNavigation
0202                     y: wordListView.currentItem ? wordListView.currentItem.y : 0
0203                     Behavior on y {
0204                         SpringAnimation {
0205                             spring: 3
0206                             damping: 0.2
0207                         }
0208                     }
0209                 }
0210                 highlightFollowsCurrentItem: false
0211                 focus: true
0212                 keyNavigationWraps: true
0213 
0214                 property int buttonHeight: height / wordListModel.count * 0.9
0215 
0216                 delegate: Item {
0217 
0218                     id: wordListViewDelegate
0219 
0220                     width: wordListView.width
0221                     height: wordListView.buttonHeight
0222 
0223                     Image {
0224                         id: wordImageQuiz
0225                         width: height
0226                         height: wordListView.buttonHeight
0227                         mipmap: true
0228                         source: image
0229                         z: 7
0230                         fillMode: Image.PreserveAspectFit
0231                         anchors.right: parent.right
0232                         visible: (QuizActivity.mode == 1) ? true : false  // hide images after first mini game
0233                     }
0234 
0235                     AnswerButton {
0236                         id: wordRectangle
0237                         width: parent.width * 0.6
0238                         height: wordListView.buttonHeight
0239                         textLabel: translatedTxt
0240                         anchors.right: wordImageQuiz.visible ? wordImageQuiz.left : parent.right
0241                         anchors.left: parent.left
0242                         audioEffects: activity.audioEffects
0243                         blockAllButtonClicks: quiz.buttonsBlocked
0244                         onPressed: {
0245                             quiz.buttonsBlocked = true
0246                             if(isCorrectAnswer) {
0247                                 score.currentSubLevel++
0248                                 score.playWinAnimation()
0249                             }
0250                         }
0251                         isCorrectAnswer: translatedTxt === quiz.goodWord.translatedTxt
0252                         onIncorrectlyPressed: {
0253                             // push the error to have it asked again
0254                             QuizActivity.remainingWords.unshift(quiz.goodWord);
0255                             QuizActivity.nextSubLevelQuiz();
0256                         }
0257                         onCorrectlyPressed: {
0258                             QuizActivity.nextSubLevelQuiz();
0259                         }
0260                     }
0261                 }
0262             }
0263         }
0264 
0265         BarButton {
0266             id: repeatItem
0267             source: "qrc:/gcompris/src/core/resource/bar_repeat.svg";
0268             sourceSize.width: 64 * ApplicationInfo.ratio
0269 
0270             z: 12
0271             anchors {
0272                 top: parent.top
0273                 left: parent.left
0274                 margins: 10 * ApplicationInfo.ratio
0275             }
0276             onClicked: Activity.playWord(goodWord.voice)
0277             Behavior on opacity { PropertyAnimation { duration: 200 } }
0278         }
0279 
0280         Score {
0281             id: score
0282             parent: quiz
0283         }
0284 
0285         Bonus {
0286             id: bonus
0287             onWin: imageReview.nextMiniGame()
0288         }
0289     }
0290 }