using DevExpress.XtraReports.Design;
using GeoSigma.SigmaDrawerStyle.Converter;
using GeoSigmaDrawLib;
using SigmaDrawerStyle;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using static SigmaDrawerElement.DrawerScaleRuler;
namespace SigmaDrawerElement
{
///
/// 坐标边框类.
///
public class DrawerScaleRuler : ElementBase
{
///
/// Initializes a new instance of the class.
private const string ScaleRulerCategory = "\t\t标尺";
private const string titleCategory = "标题";
///
public DrawerScaleRuler()
{
ElementType = DrawElementType.ELEMENT_SCALERULER;
}
#region Coordinate
[Browsable(false)]
[XmlElement("Coordinate")]
public DrawerCoordinateLBTR Coordinate { get; set; } = new DrawerCoordinateLBTR();
[XmlIgnore]
[Category(ScaleRulerCategory), DisplayName("\t\t\t坐标范围")]
[PropertyOrder(0)]
[TypeConverter(typeof(GeoSigma.SigmaDrawerStyle.Converter.BaseExpandableConverter))]
public DrawerRange RangeDelta
{
get
{
return Coordinate.RangeDelta;
}
set
{
Coordinate.Create(value.Left, value.Right, value.Bottom, value.Top);
}
}
//public SizeF sizeDelta;
//[XmlIgnore]
//[Category(ScaleRulerCategory), DisplayName("\t\t尺寸")]//, ReadOnlyAttribte(true)
//public SizeF SizeDelta
//{
// get
// {
// sizeDelta = new SizeF((float)Coordinate.SizeDelta.Width, (float)Coordinate.SizeDelta.Height);
// return sizeDelta;
// }
// private set
// {
// }
//}
[XmlIgnore]
[Category(ScaleRulerCategory), DisplayName("步长")]
[PropertyOrder(1)]
public double Step
{
get => axisProperty.MajorGradStep;
set => axisProperty.MajorGradStep = value;
}
[XmlIgnore]
[Category(ScaleRulerCategory), DisplayName("副刻度个数")]
[PropertyOrder(2)]
public int MinorGridCount
{
get => axisProperty.MinorGridCount;
set => axisProperty.MinorGridCount = value;
}
[XmlIgnore]
[Category(ScaleRulerCategory), DisplayName("显示副刻度")]
[TypeConverter(typeof(YesNoConverter))]
[PropertyOrder(3)]
public bool ShowMinorGrid
{
get
{
int ivalue = Convert.ToInt32(axisProperty.Style, 16);
return (ivalue & 0x0080) != 0;
}
set
{
int ivalue = Convert.ToInt32(axisProperty.Style, 16);
if (value)
{
ivalue = ivalue | 0x0080;
}
else
{
ivalue = ivalue &~0x0080;
}
axisProperty.Style = ivalue.ToString("X");
}
}
[XmlIgnore]
[Category(ScaleRulerCategory), DisplayName("刻度位置")]
[TypeConverter(typeof(DsrPosConverter))]
[PropertyOrder(4)]
public int posId
{
get
{
int ivalue = Convert.ToInt32(axisProperty.Style, 16);
int ipos = 0;
if ((ivalue & 0x0002) == 0)
ipos = 1;
return ipos;
}
set
{
int ipos = value;
int ivalue = Convert.ToInt32(axisProperty.Style, 16);
ivalue = ivalue & ~(0x0002 | 0x0004); //先去掉左右位置属性
if (ipos ==0)
{//左侧位置
ivalue = ivalue|0x0002;
}
else
{//右侧位置
ivalue = ivalue | 0x0004;
}
axisProperty.Style = ivalue.ToString("X");
}
}
[XmlIgnore]
[Category(ScaleRulerCategory), DisplayName("刻度文本高度")]
[PropertyOrder(5)]
public double gradFontHeight
{
get => gradFont.Font.Height;
set
{
gradFont.Font.Height = value;
gradFont.Font.Width = gradFont.Font.Height * 0.4;
}
}
[XmlIgnore]
[Category(titleCategory), DisplayName("标尺标题")]
[PropertyOrder(7)]
public string title
{
get => axisProperty.Title;
set => axisProperty.Title = value;
}
[XmlIgnore]
[Category(titleCategory), DisplayName("文本高度")]
[PropertyOrder(8)]
public double titleFontHeight
{
get => titleFont.Font.Height;
set => titleFont.Font.Height = value;
}
#endregion
#region Other
[Browsable(false)]
[XmlElement("Property")]
public AxisProperty axisProperty { get; set; }
[Browsable(false)]
[XmlElement("GradFont")]
public FontWrapper gradFont { get; set; }
[Browsable(false)]
[XmlElement("TitleFont")]
public FontWrapper titleFont { get; set; }
[Browsable(false)]
[XmlElement("MajorGradPen")]
public DsrLogPen majorPen { get; set; }
[Browsable(false)]
[XmlElement("MinorGradPen")]
public DsrLogPen minorPen { get; set; }
#endregion
}
public class AxisProperty
{
[XmlAttribute("Title")]
public string Title { get; set; }
[XmlAttribute("IsEnable")]
public int IsEnable { get; set; } // 0/1 布尔值
[XmlAttribute("IsVisible")]
public int IsVisible { get; set; } // 0/1 布尔值
[XmlAttribute("Style")]
public string Style { get; set; } // 十六进制字符串如 "31A4"
[XmlAttribute("Direction")]
public int Direction { get; set; }
[XmlAttribute("MinScale")]
public double MinScale { get; set; }
[XmlAttribute("MaxScale")]
public double MaxScale { get; set; }
[XmlAttribute("MajorGridCount")]
public int MajorGridCount { get; set; }
[XmlAttribute("MinorGridCount")]
public int MinorGridCount { get; set; }
[XmlAttribute("MajorGradStep")]
public double MajorGradStep { get; set; }
[XmlAttribute("TickLength")]
public double TickLength { get; set; }
[XmlAttribute("GradTextAngle")]
public double GradTextAngle { get; set; }
[XmlAttribute("AxisAngle")]
public double AxisAngle { get; set; }
[XmlAttribute("ScaleFormat")]
public int ScaleFormat { get; set; }
[XmlAttribute("TitleShowStyle")]
public int TitleShowStyle { get; set; }
[XmlAttribute("strUnit")]
public string Unit { get; set; } // 重命名为更符合C#习惯的属性名
}
public class FontWrapper
{
[XmlElement("Font")]
public DsrFontInfo Font { get; set; }
}
public class DsrFontInfo
{
[XmlAttribute("W")]
public double Width { get; set; }
[XmlAttribute("H")]
public double Height { get; set; }
[XmlAttribute("B")]
public int Bold { get; set; } // 400=Normal, 700=Bold
[XmlAttribute("I")]
public int Italic { get; set; } // 0=False, 1=True
[XmlAttribute("U")]
public int Underline { get; set; } // 0=False, 1=True
[XmlAttribute("S")]
public int Strikeout { get; set; } // 0=False, 1=True
[XmlAttribute("PF")]
public int PitchAndFamily { get; set; }
[XmlAttribute("FN")]
public string FontName { get; set; }
[XmlAttribute("C")]
public string ColorHex { get; set; } // "#000000"
[XmlAttribute("SP")]
public int CharSet { get; set; }
///
/// 转换为 System.Drawing.Font(需GDI+)
///
//public Font ToDrawingFont()
//{
// var style = FontStyle.Regular;
// if (Bold >= 700) style |= FontStyle.Bold;
// if (Italic == 1) style |= FontStyle.Italic;
// if (Underline == 1) style |= FontStyle.Underline;
// if (Strikeout == 1) style |= FontStyle.Strikeout;
// return new Font(FontName, Height / 20f, style); // Height单位需转换
//}
}
public class DsrPenWrapper
{
[XmlElement("LogPen")]
public DsrLogPen LogPen { get; set; }
}
public class DsrLogPen
{
[XmlAttribute("Width")]
public double Width { get; set; }
[XmlAttribute("Color")]
public string ColorHex { get; set; } // "#000000"
[XmlAttribute("Style")]
public string Style { get; set; } // "Solid", "Dash"等
}
public class DsrPosConverter : TypeConverter
{
private static readonly Dictionary ValueMap = new Dictionary() // (StringComparer.OrdinalIgnoreCase)
{//以后还会添加上,下
{ 0, "左" },
{ 1, "右" },
};
private static readonly Dictionary ReverseMap =
ValueMap.GroupBy(kvp => kvp.Value).ToDictionary(g => g.Key, g => g.First().Key);
public override bool GetStandardValuesSupported(ITypeDescriptorContext context) => true;
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) => true;
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(new[] { "左", "右" });
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)//进行逆向转换时要重新写这个函数
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value is int iValue)
{
// 将内部值映射为显示值
if (ValueMap.TryGetValue(iValue, out var displayValue))
{
return displayValue;
}
return iValue; // 如果没有映射,返回原值
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string stringValue)
{
// 将显示值映射回内部值
if (ReverseMap.TryGetValue(stringValue, out var internalValue))
{
return internalValue;
}
return stringValue; // 如果没有映射,返回原值
}
return base.ConvertFrom(context, culture, value);
}
}
}