Debian Codegraph @

src/activities/analog_electricity/components/Switch1.qml

raw
1 /* GCompris - Switch1.qml
2 *
3 * SPDX-FileCopyrightText: 2020 Aiswarya Kaitheri Kandoth <[email protected]>
4 *
5 * Authors:
6 * Bruno Coudoin <[email protected]> (GTK+ version)
7 * Aiswarya Kaitheri Kandoth <[email protected]> (Qt Quick port)
8 *
9 * SPDX-License-Identifier: GPL-3.0-or-later
10 */
11 import QtQuick
12 import core 1.0
13 import "../analog_electricity.js" as Activity
14
15 ElectricalComponent {
16 id: switch1 // A resistor
17 terminalSize: 0.2
18 noOfConnectionPoints: 2
19 information: qsTr("Switch can connect or disconnect the conducting path in an electrical circuit.")
20 source: Activity.url + "switch_off.svg"
21
22 property double componentVoltage: 0
23 property double current: 0
24 property bool switchOn: false
25 property alias connectionPoints: connectionPoints
26 property var connectionPointPosX: [0.1, 0.9]
27 property string componentName: "Switch1"
28 property var externalNetlistIndex: [0, 0]
29 property var netlistModel:
30 [
31 "r",
32 [
33 ],
34 {
35 "name": componentName,
36 "r": "0",
37 "_json_": 0
38 },
39 [
40 0,
41 0
42 ]
43 ]
44
45 Repeater {
46 id: connectionPoints
47 model: 2
48 delegate: connectionPoint
49 Component {
50 id: connectionPoint
51 TerminalPoint {
52 posX: connectionPointPosX[index]
53 posY: 0.75
54 }
55 }
56 }
57
58 MouseArea {
59 id: clickArea
60 height: parent.height * 0.5
61 width: parent.width * 0.25
62 anchors.horizontalCenter: parent.horizontalCenter
63 anchors.top: parent.top
64 onClicked: {
65 if(switch1.source == Activity.url + "switch_off.svg") {
66 switch1.source = Activity.url + "switch_on.svg";
67 switchOn = true;
68 } else {
69 switch1.source = Activity.url + "switch_off.svg";
70 switchOn = false;
71 }
72 Activity.restartTimer();
73 }
74 }
75
76 function checkConnections() {
77 terminalConnected = 0;
78 for(var i = 0; i < noOfConnectionPoints; i++) {
79 if(connectionPoints.itemAt(i).wires.length > 0)
80 terminalConnected += 1;
81 }
82 }
83
84 function updateValues() {
85 return;
86 }
87
88 function initConnections() {
89 var connectionIndex = Activity.connectionCount;
90 switch1.externalNetlistIndex[0] = ++connectionIndex;
91 connectionPoints.itemAt(0).updateNetlistIndex(connectionIndex);
92 switch1.externalNetlistIndex[1] = ++connectionIndex;
93 connectionPoints.itemAt(1).updateNetlistIndex(connectionIndex);
94 Activity.connectionCount = connectionIndex;
95 }
96
97 function addToNetlist() {
98 if(switchOn) {
99 var netlistItem = switch1.netlistModel;
100 Activity.netlistComponents.push(switch1);
101 Activity.vSourcesList.push(switch1);
102 netlistItem[2].name = componentName;
103 netlistItem[2]._json = Activity.netlist.length;
104 netlistItem[3][0] = switch1.externalNetlistIndex[0];
105 netlistItem[3][1] = switch1.externalNetlistIndex[1];
106 Activity.netlist.push(netlistItem);
107 }
108 }
109
110 function checkComponentAnswer(): string {
111 if(switch1.source == Activity.url + "switch_off.svg" && terminalConnected >= 2) {
112 switch1.source = Activity.url + "switch_on.svg";
113 switchOn = true;
114 Activity.createNetlist();
115
116 if(current != 0)
117 return "switch1In";
118 } else if(switch1.source == Activity.url + "switch_on.svg" && terminalConnected >= 2 && current != 0) {
119 return "switch1In";
120 } else
121 return "";
122 }
123 }
124