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