329 lines
14 KiB
C#
329 lines
14 KiB
C#
/*
|
|
* Copyright (c) 2022 LG Electronics Inc.
|
|
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
|
|
*/
|
|
using System;
|
|
using System.Globalization;
|
|
using System.Windows.Data;
|
|
|
|
namespace AuroraUIComponent.UIConverter
|
|
{
|
|
/// <summary>
|
|
/// 지정한 값 (CommandParameter)과의 비교
|
|
/// </summary>
|
|
public class EqualConverter : IValueConverter, IMultiValueConverter
|
|
{
|
|
public object TrueValue { get; set; } = true;
|
|
public object FalseValue { get; set; } = false;
|
|
|
|
public enum ConverterType { OBJECT, STRING, INT, DOUBLE, FLOAT}
|
|
public object CompareValue { get; set; }
|
|
public ConverterType CompareValueType { get; set; } = ConverterType.OBJECT;
|
|
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (CompareValue == null) { return value == null; }
|
|
|
|
switch (CompareValueType) {
|
|
case ConverterType.OBJECT:
|
|
return CompareValue.Equals(value) ? TrueValue : FalseValue;
|
|
case ConverterType.STRING:
|
|
return CompareValue.ToString().Equals(value.ToString()) ? TrueValue : FalseValue;
|
|
case ConverterType.INT:
|
|
return System.Convert.ToInt32(CompareValue).Equals(System.Convert.ToInt32(value)) ? TrueValue : FalseValue;
|
|
case ConverterType.DOUBLE:
|
|
return System.Convert.ToDouble(CompareValue).Equals(System.Convert.ToDouble(value)) ? TrueValue : FalseValue;
|
|
case ConverterType.FLOAT:
|
|
return System.Convert.ToSingle(CompareValue).Equals(System.Convert.ToSingle(value)) ? TrueValue : FalseValue;
|
|
default:
|
|
return CompareValue.Equals(value) ? TrueValue : FalseValue;
|
|
}
|
|
}
|
|
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (values == null || values.Length < 2) { throw new NotImplementedException(); }
|
|
if (values[0] == null || values[1] == null) { return FalseValue; }
|
|
|
|
CompareValue = values[1];
|
|
|
|
switch (CompareValueType) {
|
|
case ConverterType.OBJECT:
|
|
return CompareValue.Equals(values[0]) ? TrueValue : FalseValue;
|
|
case ConverterType.STRING:
|
|
return CompareValue.ToString().Equals(values[0].ToString()) ? TrueValue : FalseValue;
|
|
case ConverterType.INT:
|
|
return System.Convert.ToInt32(CompareValue).Equals(System.Convert.ToInt32(values[0])) ? TrueValue : FalseValue;
|
|
case ConverterType.DOUBLE:
|
|
return System.Convert.ToDouble(CompareValue).Equals(System.Convert.ToDouble(values[0])) ? TrueValue : FalseValue;
|
|
case ConverterType.FLOAT:
|
|
return System.Convert.ToSingle(CompareValue).Equals(System.Convert.ToSingle(values[0])) ? TrueValue : FalseValue;
|
|
default:
|
|
return CompareValue.Equals(values[0]) ? TrueValue : FalseValue;
|
|
}
|
|
|
|
}
|
|
|
|
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// MultiValueBinding으로 들어온 2개 value 간의 동일성 체크
|
|
/// </summary>
|
|
public class EqualConverter2 : IMultiValueConverter
|
|
{
|
|
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (values == null || values.Length < 2) { throw new NotImplementedException(); }
|
|
|
|
if (values[0] is string strValue0 && values[1] is string strValue1 && strValue0 == strValue1) {
|
|
return true;
|
|
}
|
|
if (values[0] is double doubleValue0 && values[1] is double doubleValue1 && doubleValue0 == doubleValue1) {
|
|
return true;
|
|
}
|
|
if (values[0] is float floatValue0 && values[1] is float floatValue1 && floatValue0 == floatValue1) {
|
|
return true;
|
|
}
|
|
if (values[0] is int intValue0 && values[1] is int intValue1 && intValue0 == intValue1) {
|
|
return true;
|
|
}
|
|
if (values[0] is bool boolValue0 && values[1] is bool boolValue1 && boolValue0 == boolValue1) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
|
|
public class LTConverter : IValueConverter, IMultiValueConverter
|
|
{
|
|
public object TrueValue { get; set; } = true;
|
|
public object FalseValue { get; set; } = false;
|
|
|
|
public enum ConverterType { INT, DOUBLE, FLOAT }
|
|
public object CompareValue { get; set; }
|
|
public ConverterType CompareValueType { get; set; } = ConverterType.DOUBLE;
|
|
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
return Convert(value);
|
|
}
|
|
|
|
private object Convert(object value)
|
|
{
|
|
if (CompareValue == null) { throw new NotImplementedException(); }
|
|
|
|
switch (CompareValueType) {
|
|
case ConverterType.INT:
|
|
return System.Convert.ToInt32(CompareValue) < System.Convert.ToInt32(value) ? TrueValue : FalseValue;
|
|
case ConverterType.DOUBLE:
|
|
return System.Convert.ToDouble(CompareValue) < System.Convert.ToDouble(value) ? TrueValue : FalseValue;
|
|
case ConverterType.FLOAT:
|
|
return System.Convert.ToSingle(CompareValue) < System.Convert.ToSingle(value) ? TrueValue : FalseValue;
|
|
default:
|
|
return CompareValue.Equals(value) ? TrueValue : FalseValue;
|
|
}
|
|
}
|
|
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (values == null || values.Length < 2) { throw new NotImplementedException(); }
|
|
|
|
CompareValue = values[1];
|
|
|
|
return Convert(values[0]);
|
|
}
|
|
|
|
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
|
|
public class GTConverter : IValueConverter, IMultiValueConverter
|
|
{
|
|
public object TrueValue { get; set; } = true;
|
|
public object FalseValue { get; set; } = false;
|
|
|
|
public enum ConverterType { INT, DOUBLE, FLOAT }
|
|
public object CompareValue { get; set; }
|
|
public ConverterType CompareValueType { get; set; } = ConverterType.DOUBLE;
|
|
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
return Convert(value);
|
|
}
|
|
|
|
private object Convert(object value)
|
|
{
|
|
if (CompareValue == null) { throw new NotImplementedException(); }
|
|
|
|
switch (CompareValueType) {
|
|
case ConverterType.INT:
|
|
return System.Convert.ToInt32(CompareValue) > System.Convert.ToInt32(value) ? TrueValue : FalseValue;
|
|
case ConverterType.DOUBLE:
|
|
return System.Convert.ToDouble(CompareValue) > System.Convert.ToDouble(value) ? TrueValue : FalseValue;
|
|
case ConverterType.FLOAT:
|
|
return System.Convert.ToSingle(CompareValue) > System.Convert.ToSingle(value) ? TrueValue : FalseValue;
|
|
default:
|
|
return CompareValue.Equals(value) ? TrueValue : FalseValue;
|
|
}
|
|
}
|
|
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (values == null || values.Length < 2) { throw new NotImplementedException(); }
|
|
|
|
CompareValue = values[1];
|
|
|
|
return Convert(values[0]);
|
|
}
|
|
|
|
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 범위 내에 들어있는지 체크 (CompareValueLow <= value && value <= CompareValueHigh) -- EQ 포함 여부는 CompareValueLow / CompareValueHigh로 세팅 (default 포함)
|
|
/// </summary>
|
|
public class InRangeConverter : IValueConverter, IMultiValueConverter
|
|
{
|
|
public object TrueValue { get; set; } = true;
|
|
public object FalseValue { get; set; } = false;
|
|
|
|
public enum ConverterType { INT, DOUBLE, FLOAT }
|
|
public object CompareValueLow { get; set; }
|
|
public object CompareValueHigh { get; set; }
|
|
|
|
public bool IncludeLowEq { get; set; } = true;
|
|
public bool IncludeHighEq { get; set; } = true;
|
|
|
|
public ConverterType CompareValueType { get; set; } = ConverterType.DOUBLE;
|
|
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
return Convert(value);
|
|
}
|
|
|
|
private object Convert(object value)
|
|
{
|
|
if (CompareValueLow == null && CompareValueHigh == null) { throw new NotImplementedException(); }
|
|
|
|
bool result = true;
|
|
switch (CompareValueType) {
|
|
case ConverterType.INT: {
|
|
bool ltCheck = CompareValueLow == null || (IncludeLowEq ? (System.Convert.ToInt32(CompareValueLow) <= System.Convert.ToInt32(value)) : (System.Convert.ToInt32(CompareValueLow) < System.Convert.ToInt32(value)));
|
|
bool gtCheck = CompareValueHigh == null || (IncludeHighEq ? (System.Convert.ToInt32(CompareValueHigh) >= System.Convert.ToInt32(value)) : (System.Convert.ToInt32(CompareValueHigh) > System.Convert.ToInt32(value)));
|
|
result = ltCheck && gtCheck;
|
|
break;
|
|
}
|
|
case ConverterType.DOUBLE: {
|
|
bool ltCheck = CompareValueLow == null || (IncludeLowEq ? (System.Convert.ToDouble(CompareValueLow) <= System.Convert.ToDouble(value)) : (System.Convert.ToDouble(CompareValueLow) < System.Convert.ToDouble(value)));
|
|
bool gtCheck = CompareValueHigh == null || (IncludeHighEq ? (System.Convert.ToDouble(CompareValueHigh) >= System.Convert.ToDouble(value)) : (System.Convert.ToDouble(CompareValueHigh) > System.Convert.ToDouble(value)));
|
|
result = ltCheck && gtCheck;
|
|
break;
|
|
}
|
|
case ConverterType.FLOAT: {
|
|
bool ltCheck = CompareValueLow == null || (IncludeLowEq ? (System.Convert.ToSingle(CompareValueLow) <= System.Convert.ToSingle(value)) : (System.Convert.ToSingle(CompareValueLow) < System.Convert.ToSingle(value)));
|
|
bool gtCheck = CompareValueHigh == null || (IncludeHighEq ? (System.Convert.ToSingle(CompareValueHigh) >= System.Convert.ToSingle(value)) : (System.Convert.ToSingle(CompareValueHigh) > System.Convert.ToSingle(value)));
|
|
result = ltCheck && gtCheck;
|
|
break;
|
|
}
|
|
default:
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
return result ? TrueValue : FalseValue;
|
|
}
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (values == null || values.Length < 3) { throw new NotImplementedException(); }
|
|
|
|
CompareValueLow = values[1];
|
|
CompareValueHigh = values[2];
|
|
|
|
return Convert(values[0]);
|
|
}
|
|
|
|
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
|
|
public class MinConverter : IMultiValueConverter
|
|
{
|
|
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (values != null && values.Length > 0) {
|
|
if (targetType == typeof(double)) {
|
|
double result = (double)values[0];
|
|
|
|
for (int i = 1; i < values.Length; i++) {
|
|
if ((double)values[i] < result) result = (double)values[i];
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
if (targetType == typeof(int)) {
|
|
int result = (int)values[0];
|
|
|
|
for (int i = 1; i < values.Length; i++) {
|
|
if ((int)values[i] < result) result = (int)values[i];
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
|
|
public class NullCheckConverter : EqualConverter
|
|
{
|
|
public new object CompareValue { get; set; } = null;
|
|
}
|
|
}
|