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

0001 /* GCompris - BrailleChar.qml
0002  *
0003  * SPDX-FileCopyrightText: 2014 Arkit Vora <arkitvora123@gmail.com>
0004  *
0005  * Authors:
0006  *   Srishti Sethi <srishakatux@gmail.com> (GTK+ version)
0007  *   Arkit Vora <arkitvora123@gmail.com> (Qt Quick port)
0008  *
0009  *   SPDX-License-Identifier: GPL-3.0-or-later
0010  */
0011 import QtQuick 2.12
0012 import "braille_alphabets.js" as Activity
0013 import "../../core"
0014 import GCompris 1.0
0015 
0016 Item {
0017     id: brailleCharItem
0018     height: dotWidth * 3 + grid.spacing * 4
0019 
0020     property string brailleChar: ""
0021     property real dotWidth: width * 0.4
0022     property real dotHeight: dotWidth
0023     property alias circles: circles
0024     property bool clickable
0025     property double thinBorder: 2 * ApplicationInfo.ratio
0026     property double thickBorder: 4 * ApplicationInfo.ratio
0027     property bool isLetter: brailleChar >= 'A' && brailleChar <= 'Z'
0028     property var brailleCodesLetter: {
0029         // For ASCII each letter, this represent the active dots in Braille.
0030         "A": [1], "B": [1, 2], "C": [1, 4], "D": [1, 4, 5], "E": [1, 5],
0031         "F": [1, 2, 4], "G": [1, 2, 4, 5], "H": [1, 2, 5], "I": [2, 4],
0032         "J": [2, 4, 5], "K": [1, 3], "L": [1, 2, 3], "M": [1, 3, 4],
0033         "N": [1, 3, 4, 5], "O": [1, 3, 5], "P": [1, 2, 3, 4], "Q": [1, 2, 3, 4, 5],
0034         "R": [1, 2, 3, 5], "S": [2, 3, 4], "T": [2, 3, 4, 5], "U": [1, 3, 6],
0035         "V": [1, 2, 3, 6], "W": [2, 4, 5, 6], "X": [1, 3, 4, 6], "Y": [1, 3, 4, 5, 6],
0036         "Z": [1, 3, 5, 6]
0037     }
0038     property var brailleCodesNumber: {
0039         // For ASCII each letter, this represent the active dots in Braille.
0040         "+": [3, 4, 6], "-": [3, 6], "*": [1, 6], "/": [3, 4],
0041         "#": [3, 4, 5, 6], "1": [1], "2" :[1, 2], "3": [1, 4], "4": [1, 4, 5],
0042         "5": [1, 5], "6": [1, 2, 4], "7": [1, 2, 4, 5], "8": [1, 2, 5],
0043         "9": [2, 4], "0" :[2, 4, 5]
0044     }
0045     property var brailleCodes: isLetter ? brailleCodesLetter : brailleCodesNumber
0046 
0047     function updateDotsFromBrailleChar() {
0048         var dots = []
0049         for(var car in brailleCodes) {
0050             if(car === brailleChar) {
0051                 dots = brailleCodes[car]
0052             }
0053         }
0054 
0055         // Clear all the dots
0056         for(var i = 0; i < 6; i++) {
0057             circles.itemAt(i).state = "off"
0058         }
0059 
0060         for(var i in dots) {
0061             circles.itemAt(i).state = "on"
0062         }
0063     }
0064 
0065     function updateBrailleCharFromDots() {
0066         var dots = []
0067         for( var i = 0; i < 6; i++) {
0068             if(circles.itemAt(i).state === "on")
0069                 dots.push(i + 1)
0070         }
0071 
0072         var stringifiedDots = JSON.stringify(dots)
0073         for(var car in brailleCodes) {
0074             if(JSON.stringify(brailleCodes[car]) === stringifiedDots) {
0075                 brailleChar = car
0076                 return
0077             }
0078         }
0079         brailleChar = ""
0080     }
0081 
0082     function clearLetter() {
0083         brailleChar = ""
0084         updateDotsFromBrailleChar()
0085     }
0086 
0087     function switchState(value) {
0088         circles.itemAt(value-1).switchState()
0089     }
0090 
0091     Grid {
0092         id: grid
0093         anchors.centerIn: brailleCharItem
0094         spacing: (brailleCharItem.width - brailleCharItem.dotWidth * 2) / 2
0095         columns: 2
0096         rows: 3
0097         flow: Grid.TopToBottom
0098 
0099         Repeater {
0100 
0101             id: circles
0102             model: 6
0103 
0104             Rectangle {
0105                 id: incircle1
0106                 border.width: brailleCharItem.thinBorder
0107                 color: on ? "#373737" : "#f0f0f0"
0108                 border.color: "#373737"
0109                 width: dotWidth
0110                 height: dotHeight
0111                 radius: width * 0.5
0112 
0113                 property bool on: clickable ? false : click_on_off()
0114 
0115                 function click_on_off() {
0116                     var code = brailleCodes[brailleChar]
0117                     if(!code)
0118                         return false
0119                     for(var i = 0; i < code.length; i++) {
0120                         if(code[i] === index + 1) {
0121                             return true
0122                         }
0123                     }
0124                     return false
0125                 }
0126 
0127                 function switchState() {
0128                     if (state == "on") {
0129                         state = "off"
0130                     } else {
0131                         state = "on"
0132                     }
0133                     activity.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/scroll.wav")
0134                     // On touch screens we don't get the exit event.
0135                     border.width = 2 * ApplicationInfo.ratio
0136                     brailleCharItem.updateBrailleCharFromDots()
0137 
0138                 }
0139 
0140                 Behavior on color {
0141                     ColorAnimation {
0142                         duration: 200
0143                     }
0144                 }
0145 
0146                 GCText {
0147                     id: numtext
0148                     text: clickable ? modelData+1 : ""
0149                     color: "#2a2a2a"
0150                     anchors.left: index >= 3 ? incircle1.right : undefined
0151                     anchors.right: index < 3 ? incircle1.left : undefined
0152                     anchors.verticalCenter: incircle1.verticalCenter
0153                     font.weight: Font.DemiBold
0154                     font.pointSize: NaN // need to clear font.pointSize explicitly
0155                     font.pixelSize: Math.min(30 * ApplicationInfo.ratio,
0156                                              Math.max(parent.height, 20))
0157                     anchors.margins: 10
0158                 }
0159 
0160                 MouseArea {
0161                     id : mouse1
0162                     enabled: clickable && !items.buttonsBlocked ? true : false
0163                     anchors.fill: parent
0164                     hoverEnabled: true
0165                     onEntered: incircle1.border.width = brailleCharItem.thickBorder
0166                     onExited: incircle1.border.width = brailleCharItem.thinBorder
0167                     onClicked: {
0168                         incircle1.switchState();
0169                     }
0170                 }
0171 
0172                 states: [
0173                     State {
0174                         name: "on"
0175 
0176                         PropertyChanges {
0177                             target: incircle1
0178                             on: true
0179                         }
0180 
0181                     },
0182                     State {
0183                         name: "off"
0184 
0185                         PropertyChanges {
0186                             target: incircle1
0187                             on: false
0188                         }
0189                     }
0190                 ]
0191             }
0192         }
0193     }
0194 }