Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Charts/Legend items/Legend.cs @ 13777

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

#2283 added GUI and charts; fixed MCTS

File size: 8.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows.Controls;
6using System.Windows;
7using System.Windows.Data;
8using System.Windows.Shapes;
9using System.Windows.Media.Effects;
10using System.Collections.Specialized;
11using System.Collections.ObjectModel;
12using System.Diagnostics;
13using System.Collections;
14using System.Windows.Media;
15using Microsoft.Research.DynamicDataDisplay.Charts.Legend_items;
16
17namespace Microsoft.Research.DynamicDataDisplay.Charts
18{
19  public delegate IEnumerable<FrameworkElement> LegendItemsBuilder(IPlotterElement element);
20
21  /// <summary>
22  /// Represents a legend for a chart plotter.
23  /// </summary>
24  public class Legend : ItemsControl, IPlotterElement
25  {
26    static Legend()
27    {
28      Type thisType = typeof(Legend);
29      DefaultStyleKeyProperty.OverrideMetadata(thisType, new FrameworkPropertyMetadata(thisType));
30
31      Plotter.PlotterProperty.OverrideMetadata(thisType, new FrameworkPropertyMetadata(OnPlotterChanged));
32    }
33
34    private readonly ObservableCollection<FrameworkElement> legendItems = new ObservableCollection<FrameworkElement>();
35
36    public Legend()
37    {
38      ItemsSource = legendItems;
39    }
40
41    #region IPlotterElement Members
42
43    private static void OnPlotterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
44    {
45      Legend legend = (Legend)d;
46      if (e.OldValue != null)
47      {
48        legend.DetachFromPlotter((Plotter)e.OldValue);
49      }
50      if (e.NewValue != null)
51      {
52        legend.AttachToPlotter((Plotter)e.NewValue);
53      }
54    }
55
56    private Plotter plotter;
57    public void OnPlotterAttached(Plotter plotter)
58    {
59      plotter.CentralGrid.Children.Add(this);
60
61      AttachToPlotter(plotter);
62    }
63
64    private void AttachToPlotter(Plotter plotter)
65    {
66      if (plotter != this.plotter)
67      {
68        this.plotter = plotter;
69        plotter.Children.CollectionChanged += PlotterChildren_CollectionChanged;
70        PopulateLegend();
71      }
72    }
73
74    public void OnPlotterDetaching(Plotter plotter)
75    {
76      plotter.CentralGrid.Children.Remove(this);
77
78      DetachFromPlotter(plotter);
79    }
80
81    private void DetachFromPlotter(Plotter plotter)
82    {
83      if (plotter != null)
84      {
85        plotter.Children.CollectionChanged -= PlotterChildren_CollectionChanged;
86        this.plotter = null;
87        CleanLegend();
88      }
89    }
90
91    public Plotter Plotter
92    {
93      get { return plotter; }
94    }
95
96    #endregion
97
98    private void PlotterChildren_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
99    {
100      PopulateLegend();
101    }
102
103    public void PopulateLegend()
104    {
105      legendItems.Clear();
106
107      if (!LegendVisible) return;
108      if (plotter == null) return;
109
110      foreach (var chart in plotter.Children.OfType<DependencyObject>())
111      {
112        var legendItemsBuilder = Legend.GetLegendItemsBuilder(chart);
113        if (legendItemsBuilder != null)
114        {
115          foreach (var legendItem in legendItemsBuilder((IPlotterElement)chart))
116          {
117            legendItems.Add(legendItem);
118          }
119        }
120      }
121
122      UpdateVisibility();
123    }
124
125    private void UpdateVisibility()
126    {
127      if (legendItems.Count > 0)
128        Visibility = Visibility.Visible;
129      else
130        Visibility = Visibility.Hidden;
131    }
132
133    private void CleanLegend()
134    {
135      foreach (var legendItem in legendItems)
136      {
137        BindingOperations.ClearAllBindings(legendItem);
138      }
139      legendItems.Clear();
140
141      UpdateVisibility();
142    }
143
144    #region Attached Properties
145
146    #region Description
147
148    public static object GetDescription(DependencyObject obj)
149    {
150      return obj.GetValue(DescriptionProperty);
151    }
152
153    public static void SetDescription(DependencyObject obj, object value)
154    {
155      obj.SetValue(DescriptionProperty, value);
156    }
157
158    public static readonly DependencyProperty DescriptionProperty = DependencyProperty.RegisterAttached(
159      "Description",
160      typeof(object),
161      typeof(Legend),
162      new FrameworkPropertyMetadata(null));
163
164    #endregion // end of Description
165
166    #region Detailed description
167
168    public static object GetDetailedDescription(DependencyObject obj)
169    {
170      return (object)obj.GetValue(DetailedDescriptionProperty);
171    }
172
173    public static void SetDetailedDescription(DependencyObject obj, object value)
174    {
175      obj.SetValue(DetailedDescriptionProperty, value);
176    }
177
178    public static readonly DependencyProperty DetailedDescriptionProperty = DependencyProperty.RegisterAttached(
179      "DetailedDescription",
180      typeof(object),
181      typeof(Legend),
182      new FrameworkPropertyMetadata(null));
183
184    #endregion // end of Detailed description
185
186    #region VisualContent
187
188    public static object GetVisualContent(DependencyObject obj)
189    {
190      return (object)obj.GetValue(VisualContentProperty);
191    }
192
193    public static void SetVisualContent(DependencyObject obj, object value)
194    {
195      obj.SetValue(VisualContentProperty, value);
196    }
197
198    public static readonly DependencyProperty VisualContentProperty = DependencyProperty.RegisterAttached(
199      "VisualContent",
200      typeof(object),
201      typeof(Legend),
202      new FrameworkPropertyMetadata(null));
203
204    #endregion // end of VisualContent
205
206    #region SampleData
207
208    public static object GetSampleData(DependencyObject obj)
209    {
210      return (object)obj.GetValue(SampleDataProperty);
211    }
212
213    public static void SetSampleData(DependencyObject obj, object value)
214    {
215      obj.SetValue(SampleDataProperty, value);
216    }
217
218    public static readonly DependencyProperty SampleDataProperty = DependencyProperty.RegisterAttached(
219      "SampleData",
220      typeof(object),
221      typeof(Legend),
222      new FrameworkPropertyMetadata(null));
223
224    #endregion // end of SampleData
225
226    #region ShowInLegend
227
228    public static bool GetShowInLegend(DependencyObject obj)
229    {
230      return (bool)obj.GetValue(ShowInLegendProperty);
231    }
232
233    public static void SetShowInLegend(DependencyObject obj, bool value)
234    {
235      obj.SetValue(ShowInLegendProperty, value);
236    }
237
238    public static readonly DependencyProperty ShowInLegendProperty = DependencyProperty.RegisterAttached(
239      "ShowInLegend",
240      typeof(bool),
241      typeof(Legend),
242      new FrameworkPropertyMetadata(true, OnShowInLegendChanged));
243
244    private static void OnShowInLegendChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
245    {
246      Legend legend = (Legend)d;
247      legend.PopulateLegend();
248    }
249
250    #endregion // end of ShowInLegend
251
252    #region LegendItemsBuilder
253
254    public static LegendItemsBuilder GetLegendItemsBuilder(DependencyObject obj)
255    {
256      return (LegendItemsBuilder)obj.GetValue(LegendItemsBuilderProperty);
257    }
258
259    public static void SetLegendItemsBuilder(DependencyObject obj, LegendItemsBuilder value)
260    {
261      obj.SetValue(LegendItemsBuilderProperty, value);
262    }
263
264    public static readonly DependencyProperty LegendItemsBuilderProperty = DependencyProperty.RegisterAttached(
265      "LegendItemsBuilder",
266      typeof(LegendItemsBuilder),
267      typeof(Legend),
268      new FrameworkPropertyMetadata(null, OnLegendItemsBuilderChanged));
269
270    private static void OnLegendItemsBuilderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
271    {
272      IPlotterElement plotterElement = d as IPlotterElement;
273      if (plotterElement != null && plotterElement.Plotter != null)
274      {
275        ChartPlotter plotter = plotterElement.Plotter as ChartPlotter;
276        if (plotter != null)
277        {
278          plotter.Legend.PopulateLegend();
279        }
280      }
281    }
282
283    #endregion // end of LegendItemsBuilder
284
285    #endregion // end of Attached Properties
286
287    #region Properties
288
289    public bool LegendVisible
290    {
291      get { return (bool)GetValue(LegendVisibleProperty); }
292      set { SetValue(LegendVisibleProperty, value); }
293    }
294
295    public static readonly DependencyProperty LegendVisibleProperty = DependencyProperty.Register(
296      "LegendVisible",
297      typeof(bool),
298      typeof(Legend),
299      new FrameworkPropertyMetadata(true, OnLegendVisibleChanged));
300
301    private static void OnLegendVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
302    {
303      Legend owner = (Legend)d;
304
305      var visible = (bool)e.NewValue;
306      owner.OnLegendVisibleChanged(visible);
307    }
308
309    private void OnLegendVisibleChanged(bool visible)
310    {
311      if (visible && legendItems.Count > 0)
312      {
313        Visibility = Visibility.Visible;
314      }
315      else
316      {
317        Visibility = Visibility.Collapsed;
318      }
319    }
320
321    #endregion // end of Properties
322
323    #region Overrides
324
325    public override void OnApplyTemplate()
326    {
327      base.OnApplyTemplate();
328
329#if !RELEASEXBAP
330      var rect = (Rectangle)Template.FindName("backRect", this);
331      if (rect != null)
332      {
333        rect.Effect = new DropShadowEffect { Direction = 300, ShadowDepth = 3, Opacity = 0.4 };
334      }
335#endif
336    }
337
338    #endregion // end of Overrides
339  }
340}
Note: See TracBrowser for help on using the repository browser.