GUI/HardwareNode.cs
author moel.mich
Tue, 14 Feb 2012 23:07:55 +0000
changeset 340 600962f8a298
parent 327 e837e1e4b282
child 344 3145aadca3d2
permissions -rw-r--r--
Added a new sensor type "Factor" for dimensionless values (and similar) that are not to be shown as percent ("Level" type). Changed the write amplification sensor to use the new "Factor" sensor type. Added the temperature SMART attribute for Sandforce SSDs as hidden sensor (as it may show fake results on some hardware).
     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-2012
    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.Drawing;
    41 using OpenHardwareMonitor.Hardware;
    42 
    43 namespace OpenHardwareMonitor.GUI {
    44   public class HardwareNode : Node {
    45 
    46     private PersistentSettings settings;
    47     private UnitManager unitManager;
    48     private IHardware hardware;
    49 
    50     private List<TypeNode> typeNodes = new List<TypeNode>();
    51 
    52     public HardwareNode(IHardware hardware, PersistentSettings settings, 
    53       UnitManager unitManager) : base() 
    54     {
    55       this.settings = settings;
    56       this.unitManager = unitManager;
    57       this.hardware = hardware;
    58       this.Image = HardwareTypeImage.Instance.GetImage(hardware.HardwareType);
    59 
    60       foreach (SensorType sensorType in Enum.GetValues(typeof(SensorType)))
    61         typeNodes.Add(new TypeNode(sensorType));
    62 
    63       foreach (ISensor sensor in hardware.Sensors)
    64         SensorAdded(sensor);
    65 
    66       hardware.SensorAdded +=new SensorEventHandler(SensorAdded);
    67       hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
    68     }
    69 
    70     public override string Text {
    71       get { return hardware.Name; }
    72       set { hardware.Name = value; }
    73     }
    74 
    75     public IHardware Hardware {
    76       get { return hardware; }
    77     }
    78 
    79     private void UpdateNode(TypeNode node) {  
    80       if (node.Nodes.Count > 0) {
    81         if (!Nodes.Contains(node)) {
    82           int i = 0;
    83           while (i < Nodes.Count &&
    84             ((TypeNode)Nodes[i]).SensorType < node.SensorType)
    85             i++;
    86           Nodes.Insert(i, node);  
    87         }
    88       } else {
    89         if (Nodes.Contains(node))
    90           Nodes.Remove(node);
    91       }
    92     }
    93 
    94     private void SensorRemoved(ISensor sensor) {
    95       foreach (TypeNode typeNode in typeNodes)
    96         if (typeNode.SensorType == sensor.SensorType) { 
    97           SensorNode sensorNode = null;
    98           foreach (Node node in typeNode.Nodes) {
    99             SensorNode n = node as SensorNode;
   100             if (n != null && n.Sensor == sensor)
   101               sensorNode = n;
   102           }
   103           if (sensorNode != null) {
   104             sensorNode.PlotSelectionChanged -= SensorPlotSelectionChanged;
   105             typeNode.Nodes.Remove(sensorNode);
   106             UpdateNode(typeNode);
   107           }
   108         }
   109       if (PlotSelectionChanged != null)
   110         PlotSelectionChanged(this, null);
   111     }
   112 
   113     private void InsertSorted(Node node, ISensor sensor) {
   114       int i = 0;
   115       while (i < node.Nodes.Count &&
   116         ((SensorNode)node.Nodes[i]).Sensor.Index < sensor.Index)
   117         i++;
   118       SensorNode sensorNode = new SensorNode(sensor, settings, unitManager);
   119       sensorNode.PlotSelectionChanged += SensorPlotSelectionChanged;
   120       node.Nodes.Insert(i, sensorNode);
   121     }
   122 
   123     private void SensorPlotSelectionChanged(object sender, EventArgs e) {
   124       if (PlotSelectionChanged != null)
   125         PlotSelectionChanged(this, null);
   126     }
   127 
   128     private void SensorAdded(ISensor sensor) {
   129       foreach (TypeNode typeNode in typeNodes)
   130         if (typeNode.SensorType == sensor.SensorType) {
   131           InsertSorted(typeNode, sensor);
   132           UpdateNode(typeNode);          
   133         }
   134       if (PlotSelectionChanged != null)
   135         PlotSelectionChanged(this, null);
   136     }
   137 
   138     public event EventHandler PlotSelectionChanged;
   139   }
   140 }