trollixx

Gridview displaced bug

Sep 5th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import QtQuick 2.0
  2.  
  3. Item {
  4.     width: 480; height: 480
  5.  
  6.     Rectangle {
  7.         id: gridWrapper
  8.         antialiasing: true
  9.         anchors.centerIn: parent
  10.         border {
  11.             color: "green"
  12.             width: 2
  13.         }
  14.         radius: 10
  15.         width: grid.cellWidth * internal.columnCount; height: grid.cellHeight * internal.rowCount
  16.  
  17.         DropArea {
  18.             anchors.fill: parent
  19.  
  20.             onPositionChanged: {
  21.                 var newRow = Math.ceil(drag.y / grid.cellHeight) - 1
  22.                 var newColumn = Math.ceil(drag.x / grid.cellWidth) - 1
  23.                 var newIndex = Math.min(newRow * internal.columnCount + newColumn, gridModel.count - 1)
  24.                 if (newIndex !== drag.source.visualIndex)
  25.                     gridModel.move(drag.source.visualIndex, newIndex, 1)
  26.             }
  27.  
  28.             onExited: {
  29.                 gridModel.remove(drag.source.visualIndex)
  30.             }
  31.  
  32.             GridView {
  33.                 id: grid
  34.                 anchors.fill: parent
  35.                 cellWidth: 100; cellHeight: 100
  36.  
  37.                 displaced: Transition {
  38.                     NumberAnimation { properties: "x,y"; easing.type: Easing.OutQuad; duration: 1000 }
  39.                 }
  40.  
  41.                 interactive: false
  42.  
  43.                 model: ListModel {
  44.                     id: gridModel
  45.                     ListElement { color: "blue" }
  46.                     ListElement { color: "green" }
  47.                     ListElement { color: "red" }
  48.                     ListElement { color: "yellow" }
  49.                     ListElement { color: "orange" }
  50.                 }
  51.  
  52.                 delegate: Item {
  53.                     id: delegate
  54.  
  55.                     width: 100; height: 100
  56.  
  57.                     Item {
  58.                         id: item
  59.  
  60.                         property int visualIndex: model.index
  61.  
  62.                         anchors {
  63.                             // Center the last row
  64.                             //horizontalCenterOffset: visualIndex >= internal.lastRowItemIndex ? delegate.width / 2 : 0
  65.                             horizontalCenter: parent.horizontalCenter
  66.                             verticalCenter: parent.verticalCenter
  67.                         }
  68.  
  69.                         width: 80; height: 80
  70.  
  71.                         Drag.active: mouseArea.drag.active
  72.                         Drag.source: item
  73.                         Drag.hotSpot.x: item.width / 2
  74.                         Drag.hotSpot.y: item.height / 2
  75.  
  76.                         Rectangle {
  77.                             anchors.fill: parent
  78.                             antialiasing: true
  79.                             color: model.color
  80.                             radius: 10
  81.  
  82.  
  83.                             MouseArea {
  84.                                 id: mouseArea
  85.  
  86.                                 onCanceled: item.Drag.cancel()
  87.  
  88.                                 anchors.fill: parent
  89.                                 drag.target: item
  90.                             }
  91.                         }
  92.                         states: [
  93.                             State {
  94.                                 when: item.Drag.active
  95.                                 ParentChange {
  96.                                     target: item
  97.                                     parent: grid
  98.                                 }
  99.  
  100.                                 AnchorChanges {
  101.                                     target: item
  102.                                     anchors.horizontalCenter: undefined
  103.                                     anchors.verticalCenter: undefined
  104.                                 }
  105.                             }
  106.                         ]
  107.                     }
  108.                 }
  109.             }
  110.         }
  111.     }
  112.  
  113.     Rectangle {
  114.         anchors {
  115.             top: gridWrapper.bottom; topMargin: 10
  116.             right: gridWrapper.right
  117.         }
  118.  
  119.         antialiasing: true
  120.         border {
  121.             color: "yellow"
  122.             width: 2
  123.         }
  124.         radius: 10
  125.         width: 100; height: 100
  126.  
  127.         Text {
  128.             anchors {
  129.                 fill: parent
  130.                 margins: 5
  131.             }
  132.             horizontalAlignment: Text.AlignHCenter
  133.             text: "Move yellow this direction"
  134.             verticalAlignment: Text.AlignVCenter
  135.             wrapMode: Text.WordWrap
  136.         }
  137.     }
  138.  
  139.     QtObject {
  140.         id: internal
  141.  
  142.         /// TODO: Report Qt Creator bug
  143.         property int columnCount: {1:1,2:2,3:2,4:2,5:3,6:3}[gridModel.count]
  144.         property int rowCount: Math.ceil(gridModel.count / columnCount)
  145.         property int lastRowItemIndex: {1:6,2:6,3:2,4:6,5:3,6:6}[gridModel.count]
  146.         property int __qtCreatorFix: 0 // Fix Qt Creator autoindent
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment