Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3040_VectorBasedGP/HeuristicLab.Data.Views/3.3/EnumValueView.cs @ 18201

Last change on this file since 18201 was 18201, checked in by pfleck, 9 months ago

#3040 Improved handling of "None" Enum flags.

File size: 6.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Windows.Forms;
24using HeuristicLab.Core.Views;
25using HeuristicLab.MainForm;
26
27namespace HeuristicLab.Data.Views {
28  [View("EnumValue View")]
29  [Content(typeof(EnumValue<>), true)]
30  public partial class EnumValueView<T> : ItemView where T : struct, IComparable {
31
32    public new EnumValue<T> Content {
33      get { return (EnumValue<T>)base.Content; }
34      set { base.Content = value; }
35    }
36
37    public override bool ReadOnly {
38      get {
39        if ((Content != null) && Content.ReadOnly) return true;
40        return base.ReadOnly;
41      }
42      set { base.ReadOnly = value; }
43    }
44
45    static EnumValueView() {
46      if (!typeof(T).IsEnum)
47        throw new InvalidOperationException("Generic type " + typeof(T).Name + " is not an enum.");
48    }
49
50    public EnumValueView() {
51      InitializeComponent();
52      this.Caption = typeof(T).Name + " View";
53
54      valueComboBox.DataSource = Enum.GetValues(typeof(T));
55      foreach (T flag in Enum.GetValues(typeof(T)))
56        flagsListView.Items.Add(new ListViewItem(flag.ToString()) { Tag = flag });
57      columnHeader.Width = -1;
58
59      bool isFlags = Attribute.IsDefined(typeof(T), typeof(FlagsAttribute));
60      if (isFlags) valueLabel.Text = "Flags:";
61      valueComboBox.Visible = !isFlags;
62      flagsListView.Visible = isFlags;
63    }
64    public EnumValueView(EnumValue<T> content)
65      : this() {
66      Content = content;
67    }
68
69    protected override void DeregisterContentEvents() {
70      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
71      base.DeregisterContentEvents();
72    }
73
74    protected override void RegisterContentEvents() {
75      base.RegisterContentEvents();
76      Content.ValueChanged += new EventHandler(Content_ValueChanged);
77    }
78
79    protected override void OnContentChanged() {
80      base.OnContentChanged();
81      if (Content == null) {
82        valueComboBox.SelectedIndex = -1;
83        foreach (ListViewItem item in flagsListView.Items)
84          item.Checked = false;
85      } else {
86        valueComboBox.SelectedItem = Content.Value;
87        var value = (Enum)(object)Content.Value;
88
89        var zeroValue = Enum.ToObject(value.GetType(), Convert.ChangeType(0, value.GetTypeCode()));
90        var hasZeroValue = Enum.IsDefined(value.GetType(), zeroValue);
91
92        foreach (ListViewItem item in flagsListView.Items) {
93          var flag = (Enum)item.Tag;
94          if (hasZeroValue && flag.Equals(zeroValue)) { // only show the zero-value if the whole value is zero, otherwise the zero-value would always be checked
95            item.Checked = value.Equals(zeroValue);
96          } else {
97            item.Checked = value.HasFlag(flag);
98          }
99        }
100      }
101    }
102
103    protected override void SetEnabledStateOfControls() {
104      base.SetEnabledStateOfControls();
105      if (Content == null) {
106        valueComboBox.Enabled = false;
107        flagsListView.Enabled = false;
108      } else {
109        valueComboBox.Enabled = !ReadOnly;
110        flagsListView.Enabled = !ReadOnly;
111      }
112    }
113
114    private void Content_ValueChanged(object sender, EventArgs e) {
115      if (InvokeRequired)
116        Invoke(new EventHandler(Content_ValueChanged), sender, e);
117      else {
118        valueComboBox.SelectedItem = Content.Value;
119        var value = (Enum)(object)Content.Value;
120
121        var zeroValue = value.GetZeroValue<T>();
122        var hasZeroValue = Enum.IsDefined(value.GetType(), zeroValue);
123
124        foreach (ListViewItem item in flagsListView.Items) {
125          var flag = (Enum)item.Tag;
126          if (hasZeroValue && flag.Equals(zeroValue)) { // only show the zero-value if the whole value is zero, otherwise the zero-value would always be checked
127            item.Checked = value.Equals(zeroValue);
128          } else {
129            item.Checked = value.HasFlag(flag);
130          }
131        }
132      }
133    }
134
135    private void valueComboBox_SelectedIndexChanged(object sender, EventArgs e) {
136      if (Content != null && !Content.ReadOnly)
137        Content.Value = (T)valueComboBox.SelectedItem;
138    }
139
140    private void flagsListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
141      var flag = (T)e.Item.Tag;
142      if (Content != null && !Content.ReadOnly) {
143        var value = (Enum)(object)Content.Value;
144
145        var zeroValue = value.GetZeroValue<T>();
146        var hasZeroValue = Enum.IsDefined(value.GetType(), zeroValue);
147
148        if (hasZeroValue && flag.Equals(zeroValue)) {
149          if (e.Item.Checked) { // Checking zero-value => set zero-value
150            Content.Value = value.GetZeroValue<T>();
151          } else if (value.Equals(zeroValue)) { // Un-checking zero-value => set all-value
152            Content.Value = value.GetAllValue<T>();
153          }
154        } else {
155          Content.Value = value.SetFlag(flag, e.Item.Checked);
156        }
157      }
158    }
159  }
160
161  internal static class EnumHelper {
162    //https://stackoverflow.com/a/21581418
163    public static T SetFlag<T>(this Enum value, T flag, bool set) {
164      var baseType = Enum.GetUnderlyingType(value.GetType());
165      dynamic valueAsBase = Convert.ChangeType(value, baseType);
166      dynamic flagAsBase = Convert.ChangeType(flag, baseType);
167      if (set)
168        valueAsBase |= flagAsBase;
169      else
170        valueAsBase &= ~flagAsBase;
171      return (T)valueAsBase;
172    }
173
174    public static T GetZeroValue<T>(this Enum value) {
175      return (T)Enum.ToObject(value.GetType(), Convert.ChangeType(0, value.GetTypeCode()));
176    }
177
178    public static T GetAllValue<T>(this Enum value) {
179      var allValues = (Enum)(object)value.GetZeroValue<T>();
180      foreach (T fl in Enum.GetValues(value.GetType()))
181        allValues = (Enum)(object)allValues.SetFlag(fl, true);
182      return (T)(object)allValues;
183    }
184  }
185}
Note: See TracBrowser for help on using the repository browser.