Debian Codegraph @

src/activities/digital_electricity/components/ElectricalComponent.qml

raw
1 /* GCompris - Component.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 import "../digital_electricity.js" as Activity
15
16 Image {
17 id: electricalComponent
18 property double posX
19 property double posY
20 property double imgWidth
21 property double imgHeight
22 property string imgSrc
23 property string information
24 property string toolTipTxt
25 property var truthTable: []
26 property int index
27 property int noOfInputs
28 property int noOfOutputs
29 property int rotationAngle: 0
30 property int initialAngle: 0
31 property int startingAngle: 0
32 property double terminalSize
33 property bool destructible
34
35 property alias rotateComponent: rotateComponent
36
37 x: posX * parent.width
38 y: posY * parent.height
39 width: imgWidth * parent.width
40 height: imgHeight * parent.height
41 fillMode: Image.PreserveAspectFit
42 sourceSize.width: width
43 sourceSize.height: height
44
45 source: Activity.url + imgSrc
46 z: 2
47 antialiasing: true
48
49 onPaintedWidthChanged: {
50 updateDragConstraints()
51 Activity.updateWires(index)
52 }
53
54 PropertyAnimation {
55 id: rotateComponent
56 target: electricalComponent
57 property: "rotation"
58 from: initialAngle; to: initialAngle + rotationAngle
59 duration: 1
60 onStarted:{ Activity.animationInProgress = true }
61 onStopped: {
62 initialAngle = initialAngle + rotationAngle
63 Activity.updateWires(index)
64 if(initialAngle == startingAngle + rotationAngle * 45) {
65 if(initialAngle == 360 || initialAngle == -360)
66 initialAngle = 0
67 startingAngle = initialAngle
68 Activity.animationInProgress = false
69 updateDragConstraints()
70 }
71 else
72 rotateComponent.start()
73 }
74 easing.type: Easing.InOutQuad
75 }
76
77 function updateDragConstraints() {
78 if(initialAngle == 0 || initialAngle == 180 || initialAngle == 360 || initialAngle == -360
79 || initialAngle == -180) {
80 mouseArea.drag.minimumX = (electricalComponent.paintedWidth - electricalComponent.width)/2
81 mouseArea.drag.minimumY = (electricalComponent.paintedHeight - electricalComponent.height)/2
82
83 mouseArea.drag.maximumX = electricalComponent.parent.width -
84 (electricalComponent.width + electricalComponent.paintedWidth)/2
85 mouseArea.drag.maximumY = electricalComponent.parent.height -
86 (electricalComponent.height + electricalComponent.paintedHeight)/2
87 }
88 else {
89 mouseArea.drag.minimumX = (electricalComponent.paintedHeight - electricalComponent.width)/2
90 mouseArea.drag.minimumY = (electricalComponent.paintedWidth - electricalComponent.height)/2
91
92 mouseArea.drag.maximumX = electricalComponent.parent.width -
93 (electricalComponent.width + electricalComponent.paintedHeight)/2
94 mouseArea.drag.maximumY = electricalComponent.parent.height -
95 (electricalComponent.height + electricalComponent.paintedWidth)/2
96 }
97 }
98
99 MouseArea {
100 id: mouseArea
101 width: parent.paintedWidth
102 height: parent.paintedHeight
103 anchors.centerIn: parent
104 drag.target: electricalComponent
105 onPressed: {
106 Activity.updateToolTip(toolTipTxt)
107 Activity.componentSelected(index)
108 }
109 onClicked: {
110 if(Activity.toolDelete) {
111 if (destructible) {
112 Activity.removeComponent(index)
113 } else {
114 Activity.deselect()
115 }
116 }
117 else {
118 if(imgSrc == "switchOff.svg") {
119 imgSrc = "switchOn.svg"
120 Activity.updateComponent(index)
121 }
122 else if(imgSrc == "switchOn.svg") {
123 imgSrc = "switchOff.svg"
124 Activity.updateComponent(index)
125 }
126 }
127 }
128 onReleased: {
129 parent.posX = parent.x / parent.parent.width
130 parent.posY = parent.y / parent.parent.height
131 Activity.updateToolTip("")
132 }
133
134 onPositionChanged: {
135 updateDragConstraints()
136 Activity.updateWires(index)
137 }
138 }
139 }