Ignore:
Timestamp:
03/07/14 16:23:41 (5 years ago)
Author:
mkommend
Message:

#2076: Simplified the API of the tree layout engines.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeChart.cs

    r10561 r10565  
    2727using System.Linq;
    2828using System.Windows.Forms;
    29 using Point = System.Drawing.Point;
     29
    3030
    3131namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
     
    5050      this.backgroundColor = Color.White;
    5151      this.textFont = new Font(FontFamily.GenericSansSerif, 12);
    52       layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> {
     52
     53      visualTreeNodes = new Dictionary<ISymbolicExpressionTreeNode, VisualTreeNode<ISymbolicExpressionTreeNode>>();
     54      visualLines = new Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualTreeNodeConnection>();
     55
     56      layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees) {
    5357        NodeWidth = preferredNodeWidth,
    5458        NodeHeight = preferredNodeHeight,
     
    5660        VerticalSpacing = minVerticalDistance
    5761      };
    58     }
    59 
    60     private ILayoutEngine<ISymbolicExpressionTreeNode> TreeLayoutEngine {
    61       get { return layoutEngine; }
    62       set {
    63         layoutEngine = value;
    64         InitializeLayout();
    65         Repaint();
    66       }
    67     }
     62      reingoldTilfordToolStripMenuItem.Checked = true;
     63    }
     64
     65    //private ILayoutEngine<ISymbolicExpressionTreeNode> TreeLayoutEngine {
     66    //  get { return layoutEngine; }
     67    //  set {
     68    //    layoutEngine = value;
     69    //    InitializeLayout();
     70    //    Repaint();
     71    //  }
     72    //}
    6873
    6974    public SymbolicExpressionTreeChart(ISymbolicExpressionTree tree)
     
    114119      set {
    115120        tree = value;
    116         if (tree != null) {
    117           //the layout engine needs to be initialized here so that the visualNodes and the visualLines dictionaries are populated
    118           InitializeLayout();
    119           Repaint();
    120         }
     121        Repaint();
    121122      }
    122123    }
     
    285286    #endregion
    286287
    287     #region initialize the layout
    288     private void InitializeLayout() {
     288    private void CalculateLayout(int preferredWidth, int preferredHeight, int minHDistance, int minVDistance) {
     289      layoutEngine.NodeWidth = preferredWidth;
     290      layoutEngine.NodeHeight = preferredHeight;
     291      layoutEngine.HorizontalSpacing = minHDistance;
     292      layoutEngine.VerticalSpacing = minVDistance;
     293
    289294      var actualRoot = tree.Root;
    290295      if (actualRoot.Symbol is ProgramRootSymbol && actualRoot.SubtreeCount == 1) {
    291296        actualRoot = tree.Root.GetSubtree(0);
    292297      }
    293       layoutEngine.Initialize(actualRoot, n => n.Subtrees, n => n.GetLength(), n => n.GetDepth());
    294       layoutEngine.CalculateLayout(this.Width, this.Height);
    295       UpdateDictionaries();
    296     }
    297 
    298     private void UpdateDictionaries() {
    299       var visualNodes = layoutEngine.GetVisualNodes().ToList();
    300       //populate the visual nodes and visual connections dictionaries
     298
     299      var visualNodes = layoutEngine.CalculateLayout(actualRoot, Width, Height).ToList();
    301300      visualTreeNodes = visualNodes.ToDictionary(x => x.Content, x => x);
    302301      visualLines = new Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualTreeNodeConnection>();
     
    307306      }
    308307    }
    309     #endregion
     308
    310309    #region methods for painting the symbolic expression tree
    311310    private void DrawFunctionTree(Graphics graphics, int preferredWidth, int preferredHeight, int minHDistance, int minVDistance) {
    312       //we assume here that the layout has already been initialized when the symbolic expression tree was changed
    313       //recalculate layout according to new node widths and spacing
    314       layoutEngine.NodeWidth = preferredWidth;
    315       layoutEngine.NodeHeight = preferredHeight;
    316       layoutEngine.HorizontalSpacing = minHDistance;
    317       layoutEngine.VerticalSpacing = minVDistance;
    318       layoutEngine.CalculateLayout(Width, Height);
    319       UpdateDictionaries();//this will reset the visual nodes, therefore colors will be reset to default values
     311      CalculateLayout(preferredWidth, preferredHeight, minHDistance, minVDistance);
    320312      var visualNodes = visualTreeNodes.Values;
    321313      //draw nodes and connections
     
    420412
    421413    private void reingoldTilfordToolStripMenuItem_Click(object sender, EventArgs e) {
    422       TreeLayoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> {
     414      layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees) {
    423415        NodeWidth = preferredNodeWidth,
    424416        NodeHeight = preferredNodeHeight,
     
    426418        VerticalSpacing = minVerticalDistance
    427419      };
     420      reingoldTilfordToolStripMenuItem.Checked = true;
     421      boxesToolStripMenuItem.Checked = false;
     422      Repaint();
    428423    }
    429424
    430425    private void boxesToolStripMenuItem_Click(object sender, EventArgs e) {
    431       TreeLayoutEngine = new BoxesLayoutEngine<ISymbolicExpressionTreeNode> {
     426      layoutEngine = new BoxesLayoutEngine<ISymbolicExpressionTreeNode>(n => n.Subtrees, n => n.GetLength(), n => n.GetDepth()) {
    432427        NodeWidth = preferredNodeWidth,
    433428        NodeHeight = preferredNodeHeight,
     
    435430        VerticalSpacing = minVerticalDistance
    436431      };
     432      reingoldTilfordToolStripMenuItem.Checked = false;
     433      boxesToolStripMenuItem.Checked = true;
     434      Repaint();
    437435    }
    438436  }
Note: See TracChangeset for help on using the changeset viewer.