| 1 |
/* |
| 2 |
* Copyright (C) 2012-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 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 General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
import QtQuick 2.4 |
| 18 |
|
| 19 |
VisualDataModel { |
| 20 |
id: visualModel |
| 21 |
|
| 22 |
groups: [ |
| 23 |
VisualDataGroup { |
| 24 |
id: selectedGroup |
| 25 |
|
| 26 |
name: "selected" |
| 27 |
} |
| 28 |
] |
| 29 |
|
| 30 |
property Item currentSwipedItem: null |
| 31 |
property alias selectedItems: selectedGroup |
| 32 |
// This property holds if the selection will accept multiple items or single items |
| 33 |
property bool multipleSelection: true |
| 34 |
|
| 35 |
// This property holds a list with the index of selected items |
| 36 |
property string state: "" |
| 37 |
readonly property bool isInSelectionMode: state === "selection" |
| 38 |
|
| 39 |
function updateSwipeState(item) { |
| 40 |
if (item.swipping) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
if (item.swipeState !== "Normal") { |
| 45 |
if (currentSwipedItem !== item) { |
| 46 |
if (currentSwipedItem) { |
| 47 |
currentSwipedItem.resetSwipe(); |
| 48 |
} |
| 49 |
currentSwipedItem = item; |
| 50 |
} else if (item.swipeState !== "Normal" && currentSwipedItem === item) { |
| 51 |
currentSwipedItem = null; |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
/*! |
| 56 |
This handler is called when the selection mode is finished without be canceled |
| 57 |
*/ |
| 58 |
signal selectionDone(var items) |
| 59 |
/*! |
| 60 |
This handler is called when the selection mode is canceled |
| 61 |
*/ |
| 62 |
signal selectionCanceled() |
| 63 |
|
| 64 |
/*! |
| 65 |
Start the selection mode on the list view. |
| 66 |
*/ |
| 67 |
function startSelection() { |
| 68 |
state = "selection"; |
| 69 |
mainHeader.visible = true; |
| 70 |
} |
| 71 |
/*! |
| 72 |
Check if the item is selected |
| 73 |
Returns true if the item was marked as selected or false if the item is unselected |
| 74 |
*/ |
| 75 |
function isSelected(item) { |
| 76 |
if (item && item.VisualDataModel) { |
| 77 |
return (item.VisualDataModel.inSelected === true); |
| 78 |
} else { |
| 79 |
return false; |
| 80 |
} |
| 81 |
} |
| 82 |
/*! |
| 83 |
Mark the item as selected |
| 84 |
Returns true if the item was marked as selected or false if the item is already selected |
| 85 |
*/ |
| 86 |
function selectItem(item) { |
| 87 |
if (item.VisualDataModel.inSelected) { |
| 88 |
return false; |
| 89 |
} else { |
| 90 |
if (!multipleSelection) { |
| 91 |
clearSelection(); |
| 92 |
} |
| 93 |
item.VisualDataModel.inSelected = true; |
| 94 |
return true; |
| 95 |
} |
| 96 |
} |
| 97 |
/*! |
| 98 |
Remove the index from the selected list |
| 99 |
*/ |
| 100 |
function deselectItem(item) { |
| 101 |
var result = false; |
| 102 |
if (item.VisualDataModel.inSelected) { |
| 103 |
item.VisualDataModel.inSelected = false; |
| 104 |
result = true; |
| 105 |
} |
| 106 |
return result; |
| 107 |
} |
| 108 |
/*! |
| 109 |
Finish the selection mode with sucess |
| 110 |
*/ |
| 111 |
function endSelection() { |
| 112 |
selectionDone(visualModel.selectedItems); |
| 113 |
clearSelection(); |
| 114 |
state = ""; |
| 115 |
} |
| 116 |
/*! |
| 117 |
Cancel the selection |
| 118 |
*/ |
| 119 |
function cancelSelection() { |
| 120 |
selectionCanceled(); |
| 121 |
clearSelection(); |
| 122 |
state = ""; |
| 123 |
} |
| 124 |
/*! |
| 125 |
Remove any selected item from the selection list |
| 126 |
*/ |
| 127 |
function clearSelection() { |
| 128 |
if (selectedItems.count > 0) { |
| 129 |
selectedItems.remove(0, selectedItems.count); |
| 130 |
} |
| 131 |
} |
| 132 |
/*! |
| 133 |
Select all items in the list |
| 134 |
*/ |
| 135 |
function selectAll() { |
| 136 |
if (multipleSelection && selectedItems.count < visualModel.items.count) { |
| 137 |
visualModel.items.addGroups(0, visualModel.items.count, ["selected"] ); |
| 138 |
} else { |
| 139 |
clearSelection(); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
function getModelIndexFromIndex(index) { |
| 144 |
return visualModel.modelIndex(index); |
| 145 |
} |
| 146 |
} |