Server/DisplaySettings.cs
author StephaneLenclud
Wed, 04 Feb 2015 17:44:25 +0100
changeset 104 189aac7dd3d6
parent 57 544132d07c3b
child 106 32270ff62819
permissions -rw-r--r--
Adding events for display closed/opened.
Display types now loaded dynamically from our MiniDisplay library.
Adding editor config.
Tested against Futaba GP1212A02.
     1 using System;
     2 using System.Collections;
     3 using System.Collections.Generic;
     4 using System.Text;
     5 using System.Configuration;
     6 using System.Xml;
     7 using System.ComponentModel;
     8 using System.Runtime.Serialization;
     9 using System.Runtime.Serialization.Json;
    10 using System.IO;
    11 using System.Drawing;
    12 
    13 namespace SharpDisplayManager
    14 {
    15     /// <summary>
    16     /// Display settings for the specified hardware type
    17     /// </summary>
    18     [DataContract]
    19     public class DisplaySettings
    20     {
    21         public DisplaySettings()
    22         {
    23             Brightness = 1;
    24             DisplayType = 0;
    25             TimerInterval = 150;
    26             ReverseScreen = false;
    27             InverseColors = true;
    28             ShowBorders = false;
    29             FontName = "Microsoft Sans Serif, 9.75pt";
    30             ScaleToFit = true;
    31             MinFontSize = 15.0f;
    32             Separator = "    ";
    33         }
    34 
    35         [DataMember]
    36         public int Brightness { get; set; }
    37 
    38         /// <summary>
    39         /// See Display.TMiniDisplayType
    40         /// </summary>
    41         [DataMember]
    42         public int DisplayType { get; set; }
    43 
    44         [DataMember]
    45         public int TimerInterval { get; set; }
    46 
    47         [DataMember]
    48         public bool ReverseScreen { get; set; }
    49 
    50         [DataMember]
    51         public bool InverseColors { get; set; }
    52 
    53         [DataMember]
    54         public bool ShowBorders { get; set; }
    55 
    56         [DataMember]
    57         public bool ScaleToFit { get; set; }
    58 
    59         [DataMember]
    60         public float MinFontSize { get; set; }
    61 
    62         [DataMember]
    63         public string Separator { get; set; }
    64 
    65         [DataMember]
    66         public string FontName { get; set; }
    67 
    68         public Font Font
    69         {
    70             get
    71             {
    72                 FontConverter cvt = new FontConverter();
    73                 Font font = cvt.ConvertFromInvariantString(FontName) as Font;
    74                 return font;
    75             }
    76 
    77             set
    78             {
    79                 FontConverter cvt = new FontConverter();
    80                 FontName = cvt.ConvertToInvariantString(value);
    81             }
    82         }
    83     };
    84 
    85 
    86     /// <summary>
    87     /// Contain settings for each of our display type.
    88     /// </summary>
    89     [TypeConverter(typeof(DisplaySettingsConverter))]
    90     [DataContract]
    91     public class DisplaysSettings
    92     {
    93         public DisplaysSettings()
    94         {
    95             Init();
    96         }
    97 
    98         public void Init()
    99         {
   100             if (Displays == null)
   101             {
   102                 Displays = new List<DisplaySettings>();
   103             }
   104         }
   105 
   106         //[DataMember]
   107         //public int CurrentSettingsIndex { get; set; }
   108 
   109         [DataMember]
   110         public List<DisplaySettings> Displays { get; set; }
   111 
   112         public override string ToString()
   113         {
   114             //Save settings into JSON string
   115             MemoryStream stream = new MemoryStream();
   116             DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DisplaysSettings));
   117             ser.WriteObject(stream, this);
   118             // convert stream to string
   119             stream.Position = 0;
   120             StreamReader reader = new StreamReader(stream);
   121             string text = reader.ReadToEnd();
   122             return text;
   123         }
   124     }
   125 
   126     public class DisplaySettingsConverter : TypeConverter
   127     {
   128         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
   129         {
   130             if (sourceType == typeof(string))
   131                 return true;
   132             else
   133                 return base.CanConvertFrom(context, sourceType);
   134         }
   135 
   136         public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
   137         {
   138             string stringValue = value as string;
   139             if (stringValue != null)
   140             {
   141                 //Load settings form JSON string
   142                 byte[] byteArray = Encoding.UTF8.GetBytes(stringValue);
   143                 MemoryStream stream = new MemoryStream(byteArray);
   144                 DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DisplaysSettings));
   145                 DisplaysSettings settings = (DisplaysSettings)ser.ReadObject(stream);
   146                 settings.Init();
   147                 return settings;
   148             }
   149             else
   150                 return base.ConvertFrom(context, culture, value);
   151         }
   152     };
   153 
   154 
   155 }
   156