Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Charts/Shapes/PolylineEditor.cs @ 13398

Last change on this file since 13398 was 12503, checked in by aballeit, 10 years ago

#2283 added GUI and charts; fixed MCTS

File size: 3.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows.Markup;
6using System.Windows.Data;
7using System.Windows;
8using System.Windows.Threading;
9using System.ComponentModel;
10
11namespace Microsoft.Research.DynamicDataDisplay.Charts.Shapes
12{
13  /// <summary>
14  /// Represents an editor of points' position of ViewportPolyline or ViewportPolygon.
15  /// </summary>
16  [ContentProperty("Polyline")]
17  public class PolylineEditor : IPlotterElement
18  {
19    /// <summary>
20    /// Initializes a new instance of the <see cref="PolylineEditor"/> class.
21    /// </summary>
22    public PolylineEditor() { }
23
24    private ViewportPolylineBase polyline;
25    /// <summary>
26    /// Gets or sets the polyline, to edit points of which.
27    /// </summary>
28    /// <value>The polyline.</value>
29    [NotNull]
30    public ViewportPolylineBase Polyline
31    {
32      get { return polyline; }
33      set
34      {
35        if (value == null)
36          throw new ArgumentNullException("Polyline");
37
38        if (polyline != value)
39        {
40          polyline = value;
41          var descr = DependencyPropertyDescriptor.FromProperty(ViewportPolylineBase.PointsProperty, typeof(ViewportPolylineBase));
42          descr.AddValueChanged(polyline, OnPointsReplaced);
43
44          if (plotter != null)
45          {
46            AddLineToPlotter(false);
47          }
48        }
49      }
50    }
51
52    bool pointsAdded = false;
53    private void OnPointsReplaced(object sender, EventArgs e)
54    {
55      if (plotter == null)
56        return;
57      if (pointsAdded)
58        return;
59
60      ViewportPolylineBase line = (ViewportPolylineBase)sender;
61
62      pointsAdded = true;
63      List<IPlotterElement> draggablePoints = new List<IPlotterElement>();
64      GetDraggablePoints(draggablePoints);
65
66      foreach (var point in draggablePoints)
67      {
68        plotter.Children.Add(point);
69      }
70    }
71
72    private void AddLineToPlotter(bool async)
73    {
74      if (!async)
75      {
76        foreach (var item in GetAllElementsToAdd())
77        {
78          plotter.Children.Add(item);
79        }
80      }
81      else
82      {
83        plotter.Dispatcher.BeginInvoke(((Action)(() => { AddLineToPlotter(false); })), DispatcherPriority.Send);
84      }
85    }
86
87    private List<IPlotterElement> GetAllElementsToAdd()
88    {
89      var result = new List<IPlotterElement>(1 + polyline.Points.Count);
90      result.Add(polyline);
91
92      GetDraggablePoints(result);
93
94      return result;
95    }
96
97    private void GetDraggablePoints(List<IPlotterElement> collection)
98    {
99      for (int i = 0; i < polyline.Points.Count; i++)
100      {
101        DraggablePoint point = new DraggablePoint();
102        point.SetBinding(DraggablePoint.PositionProperty, new Binding
103        {
104          Source = polyline,
105          Path = new PropertyPath("Points[" + i + "]"),
106          Mode = BindingMode.TwoWay
107        });
108        collection.Add(point);
109      }
110    }
111
112    #region IPlotterElement Members
113
114    void IPlotterElement.OnPlotterAttached(Plotter plotter)
115    {
116      this.plotter = (Plotter2D)plotter;
117
118      if (polyline != null)
119      {
120        AddLineToPlotter(true);
121      }
122    }
123
124    void IPlotterElement.OnPlotterDetaching(Plotter plotter)
125    {
126      this.plotter = null;
127    }
128
129    private Plotter2D plotter;
130    /// <summary>
131    /// Gets the parent plotter of chart.
132    /// Should be equal to null if item is not connected to any plotter.
133    /// </summary>
134    /// <value>The plotter.</value>
135    public Plotter Plotter
136    {
137      get { return plotter; }
138    }
139
140    #endregion
141  }
142}
Note: See TracBrowser for help on using the repository browser.