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