1/* GCompris - NorGate.qml
2 *
3 * SPDX-FileCopyrightText: 2016 Pulkit Gupta <pulkitnsit@gmail.com>
4 *
5 * Authors:
6 *   Bruno Coudoin <bruno.coudoin@gcompris.net> (GTK+ version)
7 *   Pulkit Gupta <pulkitnsit@gmail.com> (Qt Quick port)
8 *
9 *   SPDX-License-Identifier: GPL-3.0-or-later
10 */
11import QtQuick 2.9
12import GCompris 1.0
13
14ElectricalComponent {
15    id: norGate
16    terminalSize: 0.5
17    noOfInputs: 2
18    noOfOutputs: 1
19    property var inputTerminalPosY: [0.2, 0.8]
20
21    information: qsTr("A NOR gate outputs the opposite of an OR gate. As soon as there is a 1 in an input the output is equal to 0. " +
22                      "To obtain a 1 all the inputs must be equal to 0:")
23
24    truthTable: [['A','B',qsTr("NOT (A OR B)")],
25                 ['0','0','1'],
26                 ['0','1','0'],
27                 ['1','0','0'],
28                 ['1','1','0']]
29
30    property alias inputTerminals: inputTerminals
31    property alias outputTerminals: outputTerminals
32
33    Repeater {
34        id: inputTerminals
35        model: 2
36        delegate: inputTerminal
37        Component {
38            id: inputTerminal
39            TerminalPoint {
40                posX: 0.04
41                posY: inputTerminalPosY[index]
42                type: "In"
43            }
44        }
45    }
46
47    Repeater {
48        id: outputTerminals
49        model: 1
50        delegate: outputTerminal
51        Component {
52            id: outputTerminal
53            TerminalPoint {
54                posX: 0.96
55                posY: 0.5
56                type: "Out"
57            }
58        }
59    }
60
61    function updateOutput(wireVisited) {
62        var terminal = outputTerminals.itemAt(0)
63        /* Keep the output value == 0 if only one of the input terminals is connected */
64        terminal.value = (inputTerminals.itemAt(0).wires.length != 0 && inputTerminals.itemAt(1).wires.length != 0) ? !(inputTerminals.itemAt(0).value | inputTerminals.itemAt(1).value) : 0
65        for(var i = 0 ; i < terminal.wires.length ; ++i)
66            terminal.wires[i].to.value = terminal.value
67
68        var componentVisited = []
69        for(var i = 0 ; i < terminal.wires.length ; ++i) {
70            var wire = terminal.wires[i]
71            var component = wire.to.parent
72            componentVisited[component] = true
73            wireVisited[wire] = true
74            component.updateOutput(wireVisited)
75        }
76    }
77}
78