Advertisement
Guest User

Untitled

a guest
Mar 4th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QML 2.81 KB | None | 0 0
  1. import QtQuick 2.3
  2. import QtQuick.Controls 2.3
  3.  
  4. ApplicationWindow {
  5.     id: root
  6.     visible: true
  7.  
  8.     ListModel {
  9.         id: my_model
  10.         dynamicRoles: true
  11.         Component.onCompleted: {
  12.             for (var i =0; i < 16; ++i) {
  13.                 append({"item": i})
  14.             }
  15.         }
  16.  
  17.         function get_item(index) {
  18.             return get(index).item
  19.         }
  20.  
  21.         function set_item(index, item) {
  22.             set(index, {"item": item})
  23.         }
  24.  
  25.         function replace(from_index, to_index) {
  26.             var from_obj = get_item(from_index)
  27.             var to_obj = get_item(to_index)
  28.             set_item(to_index, from_obj)
  29.             set_item(from_index, to_obj)
  30.         }
  31.     }
  32.  
  33.  
  34.  
  35.     ParallelAnimation {
  36.         id: anim
  37.         property Item toItem
  38.         property Item fromItem
  39.         property point fromPosition: Qt.point(0, 0)
  40.         property point toPosition: Qt.point(0, 0)
  41.         property int duration: 200
  42.  
  43.         function start_animation(from, to) {
  44.             fromItem = from
  45.             toItem = to
  46.             fromPosition = Qt.point(from.x, from.y)
  47.             toPosition = Qt.point(to.x, to.y)
  48.             start()
  49.         }
  50.  
  51.         NumberAnimation {
  52.             target: anim.fromItem
  53.             property: "x"
  54.             duration: anim.duration
  55.             from: anim.fromPosition.x
  56.             to: anim.toPosition.x
  57.             easing.type: Easing.InOutQuad
  58.         }
  59.         NumberAnimation {
  60.             target: anim.fromItem
  61.             property: "y"
  62.             duration: anim.duration
  63.             from: anim.fromPosition.y
  64.             to: anim.toPosition.y
  65.             easing.type: Easing.InOutQuad
  66.         }
  67.         onStopped: {
  68.             fromItem.x = fromPosition.x
  69.             fromItem.y = fromPosition.y
  70.             my_model.replace(fromItem._index, toItem._index)
  71.         }
  72.     }
  73.  
  74.     property Item from: null
  75.  
  76.     function item_clicked(item) {
  77.         if (from == null) {
  78.             from = item
  79.         } else {
  80.             anim.start_animation(from, item)
  81.             from = null
  82.         }
  83.     }
  84.  
  85.     Grid {
  86.         columns: 4
  87.         Repeater {
  88.             id: repeater
  89.             model: my_model
  90.             Rectangle {
  91.                 id: root_item
  92.                 property int _index: index
  93.                 width: 20
  94.                 height: 20
  95.                 color: from === root_item ? "lightblue" : "lightgray"
  96.                 border.color: "black"
  97.                 border.width: 1
  98.                 Label {
  99.                     anchors.centerIn: parent
  100.                     text: model.item
  101.                 }
  102.  
  103.                 MouseArea {
  104.                     anchors.fill: parent
  105.                     onClicked: root.item_clicked(root_item)
  106.                 }
  107.             }
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement