Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. TRoot = Record
  2. RootName : String;
  3. RootId : Integer;
  4. Kids : TList; //(of TKid)
  5. End;
  6.  
  7. TKid = Record
  8. KidName : String;
  9. KidId : Integer;
  10. End;
  11.  
  12. TNodeClass = class
  13. public
  14. Name: string;
  15. ID: Integer;
  16. end;
  17.  
  18. type
  19. TNode = class
  20. public
  21. ID: integer;
  22. Name: string;
  23. VTNode: PVirtualNode;
  24. Sub: TObjectList<TNode>;
  25.  
  26. constructor Create(aName: string = ''; anID: integer = 0);
  27. destructor Destroy; override;
  28. end;
  29.  
  30. constructor TNode.Create(aName:string; anID: Integer);
  31. begin
  32. Name := aName;
  33. ID := anID;
  34.  
  35. Sub := TObjectList<TNode>.Create;
  36. end;
  37.  
  38. destructor TNode.Destroy;
  39. begin
  40. Sub.Free;
  41. end;
  42.  
  43. Root := TNode.Create;
  44.  
  45. // Create the Contacts leaf
  46. Root.Sub.Add(TNode.Create('Contacts', -1));
  47. // Add some contacts
  48. Root.Sub[0].Sub.Add(TNode.Create('Abraham', 1));
  49. Root.Sub[0].Sub.Add(TNode.Create('Lincoln', 2));
  50.  
  51. // Create the "Recent Calls" leaf
  52. Root.Sub.Add(TNode.Create('Recent Calls', -1));
  53. // Add some recent calls
  54. Root.Sub[1].Sub.Add(TNode.Create('+00 (000) 00.00.00', 3));
  55. Root.Sub[1].Sub.Add(TNode.Create('+00 (001) 12.34.56', 4));
  56.  
  57. procedure TForm1.AddNodestoTree(ParentNode: PVirtualNode; Node: TNode);
  58. var SubNode: TNode;
  59. ThisNode: PVirtualNode;
  60.  
  61. begin
  62. ThisNode := VT.AddChild(ParentNode, Node); // This call adds a new TVirtualNode to the VT, and saves "Node" as the payload
  63.  
  64. Node.VTNode := ThisNode; // Save the PVirtualNode for future reference. This is only an example,
  65. // the same TNode might be registered multiple times in the same VT,
  66. // so it would be associated with multiple PVirtualNode's.
  67.  
  68. for SubNode in Node.Sub do
  69. AddNodestoTree(ThisNode, SubNode);
  70. end;
  71.  
  72. // And start processing like this:
  73. VT.NodeDataSize := SizeOf(Pointer); // Make sure we specify the size of the node's payload.
  74. // A variable holding an object reference in Delphi is actually
  75. // a pointer, so the node needs enough space to hold 1 pointer.
  76. AddNodesToTree(nil, Root);
  77.  
  78. procedure TForm1.VTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
  79. Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
  80. var PayloadObject:TObject;
  81. Node: TNode;
  82. Contact : TContact;
  83. ContactCategory : TContactCategory;
  84. begin
  85. PayloadObject := TObject(VT.GetNodeData(Node)^); // Extract the payload of the node as a TObject so
  86. // we can check it's type before proceeding.
  87. if not Assigned(PayloadObject) then
  88. CellText := 'Bug: Node payload not assigned'
  89. else if PayloadObject is TNode then
  90. begin
  91. Node := TNode(PayloadObject); // We know it's a TNode, assign it to the proper var so we can easily work with it
  92. CellText := Node.Name;
  93. end
  94. else if PayloadObject is TContact then
  95. begin
  96. Contact := TContact(PayloadObject);
  97. CellText := Contact.FirstName + ' ' + Contact.LastName + ' (' + Contact.PhoneNumber + ')';
  98. end
  99. else if PayloadObject is TContactCategory then
  100. begin
  101. ContactCategory := TContactCategory(PayloadObject);
  102. CellText := ContactCategory.CategoryName + ' (' + IntToStr(ContactCategory.Contacts.Count) + ' contacts)';
  103. end
  104. else
  105. CellText := 'Bug: don''t know how to extract CellText from ' + PayloadObject.ClassName;
  106. end;
  107.  
  108. procedure TForm1.ButtonModifyClick(Sender: TObject);
  109. begin
  110. Root.Sub[0].Sub[0].Name := 'Someone else'; // I'll modify the node itself
  111. VT.InvalidateNode(Root.Sub[0].Sub[0].VTNode); // and invalidate the tree; when displayed again, it will
  112. // show the updated text.
  113. end;
  114.  
  115. uses
  116. svCollections.GenericTrees;
  117.  
  118. type
  119. TMainData = record
  120. Name: string;
  121. ID: Integer;
  122. end;
  123.  
  124. MyTree: TSVTree<TMainData>;
  125.  
  126. MyTree: TSVTree<TMainData>.Create(False);
  127.  
  128. MyTree.VirtualTree := VST;
  129.  
  130. procedure TForm1.BuildStructure(Count: Integer);
  131. var
  132. i, j: Integer;
  133. svNode, svNode2: TSVTreeNode<TMainData>;
  134. Data: TMainData;
  135. begin
  136. MyTree.BeginUpdate;
  137. try
  138. for i := 0 to Count - 1 do
  139. begin
  140. Data.Name := Format('Root %D', [i]);
  141. Data.ID := i;
  142. svNode := MyTree.AddChild(nil, Data);
  143. for j:= 0 to 10 - 1 do
  144. begin
  145. Data.Name := Format('Child %D', [j]);
  146. Data.ID := j;
  147. svNode2 := MyTree.AddChild(svNode, Data);
  148. end;
  149. end;
  150. finally
  151. MyTree.EndUpdate;
  152. end;
  153. end;
  154.  
  155. procedure TForm1.vt1InitChildren(Sender: TBaseVirtualTree; Node: PVirtualNode;
  156. var ChildCount: Cardinal);
  157. var
  158. svNode: TSVTreeNode<TMainData>;
  159. begin
  160. svNode := MyTree.GetNode(Sender.GenerateIndex(Node));
  161. if Assigned(svNode) then
  162. begin
  163. ChildCount := svNode.FChildCount;
  164. end;
  165. end;
  166.  
  167. procedure TForm1.vt1InitNode(Sender: TBaseVirtualTree; ParentNode,
  168. Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);
  169. var
  170. svNode: TSVTreeNode<TMainData>;
  171. begin
  172. svNode := MyTree.GetNode(Sender.GenerateIndex(Node));
  173. if Assigned(svNode) then
  174. begin
  175. //if TSVTree<TTestas> is synced with Virtual Treeview and we are building tree by
  176. // setting RootNodeCount, then we must set svNode.FVirtualNode := Node to
  177. // have correct node references
  178. svNode.FVirtualNode := Node; // Don't Forget!!!!
  179. if svNode.HasChildren then
  180. begin
  181. Include(InitialStates, ivsHasChildren);
  182. end;
  183. end;
  184. end;
  185.  
  186. //display info how you like, I simply get name and ID values
  187. procedure TForm1.vt1GetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
  188. Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
  189. var
  190. svNode: TSVTreeNode<TMainData>;
  191. begin
  192. svNode := MyTree.GetNode(Sender.GenerateIndex(Node));
  193. if Assigned(svNode) then
  194. begin
  195. CellText := Format('%S ID:%D',[svNode.FValue.Name, svNode.FValue.ID]);
  196. end;
  197. end;
  198.  
  199. type
  200. TNode = class
  201. Parent: TNode;
  202. NextSibling: TNode;
  203. FirstChild: TNode;
  204. end;
  205.  
  206. TTree = class
  207. Root: TNode;
  208. function AddNode(Parent: TNode): TNode;
  209. end;
  210.  
  211. function TTree.AddNode(Parent: TNode);
  212. var
  213. Node: TNode;
  214. begin
  215. Result := TNode.Create;
  216.  
  217. Result.Parent := Parent;
  218. Result.NextSibling := nil;
  219. Result.FirstChild := nil;
  220.  
  221. //this may be the first node in the tree
  222. if not Assigned(Root) then begin
  223. Assert(not Assigned(Parent));
  224. Root := Result;
  225. exit;
  226. end;
  227.  
  228. //this may be the first child of this parent
  229. if Assigned(Parent) and not Assigned(Parent.FirstChild) then begin
  230. Parent.FirstChild := Result;
  231. end;
  232.  
  233. //find the previous sibling and assign its next sibling to the new node
  234. if Assigned(Parent) then begin
  235. Node := Parent.FirstChild;
  236. end else begin
  237. Node := Root;
  238. end;
  239. if Assigned(Node) then begin
  240. while Assigned(Node.NextSibling) do begin
  241. Node := Node.NextSibling;
  242. end;
  243. Node.NextSibling := Result;
  244. end;
  245. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement