lcl: TTreeNodeExpandedState: added OnGetNodeText

git-svn-id: trunk@53492 -
This commit is contained in:
mattias 2016-11-30 22:54:18 +00:00
parent 73e7b0a2f8
commit 276bf3674b
2 changed files with 36 additions and 9 deletions

View File

@ -3681,6 +3681,8 @@ type
end; end;
TTVGetNodeText = function(Node: TTreeNode): string of object;
{ TTreeNodeExpandedState } { TTreeNodeExpandedState }
{ class to store and restore the expanded state of a TTreeView { class to store and restore the expanded state of a TTreeView
The nodes are identified by their Text property. The nodes are identified by their Text property.
@ -3695,15 +3697,20 @@ type
} }
TTreeNodeExpandedState = class TTreeNodeExpandedState = class
private
FOnGetNodeText: TTVGetNodeText;
function DefaultGetNodeText(Node: TTreeNode): string;
public
NodeText: string; NodeText: string;
Children: TAvgLvlTree; Children: TAvgLvlTree;
constructor Create(FirstTreeNode: TTreeNode); constructor Create(FirstTreeNode: TTreeNode; const GetNodeTextEvent: TTVGetNodeText = nil);
constructor Create(TreeView: TCustomTreeView); constructor Create(TreeView: TCustomTreeView; const GetNodeTextEvent: TTVGetNodeText = nil);
destructor Destroy; override; destructor Destroy; override;
procedure Clear; procedure Clear;
procedure CreateChildNodes(FirstTreeNode: TTreeNode); procedure CreateChildNodes(FirstTreeNode: TTreeNode);
procedure Apply(FirstTreeNode: TTreeNode; CollapseToo: boolean = true); procedure Apply(FirstTreeNode: TTreeNode; CollapseToo: boolean = true);
procedure Apply(TreeView: TCustomTreeView; CollapseToo: boolean = true); procedure Apply(TreeView: TCustomTreeView; CollapseToo: boolean = true);
property OnGetNodeText: TTVGetNodeText read FOnGetNodeText write FOnGetNodeText;
end; end;

View File

@ -192,13 +192,28 @@ end;
{ TTreeNodeExpandedState } { TTreeNodeExpandedState }
constructor TTreeNodeExpandedState.Create(FirstTreeNode: TTreeNode); function TTreeNodeExpandedState.DefaultGetNodeText(Node: TTreeNode): string;
begin begin
Result:=Node.Text;
end;
constructor TTreeNodeExpandedState.Create(FirstTreeNode: TTreeNode;
const GetNodeTextEvent: TTVGetNodeText);
begin
if GetNodeTextEvent<>nil then
FOnGetNodeText:=GetNodeTextEvent
else
FOnGetNodeText:=@DefaultGetNodeText;
CreateChildNodes(FirstTreeNode); CreateChildNodes(FirstTreeNode);
end; end;
constructor TTreeNodeExpandedState.Create(TreeView: TCustomTreeView); constructor TTreeNodeExpandedState.Create(TreeView: TCustomTreeView;
const GetNodeTextEvent: TTVGetNodeText);
begin begin
if GetNodeTextEvent<>nil then
FOnGetNodeText:=GetNodeTextEvent
else
FOnGetNodeText:=@DefaultGetNodeText;
CreateChildNodes(TreeView.Items.GetFirstNode); CreateChildNodes(TreeView.Items.GetFirstNode);
end; end;
@ -221,16 +236,19 @@ var
ChildNode: TTreeNode; ChildNode: TTreeNode;
NewExpandedNode: TTreeNodeExpandedState; NewExpandedNode: TTreeNodeExpandedState;
begin begin
if (FirstTreeNode<>nil) and (FirstTreeNode.Parent<>nil) then Clear;
NodeText:=FirstTreeNode.Parent.Text if (FirstTreeNode=nil) then exit;
if (FirstTreeNode.Parent<>nil) then
NodeText:=OnGetNodeText(FirstTreeNode.Parent)
else else
NodeText:=''; NodeText:='';
Clear;
ChildNode:=FirstTreeNode; ChildNode:=FirstTreeNode;
while ChildNode<>nil do begin while ChildNode<>nil do begin
if ChildNode.Expanded then begin if ChildNode.Expanded then begin
if Children=nil then Children:=TAvgLvlTree.Create(@CompareExpandedNodes); if Children=nil then Children:=TAvgLvlTree.Create(@CompareExpandedNodes);
NewExpandedNode:=TTreeNodeExpandedState.Create(ChildNode.GetFirstChild); NewExpandedNode:=TTreeNodeExpandedState.Create(ChildNode.GetFirstChild,OnGetNodeText);
if ChildNode.GetFirstChild=nil then
NewExpandedNode.NodeText:=OnGetNodeText(ChildNode);
Children.Add(NewExpandedNode); Children.Add(NewExpandedNode);
end; end;
ChildNode:=ChildNode.GetNextSibling; ChildNode:=ChildNode.GetNextSibling;
@ -246,8 +264,10 @@ begin
if Children=nil then exit; if Children=nil then exit;
ChildNode:=FirstTreeNode; ChildNode:=FirstTreeNode;
while ChildNode<>nil do begin while ChildNode<>nil do begin
ChildNodeText:=ChildNode.Text; ChildNodeText:=OnGetNodeText(ChildNode);
ANode:=Children.FindKey(PChar(ChildNodeText),@CompareTextWithExpandedNode); ANode:=Children.FindKey(PChar(ChildNodeText),@CompareTextWithExpandedNode);
if ChildNodeText='' then
debugln(['TTreeNodeExpandedState.Apply ',ChildNode.GetTextPath,' ChildNodeText="',ChildNodeText,'"']);
if ANode<>nil then if ANode<>nil then
ChildNode.Expanded:=true ChildNode.Expanded:=true
else if CollapseToo then else if CollapseToo then