2
* Copyright 2015 Canonical Ltd.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation; version 3.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18
import Ubuntu.Components 1.2
19
import Ubuntu.Components.ListItems 1.0
20
import QtQml.Models 2.1
27
property bool restrictDragging: true
33
property list<Action> contextualActions: [
41
iconName: "stock_website"
46
title: "Dragging with ListModel"
49
onEnabledChanged: print("enabled", enabled)
51
ViewItems.selectMode: ViewItems.dragMode
52
ViewItems.onSelectedIndicesChanged: print(ViewItems.selectedIndices)
54
function handleDragStarted(event) {
55
if (!restrictDragging) {
60
// the first 3 items are not draggable
62
} else if (event.from >=3 && event.from <= 10) {
63
// dragging is limited between index 3 and 10
64
event.minimumIndex = 3;
65
event.maximumIndex = 10;
67
// prevent dragging above index 10
68
event.minimumIndex = 11;
71
function handleDragUpdates(event) {
72
if (restrictDragging) {
73
// deal only with indexes >= 3
74
if (event.to >= 3 && event.to <= 10 && event.from >= 3 && event.from <= 10) {
76
model.move(event.from, event.to, 1);
77
} else if (event.status == ListItemDrag.Dropped) {
78
model.move(event.from, event.to, 1);
83
// no restrictions, perform live update
84
model.move(event.from, event.to, 1);
88
ViewItems.onDragUpdated: {
89
if (event.status == ListItemDrag.Started) {
90
handleDragStarted(event);
92
handleDragUpdates(event);
97
Component.onCompleted: {
98
for (var i = 0; i < 3; i++) {
99
append({label: "List item #"+i, sectionData: "Locked"});
101
for (i = 3; i < 11; i++) {
102
append({label: "List item #"+i, sectionData: "Limited, live move"});
104
for (i = 11; i < 25; i++) {
105
append({label: "List item #"+i, sectionData: "Unlimited, drag'n'drop"});
111
property: "sectionData"
112
criteria: ViewSection.FullString
118
moveDisplaced: Transition {
119
UbuntuNumberAnimation {
126
objectName: "ListItem-" + index
127
leadingActions: ListItemActions {
128
actions: deleteAction
130
trailingActions: ListItemActions {
131
actions: contextualActions
136
color: item.dragging ? UbuntuColors.blue : "#69aa69"
139
text: label + " from index #" + index
143
print("entering/leaving draggable mode")
144
ListView.view.ViewItems.dragMode = !ListView.view.ViewItems.dragMode;
151
title: "Using DelegateModel"
155
model: DelegateModel {
156
model: ["apple", "pear", "plum", "peach", "nuts", "dates"]
157
delegate: ListItem { Label { text: modelData } onPressAndHold: dragMode = !dragMode; }
159
ViewItems.onDragUpdated: {
160
if (event.status == ListItemDrag.Moving) {
162
} else if (event.status == ListItemDrag.Dropped) {
163
var fromData = model.model[event.from];
164
var list = model.model;
165
list.splice(event.from, 1);
166
list.splice(event.to, 0, fromData);