使用 1 个数据副本同步多个 VirtualStringTree

发布于 2024-12-11 21:04:18 字数 1849 浏览 0 评论 0原文

我有一个显示在 VirtualStringTree 中的数据层次结构。我在应用程序中多次使用此层次结构,并对树的绘制/显示方式进行了轻微修改。我的方法当前使用 AddChild() 过程来添加节点,因此在应用程序运行时我有多个数据副本。

我现在想合并这些树,并拥有一个指向实际数据的“主”树,但随后让“从”树指向相同的数据。

我有点不确定是否/如何实现这一点。我认为我可以简单地加载主树并用指向我保存的数据的指针填充其 NodeData ,然后对于所有从属树,只需将相同的指针存储在其节点数据中。

不过我运气不太好。

我当前的代码如下所示:

//Initialize the NodeDataSize
procedure TForm1.FormCreate(Sender: TObject);
begin
     TreeMasterComponents.NodeDataSize := SizeOf(rMasterComponent);
     VST.NodeDataSize := SizeOf(Pointer);
end;

将主树复制到从树的过程

procedure TForm1.LoadSlaveTree(ATree: TVirtualStringTree);
var Node : PVirtualNode;

procedure RecursiveCopy(SrcPNode,SrcTNode : PVirtualNode; ATree : TVirtualStringTree);
var SrcNode, TargetNode : PVirtualNode;
SrcData : PMasterComponent;
begin
     SrcNode := TreeMasterComponents.GetFirstChild(SrcPNode);
     while Assigned(SrcNode) do
     begin
          SrcData := TreeMasterComponents.GetNodeData(SrcNode);
          TargetNode := ATree.AddChild(SrcTNode,SrcData);
          RecursiveCopy(SrcNode,TargetNode,ATree);
          SrcNode := SrcNode.NextSibling;
     end;
end;

begin
     ATree.BeginUpdate;
     ATree.Clear;
     Node := TreeMasterComponents.GetFirst(true);
     while Assigned(Node) do
     begin
         RecursiveCopy(Node,nil,ATree);
         Node := Node.NextSibling;
     end;
     ATree.EndUpdate;
end;

从属树获取CellText的过程

procedure TForm1.SlaveGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
  Column: TColumnIndex; TextType: TVSTTextType; var CellText: WideString);
var Data : PMasterComponent;
begin
     Data := Sender.GetNodeData(Node);
     Case column of
     0:CellText := Data^.ComponentCode;
     1:CellText := Data^.FullLocation;
     end;
end;

目前,节点已添加到正确的层次结构中,但是从属树没有出现文本。任何帮助将不胜感激。

I have a heirarchy of data that is displayed in a VirtualStringTree. I use this heirarchy multiple times in my application with slight modifications to the way the tree is drawn/displayed. My method currently utilizes the AddChild() procedure for adding nodes an d as such i have multiple copies of the data when the application is run.

I would now like to consolidate these trees and have a 'master' tree which points to the actual data, but then have the 'slave' trees point to the SAME data.

I am a little unsure of if/how this can be acheived. I would think i could simply load the master tree and populate its NodeData with pointers to where the data i being held, and then for all the slave trees, simply store the same pointer in their nodedata.

However im not having much luck.

My current code looks like:

//Initialize the NodeDataSize
procedure TForm1.FormCreate(Sender: TObject);
begin
     TreeMasterComponents.NodeDataSize := SizeOf(rMasterComponent);
     VST.NodeDataSize := SizeOf(Pointer);
end;

Procedure to copy the master tree to slave trees

procedure TForm1.LoadSlaveTree(ATree: TVirtualStringTree);
var Node : PVirtualNode;

procedure RecursiveCopy(SrcPNode,SrcTNode : PVirtualNode; ATree : TVirtualStringTree);
var SrcNode, TargetNode : PVirtualNode;
SrcData : PMasterComponent;
begin
     SrcNode := TreeMasterComponents.GetFirstChild(SrcPNode);
     while Assigned(SrcNode) do
     begin
          SrcData := TreeMasterComponents.GetNodeData(SrcNode);
          TargetNode := ATree.AddChild(SrcTNode,SrcData);
          RecursiveCopy(SrcNode,TargetNode,ATree);
          SrcNode := SrcNode.NextSibling;
     end;
end;

begin
     ATree.BeginUpdate;
     ATree.Clear;
     Node := TreeMasterComponents.GetFirst(true);
     while Assigned(Node) do
     begin
         RecursiveCopy(Node,nil,ATree);
         Node := Node.NextSibling;
     end;
     ATree.EndUpdate;
end;

Procedure for slave tree to getCellText

procedure TForm1.SlaveGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
  Column: TColumnIndex; TextType: TVSTTextType; var CellText: WideString);
var Data : PMasterComponent;
begin
     Data := Sender.GetNodeData(Node);
     Case column of
     0:CellText := Data^.ComponentCode;
     1:CellText := Data^.FullLocation;
     end;
end;

At the moment, the nodes are added in the correct heirarchy, however there is no text appearing for the slave trees. Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱冒险 2024-12-18 21:04:18

我不知道为什么您的奴隶树中没有出现任何文本,但我想提出以下建议。

可能更容易的是使用树和代码创建一个框架,并在表单上重用该框架。树的每个实例只需加载相同的数据(无需复制节点)。

通过为框架/树的特定实例编写新的事件处理程序,可以通过视觉形式继承来实现细微的修改。如果需要,您还可以从框架继承,创建一个新类。

I don't know why no text appears in your slave trees but I'd like to advise the following.

Probably easier would be to create a frame with the tree and code, and reuse the frame on your forms. Each instance of the tree would simply load the same data (no copying of nodes necessary).

The slight modifications can be achieved by visual form inheritance by writing new event handlers for the specific instance of the frame/tree. You can also inherit from the frame, creating a new class, if needed.

不必了 2024-12-18 21:04:18

好吧,我相信我找到了一个解决方案:

诀窍是创建一个新记录来存储指向原始数据的指针:

type PRefMasterComponent = ^RefMasterComponent;
  RefMasterComponent = packed record
     PMCData : PMasterComponent;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
     TreeMasterComponents.NodeDataSize := SizeOf(rMasterComponent);
     VST.NodeDataSize := SizeOf(RefMasterComponent);
end;

现在复制代码如下所示:

procedure TForm1.LoadTree(ATree: TVirtualStringTree);
var Node,TargetNode : PVirtualNode;
SrcData : PMasterComponent;
Data : PRefMasterComponent;

procedure RecursiveCopy(SrcPNode, TargetPNode : PVirtualNode; ATree : TVirtualStringTree);
var Node, TargetNode : PVirtualNode;
SrcData : PMasterComponent;
Data : PRefMasterComponent;
begin
     Node := TreeMasterComponents.GetFirstChild(SrcPNode);
     while Assigned(Node) do
     begin
          SrcData := TreeMasterComponents.GetNodeData(Node);
          TargetNode := ATree.AddChild(TargetPNode);
          Data := ATree.GetNodeData(TargetNode);
          Data.PMCData := SrcData;
          RecursiveCopy(Node,TargetNode,ATree);
          Node := Node.NextSibling;
     end;
end;

begin
     ATree.BeginUpdate;
     ATree.Clear;
     Node := TreeMasterComponents.GetFirst(true);
     while Assigned(Node) do
     begin
          SrcData := TreeMasterComponents.GetNodeData(Node);
          TargetNode := ATree.AddChild(nil);
          Data := ATree.GetNodeData(TargetNode);
          Data.PMCData := SrcData;
          RecursiveCopy(Node,TargetNode,ATree);
          Node := Node.NextSibling;
     end;
     ATree.EndUpdate;
end;

和 OnGetText:

procedure TForm1.vstGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
  Column: TColumnIndex; TextType: TVSTTextType; var CellText: WideString);
var Data : PRefMasterComponent;
RefData : PMasterComponent;
begin
     Data := Sender.GetNodeData(Node);
     RefData := Data.PMCData;
     Case column of
     0:CellText := RefData.ComponentCode;
     1:CellText := RefData.FullLocation;
     end;
end;

现在,如果我更改一棵树中的数据,我必须这样做要做的就是调用 VST.Invalidate ,更改会反映在另一棵树中。

Ok so i beleive i have found a solution:

The trick was to create a new record to store the Pointer to the original data:

type PRefMasterComponent = ^RefMasterComponent;
  RefMasterComponent = packed record
     PMCData : PMasterComponent;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
     TreeMasterComponents.NodeDataSize := SizeOf(rMasterComponent);
     VST.NodeDataSize := SizeOf(RefMasterComponent);
end;

Now the Copy code looks like:

procedure TForm1.LoadTree(ATree: TVirtualStringTree);
var Node,TargetNode : PVirtualNode;
SrcData : PMasterComponent;
Data : PRefMasterComponent;

procedure RecursiveCopy(SrcPNode, TargetPNode : PVirtualNode; ATree : TVirtualStringTree);
var Node, TargetNode : PVirtualNode;
SrcData : PMasterComponent;
Data : PRefMasterComponent;
begin
     Node := TreeMasterComponents.GetFirstChild(SrcPNode);
     while Assigned(Node) do
     begin
          SrcData := TreeMasterComponents.GetNodeData(Node);
          TargetNode := ATree.AddChild(TargetPNode);
          Data := ATree.GetNodeData(TargetNode);
          Data.PMCData := SrcData;
          RecursiveCopy(Node,TargetNode,ATree);
          Node := Node.NextSibling;
     end;
end;

begin
     ATree.BeginUpdate;
     ATree.Clear;
     Node := TreeMasterComponents.GetFirst(true);
     while Assigned(Node) do
     begin
          SrcData := TreeMasterComponents.GetNodeData(Node);
          TargetNode := ATree.AddChild(nil);
          Data := ATree.GetNodeData(TargetNode);
          Data.PMCData := SrcData;
          RecursiveCopy(Node,TargetNode,ATree);
          Node := Node.NextSibling;
     end;
     ATree.EndUpdate;
end;

and the OnGetText:

procedure TForm1.vstGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
  Column: TColumnIndex; TextType: TVSTTextType; var CellText: WideString);
var Data : PRefMasterComponent;
RefData : PMasterComponent;
begin
     Data := Sender.GetNodeData(Node);
     RefData := Data.PMCData;
     Case column of
     0:CellText := RefData.ComponentCode;
     1:CellText := RefData.FullLocation;
     end;
end;

Now if i change data in one tree all i have to do is call VST.Invalidate and the changes are reflected in the other tree.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文