Debian Codegraph @

src/activities/digital_electricity/components/OrGate.qml

raw
1 /* GCompris - OrGate.qml
2 *
3 * SPDX-FileCopyrightText: 2016 Pulkit Gupta <[email protected]>
4 *
5 * Authors:
6 * Bruno Coudoin <[email protected]> (GTK+ version)
7 * Pulkit Gupta <[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
14 ElectricalComponent {
15 id: orGate
16 terminalSize: 0.5
17 noOfInputs: 2
18 noOfOutputs: 1
19 property var inputTerminalPosY: [0.2, 0.8]
20
21 information: qsTr("An OR gate outputs 1 if any of the inputs is 1, otherwise its output is 0:")
22 truthTable: [['A','B',qsTr("A OR B")],
23 ['0','0','0'],
24 ['0','1','1'],
25 ['1','0','1'],
26 ['1','1','1']]
27
28 property alias inputTerminals: inputTerminals
29 property alias outputTerminals: outputTerminals
30
31 Repeater {
32 id: inputTerminals
33 model: 2
34 delegate: inputTerminal
35 Component {
36 id: inputTerminal
37 TerminalPoint {
38 posX: 0.04
39 posY: inputTerminalPosY[index]
40 type: "In"
41 }
42 }
43 }
44
45 Repeater {
46 id: outputTerminals
47 model: 1
48 delegate: outputTerminal
49 Component {
50 id: outputTerminal
51 TerminalPoint {
52 posX: 0.96
53 posY: 0.5
54 type: "Out"
55 }
56 }
57 }
58
59 function updateOutput(wireVisited) {
60 var terminal = outputTerminals.itemAt(0)
61 /* Keep the output value == 0 if only one of the input terminals is connected */
62 terminal.value = (inputTerminals.itemAt(0).wires.length != 0 && inputTerminals.itemAt(1).wires.length != 0) ? (inputTerminals.itemAt(0).value | inputTerminals.itemAt(1).value) : 0
63 for(var i = 0 ; i < terminal.wires.length ; ++i)
64 terminal.wires[i].to.value = terminal.value
65
66 var componentVisited = []
67 for(var i = 0 ; i < terminal.wires.length ; ++i) {
68 var wire = terminal.wires[i]
69 var component = wire.to.parent
70 componentVisited[component] = true
71 wireVisited[wire] = true
72 component.updateOutput(wireVisited)
73 }
74 }
75 }