Show
Ignore:
Timestamp:
06/02/07 13:00:08 (6 years ago)
Author:
boltrix
Message:

Merged the changes sent by Max, and fixed a bug related to his fix. Also, fixed a bug on setup.py which were installing the docs in the wrong place.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/umitGUI/OptionBuilder.py

    r489 r765  
    3535 
    3636 
     37class 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                  
    37188class OptionBuilder(object): 
    38     def __init__(self, xml_file): 
     189    def __init__(self, xml_file, constructor, update_func): 
     190        """ OptionBuilder(xml_file, constructor) 
     191 
     192        xml_file is a UI description xml-file 
     193        constructor is a CommandConstructor instance 
     194        """ 
    39195        xml_desc = open(xml_file) 
    40196        self.xml = minidom.parse(xml_desc) 
    41  
    42197        # Closing file to avoid problems with file descriptors 
    43198        xml_desc.close() 
     199 
     200        self.constructor = constructor 
     201        self.update_func = update_func 
    44202         
    45203        self.root_tag = "interface" 
    46         self.actions = {'option_list':self.__parse_option_list,\ 
    47                         'option_check':self.__parse_option_check} 
    48204         
    49205        self.xml = self.xml.getElementsByTagName(self.root_tag)[0] 
     
    69225        dic = {} 
    70226        for tab_name in self.groups: 
    71             tab = self.xml.getElementsByTagName(tab_name)[0] 
    72             widgets_list = [] 
    73             # Cannot use list comprehhension because text nodes raise exception 
    74             # when tagName is called 
    75             for option in tab.childNodes: 
    76                 try:option.tagName 
    77                 except:pass 
    78                 else: 
    79                     if option.tagName in self.actions.keys(): 
    80                         widgets_list.append\ 
    81                             (self.actions[option.tagName](option)) 
    82              
    83             dic[tab_name] = widgets_list 
    84          
     227            dic[tab_name] = OptionTab(self.xml.getElementsByTagName(tab_name)[0], 
     228                                      self.options, self.constructor, self.update_func) 
    85229        return dic 
    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     
    123232class OptionWidget: 
    124233    def enable_widget(self): 
     
    163272 
    164273class OptionEntry(gtk.Entry, OptionWidget): 
    165     def __init__(self): 
     274    def __init__(self, param = ""): 
    166275        gtk.Entry.__init__(self) 
     276        self.set_text(param) 
    167277 
    168278class OptionLevelSpin(gtk.SpinButton, OptionWidget): 
    169279    def __init__(self, initial=0): 
    170         gtk.SpinButton.__init__(self,gtk.Adjustment(initial,0,10,1),0.0,0) 
     280        gtk.SpinButton.__init__(self,gtk.Adjustment(int(initial),0,10,1),0.0,0) 
    171281 
    172282class OptionIntSpin(gtk.SpinButton, OptionWidget): 
    173283    def __init__(self, initial=1): 
    174         gtk.SpinButton.__init__(self,gtk.Adjustment(initial,0,10**100,1),0.0,0) 
     284        gtk.SpinButton.__init__(self,gtk.Adjustment(int(initial),0,10**100,1),0.0,0) 
    175285 
    176286class OptionFloatSpin(gtk.SpinButton, OptionWidget): 
    177287    def __init__(self, initial=1): 
    178         gtk.SpinButton.__init__(self,gtk.Adjustment(initial,0,10**100,1),0.1,2) 
     288        gtk.SpinButton.__init__(self,gtk.Adjustment(float(initial),0,10**100,1),0.1,2) 
    179289 
    180290class OptionFile(HIGHBox, OptionWidget, object): 
    181     def __init__(self): 
     291    def __init__(self, param=""): 
    182292        HIGHBox.__init__(self) 
    183293         
     
    187297        self._pack_expand_fill(self.entry) 
    188298        self._pack_noexpand_nofill(self.button) 
    189          
     299 
     300        self.entry.set_text(param) 
    190301        self.button.connect('clicked', self.open_dialog_cb) 
    191302