~baltix/gcompris-qt/0.97.1

« back to all changes in this revision

Viewing changes to src/activities/digital_electricity/components/BCDToSevenSegment.qml

  • Committer: Mantas Kriaučiūnas
  • Date: 2020-07-06 18:07:11 UTC
  • Revision ID: baltix@gmail.com-20200706180711-g254osu02cn8bc8p
GCompris-QT 0.97.1 with Lithuanian translation

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GCompris - BCDToSevenSegment.qml
 
2
 *
 
3
 * Copyright (C) 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
 *   This program is free software; you can redistribute it and/or modify
 
10
 *   it under the terms of the GNU General Public License as published by
 
11
 *   the Free Software Foundation; either version 3 of the License, or
 
12
 *   (at your option) any later version.
 
13
 *
 
14
 *   This program is distributed in the hope that it will be useful,
 
15
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 *   GNU General Public License for more details.
 
18
 *
 
19
 *   You should have received a copy of the GNU General Public License
 
20
 *   along with this program; if not, see <https://www.gnu.org/licenses/>.
 
21
 */
 
22
import QtQuick 2.6
 
23
import "../digital_electricity.js" as Activity
 
24
 
 
25
import GCompris 1.0
 
26
 
 
27
ElectricalComponent {
 
28
    id: bcdTo7Segment
 
29
    terminalSize: 0.125
 
30
    noOfInputs: 4
 
31
    noOfOutputs: 7
 
32
 
 
33
    property var inputTerminalPosY: [0.125, 0.375, 0.625, 0.875]
 
34
    property var outputTerminalPosY: [0.066, 0.211, 0.355, 0.5, 0.645, 0.789, 0.934]
 
35
 
 
36
    property var redChar: ["BCDTo7SegmentA_on.svg","BCDTo7SegmentB_on.svg","BCDTo7SegmentC_on.svg",
 
37
                               "BCDTo7SegmentD_on.svg","BCDTo7SegmentE_on.svg","BCDTo7SegmentF_on.svg",
 
38
                               "BCDTo7SegmentG_on.svg"]
 
39
 
 
40
    information: qsTr("BCD to 7 segment converter takes 4 binary inputs in its input terminals " +
 
41
                      "and gives 7 binary outputs which allow to light BCD number segments (binary-coded decimal) " +
 
42
                      "to display numbers between 0 and 9. The output for BCD To 7 Segment converted is:")
 
43
 
 
44
    truthTable: [['D','C','B','A','a','b','c','d','e','f','g'],
 
45
                 ['0','0','0','0','1','1','1','1','1','1','0'],
 
46
                 ['0','0','0','1','0','1','1','0','0','0','0'],
 
47
                 ['0','0','1','0','1','1','0','1','1','0','1'],
 
48
                 ['0','0','1','1','1','1','1','1','0','0','1'],
 
49
                 ['0','1','0','0','0','1','1','0','0','1','1'],
 
50
                 ['0','1','0','1','1','0','1','1','0','1','1'],
 
51
                 ['0','1','1','0','1','0','1','1','1','1','1'],
 
52
                 ['0','1','1','1','1','1','1','0','0','0','0'],
 
53
                 ['1','0','0','0','1','1','1','1','1','1','1'],
 
54
                 ['1','0','0','1','1','1','1','1','0','1','1']]
 
55
 
 
56
    property alias inputTerminals: inputTerminals
 
57
    property alias outputTerminals: outputTerminals
 
58
 
 
59
    Repeater {
 
60
        id: inputTerminals
 
61
        model: 4
 
62
        delegate: inputTerminal
 
63
        Component {
 
64
            id: inputTerminal
 
65
            TerminalPoint {
 
66
                posX: 0.05
 
67
                posY: inputTerminalPosY[index]
 
68
                type: "In"
 
69
            }
 
70
        }
 
71
    }
 
72
 
 
73
    Repeater {
 
74
        id: outputTerminals
 
75
        model: 7
 
76
        delegate: outputTerminal
 
77
        Component {
 
78
            id: outputTerminal
 
79
            TerminalPoint {
 
80
                posX: 0.95
 
81
                posY: outputTerminalPosY[index]
 
82
                type: "Out"
 
83
            }
 
84
        }
 
85
    }
 
86
 
 
87
    function updateOutput(wireVisited) {
 
88
        var i
 
89
        for(i = 1 ; i < truthTable.length ; ++i) {
 
90
            var j
 
91
            for(j = 0 ; j < noOfInputs; ++j) {
 
92
                if(inputTerminals.itemAt(j).value != truthTable[i][j])
 
93
                    break
 
94
            }
 
95
            if(j == noOfInputs)
 
96
                break
 
97
        }
 
98
        if(i == truthTable.length) {
 
99
            for(var j = 0 ; j < noOfOutputs ; ++j) {
 
100
                outputTerminals.itemAt(j).value = 0
 
101
                outputChar.itemAt(j).source = "qrc:/gcompris/src/core/resource/empty.svg"
 
102
            }
 
103
        }
 
104
        else {
 
105
            for(var j = 0 ; j < noOfOutputs ; ++j) {
 
106
                var terminal = outputTerminals.itemAt(j)
 
107
                terminal.value = truthTable[i][j + noOfInputs]
 
108
                outputChar.itemAt(j).source = (terminal.value == 0 ? "qrc:/gcompris/src/core/resource/empty.svg" : Activity.url + redChar[j])
 
109
            }
 
110
        }
 
111
 
 
112
        for(var i = 0 ; i < noOfOutputs ; ++i) {
 
113
            var terminal = outputTerminals.itemAt(i)
 
114
            for(var j = 0 ; j < terminal.wires.length ; ++j)
 
115
                terminal.wires[j].to.value = terminal.value
 
116
        }
 
117
 
 
118
        var componentVisited = []
 
119
        for(var i = 0 ; i < noOfOutputs ; ++i) {
 
120
            var terminal = outputTerminals.itemAt(i)
 
121
            for(var j = 0 ; j < terminal.wires.length ; ++j) {
 
122
                var wire = terminal.wires[j]
 
123
                var component = wire.to.parent
 
124
                if(componentVisited[component] != true && wireVisited[wire] != true) {
 
125
                    componentVisited[component] = true
 
126
                    wireVisited[wire] = true
 
127
                    component.updateOutput(wireVisited)
 
128
                }
 
129
            }
 
130
        }
 
131
    }
 
132
 
 
133
    Repeater {
 
134
        id: outputChar
 
135
        model: 7
 
136
        delegate: outputCharImages
 
137
        Component {
 
138
            id: outputCharImages
 
139
            Image {
 
140
                source: ""
 
141
                anchors.centerIn: parent
 
142
                height: parent.height
 
143
                width: parent.width
 
144
                fillMode: Image.PreserveAspectFit
 
145
                mipmap: true
 
146
                antialiasing: true
 
147
            }
 
148
        }
 
149
    }
 
150
}