Debian Codegraph @

src/activities/analog_electricity/components/ElectricalComponent.qml

raw
1 /* GCompris - ElectricalComponent.qml
2 *
3 * SPDX-FileCopyrightText: 2020 Aiswarya Kaitheri Kandoth <[email protected]>
4 *
5 * Authors:
6 * Bruno Coudoin <[email protected]> (GTK+ version)
7 * Pulkit Gupta <[email protected]> (Qt Quick port)
8 * Aiswarya Kaitheri Kandoth <[email protected]> (AnalogElectricity activity)
9 *
10 * SPDX-License-Identifier: GPL-3.0-or-later
11 */
12 import QtQuick
13 import core 1.0
14
15 import "../../../core"
16 import "../analog_electricity.js" as Activity
17
18 Image {
19 id: electricalComponent
20 property double posX
21 property double posY
22 property double imgWidth
23 property double imgHeight
24 property string information
25 property string toolTipTxt
26 property string labelText1: ""
27 property string labelText2: ""
28 property int componentIndex
29 property int noOfConnectionPoints
30 property var terminalConnected: 0
31 property int rotationAngle: 0
32 property int initialAngle: 0
33 property int startingAngle: 0
34 property double terminalSize
35 property bool destructible
36 property bool showLabel: false
37
38 property alias rotateComponent: rotateComponent
39
40 x: posX * parent.width
41 y: posY * parent.height
42 sourceSize.width: imgWidth * parent.sizeMultiplier
43 sourceSize.height: imgHeight * parent.sizeMultiplier
44 width: sourceSize.width
45 height: sourceSize.height
46 fillMode: Image.PreserveAspectFit
47
48 z: 2
49 antialiasing: true
50
51 onPaintedWidthChanged: {
52 updateDragConstraints();
53 Activity.updateWires(componentIndex);
54 }
55
56 PropertyAnimation {
57 id: rotateComponent
58 target: electricalComponent
59 property: "rotation"
60 from: initialAngle; to: initialAngle + rotationAngle
61 duration: 1
62 onStarted:{ Activity.animationInProgress = true }
63 onStopped: {
64 initialAngle = initialAngle + rotationAngle;
65 Activity.updateWires(componentIndex);
66 if(initialAngle == startingAngle + rotationAngle * 45) {
67 if(initialAngle == 360 || initialAngle == -360)
68 initialAngle = 0;
69 startingAngle = initialAngle;
70 Activity.animationInProgress = false;
71 updateDragConstraints();
72 }
73 else
74 rotateComponent.start();
75 }
76 easing.type: Easing.InOutQuad;
77 }
78
79 function updateDragConstraints() {
80 if(initialAngle == 0 || initialAngle == 180 || initialAngle == 360 || initialAngle == -360
81 || initialAngle == -180) {
82 mouseArea.drag.minimumX = (electricalComponent.paintedWidth - electricalComponent.width)/2;
83 mouseArea.drag.minimumY = (electricalComponent.paintedHeight - electricalComponent.height)/2;
84
85 mouseArea.drag.maximumX = electricalComponent.parent.width -
86 (electricalComponent.width + electricalComponent.paintedWidth)/2;
87 mouseArea.drag.maximumY = electricalComponent.parent.height -
88 (electricalComponent.height + electricalComponent.paintedHeight)/2;
89 }
90 else {
91 mouseArea.drag.minimumX = (electricalComponent.paintedHeight - electricalComponent.width)/2;
92 mouseArea.drag.minimumY = (electricalComponent.paintedWidth - electricalComponent.height)/2;
93
94 mouseArea.drag.maximumX = electricalComponent.parent.width -
95 (electricalComponent.width + electricalComponent.paintedHeight)/2;
96 mouseArea.drag.maximumY = electricalComponent.parent.height -
97 (electricalComponent.height + electricalComponent.paintedWidth)/2;
98 }
99 }
100
101 MouseArea {
102 id: mouseArea
103 width: parent.paintedWidth
104 height: parent.paintedHeight
105 anchors.centerIn: parent
106 drag.target: electricalComponent
107 onPressed: {
108 Activity.updateToolTip(toolTipTxt);
109 Activity.componentSelected(componentIndex);
110 }
111 onClicked: {
112 if(Activity.toolDelete) {
113 if (destructible) {
114 Activity.removeComponent(componentIndex);
115 } else {
116 Activity.deselect();
117 }
118 } else if(typeof isBroken !== "undefined") {
119 // Repair broken bulb and LED on click
120 if(isBroken && terminalConnected < noOfConnectionPoints)
121 repairComponent();
122 }
123 }
124 onReleased: {
125 parent.posX = parent.x / parent.parent.width;
126 parent.posY = parent.y / parent.parent.height;
127 Activity.updateToolTip("");
128 }
129
130 onPositionChanged: {
131 updateDragConstraints();
132 Activity.updateWires(componentIndex);
133 }
134 }
135
136 Rectangle {
137 id: componentLabel
138 z: 100
139 width: 80 * ApplicationInfo.ratio
140 height: 40 * ApplicationInfo.ratio
141 radius: height * 0.5
142 anchors.horizontalCenter: parent.horizontalCenter
143 anchors.top: parent.bottom
144 color: "#80000000"
145 visible: showLabel
146 rotation: electricalComponent.rotation * -1
147 GCText {
148 anchors.centerIn: parent
149 width: parent.width
150 height: parent.height
151 font.pointSize: NaN // need to clear font.pointSize explicitly
152 fontSizeMode: Text.Fit
153 minimumPixelSize: 10
154 font.pixelSize: componentLabel.width * 0.2
155 color: "white"
156 text: labelText1 + "<br>" + labelText2
157 verticalAlignment: Text.AlignVCenter
158 horizontalAlignment: Text.AlignHCenter
159 }
160 }
161 }