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

0001 /* GCompris - Calcudoku.qml
0002  *
0003  * SPDX-FileCopyrightText: 2023 Johnny Jazeix <jazeix@gmail.com>
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 import QtQuick 2.12
0008 import GCompris 1.0
0009 
0010 import "../../core"
0011 import "calcudoku.js" as Activity
0012 import "."
0013 
0014 ActivityBase {
0015     id: activity
0016     focus: true
0017 
0018     onStart: {focus=true}
0019     onStop: {}
0020 
0021     pageComponent: Image {
0022         id: background
0023         anchors.fill: parent
0024         source: Activity.url + "background.svg"
0025         sourceSize.width: width
0026         sourceSize.height: height
0027         fillMode: Image.PreserveAspectCrop
0028 
0029         signal start
0030         signal stop
0031 
0032         property double baseMargins: 10 * ApplicationInfo.ratio
0033         property double activityLayoutHeight: height - bar.height * 1.5
0034         property bool isHorizontalLayout: width >= activityLayoutHeight
0035 
0036         Component.onCompleted: {
0037             dialogActivityConfig.initialize()
0038             activity.start.connect(start)
0039             activity.stop.connect(stop)
0040             focus = true
0041         }
0042 
0043         QtObject {
0044             id: items
0045             property alias background: background
0046             property int currentLevel: activity.currentLevel
0047             property alias bonus: bonus
0048             property alias score: score
0049             property GCSfx audioEffects: activity.audioEffects
0050             property alias availablePiecesModel: availablePieces
0051             property alias columns: calcudokuColumn.columns
0052             property alias rows: calcudokuColumn.rows
0053             property alias calcudokuModel: calcudokuModel
0054             readonly property var levels: activity.datasetLoader.data
0055             property bool buttonsBlocked: false
0056         }
0057         onStart: Activity.start(items)
0058 
0059         onStop: { Activity.stop() }
0060 
0061         DialogHelp {
0062             id: dialogHelp
0063             onClose: home()
0064         }
0065 
0066         DialogChooseLevel {
0067             id: dialogActivityConfig
0068             currentActivity: activity.activityInfo
0069             onClose: {
0070                 home()
0071             }
0072 
0073             onSaveData: {
0074                 levelFolder = dialogActivityConfig.chosenLevels
0075                 currentActivity.currentLevels = dialogActivityConfig.chosenLevels
0076                 ApplicationSettings.setCurrentLevels(currentActivity.name, dialogActivityConfig.chosenLevels)
0077             }
0078 
0079             onStartActivity: {
0080                 background.stop()
0081                 background.start()
0082             }
0083         }
0084 
0085         Bar {
0086             id: bar
0087             level: items.currentLevel + 1
0088             content: BarEnumContent { value: help | home | level | reload | activityConfig }
0089             onHelpClicked: {
0090                 displayDialog(dialogHelp)
0091             }
0092             onActivityConfigClicked: {
0093                 displayDialog(dialogActivityConfig)
0094             }
0095             onPreviousLevelClicked: Activity.previousLevel()
0096             onNextLevelClicked: Activity.nextLevel()
0097             onReloadClicked: {
0098                 if(!items.buttonsBlocked)
0099                     Activity.reinitLevel()
0100             }
0101             onHomeClicked: activity.home()
0102         }
0103 
0104         Bonus {
0105             id: bonus
0106             z: 1002
0107             Component.onCompleted: win.connect(Activity.nextLevel)
0108         }
0109 
0110         Score {
0111             id: score
0112             z: 1003
0113             height: 48 * ApplicationInfo.ratio
0114             anchors.bottom: bar.top
0115             anchors.right: background.right
0116             anchors.bottomMargin: background.baseMargins
0117             onStop: Activity.incrementLevel()
0118         }
0119 
0120         Keys.enabled: !items.buttonsBlocked
0121         Keys.onPressed: {
0122             Activity.onKeyPressed(event);
0123         }
0124 
0125         CalcudokuListWidget {
0126             id: availablePieces
0127             audioEffects: activity.audioEffects
0128             inputBlocked: items.buttonsBlocked
0129         }
0130 
0131         ListModel {
0132             id: calcudokuModel
0133         }
0134 
0135         Item {
0136             id: gridLayout
0137             anchors.margins: background.baseMargins
0138             anchors.bottom: score.top
0139             anchors.right: background.right
0140             states: [
0141                 State {
0142                     name: "horizontalLayout"
0143                     when: background.isHorizontalLayout
0144                     AnchorChanges {
0145                         target: gridLayout
0146                         anchors.top: background.top
0147                         anchors.left: availablePieces.right
0148                     }
0149                 },
0150                 State {
0151                     name: "verticalLayout"
0152                     when: !background.isHorizontalLayout
0153                     AnchorChanges {
0154                         target: gridLayout
0155                         anchors.top: availablePieces.bottom
0156                         anchors.left: background.left
0157                     }
0158                 }
0159             ]
0160         }
0161 
0162         Grid {
0163             z: 100
0164             id: calcudokuColumn
0165             x: Math.round(gridLayout.x + gridLayout.width * 0.5 - width * 0.5)
0166             y: Math.round(gridLayout.y + gridLayout.height * 0.5 - height * 0.5)
0167             width:  Math.min(gridLayout.width, gridLayout.height)
0168             height: width
0169             property int squareSize: 2 * Math.round(width / calcudokuColumn.columns / 2)
0170 
0171             Repeater {
0172                 model: calcudokuModel
0173                 delegate: blueSquare
0174 
0175                 Component {
0176                     id: blueSquare
0177                     CalcudokuCase {
0178                         x: Math.floor((index % calcudokuColumn.columns) * width)
0179                         y: Math.floor((index / calcudokuColumn.columns) * height)
0180                         width: parent != null ? calcudokuColumn.squareSize : 1
0181                         height: width
0182                         gridIndex: index
0183                         isInitial: initial
0184                         operator: operatorValue
0185                         result: resultValue
0186                         text: textValue
0187                         state: mState
0188                     }
0189                 }
0190             }
0191         }
0192 
0193         Grid {
0194             z: 100
0195             id: calcudokuCages
0196             x: calcudokuColumn.x
0197             y: calcudokuColumn.y
0198             width:  calcudokuColumn.width
0199             height: calcudokuColumn.height
0200             columns: calcudokuColumn.columns
0201             rows: calcudokuColumn.rows
0202 
0203             Repeater {
0204                 model: calcudokuModel
0205                 delegate: cageWalls
0206 
0207                 Component {
0208                     id: cageWalls
0209                     CalcudokuCage {
0210                         x: Math.floor((index % calcudokuColumn.columns) * width)
0211                         y: Math.floor((index / calcudokuColumn.columns) * height)
0212                         width: parent != null ? calcudokuColumn.squareSize : 1
0213                         height: width
0214                         topWallVisible: topWall
0215                         leftWallVisible: leftWall
0216                         rightWallVisible: rightWall
0217                         bottomWallVisible: bottomWall
0218                     }
0219                 }
0220             }
0221         }
0222 
0223         MouseArea {
0224             id: dynamic
0225             anchors.fill: calcudokuColumn
0226             enabled: !items.buttonsBlocked
0227             hoverEnabled: true
0228 
0229             property int previousHoveredCase: -1
0230             onPositionChanged: {
0231                 var x = Math.floor(calcudokuColumn.rows * mouseX / (calcudokuColumn.width+1));
0232                 var y = Math.floor(calcudokuColumn.columns * mouseY / (calcudokuColumn.height+1));
0233                 var id = x + y * calcudokuColumn.rows;
0234 
0235                 // Only color if we can modify the case
0236                 if(calcudokuModel.get(id).mState === "default")
0237                     calcudokuModel.get(id).mState = "hovered";
0238 
0239                 // Restore previous case if different from the new one
0240                 if(previousHoveredCase != id) {
0241                     if(previousHoveredCase != -1 && calcudokuModel.get(previousHoveredCase).mState === "hovered")
0242                         calcudokuModel.get(previousHoveredCase).mState = "default"
0243                     previousHoveredCase = id
0244                 }
0245             }
0246             onExited: {
0247                 if(previousHoveredCase != -1 && calcudokuModel.get(previousHoveredCase).mState === "hovered")
0248                     calcudokuModel.get(previousHoveredCase).mState = "default"
0249                 previousHoveredCase = -1
0250             }
0251 
0252             onClicked: {
0253                 var x = Math.floor(calcudokuColumn.rows * mouseX / calcudokuColumn.width);
0254                 var y = Math.floor(calcudokuColumn.columns * mouseY / calcudokuColumn.height);
0255                 Activity.clickOn(x, y)
0256             }
0257         }
0258     }
0259 }