Warning, /education/kanagram/src/ui/main.qml is written in an unsupported language. File is not indexed.

0001 /***************************************************************************
0002  *   This file is part of the Kanagram project                             *
0003  *   Copyright 2014 Debjit Mondal <debjitmondal05@gmail.com>               *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
0019  ***************************************************************************/
0020 
0021 import QtQuick
0022 import QtQuick.Controls
0023 import QtMultimedia
0024 
0025 Rectangle {
0026     id: screen
0027     focus: true
0028     property int countDownTimerValue: 0
0029     property bool flagCorrectAnswer: true
0030 
0031     function nextAnagram() {
0032         kanagramGame.nextAnagram()
0033         if (blackboard.activeTimer) {
0034             kanagramGame.answerSkipped();
0035         }
0036         if (kanagramGame.hintHideTime())
0037             blackboard.showHintTimeInterval = 1
0038         if (kanagramGame.useSounds)
0039             if (chalkSound.hasAudio)
0040                 chalkSound.play();
0041             else
0042                 console.log("unable to play chalk sound " + chalkSound.error);
0043     }
0044 
0045     function checkSolved() {
0046         if (kanagramGame.anagram.length == 0) {
0047             if (kanagramGame.checkWord()) {
0048                 countDownTimerValue = 1;
0049                 flagCorrectAnswer = true;
0050                 if (kanagramGame.useSounds)
0051                     rightSound.play();
0052                 if (blackboard.activeTimer) {
0053                     kanagramGame.answerCorrect();
0054                 }
0055                 resetTimer.repeat = true
0056                 resetTimer.start()
0057             } else {
0058                 countDownTimerValue = 1;
0059                 flagCorrectAnswer = false;
0060                 if (kanagramGame.useSounds)
0061                     wrongSound.play();
0062                 if (blackboard.activeTimer) {
0063                     kanagramGame.answerIncorrect();
0064                 }
0065                 resetTimer.repeat = true
0066                 resetTimer.start()
0067             }
0068         }
0069     }
0070 
0071     Image {
0072         id: background
0073         anchors.fill: parent
0074         source: "images/background.jpg"
0075         smooth: true
0076         fillMode: Image.PreserveAspectCrop
0077     }
0078 
0079     Item {
0080         id: headerArea
0081         width: parent.width
0082         height: parent.height / 6
0083         anchors.top: screen.top
0084 
0085         Rectangle {
0086             id: toolBarTop
0087             anchors.fill: parent
0088             opacity: .25
0089             color: "black"
0090         }
0091 
0092         Image {
0093             id: header
0094             height: Math.round(parent.height * 0.8)
0095             width: headerArea.width - playericon.width - powerButton.width - 80
0096             anchors {
0097                 verticalCenter: parent.verticalCenter
0098                 horizontalCenter: parent.horizontalCenter
0099             }
0100             source: "images/header.png"
0101             smooth: true
0102             fillMode: Image.PreserveAspectFit
0103         }
0104 
0105         Image {
0106             id: playericon
0107             width: 60
0108             height: 60
0109             anchors.left:parent.left
0110             anchors.leftMargin: 20
0111             anchors.verticalCenter: parent.verticalCenter
0112             source: kanagramGame.singlePlayer ? "icons/1player.png" : "icons/2player.png"
0113             smooth: true
0114             fillMode: Image.PreserveAspectCrop
0115             function togglePlayerMode() {
0116                 kanagramGame.singlePlayer = !kanagramGame.singlePlayer;
0117             }
0118 
0119             MouseArea {
0120                 anchors.fill: parent
0121                 hoverEnabled: true
0122                 onEntered: playericon.state = "onEntered"
0123                 onExited: playericon.state = "onExited"
0124                 onClicked: playericon.togglePlayerMode()
0125             }
0126             states: State {
0127                 name: "onEntered"
0128                 PropertyChanges {
0129                     target: playerModeText
0130                     opacity: 1
0131                 }
0132                 PropertyChanges {
0133                     target: playericon
0134                     source: kanagramGame.singlePlayer ? "icons/2player.png" : "icons/1player.png"
0135                 }
0136             }
0137             State {
0138                 name: "onExited"
0139             }
0140             transitions: Transition {
0141                 PropertyAnimation {
0142                     properties: "x,y,opacity"
0143                     easing.type: Easing.Linear
0144                     easing.amplitude: 5.0
0145                     easing.period: 1
0146                 }
0147             }
0148         }
0149 
0150         Image {
0151             id: powerButton
0152             smooth: true
0153             height: parent.height / 3
0154             anchors {
0155                 verticalCenter: parent.verticalCenter
0156                 right: parent.right
0157                 rightMargin: parent.width / 24
0158             }
0159             source: "icons/on-off-light.svgz"
0160             fillMode: Image.PreserveAspectFit
0161 
0162             MouseArea {
0163                 anchors.fill: parent
0164                 hoverEnabled: true
0165                 onEntered: powerButton.state = "onEntered"
0166                 onExited: powerButton.state = "onExited"
0167                 onClicked: application.quit()
0168             }
0169 
0170             states: State {
0171                 name: "onEntered"
0172                 PropertyChanges {
0173                     target: quitText
0174                     opacity: 1
0175                 }
0176             }
0177             State {
0178                 name: "onExited"
0179             }
0180 
0181             transitions: Transition {
0182                 //AnchorAnimation { duration: 200 }
0183                 PropertyAnimation {
0184                     properties: "x,y,opacity"
0185                     easing.type: Easing.Linear
0186                     easing.amplitude: 5.0
0187                     easing.period: 1
0188                 }
0189             }
0190         }
0191 
0192         Text {
0193             id: quitText
0194             anchors {
0195                 top: powerButton.bottom
0196                 topMargin: 10
0197                 horizontalCenter: powerButton.horizontalCenter
0198             }
0199             color: "white"
0200             text: i18n("Quit")
0201             opacity: 0
0202             font.pixelSize: parent.width / 91
0203         }
0204 
0205         Text {
0206             id: playerModeText
0207             anchors {
0208                 top: playericon.bottom
0209                 topMargin: 2
0210                 horizontalCenter: playericon.horizontalCenter
0211             }
0212             color: "white"
0213             text: i18n("Change mode")
0214             opacity: 0
0215             font.pixelSize: parent.width / 91
0216         }
0217     } // End of headerArea
0218 
0219     Item {
0220         id: nextAnagram
0221         anchors {
0222             top: blackboard.top
0223             left: blackboard.right
0224             topMargin: blackboard.height / 7
0225         }
0226 
0227         Action {
0228             id: nextAnagramAction
0229             onTriggered: screen.nextAnagram();
0230             shortcut: "Ctrl+N"
0231         }
0232 
0233         Rectangle {
0234             id: nextAnagramButton
0235             width: blackboard.width / 9
0236             height: blackboard.height / 7
0237             radius: 8
0238             color: "black"
0239             opacity: 0.5
0240 
0241             MouseArea {
0242                 anchors.fill: parent
0243                 hoverEnabled: true
0244                 onEntered: nextAnagramButton.state = "onEntered"
0245                 onExited: nextAnagramButton.state = "onExited"
0246                 onClicked: screen.nextAnagram();
0247             }
0248 
0249             states: State {
0250                 name: "onEntered"
0251                 PropertyChanges {
0252                     target: nextAnagramButton
0253                     width: blackboard.width / 2.5
0254                 }
0255                 PropertyChanges {
0256                     target: nextAnagramText
0257                     opacity: 1
0258                 }
0259                 AnchorChanges {
0260                     target: nextAnagramIcon
0261                     anchors.horizontalCenter: undefined
0262                     anchors.right: nextAnagramButton.right
0263                 }
0264                 PropertyChanges {
0265                     target: nextAnagramIcon
0266                     anchors.rightMargin: blackboard.width / 50
0267                 }
0268             }
0269             State {
0270                 name: "onExited"
0271             }
0272 
0273             transitions: Transition {
0274                 AnchorAnimation {
0275                     duration: 250
0276                 }
0277                 PropertyAnimation {
0278                     properties: "x,y,width,opacity"
0279                     easing.type: Easing.Linear
0280                     easing.amplitude: 5.0
0281                     easing.period: 1
0282                 }
0283             }
0284         }
0285 
0286         Text {
0287             id: nextAnagramText
0288             anchors {
0289                 verticalCenter: nextAnagramButton.verticalCenter
0290                 horizontalCenter: nextAnagramButton.horizontalCenter
0291             }
0292             color: "white"
0293             text: i18n("Next Anagram")
0294             opacity: 0
0295             font.pixelSize: parent.width / 68.5
0296         }
0297 
0298         Image {
0299             id: nextAnagramIcon
0300             smooth: true
0301             height: nextAnagramButton.height / 2
0302             width: nextAnagramText.width / 4
0303             anchors {
0304                 verticalCenter: nextAnagramButton.verticalCenter
0305                 horizontalCenter: nextAnagramButton.horizontalCenter
0306             }
0307             source: "icons/arrow-light.svgz"
0308             fillMode: Image.PreserveAspectFit
0309         }
0310     } // End of nextAnagram
0311 
0312     Item {
0313         id: playerBox
0314         anchors {
0315             top: blackboard.top
0316             right: blackboard.left
0317             rightMargin: 185
0318             topMargin: blackboard.height / 7
0319         }
0320 
0321         visible: kanagramGame.singlePlayer ? false : true
0322 
0323         Rectangle {
0324             id: playerButton
0325             width: blackboard.width / 3
0326             height: blackboard.height / 7
0327             radius: 8
0328             color: "black"
0329 
0330             MouseArea {
0331                 anchors.fill: parent
0332             }
0333 
0334             Text {
0335                 id: currentPlayerText
0336                 anchors{
0337                     verticalCenter: playerButton.verticalCenter
0338                     horizontalCenter: playerButton.horizontalCenter
0339                  }
0340                 color : "white"
0341                 text: kanagramGame.currentPlayer == 1 ? i18n("1st Player") : i18n("2nd Player")
0342             }
0343         }
0344     }
0345 
0346     Item {
0347         id: configure
0348         anchors {
0349             left: blackboard.right
0350             verticalCenter: blackboard.verticalCenter
0351         }
0352 
0353         Rectangle {
0354             id: configureButton
0355             width: blackboard.width / 9
0356             height: blackboard.height / 7
0357             anchors {
0358                 left: parent.left
0359                 verticalCenter: parent.verticalCenter
0360             }
0361             radius: 8
0362             color: "black"
0363             opacity: 0.5
0364 
0365             MouseArea {
0366                 anchors.fill: parent
0367                 hoverEnabled: true
0368                 onEntered: configureButton.state = "onEntered"
0369                 onExited: configureButton.state = "onExited"
0370                 onClicked: {
0371                     mainwindow.showSettings()
0372                 }
0373             }
0374 
0375             states: State {
0376                 name: "onEntered"
0377                 PropertyChanges {
0378                     target: configureButton
0379                     width: blackboard.width / 2.5
0380                 }
0381                 PropertyChanges {
0382                     target: configureText
0383                     opacity: 1
0384                 }
0385                 AnchorChanges {
0386                     target: configureIcon
0387                     anchors.horizontalCenter: undefined
0388                     anchors.right: configureButton.right
0389                 }
0390                 PropertyChanges {
0391                     target: configureIcon
0392                     anchors.rightMargin: blackboard.width / 50
0393                 }
0394             }
0395             State {
0396                 name: "onExited"
0397             }
0398 
0399             transitions: Transition {
0400                 AnchorAnimation {
0401                     duration: 250
0402                 }
0403                 PropertyAnimation {
0404                     properties: "x,y,width,opacity"
0405                     easing.type: Easing.Linear
0406                     easing.amplitude: 5.0
0407                     easing.period: 1
0408                 }
0409             }
0410         }
0411 
0412         Text {
0413             id: configureText
0414             anchors {
0415                 verticalCenter: configureButton.verticalCenter
0416                 horizontalCenter: configureButton.horizontalCenter
0417             }
0418             color: "white"
0419             text: i18n("Configure")
0420             opacity: 0
0421             font.pixelSize: parent.width / 68.5
0422         }
0423 
0424         Image {
0425             id: configureIcon
0426             smooth: true
0427             height: configureButton.height / 2
0428             width: configureText.width / 3
0429             anchors {
0430                 verticalCenter: configureButton.verticalCenter
0431                 horizontalCenter: configureButton.horizontalCenter
0432             }
0433             source: "icons/spanner-light.svgz"
0434             fillMode: Image.PreserveAspectFit
0435         }
0436     } // End of configure
0437 
0438     Item {
0439         id: help
0440         width: blackboard.width / 9
0441         height: blackboard.height / 7
0442         anchors {
0443             bottom: blackboard.bottom
0444             bottomMargin: blackboard.height / 7
0445             left: blackboard.right
0446         }
0447 
0448         Rectangle {
0449             id: helpButton
0450             width: blackboard.width / 9
0451             height: blackboard.height / 7
0452             radius: 8
0453             color: "black"
0454             opacity: 0.5
0455 
0456             property bool flag: true
0457 
0458             function toggleMoreOptions() {
0459                 if (flag) {
0460                     moreOptionsButton.opacity = 0.5
0461                     moreOptionsButton.width = blackboard.width / 2.5
0462                     kanagramHandbookIcon.visible = true
0463                     aboutKdeIcon.visible = true
0464                     aboutKanagramIcon.visible = true
0465                     flag = false
0466                 } else {
0467                     moreOptionsButton.opacity = 0
0468                     moreOptionsTextBar.opacity = 0
0469                     moreOptionsText.opacity = 0
0470                     kanagramHandbookIcon.visible = false
0471                     aboutKdeIcon.visible = false
0472                     aboutKanagramIcon.visible = false
0473                     flag = true
0474                 }
0475             }
0476 
0477             MouseArea {
0478                 anchors.fill: parent
0479                 hoverEnabled: true
0480                 onEntered: helpButton.state = "onEntered"
0481                 onExited: helpButton.state = "onExited"
0482                 onClicked: helpButton.toggleMoreOptions()
0483             }
0484 
0485             states: State {
0486                 name: "onEntered"
0487                 PropertyChanges {
0488                     target: helpButton
0489                     width: blackboard.width / 2.5
0490                 }
0491                 PropertyChanges {
0492                     target: helpText
0493                     opacity: 1
0494                 }
0495                 AnchorChanges {
0496                     target: helpIcon
0497                     anchors.horizontalCenter: undefined
0498                     anchors.right: helpButton.right
0499                 }
0500                 PropertyChanges {
0501                     target: helpIcon
0502                     anchors.rightMargin: blackboard.width / 50
0503                 }
0504             }
0505             State {
0506                 name: "onExited"
0507             }
0508 
0509             transitions: Transition {
0510                 AnchorAnimation {
0511                     duration: 250
0512                 }
0513                 PropertyAnimation {
0514                     properties: "x,y,width,opacity"
0515                     easing.type: Easing.Linear
0516                     easing.amplitude: 5.0
0517                     easing.period: 1
0518                 }
0519             }
0520         }
0521 
0522         Rectangle {
0523             id: moreOptionsButton
0524             width: blackboard.width / 9
0525             height: blackboard.height / 7
0526             anchors {
0527                 top: helpButton.bottom
0528                 topMargin: blackboard.height / 50
0529                 left: parent.left
0530             }
0531             radius: 8
0532             color: "black"
0533             opacity: 0
0534         }
0535 
0536         Rectangle {
0537             id: moreOptionsTextBar
0538             width: blackboard.width / 4
0539             height: blackboard.height / 16
0540             anchors {
0541                 top: moreOptionsButton.bottom
0542                 horizontalCenter: moreOptionsButton.horizontalCenter
0543             }
0544             radius: 8
0545             color: "black"
0546             opacity: 0
0547         }
0548 
0549         Text {
0550             id: moreOptionsText
0551             anchors {
0552                 verticalCenter: moreOptionsTextBar.verticalCenter
0553                 horizontalCenter: moreOptionsTextBar.horizontalCenter
0554             }
0555             color: "white"
0556             text: i18n("  ")
0557             opacity: 0
0558             font.pixelSize: parent.width / 91
0559         }
0560 
0561         Text {
0562             id: helpText
0563             anchors {
0564                 verticalCenter: helpButton.verticalCenter
0565                 horizontalCenter: helpButton.horizontalCenter
0566             }
0567             color: "white"
0568             text: i18n("About")
0569             opacity: 0
0570             font.pixelSize: screen.width / 68.5
0571         }
0572 
0573         Image {
0574             id: helpIcon
0575             smooth: true
0576             height: blackboard.height / 14
0577             anchors {
0578                 verticalCenter: helpButton.verticalCenter
0579                 horizontalCenter: helpButton.horizontalCenter
0580             }
0581             source: "icons/question-light.svgz"
0582             visible: true
0583             fillMode: Image.PreserveAspectFit
0584         }
0585 
0586         Image {
0587             id: aboutKdeIcon
0588             smooth: true
0589             height: blackboard.height / 14
0590             anchors {
0591                 verticalCenter: moreOptionsButton.verticalCenter
0592                 right: moreOptionsButton.right
0593                 rightMargin: moreOptionsButton.width / 8
0594             }
0595             source: "icons/about-kde.png"
0596             visible: false
0597             fillMode: Image.PreserveAspectFit
0598 
0599             MouseArea {
0600                 anchors.fill: parent
0601                 hoverEnabled: true
0602                 onEntered: aboutKdeIcon.state = "onEntered"
0603                 onExited: aboutKdeIcon.state = "onExited"
0604                 onClicked: mainwindow.showAboutKDE()
0605                 preventStealing: true
0606             }
0607 
0608             states: State {
0609                 name: "onEntered"
0610                 PropertyChanges {
0611                     target: moreOptionsTextBar
0612                     opacity: 0.5
0613                 }
0614                 PropertyChanges {
0615                     target: moreOptionsText
0616                     text: i18n("About KDE")
0617                     opacity: 1
0618                 }
0619             }
0620             State {
0621                 name: "onExited"
0622             }
0623 
0624             transitions: Transition {
0625                 AnchorAnimation {
0626                     duration: 250
0627                 }
0628                 PropertyAnimation {
0629                     properties: "x,y,width,opacity"
0630                     easing.type: Easing.Linear
0631                     easing.amplitude: 5.0
0632                     easing.period: 1
0633                 }
0634             }
0635         }
0636 
0637         Image {
0638             id: aboutKanagramIcon
0639             height: blackboard.height / 14
0640             anchors {
0641                 verticalCenter: moreOptionsButton.verticalCenter
0642                 horizontalCenter: moreOptionsButton.horizontalCenter
0643             }
0644             source: "icons/about-kanagram.png"
0645             smooth: true
0646             visible: false
0647             fillMode: Image.PreserveAspectFit
0648 
0649             MouseArea {
0650                 anchors.fill: parent
0651                 hoverEnabled: true
0652                 onEntered: aboutKanagramIcon.state = "onEntered"
0653                 onExited: aboutKanagramIcon.state = "onExited"
0654                 onClicked: mainwindow.showAboutKanagram()
0655                 preventStealing: true
0656             }
0657 
0658             states: State {
0659                 name: "onEntered"
0660                 PropertyChanges {
0661                     target: moreOptionsTextBar
0662                     opacity: 0.5
0663                 }
0664                 PropertyChanges {
0665                     target: moreOptionsText
0666                     text: i18n("About Kanagram")
0667                     opacity: 1
0668                 }
0669             }
0670             State {
0671                 name: "onExited"
0672             }
0673 
0674             transitions: Transition {
0675                 AnchorAnimation {
0676                     duration: 250
0677                 }
0678                 PropertyAnimation {
0679                     properties: "x,y,width,opacity"
0680                     easing.type: Easing.Linear
0681                     easing.amplitude: 5.0
0682                     easing.period: 1
0683                 }
0684             }
0685         }
0686 
0687         Image {
0688             id: kanagramHandbookIcon
0689             smooth: true
0690             height: blackboard.height / 14
0691             anchors {
0692                 verticalCenter: moreOptionsButton.verticalCenter
0693                 left: moreOptionsButton.left
0694                 leftMargin: moreOptionsButton.width / 8
0695             }
0696             source: "icons/kanagram-handbook.png"
0697             visible: false
0698             fillMode: Image.PreserveAspectFit
0699 
0700             MouseArea {
0701                 anchors.fill: parent
0702                 hoverEnabled: true
0703                 onEntered: kanagramHandbookIcon.state = "onEntered"
0704                 onExited: kanagramHandbookIcon.state = "onExited"
0705                 onClicked: mainwindow.showHandbook()
0706                 preventStealing: true
0707             }
0708 
0709             states: State {
0710                 name: "onEntered"
0711                 PropertyChanges {
0712                     target: moreOptionsTextBar
0713                     opacity: 0.5
0714                 }
0715                 PropertyChanges {
0716                     target: moreOptionsText
0717                     text: i18n("Kanagram Handbook")
0718                     opacity: 1
0719                 }
0720             }
0721             State {
0722                 name: "onExited"
0723             }
0724 
0725             transitions: Transition {
0726                 AnchorAnimation {
0727                     duration: 250
0728                 }
0729                 PropertyAnimation {
0730                     properties: "x,y,width,opacity"
0731                     easing.type: Easing.Linear
0732                     easing.amplitude: 5.0
0733                     easing.period: 1
0734                 }
0735             }
0736         }
0737     } // End help
0738 
0739     Rectangle {
0740         id: inputField
0741         width: blackboard.width
0742         height: parent.height / 10
0743         anchors {
0744             horizontalCenter: blackboard.horizontalCenter
0745             bottom: parent.bottom
0746             bottomMargin: parent.height / 35
0747         }
0748         radius: 8
0749         border.color: "white"
0750         border.width: 0
0751         color: "black"
0752         opacity: 0.35
0753     }
0754 
0755     Grid {
0756         id: answerGrid
0757         anchors {
0758             centerIn: inputField
0759         }
0760         spacing: 5
0761         columns: 13
0762         Repeater {
0763             model: kanagramGame.userAnswer
0764             LetterButton {
0765                 text: modelData
0766                 onClicked: {
0767                     if (countDownTimerValue == 0)
0768                         kanagramGame.moveLetterToAnagram( index )
0769                 }
0770             }
0771         }
0772     }
0773 
0774     Timer {
0775         id: resetTimer
0776         interval: 1000
0777         repeat: true
0778         running: false
0779         triggeredOnStart: false
0780 
0781         onTriggered: {
0782             if (--countDownTimerValue == 0) {
0783                 if (flagCorrectAnswer) {
0784                     kanagramGame.nextAnagram();
0785                     if (kanagramGame.hintHideTime())
0786                         blackboard.showHintTimeInterval = 1
0787                 } else {
0788                     kanagramGame.resetAnagram();
0789                 }
0790                 stop()
0791             }
0792         }
0793     }
0794 
0795     MediaPlayer {
0796         id: chalkSound
0797         source: "sounds/chalk.wav"
0798     }
0799 
0800     MediaPlayer {
0801         id: rightSound
0802         source: "sounds/right.wav"
0803     }
0804 
0805     MediaPlayer {
0806         id: wrongSound
0807         source: "sounds/wrong.wav"
0808     }
0809 
0810     Blackboard {
0811         id: blackboard
0812         border {
0813             width: parent.width / 68.5
0814             color: "#613529"
0815         }
0816         width: parent.width / 2
0817         height: parent.height / 1.5
0818         anchors {
0819             horizontalCenter: parent.horizontalCenter
0820             top: headerArea.bottom
0821         }
0822         onNextAnagram: {
0823             screen.nextAnagram();
0824         }
0825     }
0826 
0827     Keys.onPressed: {
0828         if (countDownTimerValue != 0)
0829             return;
0830 
0831         if (event.text.length > 0) {
0832             kanagramGame.moveLetter(event.text);
0833             checkSolved();
0834         }
0835         if (event.key == Qt.Key_Backspace)
0836         {
0837             var length = kanagramGame.userAnswer.length;
0838             if (length > 0)
0839                 kanagramGame.moveLetterToAnagram(length - 1);
0840         }
0841     }
0842 }