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

0001 /* GCompris - Positions.qml
0002  *
0003  * SPDX-FileCopyrightText: 2021 Mariam Fahmy <mariamfahmy66@gmail.com>
0004  *
0005  * Authors:
0006  *   Mariam Fahmy <mariamfahmy66@gmail.com>
0007  *
0008  *   SPDX-License-Identifier: GPL-3.0-or-later
0009  */
0010 import QtQuick 2.12
0011 import GCompris 1.0
0012 import "../../core"
0013 import "positions.js" as Activity
0014 import "qrc:/gcompris/src/core/core.js" as Core
0015 
0016 ActivityBase {
0017     id: activity
0018 
0019     onStart: focus = true;
0020     pageComponent: Image {
0021         id: background
0022         source: "qrc:/gcompris/src/activities/family/resource/background.svg"
0023         sourceSize.width: width
0024         sourceSize.height: height
0025         focus: true
0026         signal start
0027         signal stop
0028         fillMode: Image.PreserveAspectCrop
0029 
0030         property bool keyboardMode: false
0031 
0032         QtObject {
0033             id: items
0034             property int currentLevel: activity.currentLevel
0035             property alias bonus: bonus
0036             readonly property var levels: activity.datasetLoader.data
0037             property GCSfx audioEffects: activity.audioEffects
0038             property alias score: score
0039             property int checkState: -1
0040             property int selectedPosition: -1
0041             property string questionText: ""
0042             property alias positionModels: positionModels
0043             property var view: items.currentLevel % 2 !== 0 ? answerViews : positionViews
0044             property bool buttonsBlocked: false
0045             property alias errorRectangle: errorRectangle
0046         }
0047 
0048         Component.onCompleted: {
0049             activity.start.connect(start)
0050             activity.stop.connect(stop)
0051         }
0052         onStart: { Activity.start(items) }
0053         onStop: { Activity.stop() }
0054 
0055         Keys.enabled: !bonus.isPlaying
0056         Keys.onPressed: {
0057             if(event.key === Qt.Key_Space || event.key === Qt.Key_Enter || event.key === Qt.Key_Return) {
0058                 Activity.verifyAnswer()
0059                 event.accepted = true
0060             }
0061             else if(event.key === Qt.Key_Left) {
0062                 items.view.moveCurrentIndexLeft()
0063                 items.view.currentItem.selectCurrentItem()
0064                 event.accepted = true
0065             }
0066             else if(event.key === Qt.Key_Right) {
0067                 items.view.moveCurrentIndexRight()
0068                 items.view.currentItem.selectCurrentItem()
0069                 event.accepted = true
0070             }
0071             else if(event.key === Qt.Key_Up) {
0072                 items.view.moveCurrentIndexUp()
0073                 items.view.currentItem.selectCurrentItem()
0074                 event.accepted = true
0075             }
0076             else if(event.key === Qt.Key_Down) {
0077                 items.view.moveCurrentIndexDown()
0078                 items.view.currentItem.selectCurrentItem()
0079                 event.accepted = true
0080             }
0081         }
0082 
0083         Keys.onReleased: {
0084             keyboardMode = true
0085             event.accepted = false
0086         }
0087 
0088         Item {
0089             id: mainScreen
0090             width: background.width - okButton.width
0091             height: background.height - bar.height * 1.5
0092             anchors.top: background.top
0093             anchors.left: background.left
0094 
0095             property bool horizontalLayout: mainScreen.width >= mainScreen.height
0096 
0097             Rectangle {
0098                 id: backgroundScreen
0099                 width: parent.width * 0.7
0100                 height: parent.height * 0.7
0101                 anchors.horizontalCenter: parent.horizontalCenter
0102                 anchors.top: parent.top
0103                 anchors.topMargin: 30 * ApplicationInfo.ratio
0104                 visible: items.currentLevel % 2 !== 0 ? true : false
0105                 color: "#55333333"
0106                 radius: 5
0107 
0108                 BoxBoyPosition {
0109                     id: currentPosition
0110                     anchors.centerIn: parent
0111                     checkState: items.checkState
0112                     width: Math.min(parent.width, parent.height)
0113                     height: width
0114                 }
0115 
0116                 states: [
0117                     State {
0118                         name: "verticalScreen"
0119                         when: !mainScreen.horizontalLayout
0120                         PropertyChanges {
0121                             target: backgroundScreen
0122                             height: parent.height * 0.5
0123                         }
0124                     }
0125                 ]
0126             }
0127 
0128             GridView {
0129                 id: answerViews
0130                 width: mainScreen.width * 0.7
0131                 height: mainScreen.height * 0.1
0132                 anchors.horizontalCenter: parent.horizontalCenter
0133                 visible: items.currentLevel % 2 !== 0 ? true : false
0134                 anchors.top: backgroundScreen.bottom
0135                 anchors.topMargin: 30 * ApplicationInfo.ratio
0136                 cellWidth: mainScreen.horizontalLayout ? answerViews.width / answerViews.count : answerViews.width
0137                 cellHeight: mainScreen.horizontalLayout ? answerViews.height : answerViews.height / answerViews.count
0138                 keyNavigationWraps: true
0139                 model: positionModels
0140                 focus: false
0141                 currentIndex: -1
0142 
0143                 delegate: Rectangle {
0144                     id: answer
0145                     color: index == answerViews.currentIndex ? "#FFFFFFFF" : "#80FFFFFF"
0146                     radius: 15
0147                     width: answerViews.cellWidth - 2 * ApplicationInfo.ratio
0148                     height: answerViews.cellHeight - 2 * ApplicationInfo.ratio
0149                     border.width: index == answerViews.currentIndex ? 3 : 0
0150                     border.color: "#373737"
0151 
0152                     property alias text: answerText.text
0153                     GCText {
0154                         id: answerText
0155                         text: stateName
0156                         fontSize: smallSize
0157                         wrapMode: Text.WordWrap
0158                         fontSizeMode: Text.Fit
0159                         width: answer.width
0160                         height: answer.height
0161                         verticalAlignment: Text.AlignVCenter
0162                         horizontalAlignment: Text.AlignHCenter
0163                     }
0164 
0165                     MouseArea {
0166                         id: textArea
0167                         anchors.fill: parent
0168                         onClicked: selectCurrentItem()
0169                         enabled: !items.buttonsBlocked
0170                     }
0171 
0172                     function selectCurrentItem() {
0173                         answerViews.currentIndex = index
0174                         items.selectedPosition = stateId
0175                     }
0176                 }
0177 
0178                 states: [
0179                     State {
0180                         name: "verticalScreen"
0181                         when: !mainScreen.horizontalLayout
0182                         PropertyChanges {
0183                             target: answerViews
0184                             height: mainScreen.height * 0.3
0185                         }
0186                     }
0187                 ]
0188             }
0189 
0190             ListModel {
0191                 id: positionModels
0192             }
0193 
0194             GridView {
0195                 id: positionViews
0196                 anchors.top: questionArea.bottom
0197                 anchors.horizontalCenter: mainScreen.horizontalCenter
0198                 visible: items.currentLevel % 2 === 0 ? true : false
0199                 width: mainScreen.width * 0.9
0200                 height: mainScreen.height * 0.8
0201                 interactive: false
0202                 cellWidth: itemWidth
0203                 cellHeight: itemWidth
0204                 keyNavigationWraps: true
0205                 model: positionModels
0206                 layoutDirection: Qt.LeftToRight
0207                 highlightFollowsCurrentItem: true
0208                 focus: false
0209                 currentIndex: -1
0210 
0211                 property int itemWidth: Core.fitItems(positionViews.width, positionViews.height, positionViews.count)
0212 
0213                 delegate: BoxBoyPosition {
0214                     id: posItem
0215                     checkState: stateId
0216                     width: positionViews.itemWidth
0217                     height: positionViews.itemWidth
0218                     scale: mouseArea.containsMouse? 1.1 : 1
0219 
0220                     MouseArea {
0221                         id: mouseArea
0222                         anchors.fill: parent
0223                         onClicked: selectCurrentItem()
0224                         enabled: !items.buttonsBlocked
0225                         hoverEnabled: true
0226                     }
0227 
0228                     function selectCurrentItem() {
0229                         positionViews.currentIndex = index
0230                         items.selectedPosition = positionModels.get(index).stateId
0231                     }
0232                 }
0233 
0234                 highlight: Rectangle {
0235                     width: positionViews.itemWidth
0236                     height: positionViews.itemWidth
0237                     radius: 15
0238                     color: "#C0FFFFFF"
0239                     border.width: 3
0240                     border.color: "#373737"
0241                     Behavior on x { SpringAnimation { spring: 2; damping: 0.2 } }
0242                     Behavior on y { SpringAnimation { spring: 2; damping: 0.2 } }
0243                  }
0244             }
0245 
0246             Rectangle {
0247                 id: questionArea
0248                 anchors.centerIn: questionItem
0249                 width: questionItem.paintedWidth * 1.1
0250                 height: questionItem.paintedHeight * 1.1
0251                 radius: 10
0252                 color: "#373737"
0253                 border.width: 2
0254                 border.color: "#F2F2F2"
0255                 visible: questionItem.visible
0256             }
0257 
0258             GCText {
0259                 id: questionItem
0260                 visible: items.currentLevel % 2 === 0 ? true : false
0261                 anchors.horizontalCenter: mainScreen.horizontalCenter
0262                 anchors.top: mainScreen.top
0263                 anchors.topMargin: background.height * 0.02
0264                 text: items.questionText
0265                 fontSize: smallSize
0266                 width: parent.width * 0.8
0267                 horizontalAlignment: Text.AlignHCenter
0268                 wrapMode: Text.WordWrap
0269                 color: "white"
0270             }
0271         }
0272         
0273         ErrorRectangle {
0274             id: errorRectangle
0275             radius: 15
0276             imageSize: okButton.width
0277 
0278             function releaseControls() { items.buttonsBlocked = false; }
0279 
0280             function startAnimation() {
0281                 errorRectangle.width = items.view.currentItem.width;
0282                 errorRectangle.height = items.view.currentItem.height;
0283                 if (answerViews.visible) {
0284                     errorRectangle.x = answerViews.x + items.view.currentItem.x;
0285                     errorRectangle.y = answerViews.y + items.view.currentItem.y;
0286                 } else {
0287                     errorRectangle.x = positionViews.x + items.view.currentItem.x;
0288                     errorRectangle.y = positionViews.y + items.view.currentItem.y;
0289                 }
0290                 errorAnimation.restart();
0291             }
0292         }
0293 
0294         DialogHelp {
0295             id: dialogHelpLeftRight
0296             onClose: home()
0297         }
0298 
0299         DialogChooseLevel {
0300             id: dialogActivityConfig
0301             currentActivity: activity.activityInfo
0302 
0303             onSaveData: {
0304                 levelFolder = dialogActivityConfig.chosenLevels
0305                 currentActivity.currentLevels = dialogActivityConfig.chosenLevels
0306                 ApplicationSettings.setCurrentLevels(currentActivity.name, dialogActivityConfig.chosenLevels)
0307             }
0308 
0309             onClose: {
0310                 home()
0311             }
0312 
0313             onStartActivity: {
0314                 background.stop()
0315                 background.start()
0316             }
0317         }
0318 
0319         Bar {
0320             id: bar
0321             level: items.currentLevel + 1
0322             content: BarEnumContent { value: help | home | level | activityConfig }
0323             onHelpClicked: {
0324                 displayDialog(dialogHelpLeftRight)
0325             }
0326             onActivityConfigClicked: {
0327                 displayDialog(dialogActivityConfig)
0328             }
0329             onPreviousLevelClicked: Activity.previousLevel()
0330             onNextLevelClicked: Activity.nextLevel()
0331             onHomeClicked: home()
0332         }
0333 
0334         BarButton {
0335             id: okButton
0336             anchors {
0337                 bottom: bar.top
0338                 right: parent.right
0339                 leftMargin: 10 * ApplicationInfo.ratio
0340                 rightMargin: 10 * ApplicationInfo.ratio
0341                 bottomMargin: 10 * ApplicationInfo.ratio
0342             }
0343             source: "qrc:/gcompris/src/core/resource/bar_ok.svg"
0344             width: (background.height - bar.height * 1.2) * 0.15
0345             sourceSize.width: width
0346             onClicked: Activity.verifyAnswer()
0347             mouseArea.enabled: !items.buttonsBlocked
0348         }
0349 
0350         Bonus {
0351             id: bonus
0352             onWin: Activity.nextLevel()
0353         }
0354 
0355         Score {
0356             id: score
0357             width: background.width * 0.1
0358             height: background.height * 0.1
0359             anchors.top: background.top
0360             anchors.topMargin: parent.height * 0.01
0361             anchors.bottom: undefined
0362             onStop: Activity.nextSubLevel()
0363         }
0364     }
0365 }