Skip to content

Commit c588739

Browse files
author
Davide Rosa
committed
ListView for procedures and functions.
1 parent cce026d commit c588739

File tree

1 file changed

+22
-142
lines changed

1 file changed

+22
-142
lines changed

parser_module.py

Lines changed: 22 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,25 @@ def fread_lines(file_path_and_name):
1717
return content
1818

1919

20-
class KRLModuleSrcFileParser(parser_instructions.KRLGenericParser):
20+
class KRLModuleSrcFileParser(parser_instructions.KRLGenericParser, gui.HBox):
2121
file_path_name = '' # the str path and file
22-
2322
def __init__(self, file_path_name):
2423
# read all instructions, parse and collect definitions
24+
self.krl_procedures_and_functions_list = []
2525
self.indent_comments = False
2626
self.file_path_name = file_path_name
2727
permissible_instructions = ['procedure begin', 'function begin']
2828
permissible_instructions_dictionary = {k:v for k,v in parser_instructions.instructions_defs.items() if k in permissible_instructions}
2929
parser_instructions.KRLGenericParser.__init__(self, permissible_instructions_dictionary)
30+
31+
gui.HBox.__init__(self)
32+
self.append(gui.ListView(), 'list')
33+
self.children['list'].onselection.do(self.on_proc_list_selected)
3034

35+
def on_proc_list_selected(self, widget, selected_key):
36+
self.append(widget.children[selected_key].node, 'proc_to_view')
37+
widget.children[selected_key].node.draw()
38+
3139
def parse_single_instruction(self, code_line_original, code_line, instruction_name, match_groups, file_lines):
3240
translation_result_tmp = []
3341

@@ -41,7 +49,10 @@ def parse_single_instruction(self, code_line_original, code_line, instruction_na
4149
translation_result_tmp.append( "def " + procedure_name + "(" + ", ".join(param_names) + "):" )
4250

4351
node = parser_instructions.KRLProcedureParser( procedure_name, param_names )
44-
self.append(node)
52+
#self.append(node)
53+
li = gui.ListItem(node.name)
54+
li.node = node
55+
self.children['list'].append(li)
4556
_translation_result_tmp, file_lines = node.parse(file_lines)
4657
if len(_translation_result_tmp):
4758
translation_result_tmp.extend(_translation_result_tmp)
@@ -57,7 +68,9 @@ def parse_single_instruction(self, code_line_original, code_line, instruction_na
5768
translation_result_tmp.append( "def " + procedure_name + "(" + ", ".join(param_names) + "): #function returns %s"%return_value_type_name )
5869

5970
node = parser_instructions.KRLFunctionParser( procedure_name, param_names, return_value_type_name )
60-
self.append(node)
71+
li = gui.ListItem(node.name)
72+
li.node = node
73+
self.children['list'].append(li)
6174
_translation_result_tmp, file_lines = node.parse(file_lines)
6275
if len(_translation_result_tmp):
6376
translation_result_tmp.extend(_translation_result_tmp)
@@ -68,53 +81,6 @@ def parse_single_instruction(self, code_line_original, code_line, instruction_na
6881

6982
return translation_result_tmp, file_lines
7083

71-
def recalc_size_and_arrange_children(self):
72-
#remove all drawings prior to redraw it
73-
for k in self.drawings_keys:
74-
self.remove_child(self.children[k])
75-
self.drawings_keys = []
76-
77-
w = max( len(self.box_text_content) * self.text_letter_width, 200 )
78-
h = self.box_height
79-
80-
w_max = w
81-
#estimate self width
82-
for k in self._render_children_list:
83-
v = self.children[k]
84-
v.draw()
85-
w_max = max(w_max, float(v.attr_width))
86-
87-
gap_between_elements = 70
88-
#set position for children
89-
for k in self._render_children_list:
90-
v = self.children[k]
91-
v.set_position(-float(v.attr_width)/2, h + gap_between_elements)
92-
h = h + float(v.attr_height) + gap_between_elements
93-
94-
gui._MixinSvgSize.set_size(self, w_max+self.margins*2, h+self.margins*2)
95-
96-
self.set_viewbox(-w_max/2-self.margins, -self.margins, w_max+self.margins*2, h+self.margins*2)
97-
98-
return w_max+self.margins*2, h+self.margins*2
99-
100-
#overloads FlowInstruction.draw§()
101-
def draw(self):
102-
self.box_height = 50
103-
w, h = self.recalc_size_and_arrange_children()
104-
105-
#central box
106-
self.drawings_keys.append( self.append(gui.SvgRectangle(-w/2, 0, w, h), 'box') )
107-
108-
#text
109-
txt = gui.SvgText(0, self.box_height/2, self.box_text_content)
110-
txt.attributes['text-anchor'] = 'middle'
111-
txt.attributes['dominant-baseline'] = 'middle' #'central'
112-
self.drawings_keys.append( self.append(txt, 'text') )
113-
114-
self.children['box'].set_stroke(1, 'black')
115-
self.children['box'].set_fill('transparent')
116-
117-
11884

11985
class KRLModuleDatFileParser(parser_instructions.KRLGenericParser):
12086
file_path_name = '' # the str path and file
@@ -130,53 +96,14 @@ def __init__(self, file_path_name):
13096
permissible_instructions_dictionary = {k:v for k,v in parser_instructions.instructions_defs.items() if k in permissible_instructions}
13197
self.permissible_instructions_dictionary.update(permissible_instructions_dictionary)
13298

133-
def parse_single_instruction(self, code_line_original, code_line, instruction_name, match_groups, file_lines):
134-
translation_result_tmp = []
13599

136-
if instruction_name == 'dat begin':
137-
return translation_result_tmp, file_lines
138-
139-
if instruction_name == 'dat end':
140-
return translation_result_tmp, file_lines
141-
142-
_translation_result_tmp, file_lines = parser_instructions.KRLGenericParser.parse_single_instruction(self, code_line_original, code_line, instruction_name, match_groups, file_lines)
143-
if len(_translation_result_tmp):
144-
translation_result_tmp.extend(_translation_result_tmp)
145-
146-
return translation_result_tmp, file_lines
147-
148-
#overloads FlowInstruction.draw§()
149-
def draw(self):
150-
self.box_height = 50
151-
w, h = self.recalc_size_and_arrange_children()
152-
153-
title_box_width = max( len(self.box_text_content) * self.text_letter_width, 200 )
154-
155-
self.drawings_keys.append( self.append(gui.SvgRectangle(-w/2, 0, title_box_width, self.box_height), 'title') )
156-
#central box
157-
self.drawings_keys.append( self.append(gui.SvgRectangle(-w/2, self.box_height, w, h), 'box') )
158-
159-
#text
160-
txt = gui.SvgText(-w/2 + title_box_width/2, self.box_height/2, self.box_text_content)
161-
#txt.attr_textLength = w-w*0.1
162-
#txt.attr_lengthAdjust = 'spacingAndGlyphs' # 'spacing'
163-
txt.attributes['text-anchor'] = 'middle'
164-
txt.attributes['dominant-baseline'] = 'middle' #'central'
165-
self.drawings_keys.append( self.append(txt, 'text') )
166-
167-
self.children['title'].set_stroke(1, 'black')
168-
self.children['title'].set_fill('lightyellow')
169-
170-
self.children['box'].set_stroke(1, 'black')
171-
self.children['box'].set_fill('transparent')
172-
173-
174-
class KRLModule(flow_chart_graphics.FlowInstruction):
100+
class KRLModule(gui.VBox):
175101
name = ''
176102
module_dat = None # KRLDat instance
177103
module_src = None # KRLSrc instance
178-
def __init__(self, module_name, dat_path_and_file = '', src_path_and_file = '', imports_to_prepend = ''):
179-
flow_chart_graphics.FlowInstruction.__init__(self)
104+
def __init__(self, module_name, dat_path_and_file = '', src_path_and_file = '', imports_to_prepend = '', *args, **kwargs):
105+
super(KRLModule, self).__init__(*args, **kwargs)
106+
self.append(gui.Label("MODULE: %s"%module_name))
180107
self.name = module_name
181108
if len(dat_path_and_file):
182109
self.module_dat = KRLModuleDatFileParser(dat_path_and_file)
@@ -195,7 +122,6 @@ def __init__(self, module_name, dat_path_and_file = '', src_path_and_file = '',
195122
if len(src_path_and_file):
196123
has_dat = not (self.module_dat is None)
197124
self.module_src = KRLModuleSrcFileParser(src_path_and_file)
198-
self.module_src.box_text_content = "SRC %s"%module_name
199125
self.append(self.module_src)
200126
file_lines = fread_lines(src_path_and_file)
201127
translation_result, file_lines = self.module_src.parse(file_lines)
@@ -204,53 +130,7 @@ def __init__(self, module_name, dat_path_and_file = '', src_path_and_file = '',
204130
f.write(imports_to_prepend)
205131
for l in translation_result:
206132
f.write(l + '\n')
207-
self.box_text_content = self.name
208-
self.draw()
209-
210-
def recalc_size_and_arrange_children(self):
211-
#remove all drawings prior to redraw it
212-
for k in self.drawings_keys:
213-
self.remove_child(self.children[k])
214-
self.drawings_keys = []
215-
216-
w = max( len(self.box_text_content) * self.text_letter_width, 200 )
217-
h = self.box_height
218-
219-
w_max = w
220-
#estimate self width
221-
for k in self._render_children_list:
222-
v = self.children[k]
223-
v.draw()
224-
w_max = max(w_max, float(v.attr_width))
225-
226-
#set position for children
227-
for k in self._render_children_list:
228-
v = self.children[k]
229-
v.set_position(-float(v.attr_width)/2, h)
230-
h = h + float(v.attr_height)
231-
232-
gui._MixinSvgSize.set_size(self, w_max+self.margins*2, h+self.margins*2)
233-
234-
self.set_viewbox(-w_max/2-self.margins, -self.margins, w_max+self.margins*2, h+self.margins*2)
235-
236-
return w_max+self.margins*2, h+self.margins*2
237-
238-
#overloads FlowInstruction.draw§()
239-
def draw(self):
240-
self.box_height = 50
241-
w, h = self.recalc_size_and_arrange_children()
242-
243-
#central box
244-
self.drawings_keys.append( self.append(gui.SvgRectangle(-w/2, 0, w, h), 'box') )
245-
246-
#text
247-
txt = gui.SvgText(0, self.box_height/2, self.box_text_content)
248-
txt.attributes['text-anchor'] = 'middle'
249-
txt.attributes['dominant-baseline'] = 'middle' #'central'
250-
self.drawings_keys.append( self.append(txt, 'text') )
251-
252-
self.children['box'].set_stroke(1, 'black')
253-
self.children['box'].set_fill('transparent')
254133

134+
255135

256136

0 commit comments

Comments
 (0)