/*
Copyright (C) 2008 Webyog Softworks Private Limited
This file is a part of Visifire Charts.
Visifire is a free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
You should have received a copy of the GNU General Public License
along with Visifire Charts. If not, see .
If GPL is not suitable for your products or company, Webyog provides Visifire
under a flexible commercial license designed to meet your specific usage and
distribution requirements. If you have already obtained a commercial license
from Webyog, you can use this file under those license terms.
*/
using System;
using System.Windows;
using System.ComponentModel;
using Visifire.Charts;
using System.Globalization;
using System.Reflection;
using System.Collections.Generic;
namespace Visifire.Commons
{
///
/// Visifire.Commons.Converters class
///
public class Converters
{
///
/// Value converter
///
public class ValueConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return true;
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if ((String)value == String.Empty)
return Double.NaN;
else
return Double.Parse((String)value, System.Globalization.CultureInfo.InvariantCulture);
}
}
///
/// Double array converter
///
public class DoubleArrayConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return true;
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
Double[] result;
if ((String)value == String.Empty)
result = new Double[4] {0, 0, 0, 0};
else
{
String[] split = value.ToString().Split(',');
result = new Double[4];
int i = 0;
foreach (String str in split)
{
result[i++] = Double.Parse(str, System.Globalization.CultureInfo.InvariantCulture);
}
}
return result;
}
}
#if SL
///
/// NullableDouble converter
///
public class NullableDoubleConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return true;
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
Nullable data = new Nullable(System.Convert.ToDouble(value,CultureInfo.InvariantCulture));
return data;
}
}
///
/// NullableInt32 converter
///
public class NullableInt32Converter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return true;
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
Nullable data = new Nullable(System.Convert.ToInt32(value,System.Globalization.CultureInfo.InvariantCulture));
return data;
}
}
///
/// NullableThickness converter
///
public class NullableThicknessConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return true;
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
String[] thickness = value.ToString().Split(',');
Thickness markerBorderThickness;
if (thickness.Length == 1)
{
markerBorderThickness = new Thickness(Convert.ToDouble(thickness[0],System.Globalization.CultureInfo.InvariantCulture), Convert.ToDouble(thickness[0], System.Globalization.CultureInfo.InvariantCulture), Convert.ToDouble(thickness[0], System.Globalization.CultureInfo.InvariantCulture), Convert.ToDouble(thickness[0],System.Globalization.CultureInfo.InvariantCulture));
}
else if (thickness.Length == 4)
{
markerBorderThickness = new Thickness(Convert.ToDouble(thickness[0], System.Globalization.CultureInfo.InvariantCulture), Convert.ToDouble(thickness[1],System.Globalization.CultureInfo.InvariantCulture), Convert.ToDouble(thickness[2],System.Globalization.CultureInfo.InvariantCulture), Convert.ToDouble(thickness[3],System.Globalization.CultureInfo.InvariantCulture));
}
else
throw new NotSupportedException("Incorrect property value of property type Thickness");
return markerBorderThickness;
}
}
///
/// NullableLabelStyles converter
///
public class NullableLabelStylesConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return true;
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
Nullable data = new Nullable((LabelStyles)Enum.Parse(typeof(LabelStyles), value.ToString(), true));
return data;
}
}
///
/// NullableHrefTargets converter
///
public class NullableHrefTargetsConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return true;
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
Nullable data = new Nullable((HrefTargets)Enum.Parse(typeof(HrefTargets), value.ToString(), true));
return data;
}
}
///
/// NullableMarkerTypes converter
///
public class NullableMarkerTypesConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return true;
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
Nullable data = new Nullable((MarkerTypes)Enum.Parse(typeof(MarkerTypes), value.ToString(), true));
return data;
}
}
///
/// NullableLineStyles converter
///
public class NullableLineStylesConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return true;
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
Nullable data = new Nullable((LineStyles)Enum.Parse(typeof(LineStyles), value.ToString(), true));
return data;
}
}
///
/// NullableBorderStyles converter
///
public class NullableBorderStylesConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return true;
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
Nullable data = new Nullable((BorderStyles)Enum.Parse(typeof(BorderStyles), value.ToString(), true));
return data;
}
}
///
/// CornerRadius converter
///
public class CornerRadiusConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return true;
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
String[] radius = value.ToString().Split(',');
CornerRadius cornerRadius;
if (radius.Length == 1)
{
cornerRadius = new CornerRadius(Convert.ToDouble(radius[0], System.Globalization.CultureInfo.InvariantCulture), Convert.ToDouble(radius[0], System.Globalization.CultureInfo.InvariantCulture), Convert.ToDouble(radius[0], System.Globalization.CultureInfo.InvariantCulture), Convert.ToDouble(radius[0], System.Globalization.CultureInfo.InvariantCulture));
}
else if (radius.Length == 4)
{
cornerRadius = new CornerRadius(Convert.ToDouble(radius[0], System.Globalization.CultureInfo.InvariantCulture), Convert.ToDouble(radius[1], System.Globalization.CultureInfo.InvariantCulture), Convert.ToDouble(radius[2], System.Globalization.CultureInfo.InvariantCulture), Convert.ToDouble(radius[3], System.Globalization.CultureInfo.InvariantCulture));
}
else
throw new NotSupportedException("Incorrect property value of property CornerRadius");
return cornerRadius;
}
}
///
/// FontStyle converter
///
public class FontStyleConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return typeof(String) == sourceType;
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
String fontStyleString = value as String;
PropertyInfo fontProperty = typeof(FontStyles).GetProperty(fontStyleString);
FontStyleMethod fontStyleMethod = (FontStyleMethod)Delegate.CreateDelegate(typeof(FontStyleMethod), fontProperty.GetGetMethod());
return fontStyleMethod();
}
private delegate FontStyle FontStyleMethod();
}
///
/// FontWeight converter
///
public class FontWeightConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return typeof(String) == sourceType;
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
String fontWeightString = value as String;
PropertyInfo fontProperty = typeof(FontWeights).GetProperty(fontWeightString);
FontWeightMethod fontWeightMethod = (FontWeightMethod)Delegate.CreateDelegate(typeof(FontWeightMethod), fontProperty.GetGetMethod());
return fontWeightMethod();
}
private delegate FontWeight FontWeightMethod();
}
#endif
}
}