Consolidate tree processing

- Only build treeview once, destroy/create child content on change
- Single recursive AddTree(IObject3D) method handles all nodes
- BuildTree caller assigns node.TreeView instance on root node
- MatterHackers/MCCentral#3483
Differing construction implementations results in inconsistent TreeNodes
This commit is contained in:
John Lewin 2018-06-01 08:49:02 -07:00
parent 9e69b4a915
commit 5244025ae3
5 changed files with 48 additions and 70 deletions

View file

@ -378,7 +378,14 @@ namespace MatterHackers.MatterControl.CustomWidgets.TreeView
// A TreeView that represents the parent tree view that the
// tree node is assigned to, or null if the node has not been assigned to a tree
// view.
public virtual TreeView TreeView => NodeParent.TreeView;
private TreeView _treeView;
public virtual TreeView TreeView
{
get => _treeView ?? NodeParent.TreeView;
set => _treeView = value;
}
private void OnImageChanged(EventArgs args)
{

View file

@ -36,33 +36,22 @@ using MatterHackers.VectorMath;
namespace MatterHackers.MatterControl.CustomWidgets.TreeView
{
public class TopNode : TreeNode
{
internal TreeView treeView;
public override TreeView TreeView => treeView;
}
public class TreeView : ScrollableWidget
{
private ThemeConfig theme;
public TreeView(TopNode topNode, ThemeConfig theme)
: this(topNode, 0, 0, theme)
public TreeView(ThemeConfig theme)
: this(0, 0, theme)
{
}
public TreeView(TopNode topNode, int width, int height, ThemeConfig theme)
public TreeView(int width, int height, ThemeConfig theme)
: base(width, height)
{
this.theme = theme;
this.AutoScroll = true;
this.HAnchor = HAnchor.Stretch;
this.VAnchor = VAnchor.Stretch;
this.TopNode = topNode;
topNode.treeView = this;
this.AddChild(TopNode);
}
#region Events
@ -230,7 +219,6 @@ namespace MatterHackers.MatterControl.CustomWidgets.TreeView
public bool ShowPlusMinus { get; set; }
public bool ShowRootLines { get; set; }
public bool Sorted { get; set; }
public TreeNode TopNode { get; }
public IComparer TreeViewNodeSorter { get; set; }

View file

@ -31,6 +31,7 @@ using MatterHackers.Agg.UI;
using MatterHackers.DataConverters3D;
using MatterHackers.MatterControl.CustomWidgets;
using MatterHackers.MatterControl.CustomWidgets.TreeView;
using MatterHackers.MatterControl.DesignTools;
using MatterHackers.MatterControl.Library;
using MatterHackers.MatterControl.PartPreviewWindow.View3D;
@ -38,39 +39,12 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
{
public static class Object3DTreeBuilder
{
public static TreeView GetPartTreeView(IObject3D rootItem, ThemeConfig theme)
public static TreeNode BuildTree(IObject3D rootItem, ThemeConfig theme)
{
var topNode = new TopNode()
{
Text = BuildDefaultName(rootItem),
Tag = rootItem,
TextColor = theme.Colors.PrimaryTextColor,
PointSize = theme.DefaultFontSize
};
var treeView = new TreeView(topNode, theme)
{
TextColor = theme.Colors.PrimaryTextColor,
PointSize = theme.DefaultFontSize
};
treeView.SuspendLayout();
//selectionTreeNodes.Clear();
//selectionTreeNodes.Add(rootItem, topNode);
// add the children to the root node
foreach (var child in rootItem.Children)
{
AddTree(child, topNode, theme);
}
treeView.ResumeLayout();
return treeView;
return AddTree(rootItem, null, theme);
}
private static void AddTree(IObject3D item, TreeNode parent, ThemeConfig theme)
private static TreeNode AddTree(IObject3D item, TreeNode parent, ThemeConfig theme)
{
// Suppress MeshWrapper nodes in treeview - retain parent node as context reference
var contextNode = (item is MeshWrapper) ? parent : AddItem(item, parent, theme);
@ -79,6 +53,8 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
{
AddTree(child, contextNode, theme);
}
return contextNode;
}
private static TreeNode AddItem(IObject3D item, TreeNode parentNode, ThemeConfig theme)
@ -115,8 +91,11 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
};
}
parentNode.Nodes.Add(node);
parentNode.Expanded = true;
if (parentNode != null)
{
parentNode.Nodes.Add(node);
parentNode.Expanded = true;
}
return node;
}

View file

@ -192,7 +192,18 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
modelViewSidePanel.AddChild(treeSection);
// add the tree view
this.RebuildTreeSection(new Object3D(), theme);
treeView = new TreeView(theme)
{
TextColor = theme.Colors.PrimaryTextColor,
PointSize = theme.DefaultFontSize,
HAnchor =HAnchor.Stretch,
VAnchor = VAnchor.Stretch
};
treeView.AfterSelect += (s, e) =>
{
selectedObjectPanel.SetActiveItem((IObject3D)treeView.SelectedNode.Tag);
};
treeSection.AddChild(treeView);
modelViewSidePanel.AddChild(selectedObjectPanel);
splitContainer.AddChild(modelViewSidePanel);
@ -236,24 +247,6 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
}
}
private void RebuildTreeSection(IObject3D selection, ThemeConfig theme)
{
treeSection.CloseAllChildren();
treeView = Object3DTreeBuilder.GetPartTreeView(selection, theme);
treeSection.AddChild(treeView);
treeView.AfterSelect += (s, e) =>
{
selectedObjectPanel.SetActiveItem((IObject3D)treeView.SelectedNode.Tag);
};
if (this.Parent != null)
{
treeView.TopNode.Padding = treeView.TopNode.Padding.Clone(left: 8, top: 8);
treeView.SelectedNode = treeView.TopNode;
}
}
private void UpdateRenderView(object sender, EventArgs e)
{
TrackballTumbleWidget.CenterOffsetX = -modelViewSidePanel.Width;
@ -1222,9 +1215,20 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
}
// Top level selection only - rebuild tree
if (Scene.Children.Contains(Scene.SelectedItem))
var selection = Scene.SelectedItem;
if (Scene.Children.Contains(selection))
{
this.RebuildTreeSection(Scene.SelectedItem, theme);
treeView.ScrollArea.CloseAllChildren();
var rootNode = Object3DTreeBuilder.BuildTree(selection, theme);
treeView.AddChild(rootNode);
rootNode.TreeView = treeView;
if (this.Parent != null)
{
rootNode.Padding = rootNode.Padding.Clone(left: 8, top: 8);
treeView.SelectedNode = rootNode;
}
}
}

@ -1 +1 @@
Subproject commit a38d5e68bcaace904d1dc18e416ea9ace6756ac2
Subproject commit 21532f82c733e0adc9db8b82f2f10cb66b60ed52