Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/BoxChart/StringConstantBoxChartElementGenerator.cs @ 7403

Last change on this file since 7403 was 7128, checked in by epitzer, 13 years ago

#1696 Integrate fitness landscape analysis plugins from Heureka! repository.

File size: 2.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Drawing.Drawing2D;
5using System.Linq;
6using System.Text;
7using HeuristicLab.Common;
8using HeuristicLab.Core;
9using HeuristicLab.Data;
10using HeuristicLab.Optimization;
11using HeuristicLab.Parameters;
12using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
13
14namespace HeuristicLab.Analysis.FitnessLandscape.BoxCharts {
15
16  [StorableClass]
17  public class StringConstantBoxChartElementGenerator : EmptyBoxChartElementGenerator {
18
19    public ValueParameter<StringValue> TextParameter {
20      get { return (ValueParameter<StringValue>)Parameters["Text"]; }
21    }
22    public ValueParameter<IntValue> FontSizeParameter {
23      get { return (ValueParameter<IntValue>) Parameters["FontSize"]; }
24    }
25
26    protected string Text { get { return TextParameter.Value.Value; } }
27    protected int FontSize { get { return FontSizeParameter.Value.Value; }}
28
29    #region Construction & Cloning
30    [StorableConstructor]
31    protected StringConstantBoxChartElementGenerator(bool deserializing) : base(deserializing) { }
32    protected StringConstantBoxChartElementGenerator(StringConstantBoxChartElementGenerator original, Cloner cloner) : base(original, cloner) {
33      RegisterEvents();
34    }
35    public StringConstantBoxChartElementGenerator() {
36      Parameters.Add(new ValueParameter<StringValue>("Text", "The text to be rendered inside this element.", new StringValue("Text")));
37      Parameters.Add(new ValueParameter<IntValue>("FontSize", "The font size to be used for the text.", new IntValue(11)));
38      RegisterEvents();
39      if (GetType() == typeof(StringConstantBoxChartElementGenerator))
40        UpdateName();
41    }
42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new StringConstantBoxChartElementGenerator(this, cloner);
44    }
45    [StorableHook(HookType.AfterDeserialization)]
46    private void AfterDeserialization() {
47      RegisterEvents();
48    }
49    #endregion
50
51    private void RegisterEvents() {
52      TextParameter.ToStringChanged += new EventHandler(Parameters_Changed);
53    }
54
55    private void Parameters_Changed(object sender, EventArgs eventArgs) {
56      UpdateName();
57    }
58
59    protected override string CalculateName() {
60      return string.Format("{0} \"{1}\" ({2})", base.CalculateName(), Text, FontSize);
61    }
62
63    public override void Draw(IRun run, Graphics g) {
64      using(var font = new Font("Helvetica", FontSize)) {
65        var size = g.MeasureString(Text, font);
66        g.DrawString(Text, font, Brushes.Black,
67          g.ClipBounds.Left+(g.ClipBounds.Width - size.Width)/2,
68          g.ClipBounds.Top+(g.ClipBounds.Height - size.Height)/2);
69      }
70    }
71  }
72}
Note: See TracBrowser for help on using the repository browser.