| 1 |
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*- |
| 2 |
# |
| 3 |
# Copyright (C) 2016 Canonical Ltd. |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU Lesser General Public License as published by |
| 7 |
# the Free Software Foundation; version 3. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU Lesser General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU Lesser General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
import logging |
| 18 |
|
| 19 |
import autopilot.exceptions |
| 20 |
from autopilot import logging as autopilot_logging |
| 21 |
|
| 22 |
from lomiriuitoolkit import units |
| 23 |
from lomiriuitoolkit._custom_proxy_objects import _common |
| 24 |
|
| 25 |
|
| 26 |
logger = logging.getLogger(__name__) |
| 27 |
|
| 28 |
# FIXME: Only left-to-right (where x=0 is leftmost) layouts are taken into |
| 29 |
# account. Add support for horizontal flicking with right-to-left layouts. |
| 30 |
|
| 31 |
# Containers helpers. |
| 32 |
|
| 33 |
|
| 34 |
def _get_visible_container_top(containers): |
| 35 |
containers_top = [container.globalRect.y for container in containers] |
| 36 |
return max(containers_top) |
| 37 |
|
| 38 |
|
| 39 |
def _get_visible_container_bottom(containers): |
| 40 |
containers_bottom = [ |
| 41 |
container.globalRect.y + container.globalRect.height |
| 42 |
for container in containers if container.globalRect.height > 0] |
| 43 |
return min(containers_bottom) |
| 44 |
|
| 45 |
|
| 46 |
def _get_visible_container_leftmost(containers): |
| 47 |
containers_left = [container.globalRect.x for container in containers] |
| 48 |
return max(containers_left) |
| 49 |
|
| 50 |
|
| 51 |
def _get_visible_container_rightmost(containers): |
| 52 |
containers_right = [ |
| 53 |
container.globalRect.x + container.globalRect.width |
| 54 |
for container in containers if container.globalRect.width > 0] |
| 55 |
return min(containers_right) |
| 56 |
|
| 57 |
|
| 58 |
class Scrollable(_common.LomiriUIToolkitCustomProxyObjectBase): |
| 59 |
|
| 60 |
@autopilot_logging.log_action(logger.info) |
| 61 |
def is_child_visible(self, child): |
| 62 |
"""Determine if the child is visible. |
| 63 |
|
| 64 |
A child is visible if no scrolling is needed to reveal it. |
| 65 |
|
| 66 |
""" |
| 67 |
containers = self._get_containers() |
| 68 |
return self._is_child_visible(child, containers) |
| 69 |
|
| 70 |
def _get_containers(self): |
| 71 |
"""Return a list with the containers to take into account when swiping. |
| 72 |
|
| 73 |
The list includes this flickable and the top-most container. |
| 74 |
TODO add additional flickables that are between this and the top |
| 75 |
container. --elopio - 2014-03-22 |
| 76 |
|
| 77 |
""" |
| 78 |
containers = [self._get_top_container(), self] |
| 79 |
return containers |
| 80 |
|
| 81 |
def _is_child_in_visible_y_range(self, child, containers): |
| 82 |
"""Check if the center of the child has an y-value in the visible range. |
| 83 |
|
| 84 |
:return: True if the center of the child is y-visible, False otherwise. |
| 85 |
""" |
| 86 |
object_center_y = child.globalRect.y + child.globalRect.height // 2 |
| 87 |
visible_top = _get_visible_container_top(containers) |
| 88 |
visible_bottom = _get_visible_container_bottom(containers) |
| 89 |
return (object_center_y >= visible_top and |
| 90 |
object_center_y <= visible_bottom) |
| 91 |
|
| 92 |
def _is_child_in_visible_x_range(self, child, containers): |
| 93 |
"""Check if the center of the child has an x-value in the visible range. |
| 94 |
|
| 95 |
:return: True if the center of the child is x-visible. False otherwise. |
| 96 |
""" |
| 97 |
object_center_x = child.globalRect.x + child.globalRect.width // 2 |
| 98 |
visible_left = _get_visible_container_leftmost(containers) |
| 99 |
visible_right = _get_visible_container_rightmost(containers) |
| 100 |
return (object_center_x >= visible_left and |
| 101 |
object_center_x <= visible_right) |
| 102 |
|
| 103 |
def _is_child_visible(self, child, containers): |
| 104 |
"""Check if the center of the child is visible. |
| 105 |
|
| 106 |
:return: True if the center of the child is visible, False otherwise. |
| 107 |
|
| 108 |
""" |
| 109 |
return (self._is_child_in_visible_y_range(child, containers) and |
| 110 |
self._is_child_in_visible_x_range(child, containers)) |
| 111 |
|
| 112 |
def _slow_drag_rate(self): |
| 113 |
# I found that when the flickDeceleration is 1500, the rate should be |
| 114 |
# 5 and that when it's 100, the rate should be 1. With those two points |
| 115 |
# we can get the following equation. |
| 116 |
# XXX The deceleration might not be linear with respect to the rate, |
| 117 |
# but this works for the two types of scrollables we have for now. |
| 118 |
# --elopio - 2014-05-08 |
| 119 |
return (self.flickDeceleration + 250) / 350 |
| 120 |
|
| 121 |
def _slow_drag(self, start_x, stop_x, start_y, stop_y, rate=None): |
| 122 |
# If we drag too fast, we end up scrolling more than what we |
| 123 |
# should, sometimes missing the element we are looking for. |
| 124 |
|
| 125 |
# FIXME: Not all scrollables have contentX and contentY |
| 126 |
# (PathView does not have them), so use this only in a Flickable. |
| 127 |
original_content_y = self.contentY |
| 128 |
original_content_x = self.contentX |
| 129 |
|
| 130 |
if rate is None: |
| 131 |
rate = self._slow_drag_rate() |
| 132 |
self.pointing_device.drag(start_x, start_y, stop_x, stop_y, rate=rate) |
| 133 |
|
| 134 |
if (self.contentY == original_content_y and |
| 135 |
self.contentX == original_content_x): |
| 136 |
raise _common.ToolkitException('Could not swipe in the flickable.') |
| 137 |
|
| 138 |
|
| 139 |
class CannotSwipeMoreToolkitException(_common.ToolkitException): |
| 140 |
"""Exception raised when trying to swipe more than is possible.""" |
| 141 |
|
| 142 |
|
| 143 |
class QQuickFlickable(Scrollable): |
| 144 |
|
| 145 |
# Swiping from below can open the deprecated toolbar or trigger the bottom |
| 146 |
# edge gesture. Use this margin to start a swipe that will not be that |
| 147 |
# close to the bottom edge. |
| 148 |
margin_to_swipe_from_bottom = units.gu(2) |
| 149 |
# Swiping from above can open the indicators or resize the window. Use this |
| 150 |
# margin to start a swipe that will not be that close to the top edge. |
| 151 |
margin_to_swipe_from_top = units.gu(2) |
| 152 |
# Swiping from left and right can show the launcher or open app switcher. |
| 153 |
margin_to_swipe_from_left = units.gu(3) |
| 154 |
margin_to_swipe_from_right = units.gu(3) |
| 155 |
|
| 156 |
@autopilot_logging.log_action(logger.info) |
| 157 |
def swipe_child_into_view(self, child): |
| 158 |
"""Make the child visible. |
| 159 |
|
| 160 |
""" |
| 161 |
containers = self._get_containers() |
| 162 |
if not self._is_child_visible(child, containers): |
| 163 |
self._swipe_non_visible_child_into_view(child, containers) |
| 164 |
else: |
| 165 |
logger.debug('The element is already visible.') |
| 166 |
|
| 167 |
@autopilot_logging.log_action(logger.info) |
| 168 |
def _swipe_non_visible_child_into_view(self, child, containers): |
| 169 |
while not self._is_child_in_visible_y_range(child, containers): |
| 170 |
# Check the direction of the swipe based on the position of the |
| 171 |
# child relative to the immediate flickable container. |
| 172 |
if child.globalRect.y < self.globalRect.y: |
| 173 |
self.swipe_to_show_more_above(containers) |
| 174 |
else: |
| 175 |
self.swipe_to_show_more_below(containers) |
| 176 |
|
| 177 |
while not self._is_child_in_visible_x_range(child, containers): |
| 178 |
if child.globalRect.x < self.globalRect.x: |
| 179 |
self.swipe_to_show_more_left(containers) |
| 180 |
else: |
| 181 |
self.swipe_to_show_more_right(containers) |
| 182 |
|
| 183 |
@autopilot_logging.log_action(logger.info) |
| 184 |
def swipe_to_show_more_above(self, containers=None): |
| 185 |
if self.atYBeginning: |
| 186 |
raise CannotSwipeMoreToolkitException( |
| 187 |
"We are already at the top of the container.") |
| 188 |
else: |
| 189 |
self._swipe_to_show_more('above', containers) |
| 190 |
|
| 191 |
@autopilot_logging.log_action(logger.info) |
| 192 |
def swipe_to_show_more_below(self, containers=None): |
| 193 |
if self.atYEnd: |
| 194 |
raise CannotSwipeMoreToolkitException( |
| 195 |
"We are already at the bottom of the container.") |
| 196 |
else: |
| 197 |
self._swipe_to_show_more('below', containers) |
| 198 |
|
| 199 |
@autopilot_logging.log_action(logger.info) |
| 200 |
def swipe_to_show_more_left(self, containers=None): |
| 201 |
if self.atXBeginning: |
| 202 |
raise CannotSwipeMoreToolkitException( |
| 203 |
"We are already at the left of the container.") |
| 204 |
else: |
| 205 |
self._swipe_to_show_more('left', containers) |
| 206 |
|
| 207 |
@autopilot_logging.log_action(logger.info) |
| 208 |
def swipe_to_show_more_right(self, containers=None): |
| 209 |
if self.atXEnd: |
| 210 |
raise CannotSwipeMoreToolkitException( |
| 211 |
"We are already at the right of the container.") |
| 212 |
else: |
| 213 |
self._swipe_to_show_more('right', containers) |
| 214 |
|
| 215 |
def _swipe_to_show_more(self, direction, containers=None): |
| 216 |
if containers is None: |
| 217 |
containers = self._get_containers() |
| 218 |
start_x = stop_x = self.globalRect.x + (self.globalRect.width // 2) |
| 219 |
start_y = stop_y = self.globalRect.y + (self.globalRect.height // 2) |
| 220 |
|
| 221 |
top = _get_visible_container_top(containers) |
| 222 |
bottom = _get_visible_container_bottom(containers) |
| 223 |
leftmost = _get_visible_container_leftmost(containers) |
| 224 |
rightmost = _get_visible_container_rightmost(containers) |
| 225 |
|
| 226 |
# Make the drag range be a multiple of the drag "rate" value. |
| 227 |
# Workarounds https://bugs.launchpad.net/mir/+bug/1399690 |
| 228 |
rate = self._slow_drag_rate() |
| 229 |
|
| 230 |
# The swipes are not done from right at the top and bottom because |
| 231 |
# they could trigger edge gestures or resize windows. |
| 232 |
if direction == 'below': |
| 233 |
start_y = bottom - self.margin_to_swipe_from_bottom |
| 234 |
stop_y = start_y + (top - start_y) / rate * rate |
| 235 |
|
| 236 |
elif direction == 'above': |
| 237 |
start_y = top + self.margin_to_swipe_from_top |
| 238 |
stop_y = start_y + (bottom - start_y) / rate * rate |
| 239 |
|
| 240 |
elif direction == 'left': |
| 241 |
start_x = leftmost + self.margin_to_swipe_from_left |
| 242 |
stop_x = start_x + (rightmost - start_x) / rate * rate |
| 243 |
|
| 244 |
elif direction == 'right': |
| 245 |
start_x = rightmost - self.margin_to_swipe_from_right |
| 246 |
stop_x = start_x + (leftmost - start_x) / rate * rate |
| 247 |
|
| 248 |
else: |
| 249 |
raise _common.ToolkitException( |
| 250 |
'Invalid direction {}.'.format(direction)) |
| 251 |
self._slow_drag(start_x, stop_x, start_y, stop_y, rate) |
| 252 |
self.dragging.wait_for(False) |
| 253 |
self.moving.wait_for(False) |
| 254 |
|
| 255 |
@autopilot_logging.log_action(logger.info) |
| 256 |
def swipe_to_top(self): |
| 257 |
if not self.atYBeginning: |
| 258 |
containers = self._get_containers() |
| 259 |
while not self.atYBeginning: |
| 260 |
self.swipe_to_show_more_above(containers) |
| 261 |
|
| 262 |
@autopilot_logging.log_action(logger.info) |
| 263 |
def swipe_to_bottom(self): |
| 264 |
if not self.atYEnd: |
| 265 |
containers = self._get_containers() |
| 266 |
while not self.atYEnd: |
| 267 |
self.swipe_to_show_more_below(containers) |
| 268 |
|
| 269 |
@autopilot_logging.log_action(logger.info) |
| 270 |
def swipe_to_leftmost(self): |
| 271 |
if not self.atXBeginning: |
| 272 |
containers = self._get_containers() |
| 273 |
while not self.atXBeginning: |
| 274 |
self.swipe_to_show_more_left(containers) |
| 275 |
|
| 276 |
def swipe_to_rightmost(self): |
| 277 |
if not self.atXEnd: |
| 278 |
containers = self._get_containers() |
| 279 |
while not self.atXEnd: |
| 280 |
self.swipe_to_show_more_right(containers) |
| 281 |
|
| 282 |
@autopilot_logging.log_action(logger.info) |
| 283 |
def pull_to_refresh(self): |
| 284 |
"""Pulls the flickable down and triggers a refresh on it. |
| 285 |
|
| 286 |
:raises lomiriuitoolkit.ToolkitException: If the flickable has no pull |
| 287 |
to release functionality. |
| 288 |
|
| 289 |
""" |
| 290 |
try: |
| 291 |
pull_to_refresh = self.select_single(PullToRefresh) |
| 292 |
except autopilot.exceptions.StateNotFoundError: |
| 293 |
raise _common.ToolkitException( |
| 294 |
'The flickable has no pull to refresh functionality.') |
| 295 |
self.swipe_to_top() |
| 296 |
self._swipe_to_middle() |
| 297 |
pull_to_refresh.wait_for_refresh() |
| 298 |
|
| 299 |
@autopilot_logging.log_action(logger.info) |
| 300 |
def _swipe_to_middle(self): |
| 301 |
start_x = stop_x = self.globalRect.x + (self.globalRect.width // 2) |
| 302 |
# Start just a little under the top. |
| 303 |
containers = self._get_containers() |
| 304 |
top = _get_visible_container_top(containers) + 5 |
| 305 |
bottom = _get_visible_container_bottom(containers) |
| 306 |
|
| 307 |
start_y = top |
| 308 |
stop_y = (self.globalRect.y + bottom) // 2 |
| 309 |
self._slow_drag(start_x, stop_x, start_y, stop_y) |
| 310 |
self.dragging.wait_for(False) |
| 311 |
self.moving.wait_for(False) |
| 312 |
|
| 313 |
@autopilot_logging.log_action(logger.info) |
| 314 |
def _cancel_pull_to_refresh(self): |
| 315 |
"""Swipe down the list and then swipe it up to cancel the refresh.""" |
| 316 |
# This method is kept private because it's not for the test writers to |
| 317 |
# call. It's to test pull to refresh. --elopio - 2014-05-22 |
| 318 |
self.swipe_to_top() |
| 319 |
start_x = stop_x = self.globalRect.x + (self.globalRect.width // 2) |
| 320 |
# Start just a little under the top. |
| 321 |
containers = self._get_containers() |
| 322 |
top = _get_visible_container_top(containers) + 5 |
| 323 |
bottom = _get_visible_container_bottom(containers) |
| 324 |
|
| 325 |
start_y = top |
| 326 |
stop_y = (self.globalRect.y + bottom) // 2 |
| 327 |
self.pointing_device.move(start_x, start_y) |
| 328 |
self.pointing_device.press() |
| 329 |
self.pointing_device.move(stop_x, stop_y) |
| 330 |
self.pointing_device.move(start_x, start_y) |
| 331 |
self.pointing_device.release() |
| 332 |
|
| 333 |
|
| 334 |
class PullToRefresh(_common.LomiriUIToolkitCustomProxyObjectBase): |
| 335 |
"""Autopilot helper for the PullToRefresh component.""" |
| 336 |
|
| 337 |
def wait_for_refresh(self): |
| 338 |
activity_indicator = self.select_single('ActivityIndicator') |
| 339 |
activity_indicator.running.wait_for(False) |