| 1 |
/* |
| 2 |
* Copyright 2014 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.2 as Toolkit |
| 19 |
|
| 20 |
/*! |
| 21 |
\qmltype LomiriListView |
| 22 |
\inqmlmodule Lomiri.Components |
| 23 |
\ingroup lomiri |
| 24 |
\brief A ListView with special features tailored for a look and feel fitting the |
| 25 |
Lomiri Touch platform. |
| 26 |
The LomiriListView works just like a regular ListView, but it adds special features |
| 27 |
such as expanding/collapsing items (when used together with the Expandable item). |
| 28 |
It provides features like automatically positioning the expanding item when it |
| 29 |
expands and collapsing it again when the user taps outside of it. |
| 30 |
|
| 31 |
Examples: |
| 32 |
\qml |
| 33 |
import Lomiri.Components 1.2 |
| 34 |
import Lomiri.Components.ListItems 1.0 as ListItem |
| 35 |
|
| 36 |
Item { |
| 37 |
ListModel { |
| 38 |
id: listModel |
| 39 |
} |
| 40 |
|
| 41 |
LomiriListView { |
| 42 |
id: lomiriListView |
| 43 |
anchors { left: parent.left; right: parent.right } |
| 44 |
height: units.gu(24) |
| 45 |
model: listModel |
| 46 |
|
| 47 |
delegate: ListItem.Expandable { |
| 48 |
id: expandingItem |
| 49 |
|
| 50 |
expandedHeight: units.gu(30) |
| 51 |
|
| 52 |
onClicked: { |
| 53 |
lomiriListView.expandedIndex = index; |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
\endqml |
| 59 |
*/ |
| 60 |
|
| 61 |
ListView { |
| 62 |
id: root |
| 63 |
|
| 64 |
/*! |
| 65 |
The index of the currently expanded item. -1 if no item is expanded. |
| 66 |
*/ |
| 67 |
property int expandedIndex: -1 |
| 68 |
|
| 69 |
QtObject { |
| 70 |
id: priv |
| 71 |
|
| 72 |
function positionViewAtIndexAnimated(expandedIndex) { |
| 73 |
animation.from = root.contentY; |
| 74 |
root.currentIndex = expandedIndex; |
| 75 |
if (expandedIndex == root.count - 1) { |
| 76 |
root.positionViewAtIndex(expandedIndex, ListView.End); |
| 77 |
} else { |
| 78 |
root.positionViewAtIndex(expandedIndex + 1, ListView.End); |
| 79 |
} |
| 80 |
|
| 81 |
var effectiveExpandedHeight = Math.min(root.currentItem.expandedHeight, root.height - root.currentItem.collapsedHeight); |
| 82 |
if (root.contentY - root.originY == 0) { |
| 83 |
if (((root.currentIndex + 1) * root.currentItem.collapsedHeight) + effectiveExpandedHeight > root.height) { |
| 84 |
animation.to = ((root.currentIndex + 1) * root.currentItem.collapsedHeight + effectiveExpandedHeight) - root.height + root.originY |
| 85 |
} else { |
| 86 |
animation.to = root.originY |
| 87 |
} |
| 88 |
} else { |
| 89 |
animation.to = root.contentY + (effectiveExpandedHeight - root.currentItem.collapsedHeight); |
| 90 |
} |
| 91 |
animation.start(); |
| 92 |
} |
| 93 |
|
| 94 |
function requestFocus(reason) { |
| 95 |
// lookup for the currentItem, and if it is a FocusScope, focus the view |
| 96 |
// this will also focus the currentItem |
| 97 |
if (root.currentItem && root.currentItem.hasOwnProperty("activeFocusOnPress")) { |
| 98 |
root.forceActiveFocus(reason); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
focus: true |
| 104 |
|
| 105 |
/*! |
| 106 |
\internal |
| 107 |
Grab focus when moved, flicked or clicked |
| 108 |
*/ |
| 109 |
onMovementStarted: priv.requestFocus(Qt.MouseFocusReason) |
| 110 |
onFlickStarted: priv.requestFocus(Qt.MouseFocusReason) |
| 111 |
Toolkit.Mouse.onClicked: priv.requestFocus(Qt.MouseFocusReason) |
| 112 |
|
| 113 |
/*! |
| 114 |
Expand the item at the given index. |
| 115 |
*/ |
| 116 |
onExpandedIndexChanged: { |
| 117 |
if (expandedIndex < 0) { |
| 118 |
return; |
| 119 |
} |
| 120 |
priv.positionViewAtIndexAnimated(expandedIndex, ListView.End) |
| 121 |
} |
| 122 |
|
| 123 |
LomiriNumberAnimation { |
| 124 |
id: animation |
| 125 |
target: root |
| 126 |
property: "contentY" |
| 127 |
} |
| 128 |
|
| 129 |
MouseArea { |
| 130 |
parent: contentItem |
| 131 |
anchors.fill: parent |
| 132 |
z: 2 |
| 133 |
enabled: root.expandedIndex != -1 |
| 134 |
onClicked: root.expandedIndex = -1; |
| 135 |
} |
| 136 |
|
| 137 |
// animate move displaced |
| 138 |
moveDisplaced: Transition { |
| 139 |
LomiriNumberAnimation { |
| 140 |
properties: "x,y" |
| 141 |
} |
| 142 |
} |
| 143 |
} |