Brad
Brad

Reputation: 51

Iterating a GTK3 Treestore with child nodes in python

im trying to search a GTK 3 treestore for a string. The treestore has 4 columns,and is for a treeview widget that has callapsible nodes. im creating the nodes with this function:

def AddItem(self,ParentIter,txt,datapath='',projName=Project):         
    self.store = self.builder.get_object('theTreeStore')
    NodeId = secrets.token_hex(8) 

    if ParentIter == None:
        ParentNodeId = ''
    else:
        ParentNodeId = self.store.get_value(ParentIter, 2) 
    treeEntry = ['%s' %ParentNodeId,'%s' %txt,'%s' %NodeId,'%s' %datapath] 
    node = self.store.append(ParentIter, treeEntry) <<<<<<<<<<<<<
    self.view = self.builder.get_object('Tree')
    self.view.set_model(self.store)

    # table nodes(tParentNodeID ,tNodeTxt ,tNodeID ,tDataPath );



    sql = "INSERT INTO %s (tParentNodeID ,tNodeTxt ,tNodeID ,tDataPath ) VALUES ('%s','%s','%s','%s')" %(projName,ParentNodeId,txt,NodeId,datapath)
    self.cursor.execute(sql)
    self.mariadb_connection.commit()
    for x in self.cursor:
        print(x)
    return(node)

as you can see the data in the tree is nested in its parent.

now i need to somehow search the treestore for a row that contains a certain NodeId string. Ive read the gtk docs over and over but i cant quite figure out what to do. im guessing i need to use following methods:

store.get_iter() store.iter_children()

but idk everything i try only returns the root nodes no children.

i basically want a search function that will recursively search each node and its children,and their children for a string. something like this:

def GetRowbyNodeID(nodeid):
     for row in treestore:
       if row[1]==nodeid:
          return(row) 
       for children in row:        
         if children[1] == nodeid    
             return(children)

The code is in multiple files, i can post any functions relevant if needed.

Upvotes: 3

Views: 2155

Answers (2)

Brad
Brad

Reputation: 51

Got it all working. thanks again. im posting the relevant code just in case anyone else could use it.

def SearchTreeRows(self,store, treeiter, searchstr):
    print("\nsearch>%s"%searchstr)
    while treeiter != None:

        if store[treeiter][2] ==searchstr:
            print("found in:%s"%str(store[treeiter][:]))
            return(treeiter)
            break
        print("searched:%s"%str(store[treeiter][:]))    
        if store.iter_has_child(treeiter):
            childiter = store.iter_children(treeiter)
            ret = self.SearchTreeRows(store, childiter, searchstr)
            if ret is not None:
                return ret

        treeiter = store.iter_next(treeiter)

def NodeId2Tree(self,nodeid):
    self.store = self.builder.get_object('theTreeStore')
    rootiter = self.store.get_iter_first()
    row = self.SearchTreeRows(self.store, rootiter,nodeid)
    return(row)

def LoadProject(self):

    global Project
    global ProjSel
    sql = "SHOW TABLES"
    self.cursor.execute(sql)

    tbls = []
    for x in self.cursor:
        tbls.append(x)   

    diag = self.builder.get_object('ProjectChooser')


    self.combo = Gtk.ComboBox()
    ls =Gtk.ListStore(str)
    for tble in tbls:
        strg ="%s" %tble
        ls.append(tble)

    self.combo.set_model(ls)
    cellr = Gtk.CellRendererText()
    self.combo.pack_start(cellr,True) 
    self.combo.add_attribute(cellr, 'text', 0) 
    diag.vbox.pack_start(self.combo, True, True, 5)
    diag.show_all()
    response = diag.run()
    self.combo.destroy()
    print(ProjSel) 
    Project = ProjSel
    ProjSel = ''
    view = self.builder.get_object('Tree')
    self.store.clear()
    view.set_model(self.store)

    sql = "SELECT tParentNodeId,tNodeTxt,tNodeId FROM %s"%(Project)
    self.cursor.execute(sql)



    for x in self.cursor:
        parid = x[0]
        nodtxt = x[1]
        nodid =x[2]
        if parid == '':
            treeEntry = ['%s' %parid,  '%s' %nodtxt,  '%s' %nodid,  '']
            node = self.store.append(None, treeEntry)                #root nodes
        else:
            treeEntry = ['%s' %parid,  '%s' %nodtxt,  '%s' %nodid,  '']
            n2id = self.NodeId2Tree(parid)
            node = self.store.append(n2id, treeEntry)
            print("got return:%s   For:%s"%(n2id,treeEntry[0]))



    view.set_model(self.store)
    #select * where parentid == none  >> get root nodes ???? or parse line by line

Upvotes: 1

Alexander Dmitriev
Alexander Dmitriev

Reputation: 2525

GtkTreeStore implements GtkTreeModel interface. Thus you can use the following methods:

I'd also recommend reading this tutorial. "The Model" section contains all you need on iterating over the model (spoiler: search for print_tree_store)

Upvotes: 1

Related Questions