Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/27/18 15:46:12 (6 years ago)
Author:
pfleck
Message:

#2906 Added PreprocessingTransformation as a custom view-model for transformations in preprocessing.

File:
1 moved

Legend:

Unmodified
Added
Removed
  • branches/2906_Transformations/HeuristicLab.DataPreprocessing.Views/3.4/TransformationListView.cs

    r15864 r15865  
    2020#endregion
    2121
     22using System;
     23using System.Drawing;
    2224using System.Linq;
    2325using System.Windows.Forms;
     26using HeuristicLab.Collections;
    2427using HeuristicLab.Core;
    2528using HeuristicLab.Core.Views;
    2629using HeuristicLab.Data;
    2730using HeuristicLab.MainForm;
    28 using HeuristicLab.Problems.DataAnalysis;
    2931
    3032namespace HeuristicLab.DataPreprocessing.Views {
    31   [View("CheckedTransformationList View")]
    32   [Content(typeof(ICheckedItemList<IDataAnalysisTransformation>), false)]
    33   public partial class CheckedTransformationListView : CheckedItemListView<IDataAnalysisTransformation> {
     33  [View("TransformationList View")]
     34  [Content(typeof(IItemList<PreprocessingTransformation>), false)]
     35  public partial class TransformationListView : ItemListView<PreprocessingTransformation> {
    3436
    3537    internal IFilteredPreprocessingData PreprocessingData { get; set; }
    3638
    37     public CheckedTransformationListView() {
     39    public TransformationListView() {
    3840      InitializeComponent();
    3941      itemsGroupBox.Text = "Transformations";
    4042    }
    4143
    42     protected override IDataAnalysisTransformation CreateItem() {
    43       var newTransformation = new DataAnalysisTransformation(PreprocessingData.VariableNames.Select(x => new StringValue(x)));
     44    protected override PreprocessingTransformation CreateItem() {
     45      var variableNames = PreprocessingData.VariableNames.Select(x => new StringValue(x))
     46        .Concat(Content.Select(x => new StringValue(x.TransformedVariable))).Distinct();
     47
     48      var newTransformation = new PreprocessingTransformation(variableNames);
    4449      newTransformation.TransformedVariableParameter.ValidValues.Add(new StringValue("<New Variable>"));
    4550      return newTransformation;
    46       //if (typeSelectorDialog == null) {
    47       //  typeSelectorDialog = new TypeSelectorDialog();
    48       //  typeSelectorDialog.Caption = "Select Transformation";
    49       //  typeSelectorDialog.TypeSelector.Caption = "Available Transformations";
    50       //  typeSelectorDialog.TypeSelector.Configure(typeof(IDataAnalysisTransformation), showNotInstantiableTypes: true, showGenericTypes: false, typeCondition: CanInstanciateTransformation);
    51       //}
    52 
    53       //if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
    54       //  try {
    55       //    // TODO: Avoid accessing parent view
    56       //    var transformationView = (TransformationView)Parent;
    57       //    var columnNames = transformationView.Content.PreprocessingData.VariableNames;
    58 
    59       //    return (IDataAnalysisTransformation)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType(new[] { columnNames });
    60       //  } catch (Exception ex) {
    61       //    ErrorHandling.ShowErrorDialog(this, ex);
    62       //  }
    63       //}
    64       //return null;
    6551    }
    6652
    67     //private bool CanInstanciateTransformation(Type type) {
    68     //  foreach (ConstructorInfo ctor in type.GetConstructors(BindingFlags.Public | BindingFlags.Instance)) {
    69     //    ParameterInfo[] parameters = ctor.GetParameters();
    70     //    if (parameters.Length == 1 && parameters[0].ParameterType == typeof(IEnumerable<string>)) return true;
    71     //  }
    72     //  return false;
    73     //}
     53    protected override void OnContentChanged() {
     54      base.OnContentChanged();
     55
     56      if (Content == null) return;
     57
     58      int i = 0;
     59      foreach (ListViewItem item in ItemsListView.Items) {
     60        var transformation = PreprocessingData.Transformations[i];
     61
     62        item.ForeColor = transformation.IsApplied ? Color.Black : Color.Gray;
     63        i++;
     64      }
     65      UpdateButtonsState();
     66    }
     67
     68    protected override void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<PreprocessingTransformation>> e) {
     69      base.Content_ItemsAdded(sender, e);
     70
     71      foreach (var transformation in e.Items) {
     72        UpdateListViewItemsColor(transformation.Value);
     73      }
     74    }
     75
     76    protected override void RegisterItemEvents(PreprocessingTransformation item) {
     77      base.RegisterItemEvents(item);
     78      item.PropertyChanged += Item_PropertyChanged;
     79    }
     80    protected override void DeregisterItemEvents(PreprocessingTransformation item) {
     81      item.PropertyChanged -= Item_PropertyChanged;
     82      base.DeregisterItemEvents(item);
     83    }
     84
     85    private void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
     86      if (sender is PreprocessingTransformation transformation)
     87        UpdateListViewItemsColor(transformation);
     88    }
     89
     90    protected override void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
     91      base.itemsListView_SelectedIndexChanged(sender, e);
     92
     93      UpdateButtonsState();
     94    }
     95
     96    private void UpdateListViewItemsColor(PreprocessingTransformation transformation) {
     97      foreach (ListViewItem item in itemListViewItemMapping[transformation]) {
     98        item.ForeColor = transformation.IsApplied ? Color.Black : Color.Gray;
     99      }
     100    }
     101
     102    private void UpdateButtonsState() {
     103      if (itemsListView.SelectedItems.Count != 1) return;
     104      int selectedIndex = itemsListView.SelectedIndices[0];
     105
     106      // TODO: do not move/remove already applied transformations
     107      // TODO: general disalow of movement?
     108      if (Content[selectedIndex].IsApplied) {
     109        moveUpButton.Enabled = selectedIndex > 0 && Content[selectedIndex - 1].IsApplied;
     110        moveDownButton.Enabled = false;
     111        removeButton.Enabled = false;
     112      }
     113    }
    74114  }
    75115}
Note: See TracChangeset for help on using the changeset viewer.