SimpleGraph - Streaming Custom Data

Please post bug reports, feature requests, or any question regarding the DELPHI AREA products here.

SimpleGraph - Streaming Custom Data

Postby Kambiz » March 18th, 2006, 4:07 pm

SimpleGraph v2.1 introduces a mechanism to read (write) custom data assigned to an object from (in to) a file.

For example, suppose you want to assign a TStringList to each node of a graph, and you want also to save/load or copy/paste these strings as well as the objects.

The following piece of code illustrates how the above example can be implemented:

Code: Select all
procedure TForm1.SimpleGraph1ObjectInitInstance(Graph: TSimpleGraph;
  GraphObject: TGraphObject);
begin
  // If graph object being created is a node...
  if GraphObject.IsNode then
  begin
    // We create a new StringList and assign it to the Data property
    // of the graph object.
    GraphObject.Data := TStringList.Create;
    // We also specify that this graph object has some custom data to
    // stream out.
    GraphObject.HasCustomData := True;
  end;
end;

procedure TForm1.SimpleGraph1ObjectRead(Graph: TSimpleGraph;
  GraphObject: TGraphObject; Stream: TStream);
begin
  // If Data property of graph object is nill...
  if not Assigned(GraphObject.Data) then
    // The graph object is just created, and is loading its property
    // settings from the stream. Therefore we should create an
    // instance of StringList for it.
    GraphObject.Data := TStringList.Create;
  // We load StringList from the stream provided as parameter
  // of the event handler.
  TStringList(GraphObject.Data).LoadFromStream(Stream);
end;

procedure TForm1.SimpleGraph1ObjectWrite(Graph: TSimpleGraph;
  GraphObject: TGraphObject; Stream: TStream);
begin
  // We save StringList in to the stream provided as parameter
  // of the event handler.
  TStringList(GraphObject.Data).SaveToStream(Stream);
end;

procedure TForm1.SimpleGraph1ObjectRemove(Graph: TSimpleGraph;
  GraphObject: TGraphObject);
begin
  // if Data property is not nil...
  if Assigned(GraphObject.Data) then
  begin
    // We release the StringList instance
    TStringList(GraphObject.Data).Free;
    // And, clean up the graph object
    GraphObject.Data := nil;
  end;
end;


In the above code, assumed there is no other application to share (via file or clipboard) graph objects with it. A practical code should identify its own data and ignore not supported ones. This can be done by writing a unique signature on the stream, and checking the signature before loading data.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2408
Joined: March 7th, 2003, 7:10 pm

Postby HPW » March 18th, 2006, 7:16 pm

Thanks for the sample. I will give it a try.
Hans-Peter
HPW
Moderator
Moderator
 
Posts: 230
Joined: February 25th, 2006, 10:19 am
Location: Germany


Return to DELPHI AREA Products

Who is online

Users browsing this forum: No registered users and 1 guest