Warning, /education/gcompris/src/activities/digital_electricity/components/ElectricalComponent.qml is written in an unsupported language. File is not indexed.

0001 /* GCompris - Component.qml
0002  *
0003  * SPDX-FileCopyrightText: 2016 Pulkit Gupta <pulkitnsit@gmail.com>
0004  *
0005  * Authors:
0006  *   Bruno Coudoin <bruno.coudoin@gcompris.net> (GTK+ version)
0007  *   Pulkit Gupta <pulkitnsit@gmail.com> (Qt Quick port)
0008  *
0009  *   SPDX-License-Identifier: GPL-3.0-or-later
0010  */
0011 import QtQuick 2.12
0012 import GCompris 1.0
0013 
0014 import "../digital_electricity.js" as Activity
0015 
0016 Image {
0017     id: electricalComponent
0018     property double posX
0019     property double posY
0020     property double imgWidth
0021     property double imgHeight
0022     property string imgSrc
0023     property string information
0024     property string toolTipTxt
0025     property var truthTable: []
0026     property int index
0027     property int noOfInputs
0028     property int noOfOutputs
0029     property int rotationAngle: 0
0030     property int initialAngle: 0
0031     property int startingAngle: 0
0032     property double terminalSize
0033     property bool destructible
0034 
0035     property alias rotateComponent: rotateComponent
0036 
0037     x: posX * parent.width
0038     y: posY * parent.height
0039     width: imgWidth * parent.width
0040     height: imgHeight * parent.height
0041     fillMode: Image.PreserveAspectFit
0042 
0043     source: Activity.url + imgSrc
0044     z: 2
0045     mipmap: true
0046     antialiasing: true
0047 
0048     onPaintedWidthChanged: {
0049         updateDragConstraints()
0050         Activity.updateWires(index)
0051     }
0052 
0053     PropertyAnimation {
0054         id: rotateComponent
0055         target: electricalComponent
0056         property: "rotation"
0057         from: initialAngle; to: initialAngle + rotationAngle
0058         duration: 1
0059         onStarted:{ Activity.animationInProgress = true }
0060         onStopped: {
0061             initialAngle = initialAngle + rotationAngle
0062             Activity.updateWires(index)
0063             if(initialAngle == startingAngle + rotationAngle * 45) {
0064                 if(initialAngle == 360 || initialAngle == -360)
0065                     initialAngle = 0
0066                 startingAngle = initialAngle
0067                 Activity.animationInProgress = false
0068                 updateDragConstraints()
0069             }
0070             else
0071                 rotateComponent.start()
0072         }
0073         easing.type: Easing.InOutQuad
0074     }
0075 
0076     function updateDragConstraints() {
0077         if(initialAngle == 0 || initialAngle == 180 || initialAngle == 360 || initialAngle == -360
0078            || initialAngle == -180) {
0079             mouseArea.drag.minimumX = (electricalComponent.paintedWidth - electricalComponent.width)/2
0080             mouseArea.drag.minimumY = (electricalComponent.paintedHeight - electricalComponent.height)/2
0081 
0082             mouseArea.drag.maximumX = electricalComponent.parent.width -
0083                                       (electricalComponent.width + electricalComponent.paintedWidth)/2
0084             mouseArea.drag.maximumY = electricalComponent.parent.height -
0085                                       (electricalComponent.height + electricalComponent.paintedHeight)/2
0086         }
0087         else {
0088             mouseArea.drag.minimumX = (electricalComponent.paintedHeight - electricalComponent.width)/2
0089             mouseArea.drag.minimumY = (electricalComponent.paintedWidth - electricalComponent.height)/2
0090 
0091             mouseArea.drag.maximumX = electricalComponent.parent.width -
0092                                       (electricalComponent.width + electricalComponent.paintedHeight)/2
0093             mouseArea.drag.maximumY = electricalComponent.parent.height -
0094                                       (electricalComponent.height + electricalComponent.paintedWidth)/2
0095         }
0096     }
0097 
0098     MouseArea {
0099         id: mouseArea
0100         width: parent.paintedWidth
0101         height: parent.paintedHeight
0102         anchors.centerIn: parent
0103         drag.target: electricalComponent
0104         onPressed: {
0105             Activity.updateToolTip(toolTipTxt)
0106             Activity.componentSelected(index)
0107         }
0108         onClicked: {
0109             if(Activity.toolDelete) {
0110                 if (destructible) {
0111                     Activity.removeComponent(index)
0112                 } else {
0113                     Activity.deselect()
0114                 }
0115             }
0116             else {
0117                 if(imgSrc == "switchOff.svg") {
0118                     imgSrc = "switchOn.svg"
0119                     Activity.updateComponent(index)
0120                 }
0121                 else if(imgSrc == "switchOn.svg") {
0122                     imgSrc = "switchOff.svg"
0123                     Activity.updateComponent(index)
0124                 }
0125             }
0126         }
0127         onReleased: {
0128             parent.posX = parent.x / parent.parent.width
0129             parent.posY = parent.y / parent.parent.height
0130             Activity.updateToolTip("")
0131         }
0132 
0133         onPositionChanged: {
0134             updateDragConstraints()
0135             Activity.updateWires(index)
0136         }
0137     }
0138 }