Debian Codegraph @

src/activities/analog_electricity/components/Battery.qml

raw
1 /* GCompris - Battery.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: battery
17 terminalSize: 0.2
18 noOfConnectionPoints: 2
19 information: qsTr("Battery is used for powering up electrical devices. It can supply voltage in a closed circuit. Which means there should be a path for the current to flow from one terminal of the battery to the other.") + " " + qsTr("If the current in a circuit is too high then the battery can be damaged.")
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(current)
24 source: Activity.url + "battery.svg"
25
26 property double componentVoltage: 0
27 property var nodeVoltages: [0, 0]
28 property double current: 0
29 property alias connectionPoints: connectionPoints
30 property var connectionPointPosY: [0.1, 0.9]
31 property var connectionPointType: ["positive", "negative"]
32 property string componentName: "Voltage"
33 property var externalNetlistIndex: [0, 0]
34 property var netlistModel:
35 [
36 "v",
37 [
38 ],
39 {
40 "name": componentName,
41 "value": "dc(10)",
42 "_json_": 0
43 },
44 [
45 0,
46 0
47 ]
48 ]
49
50 Repeater {
51 id: connectionPoints
52 model: 2
53 delegate: connectionPoint
54 Component {
55 id: connectionPoint
56 TerminalPoint {
57 posX: 0.5
58 posY: connectionPointPosY[index]
59 terminalType: connectionPointType[index]
60 }
61 }
62 }
63
64 function checkConnections() {
65 terminalConnected = 0;
66 for(var i = 0; i < noOfConnectionPoints; i++) {
67 if(connectionPoints.itemAt(i).wires.length > 0)
68 terminalConnected += 1;
69 }
70
71 // show label only from level 6 or in free mode
72 if(terminalConnected >= 2 && (!Activity.items.isTutorialMode || Activity.items.currentLevel >= 5)) {
73 battery.showLabel = true;
74 } else {
75 battery.showLabel = false;
76 }
77 }
78
79 function updateValues() {
80 componentVoltage = (Math.abs(nodeVoltages[1] - nodeVoltages[0])).toFixed(2);
81 current = (Math.abs(current)).toFixed(3);
82 // short circuit case
83 if(Math.abs(current) > 1) {
84 battery.source = Activity.url + "battery_dead.svg";
85 } else {
86 battery.source = Activity.url + "battery.svg";
87 }
88 }
89
90 function initConnections() {
91 var connectionIndex = Activity.connectionCount;
92 battery.externalNetlistIndex[0] = ++connectionIndex;
93 connectionPoints.itemAt(0).updateNetlistIndex(connectionIndex);
94 battery.externalNetlistIndex[1] = ++connectionIndex;
95 connectionPoints.itemAt(1).updateNetlistIndex(connectionIndex);
96 Activity.connectionCount = connectionIndex;
97 }
98
99 function addToNetlist() {
100 var netlistItem = battery.netlistModel;
101 Activity.netlistComponents.push(battery);
102 Activity.vSourcesList.push(battery);
103 netlistItem[2].name = componentName;
104 netlistItem[2]._json = Activity.netlist.length;
105 netlistItem[3][0] = battery.externalNetlistIndex[0];
106 netlistItem[3][1] = battery.externalNetlistIndex[1];
107 Activity.netlist.push(netlistItem);
108 }
109
110 function checkComponentAnswer(): string {
111 if(terminalConnected >= 2)
112 return "batteryIn";
113 else
114 return "";
115 }
116 }