Okay, Iv managed to answer this myself, although the method is a bit of a workaround hack!
The code is below, but Ill explain quickly, the file that holds the structure of the tree is in the format:
"Root Item Name#Item Name"
This format, along with the way in which the file is loaded, would allow us to add other items of data to be related to each tree item.
Caveats:
The load routine is designed around the file that was output by the save routine! You can of course code this file by hand if you wish to!
Saving comes first because as the tree is saved it creates the correct file format for re-loading. Firstly I define three variables to hold some data later (These are used by both routines):
self.HoldBuffer1 = ""
self.HoldBuffer2 = ""
self.TROG = ""
Now then, lets assume that you have a button "btnSAVE":
#
def btnSAVE_OnClick(self, event):
#we can set this to root here because we want to
#start from the first item
self.HoldBuffer1 = "ROOT"
#just as good housekeeping
self.TROG = ""
#call the routine that will build the data to
#save
self.tSaveTree(self.cTREE.GetRootItem())
#save the data
f = open( 'treestructure.dat', 'w')
f.write(self.TROG)
f.close()
event.Skip()
#
#
#
#
#
def tSaveTree(self, treeNode):
#as we loop through the tree we add the relevant info
#to TROG
if self.HoldBuffer1 == "ROOT":
self.TROG = self.TROG + "ROOT#" + self.cTREE.GetItemText(treeNode) + "\n"
self.HoldBuffer1 = ""
else:
#because this is not the root item we need to
#find the text of the parent item
x = self.cTREE.GetItemParent(treeNode)
b = self.cTREE.GetItemText(x)
self.TROG = self.TROG + b + "#" + self.cTREE.GetItemText(treeNode) + "\n"
if self.cTREE.GetChildrenCount(treeNode, False):
cookie = -1
child, cookie = self.cTREE.GetFirstChild(treeNode)
while child:
self.tSaveTree(child)
child, cookie = self.cTREE.GetNextChild(treeNode, cookie)
#
Okay, so now there is a file (called treestructure.dat), and we know what format it is in. So loading becomes fairly easy:
#
#
def btnLOAD_OnClick(self, event):
i = 0
f = open( 'treestructure.dat', 'r')
x = f.readline()
#loop for as long as we have something in
#the line
while x > "":
#remove the linefeed at the end of the line
#or things go wrong
#KLeft is my own little function - you can
#use any method you like as long as you
#lose the linefeed!
z = KLeft(x, len(x) - 1)
#split the string
dank = z.split('#')
#if this is the first line we have read
#then we know that it will be the root item
if i == 0:
#this is the root item
troot = self.cTREE.AddRoot(dank[1])
else:
#dank 0 holds the text of the parent item
self.HoldBuffer1 = dank[0]
self.HoldBuffer2 = dank[1]
self.tLoadTree(self.cTREE.GetRootItem())
#increment this now for the next item
i = i + 1
#read the next line
x = f.readline()
#all done so close the file
f.close()
#
#
#
#
#
def tLoadTree(self, treeNode):
if self.HoldBuffer1 == self.cTREE.GetItemText(treeNode):
self.cTREE.AppendItem(treeNode,self.HoldBuffer2)
return
if self.cTREE.GetChildrenCount(treeNode, False):
cookie = -1
child, cookie = self.cTREE.GetFirstChild(treeNode)
while child:
self.tLoadTree(child)
child, cookie = self.cTREE.GetNextChild(treeNode, cookie)
#
#
All done! Like I said, it ain't pretty, but it works.
I would of course be happy for anyone to show me a faster, cleaner way of ending up with the same thing.
Regards
Max