-
Notifications
You must be signed in to change notification settings - Fork 0
/
panel_with_params_grid.py
161 lines (128 loc) · 4.7 KB
/
panel_with_params_grid.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
import wx
import wx.xrc as xrc
import wx.grid
import xml_utils
max_rows_default = 20
class panel_with_params_grid(wx.Panel):
"""This is a base class for a panel that contains a wx.Grid
specifically called self.params_grid that is used to basically
display and interact with a dictionary."""
def clear_params_grid(self):
self.params_grid.SetCellValue(0,0, "parameter")
self.params_grid.SetCellValue(0,1, "value")
max_rows = self._get_max_rows()
for i in range(1,max_rows):
self.params_grid.SetCellValue(i,0, "")
self.params_grid.SetCellValue(i,1, "")
def set_max_rows(self, max_rows):
self.max_rows = max_rows
def _get_max_rows(self):
if hasattr(self, 'max_rows'):
return self.max_rows
else:
return max_rows_default
def build_params_dict(self):
params_dict = {}
exit_code = 0
max_rows = self._get_max_rows()
for i in range(1,max_rows):
key = self.params_grid.GetCellValue(i,0)
val = self.params_grid.GetCellValue(i,1)
key = key.strip()
val = val.strip()
val = xml_utils.full_clean(val)
if not key:
break
elif not val:
## msg = 'Empty parameters are not allow: %s' % key
## wx.MessageBox(msg, 'Parameter Error',
## wx.OK | wx.ICON_ERROR)
## exit_code = 1
## break
val = None#do I really want to make this explicit, or
#just leave it blank?
params_dict[key] = val
print('params_dict = %s' % params_dict)
return params_dict
def set_grid_val(self, prop, value):
i = 0
max_rows = self._get_max_rows()
while i < max_rows:
attr = self.params_grid.GetCellValue(i, 0)
if attr == prop:
self.params_grid.SetCellValue(i, 1, str(value))
return
else:
i += 1
def delete_grid_rows(self, prop_list):
i = 0
max_rows = self._get_max_rows()
while i < max_rows:
prop = self.params_grid.GetCellValue(i, 0)
if prop in prop_list:
self.params_grid.DeleteRows(i, 1)
else:
i += 1
def find_first_empty_row(self):
i = 0
max_rows = self._get_max_rows()
while i < max_rows:
prop = self.params_grid.GetCellValue(i, 0)
if not prop:
return i
else:
i += 1
def set_cell_append_if_necessary(self, row, col=0, val=''):
n_rows = self.params_grid.GetNumberRows()
if row >= n_rows:
self.params_grid.AppendRows(1)
row = self.find_first_empty_row()
self.params_grid.SetCellValue(row, col, val)
def append_rows(self, prop_list):
print('in append_rows')
print('prop_list = ' + str(prop_list))
start_ind = self.find_first_empty_row()
print('start_ind = ' + str(start_ind))
for i, prop in enumerate(prop_list):
self.set_cell_append_if_necessary(i+start_ind, 0, prop)
def get_existing_props(self):
prop_list = []
max_rows = self._get_max_rows()
for i in range(1, max_rows):
prop = self.params_grid.GetCellValue(i, 0)
if prop:
prop_list.append(prop)
else:
return prop_list
def append_rows_if_missing(self, prop_list):
existing_rows = self.get_existing_props()
new_items = [item for item in prop_list if item not in existing_rows]
self.append_rows(new_items)
def autosize_columns(self, event=0):
self.params_grid.AutoSizeColumns()
def get_grid_val(self, prop):
i = 0
max_rows = self._get_max_rows()
while i < max_rows:
attr = self.params_grid.GetCellValue(i, 0)
if attr == prop:
val = self.params_grid.GetCellValue(i, 1)
return val.strip()
else:
i += 1
def display_params(self, params_dict, filt_func=None):
self.clear_params_grid()
keys = params_dict.keys()
keys.sort()
if filt_func is None:
filt_keys = keys
else:
filt_keys = filter(filt_func, keys)
for i, key in enumerate(filt_keys):
self.params_grid.SetCellValue(i+1,0, key)
val = params_dict[key]
if val is None:
val = ''
if type(val) not in [str, unicode]:
val = str(val)
self.params_grid.SetCellValue(i+1,1, val)