TTree Save to Stream

TeeTree VCL for Borland Delphi and C++ Builder.
Post Reply
mendelo
Newbie
Newbie
Posts: 11
Joined: Wed Sep 26, 2007 12:00 am

TTree Save to Stream

Post by mendelo » Mon Feb 18, 2008 7:10 pm

This is just getting an exercise in frustration.

I see in the help that there is a LoadTreeFromStream procedure. Great. However there is no documented SaveTreeToStream. But it exists. So that is good, now I can save the Tree to a database and then reload it. However when I load it I discover that it does not save child nodes. OK , help shows a procedure called SaveTreeTextToStream which is supposed to include child nodes. However there is no corresponding LoadTreeTextFromStream. So I try the LoadTreeFromStream but this declares the stream to be in an invalid format, so obviously the two streams are incompatible. I cannot find another procedure that loads the previously created stream , unless maybe I load this into a file (LoadTreeChildsFromFile ??) but I cannot believe it would be this obscure.

The incomplete help coupled with the lack of response on this forum makes me wonder why I bought this product.

tom
Advanced
Posts: 211
Joined: Mon Dec 01, 2003 5:00 am
Contact:

Post by tom » Wed Feb 20, 2008 12:25 am

Hi,

LoadTreeFromStream and SaveTreeToStream load and save the Tree in native format while the 'SaveTreeTextToStream' method saves the tree in plain text format.

Please have a look at the code below, this should work:

Code: Select all

procedure TForm2.CreateTreeClick(Sender: TObject);
begin
  Tree1.Clear;
  Tree1.NoOwnerShapes := False;
  with Tree1.AddRoot('Root') do
  begin
    AddChild('Child1');
    AddChild('Child2');
  end;
end;

procedure TForm2.SaveTreeClick(Sender: TObject);
var s: TFileStream;
begin
  s := TFileStream.Create('c:\test.tree', fmCreate);
  try
    SaveTreeToStream(Tree1, s);
  finally
    FreeAndNil(s);
  end;
end;

procedure TForm2.LoadTreeClick(Sender: TObject);
var s: TFileStream;
begin
  Tree1.Clear;
  s := TFileStream.Create('c:\test.tree', fmOpenRead);
  try
    s.Position := 0;
    LoadTreeFromStream(TCustomTree(Tree1), s);
  finally
    FreeAndNil(s);
  end;
end;
It's important to set NoOwnerShapes to false.

" Tree NoOwnerShapes and AssignParent properties.

These properties have different values depending if we're adding nodes at design time under Delphi or we are adding nodes programatically at runtime using code.
At design-time NoOwnerShapes is False (so all new nodes created at design-time will be assigned the Form as the Owner to ensure the nodes will be streamed out to the form's DFM file). The AssignParent property at design-time is True.

At run-time, NoOwnerShapes is True (so new created nodes at runtime will have no Owner, this increases speed a lot).
AssignParent property at runtime is False (to increase speed when creating new nodes at runtime)."

When saving the tree in plain text should be:

Code: Select all

procedure TForm2.SaveTreeTextClick(Sender: TObject);
var s: TFileStream;
begin
  s := TFileStream.Create('c:\test.treetext', fmCreate);
  try
    SaveTreeTextToStream(s, Tree1);
  finally
    FreeAndNil(s);
  end;
end;

procedure TForm2.LoadTreeTextClick(Sender: TObject);
var
  s: TFileStream;
  sl: TStringList;
begin
  Tree1.Clear;
  s := TFileStream.Create('c:\test.treetext', fmOpenRead);
  try
    s.Position := 0;
    sl := TStringList.Create;
    sl.LoadFromStream(s);
    Tree1.LoadFromStrings(sl);
    FreeAndNil(sl);
  finally
    FreeAndNil(s);
  end;
   //or Tree1.LoadFromTextFile('c:\test.treetext');
end;
Regards,
Tom

mendelo
Newbie
Newbie
Posts: 11
Joined: Wed Sep 26, 2007 12:00 am

The Lost Children

Post by mendelo » Fri Feb 29, 2008 2:08 am

Thank you for the insight into the NoOwnerShapes property. I could not find this documented in the help manual.

I have moved a little further. A TTree has nodes and child nodes added at runtime.

procedure Form1.SaveMyTree(Sender : TObject);
begin
Tree1.NoOwnerShapes:= false;
SaveTreeToStream(Tree1,strm);

end;

My tree is saved to a blob field. I can load this tree from the blob field. With NoOwnerShapes = True then the child nodes are missing. If, before saving, NoOwnerShapes = false. then the child nodes are there. Excellent .However they are no longer children of the parent node, as evidenced when you move the parent node, the children no longer follow, all the connectiions have disappeared and of course the Children.Count of the parent is 0. Setting the parent when adding the child node seems to make no difference and neither does the other undocumented TTree property - AssignParent . Neither seem to make any difference. Is there a way, when saving to stream, to retain the Parent/Child hiearchy?

Ashu
Newbie
Newbie
Posts: 24
Joined: Mon Mar 08, 2004 5:00 am

Post by Ashu » Fri Feb 29, 2008 4:33 am

I am also facing the similar type of problem.

I too save the TeeTree to a Blob field.

It seems to work correctly for a course of time, but then suddenly the newly added shapes start disappearing & same is the case with connections too. :cry:
I don't know the exact scenario but NoOwnerShape is always False in my case.

Regards,
Pratik

mendelo
Newbie
Newbie
Posts: 11
Joined: Wed Sep 26, 2007 12:00 am

Connection Issues

Post by mendelo » Fri Feb 29, 2008 6:47 pm

With regards the connection issue, once I save the TTree to stream and reload it from the stream, the connections are no longer visible. Yet I know the components are there, because in designer if I try to add a connection between the same two nodes, I get an error message that states "A component named TTreeNodeShape1_TreeNodeShape2 already exists". So the connection has not been lost, just some property. I have tried iterating through the shapes in the tree and setting Connections.Visible:= true , but that has not made the connections reappear. The results are the same if I save it to stream or a file.

tom
Advanced
Posts: 211
Joined: Mon Dec 01, 2003 5:00 am
Contact:

Post by tom » Fri Feb 29, 2008 11:14 pm

set

Code: Select all

  Tree1.NoOwnerShapes := False;
  Tree1.AssignParent := True;
in the OnCreate method of your form. This should give the result you need.

I can provide you some sample code if you provide me your email address.

regards,
tom

mendelo
Newbie
Newbie
Posts: 11
Joined: Wed Sep 26, 2007 12:00 am

Post by mendelo » Sat Mar 01, 2008 1:25 am

Tom

I would be glad to but the PM on this board has been disabled and your profile does not list an email address.

Cheers
Peter

tom
Advanced
Posts: 211
Joined: Mon Dec 01, 2003 5:00 am
Contact:

Post by tom » Sat Mar 01, 2008 11:57 pm

tom @ steema . com

Post Reply