File: listitems.py

package info (click to toggle)
lomiri-ui-toolkit 1.3.5110%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,436 kB
  • sloc: cpp: 85,830; python: 5,537; sh: 1,344; javascript: 919; ansic: 573; makefile: 204
file content (174 lines) | stat: -rw-r--r-- 5,515 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#
# Copyright (C) 2012, 2013, 2014 Canonical Ltd.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import logging

from autopilot import logging as autopilot_logging
from autopilot.introspection import dbus

from lomiriuitoolkit._custom_proxy_objects import _common


logger = logging.getLogger(__name__)


class Empty(_common.LomiriUIToolkitCustomProxyObjectBase):
    """Base class to emulate swipe to delete."""

    def exists(self):
        try:
            return self.implicitHeight > 0
        except dbus.StateNotFoundError:
            return False

    def _get_confirm_button(self):
        return self.select_single(
            'QQuickItem', objectName='confirmRemovalDialog')

    @autopilot_logging.log_action(logger.info)
    def swipe_to_delete(self, direction='deprecated'):
        """Swipe the item in from left to right in order to delete it.

        :param direction: deprecated. The only direction that will cause a
            delete is left to right. Do not pass any argument to this method,
            because it will be removed in future versions.

        """
        if direction != 'deprecated':
            logger.warn(
                'The direction argument is deprecated. Now the Lomiri SDK '
                'only deletes list items when swiping from left to right. '
                'Call swipe_to_delete without arguments.')
        else:
            direction = 'right'
        if self.removable:
            self._swipe_to_delete_removable_item(direction)
        else:
            raise _common.ToolkitException(
                'The item "{0}" is not removable'.format(self.objectName))

    def _swipe_to_delete_removable_item(self, direction):
        if direction == 'left':
            raise _common.ToolkitException(
                'Only swiping to the right will cause the item to be deleted.')
        elif direction != 'right':
            raise _common.ToolkitException(
                'Invalid direction "{0}" used on swipe to delete function'
                .format(direction))

        self._drag_pointing_device_to_delete()
        if self.confirmRemoval:
            self.waitingConfirmationForRemoval.wait_for(True)
        else:
            self._wait_until_deleted()

    def _drag_pointing_device_to_delete(self):
        x, y, width, height = self.globalRect
        start_x = x + (width * 0.2)
        stop_x = x + (width * 0.8)
        start_y = stop_y = y + (height // 2)

        self.pointing_device.drag(start_x, start_y, stop_x, stop_y)

    def _wait_until_deleted(self):
        try:
            # The item was hidden.
            self.implicitHeight.wait_for(0)
        except dbus.StateNotFoundError:
            # The item was destroyed.
            pass

    @autopilot_logging.log_action(logger.info)
    def confirm_removal(self):
        """Comfirm item removal if this was already swiped."""
        if self.waitingConfirmationForRemoval:
            deleteButton = self._get_confirm_button()
            self.pointing_device.click_object(deleteButton)
            self._wait_until_deleted()
        else:
            raise _common.ToolkitException(
                'The item "{0}" is not waiting for removal confirmation'.
                format(self.objectName))


class Base(Empty):
    pass


class Expandable(Empty):

    @autopilot_logging.log_action(logger.info)
    def expand(self):
        """Expand an expandable list item.

        If the item is already expanded, no action will be executed.
        If you want to check if the item is expanded, you can use the
        ``expanded`` property.

        """
        if not self.expanded:
            self._click_always_visible_section()
            self.expanded.wait_for(True)
            self.height.wait_for(self.expandedHeight)
        else:
            logger.debug('The item is already expanded.')

    @autopilot_logging.log_action(logger.debug)
    def _click_always_visible_section(self):
        self.pointing_device.move(
            self.globalRect.x + self.globalRect.width // 2,
            self.globalRect.y + self.collapsedHeight // 2)
        self.pointing_device.click()

    @autopilot_logging.log_action(logger.info)
    def collapse(self):
        """Collapse an expandable list item.

        If the item is already collapsed, no action will be executed.
        If you want to check if the item is expanded, you can use the
        ``expanded`` property.

        """
        if self.expanded:
            self._click_always_visible_section()
            self.expanded.wait_for(False)
            self.height.wait_for(self.collapsedHeight)
        else:
            logger.debug('The item is already collapsed.')


class Standard(Empty):
    pass


class ItemSelector(Empty):
    pass


class SingleControl(Empty):
    pass


class MultiValue(Base):
    pass


class SingleValue(Base):
    pass


class Subtitled(Base):
    pass