Debian Codegraph @

src/activities/digital_electricity/components/XorGate.qml

raw
1 /* GCompris - XorGate.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: xorGate
16 terminalSize: 0.5
17 noOfInputs: 2
18 noOfOutputs: 1
19 property var inputTerminalPosY: [0.2, 0.8]
20
21 information: qsTr("An XOR gate outputs 1 if the number of 1 in inputs is odd, and 0 if number of 1 in " +
22 "inputs is even. In this activity, a 2 inputs XOR gate is shown. The output for a 2 inputs XOR gate is:")
23 truthTable: [['A','B',qsTr("A XOR B")],
24 ['0','0','0'],
25 ['0','1','1'],
26 ['1','0','1'],
27 ['1','1','0']]
28
29 property alias inputTerminals: inputTerminals
30 property alias outputTerminals: outputTerminals
31
32 Repeater {
33 id: inputTerminals
34 model: 2
35 delegate: inputTerminal
36 Component {
37 id: inputTerminal
38 TerminalPoint {
39 posX: 0.04
40 posY: inputTerminalPosY[index]
41 type: "In"
42 }
43 }
44 }
45
46 Repeater {
47 id: outputTerminals
48 model: 1
49 delegate: outputTerminal
50 Component {
51 id: outputTerminal
52 TerminalPoint {
53 posX: 0.96
54 posY: 0.5
55 type: "Out"
56 }
57 }
58 }
59
60 function updateOutput(wireVisited) {
61 var terminal = outputTerminals.itemAt(0)
62 terminal.value = inputTerminals.itemAt(0).value ^ inputTerminals.itemAt(1).value
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 }