Debian Codegraph @

src/activities/digital_electricity/components/NotGate.qml

raw
1 /* GCompris - NotGate.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: notGate
16 terminalSize: 0.5
17 noOfInputs: 1
18 noOfOutputs: 1
19
20 information: qsTr("A Not gate (also known as inverter) outputs the opposite of the input. " +
21 "An input 0 gives an output 1. An Input 1 gives an output 0:")
22
23 truthTable: [['A',qsTr("NOT A")],
24 ['0','1'],
25 ['1','0']]
26
27 property alias inputTerminals: inputTerminals
28 property alias outputTerminals: outputTerminals
29
30 Repeater {
31 id: inputTerminals
32 model: 1
33 delegate: inputTerminal
34 Component {
35 id: inputTerminal
36 TerminalPoint {
37 posX: 0.04
38 posY: 0.5
39 type: "In"
40 }
41 }
42 }
43
44 Repeater {
45 id: outputTerminals
46 model: 1
47 delegate: outputTerminal
48 Component {
49 id: outputTerminal
50 TerminalPoint {
51 posX: 0.96
52 posY: 0.5
53 type: "Out"
54 }
55 }
56 }
57
58 function updateOutput(wireVisited) {
59 var terminal = outputTerminals.itemAt(0)
60 /* Keep the output value == 0 if only one of the input terminals is connected */
61 terminal.value = (inputTerminals.itemAt(0).wires.length != 0) ? !inputTerminals.itemAt(0).value : 0
62 for(var i = 0 ; i < terminal.wires.length ; ++i)
63 terminal.wires[i].to.value = terminal.value
64
65 var componentVisited = []
66 for(var i = 0 ; i < terminal.wires.length ; ++i) {
67 var wire = terminal.wires[i]
68 var component = wire.to.parent
69 if(componentVisited[component] != true && wireVisited[wire] != true) {
70 componentVisited[component] = true
71 wireVisited[wire] = true
72 component.updateOutput(wireVisited)
73 }
74 }
75 }
76 }