def button_clicked(self, event): log.debug("[Settings] Button clicked: {0}".format(IDS[event.GetId()])) button_id = event.GetId() keys = IDS[button_id].split(MODULE_KEY) if keys[-1] in ['list_add', 'list_remove']: self.list_operation(MODULE_KEY.join(keys[:-1]), action=keys[-1]) elif keys[-1] == 'apply_button': module_name = MODULE_KEY.join(keys[1:-1]) if self.save_settings(module_name): log.debug('Got non-dynamic changes') dialog = wx.MessageDialog(self, message=translate_key(MODULE_KEY.join(['main', 'save', 'non_dynamic'])), caption="Caption", style=wx.OK_DEFAULT, pos=wx.DefaultPosition) response = dialog.ShowModal() if response == wx.ID_YES: self.on_exit(event) else: event.StopPropagation() module_class = self.main_class.loaded_modules[module_name].get('class') if module_class: module_class.apply_settings() self.settings_saved = True elif keys[-1] == 'cancel_button': self.on_close(event) event.Skip()
def save_module(self, module_name, changed_items): non_dynamic_check = False if changed_items: module_settings = self.main_class.loaded_modules.get( module_name, {}) non_dynamic = module_settings.get('gui', {}).get('non_dynamic', []) module_config = module_settings.get('config') for item, change in changed_items.iteritems(): item_split = item.split(MODULE_KEY) if item_split[-1] in ['list_box']: del item_split[-1] section = item_split[-2] item_name = item_split[-1] deep_config = deep_get(module_config, *item_split[1:-1]) for d_item in non_dynamic: if section in d_item: if MODULE_KEY.join([section, '*']) in d_item: non_dynamic_check = True break elif MODULE_KEY.join([section, item_name]) in d_item: non_dynamic_check = True break item_type = type(deep_config[item_name]) deep_config[item_name] = item_type(change['value']) self.apply_custom_gui_settings(item, change['value']) if 'class' in module_settings: module_settings['class'].apply_settings() return non_dynamic_check
def list_operation(self, key, action): if action == 'list_add': list_input_value = wx.FindWindowById(get_id_from_name(MODULE_KEY.join([key, 'list_input']))).GetValue() try: list_input2 = wx.FindWindowById(get_id_from_name(MODULE_KEY.join([key, 'list_input2']), error=True)) list_input2_value = list_input2.GetValue() if list_input2 else None except ReferenceError: list_input2_value = None list_box = wx.FindWindowById(get_id_from_name(MODULE_KEY.join([key, 'list_box']))) list_box.AppendRows(1) row_count = list_box.GetNumberRows() - 1 list_box.SetCellValue(row_count, 0, list_input_value.strip()) if list_input2_value: list_box.SetCellValue(row_count, 1, list_input2_value.strip()) elif action == 'list_remove': list_box = wx.FindWindowById(get_id_from_name(MODULE_KEY.join([key, 'list_box']))) top = list_box.GetSelectionBlockTopLeft() bot = list_box.GetSelectionBlockBottomRight() if top and bot: top = top[0][0] bot = bot[0][0] + 1 del_rows = range(top, bot) if top < bot else range(bot, top) else: del_rows = [self.selected_cell[0]] if list_box.GetNumberRows(): ids_deleted = 0 for select in del_rows: list_box.DeleteRows(select - ids_deleted) ids_deleted += 1
def button_clicked(self, event): log.debug("[Settings] Button clicked: {0}".format( modules.interface.controls.IDS[event.GetId()])) button_id = event.GetId() keys = modules.interface.controls.IDS[button_id].split(MODULE_KEY) last_key = keys[-1] if last_key in ['list_add', 'list_remove']: self.on_list_operation(MODULE_KEY.join(keys[:-1]), action=last_key) elif last_key in ['ok_button', 'apply_button']: if self.save_settings(): log.debug('Got non-dynamic changes') dialog = wx.MessageDialog( self, message=translate_key( MODULE_KEY.join(['main', 'save', 'non_dynamic'])), caption="Caption", style=wx.OK_DEFAULT, pos=wx.DefaultPosition) dialog.ShowModal() if last_key == 'ok_button': self.on_exit(event) self.settings_saved = True elif last_key == 'cancel_button': self.on_close(event) event.Skip()
def button_clicked(self, event): log.debug("[Settings] Button clicked: {0}".format(IDS[event.GetId()])) button_id = event.GetId() keys = IDS[button_id].split(MODULE_KEY) if keys[-1] in ['list_add', 'list_remove']: self.list_operation(MODULE_KEY.join(keys[:-1]), action=keys[-1]) elif keys[-1] == 'apply_button': module_name = MODULE_KEY.join(keys[1:-1]) if self.save_settings(module_name): log.debug('Got non-dynamic changes') dialog = wx.MessageDialog( self, message=translate_key( MODULE_KEY.join(['main', 'save', 'non_dynamic'])), caption="Caption", style=wx.OK_DEFAULT, pos=wx.DefaultPosition) response = dialog.ShowModal() if response == wx.ID_YES: self.on_exit(event) else: event.StopPropagation() module_class = self.main_class.loaded_modules[module_name].get( 'class') if module_class: module_class.apply_settings() self.settings_saved = True elif keys[-1] == 'cancel_button': self.on_close(event) event.Skip()
def on_listbox_change(self, event): item_object = event.EventObject selection = item_object.get_key_from_index(item_object.GetSelection()) description = translate_key(MODULE_KEY.join([selection, 'description'])) item_key = modules.interface.controls.IDS[event.GetId()].split( MODULE_KEY) config_item_path = get_config_item_path(item_key[:-1]) module_class = deep_get(self.main_class.loaded_modules, *config_item_path) show_description = module_class.description if isinstance(item_object, KeyListBox): self.on_change(modules.interface.controls.IDS[event.GetId()], selection, item_type='listbox', section=True) if show_description: item_id_key = MODULE_KEY.join(item_key[:-1]) descr_static_text = wx.FindWindowById( modules.interface.controls.get_id_from_name( MODULE_KEY.join([item_id_key, 'descr_explain']))) descr_static_text.SetLabel(description) descr_static_text.Wrap(descr_static_text.GetSize()[0])
def create_list(self, parent, view, key, section, section_gui): is_dual = True if 'dual' in view else False style = wx.ALIGN_CENTER_VERTICAL item_sizer = wx.BoxSizer(wx.VERTICAL) addable_sizer = None if section_gui.get('addable', False): addable_sizer = wx.BoxSizer(wx.HORIZONTAL) item_input_key = MODULE_KEY.join([key, 'list_input']) addable_sizer.Add(wx.TextCtrl(parent, id=id_renew(item_input_key, update=True)), 0, style) if is_dual: item_input2_key = MODULE_KEY.join([key, 'list_input2']) addable_sizer.Add(wx.TextCtrl(parent, id=id_renew(item_input2_key, update=True)), 0, style) item_apply_key = MODULE_KEY.join([key, 'list_add']) item_apply_id = id_renew(item_apply_key, update=True) addable_sizer.Add(wx.Button(parent, id=item_apply_id, label=translate_key(item_apply_key)), 0, style) self.Bind(wx.EVT_BUTTON, self.button_clicked, id=item_apply_id) item_remove_key = MODULE_KEY.join([key, 'list_remove']) item_remove_id = id_renew(item_remove_key, update=True) addable_sizer.Add(wx.Button(parent, id=item_remove_id, label=translate_key(item_remove_key)), 0, style) self.Bind(wx.EVT_BUTTON, self.button_clicked, id=item_remove_id) item_sizer.Add(addable_sizer, 0, wx.EXPAND) list_box = wx.grid.Grid(parent, id=id_renew(MODULE_KEY.join([key, 'list_box']), update=True)) list_box.CreateGrid(0, 2 if is_dual else 1) list_box.DisableDragColSize() list_box.DisableDragRowSize() list_box.Bind(wx.grid.EVT_GRID_SELECT_CELL, self.select_cell) list_box.SetMinSize(wx.Size(-1, 100)) for index, (item, value) in enumerate(section.items()): list_box.AppendRows(1) if is_dual: list_box.SetCellValue(index, 0, item.decode('utf-8')) list_box.SetCellValue(index, 1, value.decode('utf-8')) else: list_box.SetCellValue(index, 0, item.decode('utf-8')) list_box.SetColLabelSize(1) list_box.SetRowLabelSize(1) if addable_sizer: col_size = addable_sizer.GetMinSize()[0] - 2 if is_dual: first_col_size = list_box.GetColSize(0) second_col_size = col_size - first_col_size if first_col_size < col_size else -1 list_box.SetColSize(1, second_col_size) else: list_box.SetDefaultColSize(col_size, resizeExistingCols=True) else: list_box.AutoSize() # Adding size of scrollbars size = list_box.GetEffectiveMinSize() size[0] += 18 size[1] += 18 list_box.SetMinSize(size) item_sizer.Add(list_box, 1, wx.EXPAND) return item_sizer
def fill_page_with_content(self, panel, keys): if keys[0] != 'settings': raise SettingsKeyError("Key is not for settings GUI") if keys[1] not in self.categories: raise CategoryKeyError("Key not found in categories") category = keys[1] module_id = keys[1] if not MODULE_KEY.join( keys[2:]) else MODULE_KEY.join(keys[2:]) if module_id not in self.categories[category]: raise ModuleKeyError("Key not found in modules") module_data = self.categories[category][module_id] custom_renderer = module_data.get('custom_renderer', False) module_config = module_data.get('config', {}) module_gui_config = module_data.get('gui', {}) if module_id not in self.sizer_dict: module_sizer = wx.BoxSizer(wx.VERTICAL) if custom_renderer: module_data['class'].render(sizer=module_sizer, panel=panel) else: self.create_page(sizer=module_sizer, panel=panel, config=module_config, gui=module_gui_config, key=module_id.split(MODULE_KEY)) hide_sizer_items(module_sizer) self.sizer_dict[module_id] = module_sizer page_sizer = panel.GetSizer() # type: wx.Sizer if not page_sizer: page_sizer = wx.BoxSizer(wx.VERTICAL) page_sizer.Add(self.sizer_dict[module_id], 1, wx.EXPAND) page_sizer.Show(0) panel.SetSizer(page_sizer) else: hide_sizer_items(page_sizer) found = False index = 0 for index, child in enumerate(page_sizer.GetChildren()): if self.sizer_dict[module_id] == child.GetSizer(): page_sizer.Show(index) found = True break if not found: page_sizer.Add(self.sizer_dict[module_id], 1, wx.EXPAND) page_sizer.Show(index + 1) page_sizer.Layout() panel.Layout()
def create_static_box(source_class, panel=None, value=None, gui=None, key=None, show_hidden=None, **kwargs): if isinstance(value, LCPanel): return wx.BoxSizer(wx.VERTICAL) item_value = value static_box = wx.StaticBox(panel, label=translate_key(MODULE_KEY.join(key))) static_sizer = wx.StaticBoxSizer(static_box, wx.VERTICAL) instatic_sizer = wx.BoxSizer(wx.VERTICAL) spacer_size = 7 max_text_size = 0 text_ctrls = [] log.debug("Working on {0}".format(MODULE_KEY.join(key))) spacer = False hidden_items = gui.get('hidden', []) for item, value in item_value.items(): if item in hidden_items and not show_hidden: continue view = type(value) if view in source_class.controls.keys(): bind_fn = source_class.controls[view] elif callable(value): bind_fn = source_class.controls['button'] else: # bind_fn = {'function': create_empty} raise GuiCreationError('Unable to create item, bad value map') item_dict = bind_fn['function'](source_class=source_class, panel=static_box, item=item, value=value, key=key + [item], bind=bind_fn.get('bind'), gui=gui.get(item, {}), from_sb=True) if 'text_size' in item_dict: if max_text_size < item_dict.get('text_size'): max_text_size = item_dict['text_size'] text_ctrls.append(item_dict['text_ctrl']) spacer = True if not spacer else instatic_sizer.AddSpacer(spacer_size) instatic_sizer.Add(item_dict['item'], 0, wx.EXPAND, 5) if max_text_size: for ctrl in text_ctrls: ctrl.SetMinSize((max_text_size + 50, ctrl.GetSize()[1])) item_count = instatic_sizer.GetItemCount() if not item_count: static_sizer.Destroy() return wx.BoxSizer(wx.VERTICAL) static_sizer.Add(instatic_sizer, 0, wx.EXPAND | wx.ALL, 5) return static_sizer
def on_listbox_change(self, event): item_object = event.EventObject selection = item_object.get_key_from_index(item_object.GetSelection()) description = translate_key(MODULE_KEY.join([selection, 'description'])) item_key = IDS[event.GetId()].split(MODULE_KEY) show_description = self.main_class.loaded_modules[item_key[0]]['gui'][item_key[1]].get('description', False) if show_description: item_id_key = MODULE_KEY.join(item_key[:-1]) descr_static_text = wx.FindWindowById(get_id_from_name(MODULE_KEY.join([item_id_key, 'descr_explain']))) descr_static_text.SetLabel(description) descr_static_text.Wrap(descr_static_text.GetSize()[0])
def fill_page_with_content(self, panel, setting_category, category_item, category_config): def create_button(button_key, function): button_id = id_renew(button_key, update=True) c_button = wx.Button(panel, id=button_id, label=translate_key(button_key)) self.Bind(wx.EVT_BUTTON, function, id=button_id) return c_button page_sizer = panel.GetSizer() # type: wx.Sizer if not page_sizer: page_sizer = wx.BoxSizer(wx.VERTICAL) else: page_sizer.DeleteWindows() # Creating sizer for page sizer = wx.BoxSizer(wx.VERTICAL) # Window for settings sizer.Add(self.fill_sc_with_config(panel, category_config, category_item), 1, wx.EXPAND) # Buttons button_sizer = wx.BoxSizer(wx.HORIZONTAL) for button_name in ['apply_button', 'cancel_button']: button_sizer.Add(create_button(MODULE_KEY.join([setting_category, category_item, button_name]), self.button_clicked), 0, wx.ALIGN_RIGHT) sizer.Add(button_sizer, 0, wx.ALIGN_RIGHT) page_sizer.Add(sizer, 1, wx.EXPAND) page_sizer.Layout() panel.SetSizer(page_sizer) panel.Layout()
def fill_sc_with_config(self, panel, category_config, category_item): page_sc_window = wx.ScrolledWindow(panel, id=id_renew(category_item), style=wx.VSCROLL) page_sc_window.SetScrollbars(5, 5, 10, 10) border_all = 5 sizer = wx.BoxSizer(wx.VERTICAL) for section_key, section_items in category_config['config'].items(): if section_key in SKIP_TAGS: continue static_key = MODULE_KEY.join([category_item, section_key]) static_box = wx.StaticBox(page_sc_window, label=translate_key(static_key)) static_sizer = wx.StaticBoxSizer(static_box, wx.VERTICAL) log.debug("Working on {0}".format(static_key)) static_sizer.Add( self.create_items( static_box, static_key, section_items, category_config.get('gui', {}).get(section_key, {})), 0, wx.EXPAND | wx.ALL, border_all) sizer.Add(static_sizer, 0, wx.EXPAND) page_sc_window.SetSizer(sizer) return page_sc_window
def create(self, panel, value="#FFFFFF", orientation=wx.HORIZONTAL, event=None, key=None, *args, **kwargs): item_sizer = wx.BoxSizer(orientation) self.event = event self.key = key label_panel = wx.Panel(panel, style=wx.BORDER_SIMPLE) label_sizer = wx.BoxSizer(wx.HORIZONTAL) label_sizer2 = wx.BoxSizer(wx.VERTICAL) label_text = wx.StaticText(label_panel, label=unicode(value), style=wx.ALIGN_CENTER) self.text = label_text label_sizer.Add(label_text, 1, wx.ALIGN_CENTER) label_sizer2.Add(label_sizer, 1, wx.ALIGN_CENTER) label_panel.SetSizer(label_sizer2) label_panel.SetBackgroundColour(str(value)) self.panel = label_panel button = wx.Button(panel, label=translate_key(MODULE_KEY.join(key + ['button']))) button.Bind(wx.EVT_BUTTON, self.on_button_press) border_size = wx.SystemSettings_GetMetric(wx.SYS_BORDER_Y) button_size = button.GetSize() if button_size[0] > 150: button_size[0] = 150 button_size[1] -= border_size*2 self.button = button label_panel.SetMinSize(button_size) label_panel.SetSize(button_size) item_sizer.Add(label_panel, 0, wx.ALIGN_CENTER) item_sizer.AddSpacer(2) item_sizer.Add(button, 0, wx.EXPAND) return item_sizer
def fill_page_with_content(self, panel, setting_category, category_item, category_config): def create_button(button_key, function): button_id = id_renew(button_key, update=True) c_button = wx.Button(panel, id=button_id, label=translate_key(button_key)) self.Bind(wx.EVT_BUTTON, function, id=button_id) return c_button page_sizer = panel.GetSizer() # type: wx.Sizer if not page_sizer: page_sizer = wx.BoxSizer(wx.VERTICAL) else: page_sizer.DeleteWindows() # Creating sizer for page sizer = wx.BoxSizer(wx.VERTICAL) # Window for settings sizer.Add( self.fill_sc_with_config(panel, category_config, category_item), 1, wx.EXPAND) # Buttons button_sizer = wx.BoxSizer(wx.HORIZONTAL) for button_name in ['apply_button', 'cancel_button']: button_sizer.Add( create_button( MODULE_KEY.join( [setting_category, category_item, button_name]), self.button_clicked), 0, wx.ALIGN_RIGHT) sizer.Add(button_sizer, 0, wx.ALIGN_RIGHT) page_sizer.Add(sizer, 1, wx.EXPAND) page_sizer.Layout() panel.SetSizer(page_sizer) panel.Layout()
def on_listbox_change(self, event): item_object = event.EventObject selection = item_object.get_key_from_index(item_object.GetSelection()) description = translate_key(MODULE_KEY.join([selection, 'description'])) item_key = IDS[event.GetId()].split(MODULE_KEY) show_description = self.main_class.loaded_modules[item_key[0]]['gui'][ item_key[1]].get('description', False) if show_description: item_id_key = MODULE_KEY.join(item_key[:-1]) descr_static_text = wx.FindWindowById( get_id_from_name( MODULE_KEY.join([item_id_key, 'descr_explain']))) descr_static_text.SetLabel(description) descr_static_text.Wrap(descr_static_text.GetSize()[0])
def create_spin(self, parent, view, key, section, section_gui, section_item=False, short_key=None): item_text = wx.StaticText(parent, label=translate_key(key), style=wx.ALIGN_RIGHT) key = key if section_item else MODULE_KEY.join([key, 'spin']) value = short_key if section_item else section item_box = wx.SpinCtrl(parent, id=id_renew(key, update=True), min=section_gui['min'], max=section_gui['max'], initial=value) return item_text, item_box
def create_textctrl(panel=None, value=None, key=None, bind=None, **kwargs): item_sizer = wx.BoxSizer(wx.HORIZONTAL) item_name = MODULE_KEY.join(key) item_box = wx.TextCtrl(panel, id=id_renew(item_name, update=True), value=unicode(value)) item_box.Bind(wx.EVT_TEXT, bind) item_text = wx.StaticText(panel, label=translate_key(item_name)) item_sizer.Add(item_text, 0, wx.ALIGN_CENTER) item_sizer.Add(item_box) return {'item': item_sizer, 'text_size': item_text.GetSize()[0], 'text_ctrl': item_text}
def create_checkbox(panel=None, value=None, key=None, bind=None, **kwargs): item_sizer = wx.BoxSizer(wx.HORIZONTAL) style = wx.ALIGN_CENTER_VERTICAL item_key = MODULE_KEY.join(key) item_box = wx.CheckBox(panel, id=id_renew(item_key, update=True), label=translate_key(item_key), style=style) item_box.SetValue(bool(value)) item_box.Bind(wx.EVT_CHECKBOX, bind) item_sizer.Add(item_box, 0, wx.ALIGN_LEFT) return {'item': item_sizer}
def create_dropdown(self, parent, view, key, section, section_gui, section_item=False, short_key=None): item_text = wx.StaticText(parent, label=translate_key(key), style=wx.ALIGN_RIGHT) choices = section_gui.get('choices') key = key if section_item else MODULE_KEY.join([key, 'dropdown']) item_box = KeyChoice(parent, id=id_renew(key, update=True), keys=choices, choices=choices) item_value = section[short_key] if section_item else section item_box.SetSelection(choices.index(item_value)) return item_text, item_box
def create_layout(self): self.main_grid = wx.BoxSizer(wx.HORIZONTAL) tree_ctrl_size = wx.Size(220, -1) style = wx.TR_DEFAULT_STYLE | wx.TR_HIDE_ROOT | wx.TR_TWIST_BUTTONS | wx.TR_NO_LINES # style = wx.TR_HAS_BUTTONS | wx.TR_SINGLE | wx.TR_HIDE_ROOT tree_ctrl_id = id_renew('settings.tree', update=True) tree_ctrl = wx.TreeCtrl(self, id=tree_ctrl_id, style=style) tree_ctrl.SetMinSize(tree_ctrl_size) root_key = MODULE_KEY.join(['settings', 'tree', 'root']) root_node = tree_ctrl.AddRoot(translate_key(root_key)) for item, value in self.categories.iteritems(): item_key = MODULE_KEY.join(['settings', item]) item_data = wx.TreeItemData() item_data.SetData(item_key) item_node = tree_ctrl.AppendItem(root_node, translate_key(item_key), data=item_data) for f_item, f_value in value.iteritems(): if not f_item == item: f_item_key = MODULE_KEY.join([item_key, f_item]) f_item_data = wx.TreeItemData() f_item_data.SetData(f_item_key) tree_ctrl.AppendItem(item_node, translate_key(f_item), data=f_item_data) tree_ctrl.ExpandAll() self.tree_ctrl = tree_ctrl self.Bind(wx.EVT_TREE_SEL_CHANGED, self.tree_ctrl_changed, id=tree_ctrl_id) self.main_grid.Add(self.tree_ctrl, 0, wx.EXPAND | wx.ALL, 7) content_page_id = id_renew(MODULE_KEY.join(['settings', 'content'])) self.content_page = wx.Panel(self, id=content_page_id) self.main_grid.Add(self.content_page, 1, wx.EXPAND) self.main_grid.Layout() self.SetSizer(self.main_grid) tree_ctrl.SelectItem(tree_ctrl.GetFirstChild(root_node)[0])
def on_close(self, event): dialog = wx.MessageDialog(self, message=translate_key(MODULE_KEY.join(['main', 'quit'])), caption="Caption", style=wx.YES_NO | wx.CANCEL, pos=wx.DefaultPosition) response = dialog.ShowModal() if response == wx.ID_YES: self.on_exit(event) else: event.StopPropagation()
def create_colour_picker(panel=None, value=None, key=None, bind=None, **kwargs): item_sizer = wx.BoxSizer(wx.HORIZONTAL) item_name = MODULE_KEY.join(key) colour_picker = CustomColourPickerCtrl() item_box = colour_picker.create(panel, value=value, event=bind, key=key) item_text = wx.StaticText(panel, label=translate_key(item_name)) item_sizer.Add(item_text, 0, wx.ALIGN_CENTER) item_sizer.Add(item_box, 1, wx.EXPAND) return {'item': item_sizer, 'text_size': item_text.GetSize()[0], 'text_ctrl': item_text}
def create_dropdown(panel=None, value=None, key=None, bind=None, gui=None, **kwargs): item_sizer = wx.BoxSizer(wx.HORIZONTAL) choices = value.list item_name = MODULE_KEY.join(key) item_text = wx.StaticText(panel, label=translate_key(item_name)) item_box = KeyChoice(panel, id=id_renew(item_name, update=True), keys=choices, choices=choices) item_box.Bind(wx.EVT_CHOICE, bind) item_box.SetSelection(choices.index(str(value))) item_sizer.Add(item_text, 0, wx.ALIGN_CENTER) item_sizer.Add(item_box) return {'item': item_sizer, 'text_size': item_text.GetSize()[0], 'text_ctrl': item_text}
def list_operation(self, key, action): if action == 'list_add': list_input_value = wx.FindWindowById( get_id_from_name(MODULE_KEY.join([key, 'list_input']))).GetValue() try: list_input2 = wx.FindWindowById( get_id_from_name(MODULE_KEY.join([key, 'list_input2']), error=True)) list_input2_value = list_input2.GetValue( ) if list_input2 else None except ReferenceError: list_input2_value = None list_box = wx.FindWindowById( get_id_from_name(MODULE_KEY.join([key, 'list_box']))) list_box.AppendRows(1) row_count = list_box.GetNumberRows() - 1 list_box.SetCellValue(row_count, 0, list_input_value.strip()) if list_input2_value: list_box.SetCellValue(row_count, 1, list_input2_value.strip()) elif action == 'list_remove': list_box = wx.FindWindowById( get_id_from_name(MODULE_KEY.join([key, 'list_box']))) top = list_box.GetSelectionBlockTopLeft() bot = list_box.GetSelectionBlockBottomRight() if top and bot: top = top[0][0] bot = bot[0][0] + 1 del_rows = range(top, bot) if top < bot else range(bot, top) else: del_rows = [self.selected_cell[0]] if list_box.GetNumberRows(): ids_deleted = 0 for select in del_rows: list_box.DeleteRows(select - ids_deleted) ids_deleted += 1
def on_close(self, event): dialog = wx.MessageDialog(self, message=translate_key( MODULE_KEY.join(['main', 'quit'])), caption="Caption", style=wx.YES_NO | wx.CANCEL, pos=wx.DefaultPosition) response = dialog.ShowModal() if response == wx.ID_YES: self.on_exit(event) else: event.StopPropagation()
def on_settings(self, event): log.debug("Got event from {0}".format(IDS[event.GetId()])) module_groups = IDS[event.GetId()].split(MODULE_KEY) settings_category = MODULE_KEY.join(module_groups[1:-1]) settings_menu_id = id_renew(settings_category, update=True) if self.settings_window: self.settings_window.SetFocus() else: self.settings_window = SettingsWindow(self, id=settings_menu_id, title=translate_key('settings'), size=(700, 400), main_class=self, categories=self.sorted_categories)
def create_spin(panel=None, value=None, key=None, bind=None, **kwargs): item_class = value item_sizer = wx.BoxSizer(wx.HORIZONTAL) item_name = MODULE_KEY.join(key) style = wx.ALIGN_LEFT item_box = wx.SpinCtrl(panel, id=id_renew(item_name, update=True), min=item_class.min, max=item_class.max, initial=value.simple(), style=style) item_text = wx.StaticText(panel, label=translate_key(item_name)) item_box.Bind(wx.EVT_SPINCTRL, bind) item_box.Bind(wx.EVT_TEXT, bind) item_sizer.Add(item_text, 0, wx.ALIGN_CENTER) item_sizer.Add(item_box) return {'item': item_sizer, 'text_size': item_text.GetSize()[0], 'text_ctrl': item_text}
def create_item(self, parent, view, key, section, section_gui): flex_grid = wx.FlexGridSizer(0, 2, ITEM_SPACING_VERT, ITEM_SPACING_HORZ) if not section: return wx.Sizer() for item, value in section.items(): if not self.show_hidden and item in section_gui.get('hidden', []): continue item_name = MODULE_KEY.join([key, item]) if item in section_gui: if 'list' in section_gui[item].get('view'): flex_grid.Add(self.create_list(parent, view, item_name, section, section_gui[item])) flex_grid.AddSpacer(wx.Size(0, 0)) elif 'choose' in section_gui[item].get('view'): flex_grid.Add(self.create_choose(parent, view, item_name, section, section_gui[item])) flex_grid.AddSpacer(wx.Size(0, 0)) elif 'dropdown' in section_gui[item].get('view'): text, control = self.create_dropdown(parent, view, item_name, section, section_gui[item], section_item=True, short_key=item) flex_grid.Add(text) flex_grid.Add(control) elif 'spin' in section_gui[item].get('view'): text, control = self.create_spin(parent, view, item_name, section, section_gui[item], section_item=True, short_key=section[item]) flex_grid.Add(text) flex_grid.Add(control) else: # Checking type of an item style = wx.ALIGN_CENTER_VERTICAL if value is None: # Button button_id = id_renew(item_name, update=True) item_button = wx.Button(parent, id=button_id, label=translate_key(item_name)) flex_grid.Add(item_button, 0, wx.ALIGN_LEFT) flex_grid.AddSpacer(wx.Size(0, 0)) self.main_class.Bind(wx.EVT_BUTTON, self.button_clicked, id=button_id) elif isinstance(value, bool): # Checkbox item_box = wx.CheckBox(parent, id=id_renew(item_name, update=True), label=translate_key(item_name), style=style) item_box.SetValue(value) flex_grid.Add(item_box, 0, wx.ALIGN_LEFT) flex_grid.AddSpacer(wx.Size(0, 0)) else: # TextCtrl item_box = wx.TextCtrl(parent, id=id_renew(item_name, update=True), value=str(value).decode('utf-8')) item_text = wx.StaticText(parent, label=translate_key(item_name), style=wx.ALIGN_RIGHT | wx.ALIGN_CENTER_HORIZONTAL) flex_grid.Add(item_text) flex_grid.Add(item_box) flex_grid.Fit(parent) return flex_grid
def create_slider(panel=None, value=None, key=None, bind=None, gui=None, **kwargs): item_sizer = wx.BoxSizer(wx.HORIZONTAL) item_name = MODULE_KEY.join(key) style = wx.SL_VALUE_LABEL | wx.SL_AUTOTICKS item_box = wx.Slider(panel, id=id_renew(item_name, update=True), minValue=gui['min'], maxValue=gui['max'], value=int(value), style=style) freq = (gui['max'] - gui['min'])/5 item_box.SetTickFreq(freq) item_box.SetLineSize(4) item_box.Bind(wx.EVT_SCROLL, bind) item_text = wx.StaticText(panel, label=translate_key(item_name)) item_sizer.Add(item_text, 0, wx.ALIGN_CENTER) item_sizer.Add(item_box, 1, wx.EXPAND) return {'item': item_sizer, 'text_size': item_text.GetSize()[0], 'text_ctrl': item_text}
def on_settings(self, event): log.debug("Got event from {0}".format(IDS[event.GetId()])) module_groups = IDS[event.GetId()].split(MODULE_KEY) settings_category = MODULE_KEY.join(module_groups[1:-1]) settings_menu_id = id_renew(settings_category, update=True) if self.settings_window: self.settings_window.SetFocus() else: self.settings_window = SettingsWindow( self, id=settings_menu_id, title=translate_key('settings'), size=(700, 400), main_class=self, categories=self.sorted_categories)
def save_settings(self): dynamic_check = False for module_name in self.main_class.loaded_modules.keys(): change_list = {} for item, change in self.changes.iteritems(): if module_name == item.split(MODULE_KEY)[0]: change_list[item] = change for key in change_list.keys(): self.changes.pop(key) if self.save_module(module_name, change_list): dynamic_check = True for button in self.buttons[MODULE_KEY.join( ['settings', 'apply_button'])]: button.Disable() return dynamic_check
def fill_sc_with_config(self, panel, category_config, category_item): page_sc_window = wx.ScrolledWindow(panel, id=id_renew(category_item), style=wx.VSCROLL) page_sc_window.SetScrollbars(5, 5, 10, 10) border_all = 5 sizer = wx.BoxSizer(wx.VERTICAL) for section_key, section_items in category_config['config'].items(): if section_key in SKIP_TAGS: continue static_key = MODULE_KEY.join([category_item, section_key]) static_box = wx.StaticBox(page_sc_window, label=translate_key(static_key)) static_sizer = wx.StaticBoxSizer(static_box, wx.VERTICAL) log.debug("Working on {0}".format(static_key)) static_sizer.Add(self.create_items(static_box, static_key, section_items, category_config.get('gui', {}).get(section_key, {})), 0, wx.EXPAND | wx.ALL, border_all) sizer.Add(static_sizer, 0, wx.EXPAND) page_sc_window.SetSizer(sizer) return page_sc_window
def create_button(source_class=None, panel=None, key=None, value=None, bind=None, enabled=True, multiple=None, **kwargs): item_sizer = wx.BoxSizer(wx.VERTICAL) item_name = MODULE_KEY.join(key) button_id = id_renew(item_name, update=True, multiple=multiple) c_button = wx.Button(panel, id=button_id, label=translate_key(item_name)) if not enabled: c_button.Disable() if item_name in source_class.buttons: source_class.buttons[item_name].append(c_button) else: source_class.buttons[item_name] = [c_button] if value: # TODO: Implement button function pressing if callable(value.value): c_button.Bind(wx.EVT_BUTTON, value.value, id=button_id) c_button.Bind(wx.EVT_BUTTON, bind, id=button_id) else: c_button.Bind(wx.EVT_BUTTON, bind, id=button_id) item_sizer.Add(c_button) return {'item': item_sizer}
def create_item(self, parent, view, key, section, section_gui): flex_grid = wx.FlexGridSizer(0, 2, ITEM_SPACING_VERT, ITEM_SPACING_HORZ) if not section: return wx.Sizer() for item, value in section.items(): if not self.show_hidden and item in section_gui.get('hidden', []): continue item_name = MODULE_KEY.join([key, item]) if item in section_gui: if 'list' in section_gui[item].get('view'): flex_grid.Add( self.create_list(parent, view, item_name, section, section_gui[item])) flex_grid.AddSpacer(wx.Size(0, 0)) elif 'choose' in section_gui[item].get('view'): flex_grid.Add( self.create_choose(parent, view, item_name, section, section_gui[item])) flex_grid.AddSpacer(wx.Size(0, 0)) elif 'dropdown' in section_gui[item].get('view'): text, control = self.create_dropdown(parent, view, item_name, section, section_gui[item], section_item=True, short_key=item) flex_grid.Add(text) flex_grid.Add(control) elif 'spin' in section_gui[item].get('view'): text, control = self.create_spin(parent, view, item_name, section, section_gui[item], section_item=True, short_key=section[item]) flex_grid.Add(text) flex_grid.Add(control) else: # Checking type of an item style = wx.ALIGN_CENTER_VERTICAL if value is None: # Button button_id = id_renew(item_name, update=True) item_button = wx.Button(parent, id=button_id, label=translate_key(item_name)) flex_grid.Add(item_button, 0, wx.ALIGN_LEFT) flex_grid.AddSpacer(wx.Size(0, 0)) self.main_class.Bind(wx.EVT_BUTTON, self.button_clicked, id=button_id) elif isinstance(value, bool): # Checkbox item_box = wx.CheckBox(parent, id=id_renew(item_name, update=True), label=translate_key(item_name), style=style) item_box.SetValue(value) flex_grid.Add(item_box, 0, wx.ALIGN_LEFT) flex_grid.AddSpacer(wx.Size(0, 0)) else: # TextCtrl item_box = wx.TextCtrl(parent, id=id_renew(item_name, update=True), value=str(value).decode('utf-8')) item_text = wx.StaticText(parent, label=translate_key(item_name), style=wx.ALIGN_RIGHT | wx.ALIGN_CENTER_HORIZONTAL) flex_grid.Add(item_text) flex_grid.Add(item_box) flex_grid.Fit(parent) return flex_grid
def save_settings(self, module): module_settings = self.main_class.loaded_modules.get(module, {}) non_dynamic = module_settings.get('gui', {}).get('non_dynamic', []) module_config = module_settings.get('config') non_dynamic_check = False if module_settings: parser = module_settings['parser'] # type: ConfigParser items = get_list_of_ids_from_module_name(module, return_tuple=True) for item, name in items: module_name, section, item_name = name.split(MODULE_KEY) if not parser.has_section(section): continue # Check for non-dynamic items for d_item in non_dynamic: if section in d_item: if MODULE_KEY.join([section, '*']) in d_item: non_dynamic_check = True break elif MODULE_KEY.join([section, item_name]) in d_item: non_dynamic_check = True break # Saving wx_window = wx.FindWindowById(item) if isinstance(wx_window, wx.CheckBox): if name == MODULE_KEY.join(['main', 'gui', 'show_hidden']): self.show_hidden = wx_window.IsChecked() parser.set(section, item_name, wx_window.IsChecked()) module_config[section][item_name] = wx_window.IsChecked() elif isinstance(wx_window, wx.TextCtrl): if item_name not in SKIP_TXT_CONTROLS: parser.set( section, item_name, wx_window.GetValue().encode('utf-8').strip()) module_config[section][item_name] = wx_window.GetValue( ).encode('utf-8').strip() elif isinstance(wx_window, wx.grid.Grid): col_count = wx_window.GetNumberCols() row_count = wx_window.GetNumberRows() parser_options = parser.options(section) grid_elements = [[ wx_window.GetCellValue(row, col).encode('utf-8').strip() for col in range(col_count) ] for row in range(row_count)] if not grid_elements: for option in parser_options: parser.remove_option(section, option) module_config[section].pop(option) else: item_list = [item[0] for item in grid_elements] for option in parser_options: if option not in item_list: module_config[section].pop(option) parser.remove_option(section, option) for elements in grid_elements: parser.set(section, *elements) if len(elements) == 1: module_config[section][elements[0]] = None elif len(elements) == 2: module_config[section][ elements[0]] = elements[1] elif isinstance(wx_window, wx.Button): if item_name not in SKIP_BUTTONS: parser.set(section, item_name) module_config[section][item_name] = None elif isinstance(wx_window, KeyListBox): item_id = wx_window.GetSelection() parser_options = parser.options(section) item_value = wx_window.get_key_from_index(item_id) if not item_value: for option in parser_options: parser.remove_option(section, option) module_config[section] = None else: for option in parser_options: parser.remove_option(section, option) parser.set(section, item_value) module_config[section] = item_value elif isinstance(wx_window, KeyCheckListBox): item_ids = wx_window.GetChecked() parser_options = parser.options(section) items_values = [ wx_window.get_key_from_index(item_id) for item_id in item_ids ] if not items_values: for option in parser_options: parser.remove_option(section, option) module_config[section].pop(option) else: for option in parser_options: if option not in items_values: parser.remove_option(section, option) module_config[section].pop(option) for value in items_values: parser.set(section, value) module_config[section][value] = None elif isinstance(wx_window, KeyChoice): item_id = wx_window.GetSelection() item_value = wx_window.get_key_from_index(item_id) parser.set(section, item_name, item_value) module_config[section][item_name] = item_value elif isinstance(wx_window, wx.SpinCtrl): item_value = wx_window.GetValue() parser.set(section, item_name, item_value) module_config[section][item_name] = item_value with open(module_settings['file'], 'w') as config_file: parser.write(config_file) return non_dynamic_check
def create_choose(panel=None, value=None, key=None, bind=None, **kwargs): item_list = value.value item_class = value is_single = False if value.multiple else True style = wx.LB_SINGLE if is_single else wx.LB_EXTENDED border_sizer = wx.BoxSizer(wx.VERTICAL) item_sizer = wx.BoxSizer(wx.VERTICAL) list_items = [] translated_items = [] static_label = MODULE_KEY.join(key) if not item_class.empty_label: static_text = wx.StaticText(panel, label=u'{}:'.format(translate_key(static_label)), style=wx.ALIGN_RIGHT) item_sizer.Add(static_text) if item_class.check_type in ['dir', 'folder', 'files']: check_type = item_class.check_type for item_in_list in os.listdir(os.path.join(PYTHON_FOLDER, item_class.folder)): item_path = os.path.join(PYTHON_FOLDER, item_class.folder, item_in_list) if check_type in ['dir', 'folder'] and os.path.isdir(item_path): list_items.append(item_in_list) elif check_type == 'files' and os.path.isfile(item_path): if not item_class.keep_extension: item_in_list = ''.join(os.path.basename(item_path).split('.')[:-1]) if '__init__' not in item_in_list: if item_in_list not in list_items: list_items.append(item_in_list) translated_items.append(translate_key(item_in_list)) item_key = MODULE_KEY.join(key + ['list_box']) label_text = translate_key(item_key) if label_text: item_sizer.Add(wx.StaticText(panel, label=label_text, style=wx.ALIGN_RIGHT)) if is_single: item_list_box = KeyListBox(panel, id=id_renew(item_key, update=True), keys=list_items, choices=translated_items if translated_items else list_items, style=style) else: item_list_box = KeyCheckListBox(panel, id=id_renew(item_key, update=True), keys=list_items, choices=translated_items if translated_items else list_items) item_list_box.Bind(wx.EVT_CHECKLISTBOX, bind['check_change']) item_list_box.Bind(wx.EVT_LISTBOX, bind['change']) section_for = item_list if not is_single else {item_list: None} if is_single: item, value = section_for.items()[0] if item not in item_list_box.GetItems(): if item_list_box.GetItems(): item_list_box.SetSelection(0) else: item_list_box.SetSelection(list_items.index(item)) else: check_items = [list_items.index(item) for item in section_for] item_list_box.SetChecked(check_items) if item_class.description: adv_sizer = wx.BoxSizer(wx.HORIZONTAL) adv_sizer.Add(item_list_box, 0, wx.EXPAND) descr_key = MODULE_KEY.join(key + ['descr_explain']) descr_text = wx.StaticText(panel, id=id_renew(descr_key, update=True), label=translate_key(descr_key), style=wx.ST_NO_AUTORESIZE) adv_sizer.Add(descr_text, 0, wx.EXPAND | wx.LEFT, 10) sizes = descr_text.GetSize() sizes[0] -= 20 descr_text.SetMinSize(sizes) descr_text.Fit() item_sizer.Add(adv_sizer) else: item_sizer.Add(item_list_box) border_sizer.Add(item_sizer, 0, wx.EXPAND | wx.ALL, 5) return border_sizer
def create_list(panel=None, value=None, key=None, bind=None, gui=None, from_sb=None, **kwargs): view = gui.get('view') is_dual = True if 'dual' in view else False style = wx.ALIGN_CENTER_VERTICAL border_sizer = wx.BoxSizer(wx.VERTICAL) item_sizer = wx.BoxSizer(wx.VERTICAL) static_label = MODULE_KEY.join(key) static_text = wx.StaticText(panel, label=u'{}:'.format(translate_key(static_label)), style=wx.ALIGN_RIGHT) item_sizer.Add(static_text) addable_sizer = wx.BoxSizer(wx.HORIZONTAL) if gui.get('addable') else None if addable_sizer: item_input_key = MODULE_KEY.join(key + ['list_input']) addable_sizer.Add(wx.TextCtrl(panel, id=id_renew(item_input_key, update=True)), 0, style) if is_dual: item_input2_key = MODULE_KEY.join(key + ['list_input2']) addable_sizer.Add(wx.TextCtrl(panel, id=id_renew(item_input2_key, update=True)), 0, style) item_apply_key = MODULE_KEY.join(key + ['list_add']) item_apply_id = id_renew(item_apply_key, update=True) item_apply = wx.Button(panel, id=item_apply_id, label=translate_key(item_apply_key)) addable_sizer.Add(item_apply, 0, style) item_apply.Bind(wx.EVT_BUTTON, bind['add'], id=item_apply_id) item_remove_key = MODULE_KEY.join(key + ['list_remove']) item_remove_id = id_renew(item_remove_key, update=True) item_remove = wx.Button(panel, id=item_remove_id, label=translate_key(item_remove_key)) addable_sizer.Add(item_remove, 0, style) item_remove.Bind(wx.EVT_BUTTON, bind['remove'], id=item_remove_id) item_sizer.Add(addable_sizer, 0, wx.EXPAND) list_box = wx.grid.Grid(panel, id=id_renew(MODULE_KEY.join(key + ['list_box']), update=True)) list_box.CreateGrid(0, 2 if is_dual else 1) list_box.DisableDragColSize() list_box.DisableDragRowSize() list_box.Bind(wx.grid.EVT_GRID_SELECT_CELL, bind['select']) list_box.Bind(wx.grid.EVT_GRID_CELL_CHANGED, bind['edit']) if is_dual: for index, (item, item_value) in enumerate(value.items()): list_box.AppendRows(1) list_box.SetCellValue(index, 0, item) list_box.SetCellValue(index, 1, item_value) else: for index, item in enumerate(value): list_box.AppendRows(1) list_box.SetCellValue(index, 0, item) list_box.SetColLabelSize(1) list_box.SetRowLabelSize(1) if addable_sizer: col_size = addable_sizer.GetMinSize()[0] - 2 if is_dual: first_col_size = list_box.GetColSize(0) second_col_size = col_size - first_col_size if first_col_size < col_size else -1 list_box.SetColSize(1, second_col_size) else: list_box.SetDefaultColSize(col_size, resizeExistingCols=True) else: list_box.AutoSize() item_sizer.Add(list_box) border_sizer.Add(item_sizer, 0, wx.EXPAND | wx.ALL, 5) if from_sb: return {'item': border_sizer} else: return border_sizer
def create_choose(self, parent, view, key, section, section_gui): is_single = True if 'single' in view else False description = section_gui.get('description', False) style = wx.LB_SINGLE if is_single else wx.LB_EXTENDED item_sizer = wx.BoxSizer(wx.VERTICAL) list_items = [] translated_items = [] if section_gui['check_type'] in ['dir', 'folder', 'files']: check_type = section_gui['check_type'] keep_extension = section_gui[ 'file_extension'] if 'file_extension' in section_gui else False for item_in_list in os.listdir( os.path.join(self.main_class.main_config['root_folder'], section_gui['check'])): item_path = os.path.join( self.main_class.main_config['root_folder'], section_gui['check'], item_in_list) if check_type in ['dir', 'folder' ] and os.path.isdir(item_path): list_items.append(item_in_list) elif check_type == 'files' and os.path.isfile(item_path): if not keep_extension: item_in_list = ''.join( os.path.basename(item_path).split('.')[:-1]) if '__init__' not in item_in_list: if item_in_list not in list_items: list_items.append(item_in_list) translated_items.append( translate_key(item_in_list)) elif section_gui['check_type'] == 'sections': parser = ConfigParser(allow_no_value=True) parser.read(section_gui.get('check', '')) for item in parser.sections(): list_items.append(translate_key(item)) item_key = MODULE_KEY.join([key, 'list_box']) label_text = translate_key(item_key) if label_text: item_sizer.Add( wx.StaticText(parent, label=label_text, style=wx.ALIGN_RIGHT)) if is_single: item_list_box = KeyListBox( parent, id=id_renew(item_key, update=True), keys=list_items, choices=translated_items if translated_items else list_items, style=style) else: item_list_box = KeyCheckListBox( parent, id=id_renew(item_key, update=True), keys=list_items, choices=translated_items if translated_items else list_items) self.Bind(wx.EVT_LISTBOX, self.on_listbox_change, item_list_box) section_for = section if not is_single else {section: None} if is_single: [ item_list_box.SetSelection(list_items.index(item)) for item, value in section_for.items() ] else: check_items = [ list_items.index(item) for item, value in section_for.items() ] item_list_box.SetChecked(check_items) if description: adv_sizer = wx.BoxSizer(wx.HORIZONTAL) adv_sizer.Add(item_list_box, 0, wx.EXPAND) descr_key = MODULE_KEY.join([key, 'descr_explain']) descr_text = wx.StaticText(parent, id=id_renew(descr_key, update=True), label=translate_key(descr_key), style=wx.ST_NO_AUTORESIZE) adv_sizer.Add(descr_text, 0, wx.EXPAND | wx.LEFT, 10) sizes = descr_text.GetSize() sizes[0] -= 20 descr_text.SetMinSize(sizes) descr_text.Fit() # descr_text.Wrap(descr_text.GetSize()[0]) item_sizer.Add(adv_sizer) else: item_sizer.Add(item_list_box) return item_sizer
def create_choose(self, parent, view, key, section, section_gui): is_single = True if 'single' in view else False description = section_gui.get('description', False) style = wx.LB_SINGLE if is_single else wx.LB_EXTENDED item_sizer = wx.BoxSizer(wx.VERTICAL) list_items = [] translated_items = [] if section_gui['check_type'] in ['dir', 'folder', 'files']: check_type = section_gui['check_type'] keep_extension = section_gui['file_extension'] if 'file_extension' in section_gui else False for item_in_list in os.listdir(os.path.join(self.main_class.main_config['root_folder'], section_gui['check'])): item_path = os.path.join(self.main_class.main_config['root_folder'], section_gui['check'], item_in_list) if check_type in ['dir', 'folder'] and os.path.isdir(item_path): list_items.append(item_in_list) elif check_type == 'files' and os.path.isfile(item_path): if not keep_extension: item_in_list = ''.join(os.path.basename(item_path).split('.')[:-1]) if '__init__' not in item_in_list: if item_in_list not in list_items: list_items.append(item_in_list) translated_items.append(translate_key(item_in_list)) elif section_gui['check_type'] == 'sections': parser = ConfigParser(allow_no_value=True) parser.read(section_gui.get('check', '')) for item in parser.sections(): list_items.append(translate_key(item)) item_key = MODULE_KEY.join([key, 'list_box']) label_text = translate_key(item_key) if label_text: item_sizer.Add(wx.StaticText(parent, label=label_text, style=wx.ALIGN_RIGHT)) if is_single: item_list_box = KeyListBox(parent, id=id_renew(item_key, update=True), keys=list_items, choices=translated_items if translated_items else list_items, style=style) else: item_list_box = KeyCheckListBox(parent, id=id_renew(item_key, update=True), keys=list_items, choices=translated_items if translated_items else list_items) self.Bind(wx.EVT_LISTBOX, self.on_listbox_change, item_list_box) section_for = section if not is_single else {section: None} if is_single: [item_list_box.SetSelection(list_items.index(item)) for item, value in section_for.items()] else: check_items = [list_items.index(item) for item, value in section_for.items()] item_list_box.SetChecked(check_items) if description: adv_sizer = wx.BoxSizer(wx.HORIZONTAL) adv_sizer.Add(item_list_box, 0, wx.EXPAND) descr_key = MODULE_KEY.join([key, 'descr_explain']) descr_text = wx.StaticText(parent, id=id_renew(descr_key, update=True), label=translate_key(descr_key), style=wx.ST_NO_AUTORESIZE) adv_sizer.Add(descr_text, 0, wx.EXPAND | wx.LEFT, 10) sizes = descr_text.GetSize() sizes[0] -= 20 descr_text.SetMinSize(sizes) descr_text.Fit() # descr_text.Wrap(descr_text.GetSize()[0]) item_sizer.Add(adv_sizer) else: item_sizer.Add(item_list_box) return item_sizer
def save_settings(self, module): module_settings = self.main_class.loaded_modules.get(module, {}) non_dynamic = module_settings.get('gui', {}).get('non_dynamic', []) module_config = module_settings.get('config') non_dynamic_check = False if module_settings: parser = module_settings['parser'] # type: ConfigParser items = get_list_of_ids_from_module_name(module, return_tuple=True) for item, name in items: module_name, section, item_name = name.split(MODULE_KEY) if not parser.has_section(section): continue # Check for non-dynamic items for d_item in non_dynamic: if section in d_item: if MODULE_KEY.join([section, '*']) in d_item: non_dynamic_check = True break elif MODULE_KEY.join([section, item_name]) in d_item: non_dynamic_check = True break # Saving wx_window = wx.FindWindowById(item) if isinstance(wx_window, wx.CheckBox): if name == MODULE_KEY.join(['main', 'gui', 'show_hidden']): self.show_hidden = wx_window.IsChecked() parser.set(section, item_name, wx_window.IsChecked()) module_config[section][item_name] = wx_window.IsChecked() elif isinstance(wx_window, wx.TextCtrl): if item_name not in SKIP_TXT_CONTROLS: parser.set(section, item_name, wx_window.GetValue().encode('utf-8').strip()) module_config[section][item_name] = wx_window.GetValue().encode('utf-8').strip() elif isinstance(wx_window, wx.grid.Grid): col_count = wx_window.GetNumberCols() row_count = wx_window.GetNumberRows() parser_options = parser.options(section) grid_elements = [[wx_window.GetCellValue(row, col).encode('utf-8').strip() for col in range(col_count)] for row in range(row_count)] if not grid_elements: for option in parser_options: parser.remove_option(section, option) module_config[section].pop(option) else: item_list = [item[0] for item in grid_elements] for option in parser_options: if option not in item_list: module_config[section].pop(option) parser.remove_option(section, option) for elements in grid_elements: parser.set(section, *elements) if len(elements) == 1: module_config[section][elements[0]] = None elif len(elements) == 2: module_config[section][elements[0]] = elements[1] elif isinstance(wx_window, wx.Button): if item_name not in SKIP_BUTTONS: parser.set(section, item_name) module_config[section][item_name] = None elif isinstance(wx_window, KeyListBox): item_id = wx_window.GetSelection() parser_options = parser.options(section) item_value = wx_window.get_key_from_index(item_id) if not item_value: for option in parser_options: parser.remove_option(section, option) module_config[section] = None else: for option in parser_options: parser.remove_option(section, option) parser.set(section, item_value) module_config[section] = item_value elif isinstance(wx_window, KeyCheckListBox): item_ids = wx_window.GetChecked() parser_options = parser.options(section) items_values = [wx_window.get_key_from_index(item_id) for item_id in item_ids] if not items_values: for option in parser_options: parser.remove_option(section, option) module_config[section].pop(option) else: for option in parser_options: if option not in items_values: parser.remove_option(section, option) module_config[section].pop(option) for value in items_values: parser.set(section, value) module_config[section][value] = None elif isinstance(wx_window, KeyChoice): item_id = wx_window.GetSelection() item_value = wx_window.get_key_from_index(item_id) parser.set(section, item_name, item_value) module_config[section][item_name] = item_value elif isinstance(wx_window, wx.SpinCtrl): item_value = wx_window.GetValue() parser.set(section, item_name, item_value) module_config[section][item_name] = item_value with open(module_settings['file'], 'w') as config_file: parser.write(config_file) return non_dynamic_check
def create_list(self, parent, view, key, section, section_gui): is_dual = True if 'dual' in view else False style = wx.ALIGN_CENTER_VERTICAL item_sizer = wx.BoxSizer(wx.VERTICAL) addable_sizer = None if section_gui.get('addable', False): addable_sizer = wx.BoxSizer(wx.HORIZONTAL) item_input_key = MODULE_KEY.join([key, 'list_input']) addable_sizer.Add( wx.TextCtrl(parent, id=id_renew(item_input_key, update=True)), 0, style) if is_dual: item_input2_key = MODULE_KEY.join([key, 'list_input2']) addable_sizer.Add( wx.TextCtrl(parent, id=id_renew(item_input2_key, update=True)), 0, style) item_apply_key = MODULE_KEY.join([key, 'list_add']) item_apply_id = id_renew(item_apply_key, update=True) addable_sizer.Add( wx.Button(parent, id=item_apply_id, label=translate_key(item_apply_key)), 0, style) self.Bind(wx.EVT_BUTTON, self.button_clicked, id=item_apply_id) item_remove_key = MODULE_KEY.join([key, 'list_remove']) item_remove_id = id_renew(item_remove_key, update=True) addable_sizer.Add( wx.Button(parent, id=item_remove_id, label=translate_key(item_remove_key)), 0, style) self.Bind(wx.EVT_BUTTON, self.button_clicked, id=item_remove_id) item_sizer.Add(addable_sizer, 0, wx.EXPAND) list_box = wx.grid.Grid(parent, id=id_renew(MODULE_KEY.join([key, 'list_box']), update=True)) list_box.CreateGrid(0, 2 if is_dual else 1) list_box.DisableDragColSize() list_box.DisableDragRowSize() list_box.Bind(wx.grid.EVT_GRID_SELECT_CELL, self.select_cell) list_box.SetMinSize(wx.Size(-1, 100)) for index, (item, value) in enumerate(section.items()): list_box.AppendRows(1) if is_dual: list_box.SetCellValue(index, 0, item.decode('utf-8')) list_box.SetCellValue(index, 1, value.decode('utf-8')) else: list_box.SetCellValue(index, 0, item.decode('utf-8')) list_box.SetColLabelSize(1) list_box.SetRowLabelSize(1) if addable_sizer: col_size = addable_sizer.GetMinSize()[0] - 2 if is_dual: first_col_size = list_box.GetColSize(0) second_col_size = col_size - first_col_size if first_col_size < col_size else -1 list_box.SetColSize(1, second_col_size) else: list_box.SetDefaultColSize(col_size, resizeExistingCols=True) else: list_box.AutoSize() # Adding size of scrollbars size = list_box.GetEffectiveMinSize() size[0] += 18 size[1] += 18 list_box.SetMinSize(size) item_sizer.Add(list_box, 1, wx.EXPAND) return item_sizer