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

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