Page 1 of 1

Create object in Tsimplegraph

PostPosted: November 25th, 2008, 7:49 pm
by jacek
Helo!

Can anybody help me to do such a thing:
I would like to create for example node which will be including the TButton or other object inside, and then will be possible to use its properties.

thank You
regards
Jacek

Re: Create object in Tsimplegraph

PostPosted: November 26th, 2008, 11:24 am
by Kambiz
You just need to write handler for three events.

In the following code snippet, I've attached a button to center of each rectangular node.

Code: Select all
// create the button when a rectangular node is inserted
procedure TForm1.SimpleGraph1ObjectInsert(Graph: TSimpleGraph;
  GraphObject: TGraphObject);
var
  btn: TButton;
begin
  if GraphObject is TRectangularNode then
  begin
    // create the button
    btn := TButton.Create(nil);
    // set button's parent to simplegraph
    btn.Parent := Graph;
    // save a reference to button for recall
    GraphObject.Tag := Integer(btn);
    // position the button
    SimpleGraph1NodeMoveResize(Graph, TGraphNode(GraphObject));
  end;
end;

// destroy the button when the node is deleted
procedure TForm1.SimpleGraph1ObjectRemove(Graph: TSimpleGraph;
  GraphObject: TGraphObject);
var
  btn: TButton;
begin
  // if any button is attached
  if GraphObject.Tag <> 0 then
  begin
    // get the button
    btn := TButton(GraphObject.Tag);
    // remove reference
    GraphObject.Tag := 0;
    // free the button
    btn.Free;
  end;
end;

// reposition/resize the button when its node moved or resized
procedure TForm1.SimpleGraph1NodeMoveResize(Graph: TSimpleGraph;
  Node: TGraphNode);
var
  btn: TButton;
  Pos: TPoint;
  Size: TPoint;
begin
  // if any button is attached
  if Node.Tag <> 0 then
  begin
    // get the button
    btn := TButton(Node.Tag);
    // convert node's coordinates from graph to simplegraph
    Pos := Graph.GraphToClient(Node.Left, Node.Top);
    Size := Graph.GraphToClient(Node.Width, Node.Height);
    // Set button's size as half size of the node and center it inside the node
    Size.X := Size.X div 2;
    Size.Y := Size.Y div 2;
    Inc(Pos.X, Size.X div 2);
    Inc(Pos.Y, Size.Y div 2);
    btn.SetBounds(Pos.X, Pos.Y, Size.X, Size.Y);
  end;
end;

Re: Create object in Tsimplegraph

PostPosted: January 12th, 2011, 9:58 pm
by kaouane
It's fantastic Kambiz, but it make problem when zoomed :(

Re: Create object in Tsimplegraph

PostPosted: January 13th, 2011, 11:21 am
by Kambiz
You should resize the associated control on OnZoomChange event too.

Re: Create object in Tsimplegraph

PostPosted: January 13th, 2011, 4:58 pm
by kaouane
Excuse me Kambize, but I don't know how can I do it.
If you can, please indicate me. Thank you.

Re: Create object in Tsimplegraph

PostPosted: January 13th, 2011, 9:53 pm
by Kambiz
Here is the code for the above example:

Code: Select all
procedure TForm1.SimpleGraph1ZoomChange(Sender: TObject);
begin
  SimpleGraph1.ForEachObject(AdjustNodeBounds, 0);
end;

function TForm1.AdjustNodeBounds(GraphObject: TGraphObject; UserData: Integer): Boolean;
begin
  if (GraphObject.Tag <> 0) and (GraphObject is TGraphNode) then
    SimpleGraph1NodeMoveResize(SimpleGraph1, TGraphNode(GraphObject));
  Result := True;
end;

Re: Create object in Tsimplegraph

PostPosted: January 13th, 2011, 11:57 pm
by kaouane
Sincerely, you are a big boss;
Thank you very much Kambiz, I am very glad to be a member for your website;

Re: Create object in Tsimplegraph

PostPosted: January 14th, 2011, 9:40 pm
by Kambiz
Thank you!