| 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 |
import QtQuick 2.4 |
| 17 |
import Lomiri.Components 1.3 |
| 18 |
|
| 19 |
/*! |
| 20 |
\qmltype Expandable |
| 21 |
\inqmlmodule Lomiri.Components.ListItems 1.0 |
| 22 |
\ingroup lomiri-listitems |
| 23 |
\brief An expandable list item with no contents. |
| 24 |
The Expandable class can be used for generic list items containing other |
| 25 |
components such as buttons. It subclasses \l Empty and thus brings all that |
| 26 |
functionality, but additionally provides means to expand and collapse the item. |
| 27 |
|
| 28 |
When used together with an \l LomiriListView or \l ExpandablesColumn it |
| 29 |
can coordinate with other items in the list to make sure it is scrolled while |
| 30 |
expanding to be fully visible in the view. Additionally it is made sure that |
| 31 |
only one Expandable item is expanded at a time and it is collapsed when the |
| 32 |
user clicks outside the item. |
| 33 |
|
| 34 |
You can set \l expanded to true/false to expand/collapse the item. |
| 35 |
|
| 36 |
Examples: |
| 37 |
\qml |
| 38 |
import Lomiri.Components 1.3 |
| 39 |
import Lomiri.Components.ListItems 1.3 as ListItem |
| 40 |
|
| 41 |
Item { |
| 42 |
ListModel { |
| 43 |
id: listModel |
| 44 |
} |
| 45 |
|
| 46 |
ListItem.LomiriListView { |
| 47 |
anchors { left: parent.left; right: parent.right } |
| 48 |
height: units.gu(24) |
| 49 |
model: listModel |
| 50 |
|
| 51 |
delegate: ListItem.Expandable { |
| 52 |
id: expandingItem |
| 53 |
|
| 54 |
expandedHeight: units.gu(30) |
| 55 |
|
| 56 |
onClicked: { |
| 57 |
expanded = true; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
\endqml |
| 63 |
*/ |
| 64 |
|
| 65 |
Empty { |
| 66 |
id: root |
| 67 |
implicitHeight: expanded ? priv.maxExpandedHeight : collapsedHeight |
| 68 |
|
| 69 |
/*! |
| 70 |
Reflects the expanded state. Set this to true/false to expand/collapse the item. |
| 71 |
*/ |
| 72 |
property bool expanded: false |
| 73 |
|
| 74 |
/*! |
| 75 |
The collapsed (normal) height of the item. Defaults to the standard height for list items. |
| 76 |
*/ |
| 77 |
property real collapsedHeight: __height |
| 78 |
|
| 79 |
/*! |
| 80 |
The expanded height of the item's content. Defaults to the same as collapsedHeight which |
| 81 |
disables the expanding feature. In order for the item to be expandable, set this to the |
| 82 |
expanded size. Note that the actual expanded size can be smaller if there is not enough |
| 83 |
space in the containing list. In that case the item becomes flickable automatically. |
| 84 |
*/ |
| 85 |
property real expandedHeight: collapsedHeight |
| 86 |
|
| 87 |
/*! |
| 88 |
If set to true, the item will collapse again when the user clicks somewhere in the always |
| 89 |
visible (when collapsed) area. |
| 90 |
*/ |
| 91 |
property bool collapseOnClick: false |
| 92 |
|
| 93 |
/*! |
| 94 |
Reparent any content to inside the Flickable |
| 95 |
\qmlproperty QtObject children |
| 96 |
\default |
| 97 |
*/ |
| 98 |
default property alias children: flickableContent.data |
| 99 |
|
| 100 |
/*! \internal */ |
| 101 |
QtObject { |
| 102 |
id: priv |
| 103 |
|
| 104 |
/*! |
| 105 |
\internal |
| 106 |
Points to the containing ExpandablesListView or ExpandablesColumn |
| 107 |
*/ |
| 108 |
property Item view: root.ListView.view ? root.ListView.view : (root.parent.parent.parent.hasOwnProperty("expandItem") ? root.parent.parent.parent : null) |
| 109 |
|
| 110 |
/*! \internal |
| 111 |
Gives information whether this item is inside an item based container supporting Expandable items, such as ExpandablesColumn |
| 112 |
*/ |
| 113 |
readonly property bool isInExpandableColumn: view && view !== undefined && view.hasOwnProperty("expandItem") && view.hasOwnProperty("collapse") |
| 114 |
|
| 115 |
/*! \internal |
| 116 |
Gives information whether this item is inside an index based container supporting Expandable items, such as LomiriListView |
| 117 |
*/ |
| 118 |
readonly property bool isInExpandableListView: view && view !== undefined && view.hasOwnProperty("expandedIndex") |
| 119 |
|
| 120 |
/*! \internal |
| 121 |
Gives information if there is another item expanded in the containing ExpandablesListView or ExpandablesColumn |
| 122 |
*/ |
| 123 |
readonly property bool otherExpanded: (isInExpandableColumn && view.expandedItem !== null && view.expandedItem !== undefined && view.expandedItem !== root) |
| 124 |
|| (isInExpandableListView && view.expandedIndex !== -1 && view.expandedIndex !== index) |
| 125 |
|
| 126 |
/*! \internal |
| 127 |
Gives information about the maximum expanded height, in case that is limited by the containing ExpandablesListView or ExpandablesColumn |
| 128 |
*/ |
| 129 |
readonly property real maxExpandedHeight: (isInExpandableColumn || isInExpandableListView) ? Math.min(view.height - collapsedHeight, expandedHeight) : expandedHeight |
| 130 |
} |
| 131 |
|
| 132 |
states: [ |
| 133 |
State { |
| 134 |
name: "" |
| 135 |
PropertyChanges { target: root; opacity: 1 } |
| 136 |
}, |
| 137 |
State { |
| 138 |
name: "otherExpanded"; when: priv.otherExpanded |
| 139 |
PropertyChanges { target: root; opacity: .5 } |
| 140 |
}, |
| 141 |
State { |
| 142 |
name: "expanded"; when: expanded |
| 143 |
PropertyChanges { target: root; z: 3 } |
| 144 |
} |
| 145 |
] |
| 146 |
|
| 147 |
Component.onCompleted: { |
| 148 |
if (priv.isInExpandableListView && priv.view.expandedIndex == index) { |
| 149 |
root.expanded = true; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
Connections { |
| 154 |
target: priv.isInExpandableListView ? priv.view : null |
| 155 |
function onExpandedIndexChanged() { |
| 156 |
if (priv.view.expandedIndex == index) { |
| 157 |
root.expanded = true; |
| 158 |
} else if (root.expanded = true) { |
| 159 |
root.expanded = false; |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/*! \internal */ |
| 165 |
onExpandedChanged: { |
| 166 |
if (!expanded) { |
| 167 |
contentFlickable.contentY = 0; |
| 168 |
} |
| 169 |
|
| 170 |
if (priv.isInExpandableColumn) { |
| 171 |
if (expanded) { |
| 172 |
priv.view.expandItem(root); |
| 173 |
} else { |
| 174 |
priv.view.collapse(); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
Behavior on height { |
| 180 |
LomiriNumberAnimation {} |
| 181 |
} |
| 182 |
Behavior on opacity { |
| 183 |
LomiriNumberAnimation {} |
| 184 |
} |
| 185 |
|
| 186 |
Flickable { |
| 187 |
id: contentFlickable |
| 188 |
objectName: "__expandableContentFlickable" |
| 189 |
anchors { fill: parent; leftMargin: root.__contentsMargins; rightMargin: __contentsMargins; bottomMargin: divider.height } |
| 190 |
interactive: root.expanded && contentHeight > height + root.divider.height |
| 191 |
contentHeight: root.expandedHeight |
| 192 |
flickableDirection: Flickable.VerticalFlick |
| 193 |
clip: true |
| 194 |
|
| 195 |
Behavior on contentY { |
| 196 |
LomiriNumberAnimation {} |
| 197 |
} |
| 198 |
|
| 199 |
Item { |
| 200 |
id: flickableContent |
| 201 |
anchors { |
| 202 |
left: parent.left |
| 203 |
right: parent.right |
| 204 |
} |
| 205 |
height: childrenRect.height |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
MouseArea { |
| 210 |
anchors { left: parent.left; top: parent.top; right: parent.right } |
| 211 |
enabled: root.collapseOnClick && root.expanded |
| 212 |
height: root.collapsedHeight |
| 213 |
onClicked: { |
| 214 |
if (priv.isInExpandableListView) { |
| 215 |
priv.view.expandedIndex = -1; |
| 216 |
} else { |
| 217 |
root.expanded = false; |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
} |