| | 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 | |
| | 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 | |