Debian Codegraph @

src/activities/digital_electricity/components/SignalGenerator.qml

raw
1 /* GCompris - SignalGenerator.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 import "../digital_electricity.js" as Activity
14 import "../../../core"
15
16 ElectricalComponent {
17 id: signalGenerator
18 terminalSize: 0.25
19 noOfInputs: 0
20 noOfOutputs: 1
21
22 information: qsTr("A signal generator is used to generate alternating signals of 0 and 1. " +
23 "The time between two changes can be modified by pressing the arrows on the generator.")
24
25 property alias outputTerminals: outputTerminals
26 property double period: 1
27 property var periodFraction: ["1/4","1/2","1","2"]
28 property int periodIndex: 2
29
30 Repeater {
31 id: outputTerminals
32 model: 1
33 delegate: outputTerminal
34 Component {
35 id: outputTerminal
36 TerminalPoint {
37 posX: 0.94
38 posY: 0.5
39 value: 0
40 type: "Out"
41 }
42 }
43 }
44
45 function updateOutput(wireVisited) {
46 var terminal = outputTerminals.itemAt(0)
47 terminal.value = terminal.value == 1 ? 0 : 1
48 for(var i = 0 ; i < terminal.wires.length ; ++i)
49 terminal.wires[i].to.value = terminal.value
50
51 var componentVisited = []
52 for(var i = 0 ; i < terminal.wires.length ; ++i) {
53 var wire = terminal.wires[i]
54 var component = wire.to.parent
55 if(componentVisited[component] != true && wireVisited[wire] != true) {
56 componentVisited[component] = true
57 wireVisited[wire] = true
58 component.updateOutput(wireVisited)
59 }
60 }
61 }
62
63 Timer {
64 id: timer
65 interval: 1000 * signalGenerator.period
66 running: true
67 repeat: true
68 onTriggered: Activity.updateComponent(signalGenerator.index)
69 }
70
71 Image {
72 source: Activity.url + "arrowUp.svg"
73 height: 0.437 * parent.height
74 width: 0.208 * parent.width
75 sourceSize.height: height
76 sourceSize.width: width
77 property double posX: 0.6
78 property double posY: 0.28
79 x: (parent.width - parent.paintedWidth) / 2 + posX * parent.paintedWidth - width / 2
80 y: (parent.height - parent.paintedHeight) / 2 + posY * parent.paintedHeight - height / 2
81 fillMode: Image.PreserveAspectFit
82 antialiasing: true
83 opacity: 0
84 MouseArea {
85 anchors.fill: parent
86 anchors.centerIn: parent
87 enabled: signalGenerator.period != 2
88 onPressed: {
89 parent.opacity = 1
90 signalGenerator.period *= 2
91 periodIndex++
92 timer.restart()
93 outputTerminals.itemAt(0).value = 1
94 Activity.updateComponent(signalGenerator.index)
95 }
96 onReleased: {
97 parent.opacity = 0
98 }
99 }
100 }
101
102 Image {
103 source: Activity.url + "arrowDown.svg"
104 height: 0.437 * parent.height
105 width: 0.208 * parent.width
106 sourceSize.height: height
107 sourceSize.width: width
108 property double posX: 0.6
109 property double posY: 0.72
110 x: (parent.width - parent.paintedWidth) / 2 + posX * parent.paintedWidth - width / 2
111 y: (parent.height - parent.paintedHeight) / 2 + posY * parent.paintedHeight - height / 2
112 fillMode: Image.PreserveAspectFit
113 antialiasing: true
114 opacity: 0
115 MouseArea {
116 anchors.fill: parent
117 anchors.centerIn: parent
118 enabled: signalGenerator.period != 0.25
119 onPressed: {
120 parent.opacity = 1
121 signalGenerator.period /= 2
122 periodIndex--
123 timer.restart()
124 outputTerminals.itemAt(0).value = 1
125 Activity.updateComponent(signalGenerator.index)
126 }
127 onReleased: {
128 parent.opacity = 0
129 }
130 }
131 }
132
133 Rectangle {
134 height: 0.625 * parent.height
135 width: 0.35 * parent.width
136 color: "#00000000"
137 property double posX: 0.25
138 property double posY: 0.5
139 x: (parent.width - parent.paintedWidth) / 2 + posX * parent.paintedWidth - width / 2
140 y: (parent.height - parent.paintedHeight) / 2 + posY * parent.paintedHeight - height / 2
141 GCText {
142 id: value
143 anchors.centerIn: parent
144 fontSizeMode: Text.Fit
145 minimumPointSize: 6
146 font.pointSize: 32
147 color: "#3f6f71"
148 horizontalAlignment: Text.AlignHCenter
149 verticalAlignment: Text.AlignVCenter
150 height: parent.height - 10
151 width: parent.width - 10
152 text: qsTr("%1 s").arg(signalGenerator.periodFraction[periodIndex])
153 }
154 }
155 }