GUI/ParameterForm.cs
author StephaneLenclud
Thu, 18 Apr 2013 23:25:10 +0200
branchMiniDisplay
changeset 444 9b09e2ee0968
parent 165 813d8bc3192f
permissions -rw-r--r--
Front View plug-in does not init if no sensor added.
Fixing some format to make strings shorter.
Now trying to start SoundGraphAccess.exe process from same directory.
Packed mode now can display three sensors along with the current time.
moel@86
     1
/*
moel@344
     2
 
moel@344
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@344
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@344
     6
 
moel@344
     7
  Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344
     8
	
moel@344
     9
*/
moel@86
    10
moel@86
    11
moel@86
    12
using System;
moel@63
    13
using System.Collections.Generic;
moel@63
    14
using System.ComponentModel;
moel@63
    15
using System.Text;
moel@63
    16
using System.Windows.Forms;
moel@63
    17
using OpenHardwareMonitor.Hardware;
moel@165
    18
using OpenHardwareMonitor.Collections;
moel@63
    19
moel@63
    20
namespace OpenHardwareMonitor.GUI {
moel@63
    21
  public partial class ParameterForm : Form {
moel@63
    22
moel@63
    23
    private IReadOnlyArray<IParameter> parameters;
moel@63
    24
    private BindingList<ParameterRow> parameterRows;
moel@63
    25
moel@63
    26
    public ParameterForm() {
moel@63
    27
      InitializeComponent();
moel@63
    28
    }
moel@63
    29
    
moel@63
    30
    public IReadOnlyArray<IParameter> Parameters {
moel@63
    31
      get {
moel@63
    32
        return parameters;
moel@63
    33
      }
moel@63
    34
      set {
moel@63
    35
        parameters = value;
moel@63
    36
        parameterRows = new BindingList<ParameterRow>();
moel@63
    37
        foreach (IParameter parameter in parameters)
moel@63
    38
          parameterRows.Add(new ParameterRow(parameter));
moel@63
    39
        bindingSource.DataSource = parameterRows;
moel@63
    40
      }
moel@63
    41
    }
moel@63
    42
moel@63
    43
    private class ParameterRow : INotifyPropertyChanged {
moel@63
    44
      public IParameter parameter;
moel@63
    45
      private float value;
moel@63
    46
      public bool isDefault;
moel@63
    47
moel@63
    48
      public event PropertyChangedEventHandler PropertyChanged;
moel@63
    49
moel@63
    50
      private void NotifyPropertyChanged(String propertyName) {
moel@63
    51
        if (PropertyChanged != null) {
moel@63
    52
          PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
moel@63
    53
        }
moel@63
    54
      }
moel@63
    55
moel@63
    56
      public ParameterRow(IParameter parameter){
moel@63
    57
        this.parameter = parameter;
moel@63
    58
        this.value = parameter.Value;
moel@63
    59
        this.isDefault = parameter.IsDefault;
moel@63
    60
      }
moel@63
    61
moel@63
    62
      public string Name {
moel@63
    63
        get { return parameter.Name; }
moel@63
    64
      }
moel@63
    65
moel@63
    66
      public float Value {
moel@63
    67
        get { return value; }
moel@63
    68
        set {            
moel@63
    69
          this.isDefault = false;
moel@63
    70
          this.value = value;
moel@63
    71
          NotifyPropertyChanged("Default");
moel@63
    72
          NotifyPropertyChanged("Value");
moel@63
    73
        }
moel@63
    74
      }
moel@63
    75
moel@63
    76
      public bool Default {
moel@63
    77
        get { return isDefault; }
moel@63
    78
        set {
moel@63
    79
          isDefault = value;
moel@63
    80
          if (value)
moel@63
    81
            this.value = parameter.DefaultValue;
moel@63
    82
          NotifyPropertyChanged("Default");
moel@63
    83
          NotifyPropertyChanged("Value");
moel@63
    84
        }
moel@63
    85
      }
moel@63
    86
    }
moel@63
    87
moel@63
    88
    private void dataGridView_RowEnter(object sender, 
moel@63
    89
      DataGridViewCellEventArgs e) 
moel@63
    90
    {
moel@63
    91
      if (e.RowIndex >= 0 && e.RowIndex < parameters.Length)
moel@63
    92
        descriptionLabel.Text = parameters[e.RowIndex].Description;
moel@63
    93
      else
moel@63
    94
        descriptionLabel.Text = "";
moel@63
    95
    }
moel@63
    96
moel@63
    97
    private void dataGridView_CellValidating(object sender, 
moel@63
    98
      DataGridViewCellValidatingEventArgs e) 
moel@63
    99
    {
moel@63
   100
      float value;
moel@63
   101
      if (e.ColumnIndex == 2 &&
moel@63
   102
        !float.TryParse(e.FormattedValue.ToString(), out value)) {
moel@63
   103
        dataGridView.Rows[e.RowIndex].Cells[0].ErrorText = 
moel@63
   104
          "Invalid value";
moel@63
   105
        e.Cancel = true;
moel@63
   106
      }
moel@63
   107
    }
moel@63
   108
moel@63
   109
    private void dataGridView_CellEndEdit(object sender,
moel@63
   110
      DataGridViewCellEventArgs e) {
moel@63
   111
      dataGridView.Rows[e.RowIndex].Cells[0].ErrorText = "";
moel@63
   112
    }
moel@63
   113
moel@63
   114
    private void okButton_Click(object sender, EventArgs e) {
moel@63
   115
      foreach (ParameterRow row in parameterRows) {
moel@63
   116
        if (row.Default) {
moel@63
   117
          row.parameter.IsDefault = true;
moel@63
   118
        } else {
moel@63
   119
          row.parameter.Value = row.Value;
moel@63
   120
        }
moel@63
   121
      }
moel@63
   122
    }
moel@63
   123
moel@63
   124
    private void dataGridView_CurrentCellDirtyStateChanged(object sender, 
moel@63
   125
      EventArgs e) {
moel@63
   126
      if (dataGridView.CurrentCell is DataGridViewCheckBoxCell ||
moel@63
   127
        dataGridView.CurrentCell is DataGridViewComboBoxCell) 
moel@63
   128
      {
moel@63
   129
        dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
moel@63
   130
      }
moel@63
   131
    }
moel@63
   132
  }
moel@63
   133
}