Advertisement
Guest User

Untitled

a guest
May 23rd, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. import QtQuick 1.1
  2.  
  3. Rectangle {
  4. width: 800
  5. height: 600
  6. id: root
  7.  
  8. // MODEL
  9.  
  10. ListModel {
  11. id: leftModel
  12. ListElement {
  13. iColor: "red"
  14. }
  15. ListElement {
  16. iColor: "blue"
  17. }
  18. ListElement {
  19. iColor: "green"
  20. }
  21. ListElement {
  22. iColor: "yellow"
  23. }
  24. ListElement {
  25. iColor: "pink"
  26. }
  27. ListElement {
  28. iColor: "orange"
  29. }
  30. ListElement {
  31. iColor: "brown"
  32. }
  33. ListElement {
  34. iColor: "gray"
  35. }
  36. }
  37.  
  38. // VIEW
  39.  
  40. ListView {
  41. id: dataView
  42. model: leftModel
  43. delegate: activity
  44. height: leftModel.count * 50
  45. width: 100
  46. x: 200
  47. interactive: false
  48.  
  49. Rectangle
  50. {
  51. id: dropPlaceholder
  52. color: "transparent"
  53. width: parent.width
  54. height: 50
  55.  
  56. Rectangle
  57. {
  58. anchors.centerIn: parent
  59. width: parent.width * 0.8
  60. height: parent.height * 0.8
  61. border.width: 2
  62. border.color: "black"
  63. }
  64.  
  65. opacity: 0.0
  66. }
  67. }
  68.  
  69. // CONTROL
  70.  
  71. MouseArea
  72. {
  73. anchors.fill: dataView
  74. hoverEnabled: true
  75. drag.axis: Drag.YAxis
  76. drag.target: null
  77. z: 10
  78.  
  79. property bool mousePressed: false
  80. property int xPos: 0
  81. property int yPos: 0
  82. property int lastMouseX: 0
  83. property int lastMouseY: 0
  84. property int itemHeight: 0
  85. property int currentIndex: 0
  86. property Item currentItem: null
  87.  
  88. onPressed:
  89. {
  90. mousePressed = true
  91. currentItem.z = 2
  92.  
  93. // Make the dropplaceholder visible
  94. dropPlaceholder.x = xPos
  95. dropPlaceholder.y = yPos
  96. dropPlaceholder.opacity = 0.5
  97. }
  98.  
  99. onReleased:
  100. {
  101. mousePressed = false
  102.  
  103. currentItem.x = xPos
  104. currentItem.y = yPos
  105. currentItem.z = 1
  106. dropPlaceholder.opacity = 0.0
  107. }
  108.  
  109. onPositionChanged:
  110. {
  111. // Get the current index based on the mouse position.
  112. var tempIndex = dataView.indexAt(mouseX, mouseY)
  113.  
  114.  
  115. if(!mousePressed)
  116. {
  117. console.log("X: " + mouseX + " Y: " + mouseY)
  118.  
  119.  
  120. // Set the current index in the ListView so that we can get the item that coresponds this index.
  121. currentIndex = tempIndex
  122. dataView.currentIndex = tempIndex
  123.  
  124. // Get the item coresponding the index (yeah, quite a detour to get here)
  125. currentItem = dataView.currentItem
  126. dataView.currentIndex = -1
  127.  
  128. // Store the default position. We restore this position when we release the mouse or leave the mousearea.
  129. xPos = currentItem.x
  130. yPos = currentItem.y
  131. itemHeight = currentItem.height
  132.  
  133. // Set our current item as the drag target.
  134. drag.target = currentItem
  135.  
  136. console.log(currentItem)
  137. }
  138. else
  139. {
  140. var halfOfObjHeight = Math.floor(currentItem.height / 2);
  141. var mouseLeftoverYPos = mouseY % itemHeight;
  142. var mouseDirection = 0; // 0 = down. 1 = up
  143. var updatePlaceholderElement = false;
  144. //console.log("Mouse leftover Y position: " + mouseLeftoverYPos)
  145.  
  146. if(lastMouseY >= mouseY)
  147. {
  148. // We're going up.
  149. mouseDirection = 1
  150. }
  151.  
  152. if(mouseDirection === 1 && mouseLeftoverYPos <= halfOfObjHeight)
  153. {
  154. updatePlaceholderElement = true
  155.  
  156. }
  157. else if(mouseDirection === 0 && mouseLeftoverYPos >= halfOfObjHeight)
  158. {
  159. updatePlaceholderElement = true
  160. }
  161.  
  162.  
  163. //console.log("Index:" + currentIndex + " Hover Index: " + tempIndex)
  164.  
  165. if(updatePlaceholderElement && currentIndex !== tempIndex)
  166. {
  167. dataView.currentIndex = tempIndex
  168. var subCurrentItem = dataView.currentItem
  169. dataView.currentIndex = -1
  170.  
  171. dropPlaceholder.x = subCurrentItem.x
  172. dropPlaceholder.y = subCurrentItem.y
  173. console.log("Update placeholder...")
  174. }
  175.  
  176. }
  177.  
  178. lastMouseX = mouseX
  179. lastMouseY = mouseY
  180.  
  181. }
  182.  
  183. }
  184.  
  185. Component {
  186. id: activity
  187. Rectangle
  188. {
  189. id: rect
  190. color: iColor
  191. width: 100
  192. height: 50
  193. z: 0
  194. }
  195. }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement