Changeset 14597


Ignore:
Timestamp:
01/23/17 12:15:12 (9 months ago)
Author:
pfleck
Message:

#2724

  • Added a ListView for Flag-attributed enums in the EnumValueView.
  • Simplified EnumValue cloning (already done in base-constructor).
  • Added a SetFlag helper for enums.
Location:
trunk/sources
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Data.Views/3.3/EnumValueView.Designer.cs

    r14185 r14597  
    4747      this.valueLabel = new System.Windows.Forms.Label();
    4848      this.valueComboBox = new System.Windows.Forms.ComboBox();
     49      this.flagsListView = new System.Windows.Forms.ListView();
     50      this.columnHeader = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
    4951      this.SuspendLayout();
    5052      //
     
    5456      this.valueLabel.Location = new System.Drawing.Point(3, 3);
    5557      this.valueLabel.Name = "valueLabel";
    56       this.valueLabel.Size = new System.Drawing.Size(56, 13);
     58      this.valueLabel.Size = new System.Drawing.Size(37, 13);
    5759      this.valueLabel.TabIndex = 0;
    5860      this.valueLabel.Text = "&Value:";
     
    6062      // valueComboBox
    6163      //
    62       this.valueComboBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
     64      this.valueComboBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
    6365            | System.Windows.Forms.AnchorStyles.Right)));
    6466      this.valueComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
     
    7173      this.valueComboBox.SelectedIndexChanged += new System.EventHandler(this.valueComboBox_SelectedIndexChanged);
    7274      //
     75      // flagsListView
     76      //
     77      this.flagsListView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
     78            | System.Windows.Forms.AnchorStyles.Left)
     79            | System.Windows.Forms.AnchorStyles.Right)));
     80      this.flagsListView.CheckBoxes = true;
     81      this.flagsListView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
     82            this.columnHeader});
     83      this.flagsListView.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
     84      this.flagsListView.Location = new System.Drawing.Point(65, 0);
     85      this.flagsListView.Name = "flagsListView";
     86      this.flagsListView.Size = new System.Drawing.Size(212, 21);
     87      this.flagsListView.TabIndex = 2;
     88      this.flagsListView.UseCompatibleStateImageBehavior = false;
     89      this.flagsListView.View = System.Windows.Forms.View.Details;
     90      this.flagsListView.ItemChecked += new System.Windows.Forms.ItemCheckedEventHandler(this.flagsListView_ItemChecked);
     91      //
     92      // columnHeader
     93      //
     94      this.columnHeader.Width = 100;
     95      //
    7396      // EnumValueView
    7497      //
    75       this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    7698      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
     99      this.Controls.Add(this.flagsListView);
    77100      this.Controls.Add(this.valueComboBox);
    78101      this.Controls.Add(this.valueLabel);
     
    81104      this.ResumeLayout(false);
    82105      this.PerformLayout();
     106
    83107    }
    84108
     
    87111    private System.Windows.Forms.Label valueLabel;
    88112    private System.Windows.Forms.ComboBox valueComboBox;
     113    private System.Windows.Forms.ListView flagsListView;
     114    private System.Windows.Forms.ColumnHeader columnHeader;
    89115  }
    90116}
  • trunk/sources/HeuristicLab.Data.Views/3.3/EnumValueView.cs

    r14185 r14597  
    2121
    2222using System;
     23using System.Windows.Forms;
    2324using HeuristicLab.Core.Views;
    2425using HeuristicLab.MainForm;
     
    4445    public EnumValueView() {
    4546      InitializeComponent();
     47      this.Name = typeof(T).Name + "EnumView";
     48
    4649      valueComboBox.DataSource = Enum.GetValues(typeof(T));
    47       this.Name = typeof(T).Name + "EnumView";
     50      foreach (T flag in Enum.GetValues(typeof(T)))
     51        flagsListView.Items.Add(new ListViewItem(flag.ToString()) { Tag = flag });
     52      columnHeader.Width = -1;
     53
     54      bool isFlags = Attribute.IsDefined(typeof(T), typeof(FlagsAttribute));
     55      if (isFlags) valueLabel.Text = "Flags:";
     56      valueComboBox.Visible = !isFlags;
     57      flagsListView.Visible = isFlags;
    4858    }
    4959    public EnumValueView(EnumValue<T> content)
     
    6474    protected override void OnContentChanged() {
    6575      base.OnContentChanged();
    66       if (Content == null)
    67         valueComboBox.Enabled = false;
    68       else
     76      if (Content == null) {
     77        valueComboBox.SelectedIndex = -1;
     78        foreach (ListViewItem item in flagsListView.Items)
     79          item.Checked = false;
     80      } else {
    6981        valueComboBox.SelectedItem = Content.Value;
     82        foreach (ListViewItem item in flagsListView.Items) {
     83          var flag = (Enum)item.Tag;
     84          item.Checked = ((Enum)(object)Content.Value).HasFlag(flag);
     85        }
     86      }
    7087    }
    7188
    7289    protected override void SetEnabledStateOfControls() {
    7390      base.SetEnabledStateOfControls();
    74       if (Content == null) valueComboBox.Enabled = false;
    75       else valueComboBox.Enabled = !ReadOnly;
     91      if (Content == null) {
     92        valueComboBox.Enabled = false;
     93        flagsListView.Enabled = false;
     94      } else {
     95        valueComboBox.Enabled = !ReadOnly;
     96        flagsListView.Enabled = !ReadOnly;
     97      }
    7698    }
    7799
     
    79101      if (InvokeRequired)
    80102        Invoke(new EventHandler(Content_ValueChanged), sender, e);
    81       else
     103      else {
    82104        valueComboBox.SelectedItem = Content.Value;
     105        foreach (ListViewItem item in flagsListView.Items) {
     106          var flag = (Enum)item.Tag;
     107          item.Checked = ((Enum)(object)Content.Value).HasFlag(flag);
     108        }
     109      }
    83110    }
    84111
     
    87114        Content.Value = (T)valueComboBox.SelectedItem;
    88115    }
     116
     117    private void flagsListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
     118      var flag = (T)e.Item.Tag;
     119      if ((Content != null) && !Content.ReadOnly)
     120        Content.Value = ((Enum)(object)Content.Value).SetFlag(flag, e.Item.Checked);
     121    }
    89122  }
    90123}
  • trunk/sources/HeuristicLab.Data/3.3/EnumValue.cs

    r14185 r14597  
    5252    private EnumValue(EnumValue<T> original, Cloner cloner)
    5353      : base(original, cloner) {
    54       this.value = original.value;
    55       this.readOnly = original.readOnly;
    5654    }
    5755
     
    6462    }
    6563  }
     64
     65  public static class EnumHelper {
     66    public static T SetFlag<T>(this Enum value, T flag, bool set) {
     67      var baseType = Enum.GetUnderlyingType(value.GetType());
     68      dynamic valueAsBase = Convert.ChangeType(value, baseType);
     69      dynamic flagAsBase = Convert.ChangeType(flag, baseType);
     70      if (set)
     71        valueAsBase |= flagAsBase;
     72      else
     73        valueAsBase &= ~flagAsBase;
     74      return (T)valueAsBase;
     75    }
     76  }
    6677}
  • trunk/sources/HeuristicLab.Data/3.3/HeuristicLab.Data-3.3.csproj

    r13695 r14597  
    108108  </PropertyGroup>
    109109  <ItemGroup>
     110    <Reference Include="Microsoft.CSharp" />
    110111    <Reference Include="System" />
    111112    <Reference Include="System.Core">
Note: See TracChangeset for help on using the changeset viewer.