moel@1: /* moel@1: moel@344: This Source Code Form is subject to the terms of the Mozilla Public moel@344: License, v. 2.0. If a copy of the MPL was not distributed with this moel@344: file, You can obtain one at http://mozilla.org/MPL/2.0/. moel@1: moel@344: Copyright (C) 2009-2012 Michael Möller moel@344: moel@1: */ moel@1: moel@1: using System; moel@1: using System.Collections.Generic; moel@1: using OpenHardwareMonitor.Hardware; moel@111: using OpenHardwareMonitor.Utilities; moel@1: moel@1: namespace OpenHardwareMonitor.GUI { moel@1: public class SensorNode : Node { moel@1: moel@1: private ISensor sensor; moel@165: private PersistentSettings settings; moel@165: private UnitManager unitManager; moel@1: private string format; moel@165: private bool plot = false; moel@1: moel@1: public string ValueToString(float? value) { moel@122: if (value.HasValue) { moel@122: if (sensor.SensorType == SensorType.Temperature && moel@165: unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit) { moel@122: return string.Format("{0:F1} °F", value * 1.8 + 32); moel@122: } else { moel@122: return string.Format(format, value); moel@122: } moel@122: } else moel@1: return "-"; moel@1: } moel@1: moel@165: public SensorNode(ISensor sensor, PersistentSettings settings, moel@165: UnitManager unitManager) : base() { moel@1: this.sensor = sensor; moel@165: this.settings = settings; moel@165: this.unitManager = unitManager; moel@1: switch (sensor.SensorType) { moel@287: case SensorType.Voltage: format = "{0:F3} V"; break; moel@1: case SensorType.Clock: format = "{0:F0} MHz"; break; moel@24: case SensorType.Load: format = "{0:F1} %"; break; moel@1: case SensorType.Temperature: format = "{0:F1} °C"; break; moel@1: case SensorType.Fan: format = "{0:F0} RPM"; break; moel@57: case SensorType.Flow: format = "{0:F0} L/h"; break; moel@118: case SensorType.Control: format = "{0:F1} %"; break; moel@217: case SensorType.Level: format = "{0:F1} %"; break; moel@317: case SensorType.Power: format = "{0:F1} W"; break; moel@324: case SensorType.Data: format = "{0:F1} GB"; break; moel@340: case SensorType.Factor: format = "{0:F3}"; break; moel@111: } moel@111: moel@166: bool hidden = settings.GetValue(new Identifier(sensor.Identifier, moel@111: "hidden").ToString(), sensor.IsDefaultHidden); moel@126: base.IsVisible = !hidden; moel@327: moel@327: this.Plot = settings.GetValue(new Identifier(sensor.Identifier, moel@327: "plot").ToString(), false); moel@1: } moel@1: moel@1: public override string Text { moel@1: get { return sensor.Name; } moel@1: set { sensor.Name = value; } moel@111: } moel@111: moel@111: public override bool IsVisible { moel@111: get { return base.IsVisible; } moel@111: set { moel@111: base.IsVisible = value; moel@166: settings.SetValue(new Identifier(sensor.Identifier, moel@111: "hidden").ToString(), !value); moel@111: } moel@111: } moel@1: moel@1: public bool Plot { moel@1: get { return plot; } moel@327: set { moel@327: plot = value; moel@327: settings.SetValue(new Identifier(sensor.Identifier, "plot").ToString(), moel@327: value); moel@327: if (PlotSelectionChanged != null) moel@327: PlotSelectionChanged(this, null); moel@327: } moel@1: } moel@1: moel@327: public event EventHandler PlotSelectionChanged; moel@327: moel@1: public ISensor Sensor { moel@1: get { return sensor; } moel@1: } moel@1: moel@1: public string Value { moel@1: get { return ValueToString(sensor.Value); } moel@1: } moel@1: moel@1: public string Min { moel@1: get { return ValueToString(sensor.Min); } moel@1: } moel@1: moel@1: public string Max { moel@1: get { return ValueToString(sensor.Max); } moel@1: } moel@1: moel@1: public override bool Equals(System.Object obj) { moel@1: if (obj == null) moel@1: return false; moel@1: moel@1: SensorNode s = obj as SensorNode; moel@1: if (s == null) moel@1: return false; moel@1: moel@1: return (sensor == s.sensor); moel@1: } moel@1: moel@1: public override int GetHashCode() { moel@1: return sensor.GetHashCode(); moel@1: } moel@1: moel@1: } moel@1: }