GUI/HardwareNode.cs
author StephaneLenclud
Sun, 03 Feb 2013 18:01:50 +0100
branchMiniDisplay
changeset 433 090259cfd699
parent 340 600962f8a298
permissions -rw-r--r--
Adding SoundGraphDisplay and SensorFrontView classes.
They were respectively based on SystemTray and SensorNotifyIcon.
SoundGraphDisplay is now able to load iMONDisplay.dll providing it lives on your PATH.
Adding option to sensor context menu for adding it into FrontView.
     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-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System;
    12 using System.Collections.Generic;
    13 using System.Drawing;
    14 using OpenHardwareMonitor.Hardware;
    15 
    16 namespace OpenHardwareMonitor.GUI {
    17   public class HardwareNode : Node {
    18 
    19     private PersistentSettings settings;
    20     private UnitManager unitManager;
    21     private IHardware hardware;
    22 
    23     private List<TypeNode> typeNodes = new List<TypeNode>();
    24 
    25     public HardwareNode(IHardware hardware, PersistentSettings settings, 
    26       UnitManager unitManager) : base() 
    27     {
    28       this.settings = settings;
    29       this.unitManager = unitManager;
    30       this.hardware = hardware;
    31       this.Image = HardwareTypeImage.Instance.GetImage(hardware.HardwareType);
    32 
    33       foreach (SensorType sensorType in Enum.GetValues(typeof(SensorType)))
    34         typeNodes.Add(new TypeNode(sensorType));
    35 
    36       foreach (ISensor sensor in hardware.Sensors)
    37         SensorAdded(sensor);
    38 
    39       hardware.SensorAdded +=new SensorEventHandler(SensorAdded);
    40       hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
    41     }
    42 
    43     public override string Text {
    44       get { return hardware.Name; }
    45       set { hardware.Name = value; }
    46     }
    47 
    48     public IHardware Hardware {
    49       get { return hardware; }
    50     }
    51 
    52     private void UpdateNode(TypeNode node) {  
    53       if (node.Nodes.Count > 0) {
    54         if (!Nodes.Contains(node)) {
    55           int i = 0;
    56           while (i < Nodes.Count &&
    57             ((TypeNode)Nodes[i]).SensorType < node.SensorType)
    58             i++;
    59           Nodes.Insert(i, node);  
    60         }
    61       } else {
    62         if (Nodes.Contains(node))
    63           Nodes.Remove(node);
    64       }
    65     }
    66 
    67     private void SensorRemoved(ISensor sensor) {
    68       foreach (TypeNode typeNode in typeNodes)
    69         if (typeNode.SensorType == sensor.SensorType) { 
    70           SensorNode sensorNode = null;
    71           foreach (Node node in typeNode.Nodes) {
    72             SensorNode n = node as SensorNode;
    73             if (n != null && n.Sensor == sensor)
    74               sensorNode = n;
    75           }
    76           if (sensorNode != null) {
    77             sensorNode.PlotSelectionChanged -= SensorPlotSelectionChanged;
    78             typeNode.Nodes.Remove(sensorNode);
    79             UpdateNode(typeNode);
    80           }
    81         }
    82       if (PlotSelectionChanged != null)
    83         PlotSelectionChanged(this, null);
    84     }
    85 
    86     private void InsertSorted(Node node, ISensor sensor) {
    87       int i = 0;
    88       while (i < node.Nodes.Count &&
    89         ((SensorNode)node.Nodes[i]).Sensor.Index < sensor.Index)
    90         i++;
    91       SensorNode sensorNode = new SensorNode(sensor, settings, unitManager);
    92       sensorNode.PlotSelectionChanged += SensorPlotSelectionChanged;
    93       node.Nodes.Insert(i, sensorNode);
    94     }
    95 
    96     private void SensorPlotSelectionChanged(object sender, EventArgs e) {
    97       if (PlotSelectionChanged != null)
    98         PlotSelectionChanged(this, null);
    99     }
   100 
   101     private void SensorAdded(ISensor sensor) {
   102       foreach (TypeNode typeNode in typeNodes)
   103         if (typeNode.SensorType == sensor.SensorType) {
   104           InsertSorted(typeNode, sensor);
   105           UpdateNode(typeNode);          
   106         }
   107       if (PlotSelectionChanged != null)
   108         PlotSelectionChanged(this, null);
   109     }
   110 
   111     public event EventHandler PlotSelectionChanged;
   112   }
   113 }