Recent

Author Topic: [SOLVED] Checking expanding and collapsing state of a TreeView  (Read 2526 times)

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Dear ALL,

TTreeView has methods for full collapsing/full expanding a tree, and a TTreeNode has a method for expanding itself, as well as one for checking if a node is expanded or not.

OK, but then I ask: is there a way to check the expanding/collapsing state of an entire tree (so that it can be restored to a previous state, either expanded or collapsed, after being updated)?

Thanks in advance!

With best wishes,
« Last Edit: July 20, 2020, 01:54:43 am by maurobio »
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Checking expanding and collapsing state of a TreeView
« Reply #1 on: July 19, 2020, 02:56:04 pm »
have you looked at the SaveToStream and LoadFromStream yet ?

Not sure if that will restore the state of the nodes but its worth a try..
The only true wisdom is knowing you know nothing

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: Checking expanding and collapsing state of a TreeView
« Reply #2 on: July 19, 2020, 03:02:19 pm »
@jamie,

I deal with large trees, and cannot keep saving/loading them each time a user clicks on a node!

I need a fast solution which would simply store the state of a tree before updating, so that it can be restored to the previous state after updating.

My regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Checking expanding and collapsing state of a TreeView
« Reply #3 on: July 19, 2020, 03:07:42 pm »
Ok. I understand you but you can do a stream save to a Tmemorystream, if does not need to be on file. This will work fast.

 I could also make your own code to enumerate through the nodes to save the states too and restore them later but the results will be the same..

 why not build yourself a cache of prior states that you can restore for the user because when you do all of this events do get fired that allows you to record this activity.

 I don't know of any existing feature in the TreeView now that does this.
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Checking expanding and collapsing state of a TreeView
« Reply #4 on: July 19, 2020, 03:11:08 pm »
What does "keepCollaspedNodes" Property do for you ?
The only true wisdom is knowing you know nothing

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: Checking expanding and collapsing state of a TreeView
« Reply #5 on: July 19, 2020, 03:24:47 pm »
@jamie,

Quote
What does "keepCollaspedNodes" Property do for you ?

Nothing. Each time the tree is cleared for updating, it returns to the fully collapsed state, even if it was fully expanded beforehand.

My regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Checking expanding and collapsing state of a TreeView
« Reply #6 on: July 19, 2020, 03:52:52 pm »
You can try tvoAutoExpand.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: Checking expanding and collapsing state of a TreeView
« Reply #7 on: July 19, 2020, 04:07:58 pm »
@Blaazen,

AutoExpand also does not suits my needs, because if a tree is fully collapsed before updating, it displays fully expanded after updating.

My regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

balazsszekely

  • Guest
Re: Checking expanding and collapsing state of a TreeView
« Reply #8 on: July 19, 2020, 04:26:08 pm »
@maurobio
Quote
Nothing. Each time the tree is cleared for updating, it returns to the fully collapsed state, even if it was fully expanded beforehand.
Why do you clear the tree? Instead you should loop through the nodes and update each one according to the new data. Only delete the nodes that are no longer available in your updated list(I suppose you have a list/array/etc. and you update the tree accordingly).

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: Checking expanding and collapsing state of a TreeView
« Reply #9 on: July 19, 2020, 04:34:48 pm »
@GetMem,

If I do not clear the tree, each time I loop through the array of records which stores the nodes, they are added after the bottom nodes.

What I really need is a way of checking the expanded state of each node of the tree before updating, so that they can be returned to their values (True or False) after updating.

My regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Checking expanding and collapsing state of a TreeView
« Reply #10 on: July 19, 2020, 04:34:55 pm »
If you do not need the StateIndex of the nodes you could store the Expanded/Collapsed state there (collapsed = 0, expanded = 1). But TTreeView is general enough so that you can register in the OnCreateNodeClass event your own node class type which memorizes the stored expanded/collapsed state once determined.

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyTreeNode = class(TTreeNode)
  3.   public
  4.     WasExpanded: Boolean;
  5.   end;
  6.  
  7. procedure TForm1.TreeView1CreateNodeClass(Sender: TCustomTreeView;
  8.   var NodeClass: TTreeNodeClass);
  9. begin
  10.   NodeClass := TMyTreeNode;
  11. end;  

To store or retore the expanded/collapsed state you must write a recursive procedure which iterates through all nodes and writes the current state of the Expanded property to the new WasExpanded value:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.IterateNodes(ANode: TTreeNode; What: Integer);
  2. var
  3.   child: TTreeNode;
  4. begin
  5.   if ANode = nil then
  6.     ANode := TreeView1.Items.GetFirstNode;
  7.  
  8.   if (ANode is TMyTreeNode) then
  9.     case what of
  10.       0: Listbox1.Items.Add(ANode.Text + ': ' + BoolToSTr(TMyTreeNode(ANode).WasExpanded, true));
  11.       1: TMyTreeNode(ANode).WasExpanded := ANode.Expanded;
  12.       2: if TMyTreeNode(ANode).WasExpanded then ANode.Expand(false) else ANode.Collapse(false);
  13.     end
  14.   else
  15.     exit;
  16.  
  17.   if ANode.HasChildren then
  18.   begin
  19.     child := ANode.GetFirstChild;
  20.     IterateNodes(child, what);
  21.   end;
  22.  
  23.   ANode := ANode.GetNextSibling;
  24.   while ANode <> nil do
  25.   begin
  26.     IterateNodes(ANode, What);
  27.     ANode := ANode.GetNextSibling;
  28.   end;
  29. end;  

Note the special case when ANode is nil: This is the argument to be used to iterate through the entire tree. And the argument What determines what happens with each node: 0 = log WasExpanded to a Listbox for debugging, 1 = save Expanded to WasExpanded, 2 = restore Expanded.

See the attached demo. Expand or collapse nodes of the demo tree as you like, then press "Save expanded/collapsed state", change the expansion state of some nodes again. When you press "Restore..." the saved state will be re-established.
« Last Edit: July 19, 2020, 04:36:52 pm by wp »

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: Checking expanding and collapsing state of a TreeView
« Reply #11 on: July 19, 2020, 04:57:30 pm »
@wp,

Thank you very much for the example code!  :-[

I'll try it and post the results ASAP.

With best wishes,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

maurobio

  • Hero Member
  • *****
  • Posts: 623
  • Ecology is everything.
    • GitHub
Re: Checking expanding and collapsing state of a TreeView
« Reply #12 on: July 20, 2020, 01:54:25 am »
@wp,

Your provided example works fine!

Again, thank you very much.

With best wishes. 
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 2.0.12 - FPC 3.2.0 on GNU/Linux Mint 19.1, Lubuntu 18.04, Windows XP SP3, Windows 7 Professional, Windows 10 Home

 

TinyPortal © 2005-2018