@@ -2,20 +2,34 @@ import QtQuick 2.4
22import Ubuntu.Components 1.3
33import "MathUtils.js" as MathUtils
44
5−Item {
5+Rectangle {
66 id: root
77 implicitWidth: listView.count * (height * 16/9)
8+ color: "blue"
89
910 property var screen: null
1011 property var background // TODO: should be stored in the workspace data
1112
13+// Rectangle { anchors.fill: parent; color: "green" }
1214 ListView {
1315 id: listView
1416 anchors.fill: parent
1517 anchors.leftMargin: -itemWidth
1618 anchors.rightMargin: -itemWidth
19+ interactive: false
20+
21+ model: ListModel {
22+ ListElement {text: "1"}
23+ ListElement {text: "2"}
24+ ListElement {text: "3"}
25+ ListElement {text: "4"}
26+ ListElement {text: "5"}
27+ ListElement {text: "6"}
28+ ListElement {text: "7"}
29+ ListElement {text: "8"}
30+ ListElement {text: "9"}
31+ }
1732
18− model: 5
1933
2034 orientation: ListView.Horizontal
2135 spacing: units.gu(1)
@@ -31,11 +45,11 @@ Item {
3145 delegate: Item {
3246 objectName: "delegate" + index
3347 height: parent.height
34− width: /*dndArea.draggedIndex == index ? units.gu(5) : */listView.itemWidth
48+ width: /*dndArea.draggedIndex == index ? units.gu(5) :*/ listView.itemWidth
3549 Behavior on width { UbuntuNumberAnimation {} }
3650
37− property int itemX: -listView.contentX - listView.leftMargin + index * (listView.itemWidth + listView.spacing)
38− property int distanceFromLeft: itemX
51+ property int itemX: -listView.realContentX + index * (listView.itemWidth + listView.spacing)
52+ property int distanceFromLeft: itemX //- listView.leftMargin
3953 property int distanceFromRight: listView.width - listView.leftMargin - listView.rightMargin - itemX - listView.itemWidth
4054
4155 property int maxAngle: 40
@@ -123,6 +137,79 @@ Item {
123137 background: root.background
124138 screenHeight: screen.physicalSize.height
125139 }
140+
141+ Label {
142+ anchors.centerIn: parent
143+ text: model.text
144+ fontSize: "large"
145+ color: "red"
146+ }
147+ }
148+
149+ MouseArea {
150+ id: dndArea
151+ anchors.fill: parent
152+ hoverEnabled: true
153+ preventStealing: true
154+ anchors.leftMargin: listView.leftMargin
155+ anchors.rightMargin: listView.rightMargin
156+
157+
158+ property int draggedIndex: -1
159+
160+ onMouseXChanged: {
161+ var progress = Math.max(0, Math.min(1, (mouseX - listView.foldingAreaWidth*2) / (width - listView.foldingAreaWidth * 4)))
162+// var progress = mouseX / width
163+// print("p:", progress)
164+ listView.contentX = listView.originX + (listView.contentWidth - listView.width + listView.leftMargin + listView.rightMargin) * progress - listView.leftMargin
165+
166+
167+ if (draggedIndex == -1) {
168+ return;
169+ }
170+
171+// var newIndex = (listView.realContentX) / listView.itemWidth
172+ var coords = mapToItem(listView.contentItem, mouseX + listView.itemWidth, mouseY)
173+ var newIndex = (coords.x + listView.leftMargin) / (listView.itemWidth + listView.spacing)
174+ print("newIndex", newIndex)
175+// var newIndex = listView.indexAt(coords.x, coords.y)
176+// if (newIndex == -1) return;
177+
178+ if (newIndex > draggedIndex + 1) {
179+ newIndex = draggedIndex + 1
180+ } else if (newIndex < draggedIndex) {
181+ newIndex = draggedIndex - 1
182+ } else {
183+ return
184+ }
185+
186+ if (newIndex >= 0 && newIndex < listView.count) {
187+ listView.model.move(draggedIndex, newIndex, 1)
188+ draggedIndex = newIndex
189+ }
190+ }
191+
192+ onReleased: {
193+ draggedIndex = -1
194+ }
195+
196+ onPressAndHold: {
197+// var clickedItem = listView.itemAt(mouseX /*+ listView.realContentX*/, mouseY)
198+// var clickedIndex = listView.indexAt(mouseX /*- listView.realContentX*/, mouseY)
199+
200+ for (var i = 0; i < listView.count; i++) {
201+ var x = -listView.leftMargin + i * listView.itemWidth - units.gu(1);
202+ print("x:", x, "index:", listView.indexAt(x, listView.height / 2))
203+ }
204+
205+ var coords = mapToItem(listView.contentItem, mouseX, mouseY)
206+ var clickedIndex = listView.indexAt(coords.x, coords.y)
207+
208+ draggedIndex = clickedIndex
209+
210+ print("index:", mouseX, clickedIndex)
211+ }
212+
126213 }
127214 }
128215}