| 1 |
/* |
| 2 |
* Copyright 2015 Canonical Ltd. |
| 3 |
* |
| 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. |
| 7 |
* |
| 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. |
| 12 |
* |
| 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/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
import QtQuick 2.4 |
| 18 |
import Lomiri.Components 1.3 |
| 19 |
import QtQuick.Layouts 1.1 |
| 20 |
import Lomiri.Components.ListItems 1.3 |
| 21 |
import QtQml.Models 2.1 |
| 22 |
|
| 23 |
MainView { |
| 24 |
id: main |
| 25 |
width: units.gu(40) |
| 26 |
height: units.gu(71) |
| 27 |
|
| 28 |
property bool restrictDragging: true |
| 29 |
|
| 30 |
LayoutMirroring.childrenInherit: true |
| 31 |
|
| 32 |
Action { |
| 33 |
id: deleteAction |
| 34 |
iconName: "delete" |
| 35 |
} |
| 36 |
property list<Action> contextualActions: [ |
| 37 |
Action { |
| 38 |
iconName: "edit" |
| 39 |
}, |
| 40 |
Action { |
| 41 |
iconName: "share" |
| 42 |
}, |
| 43 |
Action { |
| 44 |
iconName: "stock_website" |
| 45 |
} |
| 46 |
] |
| 47 |
Tabs { |
| 48 |
Tab { |
| 49 |
title: "Dragging with ListModel" |
| 50 |
page: Page { |
| 51 |
ListView { |
| 52 |
onEnabledChanged: print("enabled", enabled) |
| 53 |
anchors.fill: parent |
| 54 |
ViewItems.selectMode: ViewItems.dragMode |
| 55 |
ViewItems.onSelectedIndicesChanged: print(ViewItems.selectedIndices) |
| 56 |
|
| 57 |
function handleDragStarted(event) { |
| 58 |
if (!restrictDragging) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
if (event.from < 3) { |
| 63 |
// the first 3 items are not draggable |
| 64 |
event.accept = false; |
| 65 |
} else if (event.from >=3 && event.from <= 10) { |
| 66 |
// dragging is limited between index 3 and 10 |
| 67 |
event.minimumIndex = 3; |
| 68 |
event.maximumIndex = 10; |
| 69 |
} else { |
| 70 |
// prevent dragging above index 10 |
| 71 |
event.minimumIndex = 11; |
| 72 |
} |
| 73 |
} |
| 74 |
function handleDragUpdates(event) { |
| 75 |
if (restrictDragging) { |
| 76 |
// deal only with indexes >= 3 |
| 77 |
if (event.to >= 3 && event.to <= 10 && event.from >= 3 && event.from <= 10) { |
| 78 |
// live update |
| 79 |
model.move(event.from, event.to, 1); |
| 80 |
} else if (event.status == ListItemDrag.Dropped) { |
| 81 |
model.move(event.from, event.to, 1); |
| 82 |
} else { |
| 83 |
event.accept = false; |
| 84 |
} |
| 85 |
} else { |
| 86 |
// no restrictions, perform live update |
| 87 |
model.move(event.from, event.to, 1); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
ViewItems.onDragUpdated: { |
| 92 |
if (event.status == ListItemDrag.Started) { |
| 93 |
handleDragStarted(event); |
| 94 |
} else { |
| 95 |
handleDragUpdates(event); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
model: ListModel { |
| 100 |
Component.onCompleted: { |
| 101 |
for (var i = 0; i < 3; i++) { |
| 102 |
append({label: "List item #"+i, sectionData: "Locked"}); |
| 103 |
} |
| 104 |
for (i = 3; i < 11; i++) { |
| 105 |
append({label: "List item #"+i, sectionData: "Limited, live move"}); |
| 106 |
} |
| 107 |
for (i = 11; i < 25; i++) { |
| 108 |
append({label: "List item #"+i, sectionData: "Unlimited, drag'n'drop"}); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
section { |
| 114 |
property: "sectionData" |
| 115 |
criteria: ViewSection.FullString |
| 116 |
delegate: Header { |
| 117 |
text: section |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
moveDisplaced: Transition { |
| 122 |
LomiriNumberAnimation { |
| 123 |
properties: "x,y" |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
delegate: ListItem { |
| 128 |
id: item |
| 129 |
objectName: "ListItem-" + index |
| 130 |
leadingActions: ListItemActions { |
| 131 |
actions: deleteAction |
| 132 |
} |
| 133 |
trailingActions: ListItemActions { |
| 134 |
actions: contextualActions |
| 135 |
} |
| 136 |
onSelectedChanged: print(selected) |
| 137 |
|
| 138 |
RowLayout { |
| 139 |
anchors { |
| 140 |
fill: parent |
| 141 |
margins: units.gu(0.5) |
| 142 |
leftMargin: anchors.rightMargin |
| 143 |
rightMargin: units.gu(2) |
| 144 |
} |
| 145 |
Captions { |
| 146 |
id: captions |
| 147 |
title.text: label |
| 148 |
subtitle.text: "from index #" + index |
| 149 |
} |
| 150 |
Captions { |
| 151 |
captionStyle: Lomiri.SummaryCaptionStyle |
| 152 |
title.text: "LTR" |
| 153 |
subtitle.text: "RTL" |
| 154 |
MouseArea { |
| 155 |
anchors.fill: parent |
| 156 |
onClicked: item.selected = !item.selected |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
onPressAndHold: { |
| 162 |
print("entering/leaving draggable mode") |
| 163 |
ListView.view.ViewItems.dragMode = !ListView.view.ViewItems.dragMode; |
| 164 |
} |
| 165 |
onClicked: main.LayoutMirroring.enabled = !main.LayoutMirroring.enabled |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
Tab { |
| 171 |
title: "Using DelegateModel" |
| 172 |
page: Page { |
| 173 |
ListView { |
| 174 |
anchors.fill: parent |
| 175 |
model: DelegateModel { |
| 176 |
model: ["apple", "pear", "plum", "peach", "nuts", "dates"] |
| 177 |
delegate: ListItem { Label { text: modelData } onPressAndHold: dragMode = !dragMode; } |
| 178 |
} |
| 179 |
ViewItems.onDragUpdated: { |
| 180 |
if (event.status == ListItemDrag.Moving) { |
| 181 |
event.accept = false |
| 182 |
} else if (event.status == ListItemDrag.Dropped) { |
| 183 |
var fromData = model.model[event.from]; |
| 184 |
var list = model.model; |
| 185 |
list.splice(event.from, 1); |
| 186 |
list.splice(event.to, 0, fromData); |
| 187 |
model.model = list; |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
} |