Debian Codegraph @

tests/autopilot/lomiri_keyboard/emulators/keypad.py

raw
1 # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2 #
3 # Lomiri Keyboard Test Suite
4 # Copyright (C) 2013, 2015 Canonical
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 import logging
21
22 import lomiriuitoolkit
23
24 from lomiri_keyboard.emulators.key import Key
25
26
27 logger = logging.getLogger(__name__)
28
29
30 class KeyPadState:
31 NORMAL = "NORMAL"
32 SHIFTED = "SHIFTED"
33 CAPSLOCK = "CAPSLOCK"
34
35
36 class KeyPad(lomiriuitoolkit.LomiriUIToolkitCustomProxyObjectBase):
37 """A basic emulator that provides the details of the keys contained within.
38
39 """
40
41 def get_key_details(self):
42 contained_keys = {}
43 key_positions = {}
44
45 def _iter_keys(key_type, label_fn):
46 for key in self.select_many(key_type):
47 with key.no_automatic_refreshing():
48 rect = key.globalRect
49 if key.leftSide:
50 rect = (
51 key.globalRect[0] + key.leftOffset,
52 key.globalRect[1],
53 key.globalRect[2] - key.leftOffset,
54 key.globalRect[3])
55 elif key.rightSide:
56 rect = (
57 key.globalRect[0],
58 key.globalRect[1],
59 key.globalRect[2] - key.rightOffset,
60 key.globalRect[3])
61 key_pos = Key.Pos(*rect)
62 label = label_fn(key)
63 if label != '':
64 contained_keys[label] = KeyPadState.NORMAL
65 key_positions[label] = key_pos
66 if hasattr(key, "shifted") and key.shifted != '':
67 contained_keys[key.shifted] = KeyPadState.SHIFTED
68 key_positions[key.shifted] = key_pos
69
70 _iter_keys("CharKey", lambda x: x.label)
71 _iter_keys("FlickCharKey", lambda x: x.label)
72 _iter_keys("ActionKey", lambda x: x.action)
73 _iter_keys("ShiftKey", lambda x: x.action)
74 _iter_keys("LanguageKey", lambda x: x.action)
75 _iter_keys("ReturnKey", lambda x: x.action)
76 _iter_keys("CommitKey", lambda x: x.action)
77 _iter_keys("HCharKey", lambda x: x.label)
78
79 return (contained_keys, key_positions)