Debian Codegraph @

src/activities/analog_electricity/components/Bulb.qml

raw
1 /* GCompris - Bulb.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: bulb // Ammeter on both sides of a resistor
17 terminalSize: 0.2
18 noOfConnectionPoints: 2
19 information: qsTr("Bulb glows when it has enough power. Its intensity is proportional to the supplied voltage. It will be broken if there is a power greater than a certain limit.")
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(bulbCurrent)
24 source: Activity.url + "bulb.svg"
25
26 property var nodeVoltages: [0, 0]
27 property double componentVoltage: 0
28 property double power: 0
29 property double maxPower: 0.11
30 property double bulbCurrent: 0
31 property alias connectionPoints: connectionPoints
32 property alias lightBulb: lightBulb
33 property bool isBroken: false
34 property var connectionPointPosX: [0.1, 0.9]
35 property string componentName: "Bulb"
36 property var internalNetlistIndex: [0, 0]
37 property var externalNetlistIndex: [0, 0]
38 property var netlistModel:
39 [
40 "r",
41 [
42 ],
43 {
44 "name": componentName,
45 "r": "1000",
46 "_json_": 0
47 },
48 [
49 0,
50 0
51 ]
52 ]
53
54 Item {
55 id: aMeter1 // Ammeters are to measure current and voltage
56 property int jsonNumber: 0
57 property double current: 0
58 property var netlistModel:
59 [
60 "a",
61 [
62 ],
63 {
64 "name": "aMeter1-",
65 "color": "magenta",
66 "offset": "0",
67 "_json_": aMeter1.jsonNumber
68 },
69 [
70 0,
71 0
72 ]
73 ]
74 }
75
76 Item {
77 id: aMeter2
78 property int jsonNumber: 0
79 property double current: 0
80 property var netlistModel:
81 [
82 "a",
83 [
84 ],
85 {
86 "name": "aMeter2-",
87 "color": "magenta",
88 "offset": "0",
89 "_json_": aMeter2.jsonNumber
90 },
91 [
92 0,
93 0
94 ]
95 ]
96 }
97
98 Repeater {
99 id: connectionPoints
100 model: 2
101 delegate: connectionPoint
102 Component {
103 id: connectionPoint
104 TerminalPoint {
105 posX: connectionPointPosX[index]
106 posY: 0.9
107 }
108 }
109 }
110
111 Image {
112 id: lightFilament
113 source: Activity.url + "bulb_light.svg"
114 anchors.fill: parent
115 sourceSize.width: width
116 sourceSize.height: height
117 fillMode: Image.PreserveAspectFit
118 opacity: lightBulb.opacity > 0 ? 1 : 0
119 }
120
121 Image {
122 id: lightBulb
123 source: Activity.url + "bulb_max.svg"
124 anchors.fill: parent
125 sourceSize.width: width
126 sourceSize.height: height
127 fillMode: Image.PreserveAspectFit
128 opacity: power < maxPower ? power * 10 : 0
129 }
130
131 function repairComponent() {
132 bulb.source = Activity.url + "bulb.svg";
133 isBroken = false;
134 }
135
136 function checkConnections() {
137 terminalConnected = 0;
138 for(var i = 0; i < noOfConnectionPoints; i++) {
139 if(connectionPoints.itemAt(i).wires.length > 0)
140 terminalConnected += 1;
141 }
142
143 // show label only from level 6 or in free mode, and when it's connected and not broken
144 if((terminalConnected >=2 && !isBroken && !Activity.items.isTutorialMode) ||
145 (terminalConnected >= 2 && !isBroken && Activity.items.isTutorialMode &&
146 Activity.items.currentLevel >= 5)) {
147 bulb.showLabel = true;
148 } else {
149 bulb.showLabel = false;
150 }
151 }
152
153 function updateValues() {
154 bulbCurrent = (Math.abs(aMeter1.current)).toFixed(3);
155 componentVoltage = (Math.abs(nodeVoltages[1] - nodeVoltages[0])).toFixed(2);
156 power = componentVoltage * bulbCurrent;
157 if(power < maxPower)
158 lightBulb.opacity = power * 10;
159 else {
160 lightBulb.opacity = 0;
161 // if the bulb is blown, set its current to 0, hide its label and don't push it to the netlist
162 bulb.showLabel = false;
163 bulbCurrent = 0;
164 bulb.source = Activity.url + "bulb_blown.svg";
165 isBroken = true;
166 Activity.restartTimer();
167 }
168 }
169
170 function initConnections() {
171 var connectionIndex = Activity.connectionCount;
172 bulb.externalNetlistIndex[0] = ++connectionIndex;
173 connectionPoints.itemAt(0).updateNetlistIndex(connectionIndex);
174 bulb.internalNetlistIndex[0] = ++connectionIndex;
175 bulb.internalNetlistIndex[1] = ++connectionIndex;
176 bulb.externalNetlistIndex[1] = ++connectionIndex;
177 connectionPoints.itemAt(1).updateNetlistIndex(connectionIndex);
178 Activity.connectionCount = connectionIndex;
179 }
180
181 function addToNetlist() {
182 if(!isBroken) {
183 var netlistItem = aMeter1.netlistModel;
184 Activity.netlistComponents.push(aMeter1);
185 Activity.vSourcesList.push(aMeter1);
186 netlistItem[2].name = "aMeter1-" + componentName;
187 netlistItem[2]._json = Activity.netlist.length;
188 netlistItem[3][0] = bulb.externalNetlistIndex[0];
189 netlistItem[3][1] = bulb.internalNetlistIndex[0];
190 Activity.netlist.push(netlistItem);
191
192 netlistItem = bulb.netlistModel;
193 Activity.netlistComponents.push(bulb);
194 netlistItem[2].name = componentName;
195 netlistItem[2]._json = Activity.netlist.length;
196 netlistItem[3][0] = bulb.internalNetlistIndex[0];
197 netlistItem[3][1] = bulb.internalNetlistIndex[1];
198 Activity.netlist.push(netlistItem);
199
200 netlistItem = aMeter2.netlistModel;
201 Activity.netlistComponents.push(aMeter2);
202 Activity.vSourcesList.push(aMeter2);
203 netlistItem[2].name = "aMeter2-" + componentName;
204 netlistItem[2]._json = Activity.netlist.length;
205 netlistItem[3][0] = bulb.internalNetlistIndex[1];
206 netlistItem[3][1] = bulb.externalNetlistIndex[1];
207 Activity.netlist.push(netlistItem);
208 }
209 }
210
211 function checkComponentAnswer(): string {
212 // special case for level 8
213 if(Activity.items.currentLevel === 7 && componentVoltage > 0 && !isBroken)
214 return "bulbIn";
215
216 if(componentVoltage === 10) {
217 return "bulbGlows";
218 } else if(terminalConnected >= 2 && componentVoltage < 10 && bulbCurrent > 0) {
219 return "bulbGlowsLess"
220 } else if(terminalConnected >= 2 && isBroken) {
221 return "bulbBroken";
222 } else
223 return "";
224 }
225 }
226