Debian Codegraph @

src/activities/analog_electricity/components/Resistor.qml

raw
1 /* GCompris - Resistor.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: resistor // Ammeters to measure current and voltage
17 terminalSize: 0.2
18 noOfConnectionPoints: 2
19 information: qsTr("Resistors are used to reduce the current flow in an electrical circuit.")
20 //: 1st V for Voltage, 2nd V for Volt
21 labelText1: qsTr("V = %1V").arg(componentVoltage)
22 //: I for current intensity, A for Ampere
23 labelText2: qsTr("I = %1A").arg(resistorCurrent)
24 source: Activity.url + "resistor.svg"
25
26 property var nodeVoltages: [0, 0]
27 property double componentVoltage: 0
28 property double resistorCurrent: 0
29 property alias connectionPoints: connectionPoints
30 property var connectionPointPosX: [0.1, 0.9]
31 property var connectionPointPosY: [0.5, 0.5]
32 property string componentName: "Resistor"
33 property var internalNetlistIndex: [0, 0]
34 property var externalNetlistIndex: [0, 0]
35 property var netlistModel:
36 [
37 "r",
38 [
39 ],
40 {
41 "name": componentName,
42 "r": "1000",
43 "_json_": 0
44 },
45 [
46 0,
47 0
48 ]
49 ]
50
51 Item {
52 id: aMeter1
53 property int jsonNumber: 0
54 property double current: 0
55 property var netlistModel:
56 [
57 "a",
58 [
59 ],
60 {
61 "name": "aMeter1-",
62 "color": "magenta",
63 "offset": "0",
64 "_json_": aMeter1.jsonNumber
65 },
66 [
67 0,
68 0
69 ]
70 ]
71 }
72
73 Item {
74 id: aMeter2
75 property int jsonNumber: 0
76 property double current: 0
77 property var netlistModel:
78 [
79 "a",
80 [
81 ],
82 {
83 "name": "aMeter2-",
84 "color": "magenta",
85 "offset": "0",
86 "_json_": aMeter2.jsonNumber
87 },
88 [
89 0,
90 0
91 ]
92 ]
93 }
94
95 Repeater {
96 id: connectionPoints
97 model: 2
98 delegate: connectionPoint
99 Component {
100 id: connectionPoint
101 TerminalPoint {
102 posX: connectionPointPosX[index]
103 posY: connectionPointPosY[index]
104 }
105 }
106 }
107
108 function checkConnections() {
109 terminalConnected = 0;
110 for(var i = 0; i < noOfConnectionPoints; i++) {
111 if(connectionPoints.itemAt(i).wires.length > 0)
112 terminalConnected += 1;
113 }
114 if(terminalConnected >= 2) {
115 resistor.showLabel = true;
116 } else {
117 resistor.showLabel = false;
118 }
119 }
120
121 function updateValues() {
122 resistorCurrent = (Math.abs(aMeter1.current)).toFixed(3);
123 componentVoltage = (Math.abs(nodeVoltages[1] - nodeVoltages[0])).toFixed(2);
124 }
125
126 function initConnections() {
127 var connectionIndex = Activity.connectionCount;
128 resistor.externalNetlistIndex[0] = ++connectionIndex;
129 connectionPoints.itemAt(0).updateNetlistIndex(connectionIndex);
130 resistor.internalNetlistIndex[0] = ++connectionIndex;
131 resistor.internalNetlistIndex[1] = ++connectionIndex;
132 resistor.externalNetlistIndex[1] = ++connectionIndex;
133 connectionPoints.itemAt(1).updateNetlistIndex(connectionIndex);
134 Activity.connectionCount = connectionIndex;
135 }
136
137 function addToNetlist() {
138 var netlistItem = aMeter1.netlistModel;
139 Activity.netlistComponents.push(aMeter1);
140 Activity.vSourcesList.push(aMeter1);
141 netlistItem[2].name = "aMeter1-" + componentName;
142 netlistItem[2]._json = Activity.netlist.length;
143 netlistItem[3][0] = resistor.externalNetlistIndex[0];
144 netlistItem[3][1] = resistor.internalNetlistIndex[0];
145 Activity.netlist.push(netlistItem);
146
147 netlistItem = resistor.netlistModel;
148 Activity.netlistComponents.push(resistor);
149 netlistItem[2].name = componentName;
150 netlistItem[2]._json = Activity.netlist.length;
151 netlistItem[3][0] = resistor.internalNetlistIndex[0];
152 netlistItem[3][1] = resistor.internalNetlistIndex[1];
153 Activity.netlist.push(netlistItem);
154
155 netlistItem = aMeter2.netlistModel;
156 Activity.netlistComponents.push(aMeter2);
157 Activity.vSourcesList.push(aMeter2);
158 netlistItem[2].name = "aMeter2-" + componentName;
159 netlistItem[2]._json = Activity.netlist.length;
160 netlistItem[3][0] = resistor.internalNetlistIndex[1];
161 netlistItem[3][1] = resistor.externalNetlistIndex[1];
162 Activity.netlist.push(netlistItem);
163 }
164
165 function checkComponentAnswer(): string {
166 if(resistorCurrent > 0 && terminalConnected >= 2)
167 return "resistorIn";
168 else
169 return "";
170 }
171 }
172