Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/21/14 18:20:47 (11 years ago)
Author:
bburlacu
Message:

#1772: Merged trunk changes and added missing frame files (for HeuristicLab.EvolutionTracking and HeuristicLab.EvolutionTracking.Views.

Location:
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/GraphicalSymbolicExpressionTreeView.Designer.cs

    r9456 r10501  
    1919 */
    2020#endregion
     21
     22using System.Drawing;
    2123
    2224namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
     
    5961      this.symbolicExpressionTreeChart.Spacing = 5;
    6062      this.symbolicExpressionTreeChart.TabIndex = 0;
    61       this.symbolicExpressionTreeChart.TextFont = new System.Drawing.Font("Times New Roman", 6F);
     63      this.symbolicExpressionTreeChart.TextFont = new System.Drawing.Font(FontFamily.GenericSerif, 8F);
    6264      //
    6365      // FunctionTreeView
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/GraphicalSymbolicExpressionTreeView.cs

    r10347 r10501  
    2020#endregion
    2121
    22 using System.Drawing;
    2322using System.Windows.Forms;
    2423using HeuristicLab.Core.Views;
     
    5352      symbolicExpressionTreeChart.Enabled = Content != null;
    5453    }
    55 
    56     public void HighlightNode(ISymbolicExpressionTreeNode node, Color color) {
    57       symbolicExpressionTreeChart.HighlightNode(node, color);
    58     }
    59 
    60     public void HighlightSubtree(ISymbolicExpressionTreeNode subtree, Color color) {
    61       symbolicExpressionTreeChart.HighlightSubtree(subtree, color);
    62     }
    6354  }
    6455}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeChart.Designer.cs

    r10302 r10501  
    5050      this.saveImageToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
    5151      this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
    52       this.exportLatexToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     52      this.exportPgfLaTeXToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
    5353      this.contextMenuStrip.SuspendLayout();
    5454      this.SuspendLayout();
     
    5858      this.contextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
    5959            this.saveImageToolStripMenuItem,
    60             this.exportLatexToolStripMenuItem});
     60            this.exportPgfLaTeXToolStripMenuItem});
    6161      this.contextMenuStrip.Name = "contextMenuStrip";
    62       this.contextMenuStrip.Size = new System.Drawing.Size(161, 70);
     62      this.contextMenuStrip.Size = new System.Drawing.Size(166, 70);
    6363      //
    6464      // saveImageToolStripMenuItem
    6565      //
    6666      this.saveImageToolStripMenuItem.Name = "saveImageToolStripMenuItem";
    67       this.saveImageToolStripMenuItem.Size = new System.Drawing.Size(160, 22);
     67      this.saveImageToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
    6868      this.saveImageToolStripMenuItem.Text = "Save Image";
    6969      this.saveImageToolStripMenuItem.Click += new System.EventHandler(this.saveImageToolStripMenuItem_Click);
     
    7373      this.saveFileDialog.Filter = "Bitmap (*.bmp)|*.bmp|EMF (*.emf)|*.emf";
    7474      //
    75       // exportLatexToolStripMenuItem
     75      // exportPgfLaTeXToolStripMenuItem
    7676      //
    77       this.exportLatexToolStripMenuItem.Name = "exportLatexToolStripMenuItem";
    78       this.exportLatexToolStripMenuItem.Size = new System.Drawing.Size(160, 22);
    79       this.exportLatexToolStripMenuItem.Text = "Export Pgf/Latex";
    80       this.exportLatexToolStripMenuItem.Click += new System.EventHandler(this.exportLatexToolStripMenuItem_Click);
     77      this.exportPgfLaTeXToolStripMenuItem.Name = "exportPgfLaTeXToolStripMenuItem";
     78      this.exportPgfLaTeXToolStripMenuItem.Size = new System.Drawing.Size(165, 22);
     79      this.exportPgfLaTeXToolStripMenuItem.Text = "Export Pgf/LaTeX";
     80      this.exportPgfLaTeXToolStripMenuItem.Click += new System.EventHandler(this.exportLatexToolStripMenuItem_Click);
    8181      //
    8282      // SymbolicExpressionTreeChart
     
    102102    protected System.Windows.Forms.ToolStripMenuItem saveImageToolStripMenuItem;
    103103    protected System.Windows.Forms.SaveFileDialog saveFileDialog;
    104     private System.Windows.Forms.ToolStripMenuItem exportLatexToolStripMenuItem;
     104    private System.Windows.Forms.ToolStripMenuItem exportPgfLaTeXToolStripMenuItem;
    105105  }
    106106}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeChart.cs

    r10456 r10501  
    3232  public partial class SymbolicExpressionTreeChart : UserControl {
    3333    private Image image;
    34     private StringFormat stringFormat;
     34    private readonly StringFormat stringFormat;
    3535    private Dictionary<ISymbolicExpressionTreeNode, VisualSymbolicExpressionTreeNode> visualTreeNodes;
    3636    private Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualSymbolicExpressionTreeNodeConnection> visualLines;
     
    5151      this.lineColor = Color.Black;
    5252      this.backgroundColor = Color.White;
    53       this.textFont = new Font(FontFamily.GenericSerif, 14, GraphicsUnit.Pixel);
     53      this.textFont = new Font(FontFamily.GenericSansSerif, 12);
    5454      layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>();
    5555      layoutAdapter = new SymbolicExpressionTreeLayoutAdapter();
     
    108108        visualLines = new Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualSymbolicExpressionTreeNodeConnection>();
    109109        if (tree != null) {
    110           foreach (ISymbolicExpressionTreeNode node in tree.IterateNodesPrefix()) {
     110          IEnumerable<ISymbolicExpressionTreeNode> nodes;
     111          if (tree.Root.SubtreeCount == 1) nodes = tree.Root.GetSubtree(0).IterateNodesPrefix();
     112          else nodes = tree.Root.IterateNodesPrefix();
     113          foreach (ISymbolicExpressionTreeNode node in nodes) {
    111114            visualTreeNodes[node] = new VisualSymbolicExpressionTreeNode(node);
    112115            if (node.Parent != null) visualLines[Tuple.Create(node.Parent, node)] = new VisualSymbolicExpressionTreeNodeConnection();
     
    166169        this.Refresh();
    167170      }
    168     }
    169 
    170     public void HighlightNode(ISymbolicExpressionTreeNode node, Color color) {
    171       var visualNode = GetVisualSymbolicExpressionTreeNode(node);
    172       if (visualNode == null) return;
    173       visualNode.LineColor = color;
    174       RepaintNode(visualNode);
    175     }
    176 
    177     public void HighlightSubtree(ISymbolicExpressionTreeNode subtree, Color color) {
    178       foreach (var node in subtree.IterateNodesBreadth())
    179         HighlightNode(node, color);
    180171    }
    181172
     
    280271    private void DrawFunctionTree(ISymbolicExpressionTree symbolicExpressionTree, Graphics graphics, int preferredWidth, int preferredHeight, int minHDistance, int minVDistance) {
    281272      var layoutNodes = layoutAdapter.Convert(symbolicExpressionTree).ToList();
     273      if (symbolicExpressionTree.Root.SubtreeCount == 1) layoutNodes.RemoveAt(0);
    282274      layoutEngine.Reset();
    283275      layoutEngine.Root = layoutNodes[0];
    284       foreach (var ln in layoutNodes)
    285         layoutEngine.AddNode(ln.Content, ln);
    286       layoutEngine.MinHorizontalSpacing = (preferredNodeWidth + minHDistance);
    287       layoutEngine.MinVerticalSpacing = (preferredNodeHeight + minVDistance);
     276      layoutEngine.AddNodes(layoutNodes);
     277      layoutEngine.MinHorizontalSpacing = (preferredWidth + minHDistance);
     278      layoutEngine.MinVerticalSpacing = (preferredHeight + minVDistance);
    288279      layoutEngine.CalculateLayout();
    289280      var bounds = layoutEngine.Bounds();
    290       double sx = this.Width / (bounds.Width + preferredWidth);
    291       if (sx > 1) sx = 1;
    292       double sy = this.Height / bounds.Height;
    293       if (sy > 1) sy = 1;
    294       double dx = (this.Width - bounds.Width) / 2;
    295       if (dx < 0) dx = 0;
    296       double dy = (this.Height - bounds.Height) / 2;
    297       if (dy < 0) dy = 0;
    298 
    299       var levels = layoutNodes.GroupBy(n => n.Level, n => n);
    300 
    301       foreach (var level in levels) {
    302         var nodes = level.ToList();
    303         double min = 0;
    304         for (int i = 0; i < nodes.Count - 1; ++i) {
    305           var w = (nodes[i + 1].X - nodes[i].X) * sx - preferredWidth;
    306           if (w < min) min = w;
    307         }
    308         if (min > 0) min = 0;
    309 
    310         foreach (var layoutNode in level) {
     281
     282      double verticalScalingFactor = 1.0;
     283      double layoutHeight = (bounds.Height + preferredHeight);
     284      if (this.Height < layoutHeight)
     285        verticalScalingFactor = this.Height / layoutHeight;
     286
     287      double horizontalScalingFactor = 1.0;
     288      double layoutWidth = (bounds.Width + preferredWidth);
     289      if (this.Width < layoutWidth)
     290        horizontalScalingFactor = this.Width / layoutWidth;
     291
     292      double horizontalOffset;
     293      if (this.Width > layoutWidth)
     294        horizontalOffset = (this.Width - layoutWidth) / 2.0;
     295      else
     296        horizontalOffset = preferredWidth / 2.0;
     297
     298      var levels = layoutNodes.GroupBy(n => n.Level, n => n).ToList();
     299      for (int i = levels.Count - 1; i >= 0; --i) {
     300        var nodes = levels[i].ToList();
     301
     302        double minSpacing = double.MaxValue;
     303        if (nodes.Count > 1) {
     304          for (int j = 1; j < nodes.Count() - 1; ++j) {
     305            var distance = nodes[j].X - nodes[j - 1].X; // guaranteed to be > 0
     306            if (minSpacing > distance) minSpacing = distance;
     307          }
     308        }
     309        minSpacing *= horizontalScalingFactor;
     310
     311        int minWidth = (int)Math.Round(preferredWidth * horizontalScalingFactor);
     312        int width = (int)Math.Min(minSpacing, preferredWidth) - 2; // leave some pixels so that node margins don't overlap
     313
     314        foreach (var layoutNode in nodes) {
    311315          var visualNode = visualTreeNodes[layoutNode.Content];
    312           visualNode.Width = (int)Math.Round(preferredWidth + min) - 2; // -2 to allow enough padding (1px on each side) for the node contour to be drawn
    313           visualNode.Height = (int)Math.Round(preferredHeight * sy);
    314           visualNode.X = (int)(Math.Round(layoutNode.X * sx + dx));
    315           visualNode.Y = (int)(Math.Round(layoutNode.Y * sy + dy));
     316          visualNode.Width = width;
     317          visualNode.Height = (int)Math.Round(preferredHeight * verticalScalingFactor);
     318          visualNode.X = (int)Math.Round((layoutNode.X + horizontalOffset) * horizontalScalingFactor + (minWidth - width) / 2.0);
     319          visualNode.Y = (int)Math.Round(layoutNode.Y * verticalScalingFactor);
    316320          DrawTreeNode(graphics, visualNode);
    317321        }
    318322      }
    319 
    320       graphics.ResetClip(); // reset clip region
    321323      // draw node connections
    322324      foreach (var visualNode in visualTreeNodes.Values) {
     
    327329          var origin = new Point(visualNode.X + visualNode.Width / 2, visualNode.Y + visualNode.Height);
    328330          var target = new Point(visualSubtree.X + visualSubtree.Width / 2, visualSubtree.Y);
     331          graphics.Clip = new Region(new Rectangle(Math.Min(origin.X, target.X), origin.Y, Math.Max(origin.X, target.X), target.Y));
    329332          using (var linePen = new Pen(visualLine.LineColor)) {
    330333            linePen.DashStyle = visualLine.DashStyle;
     
    378381      Image image = new Bitmap(Width, Height);
    379382      using (Graphics g = Graphics.FromImage(image)) {
    380         int height = this.Height / tree.Depth;
    381         DrawFunctionTree(tree, g, 0, 0, Width, height);
     383        DrawFunctionTree(tree, g, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance);
    382384      }
    383385      image.Save(filename);
     
    386388    public void SaveImageAsEmf(string filename) {
    387389      if (tree == null) return;
    388       using (var graphics = Graphics.FromImage(image)) {
    389         var rectangle = new Rectangle(0, 0, image.Width, image.Height);
    390         using (var metafile = new Metafile(filename, graphics.GetHdc(), rectangle, MetafileFrameUnit.Pixel, EmfType.EmfPlusDual)) {
    391           graphics.ReleaseHdc();
    392           using (var g = Graphics.FromImage(metafile)) {
    393             DrawFunctionTree(tree, g, 0, 0, image.Width, image.Height);
     390      using (Graphics g = CreateGraphics()) {
     391        using (Metafile file = new Metafile(filename, g.GetHdc())) {
     392          using (Graphics emfFile = Graphics.FromImage(file)) {
     393            DrawFunctionTree(tree, emfFile, preferredNodeWidth, preferredNodeHeight, minHorizontalDistance, minVerticalDistance);
    394394          }
    395395        }
     396        g.ReleaseHdc();
    396397      }
    397398    }
Note: See TracChangeset for help on using the changeset viewer.