ElectricalComponent.qml 6.02 KB
Newer Older
1 2
/* GCompris - ElectricalComponent.qml
 *
3
 * Copyright (C) 2020 Aiswarya Kaitheri Kandoth <aiswaryakk29@gmail.com>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * Authors:
 *   Bruno Coudoin <bruno.coudoin@gcompris.net> (GTK+ version)
 *   Pulkit Gupta <pulkitnsit@gmail.com> (Qt Quick port)
 *   Aiswarya Kaitheri Kandoth <aiswaryakk29@gmail.com> (AnalogElectricity activity)
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, see <https://www.gnu.org/licenses/>.
 */
import QtQuick 2.6
import GCompris 1.0

26
import "../../../core"
27 28 29 30 31 32 33 34 35 36
import "../analog_electricity.js" as Activity

Image {
    id: electricalComponent
    property double posX
    property double posY
    property double imgWidth
    property double imgHeight
    property string information
    property string toolTipTxt
37 38
    property string labelText1: ""
    property string labelText2: ""
39
    property int componentIndex
40
    property int noOfConnectionPoints
41
    property var terminalConnected: 0
42 43 44 45 46
    property int rotationAngle: 0
    property int initialAngle: 0
    property int startingAngle: 0
    property double terminalSize
    property bool destructible
47
    property bool showLabel: false
48 49 50 51 52 53 54 55 56 57 58 59 60 61

    property alias rotateComponent: rotateComponent

    x: posX * parent.width
    y: posY * parent.height
    width: imgWidth * parent.width
    height: imgHeight * parent.height
    fillMode: Image.PreserveAspectFit

    z: 2
    mipmap: true
    antialiasing: true

    onPaintedWidthChanged: {
62 63
        updateDragConstraints();
        Activity.updateWires(componentIndex);
64 65 66 67 68 69 70 71 72 73
    }

    PropertyAnimation {
        id: rotateComponent
        target: electricalComponent
        property: "rotation"
        from: initialAngle; to: initialAngle + rotationAngle
        duration: 1
        onStarted:{ Activity.animationInProgress = true }
        onStopped: {
74 75
            initialAngle = initialAngle + rotationAngle;
            Activity.updateWires(componentIndex);
76 77
            if(initialAngle == startingAngle + rotationAngle * 45) {
                if(initialAngle == 360 || initialAngle == -360)
78 79 80 81
                    initialAngle = 0;
                startingAngle = initialAngle;
                Activity.animationInProgress = false;
                updateDragConstraints();
82 83
            }
            else
84
                rotateComponent.start();
85
        }
86
        easing.type: Easing.InOutQuad;
87 88 89 90 91
    }

    function updateDragConstraints() {
        if(initialAngle == 0 || initialAngle == 180 || initialAngle == 360 || initialAngle == -360
           || initialAngle == -180) {
92 93
            mouseArea.drag.minimumX = (electricalComponent.paintedWidth - electricalComponent.width)/2;
            mouseArea.drag.minimumY = (electricalComponent.paintedHeight - electricalComponent.height)/2;
94 95

            mouseArea.drag.maximumX = electricalComponent.parent.width -
96
                                      (electricalComponent.width + electricalComponent.paintedWidth)/2;
97
            mouseArea.drag.maximumY = electricalComponent.parent.height -
98
                                      (electricalComponent.height + electricalComponent.paintedHeight)/2;
99 100
        }
        else {
101 102
            mouseArea.drag.minimumX = (electricalComponent.paintedHeight - electricalComponent.width)/2;
            mouseArea.drag.minimumY = (electricalComponent.paintedWidth - electricalComponent.height)/2;
103 104

            mouseArea.drag.maximumX = electricalComponent.parent.width -
105
                                      (electricalComponent.width + electricalComponent.paintedHeight)/2;
106
            mouseArea.drag.maximumY = electricalComponent.parent.height -
107
                                      (electricalComponent.height + electricalComponent.paintedWidth)/2;
108 109 110 111 112 113 114 115 116 117
        }
    }

    MouseArea {
        id: mouseArea
        width: parent.paintedWidth
        height: parent.paintedHeight
        anchors.centerIn: parent
        drag.target: electricalComponent
        onPressed: {
118 119
            Activity.updateToolTip(toolTipTxt);
            Activity.componentSelected(componentIndex);
120 121
        }
        onClicked: {
122
            if(Activity.toolDelete) {
123
                if (destructible) {
124
                    Activity.removeComponent(componentIndex);
125
                } else {
126
                    Activity.deselect();
127
                }
128
            } else if(typeof isBroken !== "undefined") {
129
                // Repair broken bulb and LED on click
130
                if(isBroken && terminalConnected < noOfConnectionPoints)
131
                    repairComponent();
132 133 134
            }
        }
        onReleased: {
135 136 137
            parent.posX = parent.x / parent.parent.width;
            parent.posY = parent.y / parent.parent.height;
            Activity.updateToolTip("");
138 139 140
        }

        onPositionChanged: {
141 142
            updateDragConstraints();
            Activity.updateWires(componentIndex);
143 144
        }
    }
145 146 147

    Rectangle {
        id: componentLabel
148
        z: 100
149 150 151 152 153
        width: 100
        height: 100
        anchors.centerIn: parent
        color: "#80000000"
        visible: showLabel
154
        rotation: electricalComponent.rotation * -1
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
        GCText {
            anchors.top: parent.top
            anchors.horizontalCenter: parent.horizontalCenter
            fontSize: 10
            color: "white"
            text: labelText1
            horizontalAlignment: Text.AlignHCenter
        }
        GCText {
            anchors.bottom: parent.bottom
            anchors.horizontalCenter: parent.horizontalCenter
            fontSize: 10
            color: "white"
            text: labelText2
            horizontalAlignment: Text.AlignHCenter
        }
    }
172
}