Debian Codegraph @

rc/qml/Components/Checkerboard.qml

raw
1 /*
2 * Copyright (C) 2011-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 version 3 as
6 * published by the Free Software Foundation.
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 * Authors:
17 * Jim Nelson <[email protected]>
18 * Lucas Beeler <[email protected]>
19 * Charles Lindsay <[email protected]>
20 */
21
22 import QtQuick 2.9
23 import Lomiri.Components 1.3
24 import "../../js/Gallery.js" as Gallery
25 import "../../js/GalleryUtility.js" as GalleryUtility
26 import "../Utility"
27
28 /*!
29 */
30 Page {
31 id: checkerboard
32
33 /*!
34 */
35 signal activated(variant object, variant objectModel, variant activatedRect, Item activatingItem)
36
37 /*!
38 */
39 signal movementStarted()
40 /*!
41 */
42 signal movementEnded()
43 /*!
44 */
45 signal hidden(int currScrollPos)
46
47 /*!
48 */
49 property alias model: grid.model
50 /*!
51 */
52 property alias delegate: grid.delegate
53
54 /*!
55 */
56 property alias contentX: grid.contentX
57 /*!
58 */
59 property alias contentY: grid.contentY
60 /*!
61 */
62 property alias footer: grid.footer
63
64 /*!
65 */
66 property real itemWidth
67 /*!
68 */
69 property real itemHeight
70 /*!
71 */
72 property real minGutterWidth
73 /*!
74 */
75 property real minGutterHeight
76 /*!
77 */
78 property real topExtraGutter: 0
79 /*!
80 */
81 property real bottomExtraGutter: 0
82 /*!
83 */
84 property real leftExtraGutter: 0
85 /*!
86 */
87 property real rightExtraGutter: 0
88
89 /*!
90 */
91 property SelectionState selection
92
93 // readonly
94 /*!
95 */
96 property real delegateWidth: itemWidth + gutterWidth
97 /*!
98 */
99 property real delegateHeight: itemHeight + gutterHeight
100
101 // internal
102 /*!
103 */
104 property real itemAreaWidth: width - leftExtraGutter - rightExtraGutter
105 /*!
106 */
107 property real minDelegateWidth: itemWidth + minGutterWidth
108 /*!
109 */
110 property int columns: Math.max(1, Math.floor(itemAreaWidth / minDelegateWidth))
111 /*!
112 */
113 property real gutterWidth: Math.floor(itemAreaWidth / columns - itemWidth)
114 /*!
115 */
116 property real gutterHeight: minGutterHeight
117
118 /*!
119 */
120 function ensureIndexVisible(index, centered) {
121 grid.positionViewAtIndex(index, centered ? GridView.Center : GridView.Visible);
122 }
123
124 /*!
125 */
126 function setScrollPos(newScrollPos) {
127 contentY = newScrollPos;
128 }
129
130 /*!
131 */
132 function scrollToTop() {
133 gridScroller.restart();
134 }
135
136 // Ensures the view is inside its normal bounds after you've mucked with
137 // things manually.
138 function returnToBounds() {
139 grid.returnToBounds();
140 }
141
142 // Uses black magic to hunt for the delegate instance with the given index.
143 // Returns undefined if there's no currently-instantiated delegate with that
144 // index.
145 function getDelegateInstanceAt(index) {
146 for(var i = 0; i < grid.contentItem.children.length; ++i) {
147 var item = grid.contentItem.children[i];
148 // We have to check for the specific objectName we gave our delegates in
149 // CheckerboardDelegate, since we also get some items that were not our
150 // delegates here.
151 if (item.objectName == "checkerboardDelegate" && item.index == index)
152 return item;
153 }
154 return undefined;
155 }
156
157 /*!
158 */
159 function getRectOfItem(item, relativeTo, adjustForGutter) {
160 var rect = GalleryUtility.getRectRelativeTo(item, relativeTo);
161
162 if (adjustForGutter) {
163 // Now we have to adjust for the gutter inside the delegate.
164 rect.x += gutterWidth / 2;
165 rect.y += gutterHeight / 2;
166 rect.width -= gutterWidth;
167 rect.height -= gutterHeight;
168 }
169
170 return rect;
171 }
172
173 // Uses getDelegateInstanceAt() to get the delegate instance with the given
174 // index, then returns a rect with its coords relative to the given object.
175 function getRectOfItemAt(index, relativeTo) {
176 var item = getDelegateInstanceAt(index);
177
178 return (item) ? getRectOfItem(item, relativeTo, true) : undefined;
179 }
180
181 /*!
182 */
183 function getVisibleDelegates() {
184 return GalleryUtility.getVisibleItems(grid, grid, function(child) {
185 return child.objectName == "checkerboardDelegate"
186 });
187 }
188
189 onVisibleChanged: {
190 if(!visible) {
191 // Tell other parts of the application how far we've scrolled;
192 // used in places where the contentY's of two or more checkerboards
193 // must stay in sync.
194 hidden(contentY);
195 }
196 }
197
198 clip: true
199
200 GridView {
201 id: grid
202 objectName: "grid"
203
204 anchors.fill: parent
205 anchors.topMargin: topExtraGutter
206 anchors.bottomMargin: bottomExtraGutter
207 anchors.leftMargin: leftExtraGutter
208 anchors.rightMargin: rightExtraGutter
209
210 cellWidth: delegateWidth
211 cellHeight: delegateHeight
212 flickDeceleration: 800
213
214 onMovementStarted: checkerboard.movementStarted()
215 onMovementEnded: checkerboard.movementEnded()
216
217 // "Grid-shift" transition
218 displaced: Transition {
219 NumberAnimation {
220 easing.type: Easing.InQuint
221 properties: "x,y"
222 duration: Gallery.FAST_DURATION
223 }
224 }
225 }
226
227 NumberAnimation {
228 id: gridScroller
229
230 target: grid
231 property: "contentY"
232 to: 0
233
234 easing.type: Easing.OutQuad
235 duration: 200
236 }
237 }