On 10/23/2013 11:03 AM, Jacob Peck wrote:
When I have time I'll give it a go.

-->Jake
Well... that was way easier than expected.

The following is the new version of the code, and it works so far... Edward, do you see any issues with the undoer code?

=====
@language python

def create_node_hierarchy(heads, parent=None, forcecreate=False):
  ''' create the proper hierarchy of nodes with headlines defined in
      'head' under 'parent' if they do not exist

      params:
      parent - parent node to start from.  Set to None for top-level nodes
heads - list of headlines in order to create, i.e. ['foo','bar','baz']
              will create:
                parent
                -foo
                --bar
                ---baz
forcecreate - If False (default), will not create nodes unless they don't exist
                    If True, will create nodes regardless of existing nodes
      returns the final position ('baz' in the above example)
  '''
  u = c.undoer
  undoType = 'Create Node Hierarchy'
  undoType2 = 'Insert Node In Hierarchy'
  u_node = parent or c.rootPosition()
  undoData = u.beforeChangeGroup(u_node,undoType)
  for idx,head in enumerate(heads):
if parent == None and idx == 0: # if parent = None, create top level node for first head
      if not forcecreate:
        for pos in c.all_positions():
          if pos.h == head:
            parent = pos
            break
      if parent is None or forcecreate:
        u_d = u.beforeInsertNode(u_node)
        n = c.rootPosition().insertAfter()
        n.h = head
        u.afterInsertNode(n,undoType2,u_d)
        parent = n
    else: # else, simply create child nodes each round
      if not forcecreate:
        for ch in parent.children():
          if ch.h == head:
            parent = ch
            break
      if parent.h != head or forcecreate:
        u_d = u.beforeInsertNode(parent)
        n = parent.insertAsLastChild()
        n.h = head
        u.afterInsertNode(n, undoType2, u_d)
        parent = n
  g.es('changes done')
  u.afterChangeGroup(parent,undoType,undoData)
  return parent # actually the last created/found position

### test script
h = ['test1', 'test2', 'test3']
## The following creates:
## test1->test2->test3
## under the current node, not recreating any nodes that already exist
#pos = create_node_hierarchy(h, parent=p, forcecreate=False)
## The following creates:
## test1->test2->test3
## starting with test1 as a TOP LEVEL NODE, making new nodes regardless of the existence of similar nodes
pos = create_node_hierarchy(h, forcecreate=True)
g.es(pos.h)
c.selectPosition(pos)
c.redraw_now()
=====

Thanks,
-->Jake

--
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to