| 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 |
| 19 |
|
| 20 |
/*! |
| 21 |
\qmltype ExpandablesColumn |
| 22 |
\inqmlmodule Lomiri.Components.ListItems 1.0 |
| 23 |
\ingroup lomiri-listitems |
| 24 |
\brief A column to be used together with the \l Expandable item. |
| 25 |
This lays out its content just like a regular Column inside a Flickable but |
| 26 |
when used together with items of type \l Expandable it provides additional features |
| 27 |
like automatically positioning the expanding item when it expands and collapsing |
| 28 |
it again when the user taps outside of it. |
| 29 |
|
| 30 |
Examples: |
| 31 |
\qml |
| 32 |
import Lomiri.Components 1.2 |
| 33 |
import Lomiri.Components.ListItems 1.0 as ListItem |
| 34 |
|
| 35 |
ListItem.ExpandablesColumn { |
| 36 |
anchors { left: parent.left; right: parent.right } |
| 37 |
height: units.gu(24) |
| 38 |
|
| 39 |
Repeater { |
| 40 |
model: 8 |
| 41 |
ListItem.Expandable { |
| 42 |
expandedHeight: units.gu(30) |
| 43 |
|
| 44 |
onClicked: { |
| 45 |
expanded = true; |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
\endqml |
| 51 |
*/ |
| 52 |
|
| 53 |
Flickable { |
| 54 |
id: root |
| 55 |
contentHeight: column.height |
| 56 |
|
| 57 |
/*! |
| 58 |
Points to the currently expanded item. Null if no item is expanded. |
| 59 |
\qmlproperty Item expandedItem |
| 60 |
*/ |
| 61 |
readonly property alias expandedItem: priv.expandedItem |
| 62 |
|
| 63 |
/*! |
| 64 |
Expand the given item. The item must be a child of this ListView. |
| 65 |
*/ |
| 66 |
function expandItem(item) { |
| 67 |
if (!item.hasOwnProperty("expandedHeight") || !item.hasOwnProperty("collapsedHeight")) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
if (priv.expandedItem != null) { |
| 72 |
priv.expandedItem.expanded = false; |
| 73 |
} |
| 74 |
priv.expandedItem = item; |
| 75 |
|
| 76 |
var maxExpandedHeight = root.height - item.collapsedHeight; |
| 77 |
var expandedItemHeight = Math.min(item.expandedHeight, maxExpandedHeight); |
| 78 |
var bottomIntersect = root.mapFromItem(item, 0, 0).y + expandedItemHeight - maxExpandedHeight; |
| 79 |
if (bottomIntersect > 0) { |
| 80 |
contentY += bottomIntersect; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/*! |
| 85 |
Collapse the currently expanded item. If there isn't any item expanded, this function does nothing. |
| 86 |
*/ |
| 87 |
function collapse() { |
| 88 |
priv.expandedItem.expanded = false; |
| 89 |
priv.expandedItem = null; |
| 90 |
} |
| 91 |
|
| 92 |
/*! |
| 93 |
Reparent any content to inside the Column. |
| 94 |
\qmlproperty QtObject children |
| 95 |
\default |
| 96 |
*/ |
| 97 |
default property alias children: column.data |
| 98 |
|
| 99 |
/*! \internal */ |
| 100 |
QtObject { |
| 101 |
id: priv |
| 102 |
|
| 103 |
/*! \internal |
| 104 |
Points to the currently expanded item. Null if no item is expanded. |
| 105 |
*/ |
| 106 |
property var expandedItem: null |
| 107 |
} |
| 108 |
|
| 109 |
Behavior on contentY { |
| 110 |
LomiriNumberAnimation { } |
| 111 |
} |
| 112 |
|
| 113 |
Column { |
| 114 |
id: column |
| 115 |
anchors { left: parent.left; right: parent.right } |
| 116 |
} |
| 117 |
|
| 118 |
MouseArea { |
| 119 |
anchors { left: parent.left; right: parent.right; top: parent.top } |
| 120 |
height: root.mapFromItem(priv.expandedItem, 0, 0).y + root.contentY |
| 121 |
enabled: priv.expandedItem != null |
| 122 |
onClicked: root.collapse(); |
| 123 |
} |
| 124 |
|
| 125 |
MouseArea { |
| 126 |
anchors { left: parent.left; right: parent.right; bottom: parent.bottom } |
| 127 |
height: priv.expandedItem != null ? root.contentHeight - (root.mapFromItem(priv.expandedItem, 0, 0).y + root.contentY + priv.expandedItem.height) : 0 |
| 128 |
enabled: priv.expandedItem != null |
| 129 |
onClicked: root.collapse(); |
| 130 |
} |
| 131 |
} |