Refactored the hardware monitoring code into a library (Issue 101).

This commit is contained in:
Michael Möller
2010-08-08 13:57:26 +00:00
parent 9f90d4063b
commit a2650ba983
59 changed files with 698 additions and 548 deletions

View File

@@ -43,14 +43,41 @@ using OpenHardwareMonitor.Hardware;
namespace OpenHardwareMonitor.GUI {
public class HardwareNode : Node {
private PersistentSettings settings;
private UnitManager unitManager;
private IHardware hardware;
private List<TypeNode> typeNodes = new List<TypeNode>();
public HardwareNode(IHardware hardware) : base(hardware.Name) {
public HardwareNode(IHardware hardware, PersistentSettings settings,
UnitManager unitManager) : base(hardware.Name)
{
this.settings = settings;
this.unitManager = unitManager;
this.hardware = hardware;
this.Image = hardware.Icon;
switch (hardware.HardwareType) {
case HardwareType.CPU:
this.Image = Utilities.EmbeddedResources.GetImage("cpu.png");
break;
case HardwareType.GPU:
if (hardware.Identifier.ToString().Contains("nvidia"))
this.Image = Utilities.EmbeddedResources.GetImage("nvidia.png");
else
this.Image = Utilities.EmbeddedResources.GetImage("ati.png");
break;
case HardwareType.HDD:
this.Image = Utilities.EmbeddedResources.GetImage("hdd.png");
break;
case HardwareType.Mainboard:
this.Image = Utilities.EmbeddedResources.GetImage("mainboard.png");
break;
case HardwareType.SuperIO:
this.Image = Utilities.EmbeddedResources.GetImage("chip.png");
break;
case HardwareType.TBalancer:
this.Image = Utilities.EmbeddedResources.GetImage("bigng.png");
break;
}
typeNodes.Add(new TypeNode(SensorType.Voltage));
typeNodes.Add(new TypeNode(SensorType.Clock));
@@ -105,7 +132,7 @@ namespace OpenHardwareMonitor.GUI {
while (i < node.Nodes.Count &&
((SensorNode)node.Nodes[i]).Sensor.Index < sensor.Index)
i++;
SensorNode sensorNode = new SensorNode(sensor);
SensorNode sensorNode = new SensorNode(sensor, settings, unitManager);
node.Nodes.Insert(i, sensorNode);
}

View File

@@ -51,7 +51,9 @@ using OpenHardwareMonitor.Utilities;
namespace OpenHardwareMonitor.GUI {
public partial class MainForm : Form {
private Computer computer = new Computer();
private PersistentSettings settings;
private UnitManager unitManager;
private Computer computer;
private Node root;
private TreeModel treeModel;
private IDictionary<ISensor, Color> sensorPlotColors =
@@ -74,6 +76,12 @@ namespace OpenHardwareMonitor.GUI {
public MainForm() {
InitializeComponent();
this.settings = new PersistentSettings();
this.settings.Load(Path.ChangeExtension(
System.Windows.Forms.Application.ExecutablePath, ".config"));
this.unitManager = new UnitManager(settings);
// set the DockStyle here, to avoid conflicts with the MainMenu
this.splitContainer.Dock = DockStyle.Fill;
@@ -98,10 +106,10 @@ namespace OpenHardwareMonitor.GUI {
nodeTextBoxMax.DrawText += nodeTextBoxText_DrawText;
nodeTextBoxText.EditorShowing += nodeTextBoxText_EditorShowing;
if (Utilities.Config.Contains("mainForm.Location.X")) {
int x = Utilities.Config.Get("mainForm.Location.X", Location.X);
if (settings.Contains("mainForm.Location.X")) {
int x = settings.Get("mainForm.Location.X", Location.X);
x = x < 0 ? 0 : x;
int y = Utilities.Config.Get("mainForm.Location.Y", Location.Y);
int y = settings.Get("mainForm.Location.Y", Location.Y);
y = y < 0 ? 0 : y;
this.Location = new Point(x, y);
} else {
@@ -109,12 +117,12 @@ namespace OpenHardwareMonitor.GUI {
}
ClientSize = new Size(
Utilities.Config.Get("mainForm.Width", 470),
Utilities.Config.Get("mainForm.Height", 640));
settings.Get("mainForm.Width", 470),
settings.Get("mainForm.Height", 640));
foreach (TreeColumn column in treeView.Columns)
column.Width = Math.Max(20, Math.Min(400,
Config.Get("treeView.Columns." + column.Header + ".Width",
column.Width = Math.Max(20, Math.Min(400,
settings.Get("treeView.Columns." + column.Header + ".Width",
column.Width)));
treeModel = new TreeModel();
@@ -122,9 +130,11 @@ namespace OpenHardwareMonitor.GUI {
root.Image = Utilities.EmbeddedResources.GetImage("computer.png");
treeModel.Nodes.Add(root);
treeView.Model = treeModel;
treeView.Model = treeModel;
systemTray = new SystemTray(computer);
this.computer = new Computer(settings);
systemTray = new SystemTray(computer, settings);
systemTray.HideShowCommand += hideShowClick;
systemTray.ExitCommand += exitClick;
@@ -149,52 +159,52 @@ namespace OpenHardwareMonitor.GUI {
plotColorPalette[11] = Color.Olive;
plotColorPalette[12] = Color.Firebrick;
showHiddenSensors = new UserOption("hiddenMenuItem", false, hiddenMenuItem);
showHiddenSensors = new UserOption("hiddenMenuItem", false, hiddenMenuItem, settings);
showHiddenSensors.Changed += delegate(object sender, EventArgs e) {
treeModel.ForceVisible = showHiddenSensors.Value;
};
showPlot = new UserOption("plotMenuItem", false, plotMenuItem);
showPlot = new UserOption("plotMenuItem", false, plotMenuItem, settings);
showPlot.Changed += delegate(object sender, EventArgs e) {
splitContainer.Panel2Collapsed = !showPlot.Value;
treeView.Invalidate();
};
showValue = new UserOption("valueMenuItem", true, valueMenuItem);
showValue = new UserOption("valueMenuItem", true, valueMenuItem, settings);
showValue.Changed += delegate(object sender, EventArgs e) {
treeView.Columns[1].IsVisible = showValue.Value;
};
showMin = new UserOption("minMenuItem", false, minMenuItem);
showMin = new UserOption("minMenuItem", false, minMenuItem, settings);
showMin.Changed += delegate(object sender, EventArgs e) {
treeView.Columns[2].IsVisible = showMin.Value;
};
showMax = new UserOption("maxMenuItem", true, maxMenuItem);
showMax = new UserOption("maxMenuItem", true, maxMenuItem, settings);
showMax.Changed += delegate(object sender, EventArgs e) {
treeView.Columns[3].IsVisible = showMax.Value;
};
startMinimized = new UserOption("startMinMenuItem", false, startMinMenuItem);
startMinimized = new UserOption("startMinMenuItem", false, startMinMenuItem, settings);
minimizeToTray = new UserOption("minTrayMenuItem", true, minTrayMenuItem);
minimizeToTray = new UserOption("minTrayMenuItem", true, minTrayMenuItem, settings);
minimizeToTray.Changed += delegate(object sender, EventArgs e) {
systemTray.IsMainIconEnabled = minimizeToTray.Value;
};
autoStart = new UserOption(null, startupManager.Startup, startupMenuItem);
autoStart = new UserOption(null, startupManager.Startup, startupMenuItem, settings);
autoStart.Changed += delegate(object sender, EventArgs e) {
startupManager.Startup = autoStart.Value; ;
};
readHddSensors = new UserOption("hddMenuItem", true, hddMenuItem);
readHddSensors = new UserOption("hddMenuItem", true, hddMenuItem, settings);
readHddSensors.Changed += delegate(object sender, EventArgs e) {
computer.HDDEnabled = readHddSensors.Value;
UpdatePlotSelection(null, null);
};
celciusMenuItem.Checked =
UnitManager.TemperatureUnit == TemperatureUnit.Celcius;
unitManager.TemperatureUnit == TemperatureUnit.Celcius;
fahrenheitMenuItem.Checked = !celciusMenuItem.Checked;
startupMenuItem.Visible = startupManager.IsAvailable;
@@ -219,14 +229,14 @@ namespace OpenHardwareMonitor.GUI {
}
private void SubHardwareAdded(IHardware hardware, Node node) {
Node hardwareNode = new HardwareNode(hardware);
Node hardwareNode = new HardwareNode(hardware, settings, unitManager);
node.Nodes.Add(hardwareNode);
foreach (IHardware subHardware in hardware.SubHardware)
SubHardwareAdded(subHardware, hardwareNode);
}
private void HardwareAdded(IHardware hardware) {
Node hardwareNode = new HardwareNode(hardware);
Node hardwareNode = new HardwareNode(hardware, settings, unitManager);
root.Nodes.Add(hardwareNode);
foreach (IHardware subHardware in hardware.SubHardware)
SubHardwareAdded(subHardware, hardwareNode);
@@ -307,17 +317,18 @@ namespace OpenHardwareMonitor.GUI {
private void SaveConfiguration() {
if (WindowState != FormWindowState.Minimized) {
Config.Set("mainForm.Location.X", Location.X);
Config.Set("mainForm.Location.Y", Location.Y);
Config.Set("mainForm.Width", ClientSize.Width);
Config.Set("mainForm.Height", ClientSize.Height);
settings.Set("mainForm.Location.X", Location.X);
settings.Set("mainForm.Location.Y", Location.Y);
settings.Set("mainForm.Width", ClientSize.Width);
settings.Set("mainForm.Height", ClientSize.Height);
}
foreach (TreeColumn column in treeView.Columns)
Config.Set("treeView.Columns." + column.Header + ".Width",
settings.Set("treeView.Columns." + column.Header + ".Width",
column.Width);
Config.Save();
settings.Save(Path.ChangeExtension(
System.Windows.Forms.Application.ExecutablePath, ".config"));
}
private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
@@ -451,13 +462,13 @@ namespace OpenHardwareMonitor.GUI {
private void celciusMenuItem_Click(object sender, EventArgs e) {
celciusMenuItem.Checked = true;
fahrenheitMenuItem.Checked = false;
UnitManager.TemperatureUnit = TemperatureUnit.Celcius;
unitManager.TemperatureUnit = TemperatureUnit.Celcius;
}
private void fahrenheitMenuItem_Click(object sender, EventArgs e) {
celciusMenuItem.Checked = false;
fahrenheitMenuItem.Checked = true;
UnitManager.TemperatureUnit = TemperatureUnit.Fahrenheit;
unitManager.TemperatureUnit = TemperatureUnit.Fahrenheit;
}
private void sumbitReportMenuItem_Click(object sender, EventArgs e)

View File

@@ -41,7 +41,7 @@ using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using OpenHardwareMonitor.Hardware;
using OpenHardwareMonitor.Utilities;
using OpenHardwareMonitor.Collections;
namespace OpenHardwareMonitor.GUI {
public partial class ParameterForm : Form {

View File

@@ -44,13 +44,15 @@ namespace OpenHardwareMonitor.GUI {
public class SensorNode : Node {
private ISensor sensor;
private PersistentSettings settings;
private UnitManager unitManager;
private string format;
private bool plot = false;
private bool plot = false;
public string ValueToString(float? value) {
if (value.HasValue) {
if (sensor.SensorType == SensorType.Temperature &&
UnitManager.TemperatureUnit == TemperatureUnit.Fahrenheit) {
unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit) {
return string.Format("{0:F1} °F", value * 1.8 + 32);
} else {
return string.Format(format, value);
@@ -59,8 +61,11 @@ namespace OpenHardwareMonitor.GUI {
return "-";
}
public SensorNode(ISensor sensor) : base() {
public SensorNode(ISensor sensor, PersistentSettings settings,
UnitManager unitManager) : base() {
this.sensor = sensor;
this.settings = settings;
this.unitManager = unitManager;
switch (sensor.SensorType) {
case SensorType.Voltage: format = "{0:F2} V"; break;
case SensorType.Clock: format = "{0:F0} MHz"; break;
@@ -71,7 +76,7 @@ namespace OpenHardwareMonitor.GUI {
case SensorType.Control: format = "{0:F1} %"; break;
}
bool hidden = Config.Get(new Identifier(sensor.Identifier,
bool hidden = settings.Get(new Identifier(sensor.Identifier,
"hidden").ToString(), sensor.IsDefaultHidden);
base.IsVisible = !hidden;
}
@@ -85,7 +90,7 @@ namespace OpenHardwareMonitor.GUI {
get { return base.IsVisible; }
set {
base.IsVisible = value;
Config.Set(new Identifier(sensor.Identifier,
settings.Set(new Identifier(sensor.Identifier,
"hidden").ToString(), !value);
}
}

View File

@@ -62,7 +62,7 @@ namespace OpenHardwareMonitor.GUI {
private Font font;
public SensorNotifyIcon(SystemTray sensorSystemTray, ISensor sensor,
bool balloonTip)
bool balloonTip, PersistentSettings settings)
{
this.sensor = sensor;
this.notifyIcon = new NotifyIcon();
@@ -71,7 +71,7 @@ namespace OpenHardwareMonitor.GUI {
if (sensor.SensorType == SensorType.Load) {
defaultColor = Color.FromArgb(0xff, 0x70, 0x8c, 0xf1);
}
Color = Config.Get(new Identifier(sensor.Identifier,
Color = settings.Get(new Identifier(sensor.Identifier,
"traycolor").ToString(), defaultColor);
this.pen = new Pen(Color.FromArgb(96, Color.Black));
@@ -95,7 +95,7 @@ namespace OpenHardwareMonitor.GUI {
dialog.Color = Color;
if (dialog.ShowDialog() == DialogResult.OK) {
Color = dialog.Color;
Config.Set(new Identifier(sensor.Identifier,
settings.Set(new Identifier(sensor.Identifier,
"traycolor").ToString(), Color);
}
};

View File

@@ -46,12 +46,14 @@ using OpenHardwareMonitor.Utilities;
namespace OpenHardwareMonitor.GUI {
public class SystemTray : IDisposable {
private IComputer computer;
private PersistentSettings settings;
private List<SensorNotifyIcon> list = new List<SensorNotifyIcon>();
private bool mainIconEnabled = false;
private NotifyIcon mainIcon;
public SystemTray(IComputer computer) {
public SystemTray(IComputer computer, PersistentSettings settings) {
this.computer = computer;
this.settings = settings;
computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
@@ -95,7 +97,7 @@ namespace OpenHardwareMonitor.GUI {
}
private void SensorAdded(ISensor sensor) {
if (Config.Get(new Identifier(sensor.Identifier,
if (settings.Get(new Identifier(sensor.Identifier,
"tray").ToString(), false))
Add(sensor, false);
}
@@ -127,9 +129,9 @@ namespace OpenHardwareMonitor.GUI {
if (Contains(sensor)) {
return;
} else {
list.Add(new SensorNotifyIcon(this, sensor, balloonTip));
list.Add(new SensorNotifyIcon(this, sensor, balloonTip, settings));
UpdateMainIconVisibilty();
Config.Set(new Identifier(sensor.Identifier, "tray").ToString(), true);
settings.Set(new Identifier(sensor.Identifier, "tray").ToString(), true);
}
}
@@ -139,9 +141,9 @@ namespace OpenHardwareMonitor.GUI {
private void Remove(ISensor sensor, bool deleteConfig) {
if (deleteConfig) {
Config.Remove(
settings.Remove(
new Identifier(sensor.Identifier, "tray").ToString());
Config.Remove(
settings.Remove(
new Identifier(sensor.Identifier, "traycolor").ToString());
}
SensorNotifyIcon instance = null;

View File

@@ -37,7 +37,6 @@
using System;
using System.Collections.Generic;
using OpenHardwareMonitor.Utilities;
namespace OpenHardwareMonitor.GUI {
@@ -48,18 +47,20 @@ namespace OpenHardwareMonitor.GUI {
public class UnitManager {
private static TemperatureUnit temperatureUnit;
private PersistentSettings settings;
private TemperatureUnit temperatureUnit;
static UnitManager () {
temperatureUnit = (TemperatureUnit)Config.Get("TemperatureUnit",
public UnitManager(PersistentSettings settings) {
this.settings = settings;
this.temperatureUnit = (TemperatureUnit)settings.Get("TemperatureUnit",
(int)TemperatureUnit.Celcius);
}
public static TemperatureUnit TemperatureUnit {
public TemperatureUnit TemperatureUnit {
get { return temperatureUnit; }
set {
temperatureUnit = value;
Config.Set("TemperatureUnit", (int)temperatureUnit);
this.temperatureUnit = value;
this.settings.Set("TemperatureUnit", (int)temperatureUnit);
}
}
}

View File

@@ -46,13 +46,15 @@ namespace OpenHardwareMonitor.GUI {
private bool value;
private MenuItem menuItem;
private event EventHandler changed;
private PersistentSettings settings;
public UserOption(string name, bool value,
MenuItem menuItem) {
MenuItem menuItem, PersistentSettings settings) {
this.settings = settings;
this.name = name;
if (name != null)
this.value = Config.Get(name, value);
this.value = settings.Get(name, value);
else
this.value = value;
this.menuItem = menuItem;
@@ -70,7 +72,7 @@ namespace OpenHardwareMonitor.GUI {
if (this.value != value) {
this.value = value;
if (this.name != null)
Config.Set(name, value);
settings.Set(name, value);
this.menuItem.Checked = value;
if (changed != null)
changed(this, null);