Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/06/14 15:07:05 (11 years ago)
Author:
bburlacu
Message:

#2076: Updated the way the layout is used in the SymbolicExpressionTreeChart; updated simplifier view accordingly.

File:
1 edited

Legend:

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

    r10531 r10561  
    5858    }
    5959
     60    private ILayoutEngine<ISymbolicExpressionTreeNode> TreeLayoutEngine {
     61      get { return layoutEngine; }
     62      set {
     63        layoutEngine = value;
     64        InitializeLayout();
     65        Repaint();
     66      }
     67    }
     68
    6069    public SymbolicExpressionTreeChart(ISymbolicExpressionTree tree)
    6170      : this() {
     
    128137      if (this.Width <= 1 || this.Height <= 1)
    129138        this.image = new Bitmap(1, 1);
    130       else
     139      else {
    131140        this.image = new Bitmap(Width, Height);
     141      }
    132142      this.Repaint();
     143    }
     144
     145    public event EventHandler Repainted;//expose this event to notify the parent control that the tree was repainted
     146    protected virtual void OnRepaint(object sender, EventArgs e) {
     147      var repainted = Repainted;
     148      if (repainted != null) {
     149        repainted(sender, e);
     150      }
    133151    }
    134152
     
    137155        this.GenerateImage();
    138156        this.Refresh();
     157        OnRepaint(this, EventArgs.Empty);
    139158      }
    140159    }
     
    147166          foreach (var visualNode in visualTreeNodes.Values) {
    148167            DrawTreeNode(graphics, visualNode);
     168            if (visualNode.Content.SubtreeCount > 0) {
     169              foreach (var visualSubtree in visualNode.Content.Subtrees.Select(s => visualTreeNodes[s])) {
     170                DrawLine(graphics, visualNode, visualSubtree);
     171              }
     172            }
    149173          }
    150174        }
     
    170194        graphics.Clear(backgroundColor);
    171195        if (tree != null) {
    172           DrawFunctionTree(tree, graphics, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance);
     196          DrawFunctionTree(graphics, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance);
    173197        }
    174198      }
     
    236260
    237261    private void SymbolicExpressionTreeChart_MouseMove(object sender, MouseEventArgs e) {
    238 
    239262      VisualTreeNode<ISymbolicExpressionTreeNode> visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
    240263      if (draggedSymbolicExpressionTree != null &&
     
    262285    #endregion
    263286
     287    #region initialize the layout
    264288    private void InitializeLayout() {
    265       var actualRoot = tree.Root.SubtreeCount == 1 ? tree.Root.GetSubtree(0) : tree.Root;
     289      var actualRoot = tree.Root;
     290      if (actualRoot.Symbol is ProgramRootSymbol && actualRoot.SubtreeCount == 1) {
     291        actualRoot = tree.Root.GetSubtree(0);
     292      }
    266293      layoutEngine.Initialize(actualRoot, n => n.Subtrees, n => n.GetLength(), n => n.GetDepth());
    267294      layoutEngine.CalculateLayout(this.Width, this.Height);
     295      UpdateDictionaries();
     296    }
     297
     298    private void UpdateDictionaries() {
    268299      var visualNodes = layoutEngine.GetVisualNodes().ToList();
    269300      //populate the visual nodes and visual connections dictionaries
     
    276307      }
    277308    }
    278 
     309    #endregion
    279310    #region methods for painting the symbolic expression tree
    280     private void DrawFunctionTree(ISymbolicExpressionTree symbExprTree, Graphics graphics, int preferredWidth, int preferredHeight, int minHDistance, int minVDistance) {
     311    private void DrawFunctionTree(Graphics graphics, int preferredWidth, int preferredHeight, int minHDistance, int minVDistance) {
    281312      //we assume here that the layout has already been initialized when the symbolic expression tree was changed
    282313      //recalculate layout according to new node widths and spacing
     
    286317      layoutEngine.VerticalSpacing = minVDistance;
    287318      layoutEngine.CalculateLayout(Width, Height);
     319      UpdateDictionaries();//this will reset the visual nodes, therefore colors will be reset to default values
    288320      var visualNodes = visualTreeNodes.Values;
    289321      //draw nodes and connections
     
    333365      }
    334366    }
     367
     368    protected void DrawLine(Graphics graphics, VisualTreeNode<ISymbolicExpressionTreeNode> startNode, VisualTreeNode<ISymbolicExpressionTreeNode> endNode) {
     369      var origin = new Point(startNode.X + startNode.Width / 2, startNode.Y + startNode.Height);
     370      var target = new Point(endNode.X + endNode.Width / 2, endNode.Y);
     371      graphics.Clip = new Region(new Rectangle(Math.Min(origin.X, target.X), origin.Y, Math.Max(origin.X, target.X), target.Y));
     372      var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(startNode.Content, endNode.Content);
     373      using (var linePen = new Pen(visualLine.LineColor)) {
     374        linePen.DashStyle = visualLine.DashStyle;
     375        graphics.DrawLine(linePen, origin, target);
     376      }
     377    }
    335378    #endregion
    336379    #region save image
     
    348391      Image image = new Bitmap(Width, Height);
    349392      using (Graphics g = Graphics.FromImage(image)) {
    350         DrawFunctionTree(tree, g, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance);
     393        DrawFunctionTree(g, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance);
    351394      }
    352395      image.Save(filename);
     
    358401        using (Metafile file = new Metafile(filename, g.GetHdc())) {
    359402          using (Graphics emfFile = Graphics.FromImage(file)) {
    360             DrawFunctionTree(tree, emfFile, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance);
     403            DrawFunctionTree(emfFile, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance);
    361404          }
    362405        }
     
    377420
    378421    private void reingoldTilfordToolStripMenuItem_Click(object sender, EventArgs e) {
    379       layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> {
     422      TreeLayoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> {
    380423        NodeWidth = preferredNodeWidth,
    381424        NodeHeight = preferredNodeHeight,
     
    383426        VerticalSpacing = minVerticalDistance
    384427      };
    385       InitializeLayout();
    386       Repaint();
    387428    }
    388429
    389430    private void boxesToolStripMenuItem_Click(object sender, EventArgs e) {
    390       layoutEngine = new BoxesLayoutEngine<ISymbolicExpressionTreeNode> {
     431      TreeLayoutEngine = new BoxesLayoutEngine<ISymbolicExpressionTreeNode> {
    391432        NodeWidth = preferredNodeWidth,
    392433        NodeHeight = preferredNodeHeight,
     
    394435        VerticalSpacing = minVerticalDistance
    395436      };
    396       InitializeLayout();
    397       Repaint();
    398437    }
    399438  }
Note: See TracChangeset for help on using the changeset viewer.