Warning, /education/gcompris/src/activities/analog_electricity/components/Rheostat.qml is written in an unsupported language. File is not indexed.
0001 /* GCompris - Rheostat.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: rheostat // Two resistors with ammeters on each connection point 0017 terminalSize: 0.2 0018 noOfConnectionPoints: 3 0019 information: qsTr("Rheostat is used to vary resistance in an electrical circuit.") 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(current) 0024 source: Activity.url + "rheostat.svg" 0025 0026 property var nodeVoltages: [0, 0, 0] 0027 property double componentVoltage: 0 0028 property double current: 0 0029 property double bottomCurrent: 0 0030 property double wiperPosition: 0 0031 property double topResistance: 1000 * wiperPosition 0032 property double bottomResistance: 1000 - topResistance 0033 property string topResistanceTxt: topResistance.toString() 0034 property string bottomResistanceTxt: bottomResistance.toString() 0035 onBottomResistanceTxtChanged: Activity.restartTimer(); 0036 property string componentName: "Rheostat" 0037 property alias connectionPoints: connectionPoints 0038 property var connectionPointPosX: [0.335, 0.83, 0.335] 0039 property var connectionPointPosY: [0.1, 0.5, 0.9] 0040 property var internalNetlistIndex: [0, 0, 0] 0041 property var externalNetlistIndex: [0, 0, 0] 0042 property var netlistModel: 0043 [ 0044 "r", 0045 [ 0046 ], 0047 { 0048 "name": componentName, 0049 "r": topResistanceTxt, 0050 "_json_": 0 0051 }, 0052 [ 0053 0, 0054 0 0055 ] 0056 ] 0057 0058 Item { 0059 id: bottomResistor 0060 property int jsonNumber: 0 0061 property double current: 0 0062 property var netlistModel: 0063 [ 0064 "r", 0065 [ 0066 ], 0067 { 0068 "name": "-bottom", 0069 "r": rheostat.bottomResistanceTxt, 0070 "_json_": 0 0071 }, 0072 [ 0073 0, 0074 0 0075 ] 0076 ] 0077 } 0078 0079 Item { 0080 id: aMeter1 0081 property int jsonNumber: 0 0082 property double current: 0 0083 property var netlistModel: 0084 [ 0085 "a", 0086 [ 0087 ], 0088 { 0089 "name": "aMeter1-", 0090 "color": "magenta", 0091 "offset": "0", 0092 "_json_": aMeter1.jsonNumber 0093 }, 0094 [ 0095 0, 0096 0 0097 ] 0098 ] 0099 } 0100 0101 Item { 0102 id: aMeter2 0103 property int jsonNumber: 0 0104 property double current: 0 0105 property var netlistModel: 0106 [ 0107 "a", 0108 [ 0109 ], 0110 { 0111 "name": "aMeter2-", 0112 "color": "magenta", 0113 "offset": "0", 0114 "_json_": aMeter2.jsonNumber 0115 }, 0116 [ 0117 0, 0118 0 0119 ] 0120 ] 0121 } 0122 0123 Item { 0124 id: aMeter3 0125 property int jsonNumber: 0 0126 property double current: 0 0127 property var netlistModel: 0128 [ 0129 "a", 0130 [ 0131 ], 0132 { 0133 "name": "aMeter3-", 0134 "color": "magenta", 0135 "offset": "0", 0136 "_json_": aMeter3.jsonNumber 0137 }, 0138 [ 0139 0, 0140 0 0141 ] 0142 ] 0143 } 0144 0145 Repeater { 0146 id: connectionPoints 0147 model: 3 0148 delegate: connectionPoint 0149 Component { 0150 id: connectionPoint 0151 TerminalPoint { 0152 posX: connectionPointPosX[index] 0153 posY: connectionPointPosY[index] 0154 } 0155 } 0156 } 0157 0158 Item { 0159 id: wiperArea 0160 width: parent.width * 0.5 0161 height: parent.height * 0.4 0162 anchors.centerIn: parent 0163 anchors.horizontalCenterOffset: width * -0.33 0164 property double wiperLimit: wiperArea.height * 0.8 0165 //update wiper on zoom or window size changes 0166 onWiperLimitChanged: { 0167 wiper.y = wiperArea.wiperLimit * wiperPosition; 0168 } 0169 Rectangle { 0170 id: internalWire 0171 height: wiper.height * 0.4 0172 radius: height * 0.5 0173 color: "#535353" 0174 transformOrigin: Item.Left 0175 0176 property double wiperXCenter: wiper.x + wiper.width * 0.5 0177 property double wiperYCenter: wiper.y + wiper.height * 0.5 0178 property double pointXCenter: wiperXCenter + wiperArea.width 0179 property double pointYCenter: wiperArea.height * 0.5 0180 property double rectangleWidth: pointXCenter - wiperXCenter 0181 property double rectangleHeight: pointYCenter - wiperYCenter 0182 0183 x: wiperXCenter 0184 y: wiperYCenter - radius 0185 width: Math.sqrt(Math.pow(rectangleWidth, 2) + Math.pow(Math.abs(rectangleHeight), 2)) 0186 rotation: rectangleHeight >= 0 ? 90 - (Math.atan(rectangleWidth / rectangleHeight) * 57.3) : 0187 (Math.atan(rectangleWidth / rectangleHeight) * -57.3) - 90 0188 } 0189 Rectangle { 0190 id: wiper 0191 width: parent.width * 1.1 0192 height: wiperArea.height * 0.2 0193 anchors.horizontalCenter: parent.horizontalCenter 0194 color: "#535353" 0195 border.width: height * 0.2 0196 border.color: "#373737" 0197 radius: height * 0.2 0198 y: 0 0199 MouseArea { 0200 width: parent.width 0201 height: parent.height * 2 0202 anchors.centerIn: parent 0203 drag.target: wiper 0204 onPositionChanged: { 0205 if(wiper.y < 0) 0206 wiper.y = 0; 0207 if(wiper.y > wiperArea.wiperLimit) 0208 wiper.y = wiperArea.wiperLimit; 0209 wiperPosition = wiper.y / wiperArea.wiperLimit; 0210 } 0211 } 0212 } 0213 Rectangle { 0214 height: internalWire.height * 1.5 0215 width: height 0216 radius: height * 0.5 0217 anchors.centerIn: wiper 0218 color: "#888888" 0219 } 0220 } 0221 0222 function checkConnections() { 0223 terminalConnected = 0; 0224 for(var i = 0; i < noOfConnectionPoints; i++) { 0225 if(connectionPoints.itemAt(i).wires.length > 0) 0226 terminalConnected += 1; 0227 } 0228 if(terminalConnected >= 2) { 0229 rheostat.showLabel = true; 0230 } else { 0231 rheostat.showLabel = false; 0232 } 0233 } 0234 0235 function updateValues() { 0236 bottomCurrent = aMeter3.current; 0237 if(connectionPoints.itemAt(0).wires.length > 0 && connectionPoints.itemAt(2).wires.length > 0) { 0238 componentVoltage = (Math.abs(nodeVoltages[2] - nodeVoltages[0])).toFixed(2); 0239 current = (Math.abs(aMeter1.current)).toFixed(3); 0240 } else if(connectionPoints.itemAt(0).wires.length > 0 && connectionPoints.itemAt(1).wires.length > 0) { 0241 componentVoltage = (Math.abs(nodeVoltages[1] - nodeVoltages[0])).toFixed(2); 0242 current = (Math.abs(aMeter2.current)).toFixed(3); 0243 } else { 0244 componentVoltage = (Math.abs(nodeVoltages[2] - nodeVoltages[1])).toFixed(2); 0245 current = (Math.abs(bottomCurrent)).toFixed(3); 0246 } 0247 } 0248 0249 function initConnections() { 0250 var connectionIndex = Activity.connectionCount; 0251 rheostat.externalNetlistIndex[0] = ++connectionIndex; 0252 connectionPoints.itemAt(0).updateNetlistIndex(connectionIndex); 0253 rheostat.internalNetlistIndex[0] = ++connectionIndex; 0254 rheostat.internalNetlistIndex[1] = ++connectionIndex; 0255 rheostat.internalNetlistIndex[2] = ++connectionIndex; 0256 rheostat.externalNetlistIndex[1] = ++connectionIndex; 0257 connectionPoints.itemAt(1).updateNetlistIndex(connectionIndex); 0258 rheostat.externalNetlistIndex[2] = ++connectionIndex; 0259 connectionPoints.itemAt(2).updateNetlistIndex(connectionIndex); 0260 Activity.connectionCount = connectionIndex; 0261 } 0262 0263 function addToNetlist() { 0264 var netlistItem = aMeter1.netlistModel; 0265 Activity.netlistComponents.push(aMeter1); 0266 Activity.vSourcesList.push(aMeter1); 0267 netlistItem[2].name = "aMeter1-" + componentName; 0268 netlistItem[2]._json = Activity.netlist.length; 0269 netlistItem[3][0] = rheostat.externalNetlistIndex[0]; 0270 netlistItem[3][1] = rheostat.internalNetlistIndex[0]; 0271 Activity.netlist.push(netlistItem); 0272 0273 netlistItem = rheostat.netlistModel; 0274 Activity.netlistComponents.push(rheostat); 0275 if(netlistItem[2].r === "0") { 0276 Activity.vSourcesList.push(rheostat); 0277 } 0278 netlistItem[2].name = componentName; 0279 netlistItem[2]._json = Activity.netlist.length; 0280 netlistItem[3][0] = rheostat.internalNetlistIndex[0]; 0281 netlistItem[3][1] = rheostat.internalNetlistIndex[1]; 0282 Activity.netlist.push(netlistItem); 0283 0284 netlistItem = bottomResistor.netlistModel; 0285 Activity.netlistComponents.push(bottomResistor); 0286 if(netlistItem[2].r === "0") { 0287 Activity.vSourcesList.push(bottomResistor); 0288 } 0289 netlistItem[2].name = componentName + "-bottom"; 0290 netlistItem[2]._json = Activity.netlist.length; 0291 netlistItem[3][0] = rheostat.internalNetlistIndex[1]; 0292 netlistItem[3][1] = rheostat.internalNetlistIndex[2]; 0293 Activity.netlist.push(netlistItem); 0294 0295 netlistItem = aMeter2.netlistModel; 0296 Activity.netlistComponents.push(aMeter2); 0297 Activity.vSourcesList.push(aMeter2); 0298 netlistItem[2].name = "aMeter2-" + componentName; 0299 netlistItem[2]._json = Activity.netlist.length; 0300 netlistItem[3][0] = rheostat.internalNetlistIndex[1]; 0301 netlistItem[3][1] = rheostat.externalNetlistIndex[1]; 0302 Activity.netlist.push(netlistItem); 0303 0304 netlistItem = aMeter3.netlistModel; 0305 Activity.netlistComponents.push(aMeter3); 0306 Activity.vSourcesList.push(aMeter3); 0307 netlistItem[2].name = "aMeter3-" + componentName; 0308 netlistItem[2]._json = Activity.netlist.length; 0309 netlistItem[3][0] = rheostat.internalNetlistIndex[2]; 0310 netlistItem[3][1] = rheostat.externalNetlistIndex[2]; 0311 Activity.netlist.push(netlistItem); 0312 } 0313 0314 function checkComponentAnswer() { 0315 if(connectionPoints.itemAt(0).wires.length > 0 && connectionPoints.itemAt(2).wires.length > 0 && current === 0.005) { 0316 return "rheostatConst"; 0317 } else if(terminalConnected >= 2 && current > 0) { 0318 return "rheostatIn"; 0319 } else 0320 return ""; 0321 } 0322 }