107
106
except dbus.StateNotFoundError:
108
107
raise _common.ToolkitException(
109
108
'ListView delegate is not a ListItem or not in selectMode')
110
def _get_first_item(self):
111
"""Returns the first item from the ListView."""
112
items = self.get_children_by_type('QQuickItem')[0].get_children()
113
items = sorted(items, key=lambda item: item.globalRect.y)
116
@autopilot_logging.log_action(logger.info)
117
def drag_item(self, from_index, to_index):
118
if platform.model() != 'Desktop':
119
raise NotImplementedError(
120
'Drag does not work on the phone because of bug #1266601')
122
self._enable_drag_mode()
124
both_items_visible = (
125
self._is_drag_panel_visible(from_index) and
126
self._is_drag_panel_visible(to_index))
127
if both_items_visible:
128
self._drag_both_items_visible(from_index, to_index)
130
self._drag_item_with_pagination(from_index, to_index)
131
# wait 1 second till all animations complete
134
@autopilot_logging.log_action(logger.debug)
135
def _enable_drag_mode(self):
137
first_item = self._get_first_item()
138
self.pointing_device.click_object(first_item, press_duration=2)
139
self._get_drag_panel(wait=True)
141
def _is_drag_panel_visible(self, index):
143
drag_panel = self._get_drag_panel(index)
147
return self.is_child_visible(drag_panel)
149
def _get_drag_panel(self, index=0, wait=False):
150
select_method = self.wait_select_single if wait else self.select_single
152
return select_method(
153
'QQuickItem', objectName='drag_panel{}'.format(index))
155
# While we are dragging, each panel item is duplicated.
156
# A ValueError will be raised in this scenario. We can take
157
# any of the returned elements, as they have the same position.
158
return self.select_many(
159
'QQuickItem', objectName='drag_panel{}'.format(index))[0]
161
def _drag_both_items_visible(self, from_index, to_index):
162
from_drag_handler = self._get_drag_panel(from_index)
163
to_drag_handler = self._get_drag_panel(to_index)
164
start_x, start_y = input.get_center_point(from_drag_handler)
165
stop_x, stop_y = input.get_center_point(to_drag_handler)
166
self.pointing_device.drag(start_x, start_y, stop_x, stop_y)
168
def _drag_item_with_pagination(self, from_index, to_index):
169
# the from_index might be invisible
170
from_item = self._find_element('listitem{}'.format(from_index))
171
from_item.swipe_into_view()
172
from_drag_panel = self._get_drag_panel(from_index)
173
containers = self._get_containers()
174
if from_index < to_index:
175
self._drag_downwards(from_drag_panel, to_index, containers)
177
self._drag_upwards(from_drag_panel, to_index, containers)
179
def _drag_downwards(self, drag_panel, to_index, containers):
180
visible_bottom = _flickable._get_visible_container_bottom(
182
start_x, start_y = input.get_center_point(drag_panel)
184
self.pointing_device.move(start_x, start_y)
185
self.pointing_device.press()
187
self.pointing_device.move(stop_x, visible_bottom)
188
to_drag_panel = self._get_drag_panel(to_index, wait=True)
189
while not self.is_child_visible(to_drag_panel):
192
h = to_drag_panel.height / 2
193
self.pointing_device.move(stop_x, self.pointing_device.y - h)
194
# move under the item with the to_index
195
to_drag_panel = self._get_drag_panel(to_index - 1)
197
to_drag_panel.globalRect.y +
198
to_drag_panel.globalRect.height)
199
self.pointing_device.move(stop_x, stop_y)
200
self.pointing_device.release()
202
def _drag_upwards(self, handler, to_index, containers):
203
visible_top = _flickable._get_visible_container_top(
205
start_x, start_y = input.get_center_point(handler)
207
self.pointing_device.move(start_x, start_y)
208
self.pointing_device.press()
210
# Header alters topMargin, therefore drag only till that edge
211
self.pointing_device.move(stop_x, visible_top + self.topMargin)
212
to_drag_panel = self._get_drag_panel(to_index, wait=True)
213
while not self.is_child_visible(to_drag_panel):
216
h = to_drag_panel.height / 2
217
self.pointing_device.move(stop_x, self.pointing_device.y + h)
218
# move after the item with the to_index
219
to_drag_panel = self._get_drag_panel(to_index)
221
to_drag_panel.globalRect.y +
222
to_drag_panel.globalRect.height)
223
self.pointing_device.move(stop_x, stop_y)
224
self.pointing_device.release()