Changeset 3715

Show
Ignore:
Timestamp:
06/27/08 00:24:07 (8 weeks ago)
Author:
travis
Message:

adding update node method

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/opus_gui/results/xml_helper_methods.py

    r3689 r3715  
    6969    def __init__(self, toolboxStuff): 
    7070        self.toolboxStuff = toolboxStuff 
     71 
    7172         
    7273    ##################################################### 
     
    9091        return self._get_node_group(node_value = 'indicator',  
    9192                                    node_attribute = 'type',  
    92                                     child_attributes = attributes)         
    93      
    94     def get_available_indicator_batch_names(self, attributes = []): 
    95         return self._get_node_group(node_value = 'indicator_group',  
    96                                     node_attribute = 'type',  
    97                                     child_attributes = attributes)         
     93                                    child_attributes = attributes)          
    9894     
    9995    def get_available_run_info(self, attributes = []): 
     
    335331                               parent_name = 'Indicator_batches') 
    336332 
    337     def addNewVisualizationToBatch(self, viz_name, batch_name, viz_type, dataset_name, viz_params): 
     333    def addNewVisualizationToBatch(self, viz_name, batch_name, viz_type, viz_params): 
    338334        head_node_args = {'type':'batch_visualization', 
    339335                          'value':''} 
     
    341337        viz_params.append({'value':self.get_visualization_options()[viz_type], 
    342338                           'name':'visualization_type'}) 
    343         viz_params.append({'value':dataset_name, 
    344                            'name':'dataset_name'}) 
    345339             
    346340        for param in viz_params: 
    347341            value = param['value'] 
    348             if isinstance(value,str): 
     342            if isinstance(value,str) or isinstance(value,QString): 
    349343                param['type'] = 'string' 
    350344                param['value'] = value 
     
    360354                               temporary = False,  
    361355                               children_hidden = True) 
     356                 
     357    def _insert_children(self, head_node, child_node_definitions, children_hidden = True, temporary = False): 
     358        # Loop through all the child definitions and create nodes if they are needed 
     359        if child_node_definitions == []: return 
     360 
     361        # This list is to hold all of the child dom nodes created 
     362        model = self.toolboxStuff.resultsManagerTree.model 
     363 
     364        child_nodes = [] 
     365        for args in child_node_definitions: 
     366            if children_hidden: 
     367                if 'flags' in args:  
     368                    args['flags'] += '|hidden' 
     369                else: 
     370                    args['flags'] = 'hidden' 
     371            child_node = model.create_node(document = self.toolboxStuff.doc, 
     372                                           temporary = temporary, 
     373                                           **args) 
     374            child_nodes.append(child_node) 
     375        # Now loop through the nodes we created to append them to the head_node 
     376        for node in sorted(child_nodes, reverse=True): 
     377            head_node.appendChild(node) 
     378                 
    362379                 
    363380    def _add_new_xml_tree(self,  
     
    378395                                    **head_node_args) 
    379396 
     397        self._insert_children(head_node=head_node,  
     398                              child_node_definitions=child_node_definitions,  
     399                              children_hidden=children_hidden,  
     400                              temporary=temporary) 
     401 
    380402        # Find the parent node index to append to 
    381403        parentIndex = model.index(0,0,QModelIndex()).parent() 
    382404        current_index = model.findElementIndexByName(parent_name, parentIndex)[0] 
    383  
    384         # Loop through all the child definitions and create nodes if they are needed 
    385         if child_node_definitions != []: 
    386             # This list is to hold all of the child dom nodes created 
    387             child_nodes = [] 
    388             for args in child_node_definitions: 
    389                 if children_hidden: 
    390                     if 'flags' in args:  
    391                         args['flags'] += '|hidden' 
    392                     else: 
    393                         args['flags'] = 'hidden' 
    394                 child_node = model.create_node(document = document, 
    395                                                temporary = temporary, 
    396                                                **args) 
    397                 child_nodes.append(child_node) 
    398             # Now loop through the nodes we created to append them to the head_node 
    399             for node in sorted(child_nodes, reverse=True): 
    400                 head_node.appendChild(node) 
    401405 
    402406        # Now insert the head_node 
     
    406410 
    407411        model.emit(SIGNAL("layoutChanged()")) 
     412         
     413    def update_dom_node(self, index, new_base_node_name = None, children_to_update = None, children_hidden = True, temporary = False): 
     414        if index is None: return  
     415         
     416        model = self.toolboxStuff.resultsManagerTree.model 
     417        # Keep track of any edits so we can mark the GUI as edited and force a save 
     418        # as well as make the node editable if it is not already... 
     419        dirty = False 
     420        # Grab the base node... this is a QDomNode 
     421        base_node = index.internalPointer().node() 
     422 
     423        if not base_node.isNull(): 
     424            # We only want to check out this node if it is of type "element" 
     425            if base_node.isElement(): 
     426                domElement = base_node.toElement() 
     427                if not domElement.isNull(): 
     428                    # Now we check to see if the tagname is the one we are looking for 
     429                    name = str(domElement.tagName()) 
     430                    # and more importantly if it has changed... we only update on a changed value 
     431                    if name != new_base_node_name: 
     432                        # This path is to allow us to verify if the node being modified 
     433                        # is inherited and needs to be added back in 
     434                        domNodePath = model.domNodePath(base_node) 
     435                        # Actually update the tagname 
     436                        domElement.setTagName(QString(new_base_node_name)) 
     437                        # Now search and check if inherited and needs to be added back in to tree 
     438                        model.checkIfInheritedAndAddBackToTree(domNodePath, index.parent()) 
     439                        # We have made updates so we need to do the "dirty stuff" later 
     440                        dirty = True 
     441 
     442        # Lets avoid calling setData directly... Will create a new method that will do the above 
     443        #self.model.setData(self.selected_index,QVariant(indicator_name),Qt.EditRole) 
     444         
     445        # Get the first child node (also a QDomNode) for traversal 
     446        node = base_node.firstChild() 
     447         
     448        # Only march on if we have non-null nodes 
     449        while not node.isNull(): 
     450            # We only want to check out this node if it is of type "element" 
     451            if node.isElement(): 
     452                domElement = node.toElement() 
     453                if not domElement.isNull(): 
     454                    # Now we check to see if the tagname is the one we are looking for 
     455                    name = str(domElement.tagName()) 
     456                    if name in children_to_update: 
     457                        # We have a match se we need to grab the text node for the element 
     458                        elementText = str(domElement.text()) 
     459                        # If the text node value has changed we need to update 
     460                        if elementText != children_to_update[name]: 
     461                            # We need to grab the text node from the element 
     462                            if domElement.hasChildNodes(): 
     463                                children = domElement.childNodes() 
     464                                for x in xrange(0,children.count(),1): 
     465                                    if children.item(x).isText(): 
     466                                        textNode = children.item(x).toText() 
     467                                        # Finally set the text node value 
     468                                        textNode.setData(children_to_update[name]) 
     469 
     470                                        # We have made this element dirty so we need to mark it all dirty 
     471                                        dirty = True 
     472                        del children_to_update[name] 
     473            # Continue to loop through children 
     474            node = node.nextSibling() 
     475         
     476        #add children which did not already exist 
     477        self._insert_children(head_node=base_node,  
     478                              child_node_definitions=self._convert_to_node_dictionary(child_dictionary = children_to_update),  
     479                              children_hidden = children_hidden,  
     480                              temporary=temporary) 
     481                     
     482        # TODO: Should gather all of this into a method in the model to allow for bulk update 
     483        if dirty:     
     484            # If we have changed something we need to make sure the node we are editing is marked 
     485            # as editable since there was no check that the node was editable before allowing 
     486            # the right click edit option. 
     487            model.makeEditable(base_node) 
     488            # Flag the model as dirty to prompt for save 
     489            model.markAsDirty() 
     490         
     491    def _convert_to_node_dictionary(self, child_dictionary): 
     492        type_map = { 
     493            str:'string', 
     494            list:'list', 
     495            int:'integer', 
     496            QString:'string' 
     497        } 
     498        node_vals = [] 
     499        for k,v in child_dictionary.items(): 
     500            node_vals.append({'name':k, 'value':v, 'type':type_map[type(v)]}) 
     501             
     502        return node_vals 
     503         
     504         
     505         
     506