| 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 PullToRefresh |
| 22 |
\inqmlmodule Lomiri.Components |
| 23 |
\ingroup lomiri |
| 24 |
\since Lomiri.Components 1.1 |
| 25 |
\brief Pull-to-refresh component for Flickables to reload a model upon pull. |
| 26 |
|
| 27 |
The component provides ability to initiate data model refresh by pulling the |
| 28 |
attached Flickable's content. The refresh can be initiated when the flickable |
| 29 |
content is at its top boundary. By dragging the content further, reaching the |
| 30 |
threshold value defined by the style will initiate the manual refresh by emitting |
| 31 |
the \l refresh signal. The progress of the refresh must be notified to the |
| 32 |
component by defining the completion clause to the \l refreshing property. |
| 33 |
|
| 34 |
\qml |
| 35 |
import QtQuick 2.4 |
| 36 |
import QtQuick.XmlListModel 2.0 |
| 37 |
import Lomiri.Components 1.2 |
| 38 |
import Lomiri.Components.ListItems 1.0 |
| 39 |
|
| 40 |
MainView { |
| 41 |
width: units.gu(40) |
| 42 |
height: units.gu(71) |
| 43 |
|
| 44 |
XmlListModel { |
| 45 |
id: listModel |
| 46 |
source: "http://feeds.reuters.com/reuters/topNews" |
| 47 |
query: "/rss/channel/item" |
| 48 |
XmlRole { name: "title"; query: "title/string()" } |
| 49 |
} |
| 50 |
|
| 51 |
Page { |
| 52 |
title: "Reuters" |
| 53 |
ListView { |
| 54 |
id: view |
| 55 |
anchors.fill: parent |
| 56 |
model: listModel |
| 57 |
delegate: Standard { |
| 58 |
width: ListView.view.width |
| 59 |
height: units.gu(5) |
| 60 |
text: title |
| 61 |
} |
| 62 |
PullToRefresh { |
| 63 |
refreshing: view.model.status === XmlListModel.Loading |
| 64 |
onRefresh: view.model.reload() |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
\endqml |
| 70 |
|
| 71 |
\note LomiriListView has a built-in PullToRefresh, therefore it is recommended |
| 72 |
to use LomiriListView instead of ListView. |
| 73 |
|
| 74 |
The component will also show the progress of the model's update when the refresh |
| 75 |
gets initiated by the model or from other party. Style implementations can decide |
| 76 |
whether to visualize that or not. |
| 77 |
|
| 78 |
As default, the component displays a Label visualizing the two states of the |
| 79 |
component, which is pull to refresh and release to refresh. As mentioned, this |
| 80 |
is driven by the threshold value specified by the style, and the state is reported |
| 81 |
by the \l releaseToRefresh property. The \l content specifies the visuals |
| 82 |
to be shown by the component. Custom implementations can hold any component, |
| 83 |
which will be anchor filled to the component itself. |
| 84 |
\qml |
| 85 |
import QtQuick 2.4 |
| 86 |
import QtQuick.XmlListModel 2.0 |
| 87 |
import Lomiri.Components 1.2 |
| 88 |
import Lomiri.Components.ListItems 1.0 |
| 89 |
|
| 90 |
MainView { |
| 91 |
width: units.gu(40) |
| 92 |
height: units.gu(71) |
| 93 |
|
| 94 |
XmlListModel { |
| 95 |
id: listModel |
| 96 |
source: "http://feeds.reuters.com/reuters/topNews" |
| 97 |
query: "/rss/channel/item" |
| 98 |
XmlRole { name: "title"; query: "title/string()" } |
| 99 |
} |
| 100 |
|
| 101 |
Page { |
| 102 |
title: "Reuters" |
| 103 |
ListView { |
| 104 |
id: view |
| 105 |
anchors.fill: parent |
| 106 |
model: listModel |
| 107 |
delegate: Standard { |
| 108 |
width: ListView.view.width |
| 109 |
height: units.gu(5) |
| 110 |
text: title |
| 111 |
} |
| 112 |
PullToRefresh { |
| 113 |
id: pullToRefresh |
| 114 |
refreshing: view.model.status === XmlListModel.Loading |
| 115 |
onRefresh: view.model.reload() |
| 116 |
content: Item { |
| 117 |
Icon { |
| 118 |
name: pullToRefresh.releaseToRefresh ? "search" : "" |
| 119 |
height: parent.height |
| 120 |
width: height |
| 121 |
anchors.horizontalCenter: parent.horizontalCenter |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
\endqml |
| 129 |
|
| 130 |
\note When declared as child of Flickable, set parent to the flickable explicitly |
| 131 |
so the component does not land in the \c content of Flickable. |
| 132 |
\qml |
| 133 |
import QtQuick 2.4 |
| 134 |
import QtQuick.XmlListModel 2.0 |
| 135 |
import Lomiri.Components 1.2 |
| 136 |
import Lomiri.Components.ListItems 1.0 |
| 137 |
|
| 138 |
MainView { |
| 139 |
id: main |
| 140 |
width: units.gu(40) |
| 141 |
height: units.gu(71) |
| 142 |
|
| 143 |
XmlListModel { |
| 144 |
id: rssFeed |
| 145 |
source: "http://feeds.reuters.com/reuters/topNews" |
| 146 |
query: "/rss/channel/item" |
| 147 |
XmlRole { name: "title"; query: "title/string()" } |
| 148 |
} |
| 149 |
Page { |
| 150 |
title: "Reuters" |
| 151 |
Flickable { |
| 152 |
id: flickable |
| 153 |
anchors.fill: parent |
| 154 |
contentHeight: column.childrenRect.height |
| 155 |
contentWidth: column.childrenRect.width |
| 156 |
Column { |
| 157 |
id: column |
| 158 |
Repeater { |
| 159 |
model: rssFeed |
| 160 |
Standard { |
| 161 |
width: main.width |
| 162 |
height: units.gu(5) |
| 163 |
text: title |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
PullToRefresh { |
| 169 |
parent: flickable |
| 170 |
refreshing: rssFeed.status === XmlListModel.Loading |
| 171 |
onRefresh: rssFeed.reload() |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
\endqml |
| 177 |
|
| 178 |
\section2 Styling |
| 179 |
The component style API is defined by the \l PullToRefreshStyle component. |
| 180 |
Styles may define different ways to initiate refresh upon dragging. |
| 181 |
*/ |
| 182 |
|
| 183 |
StyledItem { |
| 184 |
id: control |
| 185 |
|
| 186 |
/*! |
| 187 |
The property specifies when the component is ready to trigger the refresh() |
| 188 |
signal. The logic is defined by the style and the value is transferred |
| 189 |
from the style's releaseToRefresh property. The property can be used to |
| 190 |
define custom visuals for \l content. |
| 191 |
*/ |
| 192 |
readonly property bool releaseToRefresh: __styleInstance.releaseToRefresh |
| 193 |
|
| 194 |
/*! |
| 195 |
The property holds the offset the component is pulled from the \l target |
| 196 |
Flickable's \a topMargin. The property can be used to provide animations |
| 197 |
in custom contents. |
| 198 |
*/ |
| 199 |
readonly property real offset: -(target.topMargin - (y + height)) |
| 200 |
|
| 201 |
/*! |
| 202 |
The property holds the visuals to be displayed when the component is revealed |
| 203 |
upon manual refresh. The default value is a Label showing "Pull to refresh..." |
| 204 |
text when the component is pulled down till the activation threshold, and |
| 205 |
"Release to refresh..." after that. |
| 206 |
*/ |
| 207 |
property Component content: __styleInstance.defaultContent |
| 208 |
|
| 209 |
/*! |
| 210 |
The Flickable or derivate the component is attached to. This can only be |
| 211 |
the parent or a sibling of the component. Defaults to the parent. |
| 212 |
*/ |
| 213 |
property Flickable target: parent |
| 214 |
|
| 215 |
/*! |
| 216 |
The property notifies the component about the ongoing refresh operation. |
| 217 |
*/ |
| 218 |
property bool refreshing: false |
| 219 |
|
| 220 |
/*! |
| 221 |
Signal emitted when the model refresh is initiated by the component. |
| 222 |
*/ |
| 223 |
signal refresh() |
| 224 |
|
| 225 |
style: Theme.createStyleComponent("PullToRefreshStyle.qml", control) |
| 226 |
implicitHeight: __styleInstance.implicitHeight |
| 227 |
anchors { |
| 228 |
left: target.left |
| 229 |
right: target.right |
| 230 |
} |
| 231 |
y: -(internals.contentY + height) |
| 232 |
|
| 233 |
QtObject { |
| 234 |
id: internals |
| 235 |
property bool completed: false |
| 236 |
property real contentY: target.contentY - target.originY |
| 237 |
} |
| 238 |
|
| 239 |
Component.onCompleted: { |
| 240 |
internals.completed = true; |
| 241 |
} |
| 242 |
} |