Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import QtQuick 2.0
- Item {
- width: 480; height: 480
- Rectangle {
- id: gridWrapper
- antialiasing: true
- anchors.centerIn: parent
- border {
- color: "green"
- width: 2
- }
- radius: 10
- width: grid.cellWidth * internal.columnCount; height: grid.cellHeight * internal.rowCount
- DropArea {
- anchors.fill: parent
- onPositionChanged: {
- var newRow = Math.ceil(drag.y / grid.cellHeight) - 1
- var newColumn = Math.ceil(drag.x / grid.cellWidth) - 1
- var newIndex = Math.min(newRow * internal.columnCount + newColumn, gridModel.count - 1)
- if (newIndex !== drag.source.visualIndex)
- gridModel.move(drag.source.visualIndex, newIndex, 1)
- }
- onExited: {
- gridModel.remove(drag.source.visualIndex)
- }
- GridView {
- id: grid
- anchors.fill: parent
- cellWidth: 100; cellHeight: 100
- displaced: Transition {
- NumberAnimation { properties: "x,y"; easing.type: Easing.OutQuad; duration: 1000 }
- }
- interactive: false
- model: ListModel {
- id: gridModel
- ListElement { color: "blue" }
- ListElement { color: "green" }
- ListElement { color: "red" }
- ListElement { color: "yellow" }
- ListElement { color: "orange" }
- }
- delegate: Item {
- id: delegate
- width: 100; height: 100
- Item {
- id: item
- property int visualIndex: model.index
- anchors {
- // Center the last row
- //horizontalCenterOffset: visualIndex >= internal.lastRowItemIndex ? delegate.width / 2 : 0
- horizontalCenter: parent.horizontalCenter
- verticalCenter: parent.verticalCenter
- }
- width: 80; height: 80
- Drag.active: mouseArea.drag.active
- Drag.source: item
- Drag.hotSpot.x: item.width / 2
- Drag.hotSpot.y: item.height / 2
- Rectangle {
- anchors.fill: parent
- antialiasing: true
- color: model.color
- radius: 10
- MouseArea {
- id: mouseArea
- onCanceled: item.Drag.cancel()
- anchors.fill: parent
- drag.target: item
- }
- }
- states: [
- State {
- when: item.Drag.active
- ParentChange {
- target: item
- parent: grid
- }
- AnchorChanges {
- target: item
- anchors.horizontalCenter: undefined
- anchors.verticalCenter: undefined
- }
- }
- ]
- }
- }
- }
- }
- }
- Rectangle {
- anchors {
- top: gridWrapper.bottom; topMargin: 10
- right: gridWrapper.right
- }
- antialiasing: true
- border {
- color: "yellow"
- width: 2
- }
- radius: 10
- width: 100; height: 100
- Text {
- anchors {
- fill: parent
- margins: 5
- }
- horizontalAlignment: Text.AlignHCenter
- text: "Move yellow this direction"
- verticalAlignment: Text.AlignVCenter
- wrapMode: Text.WordWrap
- }
- }
- QtObject {
- id: internal
- /// TODO: Report Qt Creator bug
- property int columnCount: {1:1,2:2,3:2,4:2,5:3,6:3}[gridModel.count]
- property int rowCount: Math.ceil(gridModel.count / columnCount)
- property int lastRowItemIndex: {1:6,2:6,3:2,4:6,5:3,6:6}[gridModel.count]
- property int __qtCreatorFix: 0 // Fix Qt Creator autoindent
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment