Warning, /education/gcompris/src/activities/ordering_numbers/OrderingElement.qml is written in an unsupported language. File is not indexed.

0001 /* GCompris - OrderingElement.qml
0002  *
0003  * SPDX-FileCopyrightText: 2021 Emmanuel Charruau <echarruau@gmail.com>
0004  *
0005  * Authors:
0006  *   Harsh Kumar <hadron43@yahoo.com>
0007  *   Emmanuel Charruau <echarruau@gmail.com>
0008  *   Timothée Giet <animtim@gmail.com>
0009  *
0010  *   SPDX-License-Identifier: GPL-3.0-or-later
0011  */
0012 
0013 import QtQuick 2.12
0014 import GCompris 1.0
0015 
0016 import "../../core"
0017 import "ordering.js" as Activity
0018 
0019 Item {
0020 
0021     id: orderingElement
0022 
0023     property int index
0024 
0025     property string elementKey
0026     // Mode : numbers | alphabets | sentences | chronology
0027     property string mode
0028 
0029     property bool orderingElementIsEntered
0030     property bool isMoveAllowed: (placeholderName === "target")
0031 
0032     property ListModel listModel: null
0033     property Image highestParent: null
0034 
0035     property string placeholderName
0036 
0037     width: draggable.width
0038     height: draggable.height
0039 
0040     DropArea {
0041         id: orderingElementDropArea
0042 
0043         anchors.fill: parent
0044 
0045         // In target placeholder, detect all drops
0046         // In origin placeholder, detect no drops
0047         keys: elementKey
0048 
0049         onEntered: {
0050             if (isMoveAllowed) {
0051                 targetListModel.move(drag.source.index,index,1)
0052             }
0053         }
0054     }
0055 
0056     MouseArea {
0057         id: orderingElementMouseArea
0058 
0059         property bool dropActive: true
0060         property int onGoingAnimationCount: 0
0061 
0062         // For storing the initial position when dragged
0063         property point beginDragPosition
0064         property MouseArea originalParent
0065         property bool mouseHeld: false
0066 
0067         anchors.fill: parent
0068 
0069         drag.target: enabled ? draggable : null
0070         enabled: (!onGoingAnimationCount)
0071 
0072         onPressed: {
0073             orderingElementMouseArea.beginDragPosition = Qt.point(draggable.x, draggable.y)
0074             mouseHeld = true
0075             Activity.targetColorReset()
0076         }
0077         onReleased: {
0078             draggable.Drag.drop()
0079             mouseHeld = false
0080             draggable.parent = orderingElementMouseArea
0081 
0082             // The element may die on drop if removed, check if it exists
0083             if(draggable) {
0084                 draggable.x = beginDragPosition.x
0085                 draggable.y = beginDragPosition.y
0086             }
0087         }
0088 
0089 
0090         // To Display the text in the element
0091         Rectangle {
0092             id: draggable
0093 
0094             // To access when dragged
0095             property int index: orderingElement.index
0096             property string draggableText: elementValue
0097             property string placeholderName: orderingElement.placeholderName
0098 
0099             width: (mode === 'chronology') ? 100 * ApplicationInfo.ratio :
0100                 Math.max(elementCaption.width, 65 * ApplicationInfo.ratio)
0101 
0102             height: (mode === 'chronology') ? width :
0103             65 * ApplicationInfo.ratio / ((mode === 'sentences') ? 1.5 : 1)
0104 
0105             color: "white"
0106             opacity: 1
0107 
0108             Drag.keys: orderingElement.elementKey
0109             Drag.active: orderingElementMouseArea.drag.active
0110             Drag.hotSpot.x: width/2
0111             Drag.hotSpot.y: height/2
0112 
0113             border.color: borderColor
0114             border.width: 3 * ApplicationInfo.ratio
0115 
0116             radius: 10
0117 
0118             GCText {
0119                 id: elementCaption
0120 
0121                 anchors {
0122                     top: parent.top
0123                     left: parent.left
0124                     leftMargin: (parent.width - width) / 2
0125                 }
0126                 padding: 15 * ApplicationInfo.ratio
0127                 height: parent.height
0128                 color: "#373737"
0129                 verticalAlignment: Text.AlignVCenter
0130                 horizontalAlignment: Text.AlignHCenter
0131                 text: ([
0132                     'alphabets',
0133                     'numbers',
0134                     'sentences',
0135                 ].indexOf(mode) != -1) ? elementValue : ""
0136             }
0137             Image {
0138                 source: (mode === 'chronology') ? elementValue : ""
0139                 width: parent.width - 2 * parent.radius
0140                 height: parent.height - 2 * parent.radius
0141                 fillMode: Image.PreserveAspectFit
0142 
0143                 anchors {
0144                     top: parent.top
0145                     left: parent.left
0146                     margins: 10
0147                 }
0148             }
0149 
0150             Behavior on x {
0151                 PropertyAnimation {
0152                     id: animationX
0153                     duration: 500
0154                     easing.type: Easing.InOutBack
0155                     onRunningChanged: {
0156                         if(animationX.running) {
0157                             orderingElementMouseArea.onGoingAnimationCount++
0158                         } else {
0159                             orderingElementMouseArea.onGoingAnimationCount--
0160                         }
0161                     }
0162                 }
0163             }
0164 
0165             Behavior on y {
0166                 PropertyAnimation {
0167                     id: animationY
0168                     duration: 500
0169                     easing.type: Easing.InOutBack
0170                     onRunningChanged: {
0171                         if(animationY.running) {
0172                             orderingElementMouseArea.onGoingAnimationCount++
0173                         } else {
0174                             orderingElementMouseArea.onGoingAnimationCount--
0175                         }
0176                     }
0177                 }
0178             }
0179         }
0180 
0181         states: State {
0182             when: orderingElementMouseArea.mouseHeld
0183             ParentChange { target: draggable; parent: highestParent }
0184         }
0185     }
0186 }