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