GUI/HardwareNode.cs
author moel.mich
Sun, 01 Jan 2012 17:12:34 +0000
changeset 327 e837e1e4b282
parent 324 c6ee430d6995
child 340 600962f8a298
permissions -rw-r--r--
Added code to save and restore the plot sensor selection.
     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-2011
    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       typeNodes.Add(new TypeNode(SensorType.Voltage));
    61       typeNodes.Add(new TypeNode(SensorType.Clock));      
    62       typeNodes.Add(new TypeNode(SensorType.Temperature));
    63       typeNodes.Add(new TypeNode(SensorType.Load));
    64       typeNodes.Add(new TypeNode(SensorType.Fan));
    65       typeNodes.Add(new TypeNode(SensorType.Flow));
    66       typeNodes.Add(new TypeNode(SensorType.Control));
    67       typeNodes.Add(new TypeNode(SensorType.Level));
    68       typeNodes.Add(new TypeNode(SensorType.Power));
    69       typeNodes.Add(new TypeNode(SensorType.Data));
    70 
    71       foreach (ISensor sensor in hardware.Sensors)
    72         SensorAdded(sensor);
    73 
    74       hardware.SensorAdded +=new SensorEventHandler(SensorAdded);
    75       hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
    76     }
    77 
    78     public override string Text {
    79       get { return hardware.Name; }
    80       set { hardware.Name = value; }
    81     }
    82 
    83     public IHardware Hardware {
    84       get { return hardware; }
    85     }
    86 
    87     private void UpdateNode(TypeNode node) {  
    88       if (node.Nodes.Count > 0) {
    89         if (!Nodes.Contains(node)) {
    90           int i = 0;
    91           while (i < Nodes.Count &&
    92             ((TypeNode)Nodes[i]).SensorType < node.SensorType)
    93             i++;
    94           Nodes.Insert(i, node);  
    95         }
    96       } else {
    97         if (Nodes.Contains(node))
    98           Nodes.Remove(node);
    99       }
   100     }
   101 
   102     private void SensorRemoved(ISensor sensor) {
   103       foreach (TypeNode typeNode in typeNodes)
   104         if (typeNode.SensorType == sensor.SensorType) { 
   105           SensorNode sensorNode = null;
   106           foreach (Node node in typeNode.Nodes) {
   107             SensorNode n = node as SensorNode;
   108             if (n != null && n.Sensor == sensor)
   109               sensorNode = n;
   110           }
   111           if (sensorNode != null) {
   112             sensorNode.PlotSelectionChanged -= SensorPlotSelectionChanged;
   113             typeNode.Nodes.Remove(sensorNode);
   114             UpdateNode(typeNode);
   115           }
   116         }
   117       if (PlotSelectionChanged != null)
   118         PlotSelectionChanged(this, null);
   119     }
   120 
   121     private void InsertSorted(Node node, ISensor sensor) {
   122       int i = 0;
   123       while (i < node.Nodes.Count &&
   124         ((SensorNode)node.Nodes[i]).Sensor.Index < sensor.Index)
   125         i++;
   126       SensorNode sensorNode = new SensorNode(sensor, settings, unitManager);
   127       sensorNode.PlotSelectionChanged += SensorPlotSelectionChanged;
   128       node.Nodes.Insert(i, sensorNode);
   129     }
   130 
   131     private void SensorPlotSelectionChanged(object sender, EventArgs e) {
   132       if (PlotSelectionChanged != null)
   133         PlotSelectionChanged(this, null);
   134     }
   135 
   136     private void SensorAdded(ISensor sensor) {
   137       foreach (TypeNode typeNode in typeNodes)
   138         if (typeNode.SensorType == sensor.SensorType) {
   139           InsertSorted(typeNode, sensor);
   140           UpdateNode(typeNode);          
   141         }
   142       if (PlotSelectionChanged != null)
   143         PlotSelectionChanged(this, null);
   144     }
   145 
   146     public event EventHandler PlotSelectionChanged;
   147   }
   148 }