Recent

Author Topic: Cutting and pasting nodes in a Virtual Tree  (Read 1156 times)

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 502
    • Double Dummy Solver - free download
Cutting and pasting nodes in a Virtual Tree
« on: June 23, 2021, 08:47:17 pm »
My code is close but doesn't handle child nodes. It adds all nodes with the same indentation.

The selected nodes are saved when the user clicks the form's Copy button with:
Code: Pascal  [Select][+][-]
  1.   NodeArray := VST.GetSortedSelection(False);
where     NodeArray: TNodeArray; has been defined as a public variable for the form

In my example the VirtualNode only has one item in the record - "Caption" although this approach works fine with numerous fields in the record.

Those nodes are pasted with:
Code: Pascal  [Select][+][-]
  1. procedure TSystemForm.PasteBtnClick(Sender: TObject);
  2. var
  3.   i : integer;
  4.   NewData, OldData: PTreeData;
  5.   Node: PVirtualNode;
  6. begin
  7.   with VirtualStringTree do
  8.   begin
  9.     if Assigned(FocusedNode) then  
  10.       Node := InsertNode(FocusedNode, amInsertAfter)
  11.     else
  12.       Node := AddChild(nil);
  13.     for i := Low(NodeArray) to High(NodeArray) do
  14.     begin
  15.       OldData := GetNodeData(NodeArray[i]);
  16.       NewData := GetNodeData(Node);
  17.       NewData^.Caption := OldData^.Caption;
  18.       if i <> High(NodeArray) then
  19.         Node := InsertNode(Node, amInsertAfter);
  20.     end; // for i
  21.     ValidateNode(FocusedNode, False);
  22.   end; // with
  23. end;
  24.  

The goal is to have the initial node added to the same level as the selected "FocusedNode" and then utilize the parent/child properties of the copied nodes for subsequent new nodes. It seems  that the code on line 19 in this code might be changed to something like this pseudo code.
  if add to same level then
      Node := InsertNode(Node, amInsertAfter)
  else // add indented
      Node := InsertNode(Node, amAddChildLast);

This approach seems very difficult to handle with child nodes moving in and out. How can I retain the proper parent/child relationships in the added nodes?
Win10 64-bit / Lazarus 32-bit 2.0.12 / FPC 3.2.0

 

TinyPortal © 2005-2018