Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Common/Plotter.xaml.cs @ 13847

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

#2283 added GUI and charts; fixed MCTS

File size: 5.0 KB
Line 
1using System;
2using System.IO;
3using System.Linq;
4using System.Windows;
5using System.Windows.Controls;
6using System.Windows.Media;
7using System.Windows.Media.Imaging;
8using Microsoft.Research.DynamicDataDisplay.Common;
9using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
10using System.Windows.Markup;
11using System.ComponentModel;
12
13namespace Microsoft.Research.DynamicDataDisplay
14{
15  /// <summary>Plotter is a base control for displaying various graphs. It provides
16  /// mainCanvas to draw graph itself and side space for axes, annotations, etc</summary>
17  [ContentProperty("Children")]
18  public partial class Plotter : ContentControl
19  {
20    static Plotter() {
21      //DefaultStyleKeyProperty.OverrideMetadata(typeof(Plotter), new FrameworkPropertyMetadata(typeof(Plotter)));
22    }
23
24    /// <summary>
25    /// Initializes a new instance of the <see cref="Plotter"/> class.
26    /// </summary>
27    public Plotter()
28    {
29      children.ChildAdded += OnChildAdded;
30      children.ChildRemoving += OnChildRemoving;
31
32      InitializeComponent();
33    }
34
35    public override void OnApplyTemplate()
36    {
37      base.OnApplyTemplate();
38
39      //headerPanel = GetTemplateChild("headerPanel") as StackPanel;
40    }
41
42    #region Children and add/removed events handling
43
44    private readonly ObservableChartsCollection children = new ObservableChartsCollection();
45
46    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
47    public ObservableChartsCollection Children
48    {
49      get { return children; }
50    }
51
52    private void OnChildRemoving(object sender, PlotterCollectionChangedEventArgs e)
53    {
54      OnChildRemoving(e.Child);
55    }
56
57    protected virtual void OnChildRemoving(IPlotterElement uiElement)
58    {
59      IPlotterElement child = uiElement as IPlotterElement;
60      if (child != null)
61      {
62        child.OnPlotterDetaching(this);
63      }
64    }
65
66    private void OnChildAdded(object sender, PlotterCollectionChangedEventArgs e)
67    {
68      OnChildAdded(e.Child);
69    }
70
71    protected virtual void OnChildAdded(IPlotterElement uiElement)
72    {
73      IPlotterElement child = uiElement as IPlotterElement;
74      if (child != null)
75      {
76        child.OnPlotterAttached(this);
77      }
78    }
79
80    #endregion
81
82    #region Layout zones
83
84    public StackPanel HeaderPanel
85    {
86      get { return headerPanel; }
87    }
88
89    public StackPanel FooterPanel
90    {
91      get { return footerPanel; }
92    }
93
94    public StackPanel LeftPanel
95    {
96      get { return leftPanel; }
97    }
98
99    public StackPanel BottomPanel
100    {
101      get { return bottomPanel; }
102    }
103
104    public Canvas MainCanvas
105    {
106      get { return mainCanvas; }
107    }
108
109    public Grid CentralGrid
110    {
111      get { return centralGrid; }
112    }
113
114    #endregion
115
116    #region Screenshots & copy to clipboard
117
118    public BitmapSource CreateScreenshot()
119    {
120      Window window = Window.GetWindow(this);
121
122      Rect renderBounds = new Rect(RenderSize);
123
124      Point p1 = TranslatePoint(renderBounds.Location, window);
125      Point p2 = TranslatePoint(renderBounds.BottomRight, window);
126
127      Int32Rect rect = new Rect(p1, p2).ToInt32Rect();
128      rect.Y = 0;
129
130      return ScreenshotHelper.CreateScreenshot(this, rect);
131    }
132
133
134    /// <summary>Saves the screenshot.</summary>
135    /// <param name="filePath">The file path.</param>
136    public void SaveScreenshot(string filePath)
137    {
138      ScreenshotHelper.SaveBitmap(CreateScreenshot(), filePath);
139    }
140
141    /// <summary>Copies the screenshot to clipboard.</summary>
142    public void CopyScreenshotToClipboard()
143    {
144      Clipboard.Clear();
145      Clipboard.SetImage(CreateScreenshot());
146    }
147
148    #endregion
149
150    #region IsDefaultElement attached property
151
152    protected void SetAllChildrenAsDefault()
153    {
154      foreach (var child in Children.OfType<DependencyObject>())
155      {
156        child.SetValue(IsDefaultElementProperty, true);
157      }
158    }
159
160    /// <summary>Gets a value whether specified graphics object is default to this plotter or not</summary>
161    /// <param name="obj">Graphics object to check</param>
162    /// <returns>True if it is default or false otherwise</returns>
163    public static bool GetIsDefaultElement(DependencyObject obj)
164    {
165      return (bool)obj.GetValue(IsDefaultElementProperty);
166    }
167
168    public static void SetIsDefaultElement(DependencyObject obj, bool value)
169    {
170      obj.SetValue(IsDefaultElementProperty, value);
171    }
172
173    public static readonly DependencyProperty IsDefaultElementProperty = DependencyProperty.RegisterAttached(
174      "IsDefaultElement",
175      typeof(bool),
176      typeof(Plotter),
177      new UIPropertyMetadata(false));
178
179    /// <summary>Removes all user graphs from given UIElementCollection,
180    /// leaving only default graphs</summary>
181    protected static void RemoveUserElements(ObservableChartsCollection elements)
182    {
183      int index = 0;
184
185      while (index < elements.Count)
186      {
187        DependencyObject d = elements[index] as DependencyObject;
188        if (d != null && !GetIsDefaultElement(d))
189        {
190          elements.RemoveAt(index);
191        }
192        else
193        {
194          index++;
195        }
196      }
197    }
198
199    public void RemoveUserElements()
200    {
201      RemoveUserElements(Children);
202    }
203
204    #endregion
205  }
206}
Note: See TracBrowser for help on using the repository browser.