| | 37 | class OptionTab(object): |
| | 38 | def __init__(self, root_tab, options, constructor, update_func): |
| | 39 | actions = {'option_list':self.__parse_option_list,\ |
| | 40 | 'option_check':self.__parse_option_check} |
| | 41 | |
| | 42 | self.options = options |
| | 43 | self.constructor = constructor |
| | 44 | self.update_func = update_func |
| | 45 | self.widgets_list = [] |
| | 46 | |
| | 47 | options_used = self.constructor.get_options() |
| | 48 | |
| | 49 | # Cannot use list comprehhension because text nodes raise exception |
| | 50 | # when tagName is called |
| | 51 | for option_element in root_tab.childNodes: |
| | 52 | try:option_element.tagName |
| | 53 | except:pass |
| | 54 | else: |
| | 55 | if option_element.tagName in actions.keys(): |
| | 56 | self.widgets_list.append(actions[option_element.tagName](option_element, options_used)) |
| | 57 | |
| | 58 | def __parse_option_list(self, option_list, options_used): |
| | 59 | options = option_list.getElementsByTagName(u'option') |
| | 60 | |
| | 61 | label = HIGEntryLabel(option_list.getAttribute(u'label')) |
| | 62 | opt_list = OptionList() |
| | 63 | |
| | 64 | for opt in options: |
| | 65 | opt_list.append(self.options.get_option(opt.getAttribute(u'name'))) |
| | 66 | |
| | 67 | for i, row in enumerate(opt_list.list): |
| | 68 | if row[0] in options_used: |
| | 69 | opt_list.set_active(i) |
| | 70 | |
| | 71 | return label, opt_list |
| | 72 | |
| | 73 | def __parse_option_check(self, option_check, options_used): |
| | 74 | arg_type = option_check.getAttribute(u'arg_type') |
| | 75 | option = option_check.getAttribute(u'option') |
| | 76 | label = option_check.getAttribute(u'label') |
| | 77 | |
| | 78 | check = OptionCheck(label, self.options.get_option(option)) |
| | 79 | check.set_active(option in options_used) |
| | 80 | |
| | 81 | type_mapping = { |
| | 82 | "str": OptionEntry, |
| | 83 | "int": OptionIntSpin, |
| | 84 | "float": OptionFloatSpin, |
| | 85 | "level": OptionLevelSpin, |
| | 86 | "path": OptionFile, |
| | 87 | "interface": OptionInterface |
| | 88 | } |
| | 89 | |
| | 90 | additional = None |
| | 91 | if type_mapping.has_key(arg_type): |
| | 92 | value = options_used.get(option, None) |
| | 93 | if value: |
| | 94 | additional = type_mapping[arg_type](value) |
| | 95 | else: |
| | 96 | additional = type_mapping[arg_type]() |
| | 97 | |
| | 98 | check.connect('toggled', self.update_check, additional) |
| | 99 | |
| | 100 | return check, additional |
| | 101 | |
| | 102 | def fill_table(self, table, expand_fill = True): |
| | 103 | yopt = (0, gtk.EXPAND | gtk.FILL)[expand_fill] |
| | 104 | for y, widget in enumerate(self.widgets_list): |
| | 105 | if widget[1] == None: |
| | 106 | table.attach(widget[0], 0, 2, y, y+1, yoptions=yopt) |
| | 107 | else: |
| | 108 | table.attach(widget[0], 0, 1, y, y+1, yoptions=yopt) |
| | 109 | table.attach(widget[1], 1, 2, y, y+1, yoptions=yopt) |
| | 110 | |
| | 111 | for widget in self.widgets_list: |
| | 112 | te = type(widget[1]) |
| | 113 | if te == type(OptionList()): |
| | 114 | widget[1].connect('changed',self.update_list_option) |
| | 115 | elif te == type(OptionIntSpin()) or\ |
| | 116 | te == type(OptionFloatSpin()) or\ |
| | 117 | te == type(OptionEntry()): |
| | 118 | widget[1].connect('changed', self.update_entry, widget[0]) |
| | 119 | elif te == type(OptionLevelSpin()): |
| | 120 | widget[1].connect('changed', self.update_level, widget[0]) |
| | 121 | elif te == type(OptionFile()): |
| | 122 | widget[1].entry.connect('changed', self.update_entry, widget[0]) |
| | 123 | elif te == type(OptionInterface()): |
| | 124 | widget[1].child.connect('changed', self.update_entry, widget[0]) |
| | 125 | |
| | 126 | def update_check(self, check, extra): |
| | 127 | if check.get_active(): |
| | 128 | te = type(extra) |
| | 129 | if te == type(OptionEntry()) or\ |
| | 130 | te == type(OptionIntSpin()) or\ |
| | 131 | te == type(OptionFloatSpin()): |
| | 132 | self.update_entry(extra, check) |
| | 133 | elif te == type(OptionLevelSpin()): |
| | 134 | self.update_level(extra, check) |
| | 135 | elif te == type(OptionFile()): |
| | 136 | self.update_entry(extra.entry, check) |
| | 137 | elif te == type(OptionInterface()): |
| | 138 | self.update_entry(extra.child, check) |
| | 139 | else: |
| | 140 | self.constructor.add_option(check.option['name']) |
| | 141 | else: |
| | 142 | self.constructor.remove_option(check.option['name']) |
| | 143 | |
| | 144 | self.update_command() |
| | 145 | |
| | 146 | def update_entry(self, widget, check): |
| | 147 | if not check.get_active(): |
| | 148 | check.set_active(True) |
| | 149 | |
| | 150 | self.constructor.remove_option(check.option['name']) |
| | 151 | self.constructor.add_option(check.option['name'], widget.get_text()) |
| | 152 | |
| | 153 | self.update_command() |
| | 154 | |
| | 155 | def update_level(self, widget, check): |
| | 156 | if not check.get_active(): |
| | 157 | check.set_active(True) |
| | 158 | |
| | 159 | try: |
| | 160 | self.constructor.remove_option(check.option['name']) |
| | 161 | if int(widget.get_text()) == 0: |
| | 162 | check.set_active(False) |
| | 163 | else: |
| | 164 | self.constructor.add_option(check.option['name'],\ |
| | 165 | level=int(widget.get_text())) |
| | 166 | except:pass |
| | 167 | |
| | 168 | self.update_command() |
| | 169 | |
| | 170 | def update_list_option(self, widget): |
| | 171 | try:widget.last_selected |
| | 172 | except:pass |
| | 173 | else: |
| | 174 | self.constructor.remove_option(widget.last_selected) |
| | 175 | |
| | 176 | option_name = widget.options[widget.get_active()]['name'] |
| | 177 | |
| | 178 | self.constructor.add_option(option_name) |
| | 179 | widget.last_selected = option_name |
| | 180 | |
| | 181 | self.update_command() |
| | 182 | |
| | 183 | def update_command(self): |
| | 184 | if self.update_func: |
| | 185 | self.update_func() |
| | 186 | |
| | 187 | |
| 86 | | |
| 87 | | def __parse_option_list(self, option_list): |
| 88 | | options = option_list.getElementsByTagName(u'option') |
| 89 | | |
| 90 | | label = HIGEntryLabel(option_list.getAttribute(u'label')) |
| 91 | | opt_list = OptionList() |
| 92 | | |
| 93 | | for opt in options: |
| 94 | | opt_list.append(self.options.get_option\ |
| 95 | | (opt.getAttribute(u'name'))) |
| 96 | | |
| 97 | | return label, opt_list |
| 98 | | |
| 99 | | def __parse_option_check(self, option_check): |
| 100 | | arg_type = option_check.getAttribute(u'arg_type') |
| 101 | | option = option_check.getAttribute(u'option') |
| 102 | | label = option_check.getAttribute(u'label') |
| 103 | | |
| 104 | | check = OptionCheck(label, self.options.get_option\ |
| 105 | | (option_check.getAttribute(u'option'))) |
| 106 | | additional = None |
| 107 | | |
| 108 | | if arg_type == "str": |
| 109 | | additional = OptionEntry() |
| 110 | | elif arg_type == "int": |
| 111 | | additional = OptionIntSpin() |
| 112 | | elif arg_type == "float": |
| 113 | | additional = OptionFloatSpin() |
| 114 | | elif arg_type == "level": |
| 115 | | additional = OptionLevelSpin() |
| 116 | | elif arg_type == "path": |
| 117 | | additional = OptionFile() |
| 118 | | elif arg_type == "interface": |
| 119 | | additional = OptionInterface() |
| 120 | | |
| 121 | | return check, additional |
| 122 | | |
| | 230 | |
| | 231 | |