| 1 |
/* GCompris - Rheostat.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: rheostat // Two resistors with ammeters on each connection point |
| 17 |
terminalSize: 0.2 |
| 18 |
noOfConnectionPoints: 3 |
| 19 |
information: qsTr("Rheostat is used to vary resistance 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(current) |
| 24 |
source: Activity.url + "rheostat.svg" |
| 25 |
|
| 26 |
property var nodeVoltages: [0, 0, 0] |
| 27 |
property double componentVoltage: 0 |
| 28 |
property double current: 0 |
| 29 |
property double bottomCurrent: 0 |
| 30 |
property double wiperPosition: 0 |
| 31 |
property double topResistance: 1000 * wiperPosition |
| 32 |
property double bottomResistance: 1000 - topResistance |
| 33 |
property string topResistanceTxt: topResistance.toString() |
| 34 |
property string bottomResistanceTxt: bottomResistance.toString() |
| 35 |
onBottomResistanceTxtChanged: Activity.restartTimer(); |
| 36 |
property string componentName: "Rheostat" |
| 37 |
property alias connectionPoints: connectionPoints |
| 38 |
property var connectionPointPosX: [0.335, 0.83, 0.335] |
| 39 |
property var connectionPointPosY: [0.1, 0.5, 0.9] |
| 40 |
property var internalNetlistIndex: [0, 0, 0] |
| 41 |
property var externalNetlistIndex: [0, 0, 0] |
| 42 |
property var netlistModel: |
| 43 |
[ |
| 44 |
"r", |
| 45 |
[ |
| 46 |
], |
| 47 |
{ |
| 48 |
"name": componentName, |
| 49 |
"r": topResistanceTxt, |
| 50 |
"_json_": 0 |
| 51 |
}, |
| 52 |
[ |
| 53 |
0, |
| 54 |
0 |
| 55 |
] |
| 56 |
] |
| 57 |
|
| 58 |
Item { |
| 59 |
id: bottomResistor |
| 60 |
property int jsonNumber: 0 |
| 61 |
property double current: 0 |
| 62 |
property var netlistModel: |
| 63 |
[ |
| 64 |
"r", |
| 65 |
[ |
| 66 |
], |
| 67 |
{ |
| 68 |
"name": "-bottom", |
| 69 |
"r": rheostat.bottomResistanceTxt, |
| 70 |
"_json_": 0 |
| 71 |
}, |
| 72 |
[ |
| 73 |
0, |
| 74 |
0 |
| 75 |
] |
| 76 |
] |
| 77 |
} |
| 78 |
|
| 79 |
Item { |
| 80 |
id: aMeter1 |
| 81 |
property int jsonNumber: 0 |
| 82 |
property double current: 0 |
| 83 |
property var netlistModel: |
| 84 |
[ |
| 85 |
"a", |
| 86 |
[ |
| 87 |
], |
| 88 |
{ |
| 89 |
"name": "aMeter1-", |
| 90 |
"color": "magenta", |
| 91 |
"offset": "0", |
| 92 |
"_json_": aMeter1.jsonNumber |
| 93 |
}, |
| 94 |
[ |
| 95 |
0, |
| 96 |
0 |
| 97 |
] |
| 98 |
] |
| 99 |
} |
| 100 |
|
| 101 |
Item { |
| 102 |
id: aMeter2 |
| 103 |
property int jsonNumber: 0 |
| 104 |
property double current: 0 |
| 105 |
property var netlistModel: |
| 106 |
[ |
| 107 |
"a", |
| 108 |
[ |
| 109 |
], |
| 110 |
{ |
| 111 |
"name": "aMeter2-", |
| 112 |
"color": "magenta", |
| 113 |
"offset": "0", |
| 114 |
"_json_": aMeter2.jsonNumber |
| 115 |
}, |
| 116 |
[ |
| 117 |
0, |
| 118 |
0 |
| 119 |
] |
| 120 |
] |
| 121 |
} |
| 122 |
|
| 123 |
Item { |
| 124 |
id: aMeter3 |
| 125 |
property int jsonNumber: 0 |
| 126 |
property double current: 0 |
| 127 |
property var netlistModel: |
| 128 |
[ |
| 129 |
"a", |
| 130 |
[ |
| 131 |
], |
| 132 |
{ |
| 133 |
"name": "aMeter3-", |
| 134 |
"color": "magenta", |
| 135 |
"offset": "0", |
| 136 |
"_json_": aMeter3.jsonNumber |
| 137 |
}, |
| 138 |
[ |
| 139 |
0, |
| 140 |
0 |
| 141 |
] |
| 142 |
] |
| 143 |
} |
| 144 |
|
| 145 |
Repeater { |
| 146 |
id: connectionPoints |
| 147 |
model: 3 |
| 148 |
delegate: connectionPoint |
| 149 |
Component { |
| 150 |
id: connectionPoint |
| 151 |
TerminalPoint { |
| 152 |
posX: connectionPointPosX[index] |
| 153 |
posY: connectionPointPosY[index] |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
Item { |
| 159 |
id: wiperArea |
| 160 |
width: parent.width * 0.5 |
| 161 |
height: parent.height * 0.4 |
| 162 |
anchors.centerIn: parent |
| 163 |
anchors.horizontalCenterOffset: width * -0.33 |
| 164 |
property double wiperLimit: wiperArea.height * 0.8 |
| 165 |
//update wiper on zoom or window size changes |
| 166 |
onWiperLimitChanged: { |
| 167 |
wiper.y = wiperArea.wiperLimit * wiperPosition; |
| 168 |
} |
| 169 |
Rectangle { |
| 170 |
id: internalWire |
| 171 |
height: wiper.height * 0.4 |
| 172 |
radius: height * 0.5 |
| 173 |
color: "#535353" |
| 174 |
transformOrigin: Item.Left |
| 175 |
|
| 176 |
property double wiperXCenter: wiper.x + wiper.width * 0.5 |
| 177 |
property double wiperYCenter: wiper.y + wiper.height * 0.5 |
| 178 |
property double pointXCenter: wiperXCenter + wiperArea.width |
| 179 |
property double pointYCenter: wiperArea.height * 0.5 |
| 180 |
property double rectangleWidth: pointXCenter - wiperXCenter |
| 181 |
property double rectangleHeight: pointYCenter - wiperYCenter |
| 182 |
|
| 183 |
x: wiperXCenter |
| 184 |
y: wiperYCenter - radius |
| 185 |
width: Math.sqrt(Math.pow(rectangleWidth, 2) + Math.pow(Math.abs(rectangleHeight), 2)) |
| 186 |
rotation: rectangleHeight >= 0 ? 90 - (Math.atan(rectangleWidth / rectangleHeight) * 57.3) : |
| 187 |
(Math.atan(rectangleWidth / rectangleHeight) * -57.3) - 90 |
| 188 |
} |
| 189 |
Rectangle { |
| 190 |
id: wiper |
| 191 |
width: parent.width * 1.1 |
| 192 |
height: wiperArea.height * 0.2 |
| 193 |
anchors.horizontalCenter: parent.horizontalCenter |
| 194 |
color: "#535353" |
| 195 |
border.width: height * 0.2 |
| 196 |
border.color: "#373737" |
| 197 |
radius: height * 0.2 |
| 198 |
y: 0 |
| 199 |
MouseArea { |
| 200 |
width: parent.width |
| 201 |
height: parent.height * 2 |
| 202 |
anchors.centerIn: parent |
| 203 |
drag.target: wiper |
| 204 |
onPositionChanged: { |
| 205 |
if(wiper.y < 0) |
| 206 |
wiper.y = 0; |
| 207 |
if(wiper.y > wiperArea.wiperLimit) |
| 208 |
wiper.y = wiperArea.wiperLimit; |
| 209 |
wiperPosition = wiper.y / wiperArea.wiperLimit; |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
Rectangle { |
| 214 |
height: internalWire.height * 1.5 |
| 215 |
width: height |
| 216 |
radius: height * 0.5 |
| 217 |
anchors.centerIn: wiper |
| 218 |
color: "#888888" |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
function checkConnections() { |
| 223 |
terminalConnected = 0; |
| 224 |
for(var i = 0; i < noOfConnectionPoints; i++) { |
| 225 |
if(connectionPoints.itemAt(i).wires.length > 0) |
| 226 |
terminalConnected += 1; |
| 227 |
} |
| 228 |
if(terminalConnected >= 2) { |
| 229 |
rheostat.showLabel = true; |
| 230 |
} else { |
| 231 |
rheostat.showLabel = false; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
function updateValues() { |
| 236 |
bottomCurrent = aMeter3.current; |
| 237 |
if(connectionPoints.itemAt(0).wires.length > 0 && connectionPoints.itemAt(2).wires.length > 0) { |
| 238 |
componentVoltage = (Math.abs(nodeVoltages[2] - nodeVoltages[0])).toFixed(2); |
| 239 |
current = (Math.abs(aMeter1.current)).toFixed(3); |
| 240 |
} else if(connectionPoints.itemAt(0).wires.length > 0 && connectionPoints.itemAt(1).wires.length > 0) { |
| 241 |
componentVoltage = (Math.abs(nodeVoltages[1] - nodeVoltages[0])).toFixed(2); |
| 242 |
current = (Math.abs(aMeter2.current)).toFixed(3); |
| 243 |
} else { |
| 244 |
componentVoltage = (Math.abs(nodeVoltages[2] - nodeVoltages[1])).toFixed(2); |
| 245 |
current = (Math.abs(bottomCurrent)).toFixed(3); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
function initConnections() { |
| 250 |
var connectionIndex = Activity.connectionCount; |
| 251 |
rheostat.externalNetlistIndex[0] = ++connectionIndex; |
| 252 |
connectionPoints.itemAt(0).updateNetlistIndex(connectionIndex); |
| 253 |
rheostat.internalNetlistIndex[0] = ++connectionIndex; |
| 254 |
rheostat.internalNetlistIndex[1] = ++connectionIndex; |
| 255 |
rheostat.internalNetlistIndex[2] = ++connectionIndex; |
| 256 |
rheostat.externalNetlistIndex[1] = ++connectionIndex; |
| 257 |
connectionPoints.itemAt(1).updateNetlistIndex(connectionIndex); |
| 258 |
rheostat.externalNetlistIndex[2] = ++connectionIndex; |
| 259 |
connectionPoints.itemAt(2).updateNetlistIndex(connectionIndex); |
| 260 |
Activity.connectionCount = connectionIndex; |
| 261 |
} |
| 262 |
|
| 263 |
function addToNetlist() { |
| 264 |
var netlistItem = aMeter1.netlistModel; |
| 265 |
Activity.netlistComponents.push(aMeter1); |
| 266 |
Activity.vSourcesList.push(aMeter1); |
| 267 |
netlistItem[2].name = "aMeter1-" + componentName; |
| 268 |
netlistItem[2]._json = Activity.netlist.length; |
| 269 |
netlistItem[3][0] = rheostat.externalNetlistIndex[0]; |
| 270 |
netlistItem[3][1] = rheostat.internalNetlistIndex[0]; |
| 271 |
Activity.netlist.push(netlistItem); |
| 272 |
|
| 273 |
netlistItem = rheostat.netlistModel; |
| 274 |
Activity.netlistComponents.push(rheostat); |
| 275 |
if(netlistItem[2].r === "0") { |
| 276 |
Activity.vSourcesList.push(rheostat); |
| 277 |
} |
| 278 |
netlistItem[2].name = componentName; |
| 279 |
netlistItem[2]._json = Activity.netlist.length; |
| 280 |
netlistItem[3][0] = rheostat.internalNetlistIndex[0]; |
| 281 |
netlistItem[3][1] = rheostat.internalNetlistIndex[1]; |
| 282 |
Activity.netlist.push(netlistItem); |
| 283 |
|
| 284 |
netlistItem = bottomResistor.netlistModel; |
| 285 |
Activity.netlistComponents.push(bottomResistor); |
| 286 |
if(netlistItem[2].r === "0") { |
| 287 |
Activity.vSourcesList.push(bottomResistor); |
| 288 |
} |
| 289 |
netlistItem[2].name = componentName + "-bottom"; |
| 290 |
netlistItem[2]._json = Activity.netlist.length; |
| 291 |
netlistItem[3][0] = rheostat.internalNetlistIndex[1]; |
| 292 |
netlistItem[3][1] = rheostat.internalNetlistIndex[2]; |
| 293 |
Activity.netlist.push(netlistItem); |
| 294 |
|
| 295 |
netlistItem = aMeter2.netlistModel; |
| 296 |
Activity.netlistComponents.push(aMeter2); |
| 297 |
Activity.vSourcesList.push(aMeter2); |
| 298 |
netlistItem[2].name = "aMeter2-" + componentName; |
| 299 |
netlistItem[2]._json = Activity.netlist.length; |
| 300 |
netlistItem[3][0] = rheostat.internalNetlistIndex[1]; |
| 301 |
netlistItem[3][1] = rheostat.externalNetlistIndex[1]; |
| 302 |
Activity.netlist.push(netlistItem); |
| 303 |
|
| 304 |
netlistItem = aMeter3.netlistModel; |
| 305 |
Activity.netlistComponents.push(aMeter3); |
| 306 |
Activity.vSourcesList.push(aMeter3); |
| 307 |
netlistItem[2].name = "aMeter3-" + componentName; |
| 308 |
netlistItem[2]._json = Activity.netlist.length; |
| 309 |
netlistItem[3][0] = rheostat.internalNetlistIndex[2]; |
| 310 |
netlistItem[3][1] = rheostat.externalNetlistIndex[2]; |
| 311 |
Activity.netlist.push(netlistItem); |
| 312 |
} |
| 313 |
|
| 314 |
function checkComponentAnswer(): string { |
| 315 |
if(connectionPoints.itemAt(0).wires.length > 0 && connectionPoints.itemAt(2).wires.length > 0 && current === 0.005) { |
| 316 |
return "rheostatConst"; |
| 317 |
} else if(terminalConnected >= 2 && current > 0) { |
| 318 |
return "rheostatIn"; |
| 319 |
} else |
| 320 |
return ""; |
| 321 |
} |
| 322 |
} |