/
screen_keyboard.py
executable file
·238 lines (199 loc) · 9.88 KB
/
screen_keyboard.py
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# This file is part of pi-jukebox.
#
# pi-jukebox is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pi-jukebox 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with pi-jukebox. If not, see < http://www.gnu.org/licenses/ >.
#
# (C) 2015- by Mark Zwart, <mark.zwart@pobox.com>
"""
=======================================================
**screen_keyboard.py**: On-screen keyboard
=======================================================
"""
__author__ = 'Mark Zwart'
from settings import *
from gui_widgets import *
from gui_screens import *
""" Special button icons
:ivar
"""
ICO_SHIFT = RESOURCES + 'shift_48x32.png'
ICO_BACKSPACE = RESOURCES + 'backspace_48x32.png'
ICO_ENTER = RESOURCES + 'enter_48x32.png'
ICO_LETTERS = RESOURCES + 'letters_48x32.png'
ICO_SYMBOLS = RESOURCES + 'symbols_48x32.png'
class KeyboardBase(ScreenModal):
""" The base class of a keyboard, should not be instantiated.
:param screen_rect: The display's rectangle where the keyboard is drawn on.
:param caption: The title displayed at the top of the screen.
:param text: The text that will be edited with the keyboard, default = "".
"""
def __init__(self, screen, caption, text=""):
ScreenModal.__init__(self, screen, caption)
self.text = text
# Edit box
edit_box = LabelText('lbl_edit_box', self.surface, 5, 30, SCREEN_WIDTH - 10, 25, text)
edit_box.background_color = WHITE
edit_box.font_color = BLACK
edit_box.set_alignment(HOR_LEFT, VERT_MID, 5)
self.add_component(edit_box)
def add_row_buttons(self, list_symbols, x, y):
""" Adds a list of symbol keys starting at x on y. """
button_width = 32
for letter in list_symbols:
btn_name = 'btn_symbol_' + letter
btn = ButtonText(btn_name, self.surface, x, y, button_width, 32, letter)
self.add_component(btn)
x += button_width
def set_text(self, text):
""" Sets the edit box's text.
:param text: Text that needs to be edited using the keyboard
"""
self.text = text
self.components['lbl_edit_box'].caption = text
class KeyboardLetters(KeyboardBase):
""" Displays keyboard for letters.
"""
def __init__(self, screen, caption, text=""):
KeyboardBase.__init__(self, screen, caption, text)
self.shift_state = False
y_row = 65
y_row_increment = 45
first_row = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p']
self.add_row_buttons(first_row, 0, y_row)
second_row = ['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l']
y_row += y_row_increment
self.add_row_buttons(second_row, 17, y_row)
third_row = ['z', 'x', 'c', 'v', 'b', 'n', 'm']
y_row += y_row_increment
self.add_row_buttons(third_row, 49, y_row)
self.add_component(ButtonIcon('btn_shift', self.surface, ICO_SHIFT, 3, y_row))
self.add_component(ButtonIcon('btn_backspace', self.surface, ICO_BACKSPACE, 271, y_row))
y_row += y_row_increment
self.add_component(ButtonText('btn_symbol_comma', self.surface, 50, y_row, 32, 32, ','))
self.add_component(ButtonText('btn_symbol_space', self.surface, 82, y_row, 159, 32, ' '))
self.add_component(ButtonText('btn_symbol_point', self.surface, 241, y_row, 32, 32, '.'))
self.add_component(ButtonIcon('btn_enter', self.surface, ICO_ENTER, 271, y_row))
self.add_component(ButtonIcon('btn_symbols', self.surface, ICO_SYMBOLS, 4, y_row))
def __letters_shift(self):
""" Sets button values to lower- or uppercase depending on the shift state. """
for key, value in self.components.items():
if value.tag_name[:11] == 'btn_symbol_':
if not self.shift_state:
new_letter = value.caption.upper()
else:
new_letter = value.caption.lower()
value.caption = new_letter
self.shift_state = not self.shift_state
self.show()
def on_click(self, x, y):
tag_name = super(KeyboardLetters, self).on_click(x, y)
if tag_name is None:
return
if tag_name == 'btn_shift':
self.__letters_shift()
elif tag_name[:11] == 'btn_symbol_': # If keyboard symbol is pressed add it to the text
self.components['lbl_edit_box'].caption += self.components[tag_name].caption
self.components['lbl_edit_box'].draw()
if self.shift_state:
self.shift_state = False
self.__letters_shift()
elif tag_name == 'btn_backspace': # Remove last character of the text
current_value = self.components['lbl_edit_box'].caption
self.components['lbl_edit_box'].caption = current_value[:len(current_value) - 1]
self.components['lbl_edit_box'].draw()
self.text = self.components['lbl_edit_box'].caption
self.return_object = self.components['lbl_edit_box'].caption
if tag_name == 'btn_symbols':
self.return_object = 'symbols' # Switch to numbers/symbols keyboard
self.close()
elif tag_name == 'btn_enter':
self.return_object = 'enter' # Confirms current text value
self.close()
class KeyboardSymbols(KeyboardBase):
""" Displays keyboard for numbers and symbols.
"""
def __init__(self, screen, caption, text=""):
KeyboardBase.__init__(self, screen, caption, text)
y_row = 65
y_row_increment = 45
first_row = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
self.add_row_buttons(first_row, 0, y_row)
y_row += y_row_increment
second_row = ['-', '+', '=', '/', '(', ')', '%', '$', '#', '_']
self.add_row_buttons(second_row, 0, y_row)
y_row += y_row_increment
third_row = [':', ';', '.', ',', '?', '!', '\'', '*']
self.add_row_buttons(third_row, 5, y_row)
self.add_component(ButtonIcon('btn_backspace', self.surface, ICO_BACKSPACE, 271, y_row))
y_row += y_row_increment
self.add_component(ButtonIcon('btn_enter', self.surface, ICO_ENTER, 271, y_row))
self.add_component(ButtonIcon('btn_symbol_letters', self.surface, ICO_LETTERS, 0, y_row))
self.add_component(ButtonText('btn_symbol_ampersand', self.surface, 50, y_row, 32, 32, '&'))
self.add_component(ButtonText('btn_symbol_space', self.surface, 82, y_row, 159, 32, ' '))
self.add_component(ButtonText('btn_symbol_at', self.surface, 241, y_row, 32, 32, '@'))
def on_click(self, x, y):
tag_name = super(KeyboardSymbols, self).on_click(x, y)
if tag_name is None:
return
if tag_name[:11] == 'btn_symbol_': # If keyboard symbol is pressed add it to the text
self.components['lbl_edit_box'].caption += self.components[tag_name].caption
self.components['lbl_edit_box'].draw()
elif tag_name == 'btn_backspace': # Remove last character of the text
current_value = self.components['lbl_edit_box'].caption
self.components['lbl_edit_box'].caption = current_value[:len(current_value) - 1]
self.components['lbl_edit_box'].draw()
self.return_object = self.components['lbl_edit_box'].caption
self.text = self.components['lbl_edit_box'].caption # Ensure text = to the edit box
if tag_name == 'btn_symbol_letters':
self.return_object = 'letters' # Switch to letters keyboard
self.close()
if tag_name == 'btn_enter':
self.return_object = 'enter' # Confirms current text value
self.close()
class Keyboard():
""" Called keyboard class that displays a text edit field with a
letter or symbol keyboard.
:param screen_rect: The display's rectangle where the keyboard is drawn on.
:param caption: The title displayed at the top of the screen.
:param text: The text that will be edited with the keyboard, default = "".
"""
def __init__(self, screen, caption, text=""):
self.text = text
self.text_original = text
self.selected = 'letters'
self.keyboard_letters = KeyboardLetters(screen, caption, text)
self.keyboard_symbols = KeyboardSymbols(screen, caption, text)
def show(self):
""" Loops until enter, cancel or escape on the keyboard is pressed.
:return: The text as it was edited when return was pressed, or the original text in case of a cancellation.
"""
value = ''
while value != 'enter' and value != 'cancel':
# Switch between the different keyboards (letter or number/symbol)
if self.selected == 'letters':
self.keyboard_letters.set_text(self.text)
value = self.keyboard_letters.show()
self.text = self.keyboard_letters.text
if value == 'symbols':
self.selected = value
self.show()
elif self.selected == 'symbols':
self.keyboard_symbols.set_text(self.text)
value = self.keyboard_symbols.show()
self.text = self.keyboard_symbols.text
if value == 'letters':
self.selected = value
if value == 'enter':
return self.text # When the user pressed enter the entered text value is returned
elif value == 'cancel':
return self.text_original # When the user chose to cancel the original text value is returned