GUI/SensorFrontView.cs
author StephaneLenclud
Sat, 13 Apr 2013 00:43:25 +0200
changeset 396 21a9e2325617
parent 391 ca4c0e7ae75d
child 397 2dc91822f312
permissions -rw-r--r--
Sensors can now be displayed in 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.Drawing;
    13 using System.Drawing.Drawing2D;
    14 using System.Drawing.Imaging;
    15 using System.Drawing.Text;
    16 using System.Runtime.InteropServices;
    17 using System.Windows.Forms;
    18 using OpenHardwareMonitor.Hardware;
    19 using OpenHardwareMonitor.Utilities;
    20 
    21 namespace OpenHardwareMonitor.GUI
    22 {
    23     public class SensorFrontView : IDisposable
    24     {
    25 
    26         private UnitManager unitManager;
    27 
    28         private ISensor sensor;
    29         private Color color;
    30         private Color darkColor;
    31         private Font font;
    32         private Font smallFont;
    33         public string iFirstLine;
    34         public string iSecondLine;
    35 
    36 
    37         public SensorFrontView(SoundGraphDisplay soundGraphDisplay, ISensor sensor,
    38           bool balloonTip, PersistentSettings settings, UnitManager unitManager)
    39         {
    40             this.unitManager = unitManager;
    41             this.sensor = sensor;
    42        
    43             // get the default dpi to create an icon with the correct size
    44             float dpiX, dpiY;
    45             using (Bitmap b = new Bitmap(1, 1, PixelFormat.Format32bppArgb))
    46             {
    47                 dpiX = b.HorizontalResolution;
    48                 dpiY = b.VerticalResolution;
    49             }
    50 
    51             // adjust the size of the icon to current dpi (default is 16x16 at 96 dpi) 
    52             int width = (int)Math.Round(16 * dpiX / 96);
    53             int height = (int)Math.Round(16 * dpiY / 96);
    54 
    55             // make sure it does never get smaller than 16x16
    56             width = width < 16 ? 16 : width;
    57             height = height < 16 ? 16 : height;
    58 
    59             // adjust the font size to the icon size
    60             FontFamily family = SystemFonts.MessageBoxFont.FontFamily;
    61             float baseSize;
    62             switch (family.Name)
    63             {
    64                 case "Segoe UI": baseSize = 12; break;
    65                 case "Tahoma": baseSize = 11; break;
    66                 default: baseSize = 12; break;
    67             }
    68 
    69             this.font = new Font(family,
    70               baseSize * width / 16.0f, GraphicsUnit.Pixel);
    71             this.smallFont = new Font(family,
    72               0.75f * baseSize * width / 16.0f, GraphicsUnit.Pixel);
    73 
    74         }
    75 
    76         public ISensor Sensor
    77         {
    78             get { return sensor; }
    79         }
    80 
    81         public Color Color
    82         {
    83             get { return color; }
    84             set
    85             {
    86                 this.color = value;
    87                 this.darkColor = Color.FromArgb(255,
    88                   this.color.R / 3,
    89                   this.color.G / 3,
    90                   this.color.B / 3);
    91             }
    92         }
    93 
    94         public void Dispose()
    95         {
    96             font.Dispose();
    97             smallFont.Dispose();
    98         }
    99 
   100         public string GetString()
   101         {
   102             if (!sensor.Value.HasValue)
   103                 return "-";
   104 
   105             switch (sensor.SensorType)
   106             {
   107                 case SensorType.Voltage:
   108                     return string.Format("{0:F1}", sensor.Value);
   109                 case SensorType.Clock:
   110                     return string.Format("{0:F1}", 1e-3f * sensor.Value);
   111                 case SensorType.Load:
   112                     return string.Format("{0:F0}", sensor.Value);
   113                 case SensorType.Temperature:
   114                     if (unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit)
   115                         return string.Format("{0:F0}",
   116                           UnitManager.CelsiusToFahrenheit(sensor.Value));
   117                     else
   118                         return string.Format("{0:F0}", sensor.Value);
   119                 case SensorType.Fan:
   120                     return string.Format("{0:F1}", 1e-3f * sensor.Value);
   121                 case SensorType.Flow:
   122                     return string.Format("{0:F1}", 1e-3f * sensor.Value);
   123                 case SensorType.Control:
   124                     return string.Format("{0:F0}", sensor.Value);
   125                 case SensorType.Level:
   126                     return string.Format("{0:F0}", sensor.Value);
   127                 case SensorType.Power:
   128                     return string.Format("{0:F0}", sensor.Value);
   129                 case SensorType.Data:
   130                     return string.Format("{0:F0}", sensor.Value);
   131                 case SensorType.Factor:
   132                     return string.Format("{0:F1}", sensor.Value);
   133             }
   134             return "-";
   135         }
   136 
   137 
   138         public void Update()
   139         {
   140  
   141 
   142             switch (sensor.SensorType)
   143             {
   144                 case SensorType.Load:
   145                 case SensorType.Control:
   146                 case SensorType.Level:
   147                     //notifyIcon.Icon = CreatePercentageIcon();
   148                     break;
   149                 default:
   150                     //notifyIcon.Icon = CreateTransparentIcon();
   151                     break;
   152             }
   153 
   154 
   155             string format = "";
   156             switch (sensor.SensorType)
   157             {
   158                 case SensorType.Voltage: format = "{0}: {1:F2} V"; break;
   159                 case SensorType.Clock: format = "{0}: {1:F0} MHz"; break;
   160                 case SensorType.Load: format = "{0}: {1:F1} %"; break;
   161                 case SensorType.Temperature: format = "{0}: {1:F1} °C"; break;
   162                 case SensorType.Fan: format = "{0}: {1:F0} RPM"; break;
   163                 case SensorType.Flow: format = "{0}: {1:F0} L/h"; break;
   164                 case SensorType.Control: format = "{0}: {1:F1} %"; break;
   165                 case SensorType.Level: format = "{0}: {1:F1} %"; break;
   166                 case SensorType.Power: format = "{0}: {1:F0} W"; break;
   167                 case SensorType.Data: format = "{0}: {1:F0} GB"; break;
   168                 case SensorType.Factor: format = "{0}: {1:F3} GB"; break;
   169             }
   170             string formattedValue = string.Format(format, sensor.Name, sensor.Value);
   171 
   172             if (sensor.SensorType == SensorType.Temperature &&
   173               unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit)
   174             {
   175                 format = "{0}: {1:F1} °F";
   176                 formattedValue = string.Format(format, sensor.Name,
   177                   UnitManager.CelsiusToFahrenheit(sensor.Value));
   178             }
   179 
   180             iFirstLine = sensor.Hardware.Name;
   181             iSecondLine = formattedValue;
   182 
   183         }
   184     }
   185 }