|
| 1 | +import wx |
| 2 | +from odmtools.view.WizardVariableView import WizardVariableView |
| 3 | +from wx.wizard import WizardPageSimple |
| 4 | +from odmtools.odmdata import Variable |
| 5 | + |
| 6 | + |
| 7 | +class WizardVariableController(WizardPageSimple): |
| 8 | + def __init__(self, parent, service_manager, current_variable): |
| 9 | + WizardPageSimple.__init__(self, parent) |
| 10 | + |
| 11 | + self.service_manager = service_manager |
| 12 | + self.current_variable = current_variable |
| 13 | + main_sizer = wx.BoxSizer(wx.VERTICAL) |
| 14 | + self.variable_view = WizardVariableView(self) |
| 15 | + main_sizer.Add(self.variable_view, 1, wx.EXPAND | wx.RIGHT, -16) |
| 16 | + self.SetSizer(main_sizer) |
| 17 | + |
| 18 | + table_columns = ["Code", "Name", "Speciation", "Units", |
| 19 | + "Sample Medium", "Value Type", "IsRegular", "Time Support", |
| 20 | + "Time Units", "DataType", "Genaral Category", "NoDataValue", "ID"] |
| 21 | + self.variable_view.variable_table.set_columns(table_columns) |
| 22 | + self.on_current_radio(None) |
| 23 | + |
| 24 | + self.__fetch_data() |
| 25 | + self.variable_view.current_variable_radio.Bind(wx.EVT_RADIOBUTTON, self.on_current_radio) |
| 26 | + self.variable_view.existing_variable_radio.Bind(wx.EVT_RADIOBUTTON, self.on_existing_radio) |
| 27 | + self.variable_view.create_variable_radio.Bind(wx.EVT_RADIOBUTTON, self.on_create_radio) |
| 28 | + |
| 29 | + def on_current_radio(self, event): |
| 30 | + self.variable_view.variable_table.Enable(False) |
| 31 | + self.__set_create_variable_section(False) |
| 32 | + |
| 33 | + def on_create_radio(self, event): |
| 34 | + self.variable_view.variable_table.Enable(False) |
| 35 | + self.__set_create_variable_section(True) |
| 36 | + |
| 37 | + def on_existing_radio(self, event): |
| 38 | + self.variable_view.variable_table.Enable(True) |
| 39 | + self.__set_create_variable_section(False) |
| 40 | + |
| 41 | + def __set_create_variable_section(self, active): |
| 42 | + if not isinstance(active, bool): |
| 43 | + raise Exception("active must be type bool") |
| 44 | + |
| 45 | + self.variable_view.variable_code_text_ctrl.Enable(active) |
| 46 | + self.variable_view.variable_name_combo.Enable(active) |
| 47 | + self.variable_view.variable_type_combo.Enable(active) |
| 48 | + self.variable_view.no_data_value_text_ctrl.Enable(active) |
| 49 | + self.variable_view.speciation_combo.Enable(active) |
| 50 | + self.variable_view.definition_text_ctrl.Enable(active) |
| 51 | + |
| 52 | + def __fetch_data(self): |
| 53 | + self.__populate_variable_table() |
| 54 | + |
| 55 | + cv_service = self.service_manager.get_cv_service() |
| 56 | + name_list = [x.term for x in cv_service.get_variable_name_cvs()] |
| 57 | + var_unit = [x.name for x in cv_service.get_units_names()] |
| 58 | + spec_list = [x.term for x in cv_service.get_speciation_cvs()] |
| 59 | + |
| 60 | + self.variable_view.variable_name_combo.AppendItems(name_list) |
| 61 | + self.variable_view.speciation_combo.AppendItems(spec_list) |
| 62 | + self.variable_view.variable_type_combo.AppendItems(var_unit) |
| 63 | + |
| 64 | + def __populate_variable_table(self): |
| 65 | + series_serivce = self.service_manager.get_series_service() |
| 66 | + variables = series_serivce.get_all_variables() |
| 67 | + data = [] |
| 68 | + for var in variables: |
| 69 | + data.append([var.code, |
| 70 | + var.name, |
| 71 | + var.speciation, |
| 72 | + var.variable_unit.name, |
| 73 | + var.sample_medium, |
| 74 | + var.value_type, |
| 75 | + var.is_regular, |
| 76 | + var.time_support, |
| 77 | + var.time_unit.name, |
| 78 | + var.data_type, |
| 79 | + var.general_category, |
| 80 | + var.no_data_value, |
| 81 | + var.id]) |
| 82 | + |
| 83 | + self.variable_view.variable_table.set_table_content(data=data) |
| 84 | + |
| 85 | + def get_variable(self): |
| 86 | + v = Variable() |
| 87 | + if self.variable_view.current_variable_radio.GetValue(): |
| 88 | + v = self.current_variable |
| 89 | + elif self.variable_view.existing_variable_radio.GetValue(): |
| 90 | + row = self.variable_view.variable_table.get_selected_row() |
| 91 | + code = row[0] |
| 92 | + v = self.service_manager.get_series_service().get_variable_by_code(code) |
| 93 | + |
| 94 | + elif self.variable_view.create_variable_radio.GetValue(): |
| 95 | + # v = self.createdVar |
| 96 | + v = self.get_new_variable() |
| 97 | + |
| 98 | + return v |
| 99 | + |
| 100 | + def get_new_variable(self): |
| 101 | + v = Variable() |
| 102 | + v.code = self.variable_view.variable_code_text_ctrl.GetValue() if self.variable_view.variable_code_text_ctrl.GetValue() <> "" else None |
| 103 | + v.name = self.variable_view.variable_name_combo.GetValue() if self.variable_view.variable_name_combo.GetValue() <> "" else None |
| 104 | + v.speciation = self.variable_view.speciation_combo.GetValue() if self.variable_view.speciation_combo.GetValue() <> "" else None |
| 105 | + v.variable_unit = self.service_manager.get_series_service() |
| 106 | + v.no_data_value = self.variable_view.no_data_value_text_ctrl.GetValue() if self.variable_view.no_data_value_text_ctrl.GetValue() <> "" else None |
| 107 | + # Need unit name, time support but neither of them are in the form... |
| 108 | + |
| 109 | + return v |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
0 commit comments