GUI/ParameterForm.cs
author moel.mich
Sun, 08 Aug 2010 13:57:26 +0000
changeset 165 813d8bc3192f
parent 86 b4f0f206173d
child 344 3145aadca3d2
permissions -rw-r--r--
Refactored the hardware monitoring code into a library (Issue 101).
moel@86
     1
/*
moel@86
     2
  
moel@86
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@86
     4
moel@86
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@86
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@86
     7
  the License. You may obtain a copy of the License at
moel@86
     8
 
moel@86
     9
  http://www.mozilla.org/MPL/
moel@86
    10
moel@86
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@86
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@86
    13
  for the specific language governing rights and limitations under the License.
moel@86
    14
moel@86
    15
  The Original Code is the Open Hardware Monitor code.
moel@86
    16
moel@86
    17
  The Initial Developer of the Original Code is 
moel@86
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@86
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@86
    20
  the Initial Developer. All Rights Reserved.
moel@86
    21
moel@86
    22
  Contributor(s):
moel@86
    23
moel@86
    24
  Alternatively, the contents of this file may be used under the terms of
moel@86
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@86
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@86
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@86
    28
  of those above. If you wish to allow use of your version of this file only
moel@86
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@86
    30
  use your version of this file under the terms of the MPL, indicate your
moel@86
    31
  decision by deleting the provisions above and replace them with the notice
moel@86
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@86
    33
  the provisions above, a recipient may use your version of this file under
moel@86
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@86
    35
 
moel@86
    36
*/
moel@86
    37
moel@86
    38
using System;
moel@63
    39
using System.Collections.Generic;
moel@63
    40
using System.ComponentModel;
moel@63
    41
using System.Text;
moel@63
    42
using System.Windows.Forms;
moel@63
    43
using OpenHardwareMonitor.Hardware;
moel@165
    44
using OpenHardwareMonitor.Collections;
moel@63
    45
moel@63
    46
namespace OpenHardwareMonitor.GUI {
moel@63
    47
  public partial class ParameterForm : Form {
moel@63
    48
moel@63
    49
    private IReadOnlyArray<IParameter> parameters;
moel@63
    50
    private BindingList<ParameterRow> parameterRows;
moel@63
    51
moel@63
    52
    public ParameterForm() {
moel@63
    53
      InitializeComponent();
moel@63
    54
    }
moel@63
    55
    
moel@63
    56
    public IReadOnlyArray<IParameter> Parameters {
moel@63
    57
      get {
moel@63
    58
        return parameters;
moel@63
    59
      }
moel@63
    60
      set {
moel@63
    61
        parameters = value;
moel@63
    62
        parameterRows = new BindingList<ParameterRow>();
moel@63
    63
        foreach (IParameter parameter in parameters)
moel@63
    64
          parameterRows.Add(new ParameterRow(parameter));
moel@63
    65
        bindingSource.DataSource = parameterRows;
moel@63
    66
      }
moel@63
    67
    }
moel@63
    68
moel@63
    69
    private class ParameterRow : INotifyPropertyChanged {
moel@63
    70
      public IParameter parameter;
moel@63
    71
      private float value;
moel@63
    72
      public bool isDefault;
moel@63
    73
moel@63
    74
      public event PropertyChangedEventHandler PropertyChanged;
moel@63
    75
moel@63
    76
      private void NotifyPropertyChanged(String propertyName) {
moel@63
    77
        if (PropertyChanged != null) {
moel@63
    78
          PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
moel@63
    79
        }
moel@63
    80
      }
moel@63
    81
moel@63
    82
      public ParameterRow(IParameter parameter){
moel@63
    83
        this.parameter = parameter;
moel@63
    84
        this.value = parameter.Value;
moel@63
    85
        this.isDefault = parameter.IsDefault;
moel@63
    86
      }
moel@63
    87
moel@63
    88
      public string Name {
moel@63
    89
        get { return parameter.Name; }
moel@63
    90
      }
moel@63
    91
moel@63
    92
      public float Value {
moel@63
    93
        get { return value; }
moel@63
    94
        set {            
moel@63
    95
          this.isDefault = false;
moel@63
    96
          this.value = value;
moel@63
    97
          NotifyPropertyChanged("Default");
moel@63
    98
          NotifyPropertyChanged("Value");
moel@63
    99
        }
moel@63
   100
      }
moel@63
   101
moel@63
   102
      public bool Default {
moel@63
   103
        get { return isDefault; }
moel@63
   104
        set {
moel@63
   105
          isDefault = value;
moel@63
   106
          if (value)
moel@63
   107
            this.value = parameter.DefaultValue;
moel@63
   108
          NotifyPropertyChanged("Default");
moel@63
   109
          NotifyPropertyChanged("Value");
moel@63
   110
        }
moel@63
   111
      }
moel@63
   112
    }
moel@63
   113
moel@63
   114
    private void dataGridView_RowEnter(object sender, 
moel@63
   115
      DataGridViewCellEventArgs e) 
moel@63
   116
    {
moel@63
   117
      if (e.RowIndex >= 0 && e.RowIndex < parameters.Length)
moel@63
   118
        descriptionLabel.Text = parameters[e.RowIndex].Description;
moel@63
   119
      else
moel@63
   120
        descriptionLabel.Text = "";
moel@63
   121
    }
moel@63
   122
moel@63
   123
    private void dataGridView_CellValidating(object sender, 
moel@63
   124
      DataGridViewCellValidatingEventArgs e) 
moel@63
   125
    {
moel@63
   126
      float value;
moel@63
   127
      if (e.ColumnIndex == 2 &&
moel@63
   128
        !float.TryParse(e.FormattedValue.ToString(), out value)) {
moel@63
   129
        dataGridView.Rows[e.RowIndex].Cells[0].ErrorText = 
moel@63
   130
          "Invalid value";
moel@63
   131
        e.Cancel = true;
moel@63
   132
      }
moel@63
   133
    }
moel@63
   134
moel@63
   135
    private void dataGridView_CellEndEdit(object sender,
moel@63
   136
      DataGridViewCellEventArgs e) {
moel@63
   137
      dataGridView.Rows[e.RowIndex].Cells[0].ErrorText = "";
moel@63
   138
    }
moel@63
   139
moel@63
   140
    private void okButton_Click(object sender, EventArgs e) {
moel@63
   141
      foreach (ParameterRow row in parameterRows) {
moel@63
   142
        if (row.Default) {
moel@63
   143
          row.parameter.IsDefault = true;
moel@63
   144
        } else {
moel@63
   145
          row.parameter.Value = row.Value;
moel@63
   146
        }
moel@63
   147
      }
moel@63
   148
    }
moel@63
   149
moel@63
   150
    private void dataGridView_CurrentCellDirtyStateChanged(object sender, 
moel@63
   151
      EventArgs e) {
moel@63
   152
      if (dataGridView.CurrentCell is DataGridViewCheckBoxCell ||
moel@63
   153
        dataGridView.CurrentCell is DataGridViewComboBoxCell) 
moel@63
   154
      {
moel@63
   155
        dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
moel@63
   156
      }
moel@63
   157
    }
moel@63
   158
  }
moel@63
   159
}