| 1 |
/* GCompris - Switch2.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: switch2 // two resistors |
| 17 |
terminalSize: 0.2 |
| 18 |
noOfConnectionPoints: 3 |
| 19 |
information: qsTr("A three points switch can toggle a circuit between two connection points.") |
| 20 |
source: Activity.url + "switch2_off.svg" |
| 21 |
|
| 22 |
property double componentVoltage: 0 |
| 23 |
property double current: 0 |
| 24 |
property string resistanceValueOn: "0" |
| 25 |
property string resistanceValueOff: "100000000" |
| 26 |
property string resistanceTop: resistanceValueOn |
| 27 |
property string resistanceBottom: resistanceValueOff |
| 28 |
property alias connectionPoints: connectionPoints |
| 29 |
property var connectionPointPosX: [0.1, 0.9, 0.9] |
| 30 |
property var connectionPointPosY: [0.5, 0.165, 0.835] |
| 31 |
property string componentName: "Switch2" |
| 32 |
property var externalNetlistIndex: [0, 0, 0] |
| 33 |
property var netlistModel: |
| 34 |
[ |
| 35 |
"r", |
| 36 |
[ |
| 37 |
], |
| 38 |
{ |
| 39 |
"name": "-top", |
| 40 |
"r": resistanceValueOff, |
| 41 |
"_json_": 0 |
| 42 |
}, |
| 43 |
[ |
| 44 |
0, |
| 45 |
0 |
| 46 |
] |
| 47 |
] |
| 48 |
|
| 49 |
Item { |
| 50 |
id: switch2Bottom |
| 51 |
property int jsonNumber: 0 |
| 52 |
property double current: 0 |
| 53 |
property var netlistModel: |
| 54 |
[ |
| 55 |
"r", |
| 56 |
[ |
| 57 |
], |
| 58 |
{ |
| 59 |
"name": "-bottom", |
| 60 |
"r": switch2.resistanceValueOn, |
| 61 |
"_json_": 0 |
| 62 |
}, |
| 63 |
[ |
| 64 |
0, |
| 65 |
0 |
| 66 |
] |
| 67 |
] |
| 68 |
} |
| 69 |
|
| 70 |
Repeater { |
| 71 |
id: connectionPoints |
| 72 |
model: 3 |
| 73 |
delegate: connectionPoint |
| 74 |
Component { |
| 75 |
id: connectionPoint |
| 76 |
TerminalPoint { |
| 77 |
posX: connectionPointPosX[index] |
| 78 |
posY: connectionPointPosY[index] |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
MouseArea { |
| 84 |
height: parent.height * 0.33 |
| 85 |
width: parent.width * 0.25 |
| 86 |
anchors.horizontalCenter: parent.horizontalCenter |
| 87 |
anchors.horizontalCenterOffset: width * -0.5 |
| 88 |
anchors.top: parent.top |
| 89 |
onClicked: { |
| 90 |
if(switch2.source == Activity.url + "switch2_off.svg") { |
| 91 |
switch2.source = Activity.url + "switch2_on.svg"; |
| 92 |
switch2.netlistModel[2].r = resistanceValueOn; |
| 93 |
switch2Bottom.netlistModel[2].r = resistanceValueOff; |
| 94 |
} else { |
| 95 |
switch2.source = Activity.url + "switch2_off.svg"; |
| 96 |
switch2.netlistModel[2].r = resistanceValueOff; |
| 97 |
switch2Bottom.netlistModel[2].r = resistanceValueOn; |
| 98 |
} |
| 99 |
Activity.restartTimer(); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
function checkConnections() { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
function updateValues() { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
function initConnections() { |
| 112 |
var connectionIndex = Activity.connectionCount; |
| 113 |
switch2.externalNetlistIndex[0] = ++connectionIndex; |
| 114 |
connectionPoints.itemAt(0).updateNetlistIndex(connectionIndex); |
| 115 |
switch2.externalNetlistIndex[1] = ++connectionIndex; |
| 116 |
connectionPoints.itemAt(1).updateNetlistIndex(connectionIndex); |
| 117 |
switch2.externalNetlistIndex[2] = ++connectionIndex; |
| 118 |
connectionPoints.itemAt(2).updateNetlistIndex(connectionIndex); |
| 119 |
Activity.connectionCount = connectionIndex; |
| 120 |
} |
| 121 |
|
| 122 |
function addToNetlist() { |
| 123 |
var netlistItem = switch2.netlistModel; |
| 124 |
Activity.netlistComponents.push(switch2); |
| 125 |
if(netlistItem[2].r === "0") { |
| 126 |
Activity.vSourcesList.push(switch2); |
| 127 |
} |
| 128 |
netlistItem[2].name = componentName + "-top"; |
| 129 |
netlistItem[2]._json = Activity.netlist.length; |
| 130 |
netlistItem[3][0] = switch2.externalNetlistIndex[0]; |
| 131 |
netlistItem[3][1] = switch2.externalNetlistIndex[1]; |
| 132 |
Activity.netlist.push(netlistItem); |
| 133 |
|
| 134 |
netlistItem = switch2Bottom.netlistModel; |
| 135 |
Activity.netlistComponents.push(switch2Bottom); |
| 136 |
if(netlistItem[2].r === "0") { |
| 137 |
Activity.vSourcesList.push(switch2Bottom); |
| 138 |
} |
| 139 |
netlistItem[2].name = componentName + "-bottom"; |
| 140 |
netlistItem[2]._json = Activity.netlist.length; |
| 141 |
netlistItem[3][0] = switch2.externalNetlistIndex[0]; |
| 142 |
netlistItem[3][1] = switch2.externalNetlistIndex[2]; |
| 143 |
Activity.netlist.push(netlistItem); |
| 144 |
|
| 145 |
} |
| 146 |
} |