1 """ Original code from https://github.com/skullydazed/kle2xy
5 from decimal
import Decimal
9 """Abstract interface for interacting with a KLE layout.
11 def __init__(self
, layout
=None, name
='', invert_y
=True):
12 super(KLE2xy
, self
).__init
__()
15 self
.invert_y
= invert_y
16 self
.key_width
= Decimal('19.05')
17 self
.key_skel
= {'decal': False, 'border_color': 'none', 'keycap_profile': '', 'keycap_color': 'grey', 'label_color': 'black', 'label_size': 3, 'label_style': 4, 'width': Decimal('1'), 'height': Decimal('1'), 'x': Decimal('0'), 'y': Decimal('0')}
18 self
.rows
= Decimal(0)
19 self
.columns
= Decimal(0)
22 self
.parse_layout(layout
)
26 """Returns the width of the keyboard plate.
28 return (Decimal(self
.columns
) * self
.key_width
) + self
.key_width
/ 2
32 """Returns the height of the keyboard plate.
34 return (self
.rows
* self
.key_width
) + self
.key_width
/ 2
38 """Returns the size of the keyboard plate.
40 return (self
.width
, self
.height
)
42 def attrs(self
, properties
):
43 """Parse the keyboard properties dictionary.
45 # FIXME: Store more than just the keyboard name.
46 if 'name' in properties
:
47 self
.name
= properties
['name']
49 def parse_layout(self
, layout
): # noqa FIXME(skullydazed): flake8 says this has a complexity of 25, it should be refactored.
50 # Wrap this in a dictionary so hjson will parse KLE raw data
51 layout
= '{"layout": [' + layout
+ ']}'
52 layout
= hjson
.loads(layout
)['layout']
54 # Initialize our state machine
55 current_key
= self
.key_skel
.copy()
56 current_row
= Decimal(0)
57 current_col
= Decimal(0)
59 current_y
= self
.key_width
/ 2
61 if isinstance(layout
[0], dict):
65 for row_num
, row
in enumerate(layout
):
68 # Process the current row
70 if isinstance(key
, dict):
71 if 'w' in key
and key
['w'] != Decimal(1):
72 current_key
['width'] = Decimal(key
['w'])
73 if 'w2' in key
and 'h2' in key
and key
['w2'] == 1.5 and key
['h2'] == 1:
74 # FIXME: ISO Key uses these params: {x:0.25,w:1.25,h:2,w2:1.5,h2:1,x2:-0.25}
75 current_key
['isoenter'] = True
76 if 'h' in key
and key
['h'] != Decimal(1):
77 current_key
['height'] = Decimal(key
['h'])
79 current_key
['label_style'] = self
.key_skel
['label_style'] = int(key
['a'])
80 if current_key
['label_style'] < 0:
81 current_key
['label_style'] = 0
82 elif current_key
['label_style'] > 9:
83 current_key
['label_style'] = 9
85 font_size
= int(key
['f'])
90 current_key
['label_size'] = self
.key_skel
['label_size'] = font_size
92 current_key
['keycap_profile'] = self
.key_skel
['keycap_profile'] = key
['p']
94 current_key
['keycap_color'] = self
.key_skel
['keycap_color'] = key
['c']
96 # FIXME: Need to do better validation, plus figure out how to support multiple colors
98 key
['t'] = key
['t'].split('\n')[0]
101 current_key
['label_color'] = self
.key_skel
['label_color'] = key
['t']
103 current_col
+= Decimal(key
['x'])
104 current_x
+= Decimal(key
['x']) * self
.key_width
106 current_row
+= Decimal(key
['y'])
107 current_y
+= Decimal(key
['y']) * self
.key_width
109 current_key
['decal'] = True
112 current_key
['name'] = key
113 current_key
['row'] = current_row
114 current_key
['column'] = current_col
116 # Determine the X center
117 x_center
= (current_key
['width'] * self
.key_width
) / 2
118 current_x
+= x_center
119 current_key
['x'] = current_x
120 current_x
+= x_center
122 # Determine the Y center
123 y_center
= (current_key
['height'] * self
.key_width
) / 2
124 y_offset
= y_center
- (self
.key_width
/ 2)
125 current_key
['y'] = (current_y
+ y_offset
)
127 # Tend to our row/col count
128 current_col
+= current_key
['width']
129 if current_col
> self
.columns
:
130 self
.columns
= current_col
132 # Invert the y-axis if neccesary
134 current_key
['y'] = -current_key
['y']
137 self
[-1].append(current_key
)
138 current_key
= self
.key_skel
.copy()
140 # Move to the next row
142 current_y
+= self
.key_width
143 current_col
= Decimal(0)
144 current_row
+= Decimal(1)
145 if current_row
> self
.rows
:
146 self
.rows
= Decimal(current_row
)