Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/BoxChart/StringConstantBoxChartElementGenerator.cs @ 16995

Last change on this file since 16995 was 16995, checked in by gkronber, 5 years ago

#2520 Update plugin dependencies and references for HL.FLA for new persistence

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