| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). |
| 4 |
** Copyright (C) 2014 Canonical Ltda |
| 5 |
** All rights reserved. |
| 6 |
** Contact: Nokia Corporation ([email protected]) |
| 7 |
** |
| 8 |
** This file is part of the Qt Components project. |
| 9 |
** |
| 10 |
** $QT_BEGIN_LICENSE:BSD$ |
| 11 |
** You may use this file under the terms of the BSD license as follows: |
| 12 |
** |
| 13 |
** "Redistribution and use in source and binary forms, with or without |
| 14 |
** modification, are permitted provided that the following conditions are |
| 15 |
** met: |
| 16 |
** * Redistributions of source code must retain the above copyright |
| 17 |
** notice, this list of conditions and the following disclaimer. |
| 18 |
** * Redistributions in binary form must reproduce the above copyright |
| 19 |
** notice, this list of conditions and the following disclaimer in |
| 20 |
** the documentation and/or other materials provided with the |
| 21 |
** distribution. |
| 22 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
| 23 |
** the names of its contributors may be used to endorse or promote |
| 24 |
** products derived from this software without specific prior written |
| 25 |
** permission. |
| 26 |
** |
| 27 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 28 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 29 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 30 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 31 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 32 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 33 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 34 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 35 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 36 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 37 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 38 |
** $QT_END_LICENSE$ |
| 39 |
** |
| 40 |
****************************************************************************/ |
| 41 |
|
| 42 |
// FastScroll.qml |
| 43 |
import QtQuick 2.4 |
| 44 |
import Lomiri.Components 1.3 |
| 45 |
import "FastScroll.js" as Sections |
| 46 |
|
| 47 |
Item { |
| 48 |
id: root |
| 49 |
|
| 50 |
property ListView listView |
| 51 |
property int pinSize: units.gu(2) |
| 52 |
|
| 53 |
readonly property var letters: ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"] |
| 54 |
readonly property alias fastScrolling: internal.fastScrolling |
| 55 |
readonly property bool showing: (rail.opacity !== 0.0) |
| 56 |
readonly property double minimumHeight: rail.height |
| 57 |
|
| 58 |
width: units.gu(7) |
| 59 |
height: rail.height |
| 60 |
visible: enabled |
| 61 |
|
| 62 |
onListViewChanged: { |
| 63 |
if (listView && listView.model) { |
| 64 |
internal.initDirtyObserver(); |
| 65 |
} else if (listView) { |
| 66 |
listView.modelChanged.connect(function() { |
| 67 |
if (listView.model) { |
| 68 |
internal.initDirtyObserver(); |
| 69 |
} |
| 70 |
}); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
LomiriShape { |
| 76 |
id: magnified |
| 77 |
|
| 78 |
aspect: LomiriShape.Flat |
| 79 |
color: Theme.palette.normal.foreground |
| 80 |
radius: "medium" |
| 81 |
height: units.gu(6) |
| 82 |
width: units.gu(8) |
| 83 |
opacity: internal.fastScrolling && root.enabled ? 1.0 : 0.0 |
| 84 |
x: -magnified.width |
| 85 |
y: { |
| 86 |
if (internal.currentItem) { |
| 87 |
var itemCenterY = rail.y + internal.currentItem.y + (internal.currentItem.height / 2) |
| 88 |
return (itemCenterY - (magnified.height / 2)) |
| 89 |
} else { |
| 90 |
return 0 |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
Triangle { |
| 95 |
id: arrow |
| 96 |
|
| 97 |
color: magnified.color |
| 98 |
fill: true |
| 99 |
height: units.gu(1.5) |
| 100 |
width: units.gu(0.5) |
| 101 |
anchors { |
| 102 |
verticalCenter: parent.verticalCenter |
| 103 |
right: parent.right |
| 104 |
rightMargin: -width |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
Label { |
| 109 |
color: Theme.palette.selected.backgroundText |
| 110 |
anchors.fill: parent |
| 111 |
horizontalAlignment: Text.AlignHCenter |
| 112 |
verticalAlignment: Text.AlignVCenter |
| 113 |
text: internal.desireSection |
| 114 |
fontSize: "small" |
| 115 |
} |
| 116 |
|
| 117 |
Behavior on opacity { |
| 118 |
LomiriNumberAnimation {} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
Rectangle { |
| 123 |
id: cursor |
| 124 |
|
| 125 |
property bool showLabel: false |
| 126 |
property string currentSectionName: "" |
| 127 |
|
| 128 |
radius: pinSize * 0.3 |
| 129 |
height: pinSize |
| 130 |
width: height |
| 131 |
color: Theme.palette.normal.foreground |
| 132 |
opacity: rail.opacity |
| 133 |
x: rail.x |
| 134 |
y: { |
| 135 |
if (internal.currentItem) { |
| 136 |
var itemCenterY = rail.y + internal.currentItem.y + (internal.currentItem.height / 2) |
| 137 |
return (itemCenterY - (cursor.height / 2)) |
| 138 |
} else { |
| 139 |
return 0 |
| 140 |
} |
| 141 |
} |
| 142 |
Behavior on y { |
| 143 |
enabled: !internal.fastScrolling |
| 144 |
LomiriNumberAnimation { } |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
Column { |
| 149 |
id: rail |
| 150 |
|
| 151 |
property bool isVisible: root.enabled && |
| 152 |
(listView.flicking || dragArea.pressed) |
| 153 |
anchors { |
| 154 |
right: parent.right |
| 155 |
rightMargin: units.gu(2) |
| 156 |
left: parent.left |
| 157 |
leftMargin: units.gu(2) |
| 158 |
top: parent.top |
| 159 |
} |
| 160 |
height: childrenRect.height |
| 161 |
opacity: 0.0 |
| 162 |
onIsVisibleChanged: { |
| 163 |
if (isVisible) { |
| 164 |
rail.opacity = 1.0 |
| 165 |
hideTimer.stop() |
| 166 |
} else if (!root.enabled) { |
| 167 |
rail.opacity = 0.0 |
| 168 |
} else { |
| 169 |
hideTimer.restart() |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
Behavior on opacity { |
| 174 |
LomiriNumberAnimation { } |
| 175 |
} |
| 176 |
|
| 177 |
Repeater { |
| 178 |
id: sectionsRepeater |
| 179 |
|
| 180 |
model: root.letters |
| 181 |
Label { |
| 182 |
id: lbl |
| 183 |
|
| 184 |
anchors.left: parent.left |
| 185 |
height: pinSize |
| 186 |
width: pinSize |
| 187 |
verticalAlignment: Text.AlignVCenter |
| 188 |
horizontalAlignment: Text.AlignHCenter |
| 189 |
text: modelData |
| 190 |
fontSize: "x-small" |
| 191 |
color: cursor.y === y ? Theme.palette.selected.backgroundText : Theme.palette.normal.foregroundText |
| 192 |
opacity: !internal.modelDirty && Sections.contains(text) ? 1.0 : 0.5 |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
Timer { |
| 197 |
id: hideTimer |
| 198 |
|
| 199 |
running: false |
| 200 |
interval: 2000 |
| 201 |
onTriggered: rail.opacity = 0.0 |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
MouseArea { |
| 206 |
id: dragArea |
| 207 |
|
| 208 |
anchors { |
| 209 |
left: parent.left |
| 210 |
right: parent.right |
| 211 |
} |
| 212 |
y: rail.y |
| 213 |
height: rail.height |
| 214 |
visible: rail.opacity == 1.0 |
| 215 |
|
| 216 |
preventStealing: true |
| 217 |
onPressed: { |
| 218 |
internal.adjustContentPosition(mouseY) |
| 219 |
internal.fastScrolling = true |
| 220 |
} |
| 221 |
|
| 222 |
onReleased: { |
| 223 |
internal.fastScrolling = false |
| 224 |
} |
| 225 |
|
| 226 |
onPositionChanged: internal.adjustContentPosition(mouseY) |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
Timer { |
| 231 |
id: dirtyTimer |
| 232 |
interval: 500 |
| 233 |
running: false |
| 234 |
onTriggered: { |
| 235 |
Sections.initSectionData(listView); |
| 236 |
internal.modelDirty = false; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
Timer { |
| 241 |
id: timerScroll |
| 242 |
|
| 243 |
running: false |
| 244 |
interval: 10 |
| 245 |
onTriggered: { |
| 246 |
if (internal.desireSection != internal.currentSection) { |
| 247 |
var idx = Sections.getIndexFor(internal.desireSection) |
| 248 |
if (idx !== -1) { |
| 249 |
listView.cancelFlick() |
| 250 |
listView.positionViewAtIndex(idx, ListView.Beginning) |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
QtObject { |
| 257 |
id: internal |
| 258 |
|
| 259 |
property string currentSection: listView.currentSection |
| 260 |
property string desireSection: "" |
| 261 |
property string targetSection: fastScrolling ? desireSection : currentSection |
| 262 |
property int oldY: 0 |
| 263 |
property bool modelDirty: false |
| 264 |
property bool down: true |
| 265 |
property bool fastScrolling: false |
| 266 |
property var currentItem: null |
| 267 |
|
| 268 |
onTargetSectionChanged: moveIndicator(targetSection) |
| 269 |
|
| 270 |
function initDirtyObserver() { |
| 271 |
Sections.initialize(listView); |
| 272 |
function dirtyObserver() { |
| 273 |
if (!internal.modelDirty) { |
| 274 |
internal.modelDirty = true; |
| 275 |
dirtyTimer.running = true; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
if (listView.model.countChanged) |
| 280 |
listView.model.countChanged.connect(dirtyObserver); |
| 281 |
|
| 282 |
if (listView.model.itemsChanged) |
| 283 |
listView.model.itemsChanged.connect(dirtyObserver); |
| 284 |
|
| 285 |
if (listView.model.itemsInserted) |
| 286 |
listView.model.itemsInserted.connect(dirtyObserver); |
| 287 |
|
| 288 |
if (listView.model.itemsMoved) |
| 289 |
listView.model.itemsMoved.connect(dirtyObserver); |
| 290 |
|
| 291 |
if (listView.model.itemsRemoved) |
| 292 |
listView.model.itemsRemoved.connect(dirtyObserver); |
| 293 |
} |
| 294 |
|
| 295 |
function adjustContentPosition(mouseY) { |
| 296 |
var child = rail.childAt(rail.width / 2, mouseY) |
| 297 |
if (!child || child.text === "") { |
| 298 |
return |
| 299 |
} |
| 300 |
var section = child.text |
| 301 |
if (internal.desireSection !== section) { |
| 302 |
internal.desireSection = section |
| 303 |
moveIndicator(section) |
| 304 |
if (dragArea.pressed) { |
| 305 |
timerScroll.restart() |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
function moveIndicator(section) |
| 311 |
{ |
| 312 |
var index = root.letters.indexOf(section) |
| 313 |
if (index != -1) { |
| 314 |
currentItem = sectionsRepeater.itemAt(index) |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
} |