Warning, /education/gcompris/src/activities/analog_electricity/components/Switch1.qml is written in an unsupported language. File is not indexed.

0001 /* GCompris - Switch1.qml
0002  *
0003  * SPDX-FileCopyrightText: 2020 Aiswarya Kaitheri Kandoth <aiswaryakk29@gmail.com>
0004  *
0005  * Authors:
0006  *   Bruno Coudoin <bruno.coudoin@gcompris.net> (GTK+ version)
0007  *   Aiswarya Kaitheri Kandoth <aiswaryakk29@gmail.com> (Qt Quick port)
0008  *
0009  *   SPDX-License-Identifier: GPL-3.0-or-later
0010  */
0011 import QtQuick 2.12
0012 import GCompris 1.0
0013 import "../analog_electricity.js" as Activity
0014 
0015 ElectricalComponent {
0016     id: switch1 // A resistor
0017     terminalSize: 0.2
0018     noOfConnectionPoints: 2
0019     information: qsTr("Switch can connect or disconnect the conducting path in an electrical circuit.")
0020     source: Activity.url + "switch_off.svg"
0021 
0022     property double componentVoltage: 0
0023     property double current: 0
0024     property bool switchOn: false
0025     property alias connectionPoints: connectionPoints
0026     property var connectionPointPosX: [0.1, 0.9]
0027     property string componentName: "Switch1"
0028     property var externalNetlistIndex: [0, 0]
0029     property var netlistModel:
0030     [
0031         "r",
0032         [
0033         ],
0034         {
0035             "name": componentName,
0036             "r": "0",
0037             "_json_": 0
0038         },
0039         [
0040             0,
0041             0
0042         ]
0043     ]
0044 
0045     Repeater {
0046         id: connectionPoints
0047         model: 2
0048         delegate: connectionPoint
0049         Component {
0050             id: connectionPoint
0051             TerminalPoint {
0052                 posX: connectionPointPosX[index]
0053                 posY: 0.75
0054             }
0055         }
0056     }
0057 
0058     MouseArea {
0059         id: clickArea
0060         height: parent.height * 0.5
0061         width: parent.width * 0.25
0062         anchors.horizontalCenter: parent.horizontalCenter
0063         anchors.top: parent.top
0064         onClicked: {
0065             if(switch1.source == Activity.url + "switch_off.svg") {
0066                 switch1.source = Activity.url + "switch_on.svg";
0067                 switchOn = true;
0068             } else {
0069                 switch1.source = Activity.url + "switch_off.svg";
0070                 switchOn = false;
0071             }
0072             Activity.restartTimer();
0073         }
0074     }
0075 
0076     function checkConnections() {
0077         terminalConnected = 0;
0078         for(var i = 0; i < noOfConnectionPoints; i++) {
0079             if(connectionPoints.itemAt(i).wires.length > 0)
0080                 terminalConnected += 1;
0081         }
0082     }
0083 
0084     function updateValues() {
0085         return;
0086     }
0087 
0088     function initConnections() {
0089         var connectionIndex = Activity.connectionCount;
0090         switch1.externalNetlistIndex[0] = ++connectionIndex;
0091         connectionPoints.itemAt(0).updateNetlistIndex(connectionIndex);
0092         switch1.externalNetlistIndex[1] = ++connectionIndex;
0093         connectionPoints.itemAt(1).updateNetlistIndex(connectionIndex);
0094         Activity.connectionCount = connectionIndex;
0095     }
0096 
0097     function addToNetlist() {
0098         if(switchOn) {
0099             var netlistItem = switch1.netlistModel;
0100             Activity.netlistComponents.push(switch1);
0101             Activity.vSourcesList.push(switch1);
0102             netlistItem[2].name = componentName;
0103             netlistItem[2]._json = Activity.netlist.length;
0104             netlistItem[3][0] = switch1.externalNetlistIndex[0];
0105             netlistItem[3][1] = switch1.externalNetlistIndex[1];
0106             Activity.netlist.push(netlistItem);
0107         }
0108     }
0109 
0110     function checkComponentAnswer() {
0111         if(switch1.source == Activity.url + "switch_off.svg" && terminalConnected >= 2) {
0112             switch1.source = Activity.url + "switch_on.svg";
0113             switchOn = true;
0114             Activity.createNetlist();
0115 
0116             if(current != 0)
0117                 return "switch1In";
0118         } else if(switch1.source == Activity.url + "switch_on.svg" && terminalConnected >= 2 && current != 0) {
0119             return "switch1In";
0120         } else
0121             return "";
0122     }
0123 }
0124