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

0001 /* GCompris - Bulb.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: bulb // Ammeter on both sides of a resistor
0017     terminalSize: 0.2
0018     noOfConnectionPoints: 2
0019     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.")
0020     //: 1st V for Voltage, 2nd V for Volt
0021     labelText1: qsTr("V = %1V").arg(componentVoltage)
0022     //: I for current intensity, A for Ampere
0023     labelText2: qsTr("I = %1A").arg(bulbCurrent)
0024     source: Activity.url + "bulb.svg"
0025 
0026     property var nodeVoltages: [0, 0]
0027     property double componentVoltage: 0
0028     property double power: 0
0029     property double maxPower: 0.11
0030     property double bulbCurrent: 0
0031     property alias connectionPoints: connectionPoints
0032     property alias lightBulb: lightBulb
0033     property bool isBroken: false
0034     property var connectionPointPosX: [0.1, 0.9]
0035     property string componentName: "Bulb"
0036     property var internalNetlistIndex: [0, 0]
0037     property var externalNetlistIndex: [0, 0]
0038     property var netlistModel:
0039     [
0040         "r",
0041         [
0042         ],
0043         {
0044             "name": componentName,
0045             "r": "1000",
0046             "_json_": 0
0047         },
0048         [
0049             0,
0050             0
0051         ]
0052     ]
0053 
0054     Item {
0055         id: aMeter1 // Ammeters are to measure current and voltage
0056         property int jsonNumber: 0
0057         property double current: 0
0058         property var netlistModel:
0059         [
0060             "a",
0061             [
0062             ],
0063             {
0064                 "name": "aMeter1-",
0065                 "color": "magenta",
0066                 "offset": "0",
0067                 "_json_": aMeter1.jsonNumber
0068             },
0069             [
0070                 0,
0071                 0
0072             ]
0073         ]
0074     }
0075 
0076     Item {
0077         id: aMeter2
0078         property int jsonNumber: 0
0079         property double current: 0
0080         property var netlistModel:
0081         [
0082             "a",
0083             [
0084             ],
0085             {
0086                 "name": "aMeter2-",
0087                 "color": "magenta",
0088                 "offset": "0",
0089                 "_json_": aMeter2.jsonNumber
0090             },
0091             [
0092                 0,
0093                 0
0094             ]
0095         ]
0096     }
0097 
0098     Repeater {
0099         id: connectionPoints
0100         model: 2
0101         delegate: connectionPoint
0102         Component {
0103             id: connectionPoint
0104             TerminalPoint {
0105                 posX: connectionPointPosX[index]
0106                 posY: 0.9
0107             }
0108         }
0109     }
0110 
0111     Image {
0112         id: lightFilament
0113         source: Activity.url + "bulb_light.svg"
0114         anchors.fill: parent
0115         sourceSize.width: width
0116         sourceSize.height: height
0117         fillMode: Image.PreserveAspectFit
0118         opacity: lightBulb.opacity > 0 ? 1 : 0
0119     }
0120 
0121     Image {
0122         id: lightBulb
0123         source: Activity.url + "bulb_max.svg"
0124         anchors.fill: parent
0125         sourceSize.width: width
0126         sourceSize.height: height
0127         fillMode: Image.PreserveAspectFit
0128         opacity: power < maxPower ? power * 10 : 0
0129     }
0130 
0131     function repairComponent() {
0132         bulb.source = Activity.url + "bulb.svg";
0133         isBroken = false;
0134     }
0135 
0136     function checkConnections() {
0137         terminalConnected = 0;
0138         for(var i = 0; i < noOfConnectionPoints; i++) {
0139             if(connectionPoints.itemAt(i).wires.length > 0)
0140                 terminalConnected += 1;
0141         }
0142 
0143         // show label only from level 6 or in free mode, and when it's connected and not broken
0144         if((terminalConnected >=2 && !isBroken && !Activity.items.isTutorialMode) ||
0145             (terminalConnected >= 2 && !isBroken && Activity.items.isTutorialMode &&
0146             Activity.items.currentLevel >= 5)) {
0147             bulb.showLabel = true;
0148         } else {
0149             bulb.showLabel = false;
0150         }
0151     }
0152 
0153     function updateValues() {
0154         bulbCurrent = (Math.abs(aMeter1.current)).toFixed(3);
0155         componentVoltage = (Math.abs(nodeVoltages[1] - nodeVoltages[0])).toFixed(2);
0156         power = componentVoltage * bulbCurrent;
0157         if(power < maxPower)
0158             lightBulb.opacity  = power * 10;
0159         else {
0160             lightBulb.opacity = 0;
0161             // if the bulb is blown, set its current to 0, hide its label and don't push it to the netlist
0162             bulb.showLabel = false;
0163             bulbCurrent = 0;
0164             bulb.source = Activity.url + "bulb_blown.svg";
0165             isBroken = true;
0166             Activity.restartTimer();
0167         }
0168     }
0169 
0170     function initConnections() {
0171         var connectionIndex = Activity.connectionCount;
0172         bulb.externalNetlistIndex[0] = ++connectionIndex;
0173         connectionPoints.itemAt(0).updateNetlistIndex(connectionIndex);
0174         bulb.internalNetlistIndex[0] = ++connectionIndex;
0175         bulb.internalNetlistIndex[1] = ++connectionIndex;
0176         bulb.externalNetlistIndex[1] = ++connectionIndex;
0177         connectionPoints.itemAt(1).updateNetlistIndex(connectionIndex);
0178         Activity.connectionCount = connectionIndex;
0179     }
0180 
0181     function addToNetlist() {
0182         if(!isBroken) {
0183             var netlistItem = aMeter1.netlistModel;
0184             Activity.netlistComponents.push(aMeter1);
0185             Activity.vSourcesList.push(aMeter1);
0186             netlistItem[2].name = "aMeter1-" + componentName;
0187             netlistItem[2]._json = Activity.netlist.length;
0188             netlistItem[3][0] = bulb.externalNetlistIndex[0];
0189             netlistItem[3][1] = bulb.internalNetlistIndex[0];
0190             Activity.netlist.push(netlistItem);
0191 
0192             netlistItem = bulb.netlistModel;
0193             Activity.netlistComponents.push(bulb);
0194             netlistItem[2].name = componentName;
0195             netlistItem[2]._json = Activity.netlist.length;
0196             netlistItem[3][0] = bulb.internalNetlistIndex[0];
0197             netlistItem[3][1] = bulb.internalNetlistIndex[1];
0198             Activity.netlist.push(netlistItem);
0199 
0200             netlistItem = aMeter2.netlistModel;
0201             Activity.netlistComponents.push(aMeter2);
0202             Activity.vSourcesList.push(aMeter2);
0203             netlistItem[2].name = "aMeter2-" + componentName;
0204             netlistItem[2]._json = Activity.netlist.length;
0205             netlistItem[3][0] = bulb.internalNetlistIndex[1];
0206             netlistItem[3][1] = bulb.externalNetlistIndex[1];
0207             Activity.netlist.push(netlistItem);
0208         }
0209     }
0210 
0211     function checkComponentAnswer() {
0212         // special case for level 8
0213         if(Activity.items.currentLevel === 7 && componentVoltage > 0 && !isBroken)
0214             return "bulbIn";
0215 
0216         if(componentVoltage === 10) {
0217             return "bulbGlows";
0218         } else if(terminalConnected >= 2 && componentVoltage < 10 && bulbCurrent > 0) {
0219             return "bulbGlowsLess"
0220         } else if(terminalConnected >= 2 && isBroken) {
0221             return "bulbBroken";
0222         } else
0223             return "";
0224     }
0225 }
0226