GUI/HardwareNode.cs
author moel.mich
Sun, 09 Jun 2013 16:44:19 +0000
changeset 397 243b6d5afa7c
parent 340 600962f8a298
permissions -rw-r--r--
Added an option to disable stacked axes in the plot.
moel@1
     1
/*
moel@1
     2
 
moel@344
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@344
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@1
     6
 
moel@344
     7
  Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344
     8
	
moel@1
     9
*/
moel@1
    10
moel@1
    11
using System;
moel@1
    12
using System.Collections.Generic;
moel@1
    13
using System.Drawing;
moel@1
    14
using OpenHardwareMonitor.Hardware;
moel@1
    15
moel@1
    16
namespace OpenHardwareMonitor.GUI {
moel@1
    17
  public class HardwareNode : Node {
moel@1
    18
moel@165
    19
    private PersistentSettings settings;
moel@165
    20
    private UnitManager unitManager;
moel@1
    21
    private IHardware hardware;
moel@1
    22
moel@1
    23
    private List<TypeNode> typeNodes = new List<TypeNode>();
moel@1
    24
moel@165
    25
    public HardwareNode(IHardware hardware, PersistentSettings settings, 
moel@275
    26
      UnitManager unitManager) : base() 
moel@165
    27
    {
moel@165
    28
      this.settings = settings;
moel@165
    29
      this.unitManager = unitManager;
moel@1
    30
      this.hardware = hardware;
moel@176
    31
      this.Image = HardwareTypeImage.Instance.GetImage(hardware.HardwareType);
moel@1
    32
moel@340
    33
      foreach (SensorType sensorType in Enum.GetValues(typeof(SensorType)))
moel@340
    34
        typeNodes.Add(new TypeNode(sensorType));
moel@317
    35
moel@1
    36
      foreach (ISensor sensor in hardware.Sensors)
moel@1
    37
        SensorAdded(sensor);
moel@1
    38
moel@1
    39
      hardware.SensorAdded +=new SensorEventHandler(SensorAdded);
moel@1
    40
      hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
moel@1
    41
    }
moel@1
    42
moel@275
    43
    public override string Text {
moel@275
    44
      get { return hardware.Name; }
moel@275
    45
      set { hardware.Name = value; }
moel@275
    46
    }
moel@275
    47
moel@1
    48
    public IHardware Hardware {
moel@1
    49
      get { return hardware; }
moel@1
    50
    }
moel@1
    51
moel@111
    52
    private void UpdateNode(TypeNode node) {  
moel@111
    53
      if (node.Nodes.Count > 0) {
moel@1
    54
        if (!Nodes.Contains(node)) {
moel@1
    55
          int i = 0;
moel@1
    56
          while (i < Nodes.Count &&
moel@1
    57
            ((TypeNode)Nodes[i]).SensorType < node.SensorType)
moel@1
    58
            i++;
moel@1
    59
          Nodes.Insert(i, node);  
moel@1
    60
        }
moel@1
    61
      } else {
moel@1
    62
        if (Nodes.Contains(node))
moel@1
    63
          Nodes.Remove(node);
moel@1
    64
      }
moel@1
    65
    }
moel@1
    66
moel@1
    67
    private void SensorRemoved(ISensor sensor) {
moel@111
    68
      foreach (TypeNode typeNode in typeNodes)
moel@111
    69
        if (typeNode.SensorType == sensor.SensorType) { 
moel@111
    70
          SensorNode sensorNode = null;
moel@111
    71
          foreach (Node node in typeNode.Nodes) {
moel@111
    72
            SensorNode n = node as SensorNode;
moel@111
    73
            if (n != null && n.Sensor == sensor)
moel@111
    74
              sensorNode = n;
moel@111
    75
          }
moel@327
    76
          if (sensorNode != null) {
moel@327
    77
            sensorNode.PlotSelectionChanged -= SensorPlotSelectionChanged;
moel@327
    78
            typeNode.Nodes.Remove(sensorNode);
moel@327
    79
            UpdateNode(typeNode);
moel@327
    80
          }
moel@1
    81
        }
moel@327
    82
      if (PlotSelectionChanged != null)
moel@327
    83
        PlotSelectionChanged(this, null);
moel@1
    84
    }
moel@1
    85
moel@1
    86
    private void InsertSorted(Node node, ISensor sensor) {
moel@1
    87
      int i = 0;
moel@1
    88
      while (i < node.Nodes.Count &&
moel@1
    89
        ((SensorNode)node.Nodes[i]).Sensor.Index < sensor.Index)
moel@1
    90
        i++;
moel@165
    91
      SensorNode sensorNode = new SensorNode(sensor, settings, unitManager);
moel@327
    92
      sensorNode.PlotSelectionChanged += SensorPlotSelectionChanged;
moel@111
    93
      node.Nodes.Insert(i, sensorNode);
moel@1
    94
    }
moel@1
    95
moel@327
    96
    private void SensorPlotSelectionChanged(object sender, EventArgs e) {
moel@327
    97
      if (PlotSelectionChanged != null)
moel@327
    98
        PlotSelectionChanged(this, null);
moel@327
    99
    }
moel@327
   100
moel@1
   101
    private void SensorAdded(ISensor sensor) {
moel@111
   102
      foreach (TypeNode typeNode in typeNodes)
moel@111
   103
        if (typeNode.SensorType == sensor.SensorType) {
moel@111
   104
          InsertSorted(typeNode, sensor);
moel@111
   105
          UpdateNode(typeNode);          
moel@1
   106
        }
moel@327
   107
      if (PlotSelectionChanged != null)
moel@327
   108
        PlotSelectionChanged(this, null);
moel@327
   109
    }
moel@327
   110
moel@327
   111
    public event EventHandler PlotSelectionChanged;
moel@1
   112
  }
moel@1
   113
}